aboutsummaryrefslogtreecommitdiff
path: root/service/avrcp_target.cc
blob: fb63fba38d338ce97d30d05b8cd2eb5217925ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
//  Copyright (C) 2017 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#include "service/avrcp_target.h"

#include <algorithm>
#include <cerrno>
#include <climits>
#include <string>

#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "service/logging_helpers.h"

#include "stack/include/avrc_defs.h"

#define PARSE_ADDR(str)                                        \
  ({                                                           \
    RawAddress tmp;                                            \
    if (!RawAddress::FromString((str), tmp)) {                 \
      LOG(ERROR) << "Invalid device address given: " << (str); \
      return false;                                            \
    }                                                          \
    tmp;                                                       \
  })

#define TRY_RET(expr, err_msg) \
  do {                         \
    if (!(expr)) {             \
      LOG(ERROR) << err_msg;   \
      return false;            \
    }                          \
    return true;               \
  } while (0)

#define TRY_RET_FUNC(expr) TRY_RET(expr, __func__ << " failed")

using LockGuard = std::lock_guard<std::mutex>;

namespace bluetooth {

namespace {

std::vector<btrc_player_setting_text_t> StringValueToPlayerSettingsText(
    const std::vector<AvrcpStringValue>& attrs) {
  std::vector<btrc_player_setting_text_t> btrc_attrs(attrs.size());
  for (size_t i = 0; i < attrs.size(); ++i) {
    btrc_attrs[i].id = attrs[i].id();
    std::string str(attrs[i].value());
    size_t to_copy = std::min(sizeof(btrc_attrs[i].text) - 1, str.size());
    if (to_copy < str.size()) {
      LOG(WARNING) << "Value truncated";
    }

    memcpy(btrc_attrs[i].text, str.data(), to_copy);
    btrc_attrs[i].text[to_copy] = '\0';
  }

  return btrc_attrs;
}

std::vector<btrc_element_attr_val_t> StringValueToElementAttrVal(
    const std::vector<AvrcpStringValue>& attrs) {
  std::vector<btrc_element_attr_val_t> btrc_attrs(attrs.size());
  for (size_t i = 0; i < attrs.size(); ++i) {
    btrc_attrs[i].attr_id = attrs[i].id();
    std::string str(attrs[i].value());
    size_t to_copy = std::min(sizeof(btrc_attrs[i].text) - 1, str.size());
    if (to_copy < str.size()) {
      LOG(WARNING) << "Value truncated";
    }

    memcpy(btrc_attrs[i].text, str.data(), to_copy);
    btrc_attrs[i].text[to_copy] = '\0';
  }

  return btrc_attrs;
}

}  // namespace

// static
const int AvrcpTarget::kSingletonInstanceId = 0;

AvrcpTarget::AvrcpTarget(const Uuid& uuid) : app_identifier_(uuid) {
  hal::BluetoothAvrcpInterface::Get()->AddTargetObserver(this);
}

AvrcpTarget::~AvrcpTarget() {
  hal::BluetoothAvrcpInterface::Get()->RemoveTargetObserver(this);
}

const Uuid& AvrcpTarget::GetAppIdentifier() const { return app_identifier_; }

int AvrcpTarget::GetInstanceId() const { return kSingletonInstanceId; }

void AvrcpTarget::SetDelegate(Delegate* delegate) {
  LockGuard lock(delegate_mutex_);
  delegate_ = delegate;
}

bool AvrcpTarget::Enable() {
  LockGuard lock(mutex_);
  return hal::BluetoothAvrcpInterface::Get()->AvrcpTargetEnable();
}

void AvrcpTarget::Disable() {
  LockGuard lock(mutex_);
  hal::BluetoothAvrcpInterface::Get()->AvrcpTargetDisable();
}

bool AvrcpTarget::GetPlayStatusResponse(const std::string& str_addr,
                                        int32_t play_status, uint32_t song_len,
                                        uint32_t song_pos) {
  RawAddress addr = PARSE_ADDR(str_addr);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->get_play_status_rsp(
                       addr, static_cast<btrc_play_status_t>(play_status),
                       song_len, song_pos) == BT_STATUS_SUCCESS);
}

