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