summaryrefslogtreecommitdiff
path: root/sl4n/facades/bluetooth/bt_binder_facade.cpp
blob: b764ef4d07038d8c83288b7a26ba73b2159537d9 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
//  Copyright (C) 2016 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 <base.h>
#include <base/at_exit.h>
#include <base/command_line.h>
#include <base/logging.h>
#include <base/macros.h>
#include <base/strings/string_split.h>
#include <base/strings/string_util.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>

#include "bt_binder_facade.h"
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <android/bluetooth/IBluetooth.h>
#include <android/bluetooth/IBluetoothCallback.h>
#include <android/bluetooth/IBluetoothLowEnergy.h>
#include <service/common/bluetooth/low_energy_constants.h>
#include <tuple>
#include <utils/command_receiver.h>
#include <utils/common_utils.h>

using android::bluetooth::IBluetooth;
using android::bluetooth::IBluetoothLowEnergy;
using android::getService;
using android::OK;
using android::sp;
using android::String8;
using android::String16;

std::atomic_bool ble_registering(false);
std::atomic_int ble_client_id(0);

std::string kServiceName = "bluetooth-service";

bool BtBinderFacade::SharedValidator() {
  if (bt_iface == NULL) {
    LOG(ERROR) << sl4n::kTagStr << " IBluetooth interface not initialized";
    return false;
  }
  bool ret;
  bt_iface->IsEnabled(&ret);
  if (!ret) {
    LOG(ERROR) << sl4n::kTagStr << " IBluetooth interface not enabled";
    return false;
  }
  return true;
}

std::tuple<bool, int> BtBinderFacade::BtBinderEnable() {
  if (bt_iface == NULL) {
    LOG(ERROR) << sl4n::kTagStr << ": IBluetooth interface not enabled";
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }
  bool ret;
  bt_iface->Enable(&ret);
  if (!ret) {
    LOG(ERROR) << sl4n::kTagStr << ": Failed to enable the Bluetooth service";
    return std::make_tuple(false, sl4n_error_codes::kPassInt);
  } else {
    return std::make_tuple(true, sl4n_error_codes::kPassInt);
  }
}

std::tuple<std::string, int> BtBinderFacade::BtBinderGetAddress() {
  if (!SharedValidator()) {
    return std::make_tuple(sl4n::kFailStr, sl4n_error_codes::kFailInt);
  }
  String16 address;
  bt_iface->GetAddress(&address);
  return std::make_tuple(std::string(String8(address).string()), sl4n_error_codes::kPassInt);
}

std::tuple<std::string, int> BtBinderFacade::BtBinderGetName() {
  if (!SharedValidator()) {
    return std::make_tuple(sl4n::kFailStr,sl4n_error_codes::kFailInt);
  }

  String16 name16;
  bt_iface->GetName(&name16);
  std::string name = std::string(String8(name16).string());
  if (name.empty()) {
    LOG(ERROR) << sl4n::kTagStr << ": Failed to get device name";
    return std::make_tuple(sl4n::kFailStr, sl4n_error_codes::kFailInt);
  } else {
    return std::make_tuple(name, sl4n_error_codes::kPassInt);
  }
}

std::tuple<bool, int> BtBinderFacade::BtBinderSetName(
  std::string name) {

  if (!SharedValidator()) {
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }

  bool result;
  bt_iface->SetName(String16(String8(name.c_str())), &result);
  if (!result) {
    LOG(ERROR) << sl4n::kTagStr << ": Failed to set device name";
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }
  return std::make_tuple(true, sl4n_error_codes::kPassInt);
}

std::tuple<bool, int> BtBinderFacade::BtBinderInitInterface() {
  status_t status = getService(String16(kServiceName.c_str()), &bt_iface);
  if (status != OK) {
    LOG(ERROR) << "Failed to get service binder: '" << kServiceName
               << "' status=" << status;
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }
  return std::make_tuple(true, sl4n_error_codes::kPassInt);
}

std::tuple<bool, int> BtBinderFacade::BtBinderRegisterBLE() {
  // TODO (tturney): verify bt_iface initialized everywhere
  if (!SharedValidator()) {
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }
  bt_iface->GetLowEnergyInterface(&ble_iface);
  if(!ble_iface.get()) {
    LOG(ERROR) << sl4n::kTagStr << ": Failed to register BLE";
    return std::make_tuple(false, sl4n_error_codes::kFailInt);
  }
  return std::make_tuple(true, sl4n_error_codes::kPassInt);
}

std::tuple<int, int> BtBinderFacade::BtBinderSetAdvSettings(
  int mode, int timeout_seconds, int tx_power_level, bool is_connectable) {
  if (!SharedValidator()) {
    return std::make_tuple(false,sl4n_error_codes::kFailInt);
  }
  bluetooth::AdvertiseSettings::Mode adv_mode;
  switch (mode) {
    case sl4n_ble::kAdvSettingsModeLowPowerInt :
      adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_LOW_POWER;
      break;
    case sl4n_ble::kAdvSettingsModeBalancedInt :
      adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_BALANCED;
      break;
    case sl4n_ble::kAdvSettingsModeLowLatencyInt :
      adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_LOW_LATENCY;
      break;
    default :
      LOG(ERROR) << sl4n::kTagStr <<
        ": Input mode is outside the accepted values";
      return std::make_tuple(
        sl4n::kFailedCounterInt, sl4n_error_codes::kFailInt);
  }

  base::TimeDelta adv_timeout = base::TimeDelta::FromSeconds(
    timeout_seconds);

  bluetooth::AdvertiseSettings::TxPowerLevel adv_tx_power_level;
  switch (tx_power_level) {
    case sl4n_ble::kAdvSettingsTxPowerLevelUltraLowInt: adv_tx_power_level =
      bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_ULTRA_LOW;
      break;
    case sl4n_ble::kAdvSettingsTxPowerLevelLowInt: adv_tx_power_level =
      bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_LOW;
      break;
    case sl4n_ble::kAdvSettingsTxPowerLevelMediumInt: adv_tx_power_level =
      bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_MEDIUM;
      break;
    case sl4n_ble::kAdvSettingsTxPowerLevelHighInt: adv_tx_power_level =
      bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_HIGH;
      break;
    default :
      LOG(ERROR) << sl4n::kTagStr <<
        ": Input tx power level is outside the accepted values";
      return std::make_tuple(
        sl4n::kFailedCounterInt, sl4n_error_codes::kFailInt);
  }

  bluetooth::AdvertiseSettings adv_settings = bluetooth::AdvertiseSettings(
    adv_mode, adv_timeout, adv_tx_power_level, is_connectable);
  adv_settings_map[adv_settings_count] = adv_settings;
  int adv_settings_id = adv_settings_count;
  adv_settings_count++;
  return std::make_tuple(adv_settings_id, sl4n_error_codes::kPassInt);
}

