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