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 <rapidjson/document.h> 18 #include <rapidjson/writer.h> 19 #include <rapidjson/stringbuffer.h> 20 #include <map> 21 #include <string> 22 #include <stdio.h> 23 #include <tuple> 24 25 #include <base.h> 26 #include <facades/bluetooth/bluetooth_binder_facade.h> 27 #include <service/common/bluetooth/binder/IBluetooth.h> 28 #include <utils/command_receiver.h> 29 #include <utils/common_utils.h> 30 31 using android::sp; 32 using ipc::binder::IBluetooth; 33 34 typedef std::map<std::string, MFP> function_map; 35 function_map* _funcMap = NULL; 36 BluetoothBinderFacade bt_binder; 37 38 void _clean_result(rapidjson::Document &doc) { 39 doc.RemoveMember(sl4n::kMethodStr); 40 doc.RemoveMember(sl4n::kParamsStr); 41 } 42 43 void initiate(rapidjson::Document &doc) { 44 doc.AddMember(sl4n::kStatusStr, sl4n::kSuccessStr, doc.GetAllocator()); 45 } 46 47 // Begin Wrappers ... I'm the hiphopopotamus my lyrics are bottomless... 48 void bluetooth_binder_get_local_name_wrapper(rapidjson::Document &doc) { 49 int expected_param_size = 0; 50 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 51 return; 52 } 53 //check for kfailedstr or NULL??? 54 std::string name; 55 int error_code; 56 std::tie(name, error_code) = bt_binder.BluetoothBinderGetName(); 57 if (error_code == sl4n_error_codes::kFailInt) { 58 doc.AddMember(sl4n::kResultStr, sl4n::kFailStr, doc.GetAllocator()); 59 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 60 return; 61 } 62 rapidjson::Value tmp; 63 tmp.SetString(name.c_str(), doc.GetAllocator()); 64 doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator()); 65 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 66 return; 67 } 68 69 void bluetooth_binder_init_interface_wapper(rapidjson::Document &doc) { 70 int expected_param_size = 0; 71 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 72 return; 73 } 74 bool init_result; 75 int error_code; 76 std::tie(init_result, error_code) = bt_binder.BluetoothBinderInitInterface(); 77 if (error_code == sl4n_error_codes::kFailInt) { 78 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 79 } else { 80 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 81 } 82 doc.AddMember(sl4n::kResultStr, init_result, doc.GetAllocator()); 83 return; 84 } 85 86 void bluetooth_binder_set_local_name_wrapper(rapidjson::Document &doc) { 87 int expected_param_size = 1; 88 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 89 return; 90 } 91 std::string name; 92 if (!doc[sl4n::kParamsStr][0].IsString()) { 93 LOG(ERROR) << sl4n::kTagStr << ": Expected String input for name"; 94 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 95 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 96 return; 97 } else { 98 name = doc[sl4n::kParamsStr][0].GetString(); 99 } 100 bool set_result; 101 int error_code; 102 std::tie(set_result, error_code) = bt_binder.BluetoothBinderSetName(name); 103 if (error_code == sl4n_error_codes::kFailInt) { 104 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 105 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 106 } else { 107 doc.AddMember(sl4n::kResultStr, set_result, doc.GetAllocator()); 108 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 109 } 110 return; 111 } 112 113 void bluetooth_binder_get_local_address_wrapper(rapidjson::Document &doc) { 114 int expected_param_size = 0; 115 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 116 return; 117 } 118 //check for kfailedstr or NULL??? 119 std::string address; 120 int error_code; 121 std::tie(address, error_code) = bt_binder.BluetoothBinderGetAddress(); 122 if (error_code == sl4n_error_codes::kFailInt) { 123 doc.AddMember(sl4n::kResultStr, sl4n::kFailStr, doc.GetAllocator()); 124 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 125 } else { 126 rapidjson::Value tmp; 127 tmp.SetString(address.c_str(), doc.GetAllocator()); 128 doc.AddMember(sl4n::kResultStr, tmp, doc.GetAllocator()); 129 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 130 } 131 return; 132 } 133 134 void bluetooth_binder_enable_wrapper(rapidjson::Document &doc) { 135 int expected_param_size = 0; 136 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 137 return; 138 } 139 bool enable_result; 140 int error_code; 141 std::tie(enable_result, error_code) = bt_binder.BluetoothBinderEnable(); 142 if (error_code == sl4n_error_codes::kFailInt) { 143 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 144 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 145 } else { 146 doc.AddMember(sl4n::kResultStr, enable_result, doc.GetAllocator()); 147 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 148 } 149 } 150 151 void bluetooth_binder_register_ble_wrapper(rapidjson::Document &doc) { 152 int expected_param_size = 0; 153 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 154 return; 155 } 156 bool register_result; 157 int error_code; 158 std::tie(register_result, error_code) = 159 bt_binder.BluetoothBinderRegisterBLE(); 160 if (error_code == sl4n_error_codes::kFailInt) { 161 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 162 doc.AddMember(sl4n::kErrorStr, sl4n::kFailStr, doc.GetAllocator()); 163 } else { 164 doc.AddMember(sl4n::kResultStr, register_result, doc.GetAllocator()); 165 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 166 } 167 } 168 169 void bluetooth_binder_set_adv_settings_wrapper(rapidjson::Document &doc) { 170 int expected_param_size = 4; 171 if (!CommonUtils::IsParamLengthMatching(doc, expected_param_size)) { 172 return; 173 } 174 int mode; 175 int timeout_seconds; 176 int tx_power_level; 177 bool is_connectable; 178 // TODO(tturney) Verify inputs better 179 if (!doc[sl4n::kParamsStr][0].IsInt()) { 180 LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for mode"; 181 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 182 doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator()); 183 return; 184 } else { 185 mode = doc[sl4n::kParamsStr][0].GetInt(); 186 } 187 if (!doc[sl4n::kParamsStr][1].IsInt()) { 188 LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for timeout"; 189 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 190 doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator()); 191 return; 192 } else { 193 timeout_seconds = doc[sl4n::kParamsStr][1].GetInt(); 194 } 195 if (!doc[sl4n::kParamsStr][2].IsInt()) { 196 LOG(ERROR) << sl4n::kTagStr << ": Expected Int input for tx power level"; 197 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 198 doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator()); 199 return; 200 } else { 201 tx_power_level = doc[sl4n::kParamsStr][2].GetInt(); 202 } 203 if (!doc[sl4n::kParamsStr][3].IsBool()) { 204 LOG(ERROR) << sl4n::kTagStr << ": Expected Bool input for connectable"; 205 doc.AddMember(sl4n::kResultStr, false, doc.GetAllocator()); 206 doc.AddMember(sl4n::kErrorStr, sl4n::kInvalidParamStr, doc.GetAllocator()); 207 return; 208 } else { 209 is_connectable = doc[sl4n::kParamsStr][3].GetBool(); 210 } 211 212 int adv_settings; 213 int error_code; 214 std::tie(adv_settings, error_code) = bt_binder.BluetoothBinderSetAdvSettings( 215 mode, timeout_seconds, tx_power_level, is_connectable); 216 if(error_code == sl4n_error_codes::kFailInt) { 217 doc.AddMember( 218 sl4n::kResultStr, sl4n_error_codes::kFailInt, doc.GetAllocator()); 219 doc.AddMember(sl4n::kErrorStr, sl4n::kFailedCounterInt, doc.GetAllocator()); 220 return; 221 } else { 222 doc.AddMember(sl4n::kResultStr, adv_settings, doc.GetAllocator()); 223 doc.AddMember(sl4n::kErrorStr, NULL, doc.GetAllocator()); 224 } 225 } 226 227 // End Wrappers ... I'm not a large water dwelling mammal... 228 229 CommandReceiver::CommandReceiver() { 230 if (_funcMap == NULL) { 231 _funcMap = new function_map(); 232 } 233 _funcMap->insert(std::make_pair("initiate", &initiate)); 234 _funcMap->insert(std::make_pair("BluetoothBinderInitInterface", 235 &bluetooth_binder_init_interface_wapper)); 236 _funcMap->insert(std::make_pair("BluetoothBinderGetName", 237 &bluetooth_binder_get_local_name_wrapper)); 238 _funcMap->insert(std::make_pair("BluetoothBinderSetName", 239 &bluetooth_binder_set_local_name_wrapper)); 240 _funcMap->insert(std::make_pair("BluetoothBinderGetAddress", 241 &bluetooth_binder_get_local_address_wrapper)); 242 _funcMap->insert(std::make_pair("BluetoothBinderEnable", 243 &bluetooth_binder_enable_wrapper)); 244 _funcMap->insert(std::make_pair("BluetoothBinderRegisterBLE", 245 &bluetooth_binder_register_ble_wrapper)); 246 _funcMap->insert(std::make_pair("BluetoothBinderSetAdvSettings", 247 &bluetooth_binder_set_adv_settings_wrapper)); 248 } 249 250 void CommandReceiver::Call(rapidjson::Document& doc) { 251 std::string cmd; 252 if (doc.HasMember(sl4n::kCmdStr)) { 253 cmd = doc[sl4n::kCmdStr].GetString(); 254 } else if (doc.HasMember(sl4n::kMethodStr)) { 255 cmd = doc[sl4n::kMethodStr].GetString(); 256 } 257 258 function_map::const_iterator iter = _funcMap->find(cmd); 259 if (iter != _funcMap->end()) { 260 iter->second(doc); 261 } 262 _clean_result(doc); 263 } 264 265 void CommandReceiver::RegisterCommand(std::string name, MFP command) { 266 if (_funcMap == NULL) { 267 _funcMap = new function_map(); 268 } 269 270 _funcMap->insert(std::make_pair(name, command)); 271 } 272