• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "map_mse_service.h"
17 #include "log_util.h"
18 
19 namespace OHOS {
20 namespace bluetooth {
21 const uint16_t MapMseService::GOEP_L2CAP_PSM_VALUE[LPSM_SIZE] = {
22     0x1005, 0x1007, 0x1009, 0x100B, 0x100D, 0x100F, 0x1011, 0x1013, 0x1015, 0x1017, 0x1019, 0x101B
23 };
MapMseService()24 MapMseService::MapMseService() : utility::Context(PROFILE_NAME_MAP_MSE, "1.4.2")
25 {
26     MSE_LOG_INFO("%{public}s:%{public}s Create", PROFILE_NAME_MAP_MSE.c_str(), Name().c_str());
27 }
28 
~MapMseService()29 MapMseService::~MapMseService()
30 {
31     MSE_LOG_INFO("%{public}s:%{public}s Destroy", PROFILE_NAME_MAP_MSE.c_str(), Name().c_str());
32 
33     masInstanceMap_.clear();
34 }
35 
GetContext()36 utility::Context *MapMseService::GetContext()
37 {
38     return this;
39 }
40 
MnsCallback(MapMseService & mseService)41 MapMseService::MnsCallback::MnsCallback(MapMseService &mseService) : mseService_(mseService)
42 {}
43 
OnDisconnected()44 void MapMseService::MnsCallback::OnDisconnected()
45 {
46     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
47 
48     mseService_.DisableComplete();
49 }
50 
Observer(MapMseService & mseService)51 MapMseService::Observer::Observer(MapMseService &mseService) : mseService_(mseService)
52 {}
53 
OnIncomingConnect(const RawAddress & remoteAddr,const int masInstanceId)54 void MapMseService::Observer::OnIncomingConnect(const RawAddress &remoteAddr, const int masInstanceId)
55 {
56     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
57     MSE_LOG_INFO("OnIncomingConnect instance id : %{public}d ", masInstanceId);
58 
59     auto addr = remoteAddr.GetAddress();
60     if (mseService_.incomingConnectMap_.find(addr) != mseService_.incomingConnectMap_.end()) {
61         mseService_.incomingConnectMap_[addr] = mseService_.incomingConnectMap_[addr] + (masInstanceId + 1);
62         return;
63     }
64     mseService_.incomingConnectMap_[addr] = masInstanceId + 1;
65     mseService_.mapMseObservers_.ForEach(
66         [remoteAddr](IMapMseObserver &observer) { observer.OnPermission(remoteAddr); });
67 }
68 
OnTransportConnected(const RawAddress & remoteAddr,const int masInstanceId)69 void MapMseService::Observer::OnTransportConnected(const RawAddress &remoteAddr, const int masInstanceId)
70 {
71     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
72 
73     if (mseService_.mnsClient_) {
74         mseService_.mnsClient_->SdpSearch(remoteAddr);
75     }
76     IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_ON, PROFILE_NAME_MAP_MSE, remoteAddr);
77 }
78 
OnTransportDisconnected(const RawAddress & remoteAddr,const int masInstanceId)79 void MapMseService::Observer::OnTransportDisconnected(const RawAddress &remoteAddr, const int masInstanceId)
80 {
81     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
82 
83     bool hasEnd = false;
84     {
85         std::lock_guard<std::recursive_mutex> lock(mseService_.mseMapMutex_);
86         std::string addr = remoteAddr.GetAddress();
87         if (mseService_.incomingConnectMap_.find(addr) != mseService_.incomingConnectMap_.end()) {
88             mseService_.incomingConnectMap_[addr] = mseService_.incomingConnectMap_[addr] - (masInstanceId + 1);
89             if (mseService_.incomingConnectMap_[addr] == 0) {
90                 mseService_.incomingConnectMap_.erase(addr);
91                 hasEnd = true;
92             }
93         } else {
94             hasEnd = true;
95         }
96     }
97     if (hasEnd) {
98         if (mseService_.masInstanceMap_.size() > 1) {
99             OnRejectConnection(remoteAddr, masInstanceId);
100         } else {
101             OnConnectionStateChanged(remoteAddr, masInstanceId, BTConnectState::DISCONNECTED);
102         }
103     }
104     IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_OFF, PROFILE_NAME_MAP_MSE, remoteAddr);
105     mseService_.DisableComplete();
106 }
107 
OnBusy(const RawAddress & remoteAddr,bool isBusy)108 void MapMseService::Observer::OnBusy(const RawAddress &remoteAddr, bool isBusy)
109 {
110     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
111 
112     auto addr = remoteAddr.GetAddress();
113     for (auto &instance : mseService_.masInstanceMap_) {
114         if (instance.second->IsBusy(addr)) {
115             IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_MAP_MSE, remoteAddr);
116             return;
117         }
118     }
119     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_MAP_MSE, remoteAddr);
120 }
121 
OnConnectionStateChanged(const RawAddress & remoteAddr,const int masInstanceId,BTConnectState state)122 void MapMseService::Observer::OnConnectionStateChanged(
123     const RawAddress &remoteAddr, const int masInstanceId, BTConnectState state)
124 {
125     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
126 
127     mseService_.NotifyConnectionState(remoteAddr, masInstanceId, state);
128 }
129 
OnIncomingTimeout(const RawAddress & remoteAddr,const int masInstanceId)130 void MapMseService::Observer::OnIncomingTimeout(const RawAddress &remoteAddr, const int masInstanceId)
131 {
132     std::lock_guard<std::recursive_mutex> lock(mseService_.mseMapMutex_);
133     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
134     auto addr = remoteAddr.GetAddress();
135     if (mseService_.incomingConnectMap_.find(addr) != mseService_.incomingConnectMap_.end()) {
136         MSE_LOG_INFO("IncomingTimeout instance : %{public}d ", mseService_.incomingConnectMap_[addr]);
137         mseService_.incomingConnectMap_[addr] = mseService_.incomingConnectMap_[addr] - (masInstanceId + 1);
138         if (mseService_.incomingConnectMap_[addr] == 0) {
139             mseService_.incomingConnectMap_.erase(addr);
140             mseService_.mapMseObservers_.ForEach([remoteAddr](IMapMseObserver &observer) {
141                 observer.OnConnectionStateChanged(remoteAddr, static_cast<int>(BTConnectState::DISCONNECTED));
142             });
143         }
144     }
145 }
146 
OnRejectConnection(const RawAddress & remoteAddr,const int masInstanceId)147 void MapMseService::Observer::OnRejectConnection(const RawAddress &remoteAddr, const int masInstanceId)
148 {
149     std::lock_guard<std::recursive_mutex> lock(mseService_.mseMapMutex_);
150     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
151     auto addr = remoteAddr.GetAddress();
152     if (mseService_.incomingConnectMap_.find(addr) != mseService_.incomingConnectMap_.end()) {
153         MSE_LOG_INFO("Reject Connection instance : %{public}d ", mseService_.incomingConnectMap_[addr]);
154         return;
155     }
156     mseService_.RejectConnection(addr, masInstanceId);
157 }
158 
RejectConnection(std::string & addr,const int masInstanceId)159 void MapMseService::RejectConnection(std::string &addr, const int masInstanceId)
160 {
161     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
162 
163     rejectTimer_[addr] =
164         std::make_unique<utility::Timer>(std::bind(&MapMseService::ProcessReject, this, addr, masInstanceId));
165     rejectTimer_[addr]->Start(MAP_MSE_DELAY_GRANT_TIME * masInstanceMap_.size(), false);
166 }
167 
ProcessReject(std::string & addr,const int masInstanceId)168 void MapMseService::ProcessReject(std::string &addr, const int masInstanceId)
169 {
170     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
171 
172     RawAddress remoteAddr(addr);
173     mapMseObservers_.ForEach([remoteAddr](IMapMseObserver &observer) {
174         observer.OnConnectionStateChanged(remoteAddr, static_cast<int>(BTConnectState::DISCONNECTED));
175     });
176 }
177 
RegisterObserver(MapMseInstance & masInstance,const int masId)178 void MapMseService::RegisterObserver(MapMseInstance &masInstance, const int masId)
179 {
180     std::lock_guard<std::recursive_mutex> lock(mseMapMutex_);
181     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
182 
183     if (observerMap_.find(masId) != observerMap_.end()) {
184         masInstance.RegistMapObserver(*static_cast<MapObserver *>(observerMap_[masId].get()));
185     }
186 }
187 
DeregisterObserver(MapMseInstance & masInstance,const int masId)188 void MapMseService::DeregisterObserver(MapMseInstance &masInstance, const int masId)
189 {
190     std::lock_guard<std::recursive_mutex> lock(mseMapMutex_);
191     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
192 
193     if (observerMap_.find(masId) != observerMap_.end()) {
194         masInstance.DeregisterObserver(*(static_cast<MapObserver *>(observerMap_[masId].get())));
195     }
196 }
197 
NotifyConnectionState(const RawAddress & remoteAddr,const int masInstanceId,BTConnectState state)198 void MapMseService::NotifyConnectionState(const RawAddress &remoteAddr, const int masInstanceId, BTConnectState state)
199 {
200     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
201 
202     BTConnectState currentState = BTConnectState(GetConnectionState(remoteAddr));
203     MSE_LOG_INFO("CurrentState : %{public}d ", (int)currentState);
204     MSE_LOG_INFO("Change to state : %{public}d ", (int)state);
205     bool notify = false;
206     switch (currentState) {
207         case BTConnectState::DISCONNECTED:
208             if (BTConnectState::CONNECTING == state || BTConnectState::DISCONNECTED == state) {
209                 notify = true;
210             }
211             break;
212         case BTConnectState::CONNECTED:
213             if (BTConnectState::DISCONNECTING == state) {
214                 if (masInstanceMap_.find(masInstanceId) != masInstanceMap_.end()) {
215                     masInstanceMap_[masInstanceId]->SetConnectState(
216                         remoteAddr.GetAddress(), BTConnectState::DISCONNECTING);
217                 }
218                 notify = true;
219             }
220             if (BTConnectState::CONNECTED == state || BTConnectState::DISCONNECTING == state) {
221                 notify = true;
222             }
223             break;
224         case BTConnectState::CONNECTING:
225             break;
226         case BTConnectState::DISCONNECTING:
227             break;
228         default:
229             break;
230     }
231     if (notify) {
232         mapMseObservers_.ForEach([remoteAddr, state](IMapMseObserver &observer) {
233             observer.OnConnectionStateChanged(remoteAddr, static_cast<int>(state));
234         });
235     }
236 }
237 
Enable(void)238 void MapMseService::Enable(void)
239 {
240     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
241 
242     PostMessage(INIT_INSTANCE);
243     PostMessage(START_INSTANCE);
244 }
245 
ServiceStartUpComplete(bool whether)246 void MapMseService::ServiceStartUpComplete(bool whether)
247 {
248     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
249 
250     shutDownEnable_ = false;
251     GetContext()->OnEnable(PROFILE_NAME_MAP_MSE, whether);
252 }
253 
GetAccountItems(void) const254 std::vector<MapAccountItem> MapMseService::GetAccountItems(void) const
255 {
256     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
257 
258     std::vector<MapAccountItem> items;
259     items.clear();
260     for (auto &iter : stub::MapService::GetInstance()->GetEmailImAccounts()) {
261         MapAccountItem item;
262         item.id_ = iter->GetAccountId();
263         item.name_ = iter->GetName();
264         item.msgType_ = (stub::AccountType::EMAIL == iter->GetType()) ? MessageType::EMAIL : MessageType::IM;
265         item.pAuthority_ = iter->GetProviderAuthority();
266         item.uci_ = iter->GetUci();
267         item.uciPrefix_ = iter->GetUciPrefix();
268         items.push_back(std::move(item));
269     }
270     return items;
271 }
272 
InitMasInstances(void)273 void MapMseService::InitMasInstances(void)
274 {
275     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
276 
277     masInstanceMap_.clear();
278     incomingConnectMap_.clear();
279     rejectTimer_.clear();
280     int masId = MAS_ID_SMS_MMS;
281     uint16_t psm = GOEP_L2CAP_PSM_VALUE[masId];
282 
283     bool smsSupport = stub::MapService::GetInstance()->GetSystemTelephonyService().isSmsCapable;
284     if (smsSupport) {
285         masInstanceMap_[masId] = std::make_unique<MapMseInstance>(*GetDispatcher(), masId, psm, smsSupport);
286         observerMap_[masId] = std::make_unique<Observer>(*this);
287         masId++;
288     }
289     auto items = GetAccountItems();
290     for (auto &iter : items) {
291         if (masId == 0xC) {
292             break;
293         }
294         psm = GOEP_L2CAP_PSM_VALUE[masId];
295         masInstanceMap_[masId] = std::make_unique<MapMseInstance>(*GetDispatcher(), masId, psm, iter, smsSupport);
296         observerMap_[masId] = std::make_unique<Observer>(*this);
297         masId++;
298     }
299 }
300 
InstancesStarting(void)301 void MapMseService::InstancesStarting(void)
302 {
303     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
304 
305     mnsObserver_ = std::make_unique<MnsCallback>(*this);
306     mnsClient_ = std::make_unique<MapMseMnscli>(*GetDispatcher(), (MnsObserver &)*mnsObserver_);
307     bool notifyFlag = true;
308     for (auto iter = masInstanceMap_.begin(); iter != masInstanceMap_.end(); iter++) {
309         iter->second->CreateMasSdpRecord();
310         iter->second->RegistGap();
311         if (Result::SUCCEED != iter->second->Enable(*mnsClient_)) {
312             notifyFlag = false;
313         }
314         RegisterObserver(*iter->second, iter->first);
315     }
316     ServiceStartUpComplete(notifyFlag);
317 }
318 
Disable(void)319 void MapMseService::Disable(void)
320 {
321     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
322 
323     PostMessage(SHUT_DOWN);
324 }
325 
ServiceShutDown(void)326 void MapMseService::ServiceShutDown(void)
327 {
328     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
329 
330     shutDownEnable_ = true;
331     bool hasSession = false;
332     bool hasMnsConnect = false;
333     for (auto &instance : masInstanceMap_) {
334         for (auto &incoming : incomingConnectMap_) {
335             instance.second->DestroyIncomingConnect(incoming.first);
336         }
337         if (instance.second->IsConnected()) {
338             hasSession = true;
339             instance.second->DisConnect("");
340         }
341         if (instance.second->AcceptedNoCallBack(shutDownEnable_)) {
342             hasSession = true;
343         }
344     }
345     if (mnsClient_) {
346         hasMnsConnect = mnsClient_->IsDisconnected();
347     }
348     if (!hasSession && hasMnsConnect) {
349         shutDownEnable_ = false;
350         PostMessage(SHUT_DOWN_COMPLETE);
351     }
352 }
353 
DisableComplete(void)354 void MapMseService::DisableComplete(void)
355 {
356     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
357 
358     bool notEnd = true;
359     for (auto &instance : masInstanceMap_) {
360         if (instance.second->IsConnected()) {
361             notEnd = false;
362             break;
363         }
364         if (instance.second->AcceptedNoCallBack(shutDownEnable_)) {
365             notEnd = false;
366             break;
367         }
368     }
369     bool mnsNotEnd = true;
370     if (mnsClient_) {
371         if (!mnsClient_->IsDisconnected()) {
372             mnsNotEnd = false;
373         }
374     }
375     if (shutDownEnable_ && notEnd && mnsNotEnd) {
376         shutDownEnable_ = false;
377         PostMessage(SHUT_DOWN_COMPLETE);
378     }
379 }
380 
ServiceShutDownComplete(void)381 void MapMseService::ServiceShutDownComplete(void)
382 {
383     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
384 
385     for (auto iter = masInstanceMap_.begin(); iter != masInstanceMap_.end(); iter++) {
386         DeregisterObserver(*iter->second, iter->first);
387         iter->second->DestroyMasSdpRecord();
388         iter->second->UnRegistGap();
389         iter->second->Disable();
390     }
391     if (mnsClient_) {
392         mnsClient_->DeregisterL2capLPsm();
393     }
394     GetContext()->OnDisable(PROFILE_NAME_MAP_MSE, true);
395     shutDownEnable_ = false;
396 }
397 
Disconnect(const RawAddress & device)398 int MapMseService::Disconnect(const RawAddress &device)
399 {
400     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
401 
402     if (BTConnectState::DISCONNECTING == BTConnectState(GetConnectionState(device))) {
403         return RET_BAD_STATUS;
404     }
405     for (auto &instance : masInstanceMap_) {
406         if (instance.second->IsConnected(device.GetAddress()) == false) {
407             return RET_BAD_STATUS;
408         }
409     }
410     utility::Message msg(DISCONNECT);
411     std::unique_ptr<RawAddress> rawAddr = std::make_unique<RawAddress>(device.GetAddress());
412     msg.arg2_ = (void *)rawAddr.release();
413     GetDispatcher()->PostTask(std::bind(&MapMseService::ProcessMessage, this, msg));
414     return RET_NO_ERROR;
415 }
416 
DisConnectInternal(const utility::Message & msg) const417 void MapMseService::DisConnectInternal(const utility::Message &msg) const
418 {
419     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
420 
421     RawAddress *rawAddress = (RawAddress *)msg.arg2_;
422     if (!rawAddress) {
423         return;
424     }
425     for (auto &instance : masInstanceMap_) {
426         instance.second->DisConnect(rawAddress->GetAddress());
427     }
428     delete rawAddress;
429 }
430 
PostMessage(int what)431 void MapMseService::PostMessage(int what)
432 {
433     utility::Message msg(what);
434     GetDispatcher()->PostTask(std::bind(&MapMseService::ProcessMessage, this, msg));
435 }
436 
ProcessMessage(const utility::Message & msg)437 void MapMseService::ProcessMessage(const utility::Message &msg)
438 {
439     switch (msg.what_) {
440         case INIT_INSTANCE:
441             InitMasInstances();
442             break;
443         case START_INSTANCE:
444             InstancesStarting();
445             break;
446         case DISCONNECT:
447             DisConnectInternal(msg);
448             break;
449         case SHUT_DOWN:
450             ServiceShutDown();
451             break;
452         case SHUT_DOWN_COMPLETE:
453             ServiceShutDownComplete();
454             break;
455         default:
456             break;
457     }
458 }
459 
GetState(void)460 int MapMseService::GetState(void)
461 {
462     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
463 
464     int result = GetConnectState();
465     if (result & PROFILE_STATE_CONNECTED) {
466         return (int)BTConnectState::CONNECTED;
467     } else if (result & PROFILE_STATE_CONNECTING) {
468         return (int)BTConnectState::CONNECTING;
469     } else if (result & PROFILE_STATE_DISCONNECTING) {
470         return (int)BTConnectState::DISCONNECTING;
471     } else {
472         return (int)BTConnectState::DISCONNECTED;
473     }
474 }
475 
IsConnected(const RawAddress & device)476 bool MapMseService::IsConnected(const RawAddress &device)
477 {
478     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
479 
480     bool result = false;
481     for (auto &instance : masInstanceMap_) {
482         if (instance.second->IsConnected(device.GetAddress())) {
483             result = true;
484             break;
485         }
486     }
487     return result;
488 }
489 
GetDevicesByStates(std::vector<int> states)490 std::vector<RawAddress> MapMseService::GetDevicesByStates(std::vector<int> states)
491 {
492     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
493 
494     std::vector<RawAddress> devices;
495     for (auto &instance : masInstanceMap_) {
496         instance.second->GetDevicesByStates(devices, states);
497     }
498     return devices;
499 }
500 
GetConnectionState(const RawAddress & device)501 int MapMseService::GetConnectionState(const RawAddress &device)
502 {
503     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
504 
505     int result = 0;
506     for (auto &instance : masInstanceMap_) {
507         instance.second->GetDeviceState(device.GetAddress(), result);
508     }
509     MSE_LOG_INFO("Get Connection State : %{public}d", result);
510     if (result & PROFILE_STATE_DISCONNECTING) {
511         return (int)BTConnectState::DISCONNECTING;
512     } else if (result & PROFILE_STATE_CONNECTING) {
513         return (int)BTConnectState::CONNECTING;
514     } else if (result & PROFILE_STATE_CONNECTED) {
515         return (int)BTConnectState::CONNECTED;
516     } else {
517         return (int)BTConnectState::DISCONNECTED;
518     }
519 }
520 
SetConnectionStrategy(const RawAddress & device,int strategy)521 bool MapMseService::SetConnectionStrategy(const RawAddress &device, int strategy)
522 {
523     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
524 
525     IProfileConfig *config = ProfileConfig::GetInstance();
526     int value = 0;
527     std::string addr = device.GetAddress();
528     switch (BTStrategyType(strategy)) {
529         case BTStrategyType::CONNECTION_UNKNOWN:
530             return false;
531         case BTStrategyType::CONNECTION_ALLOWED:
532             value = 1;
533             break;
534         case BTStrategyType::CONNECTION_FORBIDDEN:
535             break;
536         default:
537             return false;
538     }
539     return config->SetValue(device.GetAddress(), SECTION_CONNECTION_POLICIES, PROPERTY_MAP_CONNECTION_POLICY, value);
540 }
541 
GetConnectionStrategy(const RawAddress & device)542 int MapMseService::GetConnectionStrategy(const RawAddress &device)
543 {
544     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
545 
546     IProfileConfig *config = ProfileConfig::GetInstance();
547     int value = 0;
548     if (!config->GetValue(device.GetAddress(), SECTION_CONNECTION_POLICIES, PROPERTY_MAP_CONNECTION_POLICY, value)) {
549         HILOGI("addr: %{public}s, %{public}s not found",
550             GET_ENCRYPT_ADDR(device), PROPERTY_MAP_CONNECTION_POLICY.c_str());
551         return (int)BTStrategyType::CONNECTION_UNKNOWN;
552     }
553     if (value) {
554         return (int)BTStrategyType::CONNECTION_ALLOWED;
555     } else {
556         return (int)BTStrategyType::CONNECTION_FORBIDDEN;
557     }
558 }
559 
GrantPermission(const RawAddress & device,bool allow,bool save)560 void MapMseService::GrantPermission(const RawAddress &device, bool allow, bool save)
561 {
562     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
563     GetDispatcher()->PostTask(std::bind(&MapMseService::GrantPermissionTimer, this, device, allow, save));
564 }
565 
GrantPermissionTimer(const RawAddress & device,bool allow,bool save)566 void MapMseService::GrantPermissionTimer(const RawAddress &device, bool allow, bool save)
567 {
568     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
569 
570     if (BTConnectState::CONNECTING != BTConnectState(GetConnectionState(device))) {
571         MSE_LOG_INFO("GrantPermissionTimer is returned.");
572         return;
573     }
574     std::string addr = device.GetAddress();
575     grantTimer_[addr] =
576         std::make_unique<utility::Timer>(std::bind(&MapMseService::ProcessGrantPermission, this, device, allow, save));
577     grantTimer_[addr]->Start(MAP_MSE_DELAY_GRANT_TIME * masInstanceMap_.size(), false);
578 }
579 
ProcessGrantPermission(const RawAddress & device,bool allow,bool save)580 void MapMseService::ProcessGrantPermission(const RawAddress &device, bool allow, bool save)
581 {
582     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
583     GetDispatcher()->PostTask(std::bind(&MapMseService::GrantPermissionNative, this, device, allow, save));
584 }
585 
GrantPermissionNative(const RawAddress & device,bool allow,bool save)586 void MapMseService::GrantPermissionNative(const RawAddress &device, bool allow, bool save)
587 {
588     {
589         std::lock_guard<std::recursive_mutex> lock(mseMapMutex_);
590         MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
591         std::string addr = device.GetAddress();
592         incomingConnectMap_.erase(addr);
593     }
594     for (auto &instance : masInstanceMap_) {
595         instance.second->GrantPermission(device, allow, save);
596     }
597 }
598 
Connect(const RawAddress & device)599 int MapMseService::Connect(const RawAddress &device)
600 {
601     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
602 
603     return Result::SUCCEED;
604 }
605 
GetConnectDevices(void)606 std::list<RawAddress> MapMseService::GetConnectDevices(void)
607 {
608     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
609 
610     std::list<RawAddress> devList;
611     for (auto &instance : masInstanceMap_) {
612         instance.second->GetConnectDevices(devList);
613     }
614     return devList;
615 }
616 
GetConnectState(void)617 int MapMseService::GetConnectState(void)
618 {
619     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
620 
621     uint8_t result = PROFILE_STATE_DISCONNECTED;
622     for (auto &instance : masInstanceMap_) {
623         result |= instance.second->GetConnectState();
624     }
625     return result;
626 }
627 
GetMaxConnectNum(void)628 int MapMseService::GetMaxConnectNum(void)
629 {
630     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
631 
632     int maxNum = MAP_MSE_MAX_DEFAULT_CONNECTIONS_NUMR;
633     if (AdapterConfig::GetInstance()->GetValue(SECTION_MAP_MSE_SERVICE, PROPERTY_MAX_CONNECTED_DEVICES, maxNum)) {
634         return maxNum;
635     }
636     return maxNum;
637 }
638 
RegisterObserver(IMapMseObserver & mapMseObserver)639 void MapMseService::RegisterObserver(IMapMseObserver &mapMseObserver)
640 {
641     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
642 
643     mapMseObservers_.Register(mapMseObserver);
644 }
645 
DeregisterObserver(IMapMseObserver & mapMseObserver)646 void MapMseService::DeregisterObserver(IMapMseObserver &mapMseObserver)
647 {
648     MSE_LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
649 
650     mapMseObservers_.Deregister(mapMseObserver);
651 }
652 REGISTER_CLASS_CREATOR(MapMseService);
653 }  // namespace bluetooth
654 }  // namespace OHOS