1 //
2 //  Copyright (C) 2015 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //
16 
17 #include <base.h>
18 #include <base/at_exit.h>
19 #include <base/command_line.h>
20 #include <base/logging.h>
21 #include <base/macros.h>
22 #include <base/strings/string_split.h>
23 #include <base/strings/string_util.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/ProcessState.h>
26 
27 #include "bluetooth_binder_facade.h"
28 #include <service/common/bluetooth/binder/IBluetooth.h>
29 #include <service/common/bluetooth/binder/IBluetoothCallback.h>
30 #include <service/common/bluetooth/binder/IBluetoothLowEnergy.h>
31 #include <service/common/bluetooth/low_energy_constants.h>
32 #include <tuple>
33 
34 using android::sp;
35 using ipc::binder::IBluetooth;
36 using ipc::binder::IBluetoothLowEnergy;
37 
38 std::atomic_bool ble_registering(false);
39 std::atomic_int ble_client_id(0);
40 
SharedValidator()41 bool BluetoothBinderFacade::SharedValidator() {
42   if (bt_iface == NULL) {
43     LOG(ERROR) << sl4n::kTagStr << " IBluetooth interface not initialized";
44     return false;
45   }
46   if (!bt_iface->IsEnabled()) {
47     LOG(ERROR) << sl4n::kTagStr << " IBluetooth interface not enabled";
48     return false;
49   }
50   return true;
51 }
52 
BluetoothBinderEnable()53 std::tuple<bool, int> BluetoothBinderFacade::BluetoothBinderEnable() {
54   if (bt_iface == NULL) {
55     LOG(ERROR) << sl4n::kTagStr << ": IBluetooth interface not enabled";
56     return std::make_tuple(false, sl4n_error_codes::kFailInt);
57   }
58   bool result = bt_iface->Enable(false);
59   if (!result) {
60     LOG(ERROR) << sl4n::kTagStr << ": Failed to enable the Bluetooth service";
61     return std::make_tuple(false, sl4n_error_codes::kPassInt);
62   } else {
63     return std::make_tuple(true, sl4n_error_codes::kPassInt);
64   }
65 }
66 
BluetoothBinderGetAddress()67 std::tuple<std::string, int> BluetoothBinderFacade::BluetoothBinderGetAddress() {
68   if (!SharedValidator()) {
69     return std::make_tuple(sl4n::kFailStr, sl4n_error_codes::kFailInt);
70   }
71   return std::make_tuple(bt_iface->GetAddress(), sl4n_error_codes::kPassInt);
72 }
73 
BluetoothBinderGetName()74 std::tuple<std::string, int> BluetoothBinderFacade::BluetoothBinderGetName() {
75   if (!SharedValidator()) {
76     return std::make_tuple(sl4n::kFailStr,sl4n_error_codes::kFailInt);
77   }
78   std::string name = bt_iface->GetName();
79   if (name.empty()) {
80     LOG(ERROR) << sl4n::kTagStr << ": Failed to get device name";
81     return std::make_tuple(sl4n::kFailStr, sl4n_error_codes::kFailInt);
82   } else {
83     return std::make_tuple(name, sl4n_error_codes::kPassInt);
84   }
85 }
86 
BluetoothBinderSetName(std::string name)87 std::tuple<bool, int> BluetoothBinderFacade::BluetoothBinderSetName(
88   std::string name) {
89 
90   if (!SharedValidator()) {
91     return std::make_tuple(false, sl4n_error_codes::kFailInt);
92   }
93   bool result = bt_iface->SetName(name);
94   if (!result) {
95     LOG(ERROR) << sl4n::kTagStr << ": Failed to set device name";
96     return std::make_tuple(false, sl4n_error_codes::kFailInt);
97   }
98   return std::make_tuple(true, sl4n_error_codes::kPassInt);
99 }
100 
BluetoothBinderInitInterface()101 std::tuple<bool, int> BluetoothBinderFacade::BluetoothBinderInitInterface() {
102   bt_iface = IBluetooth::getClientInterface();
103   if(!bt_iface.get()) {
104     LOG(ERROR) << sl4n::kTagStr <<
105       ": Failed to initialize IBluetooth interface";
106     return std::make_tuple(false, sl4n_error_codes::kFailInt);
107   }
108   return std::make_tuple(true, sl4n_error_codes::kPassInt);
109 }
110 
BluetoothBinderRegisterBLE()111 std::tuple<bool, int> BluetoothBinderFacade::BluetoothBinderRegisterBLE() {
112   // TODO (tturney): verify bt_iface initialized everywhere
113   if (!SharedValidator()) {
114     return std::make_tuple(false, sl4n_error_codes::kFailInt);
115   }
116   ble_iface = bt_iface->GetLowEnergyInterface();
117   if(!ble_iface.get()) {
118     LOG(ERROR) << sl4n::kTagStr << ": Failed to register BLE";
119     return std::make_tuple(false, sl4n_error_codes::kFailInt);
120   }
121   return std::make_tuple(true, sl4n_error_codes::kPassInt);
122 }
123 
BluetoothBinderSetAdvSettings(int mode,int timeout_seconds,int tx_power_level,bool is_connectable)124 std::tuple<int, int> BluetoothBinderFacade::BluetoothBinderSetAdvSettings(
125   int mode, int timeout_seconds, int tx_power_level, bool is_connectable) {
126   if (!SharedValidator()) {
127     return std::make_tuple(false,sl4n_error_codes::kFailInt);
128   }
129   bluetooth::AdvertiseSettings::Mode adv_mode;
130   switch (mode) {
131     case sl4n_ble::kAdvSettingsModeLowPowerInt :
132       adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_LOW_POWER;
133     case sl4n_ble::kAdvSettingsModeBalancedInt :
134       adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_BALANCED;
135     case sl4n_ble::kAdvSettingsModeLowLatencyInt :
136       adv_mode = bluetooth::AdvertiseSettings::Mode::MODE_LOW_LATENCY;
137     default :
138       LOG(ERROR) << sl4n::kTagStr <<
139         ": Input mode is outside the accepted values";
140       return std::make_tuple(
141         sl4n::kFailedCounterInt, sl4n_error_codes::kFailInt);
142   }
143 
144   base::TimeDelta adv_timeout = base::TimeDelta::FromSeconds(
145     timeout_seconds);
146 
147   bluetooth::AdvertiseSettings::TxPowerLevel adv_tx_power_level;
148   switch (tx_power_level) {
149     case sl4n_ble::kAdvSettingsTxPowerLevelUltraLowInt: tx_power_level =
150       bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_ULTRA_LOW;
151     case sl4n_ble::kAdvSettingsTxPowerLevelLowInt: tx_power_level =
152       bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_LOW;
153     case sl4n_ble::kAdvSettingsTxPowerLevelMediumInt: tx_power_level =
154       bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_MEDIUM;
155     case sl4n_ble::kAdvSettingsTxPowerLevelHighInt: tx_power_level =
156       bluetooth::AdvertiseSettings::TxPowerLevel::TX_POWER_LEVEL_HIGH;
157     default :
158       LOG(ERROR) << sl4n::kTagStr <<
159         ": Input tx power level is outside the accepted values";
160       return std::make_tuple(
161         sl4n::kFailedCounterInt, sl4n_error_codes::kFailInt);
162   }
163 
164   bluetooth::AdvertiseSettings adv_settings = bluetooth::AdvertiseSettings(
165     adv_mode, adv_timeout, adv_tx_power_level, is_connectable);
166   adv_settings_map[adv_settings_count] = adv_settings;
167   int adv_settings_id = adv_settings_count;
168   adv_settings_count++;
169   return std::make_tuple(adv_settings_id, sl4n_error_codes::kPassInt);
170 }
171 
BluetoothBinderFacade()172 BluetoothBinderFacade::BluetoothBinderFacade() {
173   adv_settings_count = 0;
174   manu_data_count = 0;
175 }
176