1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hfp_hf_service.h"
17
18 #include "adapter_config.h"
19 #include "class_creator.h"
20 #include "hfp_hf_defines.h"
21 #include "log.h"
22 #include "log_util.h"
23 #include "profile_service_manager.h"
24 #include "stub/telephone_client_service.h"
25
26 namespace OHOS {
27 namespace bluetooth {
28 class TelephoneHfObserver : public stub::TelephoneClientServiceObserver {
29 public:
TelephoneHfObserver(HfpHfService & service)30 explicit TelephoneHfObserver(HfpHfService& service) : service_(service)
31 {
32 }
33 ~TelephoneHfObserver() = default;
34
OnBatteryLevelChanged(int batteryLevel)35 void OnBatteryLevelChanged(int batteryLevel) override
36 {
37 LOG_DEBUG("[HFP HF]%{public}s() batteryLevel[%{public}d]", __PRETTY_FUNCTION__, batteryLevel);
38 service_.BatteryLevelChanged(batteryLevel);
39 }
40
OnVolumeChanged(int type,int volume)41 void OnVolumeChanged(int type, int volume) override
42 {
43 LOG_DEBUG("[HFP HF]%{public}s() type[%{public}d], volume[%{public}d]", __PRETTY_FUNCTION__, type, volume);
44 service_.SetHfVolume(volume, type);
45 }
46
OnEnhancedDriverSafety(int state)47 void OnEnhancedDriverSafety(int state) override
48 {
49 LOG_DEBUG("[HFP HF]%{public}s() state[%{public}d]", __PRETTY_FUNCTION__, state);
50 service_.EnhancedDriverSafety(state);
51 }
52
53 private:
54 HfpHfService &service_;
55 BT_DISALLOW_COPY_AND_ASSIGN(TelephoneHfObserver);
56 };
57
58 struct HfpHfService::impl {
implOHOS::bluetooth::HfpHfService::impl59 explicit impl(HfpHfService& service) : observer_(service)
60 {
61 }
62 TelephoneHfObserver observer_;
63 };
64
GetService()65 HfpHfService *HfpHfService::GetService()
66 {
67 auto servManager = IProfileManager::GetInstance();
68 return static_cast<HfpHfService *>(servManager->GetProfileService(PROFILE_NAME_HFP_HF));
69 }
70
HfpHfService()71 HfpHfService::HfpHfService() : utility::Context(PROFILE_NAME_HFP_HF, "1.7.1"), pimpl(nullptr)
72 {
73 LOG_INFO("[HFP HF]ProfileService:%{public}s Create", Name().c_str());
74 pimpl = std::make_unique<impl>(*this);
75 }
76
~HfpHfService()77 HfpHfService::~HfpHfService()
78 {
79 LOG_INFO("[HFP HF]ProfileService:%{public}s Release", Name().c_str());
80 }
81
GetContext()82 utility::Context *HfpHfService::GetContext()
83 {
84 return this;
85 }
86
Enable()87 void HfpHfService::Enable()
88 {
89 LOG_DEBUG("[HFP HF]%{public}s():==========<enter>==========", __FUNCTION__);
90 stub::TelePhoneClientService::GetInstance().RegisterObserver(&pimpl->observer_);
91 HfpHfMessage event(HFP_HF_SERVICE_STARTUP_EVT);
92 PostEvent(event);
93 }
94
Disable()95 void HfpHfService::Disable()
96 {
97 LOG_DEBUG("[HFP HF]%{public}s():==========<enter>==========", __FUNCTION__);
98 stub::TelePhoneClientService::GetInstance().DeregisterObserver(&pimpl->observer_);
99 HfpHfMessage event(HFP_HF_SERVICE_SHUTDOWN_EVT);
100 PostEvent(event);
101 }
102
StartUp()103 void HfpHfService::StartUp()
104 {
105 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
106 if (isStarted_ == true) {
107 GetContext()->OnEnable(PROFILE_NAME_HFP_HF, true);
108 LOG_WARN("[HFP HF]%{public}s():HfpHfService has already been started before.", __FUNCTION__);
109 return;
110 }
111
112 maxConnectionsNum_ = GetMaxConnectionsDeviceNum();
113 int ret = HfpHfProfile::RegisterService();
114 GetContext()->OnEnable(PROFILE_NAME_HFP_HF, ret ? false : true);
115 if (ret == 0) {
116 isStarted_ = true;
117 LOG_DEBUG("[HFP HF]%{public}s():HfpHfService started", __FUNCTION__);
118 }
119 }
120
ShutDown()121 void HfpHfService::ShutDown()
122 {
123 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
124 if (isStarted_ == false) {
125 GetContext()->OnDisable(PROFILE_NAME_HFP_HF, true);
126 LOG_WARN("[HFP HF]%{public}s():HfpHfService has already been shutdown before.", __FUNCTION__);
127 return;
128 }
129
130 isShuttingDown_ = true;
131 bool isDisconnected = false;
132 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
133 if ((it->second != nullptr) && (it->second->GetDeviceStateInt() > HFP_HF_STATE_DISCONNECTED)) {
134 Disconnect(RawAddress(it->first));
135 isDisconnected = true;
136 }
137 }
138
139 if (!isDisconnected) {
140 ShutDownDone(true);
141 }
142 }
143
ShutDownDone(bool isAllDisconnected)144 void HfpHfService::ShutDownDone(bool isAllDisconnected)
145 {
146 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
147 if (!isAllDisconnected) {
148 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
149 if ((it->second != nullptr) && (it->second->GetDeviceStateInt() > HFP_HF_STATE_DISCONNECTED)) {
150 return;
151 }
152 }
153 }
154
155 int ret = HfpHfProfile::DeregisterService();
156 stateMachines_.clear();
157 GetContext()->OnDisable(PROFILE_NAME_HFP_HF, ret ? false : true);
158 if (ret == 0) {
159 isStarted_ = false;
160 LOG_DEBUG("[HFP HF]%{public}s():HfpHfService shutdown", __FUNCTION__);
161 }
162 isShuttingDown_ = false;
163 }
164
Connect(const RawAddress & device)165 int HfpHfService::Connect(const RawAddress &device)
166 {
167 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
168 std::lock_guard<std::recursive_mutex> lk(mutex_);
169 std::string address = device.GetAddress();
170 auto it = stateMachines_.find(address);
171 if ((it != stateMachines_.end()) && (it->second != nullptr)) {
172 int slcState = it->second->GetDeviceStateInt();
173 if ((slcState >= HFP_HF_STATE_CONNECTED) || (slcState == HFP_HF_STATE_CONNECTING)) {
174 return HFP_HF_FAILURE;
175 }
176 }
177
178 if (GetConnectionsDeviceNum() >= maxConnectionsNum_) {
179 LOG_INFO("[HFP HF]%{public}s():Max connection number has reached!", __FUNCTION__);
180 return HFP_HF_FAILURE;
181 }
182
183 HfpHfMessage event(HFP_HF_CONNECT_EVT);
184 event.dev_ = address;
185 PostEvent(event);
186 return HFP_HF_SUCCESS;
187 }
188
Disconnect(const RawAddress & device)189 int HfpHfService::Disconnect(const RawAddress &device)
190 {
191 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
192 std::lock_guard<std::recursive_mutex> lk(mutex_);
193 std::string address = device.GetAddress();
194 auto it = stateMachines_.find(address);
195 if (it == stateMachines_.end() || it->second == nullptr) {
196 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
197 return HFP_HF_FAILURE;
198 }
199
200 int slcState = it->second->GetDeviceStateInt();
201 if ((slcState != HFP_HF_STATE_CONNECTING) && (slcState < HFP_HF_STATE_CONNECTED)) {
202 LOG_DEBUG("[HFP HF]%{public}s():slcState:%{public}d", __FUNCTION__, slcState);
203 return HFP_HF_FAILURE;
204 }
205
206 HfpHfMessage event(HFP_HF_DISCONNECT_EVT);
207 event.dev_ = address;
208 PostEvent(event);
209 return HFP_HF_SUCCESS;
210 }
211
GetConnectDevices()212 std::list<RawAddress> HfpHfService::GetConnectDevices()
213 {
214 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
215 std::lock_guard<std::recursive_mutex> lk(mutex_);
216 std::list<RawAddress> devList;
217 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
218 if ((it->second != nullptr) && (it->second->GetDeviceStateInt() >= HFP_HF_STATE_CONNECTED)) {
219 devList.push_back(RawAddress(it->first));
220 }
221 }
222 return devList;
223 }
224
GetConnectState()225 int HfpHfService::GetConnectState()
226 {
227 int result = 0;
228 std::lock_guard<std::recursive_mutex> lk(mutex_);
229 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
230 if (it->second == nullptr) {
231 result |= PROFILE_STATE_DISCONNECTED;
232 } else if (it->second->GetDeviceStateInt() >= HFP_HF_STATE_CONNECTED) {
233 result |= PROFILE_STATE_CONNECTED;
234 } else if (it->second->GetDeviceStateInt() == HFP_HF_STATE_CONNECTING) {
235 result |= PROFILE_STATE_CONNECTING;
236 } else if (it->second->GetDeviceStateInt() == HFP_HF_STATE_DISCONNECTING) {
237 result |= PROFILE_STATE_DISCONNECTING;
238 } else if (it->second->GetDeviceStateInt() == HFP_HF_STATE_DISCONNECTED) {
239 result |= PROFILE_STATE_DISCONNECTED;
240 }
241 }
242 return result;
243 }
244
GetMaxConnectNum()245 int HfpHfService::GetMaxConnectNum()
246 {
247 std::lock_guard<std::recursive_mutex> lk(mutex_);
248 return maxConnectionsNum_;
249 }
250
IsScoConnected() const251 bool HfpHfService::IsScoConnected() const
252 {
253 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
254 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
255 if (it->second != nullptr) {
256 auto audioState = it->second->GetDeviceStateInt();
257 if (audioState > HFP_HF_AUDIO_STATE_DISCONNECTED) {
258 return true;
259 }
260 }
261 }
262 return false;
263 }
264
ConnectSco(const RawAddress & device)265 bool HfpHfService::ConnectSco(const RawAddress &device)
266 {
267 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
268 std::lock_guard<std::recursive_mutex> lk(mutex_);
269 std::string address = device.GetAddress();
270 auto it = stateMachines_.find(address);
271 if (it == stateMachines_.end() || it->second == nullptr) {
272 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
273 return false;
274 }
275
276 if (it->second->GetDeviceStateInt() != HFP_HF_STATE_CONNECTED) {
277 LOG_DEBUG("[HFP HF]%{public}s():state:%{public}d", __FUNCTION__, it->second->GetDeviceStateInt());
278 return false;
279 }
280
281 HfpHfMessage event(HFP_HF_CONNECT_AUDIO_EVT);
282 event.dev_ = address;
283 PostEvent(event);
284 return true;
285 }
286
DisconnectSco(const RawAddress & device)287 bool HfpHfService::DisconnectSco(const RawAddress &device)
288 {
289 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
290 std::lock_guard<std::recursive_mutex> lk(mutex_);
291 std::string address = device.GetAddress();
292 auto it = stateMachines_.find(address);
293 if (it == stateMachines_.end() || it->second == nullptr) {
294 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
295 return false;
296 }
297
298 if (it->second->GetDeviceStateInt() <= HFP_HF_STATE_CONNECTED) {
299 LOG_DEBUG("[HFP HF]%{public}s():audio state:%{public}d", __FUNCTION__, it->second->GetDeviceStateInt());
300 return false;
301 }
302
303 HfpHfMessage event(HFP_HF_DISCONNECT_AUDIO_EVT);
304 event.dev_ = address;
305 PostEvent(event);
306 return true;
307 }
308
GetDevicesByStates(std::vector<int> states)309 std::vector<RawAddress> HfpHfService::GetDevicesByStates(std::vector<int> states)
310 {
311 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
312 std::lock_guard<std::recursive_mutex> lk(mutex_);
313 std::vector<RawAddress> devices;
314 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
315 RawAddress device(it->first);
316 for (size_t i = 0; i < states.size(); i++) {
317 if (GetDeviceState(device) == states[i]) {
318 devices.push_back(device);
319 break;
320 }
321 }
322 }
323 return devices;
324 }
325
GetDeviceState(const RawAddress & device)326 int HfpHfService::GetDeviceState(const RawAddress &device)
327 {
328 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
329 std::lock_guard<std::recursive_mutex> lk(mutex_);
330 std::string address = device.GetAddress();
331 auto it = stateMachines_.find(address);
332 if (it == stateMachines_.end() || it->second == nullptr) {
333 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
334 return stateMap_.at(HFP_HF_STATE_DISCONNECTED);
335 }
336
337 if (it->second->GetDeviceStateInt() >= HFP_HF_STATE_CONNECTED) {
338 return stateMap_.at(HFP_HF_STATE_CONNECTED);
339 } else {
340 return stateMap_.at(it->second->GetDeviceStateInt());
341 }
342 }
343
GetScoState(const RawAddress & device)344 int HfpHfService::GetScoState(const RawAddress &device)
345 {
346 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
347 std::lock_guard<std::recursive_mutex> lk(mutex_);
348 std::string address = device.GetAddress();
349 auto it = stateMachines_.find(address);
350 if (it == stateMachines_.end() || it->second == nullptr) {
351 return HFP_HF_AUDIO_STATE_DISCONNECTED;
352 }
353
354 if (it->second->GetDeviceStateInt() <= HFP_HF_AUDIO_STATE_DISCONNECTED) {
355 return HFP_HF_AUDIO_STATE_DISCONNECTED;
356 } else {
357 return it->second->GetDeviceStateInt();
358 }
359 }
360
SendDTMFTone(const RawAddress & device,uint8_t code)361 bool HfpHfService::SendDTMFTone(const RawAddress &device, uint8_t code)
362 {
363 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
364 std::lock_guard<std::recursive_mutex> lk(mutex_);
365 std::string address = device.GetAddress();
366 if (IsConnected(address) == false) {
367 return false;
368 }
369
370 if ((code < '0' || code > '9') && code != '*' && code != '#') {
371 LOG_ERROR("[HFP HF]%{public}s():invalid dtmf code", __FUNCTION__);
372 return false;
373 }
374
375 HfpHfMessage event(HFP_HF_SEND_DTMF_EVT, code);
376 event.dev_ = address;
377 PostEvent(event);
378
379 return true;
380 }
381
GetCurrentCallList(const RawAddress & device)382 std::vector<HandsFreeUnitCalls> HfpHfService::GetCurrentCallList(const RawAddress &device)
383 {
384 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
385 std::lock_guard<std::recursive_mutex> lk(mutex_);
386 std::vector<HandsFreeUnitCalls> calls;
387 std::string address = device.GetAddress();
388 auto it = stateMachines_.find(address);
389 if (it == stateMachines_.end() || it->second == nullptr) {
390 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
391 return calls;
392 }
393
394 if (it->second->GetDeviceStateInt() < HFP_HF_STATE_CONNECTED) {
395 LOG_DEBUG("[HFP HF]%{public}s():It's not connected!", __FUNCTION__);
396 return calls;
397 }
398
399 return it->second->GetCurrentCallList();
400 }
401
AcceptIncomingCall(const RawAddress & device,int flag)402 bool HfpHfService::AcceptIncomingCall(const RawAddress &device, int flag)
403 {
404 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
405 std::lock_guard<std::recursive_mutex> lk(mutex_);
406 std::string address = device.GetAddress();
407 if (IsConnected(address) == false) {
408 return false;
409 }
410
411 HfpHfMessage event(HFP_HF_ACCEPT_CALL_EVT, flag);
412 event.dev_ = address;
413 PostEvent(event);
414 return true;
415 }
416
HoldActiveCall(const RawAddress & device)417 bool HfpHfService::HoldActiveCall(const RawAddress &device)
418 {
419 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
420 std::lock_guard<std::recursive_mutex> lk(mutex_);
421 std::string address = device.GetAddress();
422 if (IsConnected(address) == false) {
423 return false;
424 }
425
426 HfpHfMessage event(HFP_HF_HOLD_CALL_EVT);
427 event.dev_ = address;
428 PostEvent(event);
429 return true;
430 }
431
RejectIncomingCall(const RawAddress & device)432 bool HfpHfService::RejectIncomingCall(const RawAddress &device)
433 {
434 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
435 std::lock_guard<std::recursive_mutex> lk(mutex_);
436 std::string address = device.GetAddress();
437 if (IsConnected(address) == false) {
438 return false;
439 }
440
441 HfpHfMessage event(HFP_HF_REJECT_CALL_EVT);
442 event.dev_ = address;
443 PostEvent(event);
444 return true;
445 }
446
HandleIncomingCall(const RawAddress & device,int flag)447 bool HfpHfService::HandleIncomingCall(const RawAddress &device, int flag)
448 {
449 HILOGI("[HFP HF]:==========<start>==========");
450 std::lock_guard<std::recursive_mutex> lk(mutex_);
451 std::string address = device.GetAddress();
452 if (IsConnected(address) == false) {
453 return false;
454 }
455
456 HfpHfMessage event(HFP_HF_HANDLE_INCOMING_CALL_EVT, flag);
457 event.dev_ = address;
458 PostEvent(event);
459 return true;
460 }
461
HandleMultiCall(const RawAddress & device,int flag,int index)462 bool HfpHfService::HandleMultiCall(const RawAddress &device, int flag, int index)
463 {
464 HILOGI("[HFP HF]:==========<start>==========");
465 std::lock_guard<std::recursive_mutex> lk(mutex_);
466 std::string address = device.GetAddress();
467 if (IsConnected(address) == false) {
468 return false;
469 }
470
471 HfpHfMessage event(HFP_HF_HANDLE_MULTI_CALL_EVT, flag);
472 event.arg3_ = index;
473 event.dev_ = address;
474 PostEvent(event);
475 return true;
476 }
477
DialLastNumber(const RawAddress & device)478 bool HfpHfService::DialLastNumber(const RawAddress &device)
479 {
480 HILOGI("[HFP HF]:==========<start>==========");
481 std::lock_guard<std::recursive_mutex> lk(mutex_);
482 std::string address = device.GetAddress();
483 if (IsConnected(address) == false) {
484 return false;
485 }
486
487 HfpHfMessage event(HFP_HF_DIAL_LAST_NUMBER);
488 event.dev_ = address;
489 PostEvent(event);
490 return true;
491 }
492
DialMemory(const RawAddress & device,int index)493 bool HfpHfService::DialMemory(const RawAddress &device, int index)
494 {
495 HILOGI("[HFP HF]:==========<start>========== index = %{public}d", index);
496 std::lock_guard<std::recursive_mutex> lk(mutex_);
497 std::string address = device.GetAddress();
498 if (IsConnected(address) == false) {
499 return false;
500 }
501
502 HfpHfMessage event(HFP_HF_DIAL_MEMORY, index);
503 event.dev_ = address;
504 PostEvent(event);
505 return true;
506 }
507
SendVoiceTag(const RawAddress & device,int index)508 bool HfpHfService::SendVoiceTag(const RawAddress &device, int index)
509 {
510 LOG_DEBUG("[HFP HF]%{public}s():==========<start>========== index = %{public}d", __FUNCTION__, index);
511 std::lock_guard<std::recursive_mutex> lk(mutex_);
512 std::string address = device.GetAddress();
513 if (IsConnected(address) == false) {
514 return false;
515 }
516
517 HfpHfMessage event(HFP_HF_SEND_VOICE_TAG, index);
518 event.dev_ = address;
519 PostEvent(event);
520 return true;
521 }
522
SendKeyPressed(const RawAddress & device)523 bool HfpHfService::SendKeyPressed(const RawAddress &device)
524 {
525 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
526 std::lock_guard<std::recursive_mutex> lk(mutex_);
527 std::string address = device.GetAddress();
528 if (IsConnected(address) == false) {
529 return false;
530 }
531
532 HfpHfMessage event(HFP_HF_SEND_KEY_PRESSED);
533 event.dev_ = address;
534 PostEvent(event);
535 return true;
536 }
537
FinishActiveCall(const RawAddress & device,const HandsFreeUnitCalls & call)538 bool HfpHfService::FinishActiveCall(const RawAddress &device, const HandsFreeUnitCalls &call)
539 {
540 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
541 std::lock_guard<std::recursive_mutex> lk(mutex_);
542 std::string address = device.GetAddress();
543 if (IsConnected(address) == false) {
544 return false;
545 }
546
547 HfpHfMessage event(HFP_HF_FINISH_CALL_EVT);
548 event.dev_ = address;
549 event.calls_ = call;
550 PostEvent(event);
551 return true;
552 }
553
StartDial(const RawAddress & device,const std::string & number)554 std::optional<HandsFreeUnitCalls> HfpHfService::StartDial(const RawAddress &device, const std::string &number)
555 {
556 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
557 std::lock_guard<std::recursive_mutex> lk(mutex_);
558 std::string address = device.GetAddress();
559 auto it = stateMachines_.find(address);
560 if (it == stateMachines_.end() || it->second == nullptr) {
561 LOG_DEBUG("[HFP HF]%{public}s():The state machine is not available!", __FUNCTION__);
562 return std::nullopt;
563 }
564
565 if (it->second->GetDeviceStateInt() < HFP_HF_STATE_CONNECTED) {
566 LOG_DEBUG("[HFP HF]%{public}s():It's not connected!", __FUNCTION__);
567 return std::nullopt;
568 }
569
570 HandsFreeUnitCalls calls(
571 address, HFP_HF_OUTGOING_CALL_ID, HFP_CALL_STATE_DIALING, number, false, true, it->second->GetIsInbandRing());
572 HfpHfMessage event(HFP_HF_DIAL_CALL_EVT);
573 event.dev_ = address;
574 event.calls_ = calls;
575 PostEvent(event);
576 return calls;
577 }
578
RegisterObserver(HfpHfServiceObserver & observer)579 void HfpHfService::RegisterObserver(HfpHfServiceObserver &observer)
580 {
581 std::lock_guard<std::recursive_mutex> lk(mutex_);
582 std::list<HfpHfServiceObserver *>::iterator iter;
583 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
584 if (*iter == &observer) {
585 LOG_INFO("Already registered!");
586 return;
587 }
588 }
589 observers_.push_back(&observer);
590 }
591
DeregisterObserver(HfpHfServiceObserver & observer)592 void HfpHfService::DeregisterObserver(HfpHfServiceObserver &observer)
593 {
594 std::lock_guard<std::recursive_mutex> lk(mutex_);
595 std::list<HfpHfServiceObserver *>::iterator iter;
596 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
597 if (*iter == &observer) {
598 break;
599 }
600 }
601
602 if (iter != observers_.end()) {
603 observers_.erase(iter);
604 }
605 return;
606 }
607
SetHfVolume(int volume,int type)608 void HfpHfService::SetHfVolume(int volume, int type)
609 {
610 HfpHfMessage event(HFP_HF_SET_VOLUME_EVT, volume);
611 event.arg3_ = type;
612 PostEvent(event);
613 return;
614 }
615
OpenVoiceRecognition(const RawAddress & device)616 bool HfpHfService::OpenVoiceRecognition(const RawAddress &device)
617 {
618 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
619 std::lock_guard<std::recursive_mutex> lk(mutex_);
620 std::string address = device.GetAddress();
621 if (IsConnected(address) == false) {
622 return false;
623 }
624
625 HfpHfMessage event(HFP_HF_OPEN_VOICE_RECOGNITION_EVT);
626 event.dev_ = address;
627 PostEvent(event);
628 return true;
629 }
630
CloseVoiceRecognition(const RawAddress & device)631 bool HfpHfService::CloseVoiceRecognition(const RawAddress &device)
632 {
633 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
634 std::lock_guard<std::recursive_mutex> lk(mutex_);
635 std::string address = device.GetAddress();
636 if (IsConnected(address) == false) {
637 return false;
638 }
639
640 HfpHfMessage event(HFP_HF_CLOSE_VOICE_RECOGNITION_EVT);
641 event.dev_ = address;
642 PostEvent(event);
643 return true;
644 }
645
BatteryLevelChanged(int batteryLevel)646 void HfpHfService::BatteryLevelChanged(int batteryLevel)
647 {
648 HfpHfMessage event(HFP_HF_BATTERY_LEVEL_CHANGED_EVT, batteryLevel);
649 PostEvent(event);
650 }
651
EnhancedDriverSafety(int state)652 void HfpHfService::EnhancedDriverSafety(int state)
653 {
654 HfpHfMessage event(HFP_HF_ENHANCED_DRIVER_SAFETY_CHANGED_EVT, state);
655 PostEvent(event);
656 }
657
SendEventToEachStateMachine(const HfpHfMessage & event) const658 void HfpHfService::SendEventToEachStateMachine(const HfpHfMessage &event) const
659 {
660 HfpHfMessage curEvent = event;
661 for (auto it = stateMachines_.begin(); it != stateMachines_.end(); ++it) {
662 if (it->second != nullptr) {
663 curEvent.dev_ = it->first;
664 it->second->ProcessMessage(curEvent);
665 }
666 }
667 }
668
ProcessConnectEvent(const HfpHfMessage & event)669 void HfpHfService::ProcessConnectEvent(const HfpHfMessage &event)
670 {
671 if (GetConnectionsDeviceNum() < maxConnectionsNum_) {
672 auto it = stateMachines_.find(event.dev_);
673 if (it != stateMachines_.end() && it->second != nullptr && it->second->IsRemoving()) {
674 // peer device may send connect request before we remove statemachine for last connection.
675 // so post this connect request, process it after we remove statemachine completely.
676 PostEvent(event);
677 } else if (it == stateMachines_.end() || it->second == nullptr) {
678 stateMachines_[event.dev_] = std::make_unique<HfpHfStateMachine>(event.dev_);
679 stateMachines_[event.dev_]->Init();
680 stateMachines_[event.dev_]->ProcessMessage(event);
681 } else {
682 it->second->ProcessMessage(event);
683 }
684 }
685 }
686
ProcessRemoveStateMachine(const std::string & address)687 void HfpHfService::ProcessRemoveStateMachine(const std::string &address)
688 {
689 stateMachines_.insert_or_assign(address, nullptr);
690 if (isShuttingDown_) {
691 ShutDownDone(false);
692 }
693 }
694
ProcessDefaultEvent(const HfpHfMessage & event) const695 void HfpHfService::ProcessDefaultEvent(const HfpHfMessage &event) const
696 {
697 auto it = stateMachines_.find(event.dev_);
698 if ((it != stateMachines_.end()) && (it->second != nullptr)) {
699 it->second->ProcessMessage(event);
700 } else {
701 HILOGE("[HFP HF] invalid address[%{public}s]", GetEncryptAddr(event.dev_).c_str());
702 }
703 }
704
PostEvent(const HfpHfMessage & event)705 void HfpHfService::PostEvent(const HfpHfMessage &event)
706 {
707 GetDispatcher()->PostTask(std::bind(&HfpHfService::ProcessEvent, this, event));
708 }
709
ProcessEvent(const HfpHfMessage & event)710 void HfpHfService::ProcessEvent(const HfpHfMessage &event)
711 {
712 std::lock_guard<std::recursive_mutex> lk(mutex_);
713 std::string address = event.dev_;
714 HILOGI("[HFP HF] address[%{public}s] event_no[%{public}d]", GetEncryptAddr(address).c_str(), event.what_);
715 switch (event.what_) {
716 case HFP_HF_SERVICE_STARTUP_EVT:
717 StartUp();
718 break;
719 case HFP_HF_SERVICE_SHUTDOWN_EVT:
720 ShutDown();
721 break;
722 case HFP_HF_CONNECT_EVT:
723 case HFP_HF_CONNECT_REQUEST_EVT:
724 ProcessConnectEvent(event);
725 break;
726 case HFP_HF_REMOVE_STATE_MACHINE_EVT:
727 ProcessRemoveStateMachine(event.dev_);
728 break;
729 case HFP_HF_SET_VOLUME_EVT:
730 case HFP_HF_BATTERY_LEVEL_CHANGED_EVT:
731 case HFP_HF_ENHANCED_DRIVER_SAFETY_CHANGED_EVT:
732 SendEventToEachStateMachine(event);
733 break;
734 default:
735 ProcessDefaultEvent(event);
736 break;
737 }
738 }
739
NotifyStateChanged(const RawAddress & device,int state)740 void HfpHfService::NotifyStateChanged(const RawAddress &device, int state)
741 {
742 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
743 std::list<HfpHfServiceObserver *>::iterator iter;
744 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
745 (*iter)->OnConnectionStateChanged(device, stateMap_.at(state));
746 }
747 }
748
NotifyScoStateChanged(const RawAddress & device,int state)749 void HfpHfService::NotifyScoStateChanged(const RawAddress &device, int state)
750 {
751 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
752 std::list<HfpHfServiceObserver *>::iterator iter;
753 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
754 (*iter)->OnScoStateChanged(device, state);
755 }
756 }
757
NotifyCallChanged(const RawAddress & device,const HandsFreeUnitCalls & call)758 void HfpHfService::NotifyCallChanged(const RawAddress &device, const HandsFreeUnitCalls &call)
759 {
760 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
761 std::lock_guard<std::recursive_mutex> lk(mutex_);
762 std::list<HfpHfServiceObserver *>::iterator iter;
763 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
764 (*iter)->OnCallChanged(device, call);
765 }
766 }
767
NotifyBatteryLevelChanged(const RawAddress & device,int batteryLevel)768 void HfpHfService::NotifyBatteryLevelChanged(const RawAddress &device, int batteryLevel)
769 {
770 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
771 std::list<HfpHfServiceObserver *>::iterator iter;
772 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
773 (*iter)->OnBatteryLevelChanged(device, batteryLevel);
774 }
775 }
776
NotifySignalStrengthChanged(const RawAddress & device,int signal)777 void HfpHfService::NotifySignalStrengthChanged(const RawAddress &device, int signal)
778 {
779 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
780 std::list<HfpHfServiceObserver *>::iterator iter;
781 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
782 (*iter)->OnSignalStrengthChanged(device, signal);
783 }
784 }
785
NotifyRegistrationStatusChanged(const RawAddress & device,int status)786 void HfpHfService::NotifyRegistrationStatusChanged(const RawAddress &device, int status)
787 {
788 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
789 std::list<HfpHfServiceObserver *>::iterator iter;
790 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
791 (*iter)->OnRegistrationStatusChanged(device, status);
792 }
793 }
794
NotifyRoamingStatusChanged(const RawAddress & device,int status)795 void HfpHfService::NotifyRoamingStatusChanged(const RawAddress &device, int status)
796 {
797 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
798 std::list<HfpHfServiceObserver *>::iterator iter;
799 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
800 (*iter)->OnRoamingStatusChanged(device, status);
801 }
802 }
803
NotifyOperatorSelectionChanged(const RawAddress & device,const std::string & name)804 void HfpHfService::NotifyOperatorSelectionChanged(const RawAddress &device, const std::string &name)
805 {
806 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
807 std::list<HfpHfServiceObserver *>::iterator iter;
808 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
809 (*iter)->OnOperatorSelectionChanged(device, name);
810 }
811 }
812
NotifySubscriberNumberChanged(const RawAddress & device,const std::string & number)813 void HfpHfService::NotifySubscriberNumberChanged(const RawAddress &device, const std::string &number)
814 {
815 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
816 std::list<HfpHfServiceObserver *>::iterator iter;
817 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
818 (*iter)->OnSubscriberNumberChanged(device, number);
819 }
820 }
821
NotifyVoiceRecognitionStatusChanged(const RawAddress & device,int status)822 void HfpHfService::NotifyVoiceRecognitionStatusChanged(const RawAddress &device, int status)
823 {
824 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
825 std::list<HfpHfServiceObserver *>::iterator iter;
826 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
827 (*iter)->OnVoiceRecognitionStatusChanged(device, status);
828 }
829 }
830
NotifyInBandRingTone(const RawAddress & device,int status)831 void HfpHfService::NotifyInBandRingTone(const RawAddress &device, int status)
832 {
833 LOG_INFO("[HFP HF]%{public}s():", __FUNCTION__);
834 std::list<HfpHfServiceObserver *>::iterator iter;
835 for (iter = observers_.begin(); iter != observers_.end(); ++iter) {
836 (*iter)->OnInBandRingToneChanged(device, status);
837 }
838 }
839
GetConnectionsDeviceNum() const840 int HfpHfService::GetConnectionsDeviceNum() const
841 {
842 int size = 0;
843 for (auto iter = stateMachines_.begin(); iter != stateMachines_.end(); ++iter) {
844 if (iter->second != nullptr) {
845 auto connectionState = iter->second->GetDeviceStateInt();
846 if ((connectionState == HFP_HF_STATE_CONNECTING) || (connectionState >= HFP_HF_STATE_CONNECTED)) {
847 size++;
848 }
849 }
850 }
851 return size;
852 }
853
GetMaxConnectionsDeviceNum() const854 int HfpHfService::GetMaxConnectionsDeviceNum() const
855 {
856 int number = MAX_CONNECTIONS_NUM;
857 if (!AdapterConfig::GetInstance()->GetValue(SECTION_HFP_HF_SERVICE, PROPERTY_MAX_CONNECTED_DEVICES, number)) {
858 LOG_DEBUG("[HFP HF]%{public}s():It's failed to get the max connection number", __FUNCTION__);
859 }
860 return number;
861 }
862
SendAtCommand(const RawAddress & device,int cmdId,const std::string & arg)863 void HfpHfService::SendAtCommand(const RawAddress &device, int cmdId, const std::string &arg)
864 {
865 LOG_DEBUG("[HFP HF]%{public}s():==========<start>==========", __FUNCTION__);
866 std::lock_guard<std::recursive_mutex> lk(mutex_);
867 std::string address = device.GetAddress();
868 if (IsConnected(address) == false) {
869 return;
870 }
871
872 HfpHfMessage event(HFP_HF_SEND_AT_COMMAND_EVT);
873 event.dev_ = address;
874 event.arg1_ = cmdId;
875 event.str_ = arg;
876 PostEvent(event);
877 return;
878 }
879
IsConnected(const std::string & address) const880 bool HfpHfService::IsConnected(const std::string &address) const
881 {
882 auto it = stateMachines_.find(address);
883 if (it == stateMachines_.end() || it->second == nullptr) {
884 HILOGE("[HFP AG] Invalid Device address:%{public}s", GetEncryptAddr(address).c_str());
885 return false;
886 }
887 if (it->second->GetDeviceStateInt() < HFP_HF_STATE_CONNECTED) {
888 HILOGE("[HFP HF] It's not connected!");
889 return false;
890 }
891 return true;
892 }
893
894 REGISTER_CLASS_CREATOR(HfpHfService);
895 } // namespace bluetooth
896 } // namespace OHOS
897