1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines map client service object. 21 * 22 */ 23 24 /** 25 * @file map_mce_service.h 26 * 27 * @brief map client service header file . 28 * 29 */ 30 31 #ifndef MAP_MCE_SERVICE_H 32 #define MAP_MCE_SERVICE_H 33 34 #include <atomic> 35 #include <cstdint> 36 #include <list> 37 #include <map> 38 #include <mutex> 39 #include <string> 40 #include <vector> 41 #include "../obex/obex_mp_server.h" 42 #include "bt_def.h" 43 #include "context.h" 44 #include "interface_profile.h" 45 #include "interface_profile_map_mce.h" 46 #include "map_mce_instance_request.h" 47 #include "map_mce_observer_manager.h" 48 #include "map_mce_parameter.h" 49 #include "map_mce_types.h" 50 #include "message.h" 51 #include "raw_address.h" 52 53 namespace OHOS { 54 namespace bluetooth { 55 // external class and c function declare 56 class MapMceMnsServer; 57 class MapMceDeviceCtrl; 58 59 enum MceServiceEnumType { 60 MCE_MAX_OF_SENDING_REQUEST = 30, 61 MCE_MAX_LENGTH_10 = 10 62 }; 63 64 enum MapMceServiceStateType { 65 MAP_MCE_STATE_STARTUP, 66 MAP_MCE_STATE_STARTUPING, 67 MAP_MCE_STATE_SHUTDOWN, 68 MAP_MCE_STATE_SHUTDOWNING 69 }; 70 71 enum MapMceServiceMessage { 72 // service message 73 MSG_MCESERVICE_START = 0x0000, // don't modify it 74 // service message 75 MSG_MCESERVICE_DEVICE_CONNECTED = MSG_MCESERVICE_START, 76 MSG_MCESERVICE_DEVICE_DISCONNECTED, 77 MSG_MCESERVICE_DEVICE_CONNECT_FAILED, 78 MSG_MCESERVICE_DEVICE_TRANSPORT_ERRO, 79 MSG_MCESERVICE_GET_SURPORT_FEATURES_FINISH, 80 MSG_MCESERVICE_GET_SURPORT_FEATURES_FAILED, 81 }; 82 /** 83 * @brief map mce service 84 */ 85 class MapMceService : public IProfileMapMce, public utility::Context { 86 public: 87 /** 88 * @brief copy operator 89 */ 90 BT_DISALLOW_COPY_AND_ASSIGN(MapMceService); 91 /** 92 * @brief service name 93 * @return const std::string 94 */ Name()95 const std::string Name() 96 { 97 return PROFILE_NAME_MAP_MCE; 98 } 99 /** 100 * @brief Construct a new Map Mce Service object 101 */ 102 MapMceService(); 103 /** 104 * @brief Destroy the Map Mce Service object 105 */ 106 ~MapMceService(); 107 /** 108 * @brief GetContext 109 */ 110 utility::Context *GetContext() override; 111 /** 112 * @brief register observer 113 * @param observer function pointer 114 */ 115 void RegisterObserver(IProfileMapMceObserver &observer) override; 116 /** 117 * @brief deregister observer 118 * @param observer function pointer 119 */ 120 void DeregisterObserver(IProfileMapMceObserver &observer) override; 121 /** 122 * @brief check connect status 123 * @param device Bluetooth Device address 124 * @return true success 125 * @return false failed 126 */ 127 bool IsConnected(const RawAddress &device) override; 128 /** 129 * @brief Get the Devices By States object 130 * @param states device state 131 * @return std::vector<RawAddress> Bluetooth Device list 132 */ 133 std::vector<RawAddress> GetDevicesByStates(const std::vector<int> &statusList) override; 134 /** 135 * @brief Set the Connection Strategy object 136 * @param device Bluetooth Device address 137 * @param strategy strategy 138 * @param masInstanceId Instance Id 139 * @return int 140 */ 141 int SetConnectionStrategy(const RawAddress &device, int strategy) override; 142 /** 143 * @brief Get the Connection Strategy object 144 * @param device Bluetooth Device address 145 * @return int 146 */ 147 int GetConnectionStrategy(const RawAddress &device) const override; 148 /** 149 * @brief Get the Unread Messages object 150 * @param device Bluetooth Device address 151 * @param type message type 152 * @param maxListCount iterm number 153 * @return int 154 */ 155 int GetUnreadMessages(const RawAddress &device, MapMessageType type, uint8_t max) override; 156 /** 157 * @brief Get the Supported Features object 158 * @param device Bluetooth Device address 159 * @return int 160 */ 161 int GetSupportedFeatures(const RawAddress &device) override; 162 /** 163 * @brief send mas request , send new messange 164 * @param device Bluetooth Device address 165 * @param msg message parameters 166 * @return int 167 */ 168 int SendMessage(const RawAddress &device, const IProfileSendMessageParameters &msg) override; 169 /** 170 * @brief Set the Notification Filter object 171 * @param device Bluetooth Device address 172 * @param on 173 * @return int 174 */ 175 int SetNotificationFilter(const RawAddress &device, const int mask) override; 176 /** 177 * @brief Set the Notification on off 178 * @param device Bluetooth Device address 179 * @param on 180 * @return int 181 */ 182 int SetNotificationRegistration(const RawAddress &device, const bool on) override; 183 /** 184 * @brief Get the Messages Listing object 185 * @param device Bluetooth Device address 186 * @param para 187 * @return int 188 */ 189 int GetMessagesListing(const RawAddress &device, const IProfileGetMessagesListingParameters ¶) override; 190 /** 191 * @brief Get the Message object 192 * @param device Bluetooth Device address 193 * @param msgHandle 194 * @param para message parameters 195 * @return int 196 */ 197 int GetMessage(const RawAddress &device, MapMessageType type, const std::u16string &msgHandle, 198 const IProfileGetMessageParameters ¶) override; 199 /** 200 * @brief Get the Message object 201 * @param device Bluetooth Device address 202 * @param para message parameters 203 * @param cfg config data 204 * @return int 205 */ 206 int GetMessageWithReqCfg(const RawAddress &device, MapMessageType type, const std::u16string &msgHandle, 207 const IProfileGetMessageParameters ¶, const MapMceRequestConfig &cfg); 208 /** 209 * @brief update inbox 210 * @param device Bluetooth Device address 211 * @return int 212 */ 213 int UpdateInbox(const RawAddress &device, MapMessageType type) override; 214 /** 215 * @brief Get the Conversation Listing object 216 * @param device Bluetooth Device address 217 * @param para 218 * @return int 219 */ 220 int GetConversationListing(const RawAddress &device, const IProfileGetConversationListingParameters ¶) override; 221 /** 222 * @brief Set the Message Status object 223 * @param device 224 * @param type 225 * @param msgStatus 226 * @return int 227 */ 228 int SetMessageStatus(const RawAddress &device, MapMessageType type, 229 const IProfileSetMessageStatus &msgStatus) override; 230 /** 231 * @brief Set the Owner Status object 232 * @param device Bluetooth Device address 233 * @param para 234 * @return int 235 */ 236 int SetOwnerStatus(const RawAddress &device, const IProfileSetOwnerStatusParameters ¶) override; 237 /** 238 * @brief Get the Owner Status object 239 * @param device Bluetooth Device address 240 * @param conversationId conversation Id 241 * @return int 242 */ 243 int GetOwnerStatus(const RawAddress &device, const std::string &conversationId) override; 244 /** 245 * @brief connect one bt device and all instance with map profile 246 * @param device 247 * @return int 248 */ 249 int Connect(const RawAddress &device) override; 250 /** 251 * @brief disconnect one bt device and all instance in the device with map profile 252 * @param device Bluetooth Device address 253 * @return int 254 */ 255 int Disconnect(const RawAddress &device) override; 256 /** 257 * @brief system request , user app do not use 258 * @return int ProfileState mask type 259 */ 260 int GetConnectState() override; 261 /** 262 * @brief Get the Connect State object 263 * @return int FrameworkStatus type 264 */ 265 int GetDeviceConnectState(const RawAddress &device) override; 266 /** 267 * @brief Get folder listing 268 * 269 * @param device bluetooth address 270 * @param maxOfListCount max of list count 271 * @param startOffset start offset 272 * @return int 273 */ 274 int GetFolderListing(const RawAddress &device, uint16_t maxOfListCount, uint16_t startOffset) override; 275 /** 276 * @brief set folder path 277 * 278 * @param device bluetooth address 279 * @param flags flags control 280 * @param folder folder name 281 * @param folderList folder list 282 * @return int 283 */ 284 int SetPath(const RawAddress &device, const uint8_t flags, const std::u16string &folder, 285 std::vector<std::u16string> &folderList) override; 286 /** 287 * @brief Get the Mas Instance Info object 288 * @param device Bluetooth Device address 289 * @return IProfileMasInstanceInfoList instance info list 290 */ 291 IProfileMasInstanceInfoList GetMasInstanceInfo(const RawAddress &device) override; 292 /** 293 * @brief startup for adapter manager 294 * @return int 295 */ 296 void Enable() override; 297 /** 298 * @brief shutdown for adapter manager 299 * @return int 300 */ 301 void Disable() override; 302 /** 303 * @brief Get the Max Connect Num object 304 * @return int 305 */ 306 int GetMaxConnectNum() override; 307 /** 308 * @brief Get the Connect Devices object 309 * @return std::list<RawAddress> 310 */ 311 std::list<RawAddress> GetConnectDevices() override; 312 /** 313 * @brief Connect single instance 314 * @param device bluetooth device address 315 * @param instanceId instance id 316 * @return int 317 */ 318 int ConnectInstance(const RawAddress &device, int instanceId); 319 /** 320 * @brief check device status 321 * @param device Bluetooth Device address 322 * @return int 323 */ 324 int CheckDeviceConnectStatus(const RawAddress &device); 325 /** 326 * @brief check device status 327 * @param device Bluetooth Device address 328 * @return int 329 */ 330 int CheckDeviceDisconnectStatus(const RawAddress &device); 331 /** 332 * @brief Send Request To Connected Device 333 * @param device Bluetooth Device address 334 * @param msg request pointer 335 * @return int 336 */ 337 int SendRequestToConnectedDevice( 338 const RawAddress &device, utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req); 339 /** 340 * @brief Count Sending Request num 341 * @param device Bluetooth Device address 342 * @param requestType request type 343 * @return int 344 */ 345 int CountSendingRequest(const RawAddress &device, MceRequestType requestType); 346 /** 347 * @brief connect one bt device and all instance in the device 348 * @param device Bluetooth Device address 349 * @return int 350 */ 351 int ConnectInternal(const RawAddress &device, bool sInsMode, int sInsId); 352 /** 353 * @brief start one instance of device to disconnecting 354 * @param device Bluetooth Device address 355 * @return int 356 */ 357 int DisconnectInternal(const RawAddress &device); 358 /** 359 * @brief start up internal 360 * @return int 361 */ 362 int StartUpInternal(); 363 /** 364 * @brief shutdown for adapter manager 365 * @return int 366 */ 367 int ShutDownInternal(); 368 /** 369 * @brief CheckSdp ForGetSupportedFeatures 370 * @param device Bluetooth Device address 371 * @return int 372 */ 373 int CheckSdpForGetSupportedFeatures(const RawAddress &device); 374 /** 375 * @brief post message to map mce thread for call back function send back message 376 * @param msg message param 377 */ 378 void PostMessage(utility::Message msg); 379 /** 380 * @brief Get the Service States object 381 * @return MapMceServiceStateType 382 */ 383 MapMceServiceStateType GetServiceStates(); 384 /** 385 * @brief Set Default Config 386 * @return void 387 */ 388 void SetDefaultConfig(const MasInstanceConfig &configSave); 389 /** 390 * @brief Mns Post Message 391 * @param msg message 392 * @param btAddr bluetooth address 393 */ 394 void MnsPostMessage(const utility::Message &msg, std::string btAddr); 395 /** 396 * @brief Mns Post Message With obex Header 397 * @param msg message 398 * @param btAddr bluetooth address 399 */ 400 void MnsPostMessageWithHeader(const utility::Message &msg, std::string btAddr, const ObexHeader &req); 401 402 protected: 403 private: 404 /** 405 * @brief shutdone internal common process 406 * @return int 407 */ 408 int ShutDownInternalCommon(); 409 /** 410 * @brief process message 411 * @param msg message param 412 */ 413 void ProcessMessage(const utility::Message &msg); 414 /** 415 * @brief process message from mns server 416 * @param msg message param 417 */ 418 void MnsProcessMessage(const utility::Message msg, std::string btAddr); 419 /** 420 * @brief process message from mns server 421 * @param msg message param 422 */ 423 void MnsProcessMessageWithHeader(utility::Message msg, std::string btAddr, ObexHeader req); 424 /** 425 * @brief sdp result dispatch th instance state machine 426 * @param msg message param 427 */ 428 void SdpDispatchToDeviceInstMgr(const utility::Message &msg); 429 /** 430 * @brief Process GetSupportedFeatures Respones 431 * @param msg 432 */ 433 void ProcessGetSupportedFeaturesRespones(const utility::Message &msg); 434 /** 435 * @brief remove no use devcie and instance 436 */ 437 void RemoveNoUseDeviceAndInstance(); 438 /** 439 * @brief remove all Bluetooth Device 440 */ 441 void RemoveAllDevice(); 442 /** 443 * @brief Get the Config From Xml object 444 */ 445 void GetConfigFromXml(); 446 /** 447 * @brief type change MapMessageType To IprofileMask int 448 * @param type messange type 449 * @return int 450 */ 451 int MapMessageTypeToIprofileMask(MapMessageType type); 452 // servcie thread dispatcher 453 utility::Dispatcher *serviceDispatcher_ = nullptr; 454 // service state 455 std::atomic_int serviceState_ {MapMceServiceStateType::MAP_MCE_STATE_SHUTDOWN}; 456 // bt device instance magr's map 457 std::map<const std::string, std::unique_ptr<MapMceDeviceCtrl>> serviceBtDeviceInstMgrMap_ {}; 458 // mns server pointer 459 std::unique_ptr<MapMceMnsServer> mnsServer_ = nullptr; 460 // notification Registration default value 461 std::atomic_bool notificationRegistration_ {true}; 462 // default config 463 MasInstanceConfig insDefaultConfig_ {}; 464 // service rpc callback manager 465 MapMceObserverManager serviceRpcCallbackMgr_ {}; 466 // mutex 467 std::recursive_mutex mceDeviceMapMutex_ {}; 468 }; 469 } // namespace bluetooth 470 } // namespace OHOS 471 #endif // MAP_MCE_SERVICE_H 472