bool AvrcpTarget::ListPlayerAppAttrResponse(const std::string& str_addr,
                                            const std::vector<int32_t>& attrs) {
  RawAddress addr = PARSE_ADDR(str_addr);

  std::vector<btrc_player_attr_t> btrc_attrs;
  btrc_attrs.reserve(attrs.size());
  for (auto attr : attrs) {
    btrc_attrs.push_back(static_cast<btrc_player_attr_t>(attr));
  }

  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->list_player_app_attr_rsp(addr, btrc_attrs.size(),
                                              btrc_attrs.data()) ==
               BT_STATUS_SUCCESS);
}

bool AvrcpTarget::GetPlayerAppValueResponse(
    const std::string& str_addr, const std::vector<AvrcpIntValue>& values) {
  RawAddress addr = PARSE_ADDR(str_addr);
  btrc_player_settings_t btrc_values;
  if (values.size() >= arraysize(btrc_values.attr_ids)) {
    LOG(ERROR) << "Too many attribute values";
    return false;
  }

  btrc_values.num_attr = values.size();
  for (size_t i = 0; i < values.size(); ++i) {
    btrc_values.attr_ids[i] = values[i].id();
    btrc_values.attr_values[i] = values[i].value();
  }

  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->get_player_app_value_rsp(addr, &btrc_values) ==
               BT_STATUS_SUCCESS);
}

bool AvrcpTarget::GetPlayerAppAttrTextResponse(
    const std::string& str_addr, const std::vector<AvrcpStringValue>& attrs) {
  RawAddress addr = PARSE_ADDR(str_addr);
  auto btrc_attrs = StringValueToPlayerSettingsText(attrs);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->get_player_app_attr_text_rsp(addr, btrc_attrs.size(),
                                                  btrc_attrs.data()) ==
               BT_STATUS_SUCCESS);
}

bool AvrcpTarget::GetPlayerAppValueTextResponse(
    const std::string& str_addr, const std::vector<AvrcpStringValue>& values) {
  RawAddress addr = PARSE_ADDR(str_addr);
  auto btrc_values = StringValueToPlayerSettingsText(values);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->get_player_app_value_text_rsp(addr, btrc_values.size(),
                                                   btrc_values.data()) ==
               BT_STATUS_SUCCESS);
}

bool AvrcpTarget::GetElementAttrResponse(
    const std::string& str_addr, const std::vector<AvrcpStringValue>& attrs) {
  RawAddress addr = PARSE_ADDR(str_addr);
  auto btrc_attrs = StringValueToElementAttrVal(attrs);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(
      hal::BluetoothAvrcpInterface::Get()
          ->GetTargetHALInterface()
          ->get_element_attr_rsp(addr, btrc_attrs.size(), btrc_attrs.data()) ==
      BT_STATUS_SUCCESS);
}

bool AvrcpTarget::SetPlayerAppValueResponse(const std::string& str_addr,
                                            int32_t rsp_status) {
  RawAddress addr = PARSE_ADDR(str_addr);
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->set_player_app_value_rsp(
                       addr, static_cast<btrc_status_t>(rsp_status)) ==
               BT_STATUS_SUCCESS);
}

bool AvrcpTarget::RegisterNotificationResponse(
    int32_t event_id, int32_t type,
    const AvrcpRegisterNotificationResponse& param) {
  auto param_copy = param.data();
  LockGuard lock(mutex_);
  TRY_RET_FUNC(hal::BluetoothAvrcpInterface::Get()
                   ->GetTargetHALInterface()
                   ->register_notification_rsp(
                       static_cast<btrc_event_id_t>(event_id),
                       static_cast<btrc_notification_type_t>(type),
                       &param_copy) == BT_STATUS_SUCCESS);
}

bool AvrcpTarget::SetVolume(int volume) {
  LockGuard lock(mutex_);
  TRY_RET_FUNC(
      hal::BluetoothAvrcpInterface::Get()->GetTargetHALInterface()->set_volume(
          volume) == BT_STATUS_SUCCESS);
}

