• 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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines map mce service object.
21  *
22  */
23 
24 /**
25  * @file map_mce_service.cpp
26  *
27  * @brief map mce source file .
28  *
29  */
30 
31 #include "map_mce_service.h"
32 #include "adapter_config.h"
33 #include "class_creator.h"
34 #include "log.h"
35 #include "log_util.h"
36 #include "map_mce_types.h"
37 #include "map_mce_instance_stm.h"
38 #include "map_mce_mns_server.h"
39 #include "profile_config.h"
40 #include "profile_service_manager.h"
41 #include "sdp.h"
42 
43 namespace OHOS {
44 namespace bluetooth {
45 // c language function declare
MceAnalyseSupportedFeatrueAttribute(int & supportedFeatrue,const SdpService & servicePointer)46 int MceAnalyseSupportedFeatrueAttribute(int &supportedFeatrue, const SdpService &servicePointer)
47 {
48     bool findInstanceFeature = false;
49     int ret = RET_NO_ERROR;
50     SdpAttribute *tempAttribute = servicePointer.attribute;
51     for (int attSeqCount = 0; attSeqCount < servicePointer.attributeNumber; attSeqCount++, tempAttribute++) {
52         if (tempAttribute->attributeId == MAP_MCE_SUPPORTED_FEATURES_ATTRIBUTE_ID) {
53             supportedFeatrue = *(uint32_t *)tempAttribute->attributeValue;
54             findInstanceFeature = true;
55             break;
56         }
57     }
58     // check param value
59     if (!findInstanceFeature) {
60         // param error
61         ret = RET_BAD_PARAM;
62         LOG_ERROR("%{public}s param error:feature=%{public}d", __PRETTY_FUNCTION__, findInstanceFeature);
63     }
64     return ret;
65 }
66 
MapMceGetSupportFeatureSdpSearchCb(const BtAddr * addr,const SdpService * serviceAry,uint16_t serviceNum,void * context)67 void MapMceGetSupportFeatureSdpSearchCb(
68     const BtAddr *addr, const SdpService *serviceAry, uint16_t serviceNum, void *context)
69 {
70     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
71 
72     int supportedFeatrue = 0;
73     int supportedFeatrueAll = 0;
74     int finishId = MSG_MCESERVICE_GET_SURPORT_FEATURES_FINISH;
75     int failedId = MSG_MCESERVICE_GET_SURPORT_FEATURES_FAILED;
76     const SdpService *tempServicePointer = serviceAry;
77     IProfileManager *serviceMgr = IProfileManager::GetInstance();
78     if ((serviceMgr == nullptr) || (addr == nullptr) || (serviceNum == 0)) {
79         LOG_ERROR("%{public}s no service mgr, serviceNum= %{public}d", __PRETTY_FUNCTION__, int(serviceNum));
80         return;
81     }
82     auto mnsService = static_cast<MapMceService *>(serviceMgr->GetProfileService(PROFILE_NAME_MAP_MCE));
83     if (mnsService == nullptr) {
84         LOG_ERROR("%{public}s mnsService is nullptr", __PRETTY_FUNCTION__);
85         return;
86     }
87     BtAddr *argPrt = new (std::nothrow)BtAddr;
88     if (argPrt == nullptr) {
89         LOG_ERROR("%{public}s argPrt is nullptr", __PRETTY_FUNCTION__);
90         return;
91     }
92     utility::Message msg(failedId);
93     *argPrt = *addr;
94     msg.arg2_ = (void *)argPrt;
95     for (int serviceCount = 0; serviceCount < serviceNum; serviceCount++, tempServicePointer++) {
96         // check param array
97         if (tempServicePointer->attributeNumber == 0) {
98             mnsService->PostMessage(msg);
99             LOG_ERROR("%{public}s param error:attributeNumber is null", __PRETTY_FUNCTION__);
100             return;
101         }
102         int ret = MceAnalyseSupportedFeatrueAttribute(supportedFeatrue, *tempServicePointer);
103         // check sdp param value
104         if (ret != RET_NO_ERROR) {
105             mnsService->PostMessage(msg);
106             LOG_ERROR("%{public}s mot found supportedFeatrue", __PRETTY_FUNCTION__);
107             return;
108         }
109         supportedFeatrueAll |= supportedFeatrue;  // make param
110     }
111     msg.what_ = finishId;
112     msg.arg1_ = supportedFeatrueAll;
113     mnsService->PostMessage(msg);
114     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
115 }
116 
MapMceService()117 MapMceService::MapMceService() : utility::Context(PROFILE_NAME_MAP_MCE, "1.4.2")
118 {
119     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
120     serviceState_ = MAP_MCE_STATE_SHUTDOWN;
121     serviceDispatcher_ = GetDispatcher();
122     if (serviceDispatcher_ == nullptr) {
123         LOG_INFO("%{public}s dispatch error", __PRETTY_FUNCTION__);
124     }
125     mnsServer_ = nullptr;
126     serviceBtDeviceInstMgrMap_.clear();
127     notificationRegistration_ = false;
128     insDefaultConfig_.l2capMtu = MCE_INSTANCE_CLIENT_OBEX_MTU;
129     insDefaultConfig_.rfcommMtu = MCE_INSTANCE_CLIENT_OBEX_MTU;
130     insDefaultConfig_.isSupportSrm = true;
131     insDefaultConfig_.deviceId = 0;
132     insDefaultConfig_.singleInstMode = false;
133     insDefaultConfig_.singleInstanceId = 0;
134     insDefaultConfig_.maxOfDevice = MCE_MAX_OF_CONNECTED_DEVICES;
135     insDefaultConfig_.maxOfGetUnread = MAP_MAX_LIST_COUNT_FOR_GET_UNREAD_MESSAGE;
136 
137     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
138 }
139 
~MapMceService()140 MapMceService::~MapMceService()
141 {
142     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
143     // release all client stm memory
144     RemoveAllDevice();
145     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
146 }
147 
GetContext()148 utility::Context *MapMceService::GetContext()
149 {
150     return this;
151 }
152 
RegisterObserver(IProfileMapMceObserver & observer)153 void MapMceService::RegisterObserver(IProfileMapMceObserver &observer)
154 {
155     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
156     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
157 
158     serviceRpcCallbackMgr_.RegisterObserver(observer);
159     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
160 }
161 
DeregisterObserver(IProfileMapMceObserver & observer)162 void MapMceService::DeregisterObserver(IProfileMapMceObserver &observer)
163 {
164     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
165     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
166 
167     serviceRpcCallbackMgr_.DeregisterObserver(observer);
168     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
169 }
170 
IsConnected(const RawAddress & device)171 bool MapMceService::IsConnected(const RawAddress &device)
172 {
173     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
174     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
175 
176     bool ret = false;
177     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
178     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
179         LOG_ERROR("%{public}s error:RET_BAD_STATUS", __PRETTY_FUNCTION__);
180         return false;
181     }
182     // find the device
183     std::string tempDev = device.GetAddress();
184     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
185     if (it != serviceBtDeviceInstMgrMap_.end()) {
186         if (it->second->GetCurrentDeviceState() == MAP_MCE_DEV_STATE_CONNECTED) {
187             ret = true;
188         }
189     }
190     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
191     return ret;
192 }
193 
GetDevicesByStates(const std::vector<int> & statusList)194 std::vector<RawAddress> MapMceService::GetDevicesByStates(const std::vector<int> &statusList)
195 {
196     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
197     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
198 
199     std::vector<RawAddress> deviceList;
200     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
201         LOG_ERROR("%{public}s error:RET_BAD_STATUS", __PRETTY_FUNCTION__);
202         return deviceList;
203     }
204     // find the device
205     std::unique_ptr<RawAddress> rawAddressPtr = nullptr;
206     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); it++) {
207         for (auto itState = statusList.begin(); itState != statusList.end(); itState++) {
208             if (it->second->GetCurrentDeviceState() == *itState) {
209                 rawAddressPtr = std::make_unique<RawAddress>(it->second->GetBtDevice());
210                 deviceList.push_back(*rawAddressPtr);
211                 break;
212             }
213         }
214     }
215     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
216     return deviceList;
217 }
218 
SetConnectionStrategy(const RawAddress & device,int strategy)219 int MapMceService::SetConnectionStrategy(const RawAddress &device, int strategy)
220 {
221     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
222     IProfileConfig *config = ProfileConfig::GetInstance();
223     if ((serviceState_ != MAP_MCE_STATE_STARTUP) || (config == nullptr)) {
224         LOG_ERROR(
225             "%{public}s state error: service state=%{public}d or IProfileConfig is NULL", __PRETTY_FUNCTION__, int(serviceState_));
226         return RET_BAD_STATUS;
227     }
228     // param error
229     if ((BTStrategyType(strategy) != BTStrategyType::CONNECTION_ALLOWED) &&
230         (BTStrategyType(strategy) != BTStrategyType::CONNECTION_FORBIDDEN)) {
231         LOG_ERROR("%{public}s strategy type unknown", __PRETTY_FUNCTION__);
232         return RET_BAD_PARAM;
233     }
234     // save the value
235     int value;
236     if (BTStrategyType(strategy) == BTStrategyType::CONNECTION_ALLOWED) {
237         value = 1;
238     } else {
239         value = 0;
240     }
241     bool ret = config->SetValue(
242         device.GetAddress(), SECTION_CONNECTION_POLICIES, PROPERTY_MAP_CLIENT_CONNECTION_POLICY, value);
243     // set success
244     if (ret) {
245         // check dispatcher
246         utility::Dispatcher *dispatcher = GetDispatcher();
247         if (dispatcher != nullptr) {
248             if (value == 1) {
249                 // service function execute
250                 dispatcher->PostTask(std::bind(&MapMceService::ConnectInternal, this, device, false, 0));
251             } else {
252                 // service function execute
253                 dispatcher->PostTask(std::bind(&MapMceService::DisconnectInternal, this, device));
254             }
255         } else {
256             LOG_ERROR("%{public}s dispatcher error", __PRETTY_FUNCTION__);
257         }
258         LOG_INFO("%{public}s success end", __PRETTY_FUNCTION__);
259         return RET_NO_ERROR;
260     } else {
261         LOG_ERROR("%{public}s error end", __PRETTY_FUNCTION__);
262         return RET_BAD_STATUS;
263     }
264 }
265 
GetConnectionStrategy(const RawAddress & device) const266 int MapMceService::GetConnectionStrategy(const RawAddress &device) const
267 {
268     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
269     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
270         LOG_ERROR("%{public}s error: startup error", __PRETTY_FUNCTION__);
271         return int(BTStrategyType::CONNECTION_FORBIDDEN);
272     }
273     IProfileConfig *config = ProfileConfig::GetInstance();
274     int value = 0;
275     const std::string dev = device.GetAddress();
276     if (!config->GetValue(dev, SECTION_CONNECTION_POLICIES, PROPERTY_MAP_CLIENT_CONNECTION_POLICY, value)) {
277         LOG_DEBUG("%{public}s %{public}s not found", dev.c_str(), PROPERTY_MAP_CLIENT_CONNECTION_POLICY.c_str());
278         return int(BTStrategyType::CONNECTION_UNKNOWN);
279     }
280     if (value) {
281         return int(BTStrategyType::CONNECTION_ALLOWED);
282     } else {
283         return int(BTStrategyType::CONNECTION_FORBIDDEN);
284     }
285 }
286 
GetUnreadMessages(const RawAddress & device,MapMessageType type,uint8_t max)287 int MapMceService::GetUnreadMessages(const RawAddress &device, MapMessageType type, uint8_t max)
288 {
289     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
290     IProfileGetMessagesListingParameters para;
291     para.folder = u"inbox";
292     para.MaxListCount = max;  // max of items
293     para.ListStartOffset = 0;
294     para.SubjectLength = MCE_MAX_LENGTH_10;  // 1-255
295     para.ParameterMask = MAP_GETMESSAGELIST_PARAMETER_MASK_SUBJECT | MAP_GETMESSAGELIST_PARAMETER_MASK_TYPE |
296                          MAP_GETMESSAGELIST_PARAMETER_MASK_RECEPTION_STATUS | MAP_GETMESSAGELIST_PARAMETER_MASK_READ |
297                          MAP_GETMESSAGELIST_PARAMETER_MASK_SENT;
298     para.FilterMessageType = MAP_FILTER_MESSAGE_MASK_NO_FILTERING;  // get alltype
299     para.FilterPeriodBegin = "";
300     para.FilterPeriodEnd = "";
301     para.FilterReadStatus = MAP_FILTER_READ_STATUS_MASK_UNREAD;
302     para.FilterRecipient = "";
303     para.FilterOriginator = "";
304     para.FilterPriority = MAP_FILTER_PRIORITY_MASK_NO_FILTERING;
305     para.ConversationID = "";
306     para.FilterMessageHandle = "";
307 
308     MapMceRequestConfig cfg;
309     cfg.maxOfGetUnread = insDefaultConfig_.maxOfGetUnread;
310 
311     utility::Message outMsg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
312     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetUreadMessages>(type, para);
313     reqPtr->SetRequestConfig(cfg);
314     int ret = SendRequestToConnectedDevice(device, outMsg, reqPtr);
315     if (ret != RET_NO_ERROR) {
316         reqPtr = nullptr;
317     }
318     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
319     return ret;
320 }
321 
GetSupportedFeatures(const RawAddress & device)322 int MapMceService::GetSupportedFeatures(const RawAddress &device)
323 {
324     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
325     int ret = RET_NO_ERROR;
326     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
327         LOG_ERROR("%{public}s error: startup error", __PRETTY_FUNCTION__);
328         return RET_BAD_STATUS;
329     }
330     // check dispatcher
331     utility::Dispatcher *dispatcher = GetDispatcher();
332     if (dispatcher == nullptr) {
333         LOG_INFO("%{public}s dispatcher error", __PRETTY_FUNCTION__);
334         return RET_NO_SUPPORT;
335     }
336     dispatcher->PostTask(std::bind(&MapMceService::CheckSdpForGetSupportedFeatures, this, device));
337     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
338     return ret;
339 }
340 
SendMessage(const RawAddress & device,const IProfileSendMessageParameters & msg)341 int MapMceService::SendMessage(const RawAddress &device, const IProfileSendMessageParameters &msg)
342 {
343     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
344     // if version_property is 1.1, need check the device's version
345     if ((msg.Charset == MapCharsetType::INVALID) || (msg.bmessage_.folder_property == u"")) {
346         LOG_ERROR("%{public}s Charset or Folder null", __PRETTY_FUNCTION__);
347         return RET_BAD_PARAM;
348     }
349     if (msg.MessageHandle != "") {
350         if ((msg.Attachment == MapAttachmentType::INVALID) || (msg.ModifyText == MapModifyTextType::INVALID)) {
351             LOG_ERROR("%{public}s have MessageHandle ,but no Attachment or ModifyText", __PRETTY_FUNCTION__);
352             return RET_BAD_PARAM;
353         }
354     }
355     if (msg.bmessage_.envelope_.maxLevelOfEnvelope_ == 0 ||
356         msg.bmessage_.envelope_.maxLevelOfEnvelope_ > MCE_RECIPIENT_LEVEL3) {
357         LOG_ERROR("%{public}s envelope_ level error = %{public}d", __PRETTY_FUNCTION__, msg.bmessage_.envelope_.maxLevelOfEnvelope_);
358         return RET_BAD_PARAM;
359     }
360     utility::Message outMsg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
361     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestPushMessage>(msg);
362     int ret = SendRequestToConnectedDevice(device, outMsg, reqPtr);
363     if (ret != RET_NO_ERROR) {
364         reqPtr = nullptr;
365     }
366     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
367     return ret;
368 }
369 
SetNotificationFilter(const RawAddress & device,const int mask)370 int MapMceService::SetNotificationFilter(const RawAddress &device, const int mask)
371 {
372     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
373     if (!notificationRegistration_) {
374         LOG_ERROR("%{public}s error: MNS server FALSE", __PRETTY_FUNCTION__);
375         return RET_BAD_STATUS;
376     }
377     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
378     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestSetNotificationFilter>(mask);
379     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
380     if (ret != RET_NO_ERROR) {
381         reqPtr = nullptr;
382     }
383     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
384     return ret;
385 }
386 
SetNotificationRegistration(const RawAddress & device,const bool on)387 int MapMceService::SetNotificationRegistration(const RawAddress &device, const bool on)
388 {
389     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
390     if (!notificationRegistration_) {
391         LOG_ERROR("%{public}s error: MNS server FALSE", __PRETTY_FUNCTION__);
392         return RET_BAD_STATUS;
393     }
394     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
395     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestSetNotificationRegistration>(on);
396     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
397     if (ret != RET_NO_ERROR) {
398         reqPtr = nullptr;
399     }
400     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
401     return ret;
402 }
403 
GetMessagesListing(const RawAddress & device,const IProfileGetMessagesListingParameters & para)404 int MapMceService::GetMessagesListing(const RawAddress &device, const IProfileGetMessagesListingParameters &para)
405 {
406     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
407     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
408     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetMessagesListing>(para);
409     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
410     if (ret != RET_NO_ERROR) {
411         reqPtr = nullptr;
412     }
413     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
414     return ret;
415 }
416 
GetMessage(const RawAddress & device,MapMessageType type,const std::u16string & msgHandle,const IProfileGetMessageParameters & para)417 int MapMceService::GetMessage(const RawAddress &device, MapMessageType type, const std::u16string &msgHandle,
418     const IProfileGetMessageParameters &para)
419 {
420     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
421     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
422     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetMessage>(msgHandle, para);
423     reqPtr->SetSupportMessageType(MapMessageTypeToIprofileMask(type));
424     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
425     if (ret != RET_NO_ERROR) {
426         reqPtr = nullptr;
427     }
428     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
429     return ret;
430 }
431 
GetMessageWithReqCfg(const RawAddress & device,MapMessageType type,const std::u16string & msgHandle,const IProfileGetMessageParameters & para,const MapMceRequestConfig & cfg)432 int MapMceService::GetMessageWithReqCfg(const RawAddress &device, MapMessageType type, const std::u16string &msgHandle,
433     const IProfileGetMessageParameters &para, const MapMceRequestConfig &cfg)
434 {
435     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
436     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
437     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetMessage>(msgHandle, para);
438     reqPtr->SetSupportMessageType(MapMessageTypeToIprofileMask(type));
439     reqPtr->SetRequestConfig(cfg);
440     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
441     if (ret != RET_NO_ERROR) {
442         reqPtr = nullptr;
443     }
444     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
445     return ret;
446 }
447 
UpdateInbox(const RawAddress & device,MapMessageType type)448 int MapMceService::UpdateInbox(const RawAddress &device, MapMessageType type)
449 {
450     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
451     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
452     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestUpdateInbox>();
453     reqPtr->SetSupportMessageType(MapMessageTypeToIprofileMask(type));
454     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
455     if (ret != RET_NO_ERROR) {
456         reqPtr = nullptr;
457     }
458     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
459     return ret;
460 }
461 
GetConversationListing(const RawAddress & device,const IProfileGetConversationListingParameters & para)462 int MapMceService::GetConversationListing(
463     const RawAddress &device, const IProfileGetConversationListingParameters &para)
464 {
465     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
466     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
467     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetConversationListing>(para);
468     reqPtr->SetSupportMessageType(MAP_MCE_SUPPORTED_MESSAGE_TYPE_IM);
469     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
470     if (ret != RET_NO_ERROR) {
471         reqPtr = nullptr;
472     }
473     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
474     return ret;
475 }
476 
SetMessageStatus(const RawAddress & device,MapMessageType type,const IProfileSetMessageStatus & msgStatus)477 int MapMceService::SetMessageStatus(
478     const RawAddress &device, MapMessageType type, const IProfileSetMessageStatus &msgStatus)
479 {
480     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
481     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
482     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestSetMessageStatus>(
483         msgStatus.msgHandle, msgStatus.statusIndicator, msgStatus.statusValue, msgStatus.extendedData);
484     reqPtr->SetSupportMessageType(MapMessageTypeToIprofileMask(type));
485     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
486     if (ret != RET_NO_ERROR) {
487         reqPtr = nullptr;
488     }
489     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
490     return ret;
491 }
492 
SetOwnerStatus(const RawAddress & device,const IProfileSetOwnerStatusParameters & para)493 int MapMceService::SetOwnerStatus(const RawAddress &device, const IProfileSetOwnerStatusParameters &para)
494 {
495     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
496     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
497     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestSetOwnerStatus>(para);
498     reqPtr->SetSupportMessageType(MAP_MCE_SUPPORTED_MESSAGE_TYPE_IM);
499     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
500     if (ret != RET_NO_ERROR) {
501         reqPtr = nullptr;
502     }
503     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
504     return ret;
505 }
506 
GetOwnerStatus(const RawAddress & device,const std::string & conversationId)507 int MapMceService::GetOwnerStatus(const RawAddress &device, const std::string &conversationId)
508 {
509     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
510     utility::Message msg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
511     std::unique_ptr<MapMceInstanceRequest> reqPtr = std::make_unique<MapMceRequestGetOwnerStatus>(conversationId);
512     reqPtr->SetSupportMessageType(MAP_MCE_SUPPORTED_MESSAGE_TYPE_IM);
513     int ret = SendRequestToConnectedDevice(device, msg, reqPtr);
514     if (ret != RET_NO_ERROR) {
515         reqPtr = nullptr;
516     }
517     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
518     return ret;
519 }
520 
Connect(const RawAddress & device)521 int MapMceService::Connect(const RawAddress &device)  // override
522 {
523     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
524     // check service status
525     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
526         LOG_ERROR("%{public}s connect startup error", __PRETTY_FUNCTION__);
527         return RET_BAD_STATUS;
528     }
529     // check dispatcher
530     utility::Dispatcher *dispatcher = GetDispatcher();
531     if (dispatcher == nullptr) {
532         LOG_ERROR("%{public}s Connect dispatcher error", __PRETTY_FUNCTION__);
533         return RET_BAD_STATUS;
534     }
535 
536     RawAddress rawDevice(device.GetAddress());
537     BTStrategyType strate = BTStrategyType(GetConnectionStrategy(rawDevice));
538     if (strate == BTStrategyType::CONNECTION_FORBIDDEN) {
539         return RET_BAD_STATUS;
540     }
541 
542     // check device connected status
543     int checkResult = CheckDeviceConnectStatus(rawDevice);
544     if (checkResult != RET_NO_ERROR) {
545         LOG_INFO("%{public}s device status error", __PRETTY_FUNCTION__);
546         return RET_BAD_STATUS;
547     }
548     // service function execute
549     dispatcher->PostTask(std::bind(&MapMceService::ConnectInternal, this, rawDevice, false, 0));
550     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
551     return RET_NO_ERROR;
552 }
553 
ConnectInstance(const RawAddress & device,int instanceId)554 int MapMceService::ConnectInstance(const RawAddress &device, int instanceId)
555 {
556     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
557     // check service status
558     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
559         LOG_ERROR("%{public}s ConnectInstance startup error", __PRETTY_FUNCTION__);
560         return RET_BAD_STATUS;
561     }
562     // check dispatcher
563     utility::Dispatcher *dispatcher = GetDispatcher();
564     if (dispatcher == nullptr) {
565         LOG_INFO("%{public}s ConnectInstance dispatcher error", __PRETTY_FUNCTION__);
566         return RET_BAD_STATUS;
567     }
568     RawAddress rawDevice(device.GetAddress());
569     BTStrategyType strate = BTStrategyType(GetConnectionStrategy(rawDevice));
570     if (strate == BTStrategyType::CONNECTION_FORBIDDEN) {
571         return RET_BAD_STATUS;
572     }
573     // check device connected status
574     int checkResult = CheckDeviceConnectStatus(rawDevice);
575     if (checkResult != RET_NO_ERROR) {
576         LOG_INFO("%{public}s device status error", __PRETTY_FUNCTION__);
577         return RET_BAD_STATUS;
578     }
579     // service function execute
580     dispatcher->PostTask(std::bind(&MapMceService::ConnectInternal, this, rawDevice, true, instanceId));
581     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
582     return RET_NO_ERROR;
583 }
584 
Disconnect(const RawAddress & device)585 int MapMceService::Disconnect(const RawAddress &device)  // override
586 {
587     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
588     // check service status
589     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
590         LOG_ERROR("%{public}s Disconnect error", __PRETTY_FUNCTION__);
591         return RET_BAD_STATUS;
592     }
593     // check dispatcher
594     utility::Dispatcher *dispatcher = GetDispatcher();
595     if (dispatcher == nullptr) {
596         LOG_INFO("%{public}s Disconnect dispatcher error", __PRETTY_FUNCTION__);
597         return RET_BAD_STATUS;
598     }
599     // check device connected status
600     RawAddress rawDevice(device.GetAddress());
601     int checkResult = CheckDeviceDisconnectStatus(rawDevice);
602     if (checkResult != RET_NO_ERROR) {
603         LOG_INFO("%{public}s device status error", __PRETTY_FUNCTION__);
604         return RET_BAD_STATUS;
605     }
606     // service function execute
607     dispatcher->PostTask(std::bind(&MapMceService::DisconnectInternal, this, rawDevice));
608     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
609     return RET_NO_ERROR;
610 }
611 
CountSendingRequest(const RawAddress & device,MceRequestType requestType)612 int MapMceService::CountSendingRequest(const RawAddress &device, MceRequestType requestType)
613 {
614     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
615     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
616 
617     int retSum = 0;
618     // find the device
619     std::string tempDev = device.GetAddress();
620     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
621     // if device in the map
622     if (it != serviceBtDeviceInstMgrMap_.end()) {
623         retSum = it->second->CountSendingRequest(requestType);
624     }
625     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
626     return retSum;
627 }
628 
CheckDeviceConnectStatus(const RawAddress & device)629 int MapMceService::CheckDeviceConnectStatus(const RawAddress &device)
630 {
631     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
632     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
633 
634     int ret = RET_NO_ERROR;
635     // find the device
636     std::string tempDev = device.GetAddress();
637     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
638     // if device in the map
639     if (it != serviceBtDeviceInstMgrMap_.end()) {
640         if (it->second->GetCurrentDeviceState() != MAP_MCE_DEV_STATE_DISCONNECTED) {
641             ret = RET_BAD_STATUS;
642         }
643         LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
644         return ret;
645     }
646     if (serviceBtDeviceInstMgrMap_.size() >= insDefaultConfig_.maxOfDevice) {
647         ret = RET_NO_SUPPORT;
648         for (auto it2 = serviceBtDeviceInstMgrMap_.begin(); it2 != serviceBtDeviceInstMgrMap_.end(); ++it2) {
649             // if it have disconnect devcie in the list
650             if (it2->second->GetCurrentDeviceState() == MAP_MCE_DEV_STATE_DISCONNECTED) {
651                 ret = RET_NO_ERROR;
652             }
653         }
654     }
655     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
656     return ret;
657 }
658 
CheckDeviceDisconnectStatus(const RawAddress & device)659 int MapMceService::CheckDeviceDisconnectStatus(const RawAddress &device)
660 {
661     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
662     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
663 
664     int ret = RET_NO_ERROR;
665     // find the device
666     std::string tempDev = device.GetAddress();
667     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
668     // if device in the map
669     if (it != serviceBtDeviceInstMgrMap_.end()) {
670         if (it->second->GetCurrentDeviceState() != MAP_MCE_DEV_STATE_CONNECTED) {
671             ret = RET_BAD_STATUS;
672         }
673     } else {
674         // device is not in the list
675         ret = RET_NO_SUPPORT;
676     }
677 
678     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
679     return ret;
680 }
681 
SendRequestToConnectedDevice(const RawAddress & device,utility::Message msg,std::unique_ptr<MapMceInstanceRequest> & req)682 int MapMceService::SendRequestToConnectedDevice(
683     const RawAddress &device, utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req)
684 {
685     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
686     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
687 
688     int ret = RET_BAD_STATUS;
689     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
690         LOG_ERROR("%{public}s error:RET_BAD_STATUS", __PRETTY_FUNCTION__);
691         return ret;
692     }
693     // ready
694     std::string tempDev = device.GetAddress();
695     // find bt device
696     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
697     // find ok
698     if (it != serviceBtDeviceInstMgrMap_.end()) {
699         // check max send request
700         int retSum = it->second->CountSendingRequest(MCE_REQUEST_TYPE_ALL);
701         if (retSum < MCE_MAX_OF_SENDING_REQUEST) {
702             // api only valid in connected state, if not valid return error at now
703             if (it->second->GetCurrentDeviceState() == MAP_MCE_DEV_STATE_CONNECTED) {
704                 // send command to the state machine
705                 ret = it->second->PostMessageWithRequest(msg, req);
706             } else {
707                 LOG_ERROR("%{public}s device not connected", __PRETTY_FUNCTION__);
708             }
709         } else {
710             LOG_ERROR("%{public}s error, MCE_MAX_OF_SENDING_REQUEST", __PRETTY_FUNCTION__);
711         }
712     } else {
713         LOG_ERROR("%{public}s not fount the device", __PRETTY_FUNCTION__);
714     }
715     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
716     return ret;
717 }
718 
ConnectInternal(const RawAddress & device,bool sInsMode,int sInsId)719 int MapMceService::ConnectInternal(const RawAddress &device, bool sInsMode, int sInsId)
720 {
721     HILOGI("address=%{public}s", GET_ENCRYPT_ADDR(device));
722     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
723 
724     insDefaultConfig_.singleInstMode = sInsMode;
725     insDefaultConfig_.singleInstanceId = sInsId;
726     LOG_INFO("%{public}s singleInstMode=%{public}d,singleInstanceId=%{public}d ", __PRETTY_FUNCTION__, sInsMode, sInsId);
727 
728     int ret = RET_NO_ERROR;
729     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
730         LOG_ERROR("%{public}s error return", __PRETTY_FUNCTION__);
731         return RET_BAD_STATUS;
732     }
733     // find the device
734     std::string tempDev = device.GetAddress();
735     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
736     if (it == serviceBtDeviceInstMgrMap_.end()) {
737         // device did not in the map
738         // remove the no useful device and instance if the map is max
739         RemoveNoUseDeviceAndInstance();
740         // insert the instance and create the client stm
741         if (serviceBtDeviceInstMgrMap_.size() < insDefaultConfig_.maxOfDevice) {
742             insDefaultConfig_.deviceId++;
743             auto deviceCtl1 = std::make_unique<MapMceDeviceCtrl>(
744                 tempDev, *this, notificationRegistration_, insDefaultConfig_, serviceRpcCallbackMgr_);
745             utility::Message outMsg1(MSG_MCEDEVICE_REQ_DEVICE_CONNECT);
746             deviceCtl1->ProcessMessage(outMsg1);
747             serviceBtDeviceInstMgrMap_.insert(
748                 std::pair<const std::string, std::unique_ptr<MapMceDeviceCtrl>>(tempDev, std::move(deviceCtl1)));
749             // start device connect
750         } else {
751             // error , device max
752             ret = RET_NO_SUPPORT;
753             LOG_ERROR("%{public}s error:connect max", __PRETTY_FUNCTION__);
754         }
755     } else {
756         // device is in the map , refresh config and connect again
757         it->second->SetDeviceCtlConfig(insDefaultConfig_);
758         utility::Message outMsg2(MSG_MCEDEVICE_REQ_DEVICE_CONNECT);
759         it->second->ProcessMessage(outMsg2);
760     }
761     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
762     return ret;
763 }
764 
DisconnectInternal(const RawAddress & device)765 int MapMceService::DisconnectInternal(const RawAddress &device)
766 {
767     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
768     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
769 
770     int ret = RET_NO_ERROR;
771     if (serviceState_ != MAP_MCE_STATE_STARTUP) {
772         LOG_ERROR("%{public}s DisconnectInternal startup error", __PRETTY_FUNCTION__);
773         return RET_BAD_STATUS;
774     }
775     // find the device
776     std::string tempDev = device.GetAddress();
777     auto it = serviceBtDeviceInstMgrMap_.find(tempDev);
778     // device is in the map
779     if (it != serviceBtDeviceInstMgrMap_.end()) {
780         // request disconnect
781         utility::Message outMsg(MSG_MCEDEVICE_REQ_DEVICE_DISCONNECT);
782         it->second->PostMessage(outMsg);
783     }
784     // find device in the device map
785     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
786     return ret;
787 }
788 
789 // find and remove the disconnect bluetooth device
RemoveNoUseDeviceAndInstance()790 void MapMceService::RemoveNoUseDeviceAndInstance()
791 {
792     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
793     // check map length
794     if (serviceBtDeviceInstMgrMap_.size() < insDefaultConfig_.maxOfDevice) {
795         return;
796     }
797     // check iterator in map
798     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); ++it) {
799         if (it->second->GetCurrentDeviceState() == MAP_MCE_DEV_STATE_DISCONNECTED) {
800             serviceBtDeviceInstMgrMap_.erase(it);
801             break;
802         }
803     }
804     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
805 }
806 
RemoveAllDevice()807 void MapMceService::RemoveAllDevice()
808 {
809     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
810     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
811 
812     serviceBtDeviceInstMgrMap_.clear();
813     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
814     return;
815 }
816 
MapMessageTypeToIprofileMask(MapMessageType type)817 int MapMceService::MapMessageTypeToIprofileMask(MapMessageType type)
818 {
819     uint8_t serviceMask;
820     switch (type) {
821         case MapMessageType::SMS_GSM:
822             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_SMS_GSM;
823             break;
824         case MapMessageType::SMS_CDMA:
825             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_SMS_CDMA;
826             break;
827         case MapMessageType::MMS:
828             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_MMS;
829             break;
830         case MapMessageType::EMAIL:
831             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_EMAIL;
832             break;
833         case MapMessageType::IM:
834             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_IM;
835             break;
836         default:
837             serviceMask = MAP_MCE_SUPPORTED_MESSAGE_TYPE_ALL;
838             break;
839     }
840     return int(serviceMask);
841 }
842 
GetConnectDevices()843 std::list<RawAddress> MapMceService::GetConnectDevices()
844 {
845     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
846     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
847 
848     std::list<RawAddress> devList;
849     // check iterator in client device map
850     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); ++it) {
851         if (it->second != nullptr) {
852             if (it->second->GetCurrentDeviceState() == MAP_MCE_DEV_STATE_CONNECTED) {
853                 RawAddress rawAddress(it->second->GetBtDevice());
854                 devList.push_back(rawAddress);
855             }
856         }
857     }
858     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
859     return devList;
860 }
861 
GetConnectState()862 int MapMceService::GetConnectState()
863 {
864     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
865     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
866 
867     MapMceDeviceStateType state;
868     int profileState = 0;
869     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
870     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); it++) {
871         if (it->second != nullptr) {
872             state = it->second->GetCurrentDeviceState();
873             profileState |= it->second->DeviceStateConvertToProfileState(state);
874         }
875     }
876     // not device connect
877     if (profileState == 0) {
878         profileState = PROFILE_STATE_DISCONNECTED;
879     }
880     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
881     return profileState;
882 }
883 
GetDeviceConnectState(const RawAddress & device)884 int MapMceService::GetDeviceConnectState(const RawAddress &device)
885 {
886     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
887     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
888 
889     MapMceDeviceStateType ret = MAP_MCE_DEV_STATE_DISCONNECTED;
890     auto it = serviceBtDeviceInstMgrMap_.find(device.GetAddress());
891     if (it != serviceBtDeviceInstMgrMap_.end()) {
892         if (it->second != nullptr) {
893             ret = it->second->GetCurrentDeviceState();
894         }
895     }
896     LOG_INFO("%{public}s end,status=%{public}d", __PRETTY_FUNCTION__, ret);
897     return ret;  // return state
898 }
899 
GetFolderListing(const RawAddress & device,uint16_t maxOfListCount,uint16_t startOffset)900 int MapMceService::GetFolderListing(const RawAddress &device, uint16_t maxOfListCount, uint16_t startOffset)
901 {
902     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
903     int ret;
904     utility::Message outMsg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
905     std::unique_ptr<MapMceInstanceRequest> reqPtr =
906         std::make_unique<MapMceRequestGetFolderListing>(maxOfListCount, startOffset);
907     ret = SendRequestToConnectedDevice(device, outMsg, reqPtr);
908     if (ret != RET_NO_ERROR) {
909         reqPtr = nullptr;
910     }
911     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
912     return ret;
913 }
914 
SetPath(const RawAddress & device,const uint8_t flags,const std::u16string & folder,std::vector<std::u16string> & folderList)915 int MapMceService::SetPath(const RawAddress &device, const uint8_t flags,
916     const std::u16string &folder, std::vector<std::u16string> &folderList)
917 {
918     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
919     int ret;
920     utility::Message outMsg(MSG_MCEDEVICE_REQ_SEND_REQUEST);
921     std::unique_ptr<MapMceInstanceRequest> reqPtr =
922         std::make_unique<MapMceRequestSetPath>(flags, folder, folderList);
923     ret = SendRequestToConnectedDevice(device, outMsg, reqPtr);
924     if (ret != RET_NO_ERROR) {
925         reqPtr = nullptr;
926     }
927     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
928     return ret;
929 }
930 
GetMasInstanceInfo(const RawAddress & device)931 IProfileMasInstanceInfoList MapMceService::GetMasInstanceInfo(const RawAddress &device)
932 {
933     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
934     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
935 
936     IProfileMasInstanceInfoList masInfoList;
937     masInfoList.isValid = false;
938     std::vector<IProfileMasInstanceInfo> instanceinfoList;
939     auto it = serviceBtDeviceInstMgrMap_.find(device.GetAddress());
940     if (it != serviceBtDeviceInstMgrMap_.end()) {
941         if (it->second != nullptr) {
942             instanceinfoList = it->second->GetMasInstanceInfo(device);
943             if (int(instanceinfoList.size()) == it->second->GetDeviceInstanseStmMapSize()) {
944                 masInfoList.isValid = true;
945                 masInfoList.masInfoList = instanceinfoList;
946             }
947         }
948     }
949     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
950     return masInfoList;
951 }
952 
Enable()953 void MapMceService::Enable()
954 {
955     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
956     utility::Dispatcher *dispatcher = GetDispatcher();
957     if (dispatcher != nullptr) {
958         dispatcher->PostTask(std::bind(&MapMceService::StartUpInternal, this));
959     } else {
960         LOG_ERROR("%{public}s dispatcher error", __PRETTY_FUNCTION__);
961     }
962 }
963 
StartUpInternal()964 int MapMceService::StartUpInternal()
965 {
966     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
967     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
968 
969     serviceState_ = MAP_MCE_STATE_STARTUP;
970     // send response to adapt manager
971     utility::Context *context = GetContext();
972     if (context != nullptr) {
973         context->OnEnable(PROFILE_NAME_MAP_MCE, true);
974     }
975     // get config parameter
976     GetConfigFromXml();
977     // mns startup
978     mnsServer_ = std::make_unique<MapMceMnsServer>(*this, insDefaultConfig_);
979     // start up mns server
980     int ret = mnsServer_->StartUp();
981     if (ret == RET_NO_ERROR) {
982         notificationRegistration_ = true;
983     } else {
984         notificationRegistration_ = false;
985         LOG_ERROR("%{public}s mns start error", __PRETTY_FUNCTION__);
986     }
987 #ifndef MCE_DISABLE_L2CAP
988     ObexMpClient::RegisterL2capLPsm(MAP_MCE_GOEP_L2CAP_PSM_VALUE);
989 #endif
990     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
991     return RET_NO_ERROR;
992 }
993 
GetMaxConnectNum()994 int MapMceService::GetMaxConnectNum()
995 {
996     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
997     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
998 
999     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1000     return serviceBtDeviceInstMgrMap_.size();
1001 }
1002 
Disable()1003 void MapMceService::Disable()
1004 {
1005     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
1006     utility::Dispatcher *dispatcher = GetDispatcher();
1007     if (dispatcher != nullptr) {
1008         dispatcher->PostTask(std::bind(&MapMceService::ShutDownInternal, this));
1009     } else {
1010         LOG_ERROR("%{public}s dispatcher error", __PRETTY_FUNCTION__);
1011     }
1012 }
1013 
ShutDownInternal()1014 int MapMceService::ShutDownInternal()
1015 {
1016     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1017     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1018 
1019     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1020     return ShutDownInternalCommon();
1021 }
1022 
ShutDownInternalCommon()1023 int MapMceService::ShutDownInternalCommon()
1024 {
1025     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1026     MapMceServiceStateType targetState = MAP_MCE_STATE_SHUTDOWN;
1027     serviceState_ = MAP_MCE_STATE_SHUTDOWNING;
1028     // check iterator in client device map
1029     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); ++it) {
1030         if (it->second->GetCurrentDeviceState() != MAP_MCE_DEV_STATE_DISCONNECTED) {
1031             // request disconnect
1032             utility::Message outMsg(MSG_MCEDEVICE_REQ_DEVICE_DISCONNECT);
1033             it->second->PostMessage(outMsg);
1034             targetState = MAP_MCE_STATE_SHUTDOWNING;
1035         }
1036     }
1037     // all client have shutdown
1038     if (targetState == MAP_MCE_STATE_SHUTDOWN) {
1039         serviceState_ = MAP_MCE_STATE_SHUTDOWN;
1040 #ifndef MCE_DISABLE_L2CAP
1041         ObexMpClient::DeregisterL2capLPsm(MAP_MCE_GOEP_L2CAP_PSM_VALUE);
1042 #endif
1043         // release mns server
1044         if (mnsServer_ != nullptr) {
1045             mnsServer_->ShutDown();
1046         }
1047         // send response to adapt manager right now
1048         utility::Context *context = GetContext();
1049         if (context != nullptr) {
1050             LOG_INFO("%{public}s context->OnDisable execute", __PRETTY_FUNCTION__);
1051             context->OnDisable(PROFILE_NAME_MAP_MCE, true);
1052         }
1053     } else {  // if some client is working
1054         // need wait all client shutdown response callback
1055         serviceState_ = MAP_MCE_STATE_SHUTDOWNING;
1056         // send response to adapt manager when callback all come
1057     }
1058     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1059     return RET_NO_ERROR;
1060 }
1061 
PostMessage(utility::Message msg)1062 void MapMceService::PostMessage(utility::Message msg)
1063 {
1064     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1065     utility::Dispatcher *dispatcher = GetDispatcher();
1066     if (dispatcher == nullptr) {
1067         LOG_INFO("%{public}s dispatcher error", __PRETTY_FUNCTION__);
1068         return;
1069     }
1070     dispatcher->PostTask(std::bind(&MapMceService::ProcessMessage, this, msg));
1071     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1072 }
1073 
GetServiceStates()1074 MapMceServiceStateType MapMceService::GetServiceStates()
1075 {
1076     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1077     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1078 
1079     LOG_INFO("%{public}s end,state=%{public}d", __PRETTY_FUNCTION__, int(serviceState_));
1080     return MapMceServiceStateType(int(serviceState_));
1081 }
1082 
MnsPostMessage(const utility::Message & msg,std::string btAddr)1083 void MapMceService::MnsPostMessage(const utility::Message &msg, std::string btAddr)
1084 {
1085     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1086     utility::Dispatcher *dispatcher = GetDispatcher();
1087     if (dispatcher == nullptr) {
1088         LOG_INFO("%{public}s dispatcher error", __PRETTY_FUNCTION__);
1089         return;
1090     }
1091     dispatcher->PostTask(std::bind(&MapMceService::MnsProcessMessage, this, msg, btAddr));
1092     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1093 }
1094 
MnsPostMessageWithHeader(const utility::Message & msg,std::string btAddr,const ObexHeader & req)1095 void MapMceService::MnsPostMessageWithHeader(const utility::Message &msg, std::string btAddr, const ObexHeader &req)
1096 {
1097     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1098     utility::Dispatcher *dispatcher = GetDispatcher();
1099     if (dispatcher == nullptr) {
1100         LOG_INFO("%{public}s dispatcher error", __PRETTY_FUNCTION__);
1101         return;
1102     }
1103     dispatcher->PostTask(std::bind(&MapMceService::MnsProcessMessageWithHeader, this, msg, btAddr, req));
1104     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1105 }
1106 
MnsProcessMessage(utility::Message msg,std::string btAddr)1107 void MapMceService::MnsProcessMessage(utility::Message msg, std::string btAddr)
1108 {
1109     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1110     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1111 
1112     auto it = serviceBtDeviceInstMgrMap_.find(btAddr);
1113     if (it != serviceBtDeviceInstMgrMap_.end()) {
1114         it->second->ProcessMessage(msg);
1115     } else {
1116         LOG_ERROR("%{public}s address error = %{public}s", __PRETTY_FUNCTION__, btAddr.c_str());
1117     }
1118     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1119 }
1120 
MnsProcessMessageWithHeader(utility::Message msg,std::string btAddr,ObexHeader req)1121 void MapMceService::MnsProcessMessageWithHeader(utility::Message msg, std::string btAddr, ObexHeader req)
1122 {
1123     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1124     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1125 
1126     auto it = serviceBtDeviceInstMgrMap_.find(btAddr);
1127     if (it != serviceBtDeviceInstMgrMap_.end()) {
1128         it->second->ProcessMnsObexObserverMessage(req, msg);
1129     } else {
1130         LOG_ERROR("%{public}s address error = %{public}s", __PRETTY_FUNCTION__, btAddr.c_str());
1131     }
1132     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1133 }
1134 
ProcessMessage(const utility::Message & msg)1135 void MapMceService::ProcessMessage(const utility::Message &msg)
1136 {
1137     LOG_INFO("%{public}s enter,input msg=0x%x", __PRETTY_FUNCTION__, msg.what_);
1138     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1139 
1140     switch (msg.what_) {
1141         case MSG_MCEDEVICE_SDP_GET_INSTANCE_FINISH:
1142         case MSG_MCEDEVICE_SDP_GET_INSTANCE_FAILED:
1143             SdpDispatchToDeviceInstMgr(msg);
1144             break;
1145         case MSG_MCESERVICE_GET_SURPORT_FEATURES_FINISH:
1146         case MSG_MCESERVICE_GET_SURPORT_FEATURES_FAILED:
1147             ProcessGetSupportedFeaturesRespones(msg);
1148             break;
1149         case MSG_MCESERVICE_DEVICE_CONNECTED:
1150             break;
1151         case MSG_MCESERVICE_DEVICE_DISCONNECTED:
1152             break;
1153         default:
1154             break;
1155     }
1156     if (serviceState_ == MAP_MCE_STATE_STARTUP) {
1157         // empty
1158     } else if (serviceState_ == MAP_MCE_STATE_SHUTDOWNING) {
1159         switch (msg.what_) {
1160             case MSG_MCESERVICE_DEVICE_DISCONNECTED:
1161                 // shutdown again;
1162                 ShutDownInternalCommon();
1163                 break;
1164             default:
1165                 break;
1166         }
1167     } else {
1168     }
1169     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1170 }
1171 
SdpDispatchToDeviceInstMgr(const utility::Message & msg)1172 void MapMceService::SdpDispatchToDeviceInstMgr(const utility::Message &msg)
1173 {
1174     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1175     auto argPrt = static_cast<MapSdpMsgArgPrt *>(msg.arg2_);
1176     if (argPrt == nullptr) {
1177         LOG_INFO("%{public}s error return", __PRETTY_FUNCTION__);
1178         return;
1179     }
1180     // check iterator in map
1181     for (auto it = serviceBtDeviceInstMgrMap_.begin(); it != serviceBtDeviceInstMgrMap_.end(); ++it) {
1182         BtAddr btAddress = it->second->GetBtAddress();
1183         if (memcmp(&(argPrt->address), &btAddress, sizeof(BtAddr)) == 0) {  // same bt address
1184             // send to the bt device
1185             it->second->ProcessMessage(msg);
1186             break;
1187         }
1188     }
1189     delete argPrt;
1190     argPrt = nullptr;
1191     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1192 }
1193 
1194 // call by post task
CheckSdpForGetSupportedFeatures(const RawAddress & device)1195 int MapMceService::CheckSdpForGetSupportedFeatures(const RawAddress &device)
1196 {
1197     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1198     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1199     int index = 0;
1200     int retServiceVal;
1201     int retStackVal;
1202     IProfileMapAction retAction;
1203     MapExecuteStatus resCode;
1204     SdpAttributeIdList attributeIdList = {SDP_TYPE_LIST, .attributeIdList = {0, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
1205     BtUuid btUuid = {0, {.uuid128 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
1206     SdpUuid sdpUuid = {0, nullptr};
1207 
1208     // get the bluetooth address
1209     BtAddr btAddr = {{0, 0, 0, 0, 0, 0}, 0};
1210     btAddr.type = BT_PUBLIC_DEVICE_ADDRESS;
1211     RawAddress rawAddr(device.GetAddress());
1212     rawAddr.ConvertToUint8(btAddr.addr, sizeof(btAddr.addr));
1213 
1214     // config search attribute
1215     attributeIdList.attributeIdList.attributeId[index++] = MAP_MCE_SUPPORTED_FEATURES_ATTRIBUTE_ID;
1216     attributeIdList.attributeIdList.attributeIdNumber = index;
1217     attributeIdList.type = SDP_TYPE_LIST;
1218     // set uuid
1219     btUuid.type = BT_UUID_16;
1220     btUuid.uuid16 = MAP_MCE_SERVICE_CLASS_UUID;
1221 
1222     sdpUuid.uuid = &btUuid;
1223     sdpUuid.uuidNum = 1;
1224     // search sdp
1225     retStackVal =
1226         SDP_ServiceSearchAttribute(&btAddr, &sdpUuid, attributeIdList, nullptr, MapMceGetSupportFeatureSdpSearchCb);
1227     if (retStackVal != BT_NO_ERROR) {
1228         LOG_ERROR("%{public}s error:SDP_ServiceSearchAttribute", __PRETTY_FUNCTION__);
1229         retServiceVal = RET_NO_SUPPORT;
1230         // action complete fail
1231         resCode = MapExecuteStatus::NOT_SUPPORT;
1232         retAction.action_ = MapActionType::GET_SUPPORTED_FEATURES;
1233         serviceRpcCallbackMgr_.ExcuteObserverOnMapActionCompleted(device.GetAddress(), retAction, resCode);
1234     } else {
1235         retServiceVal = RET_NO_ERROR;
1236     }
1237     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1238     return retServiceVal;
1239 }
1240 
1241 // internal
ProcessGetSupportedFeaturesRespones(utility::Message const & msg)1242 void MapMceService::ProcessGetSupportedFeaturesRespones(utility::Message const &msg)
1243 {
1244     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1245     uint32_t devSupportedFeatrue = 0;
1246     IProfileMapAction retAction;
1247     BtAddr *argPrt = (BtAddr *)msg.arg2_;
1248     if (argPrt == nullptr) {
1249         LOG_ERROR("%{public}s argPrt null", __PRETTY_FUNCTION__);
1250         return;
1251     }
1252     MapExecuteStatus resCode;
1253     if (msg.what_ == MSG_MCESERVICE_GET_SURPORT_FEATURES_FINISH) {
1254         devSupportedFeatrue = msg.arg1_;
1255         // call observer
1256         resCode = MapExecuteStatus::SUCCEED;
1257     } else {
1258         resCode = MapExecuteStatus::NOT_SUPPORT;
1259     }
1260     retAction.supportedFeatures_ = devSupportedFeatrue;
1261     retAction.action_ = MapActionType::GET_SUPPORTED_FEATURES;
1262     RawAddress btDevice = RawAddress::ConvertToString(argPrt->addr);
1263     serviceRpcCallbackMgr_.ExcuteObserverOnMapActionCompleted(btDevice.GetAddress(), retAction, resCode);
1264     // delete memory
1265     delete argPrt;
1266     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1267 }
1268 
SetDefaultConfig(const MasInstanceConfig & configSave)1269 void MapMceService::SetDefaultConfig(const MasInstanceConfig &configSave)
1270 {
1271     LOG_INFO("%{public}s enter", __PRETTY_FUNCTION__);
1272     std::lock_guard<std::recursive_mutex> lock(mceDeviceMapMutex_);
1273 
1274     int saveId = insDefaultConfig_.deviceId;
1275     insDefaultConfig_ = configSave;
1276     insDefaultConfig_.deviceId = saveId;
1277     LOG_INFO("%{public}s end", __PRETTY_FUNCTION__);
1278 }
1279 
GetConfigFromXml()1280 void MapMceService::GetConfigFromXml()
1281 {
1282     LOG_DEBUG("%{public}s enter", __PRETTY_FUNCTION__);
1283     int value = 0;
1284     bool boolValue = 0;
1285     IAdapterConfig *config = AdapterConfig::GetInstance();
1286 
1287     if (!config->GetValue(SECTION_MAP_MCE_SERVICE, "MaxConnectedDevices", value)) {
1288         LOG_ERROR("%{public}s error:MaxConnectedDevices not found", __PRETTY_FUNCTION__);
1289     } else {
1290         insDefaultConfig_.maxOfDevice = value;
1291     }
1292     if (!config->GetValue(SECTION_MAP_MCE_SERVICE, PROPERTY_L2CAP_MTU, value)) {
1293         LOG_ERROR("%{public}s error:l2capMtu not found", __PRETTY_FUNCTION__);
1294     } else {
1295         insDefaultConfig_.l2capMtu = value;
1296     }
1297     if (!config->GetValue(SECTION_MAP_MCE_SERVICE, PROPERTY_RFCOMM_MTU, value)) {
1298         LOG_ERROR("%{public}s error:rfcomm Mtu not found", __PRETTY_FUNCTION__);
1299     } else {
1300         insDefaultConfig_.rfcommMtu = value;
1301     }
1302     if (!config->GetValue(SECTION_MAP_MCE_SERVICE, PROPERTY_MAP_MAX_OF_GET_UREAD, value)) {
1303         LOG_ERROR("%{public}s error:MaxOfGetUreadMessage not found", __PRETTY_FUNCTION__);
1304     } else {
1305         insDefaultConfig_.maxOfGetUnread = value;
1306     }
1307     if (!config->GetValue(SECTION_MAP_MCE_SERVICE, PROPERTY_SRM_ENABLE, boolValue)) {
1308         LOG_ERROR("%{public}s error:SrmEnable not found", __PRETTY_FUNCTION__);
1309     } else {
1310         insDefaultConfig_.isSupportSrm = boolValue;
1311     }
1312     HILOGI("maxOfDevice = %{public}d, l2capMtu = %{public}d, rfcommMtu = %{public}d, UreadNumber = %{public}d, "
1313         "EnableSrm_ = %{public}d", insDefaultConfig_.maxOfDevice, insDefaultConfig_.l2capMtu,
1314         insDefaultConfig_.rfcommMtu, insDefaultConfig_.maxOfGetUnread, insDefaultConfig_.isSupportSrm);
1315 }
1316 
1317 REGISTER_CLASS_CREATOR(MapMceService);
1318 }  // namespace bluetooth
1319 }  // namespace OHOS
1320