• 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 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines map client service device control.
21  *
22  */
23 
24 /**
25  * @file map_mce_device_ctrl.h
26  *
27  * @brief map client service device control.
28  *
29  */
30 
31 #ifndef MAP_MCE_DEVICE_CTRL_H
32 #define MAP_MCE_DEVICE_CTRL_H
33 
34 #include <atomic>
35 #include <cstdint>
36 #include <map>
37 #include <mutex>
38 #include <string>
39 #include <vector>
40 #include "../obex/obex_headers.h"
41 #include "../obex/obex_session.h"
42 #include "bt_def.h"
43 #include "map_mce_instance_request.h"
44 #include "map_mce_service.h"
45 #include "map_mce_types.h"
46 #include "message.h"
47 #include "state_machine.h"
48 
49 namespace OHOS {
50 namespace bluetooth {
51 /**
52  * @brief map mce device state type
53  */
54 enum MapMceDeviceStateType {
55     MAP_MCE_DEV_STATE_CONNECTED = int(BTConnectState::CONNECTED),
56     MAP_MCE_DEV_STATE_CONNECTING = int(BTConnectState::CONNECTING),
57     MAP_MCE_DEV_STATE_DISCONNECTED = int(BTConnectState::DISCONNECTED),
58     MAP_MCE_DEV_STATE_DISCONNECTING = int(BTConnectState::DISCONNECTING)
59 };
60 /**
61  * @brief map mce device busy state type
62  */
63 enum MapMcePowerBusyState {
64     MAP_MCE_PWBUSY_STATUS_DISCONNECT = 0,
65     MAP_MCE_PWBUSY_STATUS_CONNECT = 1,
66     MAP_MCE_PWBUSY_STATUS_LOW = 2,
67     MAP_MCE_PWBUSY_STATUS_HIGH = 3
68 };
69 /**
70  * @brief map mce device state type
71  */
72 enum MapMceDeviceMessage {
73     MSG_MCEDEVICE_START = 0x1000,  // don't modify it
74     // device msg
75     MSG_MCEDEVICE_SDP_GET_INSTANCE_FINISH,
76     MSG_MCEDEVICE_SDP_GET_INSTANCE_FAILED,
77     // MAS client msg
78     MSG_MCEDEVICE_INSCLIENT_DISCONNECTED,
79     MSG_MCEDEVICE_INSCLIENT_CONNECTED,
80     MSG_MCEDEVICE_INSCLIENT_CONNECT_FAILED,
81     MSG_MCEDEVICE_INSCLIENT_TRANSPORT_ERRO,
82     MSG_MCEDEVICE_REQ_DEVICE_DISCONNECT,
83     MSG_MCEDEVICE_REQ_DEVICE_CONNECT,
84     MSG_MCEDEVICE_REQ_SEND_REQUEST,
85     MSG_MCEDEVICE_INSCLIENT_INFO_BUSY_STATUS_CHANGE,
86     // MNS server msg
87     MSG_MCEDEVICE_MNS_INFO_TRANSPORT_INCOMING,
88     MSG_MCEDEVICE_MNS_INFO_CONNECT,
89     MSG_MCEDEVICE_MNS_INFO_DISCONNECT,
90     MSG_MCEDEVICE_MNS_INFO_TRANSPORT_ERROR,
91     MSG_MCEDEVICE_MNS_INFO_TRANSPORT_DISCONNECTED,
92     MSG_MCEDEVICE_MNS_INFO_PUT,
93     MSG_MCEDEVICE_MNS_DEVICE_DISCONNECTED_FINISH,
94     MSG_MCEDEVICE_MNS_INFO_BUSY_STATUS_CHANGE
95 };
96 /**
97  * @brief mce device client control, manager the isntances list.
98  */
99 class MapMceDeviceCtrl {
100 public:
101     /**
102      * @brief  copy operator
103      */
104     BT_DISALLOW_COPY_AND_ASSIGN(MapMceDeviceCtrl);
105     /**
106      * @brief Construct a new Mce Device Instance Mgr object
107      * @param  device
108      * @param  service
109      * @param  rpcId
110      */
111     MapMceDeviceCtrl(const std::string &device, MapMceService &service, bool notifySet, MasInstanceConfig config,
112         MapMceObserverManager &callback);
113     /**
114      * @brief Destroy the Mce Device Instance Mgr object
115      */
116     ~MapMceDeviceCtrl();
117     /**
118      * @brief post message to service thread device module
119      * @param  msg
120      */
121     void PostMessage(utility::Message msg);
122     /**
123      * @brief  Post Message With Request
124      * @param  msg              message
125      * @param  req              request
126      * @return int
127      */
128     int PostMessageWithRequest(utility::Message msg, std::unique_ptr<MapMceInstanceRequest> &req);
129     /**
130      * @brief Get the Current Device State object
131      * @return MapMceDeviceStateType
132      */
133     MapMceDeviceStateType GetCurrentDeviceState();
134     /**
135      * @brief Get the Current Device State object
136      * @return MapMceDeviceStateType
137      */
138     MapMceDeviceStateType GetTargetDeviceState() const;
139     /**
140      * @brief
141      * @param  deviceState
142      * @return int
143      */
144     static int DeviceStateConvertToProfileState(MapMceDeviceStateType deviceState);
145     /**
146      * @brief process message
147      * @param  msg
148      */
149     void ProcessMessage(utility::Message msg);
150     /**
151      * @brief process MnsObexObserver message
152      * @param  msg
153      */
154     void ProcessMnsObexObserverMessage(const ObexHeader &req, utility::Message msg);
155     /**
156      * @brief Get the Bt Device object
157      * @return std::string
158      */
159     std::string GetBtDevice();
160     /**
161      * @brief Get the Bt Address object
162      * @return BtAddr
163      */
164     BtAddr GetBtAddress();
165     /**
166      * @brief Get the Device Rpc Callback Mgr object
167      * @return MapMceObserverManager*
168      */
169     MapMceObserverManager &GetDeviceCallbackMgr() const;
170     /**
171      * @brief Get the Device Instance Stm Map size
172      * @return map size
173      */
174     int GetDeviceInstanseStmMapSize();
175     /**
176      * @brief Get MasInstanceInfo
177      * @param  device           bluetooth device address
178      * @return std::vector<IProfileMasInstanceInfo>
179      */
180     std::vector<IProfileMasInstanceInfo> GetMasInstanceInfo(const RawAddress &device);
181     /**
182      * @brief Get the Mns State object
183      * @return MapMceDeviceStateType
184      */
185     MapMceDeviceStateType GetMnsState() const;
186     /**
187      * @brief Count Sending Request
188      * @param  requestType           request Type
189      * @return int
190      */
191     int CountSendingRequest(MceRequestType requestType);
192     /**
193      * @brief Se tDeviceCtl Config
194      * @param  config           config value
195      */
196     void SetDeviceCtlConfig(const MasInstanceConfig &config);
197 
198 private:
199     /**
200      * @brief trans state machine
201      * @param  state
202      */
203     void TransClientState(MapMceDeviceStateType state);
204     /**
205      * @brief state connected process massage
206      * @param  msg
207      */
208     void ClientConnectedProcMsg(utility::Message msg);
209     /**
210      * @brief state connecting process massage
211      * @param  msg
212      */
213     void ClientConnectingProcMsg(utility::Message msg);
214     /**
215      * @brief state disconnected process massage
216      * @param  msg
217      */
218     void ClientDisconnectedProcMsg(utility::Message msg);
219     /**
220      * @brief state disconnecting process massage
221      * @param  msg
222      */
223     void ClientDisConnectingProcMsg(utility::Message msg);
224     /**
225      * @brief start connecting
226      * @return int
227      */
228     int StartConnecting();
229     /**
230      * @brief All Instances start connecting
231      */
232     void AllInstancesStartConnecting();
233     /**
234      * @brief Get the Supported Mas Instances object
235      * @param  device
236      * @return int
237      */
238     static int GetSupportedMasInstances(const RawAddress &device);
239     /**
240      * @brief process the sdp coming infomachine
241      * @param  msg
242      */
243     int ProcessGetInstance(const utility::Message &msg);
244     /**
245      * @brief All Instances start disconnecting
246      * @return true
247      * @return false
248      */
249     bool AllInstancesStartDisConnecting();
250     /**
251      * @brief process obex coming connect info
252      */
253     void ProcessConnected();
254     /**
255      * @brief rocess obex coming disconnect info
256      */
257     void ProcessDisconnected();
258     /**
259      * @brief  send request
260      * @param  req              req data
261      */
262     void SendRequest(utility::Message msg);
263     /**
264      * @brief  MessageState msg Process
265      * @param  msg
266      */
267     void ProcessMessageStateProcess(utility::Message msg);
268     /**
269      * @brief  connected process message
270      * @param  msg
271      */
272     void MnsConnectedProcMsg(utility::Message msg);
273     /**
274      * @brief  connected process message
275      * @param  msg
276      */
277     void MnsConnectingProcMsg(utility::Message msg);
278     /**
279      * @brief  disconnected process message
280      * @param  msg
281      */
282     void MnsDisconnectedProcMsg(utility::Message msg);
283     /**
284      * @brief  disconnecting process message
285      * @param  msg
286      */
287     void MnsDisconnectingProcMsg(utility::Message msg);
288     /**
289      * @brief  trans mns state
290      * @param  state
291      */
292     void TransMnsState(MapMceDeviceStateType state);
293     /**
294      * @brief  process obex put
295      * @param  msg
296      */
297     void MnsProcObexPut(utility::Message msg);
298     /**
299      * @brief  mns process accept
300      * @param  msg
301      */
302     void MnsProcConnectAccept(utility::Message msg);
303     /**
304      * @brief  mns process incoming accept
305      * @param  msg
306      */
307     void MnsProcIncomingAccept(utility::Message msg);
308     /**
309      * @brief disconnected massage process
310      */
311     void MnsProcDisconnected();
312     /**
313      * @brief  Mns server Process Connect Refuse
314      * @param  msg
315      */
316     void MnsProcConnectRefuse(utility::Message msg);
317     /**
318      * @brief  Mns server Process Disconnect Accept
319      * @param  msg
320      * @return int
321      */
322     int MnsProcDisconnectAccept(utility::Message msg);
323     /**
324      * @brief Mns Process Request Disconnect
325      */
326     void MnsProcRequestDisconnect();
327     /**
328      * @brief  Mns save ObexHeader when receive observer
329      * @param  req
330      */
331     void MnsChangeSessionObexHeader(const ObexHeader &req);
332     /**
333      * @brief  Process Error Observer
334      * @param  request
335      */
336     void ProcessErrorObserver(MapMceInstanceRequest &request);
337     /**
338      * @brief  Change Request Type To Iprofile Type
339      * @param  type
340      * @return MapActionType
341      */
342     static MapActionType ChangeRequestTypeToIprofileType(MceRequestType type);
343     /**
344      * @brief  Mns server Process Check TargetId when Connect Accept
345      * @return int
346      */
347     int MnsProcConnectAcceptCheckTargetId();
348     /**
349      * @brief Check PowerBusyState Change
350      * @param  state
351      */
352     void CheckPowerBusyStateChange();
353     /**
354      * @brief trans Power Busy State
355      * @param  state
356      */
357     void TransPowerBusyState(MapMcePowerBusyState state);
358     // uuid
359     static const uint8_t mnsTargetUuidTbl_[MAX_OF_MASCLIENT_OBEX_UUID_TBL];
360     // device address string type
361     std::string btDevice_ = "";
362     // device state
363     std::atomic_int btDeviceState_ {MapMceDeviceStateType::MAP_MCE_DEV_STATE_DISCONNECTED};
364     // target device state
365     std::atomic_int btDeviceTargetState_ {MapMceDeviceStateType::MAP_MCE_DEV_STATE_DISCONNECTED};
366     // service pointer
367     MapMceService &devService_;
368     // mce mns server status
369     std::atomic_int mnsServerStatus_ {MapMceDeviceStateType::MAP_MCE_DEV_STATE_DISCONNECTED};
370     // mce mns server session
371     ObexServerSession *mnsObexSession_ = nullptr;
372     // mce mns incoming
373     ObexIncomingConnect *incomingPtr = nullptr;
374     // mce mns obex header
375     std::unique_ptr<ObexHeader> mnsSessionObexheader_ = nullptr;
376     // notification Registration default value
377     bool notificationRegistration_ = false;
378     // device supported Features_
379     uint32_t supportedFeatures_ = 0;
380     // default config
381     MasInstanceConfig insDefaultConfig_ {};
382     // connect Id
383     int connectId_ = 0;
384     // instance id map
385     std::map<const int, std::unique_ptr<utility::StateMachine>> deviceInstanceStmMap_ {};
386     // call back mgr pointer
387     MapMceObserverManager &deviceRpcCallbackMgr_;
388     // mutex
389     std::recursive_mutex mceInstanceMapMutex_ {};
390     // request ptr
391     std::unique_ptr<MapMceInstanceRequest> ctrlRequestPtr_ = nullptr;
392     // send flag
393     std::atomic_bool ctrlSendFlag_ {false};
394     // busy flag
395     MapMcePowerBusyState ctrlBusyStatus_ = MAP_MCE_PWBUSY_STATUS_DISCONNECT;
396     // mns Continue Flag
397     std::atomic_bool mnsContinueBusyFlag_ {false};
398 };
399 }  // namespace bluetooth
400 }  // namespace OHOS
401 #endif  // MAP_MCE_DEVICE_CTRL_H
402