void AvrcpTarget::RemoteFeaturesCallback(const RawAddress& bd_addr,
                                         btrc_remote_features_t features) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnGetRemoteFeatures(str_addr, static_cast<int32_t>(features));
}

void AvrcpTarget::GetPlayStatusCallback(const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnGetPlayStatus(str_addr);
}

void AvrcpTarget::ListPlayerAppAttrCallback(const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnListPlayerAppAttr(str_addr);
}

void AvrcpTarget::ListPlayerAppValuesCallback(btrc_player_attr_t attr_id,
                                              const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnListPlayerAppValues(str_addr, static_cast<int32_t>(attr_id));
}

void AvrcpTarget::GetPlayerAppValueCallback(uint8_t num_attr,
                                            btrc_player_attr_t* p_attrs,
                                            const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  std::vector<int32_t> attr_vec;
  attr_vec.reserve(num_attr);
  for (auto* it = p_attrs; it != p_attrs + num_attr; ++it) {
    attr_vec.push_back(*it);
  }

  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnGetPlayerAppValue(str_addr, attr_vec);
}

void AvrcpTarget::GetPlayerAppAttrsTextCallback(uint8_t num_attr,
                                                btrc_player_attr_t* p_attrs,
                                                const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  std::vector<int32_t> attr_vec;
  attr_vec.reserve(num_attr);
  for (auto* it = p_attrs; it != p_attrs + num_attr; ++it) {
    attr_vec.push_back(*it);
  }

  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnGetPlayerAppAttrsText(str_addr, attr_vec);
}

void AvrcpTarget::GetPlayerAppValuesTextCallback(uint8_t attr_id,
                                                 uint8_t num_val,
                                                 uint8_t* p_vals,
                                                 const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  std::vector<int32_t> val_vec;
  val_vec.reserve(num_val);
  for (auto* it = p_vals; it != p_vals + num_val; ++it) {
    val_vec.push_back(*it);
  }
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnGetPlayerAppValuesText(str_addr, attr_id, val_vec);
}

void AvrcpTarget::SetPlayerAppValueCallback(btrc_player_settings_t* p_vals,
                                            const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  std::vector<AvrcpIntValue> values;
  values.reserve(p_vals->num_attr);
  for (size_t i = 0; i < p_vals->num_attr; ++i) {
    values.emplace_back(p_vals->attr_ids[i], p_vals->attr_values[i]);
  }

  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnSetPlayerAppValue(str_addr, values);
}

void AvrcpTarget::GetElementAttrCallback(uint8_t num_attr,
                                         btrc_media_attr_t* p_attrs,
                                         const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  std::vector<int32_t> attr_vec;
  attr_vec.reserve(num_attr);
  for (auto* it = p_attrs; it != p_attrs + num_attr; ++it) {
    attr_vec.push_back(*it);
  }
  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnGetElementAttrs(str_addr, attr_vec);
}

void AvrcpTarget::RegisterNotificationCallback(btrc_event_id_t event_id,
                                               uint32_t param,
                                               const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnRegisterNotification(str_addr, static_cast<int32_t>(event_id),
                                      param);
}

void AvrcpTarget::VolumeChangeCallback(uint8_t volume, uint8_t ctype,
                                       const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnVolumeChange(str_addr, volume, ctype);
}

void AvrcpTarget::PassthroughCmdCallback(int id, int key_state,
                                         const RawAddress& bd_addr) {
  auto str_addr = BtAddrString(&bd_addr);
  LockGuard lock(delegate_mutex_);
  if (delegate_) delegate_->OnPassThroughCommand(str_addr, id, key_state);
}

// AvrcpTargetFactory implementation
// ========================================================

AvrcpTargetFactory::AvrcpTargetFactory() = default;
AvrcpTargetFactory::~AvrcpTargetFactory() = default;

bool AvrcpTargetFactory::RegisterInstance(const Uuid& uuid,
                                          const RegisterCallback& callback) {
  VLOG(1) << __func__ << " - UUID: " << uuid.ToString();

  auto avrcp_target = base::WrapUnique(new AvrcpTarget(uuid));
  callback(BLE_STATUS_SUCCESS, uuid, std::move(avrcp_target));
  return true;
}

}  // namespace bluetooth