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 instance state machine object. 21 * 22 */ 23 24 /** 25 * @file map_mce_instance_stm.h 26 * 27 * @brief map client service instance state machine header file . 28 * 29 */ 30 31 #ifndef MAP_MCE_INSTANCE_STM_H 32 #define MAP_MCE_INSTANCE_STM_H 33 34 #include <atomic> 35 #include <memory> 36 #include <mutex> 37 #include <string> 38 #include "../obex/obex_headers.h" 39 #include "btstack.h" 40 #include "dispatcher.h" 41 #include "map_mce_device_ctrl.h" 42 #include "map_mce_instance_client.h" 43 #include "map_mce_instance_request.h" 44 #include "map_mce_types.h" 45 #include "message.h" 46 #include "state_machine.h" 47 48 namespace OHOS { 49 namespace bluetooth { 50 /** 51 * @brief MceInstanceState Type 52 */ 53 enum MceInstanceStateType { 54 MCE_INSTANCE_STATE_CONNECTED, 55 MCE_INSTANCE_STATE_CONNECTING, 56 MCE_INSTANCE_STATE_DISCONNECTED, 57 MCE_INSTANCE_STATE_DISCONNECTING 58 }; 59 /** 60 * @brief state machine message define 61 */ 62 enum MapMceStmMessage { 63 // instance stm message 64 MSG_MASSTM_START = 0x2000, 65 // client stm interface api msg 66 MSG_MASSTM_REQ_CONNECT, 67 MSG_MASSTM_REQ_DISCONNECT, 68 MSG_MASSTM_REQ_GET_FOLDER_LISTING, 69 MSG_MASSTM_REQ_SET_NOTIFICATION_REGIST, 70 MSG_MASSTM_REQ_SET_PATH, 71 MSG_MASSTM_REQ_GET_INSTANCE, 72 // send request 73 MSG_MASSTM_REQ_SEND_REQUEST, 74 MSG_MASSTM_REQ_SEND_REQUEST_SELF, 75 // device msg 76 MSG_MASSTM_GAP_REQUEST_FINISH = MSG_MASSTM_START + 0x100, 77 MSG_MASSTM_GAP_REQUEST_FAILED, 78 MSG_MASSTM_OBEX_CONNECTED, 79 MSG_MASSTM_OBEX_CONNECTED_FAILED, 80 MSG_MASSTM_OBEX_TRANSPORT_FAILED, 81 MSG_MASSTM_OBEX_DISCONNECTED, 82 MSG_MASSTM_OBEX_INFO_CHG_CONTINUEBUSY_STATUS, 83 // normal request response 84 MSG_MASSTM_RECEIVE_REQUEST_COMPLETED, 85 }; 86 87 const std::string MCE_DISCONNECTED_STATE = "MceDisconnected"; 88 const std::string MCE_CONNECTING_STATE = "MceConnecting"; 89 const std::string MCE_CONNECTED_STATE = "MceConnected"; 90 const std::string MCE_DISCONNECTING_STATE = "MceDisconnecting"; 91 const std::string MCE_CONNECTED_STATE_S_REQSENDING = "MceConnectedReqSending"; 92 93 class MapMceInstanceStm : public utility::StateMachine { 94 public: 95 /** 96 * @brief copy operator 97 */ 98 BT_DISALLOW_COPY_AND_ASSIGN(MapMceInstanceStm); 99 /** 100 * @brief Construct a new Map Mce Instance Stm object 101 * @param deviceMgr 102 * @param dispatcher 103 * @param instanceId 104 */ 105 MapMceInstanceStm( 106 MapMceDeviceCtrl &deviceMgr, utility::Dispatcher &dispatcher, int instanceId, const MasInstanceConfig &config); 107 /** 108 * @brief Destroy the Mce Mas Instance Stm object 109 */ 110 ~MapMceInstanceStm(); 111 /** 112 * @brief Create a Stm object 113 */ 114 void CreateStm(); 115 /** 116 * @brief Post Message to map mce service task 117 * @param msg 118 */ 119 void PostMessage(utility::Message msg); 120 /** 121 * @brief message process 122 * @param msg 123 */ 124 void MceProcessMessage(utility::Message msg); 125 /** 126 * @brief Get the Target State object 127 * @return MceInstanceStateType 128 */ 129 MceInstanceStateType GetTargetState(); 130 /** 131 * @brief Get the current State object 132 * @return MceInstanceStateType 133 */ 134 std::string GetCurrentMceStmStateName(); 135 /** 136 * @brief Get the Bt Device object 137 * @return std::string 138 */ 139 std::string GetBtDevice(); 140 /** 141 * @brief Get the Bt Address object 142 * @return BtAddr 143 */ 144 BtAddr GetBtAddress(); 145 /** 146 * @brief Tans client TargetState 147 * @param state 148 */ 149 void TansTargetState(MceInstanceStateType state); 150 /** 151 * @brief Post Message to map mce service task 152 * @param msg message 153 * @param head obex header 154 */ 155 void PostMessageWithObexHeader(utility::Message msg, ObexHeader head); 156 /** 157 * @brief message process 158 * @param msg message 159 * @param head obex header 160 */ 161 void MceProcessMessageWithObexHeader(utility::Message msg, ObexHeader head); 162 /** 163 * @brief Post Message to map mce service task 164 * @param msg message 165 * @param req request 166 * @return int 167 */ 168 void PostMessageWithRequest(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req); 169 /** 170 * @brief process Message to map mce service task 171 * @param msg message 172 * @param req request 173 * @return int 174 */ 175 void MceProcessMessageWithRequest(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req); 176 /** 177 * @brief process Message to map mce service task 178 * @param msg message 179 * @param req request 180 * @return int 181 */ 182 void MceProcessMessageWithRequestInternal(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req); 183 /** 184 * @brief GetBusyStatus 185 * @return bool 186 */ 187 bool GetBusyStatus(); 188 /** 189 * @brief Change Busy Status 190 * @param status 191 */ 192 void ChangeStmBusyStatus(bool status); 193 /** 194 * @brief Get the Client object 195 * @return MapMceInstanceClient& 196 */ 197 MapMceInstanceClient &GetMasClient(); 198 199 private: 200 // mas client 201 std::unique_ptr<MapMceInstanceClient> masClient_ = nullptr; 202 // mce device mgr 203 MapMceDeviceCtrl &mceDeviceMgr_; 204 // obex header 205 std::unique_ptr<ObexHeader> stmObexHeader_ = nullptr; 206 // request ptr 207 std::unique_ptr<MapMceInstanceRequest> stmRequestPtr_ = nullptr; 208 // sending flag 209 std::atomic_bool stmSendFlag {false}; 210 211 /** 212 * @brief Change Busy Status 213 * @param status 214 */ 215 void ChangeContinueBusyStatus(bool status); 216 // bluetooth device address ,string 217 std::string btDevice_ = ""; 218 // bluetooth device address ,array 219 BtAddr btAddress_ {}; 220 // instance id 221 int masInstanceId_ = 0; 222 // target state 223 MceInstanceStateType targetState_ = MceInstanceStateType::MCE_INSTANCE_STATE_DISCONNECTED; 224 // dispacher 225 utility::Dispatcher &mceStmDispacher_; 226 // default config 227 MasInstanceConfig insDefaultConfig_ {}; 228 // stm mutex 229 std::recursive_mutex mceInstanceStmMutex_ {}; 230 // stm Data busy Flag 231 std::atomic_bool busyFlag_ {false}; 232 // stm ata Continue Flag 233 std::atomic_bool stmDataContinueFlag_ {false}; 234 // stm Data busy Flag 235 std::atomic_bool stmDataStmBusyFlag_ {false}; 236 /** 237 * @brief map state machine state 238 */ 239 class MapMceState : public utility::StateMachine::State { 240 public: 241 /** 242 * @brief Construct a new Map Mce State object 243 * @param name 244 * @param stm 245 * @param parent 246 */ MapMceState(const std::string & name,utility::StateMachine & stm)247 explicit MapMceState(const std::string &name, utility::StateMachine &stm) : State(name, stm){}; 248 /** 249 * @brief Construct a new Map Mce State object 250 * @param name 251 * @param stm 252 * @param parent 253 */ MapMceState(const std::string & name,utility::StateMachine & stm,State & parent)254 explicit MapMceState(const std::string &name, utility::StateMachine &stm, State &parent) 255 : State(name, stm, parent){}; 256 /** 257 * @brief Destroy the Map Mce State object 258 */ 259 virtual ~MapMceState() = default; 260 }; 261 /** 262 * @brief map state machine state 263 */ 264 class MapMceDisconnectedState : public MapMceState { 265 public: 266 /** 267 * @brief Construct a new Map Mce Disconnected State object 268 * @param stm 269 */ MapMceDisconnectedState(utility::StateMachine & stm)270 explicit MapMceDisconnectedState(utility::StateMachine &stm) 271 : MapMceState(MCE_DISCONNECTED_STATE, stm), mceInstanceStm_((MapMceInstanceStm &)stm){}; 272 /** 273 * @brief Destroy the Map Mce Disconnected State object 274 */ 275 virtual ~MapMceDisconnectedState() = default; 276 /** 277 * @brief state entry 278 */ 279 virtual void Entry(); 280 /** 281 * @brief state exit 282 */ 283 virtual void Exit(); 284 /** 285 * @brief dispatch 286 * @param msg 287 * @return true 288 * @return false 289 */ 290 virtual bool Dispatch(const utility::Message &msg); 291 292 private: 293 // instance stm 294 MapMceInstanceStm &mceInstanceStm_; 295 }; 296 /** 297 * @brief map state machine state 298 */ 299 class MapMceConnectingState : public MapMceState { 300 public: 301 /** 302 * @brief Construct a new Map Mce Connecting State object 303 * @param stm 304 */ MapMceConnectingState(utility::StateMachine & stm)305 explicit MapMceConnectingState(utility::StateMachine &stm) 306 : MapMceState(MCE_CONNECTING_STATE, stm), mceInstanceStm_((MapMceInstanceStm &)stm){}; 307 /** 308 * @brief Destroy the Map Mce Connecting State object 309 */ 310 virtual ~MapMceConnectingState() = default; 311 /** 312 * @brief state entry 313 */ 314 virtual void Entry(); 315 /** 316 * @brief state exit 317 */ 318 virtual void Exit(); 319 /** 320 * @brief msg dispatch 321 * @param msg 322 * @return true 323 * @return false 324 */ 325 virtual bool Dispatch(const utility::Message &msg); 326 327 private: 328 /** 329 * @brief Connected dispatch 330 */ 331 void DispatchProcObexConnected(); 332 /** 333 * @brief disconnected dispatch 334 */ 335 void DispatchProcObexDisconnected(); 336 /** 337 * @brief transport failed dispatch 338 */ 339 void DispatchProcTransportFailed(const utility::Message &msg); 340 /** 341 * @brief connected failed dispatch 342 */ 343 void DispatchProcObexConnectedFailed(); 344 /** 345 * @brief connected failed dispatch 346 */ 347 void DispatchProcConnectedFailed(); 348 // mce instance stm 349 MapMceInstanceStm &mceInstanceStm_; 350 }; 351 /** 352 * @brief map state machine state 353 */ 354 class MapMceConnectedState : public MapMceState { 355 public: 356 /** 357 * @brief Construct a new Map Mce Connected State object 358 * @param stm 359 */ MapMceConnectedState(utility::StateMachine & stm)360 explicit MapMceConnectedState(utility::StateMachine &stm) 361 : MapMceState(MCE_CONNECTED_STATE, stm), mceInstanceStm_((MapMceInstanceStm &)stm){}; 362 /** 363 * @brief Destroy the Map Mce Connected State object 364 */ 365 virtual ~MapMceConnectedState() = default; 366 /** 367 * @brief state entry 368 */ 369 virtual void Entry(); 370 /** 371 * @brief state exit 372 */ 373 virtual void Exit(); 374 /** 375 * @brief msg dispatch 376 * @param msg 377 * @return true 378 * @return false 379 */ 380 virtual bool Dispatch(const utility::Message &msg); 381 382 private: 383 // instance stm 384 MapMceInstanceStm &mceInstanceStm_; 385 }; 386 387 /** 388 * @brief map state machine state 389 */ 390 class MapMceConnectedStateSubReqSending : public MapMceState { 391 public: 392 /** 393 * @brief Construct a new Map Mce Connected State Sub Req Sending object 394 * @param stm 395 * @param parent 396 */ MapMceConnectedStateSubReqSending(utility::StateMachine & stm,State & parent)397 MapMceConnectedStateSubReqSending(utility::StateMachine &stm, State &parent) 398 : MapMceState(MCE_CONNECTED_STATE_S_REQSENDING, stm, parent), mceInstanceStm_((MapMceInstanceStm &)stm){}; 399 /** 400 * @brief Destroy the Map Mce Connected State Sub Req Sending object 401 */ 402 virtual ~MapMceConnectedStateSubReqSending() = default; 403 /** 404 * @brief state entry 405 */ 406 virtual void Entry(); 407 /** 408 * @brief state exit 409 */ 410 virtual void Exit(); 411 /** 412 * @brief state dispatch 413 * @param msg 414 * @return true 415 * @return false 416 */ 417 virtual bool Dispatch(const utility::Message &msg); 418 419 private: 420 /** 421 * @brief process Receive Request Complite 422 * @param msg My Param doc 423 */ 424 void ProcReceiveRequestComplite(const utility::Message &msg); 425 // mce instance stm 426 MapMceInstanceStm &mceInstanceStm_; 427 }; 428 /** 429 * @brief map state machine state 430 */ 431 class MapMceDisconnectingState : public MapMceState { 432 public: 433 /** 434 * @brief Construct a new Map Mce Disconnecting State object 435 * @param stm 436 */ MapMceDisconnectingState(utility::StateMachine & stm)437 explicit MapMceDisconnectingState(utility::StateMachine &stm) 438 : MapMceState(MCE_DISCONNECTING_STATE, stm), mceInstanceStm_((MapMceInstanceStm &)stm){}; 439 /** 440 * @brief Destroy the Map Mce Disconnecting State object 441 */ 442 virtual ~MapMceDisconnectingState() = default; 443 /** 444 * @brief state entry 445 */ 446 virtual void Entry(); 447 /** 448 * @brief state exit 449 */ 450 virtual void Exit(); 451 /** 452 * @brief msg dispatch 453 */ 454 virtual bool Dispatch(const utility::Message &msg); 455 456 private: 457 // instance stm 458 MapMceInstanceStm &mceInstanceStm_; 459 }; 460 }; 461 } // namespace bluetooth 462 } // namespace OHOS 463 #endif // MAP_MCE_INSTANCE_STM_H 464