//////////////////
// wrappers
//////////////////

static BtBinderFacade facade;  // triggers registration with CommandReceiver

void bt_binder_get_local_name_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  //check for kfailedstr or NULL???
  std::string name;
  int error_code;
  std::tie(name, error_code) = facade.BtBinderGetName();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, sl4n::kFailStr, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
    return;
  }
  rapidjson::Value tmp;
  tmp.SetString(name.c_str(), doc.GetAllocator());
  doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator());
  doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  return;
}

void bt_binder_init_interface_wapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  bool init_result;
  int error_code;
  std::tie(init_result, error_code) = facade.BtBinderInitInterface();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
  doc.AddMember(sl4n::kResultStr, init_result, doc.GetAllocator());
  return;
}

void bt_binder_set_local_name_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 1;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  std::string name;
  if (!doc[sl4n::kParamsStr][0].IsString()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected String input for name";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
    return;
  } else {
    name = doc[sl4n::kParamsStr][0].GetString();
  }
  bool set_result;
  int error_code;
  std::tie(set_result, error_code) = facade.BtBinderSetName(name);
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, set_result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
  return;
}

void bt_binder_get_local_address_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  //check for kfailedstr or NULL???
  std::string address;
  int error_code;
  std::tie(address, error_code) = facade.BtBinderGetAddress();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, sl4n::kFailStr, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    rapidjson::Value tmp;
    tmp.SetString(address.c_str(), doc.GetAllocator());
    doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
  return;
}

void bt_binder_enable_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  bool enable_result;
  int error_code;
  std::tie(enable_result, error_code) = facade.BtBinderEnable();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, enable_result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void bt_binder_register_ble_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 0;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  bool register_result;
  int error_code;
  std::tie(register_result, error_code) =
    facade.BtBinderRegisterBLE();
  if (error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator());
  } else {
    doc.AddMember(sl4n::kResultStr, register_result, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

void bt_binder_set_adv_settings_wrapper(rapidjson::Document &doc) {
  int expected_param_size = 4;
  if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) {
    return;
  }
  int mode;
  int timeout_seconds;
  int tx_power_level;
  bool is_connectable;
  // TODO(tturney) Verify inputs better
  if (!doc[sl4n::kParamsStr][0].IsInt()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for mode";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator());
    return;
  } else {
    mode = doc[sl4n::kParamsStr][0].GetInt();
  }
  if (!doc[sl4n::kParamsStr][1].IsInt()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for timeout";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator());
    return;
  } else {
    timeout_seconds = doc[sl4n::kParamsStr][1].GetInt();
  }
  if (!doc[sl4n::kParamsStr][2].IsInt()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for tx power level";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator());
    return;
  } else {
    tx_power_level = doc[sl4n::kParamsStr][2].GetInt();
  }
  if (!doc[sl4n::kParamsStr][3].IsBool()) {
    LOG(ERROR) << sl4n::kTagStr << ": Expected Bool input for connectable";
    doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator());
    return;
  } else {
    is_connectable = doc[sl4n::kParamsStr][3].GetBool();
  }

  int adv_settings;
  int error_code;
  std::tie(adv_settings, error_code) = facade.BtBinderSetAdvSettings(
    mode, timeout_seconds, tx_power_level, is_connectable);
  if(error_code == sl4n_error_codes::kFailInt) {
    doc.AddMember(
      sl4n::kResultStr, sl4n_error_codes::kFailInt, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, sl4n::kFailedCounterInt, doc.GetAllocator());
    return;
  } else {
    doc.AddMember(sl4n::kResultStr, adv_settings, doc.GetAllocator());
    doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator());
  }
}

////////////////
// constructor
////////////////

BtBinderFacade::BtBinderFacade() {
  adv_settings_count = 0;
  manu_data_count = 0;

  CommandReceiver::RegisterCommand("BtBinderInitInterface",
    &bt_binder_init_interface_wapper);
  CommandReceiver::RegisterCommand("BtBinderGetName",
    &bt_binder_get_local_name_wrapper);
  CommandReceiver::RegisterCommand("BtBinderSetName",
    &bt_binder_set_local_name_wrapper);
  CommandReceiver::RegisterCommand("BtBinderGetAddress",
    &bt_binder_get_local_address_wrapper);
  CommandReceiver::RegisterCommand("BtBinderEnable",
    &bt_binder_enable_wrapper);
  CommandReceiver::RegisterCommand("BtBinderRegisterBLE",
    &bt_binder_register_ble_wrapper);
  CommandReceiver::RegisterCommand("BtBinderSetAdvSettings",
    &bt_binder_set_adv_settings_wrapper);
}