• 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 instance client object.
21  *
22  */
23 
24 /**
25  * @file map_mce_instance_client.h
26  *
27  * @brief map client service instance client header file .
28  *
29  */
30 
31 #ifndef MAP_MCE_INSTANCE_CLIENT_H
32 #define MAP_MCE_INSTANCE_CLIENT_H
33 
34 #include <cstdint>
35 #include <list>
36 #include <mutex>
37 #include <vector>
38 #include "../obex/obex_headers.h"
39 #include "../obex/obex_mp_client.h"
40 #include "bt_def.h"
41 #include "gap_if.h"
42 #include "map_mce_instance_request.h"
43 #include "map_mce_service.h"
44 #include "raw_address.h"
45 #include "state_machine.h"
46 
47 namespace OHOS {
48 namespace bluetooth {
49 // SDP Record for the Message Access Service on the MAP Device
50 static const uint16_t MAP_MCE_SERVICE_CLASS_UUID = 0x1132;
51 static const uint16_t MAP_MCE_GOEP_L2CAP_PSM_VALUE = 0x101F;
52 static const uint16_t MAP_MCE_SUPPORTED_MESSAGE_TYPE_ATTRIBUTE_ID = 0x0316;
53 // GoepL2capPsm (MAP v1.2 and later)
54 static const uint16_t MAP_MCE_GOEP_L2CAP_PSM_ATTRIBUTE_ID = 0x0200;
55 // MASInstanceID
56 static const uint16_t MAP_MCE_INSTANCE_ATTRIBUTE_ID = 0x0315;
57 
58 // Map Supported Features
59 static const uint16_t MAP_MCE_SUPPORTED_FEATURES_ATTRIBUTE_ID = 0x0317;
60 static const uint32_t MAP_MCE_SUPPORTED_FEATURES_V14 = 0x007EFFFF;  // only bit 16 (pbap contact cross) is 0
61 
62 /**
63  * @brief mas mce sdp param
64  */
65 struct MasSdpParam {
66     uint8_t instanceId = 1;
67     uint8_t messageType = 0;
68     std::string serviceName = "";
69     bool isGoepL2capPSM = false;    // default is rfcomm
70     uint16_t scn = 0;               // rfcomm or l2cap
71     uint32_t supportedFeatrue = 0;  // rfcomm
72     GAP_Service gapNumber = MAP_CLIENT_ID_START;
73 };
74 
75 /**
76  * @brief sdp msg param
77  */
78 struct MapSdpMsgArgPrt {
79     BtAddr address {};
80     std::vector<MasSdpParam> masSdpParamListPrt {};
81 };
82 
83 /**
84  * @brief Mce InstanceClient
85  */
86 class MapMceInstanceClient {
87 public:
88     /**
89      * @brief  copy operator
90      */
91     BT_DISALLOW_COPY_AND_ASSIGN(MapMceInstanceClient);
92     /**
93      * @brief Construct a new Mce Instance Client object
94      * @param  mapStm    the pointer of the instance state machine
95      */
96     MapMceInstanceClient(utility::StateMachine &mapStm, utility::Dispatcher &dispacher,
97         MapMceObserverManager &observeMgr, const MasInstanceConfig &config, std::recursive_mutex &mutex);
98     /**
99      * @brief Destroy the Mce Instance Client object
100      */
101     ~MapMceInstanceClient();
102     /**
103      * @brief Set the Sdp Param
104      * @param  sdpSave     sdp Param
105      */
106     void SetDefaultSdpParam(const MasSdpParam &sdpSave);
107     /**
108      * @brief Set the Default Config object
109      * @param  configSave       save data
110      */
111     void SetDefaultConfig(MasInstanceConfig configSave);
112     /**
113      * @brief Get the Mas Sdp Param
114      * @return MasSdpParam*
115      */
116     MasSdpParam GetMasSdpParam();
117     /**
118      * @brief  start the instance client connecting by obex client
119      * @return int  success or failed
120      */
121     int StartClientConnect();
122     /**
123      * @brief  start the instance client disconnecting
124      * @return int   success or failed
125      */
126     int StartClientDisConnect();
127     /**
128      * @brief  Execute ObexConnect
129      * @return int
130      */
131     int ExcuteObexConnect();
132     /**
133      * @brief  Process DisConnect Finish
134      * @return int   success or failed
135      */
136     void ProcessDisConnectFinish();
137     /**
138      * @brief Get the Current Request object
139      * @return MceRequestType
140      */
141     MceRequestType GetCurrentRequest();
142     /**
143      * @brief  sending request
144      * @param  req
145      * @return int
146      */
147     int ClientSendRequest(std::unique_ptr<MapMceInstanceRequest> &req);
148     /**
149      * @brief  save request
150      * @param  req
151      * @return int
152      */
153     void ClientSaveRequest(std::unique_ptr<MapMceInstanceRequest> &req);
154 
155     /**
156      * @brief  send set patch request
157      * @param  flags            request path type
158      * @param  paths            path string
159      * @param  pathList         path string list
160      * @return int
161      */
162     int ClientSendReqSetPath(const uint8_t flags, const std::u16string &paths, std::vector<std::u16string> &pathList);
163     /**
164      * @brief Send Request Get MasInstanceInformation
165      * @return int
166      */
167     int ClientSendReqGetMasInstanceInformation();
168     /**
169      * @brief  Send Request SetNotificationRegistration
170      * @param  value
171      * @return int
172      */
173     int ClientSendReqSetNotificationRegistration(bool value);
174     /**
175      * @brief  Client Process ResponseCommonProcess
176      * @param  resp
177      * @return int
178      */
179     int ClientProcResponseCommonProcess(const ObexHeader &resp);
180     /**
181      * @brief  Client Count SendingRequest
182      * @param  requestType
183      * @return int
184      */
185     int ClientCountSendingRequest(MceRequestType requestType);
186     /**
187      * @brief  ClientSend SavedRequest
188      * @return int
189      */
190     int ClientSendSavedRequest();
191     /**
192      * @brief Client Delete AllSavedRequest
193      */
194     void ClientDeleteAllSavedRequest();
195     /**
196      * @brief save Mas InstanceInformation
197      * @param  instInfo
198      */
199     void SaveMasInstanceInformation(const MasInstanceInformation &instInfo);
200     /**
201      * @brief Get the Mas Instance Information object
202      * @return MasInstanceInformation
203      */
204     MasInstanceInformation GetMasInstanceInformation();
205     /**
206      * @brief  On GapRequestSecurity callback
207      * @param  result           stack error code
208      */
209     void OnGapRequestSecurityCb(uint16_t result);
210     /**
211      * @brief  Obex Connect Failed DisConnect
212      * @param  result           stack error code
213      */
214     int ObexConnectFailedDisConnect();
215     /**
216      * @brief  Obex Connect Fail Connecting retry
217      * @param  result           stack error code
218      */
219     int ReconnectAfterTransportFailed();
220     /**
221      * @brief Observer set to Obex
222      */
223     class MasObexClientObserver : public ObexClientObserver {
224     public:
225         /**
226          * @brief  copy operator
227          */
MasObexClientObserver(utility::StateMachine & stateMachine)228         explicit MasObexClientObserver(utility::StateMachine &stateMachine) : obexObserverInstStm_(stateMachine) {}
229         /**
230          * @brief Destroy the Mas Obex Client Observer object
231          */
232         ~MasObexClientObserver() override;
233         /**
234          * @brief  OnTransportFailed observer
235          * @param  client
236          * @param  errCd
237          */
238         void OnTransportFailed(ObexClient &client, int errCd) override;
239         /**
240          * @brief  OnConnected observer
241          * @param  client
242          * @param  resp
243          */
244         void OnConnected(ObexClient &client, const ObexHeader &resp) override;
245         /**
246          * @brief  OnConnectFailed observer
247          * @param  client
248          * @param  resp
249          */
250         void OnConnectFailed(ObexClient &client, const ObexHeader &resp) override;
251         /**
252          * @brief  OnDisconnected observer
253          * @param  client
254          */
255         void OnDisconnected(ObexClient &client) override;
256         /**
257          * @brief  OnActionCompleted observer
258          * @param  client
259          * @param  resp
260          */
261         void OnActionCompleted(ObexClient &client, const ObexHeader &resp) override;
262         /**
263          * @brief  data sending continue status
264          * @param  client
265          * @param  isBusy
266          */
267         void OnBusy(ObexClient &client, bool isBusy) override;
268 
269     private:
270         // mce instance stm
271         utility::StateMachine &obexObserverInstStm_;
272     };
273 
274 private:
275     /**
276      * @brief  Check SupportedFeatures
277      * @param  mask
278      * @return true : ready
279      * @return false : not ready
280      */
281     bool CheckSupportedFeatures(int mask) const;
282     /**
283      * @brief ClientDeleteAllSavedRequest Internal
284      */
285     void ClientDeleteAllSavedRequestInternal();
286     /**
287      * @brief Register Service Security
288      * @return int
289      */
290     int RegisterServiceSecurity(GapServiceSecurityInfo &info);
291     /**
292      * @brief Register Service Security
293      * @return int
294      */
295     int RequestSecurity(GapServiceSecurityInfo info);
296     /**
297      * @brief  Execute Callback To Framework api
298      * @param  retAction
299      * @param  resCode
300      */
301     void ExcuteCallbackToFramework(MapRequestResponseAction &retAction, MapExecuteStatus resCode);
302     /**
303      * @brief Set the Obex Config object
304      */
305     void SetObexConfig();
306     /**
307      * @brief Execute Obex Connect Internal
308      * @return int
309      */
310     int ExcuteObexConnectInternal();
311     // mce client uuid table
312     static const uint8_t mceClientUuidTbl_[MAX_OF_MASCLIENT_OBEX_UUID_TBL];
313     // instanceInfo
314     MasInstanceInformation resMasInstanceInfo_ {};
315     // mas instance statemachine pointer
316     utility::StateMachine &instanceStm_;
317     // mas obex observer
318     std::unique_ptr<MasObexClientObserver> masObexObserver_ = nullptr;
319     // send request
320     std::unique_ptr<ObexMpClient> obexClientIns_ = nullptr;
321     // new config
322     ObexClientConfig masClientConfig_ {};
323     // default config
324     MasInstanceConfig insDefaultConfig_ {};
325     // sdp param
326     MasSdpParam masSdpParamSave_ {};
327     // current request ptr
328     std::unique_ptr<MapMceInstanceRequest> currentRequestPtr_ = nullptr;
329     // request que
330     std::list<std::unique_ptr<MapMceInstanceRequest>> masRequestQue_ {};
331     // service dispatch
332     utility::Dispatcher &mceDispacher_;
333     // observer manager
334     MapMceObserverManager &observerMgrPtr_;
335     // sending mutex
336     std::recursive_mutex mceSendRequestMutex_ {};
337     // stm mutex
338     std::recursive_mutex &stmMutex_;
339     // security Info
340     GapServiceSecurityInfo securityInfo_ {};
341 };
342 }  // namespace bluetooth
343 }  // namespace OHOS
344 #endif  // MAP_MCE_INSTANCE_CLIENT_H
345