• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <codecvt>
16 #include <string>
17 #include <mutex>
18 #include <memory>
19 
20 #include "bluetooth_map_mce_observer_stub.h"
21 #include "bluetooth_map_mce_proxy.h"
22 #include "bluetooth_raw_address.h"
23 #include "bluetooth_map_mce.h"
24 
25 #include "bt_def.h"
26 #include "bluetooth_host.h"
27 #include "bluetooth_utils.h"
28 #include "bluetooth_remote_device.h"
29 #include "bluetooth_observer_list.h"
30 #include "iservice_registry.h"
31 #include "system_ability_definition.h"
32 #include "i_bluetooth_host.h"
33 using namespace OHOS::bluetooth;
34 
35 namespace OHOS {
36 namespace Bluetooth {
37 /**
38  * @brief make observer for framework
39  */
40 class BluetoothMapMceObserverImpl : public BluetoothMapMceObserverStub {
41 public:
BluetoothMapMceObserverImpl(BluetoothObserverList<MapClientObserver> & observerList)42     BluetoothMapMceObserverImpl(BluetoothObserverList<MapClientObserver> &observerList)
43         : frameworkObserverList_(observerList) {};
44     ~BluetoothMapMceObserverImpl() override = default;
45     void OnConnectionStateChanged(
46         const BluetoothRawAddress &deviceAddress, int state) override;
47     void OnMapActionCompleted(const BluetoothRawAddress &deviceAddress,
48         const BluetoothIProfileMapAction &action, int state) override;
49     void OnMapEventReported(const BluetoothRawAddress &deviceAddress,
50         const BluetoothIProfileMapEventReport &report) override;
51     void OnBmessageCompleted(const BluetoothRawAddress &deviceAddress,
52         const BluetoothIProfileBMessage &bmsg, int state) override;
53     void OnMessagesListingCompleted(const BluetoothRawAddress &deviceAddress,
54         const BluetoothIProfileMessagesListing &listing, int state) override;
55     void OnConversationListingCompleted(
56         const BluetoothRawAddress &deviceAddress,
57         const BluetoothIProfileConversationListing &listing, int state) override;
58 
59 private:
60     MapVcard ConvertVcardToFramewrokFormat(IProfileMapVcard profileVcard);
61     BluetoothObserverList<MapClientObserver> &frameworkObserverList_;
62 };
63 
OnConnectionStateChanged(const BluetoothRawAddress & deviceAddress,int state)64 void BluetoothMapMceObserverImpl::OnConnectionStateChanged(
65     const BluetoothRawAddress &deviceAddress, int state)
66 {
67     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
68     frameworkObserverList_.ForEach([remoteDevice, state](std::shared_ptr<MapClientObserver> observer) {
69         observer->OnConnectionStateChanged(remoteDevice, state);
70     });
71 }
72 
OnMapActionCompleted(const BluetoothRawAddress & deviceAddress,const BluetoothIProfileMapAction & action,int state)73 void BluetoothMapMceObserverImpl::OnMapActionCompleted(
74     const BluetoothRawAddress &deviceAddress,
75     const BluetoothIProfileMapAction &action, int state)
76 {
77     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
78     MapAction faction;
79     MapExecuteStatus fstate = MapExecuteStatus(state);
80 
81     // convert data
82     faction.action_ = (OHOS::Bluetooth::MapActionType)(action.action_);
83     faction.supportedFeatures_ = action.supportedFeatures_;
84     faction.ownerStatus_.PresenceAvailability = action.ownerStatus_.PresenceAvailability;
85     faction.ownerStatus_.PresenceText = action.ownerStatus_.PresenceText;
86     faction.ownerStatus_.LastActivity = action.ownerStatus_.LastActivity;
87     faction.ownerStatus_.ChatState = action.ownerStatus_.ChatState;
88 
89     frameworkObserverList_.ForEach([remoteDevice, faction, fstate](std::shared_ptr<MapClientObserver> observer) {
90         observer->OnMapActionCompleted(remoteDevice, faction, fstate);
91     });
92 }
93 
ConvertVcardToFramewrokFormat(IProfileMapVcard profileVcard)94 MapVcard BluetoothMapMceObserverImpl::ConvertVcardToFramewrokFormat(IProfileMapVcard profileVcard)
95 {
96     MapVcard frameworkVcard;
97     frameworkVcard.VERSION = profileVcard.VERSION;    // shall be included
98     frameworkVcard.N = profileVcard.N;                // shall be included
99     frameworkVcard.TEL = profileVcard.TEL;            // may be used
100     frameworkVcard.EMAIL = profileVcard.EMAIL;        // may be used
101     frameworkVcard.X_BT_UID = profileVcard.X_BT_UID;  // bmsg V1.1
102     frameworkVcard.X_BT_UCI = profileVcard.X_BT_UCI;  // bmsg V1.1
103     frameworkVcard.FN = profileVcard.FN;              // vcard 3.0 , shall be included
104     return frameworkVcard;
105 }
106 
OnBmessageCompleted(const BluetoothRawAddress & deviceAddress,const BluetoothIProfileBMessage & bmsg,int state)107 void BluetoothMapMceObserverImpl::OnBmessageCompleted(
108     const BluetoothRawAddress &deviceAddress,
109     const BluetoothIProfileBMessage &bmsg, int state)
110 {
111     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
112     MapBMessage fBmsg;
113     MapExecuteStatus fstate = MapExecuteStatus(state);
114 
115     // Bmessage
116     fBmsg.bMessageStringObject_ = bmsg.bMessageStringObject_;
117     IProfileBMessageStruct msg = bmsg.bMessageParam_;
118     fBmsg.FractionDeliver = (OHOS::Bluetooth::MapFractionDeliverType)bmsg.FractionDeliver;
119     fBmsg.bMessageParam_.version_property = msg.version_property;
120     fBmsg.bMessageParam_.readstatus_property = (OHOS::Bluetooth::MapMessageStatus)msg.readstatus_property;
121     fBmsg.bMessageParam_.type_property = (OHOS::Bluetooth::MapMessageType)msg.type_property;
122     fBmsg.bMessageParam_.folder_property = msg.folder_property;
123     fBmsg.bMessageParam_.extendeddata_property = msg.extendeddata_property;
124 
125     IProfileMapVcard profileVcard;
126     MapVcard frameworkVcard;
127     if (msg.originator_.size() == 1) {
128         auto itVcard = msg.originator_.begin();
129         profileVcard = *itVcard;
130         frameworkVcard = ConvertVcardToFramewrokFormat(profileVcard);
131         fBmsg.bMessageParam_.originator_.push_back(frameworkVcard);
132     }
133     for (auto it = msg.envelope_.recipientLevel1_.begin(); it != msg.envelope_.recipientLevel1_.end(); it++) {
134         profileVcard = *it;
135         frameworkVcard = ConvertVcardToFramewrokFormat(profileVcard);
136         fBmsg.bMessageParam_.envelope_.recipientLevel1_.push_back(frameworkVcard);
137     }
138     for (auto it2 = msg.envelope_.recipientLevel2_.begin(); it2 != msg.envelope_.recipientLevel2_.end(); it2++) {
139         profileVcard = *it2;
140         frameworkVcard = ConvertVcardToFramewrokFormat(profileVcard);
141         fBmsg.bMessageParam_.envelope_.recipientLevel2_.push_back(frameworkVcard);
142     }
143     for (auto it3 = msg.envelope_.recipientLevel3_.begin(); it3 != msg.envelope_.recipientLevel3_.end(); it3++) {
144         profileVcard = *it3;
145         frameworkVcard = ConvertVcardToFramewrokFormat(profileVcard);
146         fBmsg.bMessageParam_.envelope_.recipientLevel3_.push_back(frameworkVcard);
147     }
148 
149     fBmsg.bMessageParam_.envelope_.maxLevelOfEnvelope_ = msg.envelope_.maxLevelOfEnvelope_;
150     fBmsg.bMessageParam_.envelope_.msgBody_.bodyPartID = msg.envelope_.msgBody_.bodyPartID;
151     fBmsg.bMessageParam_.envelope_.msgBody_.body_encoding = msg.envelope_.msgBody_.body_encoding;
152     fBmsg.bMessageParam_.envelope_.msgBody_.body_charset = msg.envelope_.msgBody_.body_charset;
153     fBmsg.bMessageParam_.envelope_.msgBody_.body_language = msg.envelope_.msgBody_.body_language;
154     fBmsg.bMessageParam_.envelope_.msgBody_.body_content = msg.envelope_.msgBody_.body_content;
155     fBmsg.bMessageParam_.envelope_.msgBody_.body_content_length = msg.envelope_.msgBody_.body_content_length;
156 
157     frameworkObserverList_.ForEach([remoteDevice, fBmsg, fstate](std::shared_ptr<MapClientObserver> observer) {
158         observer->OnBmessageCompleted(remoteDevice, fBmsg, fstate);
159     });
160 }
161 
OnMessagesListingCompleted(const BluetoothRawAddress & deviceAddress,const BluetoothIProfileMessagesListing & listing,int state)162 void BluetoothMapMceObserverImpl::OnMessagesListingCompleted(
163     const BluetoothRawAddress &deviceAddress,
164     const BluetoothIProfileMessagesListing &listing, int state)
165 {
166     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
167     MessagesListing fListing;
168     MapExecuteStatus fstate = MapExecuteStatus(state);
169 
170     // message listing
171     fListing.messagesListingStringObject_ = listing.messagesListingStringObject_;
172     IProfileMessagesListingParamStruct serviceMsgListParam = listing.messagesListingParam_;
173     fListing.messagesListingParam_.NewMessage = (OHOS::Bluetooth::MapOnOffType)serviceMsgListParam.NewMessage; // on off
174     fListing.messagesListingParam_.MseTime =
175         serviceMsgListParam.MseTime;  // String with current time basis and UTC-offset of the MSE.
176     fListing.messagesListingParam_.ListingSize = serviceMsgListParam.ListingSize;
177     fListing.messagesListingParam_.DatabaseIdentifier =
178         serviceMsgListParam.DatabaseIdentifier;  // 128-bit value in hex string format,max 32 bytes
179     fListing.messagesListingParam_.FolderVersionCounter =
180         serviceMsgListParam.FolderVersionCounter;  // 128-bit value in hex string format,max 32 bytes
181     fListing.messagesListingParam_.Version = serviceMsgListParam.Version;  // "1.1" or "1.0"
182     IProfileMessageOutline outlineMsg;
183     MessageOutline frameworkMsg;
184     std::vector<IProfileMessageOutline> serviceMsgOutineList = listing.messageOutlineList_;
185     for (auto it = serviceMsgOutineList.begin(); it != serviceMsgOutineList.end(); it++) {
186         outlineMsg = *it;
187         frameworkMsg.handle = outlineMsg.handle;
188         frameworkMsg.subject = outlineMsg.subject;
189         frameworkMsg.datetime = outlineMsg.datetime;
190         frameworkMsg.sender_name = outlineMsg.sender_name;
191         frameworkMsg.sender_addressing = outlineMsg.sender_addressing;
192         frameworkMsg.replyto_addressing = outlineMsg.replyto_addressing;
193         frameworkMsg.recipient_name = outlineMsg.recipient_name;
194         frameworkMsg.recipient_addressing = outlineMsg.recipient_addressing;
195         frameworkMsg.type = (OHOS::Bluetooth::MapMessageType)outlineMsg.type;
196         frameworkMsg.receptionStatus = (OHOS::Bluetooth::MapMsgReceptionStatus)outlineMsg.receptionStatus;
197         frameworkMsg.size = outlineMsg.size;
198         frameworkMsg.attachment_size = outlineMsg.attachment_size;
199         frameworkMsg.text = (OHOS::Bluetooth::MapBoolType)outlineMsg.text;
200         frameworkMsg.read = (OHOS::Bluetooth::MapMessageStatus)outlineMsg.read;
201         frameworkMsg.sent = (OHOS::Bluetooth::MapBoolType)outlineMsg.sent;
202         frameworkMsg.protected_ = (OHOS::Bluetooth::MapBoolType)outlineMsg.protected_;
203         frameworkMsg.priority = (OHOS::Bluetooth::MapBoolType)outlineMsg.priority;
204         frameworkMsg.delivery_status = (OHOS::Bluetooth::MapMsgDeliveryStatus)outlineMsg.delivery_status;
205         frameworkMsg.conversation_id = outlineMsg.conversation_id;
206         frameworkMsg.conversation_name = outlineMsg.conversation_name;
207         frameworkMsg.direction = (OHOS::Bluetooth::MapMsgDirection)outlineMsg.direction;
208         frameworkMsg.attachment_mime_types = outlineMsg.attachment_mime_types;
209         fListing.messageOutlineList_.push_back(frameworkMsg);
210     }
211 
212     frameworkObserverList_.ForEach([remoteDevice, fListing, fstate](std::shared_ptr<MapClientObserver> observer) {
213         observer->OnMessagesListingCompleted(remoteDevice, fListing, fstate);
214     });
215 }
216 
OnConversationListingCompleted(const BluetoothRawAddress & deviceAddress,const BluetoothIProfileConversationListing & listing,int state)217 void BluetoothMapMceObserverImpl::OnConversationListingCompleted(
218     const BluetoothRawAddress &deviceAddress,
219     const BluetoothIProfileConversationListing &listing, int state)
220 {
221     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
222     ConversationListing fListing;
223     MapExecuteStatus fstate = MapExecuteStatus(state);
224 
225     // conversationList listing
226     // save the object
227     fListing.conversationListingStringObject_ = listing.conversationListingStringObject_;
228     // save the param
229     IProfileConversationListingParamStruct serviceParamStruct = listing.conversationListingParam_;
230     fListing.conversationListingParam_.ConversationListingVersionCounter =
231         serviceParamStruct.ConversationListingVersionCounter;  // 128-bit value in hex string format(max 32 bytes)
232     fListing.conversationListingParam_.ListingSize = serviceParamStruct.ListingSize;
233     fListing.conversationListingParam_.DatabaseIdentifier =
234         serviceParamStruct.DatabaseIdentifier;  // 128-bit value in hex string format(max 32 bytes)
235     fListing.conversationListingParam_.MseTime =
236         serviceParamStruct.MseTime;  // String with current time basis and UTC-offset of the MSE. See Section 5.5.4
237     fListing.conversationListingParam_.Version = serviceParamStruct.Version;
238     // save the conversation list
239     fListing.conversationOutlineList_.clear();
240     Participant fParticipant;
241     Conversation fConvoOutline;
242     IProfileParticipant serviceParticipant;
243     IProfileConversation serviceOutline;
244     std::vector<IProfileConversation> serviceConvoList = listing.conversationOutlineList_;
245     for (auto it = serviceConvoList.begin(); it != serviceConvoList.end(); it++) {
246         serviceOutline = *it;
247         // set value
248         fConvoOutline.id = serviceOutline.id;
249         fConvoOutline.name = serviceOutline.name;
250         fConvoOutline.last_activity = serviceOutline.last_activity;
251         fConvoOutline.read_status = serviceOutline.read_status;
252         fConvoOutline.version_counter = serviceOutline.version_counter;
253         fConvoOutline.summary = serviceOutline.summary;
254         // init list
255         fConvoOutline.participantList_.clear();
256         for (auto it2 = serviceOutline.participantList_.begin(); it2 != serviceOutline.participantList_.end(); it2++) {
257             serviceParticipant = *it2;
258             fParticipant.uci = serviceParticipant.uci;
259             fParticipant.display_name = serviceParticipant.display_name;
260             fParticipant.chat_state = serviceParticipant.chat_state;
261             fParticipant.last_activity = serviceParticipant.last_activity;
262             fParticipant.x_bt_uid = serviceParticipant.x_bt_uid;
263             fParticipant.name = serviceParticipant.name;
264             fParticipant.presence_availability = serviceParticipant.presence_availability;
265             fParticipant.presence_text = serviceParticipant.presence_text;
266             fParticipant.priority = serviceParticipant.priority;
267             fConvoOutline.participantList_.push_back(fParticipant);
268         }
269         fListing.conversationOutlineList_.push_back(fConvoOutline);
270     }
271 
272     frameworkObserverList_.ForEach([remoteDevice, fListing, fstate](std::shared_ptr<MapClientObserver> observer) {
273         observer->OnConversationListingCompleted(remoteDevice, fListing, fstate);
274     });
275 }
276 
OnMapEventReported(const BluetoothRawAddress & deviceAddress,const BluetoothIProfileMapEventReport & report)277 void BluetoothMapMceObserverImpl::OnMapEventReported(
278     const BluetoothRawAddress &deviceAddress,
279     const BluetoothIProfileMapEventReport &report)
280 {
281     BluetoothRemoteDevice remoteDevice(deviceAddress.GetAddress(), 0);
282     MapEventReport fEventReport;
283 
284     fEventReport.type = report.type;
285     fEventReport.handle = report.handle;
286     fEventReport.folder = report.folder;
287     fEventReport.old_folder = report.old_folder;
288     fEventReport.msg_type = (OHOS::Bluetooth::MapMessageType)report.msg_type;
289     fEventReport.datetime = report.datetime;
290     fEventReport.subject = report.subject;
291     fEventReport.sender_name = report.sender_name;
292     fEventReport.priority = (OHOS::Bluetooth::MapBoolType)report.priority;
293     fEventReport.conversation_name = report.conversation_name;
294     fEventReport.conversation_id = report.conversation_id;
295     fEventReport.presence_availability = report.presence_availability;
296     fEventReport.presence_text = report.presence_text;
297     fEventReport.last_activity = report.last_activity;
298     fEventReport.chat_state = report.chat_state;
299     fEventReport.read_status = (OHOS::Bluetooth::MapMessageStatus)report.read_status;
300     fEventReport.extended_data = report.extended_data;
301     fEventReport.participant_uci = report.participant_uci;
302     fEventReport.contact_uid = report.contact_uid;
303     fEventReport.version = report.version;
304     fEventReport.masInstanceId_ = report.masInstanceId_;
305     fEventReport.eventReportStringObject_ = report.eventReportStringObject_;
306 
307     frameworkObserverList_.ForEach([remoteDevice, fEventReport](std::shared_ptr<MapClientObserver> observer) {
308         observer->OnMapEventReported(remoteDevice, fEventReport);
309     });
310 }
311 
312 struct MapClient::impl {
313     impl();
~implOHOS::Bluetooth::MapClient::impl314     ~impl()
315     {
316         if (proxy_ != nullptr) {
317             proxy_->DeregisterObserver(observerImp_);
318             proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
319         }
320     }
321     sptr<IBluetoothMapMce> proxy_;
322     class BluetoothMapMceDeathRecipient;
323     sptr<BluetoothMapMceDeathRecipient> deathRecipient_ = nullptr;
324     std::mutex mutex_;
325     BluetoothObserverList<MapClientObserver> mapClientObserverList_;
326     sptr<BluetoothMapMceObserverImpl> observerImp_ = new BluetoothMapMceObserverImpl(mapClientObserverList_);
327     IProfileMapVcard ConvertVcardToProfileFormat(MapVcard frameworkVcard);
328     void ConvertEnvelopeToProfileFormat(
329         IProfileSendMessageParameters &iProfileMsg, const MapSendMessageParameters &msg);
330 };
331 
332 class MapClient::impl::BluetoothMapMceDeathRecipient final : public IRemoteObject::DeathRecipient {
333 public:
BluetoothMapMceDeathRecipient(MapClient::impl & MapMce)334     BluetoothMapMceDeathRecipient(MapClient::impl &MapMce) : MapMce_(MapMce) {};
335     ~BluetoothMapMceDeathRecipient() final = default;
336     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothMapMceDeathRecipient);
337 
OnRemoteDied(const wptr<IRemoteObject> & remote)338     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
339     {
340         HILOGI("starts");
341         MapMce_.proxy_->AsObject()->RemoveDeathRecipient(MapMce_.deathRecipient_);
342         MapMce_.proxy_ = nullptr;
343     }
344 
345 private:
346     MapClient::impl &MapMce_;
347 };
348 
impl()349 MapClient::impl::impl()
350 {
351     HILOGI("starts");
352     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
353     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
354 
355     if (!hostRemote) {
356         HILOGE("failed: no hostRemote");
357         return;
358     }
359     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
360     sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_MAP_MCE);
361 
362     if (!remote) {
363         HILOGE("failed: no remote");
364         return;
365     }
366     HILOGI("remote obtained");
367 
368     proxy_ = iface_cast<IBluetoothMapMce>(remote);
369     if (proxy_ == nullptr) {
370         return;
371     }
372     deathRecipient_ = new BluetoothMapMceDeathRecipient(*this);
373     proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
374     proxy_->RegisterObserver(observerImp_);
375 }
376 
GetProfile()377 MapClient *MapClient::GetProfile()
378 {
379     HILOGI("enter");
380     static MapClient mapClient;
381     return &mapClient;
382 }
383 
MapClient()384 MapClient::MapClient() : pimpl(nullptr)
385 {
386     HILOGI("excute");
387     pimpl = std::make_unique<impl>();
388 }
389 
~MapClient()390 MapClient::~MapClient()
391 {
392 }
393 
RegisterObserver(MapClientObserver & observer)394 void MapClient::RegisterObserver(MapClientObserver &observer)
395 {
396     HILOGI("enter");
397     std::shared_ptr<MapClientObserver> pointer(&observer, [](MapClientObserver *) {});
398     pimpl->mapClientObserverList_.Register(pointer);
399 }
400 
DeregisterObserver(MapClientObserver & observer)401 void MapClient::DeregisterObserver(MapClientObserver &observer)
402 {
403     HILOGI("enter");
404     std::shared_ptr<MapClientObserver> pointer(&observer, [](MapClientObserver *) {});
405     pimpl->mapClientObserverList_.Deregister(pointer);
406 }
407 
Connect(const BluetoothRemoteDevice & device)408 bool MapClient::Connect(const BluetoothRemoteDevice &device)
409 {
410     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
411     int retInt = RET_BAD_PARAM;
412     bool ret = false;
413     if (!device.IsValidBluetoothRemoteDevice()) {
414         HILOGE("BluetoothRemoteDevice error");
415         return ret;
416     }
417     BluetoothRawAddress rawAddress(device.GetDeviceAddr());
418     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
419         retInt = pimpl->proxy_->Connect(rawAddress);
420         if (retInt == RET_NO_ERROR) {
421             ret = true;
422         }
423     }
424     return ret;
425 }
426 
Disconnect(const BluetoothRemoteDevice & device)427 bool MapClient::Disconnect(const BluetoothRemoteDevice &device)
428 {
429     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
430     int retInt = RET_BAD_PARAM;
431     bool ret = false;
432     if (!device.IsValidBluetoothRemoteDevice()) {
433         HILOGE("BluetoothRemoteDevice error");
434         return ret;
435     }
436     BluetoothRawAddress rawAddress(device.GetDeviceAddr());
437     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
438         retInt = pimpl->proxy_->Disconnect(rawAddress);
439         if (retInt == RET_NO_ERROR) {
440             ret = true;
441         }
442     }
443     return ret;
444 }
445 
IsConnected(const BluetoothRemoteDevice & device)446 bool MapClient::IsConnected(const BluetoothRemoteDevice &device)
447 {
448     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
449     int retInt = 0;
450     if (!device.IsValidBluetoothRemoteDevice()) {
451         HILOGE("BluetoothRemoteDevice error");
452         return false;
453     }
454     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
455     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
456         retInt = pimpl->proxy_->IsConnected(rawDevice);
457     }
458     return (bool)retInt;
459 }
460 
GetConnectedDevices() const461 std::vector<BluetoothRemoteDevice> MapClient::GetConnectedDevices() const
462 {
463     HILOGI("enter");
464     std::vector<BluetoothRemoteDevice> btDeviceList;
465     std::vector<BluetoothRawAddress> btDevice;
466     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
467         pimpl->proxy_->GetConnectDevices(btDevice);
468         for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
469             btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
470         }
471     }
472     return btDeviceList;
473 }
474 
GetDevicesByStates(const std::vector<int> & statesList) const475 std::vector<BluetoothRemoteDevice> MapClient::GetDevicesByStates(const std::vector<int> &statesList) const
476 {
477     HILOGI("enter");
478     std::vector<BluetoothRemoteDevice> btDeviceList;
479     std::vector<BluetoothRawAddress> btDevice;
480     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
481         pimpl->proxy_->GetDevicesByStates(statesList, btDevice);
482         for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
483             btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
484         }
485     }
486     return btDeviceList;
487 }
488 
GetConnectionState(const BluetoothRemoteDevice & device) const489 int MapClient::GetConnectionState(const BluetoothRemoteDevice &device) const
490 {
491     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
492     int retState = (int)BTConnectState::DISCONNECTED;
493     if (!device.IsValidBluetoothRemoteDevice()) {
494         HILOGE("BluetoothRemoteDevice error");
495         return retState;
496     }
497     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
498     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
499         retState = pimpl->proxy_->GetConnectionState(rawDevice);
500     }
501     return retState;
502 }
503 
SetConnectionStrategy(const BluetoothRemoteDevice & device,const int strategy)504 bool MapClient::SetConnectionStrategy(const BluetoothRemoteDevice &device, const int strategy)
505 {
506     HILOGI("enter, device: %{public}s, device: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
507     int retInt = RET_BAD_PARAM;
508     bool ret = false;
509     if (!device.IsValidBluetoothRemoteDevice()) {
510         HILOGE("BluetoothRemoteDevice error");
511         return ret;
512     }
513     ret = RET_BAD_STATUS;
514     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
515     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
516         retInt = pimpl->proxy_->SetConnectionStrategy(rawDevice, strategy);
517         if (retInt == RET_NO_ERROR) {
518             ret = true;
519         }
520     }
521     return ret;
522 }
523 
GetConnectionStrategy(const BluetoothRemoteDevice & device) const524 int MapClient::GetConnectionStrategy(const BluetoothRemoteDevice &device) const
525 {
526     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
527     int ret = (int)BTStrategyType::CONNECTION_FORBIDDEN;
528     if (!device.IsValidBluetoothRemoteDevice()) {
529         HILOGE("BluetoothRemoteDevice error");
530         return ret;
531     }
532     ret = (int)BTStrategyType::CONNECTION_FORBIDDEN;
533     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
534     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
535         ret = pimpl->proxy_->GetConnectionStrategy(rawDevice);
536     }
537     return ret;
538 }
539 
GetUnreadMessages(const BluetoothRemoteDevice & device,MapMessageType type,uint8_t max)540 int MapClient::GetUnreadMessages(const BluetoothRemoteDevice &device, MapMessageType type, uint8_t max)
541 {
542     HILOGI("enter, device: %{public}s, max: %{public}d", GET_ENCRYPT_ADDR(device), max);
543     int ret = RET_BAD_PARAM;
544     if (!device.IsValidBluetoothRemoteDevice()) {
545         HILOGE("BluetoothRemoteDevice error");
546         return ret;
547     }
548     ret = RET_BAD_STATUS;
549     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
550         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
551         ret = pimpl->proxy_->GetUnreadMessages(rawDevice, (int)type, max);
552     }
553     return ret;
554 }
555 
GetSupportedFeatures(const BluetoothRemoteDevice & device) const556 int MapClient::GetSupportedFeatures(const BluetoothRemoteDevice &device) const
557 {
558     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
559     int ret = RET_BAD_STATUS;
560     if (!device.IsValidBluetoothRemoteDevice()) {
561         HILOGE("BluetoothRemoteDevice error");
562         return ret;
563     }
564     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
565     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
566         ret = pimpl->proxy_->GetSupportedFeatures(rawDevice);
567     }
568     return ret;
569 }
570 
571 
ConvertVcardToProfileFormat(MapVcard frameVcard)572 IProfileMapVcard MapClient::impl::ConvertVcardToProfileFormat(MapVcard frameVcard)
573 {
574     HILOGI("enter");
575     IProfileMapVcard serviceVcard;
576     serviceVcard.VERSION = frameVcard.VERSION;    // shall be included
577     serviceVcard.N = frameVcard.N;                // shall be included
578     serviceVcard.TEL = frameVcard.TEL;            // may be used
579     serviceVcard.EMAIL = frameVcard.EMAIL;        // may be used
580     serviceVcard.X_BT_UID = frameVcard.X_BT_UID;  // bmsg V1.1
581     serviceVcard.X_BT_UCI = frameVcard.X_BT_UCI;  // bmsg V1.1
582     serviceVcard.FN = frameVcard.FN;              // vcard 3.0 , shall be included
583     return serviceVcard;
584 }
585 
ConvertEnvelopeToProfileFormat(IProfileSendMessageParameters & iProfileMsg,const MapSendMessageParameters & msg)586 void MapClient::impl::ConvertEnvelopeToProfileFormat(
587     IProfileSendMessageParameters &iProfileMsg, const MapSendMessageParameters &msg)
588 {
589     HILOGI("enter");
590     MapVcard frameVcard;
591     IProfileMapVcard serviceVcard;
592     if (msg.message.originator_.size() == 1) {
593         auto itVcard = msg.message.originator_.begin();
594         frameVcard = *itVcard;
595         serviceVcard = ConvertVcardToProfileFormat(frameVcard);
596         iProfileMsg.bmessage_.originator_.push_back(serviceVcard);
597     }
598     for (auto it = msg.message.envelope_.recipientLevel1_.begin(); it != msg.message.envelope_.recipientLevel1_.end();
599         it++) {
600         frameVcard = *it;
601         serviceVcard = ConvertVcardToProfileFormat(frameVcard);
602         iProfileMsg.bmessage_.envelope_.recipientLevel1_.push_back(serviceVcard);
603     }
604     for (auto it2 = msg.message.envelope_.recipientLevel2_.begin();
605         it2 != msg.message.envelope_.recipientLevel2_.end();
606         it2++) {
607         frameVcard = *it2;
608         serviceVcard = ConvertVcardToProfileFormat(frameVcard);
609         iProfileMsg.bmessage_.envelope_.recipientLevel2_.push_back(serviceVcard);
610     }
611     for (auto it3 = msg.message.envelope_.recipientLevel3_.begin();
612         it3 != msg.message.envelope_.recipientLevel3_.end();
613         it3++) {
614         frameVcard = *it3;
615         serviceVcard = ConvertVcardToProfileFormat(frameVcard);
616         iProfileMsg.bmessage_.envelope_.recipientLevel3_.push_back(serviceVcard);
617     }
618 
619     iProfileMsg.bmessage_.envelope_.maxLevelOfEnvelope_ = msg.message.envelope_.maxLevelOfEnvelope_;
620     iProfileMsg.bmessage_.envelope_.msgBody_.bodyPartID = msg.message.envelope_.msgBody_.bodyPartID;
621     iProfileMsg.bmessage_.envelope_.msgBody_.body_encoding = msg.message.envelope_.msgBody_.body_encoding;
622     iProfileMsg.bmessage_.envelope_.msgBody_.body_charset = msg.message.envelope_.msgBody_.body_charset;
623     iProfileMsg.bmessage_.envelope_.msgBody_.body_language = msg.message.envelope_.msgBody_.body_language;
624     iProfileMsg.bmessage_.envelope_.msgBody_.body_content = msg.message.envelope_.msgBody_.body_content;
625     iProfileMsg.bmessage_.envelope_.msgBody_.body_content_length = msg.message.envelope_.msgBody_.body_content_length;
626 }
627 
SendMessage(const BluetoothRemoteDevice & device,const MapSendMessageParameters & msg)628 int MapClient::SendMessage(const BluetoothRemoteDevice &device, const MapSendMessageParameters &msg)
629 {
630     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
631     int ret = RET_BAD_STATUS;
632     if (!device.IsValidBluetoothRemoteDevice()) {
633         HILOGE("BluetoothRemoteDevice error");
634         return RET_BAD_PARAM;
635     }
636     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
637         BluetoothIProfileSendMessageParameters iProfileMsg;
638         iProfileMsg.Charset = (bluetooth::MapCharsetType)msg.Charset;
639         iProfileMsg.ConversationID = msg.ConversationID;
640         iProfileMsg.MessageHandle = msg.MessageHandle;
641         iProfileMsg.Attachment = (bluetooth::MapAttachmentType)msg.Attachment;
642         iProfileMsg.ModifyText = (bluetooth::MapModifyTextType)msg.ModifyText;
643         iProfileMsg.Retry = (bluetooth::MapOnOffType)msg.Retry;
644         iProfileMsg.Transparent = (bluetooth::MapOnOffType)msg.Transparent;
645         iProfileMsg.bmessage_.version_property = msg.message.version_property;
646         iProfileMsg.bmessage_.readstatus_property = (bluetooth::MapMessageStatus)msg.message.readstatus_property;
647         iProfileMsg.bmessage_.type_property = (bluetooth::MapMessageType)msg.message.type_property;
648         iProfileMsg.bmessage_.folder_property = msg.message.folder_property;
649         iProfileMsg.bmessage_.extendeddata_property = msg.message.extendeddata_property;
650 
651         pimpl->ConvertEnvelopeToProfileFormat(iProfileMsg, msg);
652 
653         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
654         ret = pimpl->proxy_->SendMessage(rawDevice, iProfileMsg);
655     }
656     return ret;
657 }
658 
SetNotificationFilter(const BluetoothRemoteDevice & device,const int mask)659 int MapClient::SetNotificationFilter(const BluetoothRemoteDevice &device, const int mask)
660 {
661     HILOGI("enter, device: %{public}s, mask: %{public}d", GET_ENCRYPT_ADDR(device), mask);
662     int ret = RET_BAD_PARAM;
663     if (!device.IsValidBluetoothRemoteDevice()) {
664         HILOGE("BluetoothRemoteDevice error");
665         return ret;
666     }
667     ret = RET_BAD_STATUS;
668     BluetoothRawAddress rawDevice(device.GetDeviceAddr());
669     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
670         ret = pimpl->proxy_->SetNotificationFilter(rawDevice, mask);
671     }
672     return ret;
673 }
674 
GetMessagesListing(const BluetoothRemoteDevice & device,const GetMessagesListingParameters & para) const675 int MapClient::GetMessagesListing(const BluetoothRemoteDevice &device, const GetMessagesListingParameters &para) const
676 {
677     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
678     int ret = RET_BAD_PARAM;
679     if (!device.IsValidBluetoothRemoteDevice()) {
680         HILOGE("BluetoothRemoteDevice error");
681         return ret;
682     }
683     ret = RET_BAD_STATUS;
684     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
685         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
686         BluetoothIProfileGetMessagesListingParameters servicePara;
687         servicePara.folder = para.folder;
688         servicePara.MaxListCount = para.MaxListCount;
689         servicePara.ListStartOffset = para.ListStartOffset;
690         servicePara.SubjectLength = para.SubjectLength;
691         servicePara.ParameterMask = para.ParameterMask;
692         servicePara.FilterMessageType = para.FilterMessageType;
693         servicePara.FilterPeriodBegin = para.FilterPeriodBegin;
694         servicePara.FilterPeriodEnd = para.FilterPeriodEnd;
695         servicePara.FilterReadStatus = para.FilterReadStatus;
696         servicePara.FilterRecipient = para.FilterRecipient;
697         servicePara.FilterOriginator = para.FilterOriginator;
698         servicePara.FilterPriority = para.FilterPriority;
699         servicePara.ConversationID = para.ConversationID;
700         servicePara.FilterMessageHandle = para.FilterMessageHandle;
701         ret = pimpl->proxy_->GetMessagesListing(rawDevice, servicePara);
702     }
703     return ret;
704 }
705 
GetMessage(const BluetoothRemoteDevice & device,MapMessageType type,const std::u16string & msgHandle,const GetMessageParameters & para) const706 int MapClient::GetMessage(const BluetoothRemoteDevice &device, MapMessageType type, const std::u16string &msgHandle,
707     const GetMessageParameters &para) const
708 {
709     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
710     std::string u8Str;
711     int ret = RET_BAD_PARAM;
712     if (!device.IsValidBluetoothRemoteDevice()) {
713         HILOGE("BluetoothRemoteDevice error");
714         return ret;
715     }
716     ret = RET_BAD_STATUS;
717     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
718         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
719         BluetoothIProfileGetMessageParameters servicePara;
720         servicePara.Attachment = (bluetooth::MapAttachmentType)para.Attachment;
721         servicePara.Charset = (bluetooth::MapCharsetType)para.Charset;
722         servicePara.FractionRequest = (bluetooth::MapFractionRequestType)para.FractionRequest;
723 
724         ret = pimpl->proxy_->GetMessage(
725             rawDevice, (int)type, msgHandle, servicePara);
726     }
727     return ret;
728 }
729 
UpdateInbox(const BluetoothRemoteDevice & device,MapMessageType type)730 int MapClient::UpdateInbox(const BluetoothRemoteDevice &device, MapMessageType type)
731 {
732     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
733     int ret = RET_BAD_PARAM;
734     if (!device.IsValidBluetoothRemoteDevice()) {
735         HILOGE("BluetoothRemoteDevice error");
736         return ret;
737     }
738     ret = RET_BAD_STATUS;
739     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
740         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
741         ret = pimpl->proxy_->UpdateInbox(rawDevice, (int)type);
742     }
743     return ret;
744 }
745 
GetConversationListing(const BluetoothRemoteDevice & device,const GetConversationListingParameters & para) const746 int MapClient::GetConversationListing(
747     const BluetoothRemoteDevice &device, const GetConversationListingParameters &para) const
748 {
749     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
750     int ret = RET_BAD_PARAM;
751     if (!device.IsValidBluetoothRemoteDevice()) {
752         HILOGE("BluetoothRemoteDevice error");
753         return ret;
754     }
755     ret = RET_BAD_STATUS;
756     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
757         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
758         BluetoothIProfileGetConversationListingParameters servicePara;
759         servicePara.MaxListCount = para.MaxListCount;
760         servicePara.ListStartOffset = para.ListStartOffset;
761         servicePara.FilterReadStatus = para.FilterReadStatus;
762         servicePara.FilterRecipient = para.FilterRecipient;
763         servicePara.ConversationID = para.ConversationID;
764         servicePara.FilterLastActivityBegin = para.FilterLastActivityBegin;
765         servicePara.FilterLastActivityEnd = para.FilterLastActivityEnd;
766         servicePara.ConvParameterMask = para.ConvParameterMask;
767 
768         ret = pimpl->proxy_->GetConversationListing(rawDevice, servicePara);
769     }
770     return ret;
771 }
772 
SetMessageStatus(const BluetoothRemoteDevice & device,MapMessageType type,const MapSetMessageStatus & msgStatus)773 int MapClient::SetMessageStatus(
774     const BluetoothRemoteDevice &device, MapMessageType type, const MapSetMessageStatus &msgStatus)
775 {
776     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
777     int ret = RET_BAD_PARAM;
778     if (!device.IsValidBluetoothRemoteDevice()) {
779         HILOGE("BluetoothRemoteDevice error");
780         return ret;
781     }
782     ret = RET_BAD_STATUS;
783     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
784         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
785 
786         ret = pimpl->proxy_->SetMessageStatus(rawDevice,
787             (int)type,
788             msgStatus.msgHandle,
789             (int)msgStatus.statusIndicator,
790             (int)msgStatus.statusValue,
791             msgStatus.extendedData);
792     }
793     return ret;
794 }
795 
SetOwnerStatus(const BluetoothRemoteDevice & device,const SetOwnerStatusParameters & para)796 int MapClient::SetOwnerStatus(const BluetoothRemoteDevice &device, const SetOwnerStatusParameters &para)
797 {
798     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
799     int ret = RET_BAD_PARAM;
800     if (!device.IsValidBluetoothRemoteDevice()) {
801         HILOGE("BluetoothRemoteDevice error");
802         return ret;
803     }
804     ret = RET_BAD_STATUS;
805     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
806         BluetoothIProfileSetOwnerStatusParameters servicePara;
807         servicePara.ConversationID = para.conversationId_;
808         servicePara.ownerStatus_.PresenceAvailability = para.ownerStatus_.PresenceAvailability;
809         servicePara.ownerStatus_.PresenceText = para.ownerStatus_.PresenceText;  // utf8
810         servicePara.ownerStatus_.LastActivity = para.ownerStatus_.LastActivity;  // utf8
811         servicePara.ownerStatus_.ChatState = para.ownerStatus_.ChatState;
812 
813         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
814 
815         ret = pimpl->proxy_->SetOwnerStatus(rawDevice, servicePara);
816     }
817     return ret;
818 }
819 
GetOwnerStatus(const BluetoothRemoteDevice & device,const std::string & conversationId) const820 int MapClient::GetOwnerStatus(const BluetoothRemoteDevice &device, const std::string &conversationId) const
821 {
822     HILOGI("enter, device: %{public}s, conversationId: %{public}s", GET_ENCRYPT_ADDR(device), conversationId.c_str());
823     int ret = RET_BAD_PARAM;
824     if (!device.IsValidBluetoothRemoteDevice()) {
825         HILOGE("BluetoothRemoteDevice error");
826         return ret;
827     }
828     ret = RET_BAD_STATUS;
829     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
830         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
831         ret = pimpl->proxy_->GetOwnerStatus(rawDevice, conversationId);
832     }
833     return ret;
834 }
835 
GetMasInstanceInfo(const BluetoothRemoteDevice & device) const836 MapMasInstanceInfoList MapClient::GetMasInstanceInfo(const BluetoothRemoteDevice &device) const
837 {
838     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
839     MapMasInstanceInfoList frameworkMasInfoList;
840     frameworkMasInfoList.isValid = false;
841     BluetoothIProfileMasInstanceInfoList infoList;
842     std::vector<MapMasInstanceInfo> instanceinfoList;
843 
844     if (!device.IsValidBluetoothRemoteDevice()) {
845         HILOGE("BluetoothRemoteDevice error");
846         return frameworkMasInfoList;
847     }
848     // process
849     if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
850         BluetoothRawAddress rawDevice(device.GetDeviceAddr());
851         infoList = pimpl->proxy_->GetMasInstanceInfo(rawDevice);
852 
853         HILOGI("Test!");
854         if (infoList.isValid == true) {
855             MapMasInstanceInfo info;
856             frameworkMasInfoList.isValid = true;
857             for (auto it = infoList.masInfoList.begin(); it != infoList.masInfoList.end(); it++) {
858                 info.OwnerUCI = it->OwnerUCI;
859                 info.MASInstanceInformation = it->MASInstanceInformation;
860                 info.supportedMsgTypes_ = it->supportedMsgTypes_;
861                 info.instanceId = it->instanceId;
862                 frameworkMasInfoList.masInfoList.push_back(info);
863             }
864         }
865     }
866     return frameworkMasInfoList;
867 }
868 }  // namespace Bluetooth
869 }  // namespace OHOS
870