• 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 #ifndef PCE_SERVICE_H
17 #define PCE_SERVICE_H
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <memory>
22 #include "../obex/obex_types.h"
23 #include "base_observer_list.h"
24 #include "btstack.h"
25 #include "context.h"
26 #include "interface_profile.h"
27 #include "interface_profile_pbap_pce.h"
28 #include "message.h"
29 #include "pbap_pce_def.h"
30 #include "pbap_pce_parameter.h"
31 #include "pbap_pce_sdp.h"
32 #include "state_machine.h"
33 
34 namespace OHOS {
35 namespace bluetooth {
36 /**
37  * @brief pbap pce service main state
38  */
39 enum PbapPceServiceStateType {
40     PBAP_PCE_STATE_STARTUP,
41     PBAP_PCE_STATE_STARTUPING,
42     PBAP_PCE_STATE_SHUTDOWN,
43     PBAP_PCE_STATE_SHUTDOWNING,
44 };
45 /**
46  * @brief pbap pce targe state
47  */
48 enum PbapPceTargetStateType {
49     PCE_TARGET_STATE_UNKNOWN,
50     PCE_TARGET_STATE_CONNECTED,
51     PCE_TARGET_STATE_DISCONNECTED,
52 };
53 /**
54  * @brief pbap pce config
55  */
56 struct PbapPceConfig {
57     bool srmEnable_ = true;
58     int rfcommMtu_ = OBEX_DEFAULT_MTU;
59     int l2capMtu_ = OBEX_DEFAULT_MTU;
60     int pceMaxDevices_ = PCE_MAXIMUM_DEVICES;
61 };
62 /**
63  * @brief phone book client service
64  * process connect event
65  */
66 class PbapPceService : public IProfilePbapPce, public utility::Context {
67 public:
68     /**
69      * @brief register observer
70      * @details register observer for the service of phone book client
71      * @param observer the reference to a PbapClientObserver
72      * @return void
73      */
74     void RegisterObserver(IPbapPceObserver &observer) override;
75 
76     /**
77      * @brief deregister observer
78      * @details deregister observer for the service of phone book client
79      * @param observer reference to a PbapClientObserver
80      * @return void
81      */
82     void DeregisterObserver(IPbapPceObserver &observer) override;
83 
84     /**
85      * @brief get the remote devices
86      * @details get the remote device with the specified states
87      * @param states states
88      * @return std::vector remote devices
89      */
90     std::vector<RawAddress> GetDevicesByStates(const std::vector<int> &states) override;
91 
92     /**
93      * @brief get the state of device
94      * @details get the state with the specified remote device
95      * @param device  remote device
96      * @return int @c not -1 state of the specified remote device
97      *             @c -1 device is not exist
98      */
99     int GetDeviceState(const RawAddress &device) override;
100 
101     /**
102      * @brief set the strategy of device
103      * @details set the strategy with the specified remote device
104      * @param device  remote device
105      * @param strategy  specified strategy
106      * @return int @c 0  success
107      *             @c other failure
108      */
109     int SetConnectionStrategy(const RawAddress &device, int strategy) override;
110 
111     /**
112      * @brief get the strategy of device
113      * @details get the strategy with the specified remote device
114      * @param device  remote device
115      * @return int @c  0 success
116      *             @c -1 failure
117      */
118     int GetConnectionStrategy(const RawAddress &device) override;
119 
120     /**
121      * @brief Set device's password. please call after OnServicePasswordRequired event.
122      *
123      * @param device device
124      * @param password device's password
125      * @param userId device's userId
126      */
127     int SetDevicePassword(
128         const RawAddress &device, const std::string &password, const std::string &userId = "") override;
129 
130 public:
131     /**
132      * @brief Pull phone book from remote device after connected.
133      * @param param app parameter
134      * @return int @c 0 ok
135      *             @c -1 fail
136      */
137     int PullPhoneBook(const RawAddress &device, const IPbapPullPhoneBookParam &param) override;
138 
139     /**
140      * @brief Pull phone book from remote device after connected.
141      * @param name phone book path
142      * @param flag 0 back to root; 2 go down; 3 go up
143      * @return int @c 0 ok
144      *             @c -1 fail
145      */
146     int SetPhoneBook(const RawAddress &device, const std::u16string &name, int flag) override;
147 
148     /**
149      * @brief Pull vCard listing from remote device after connected.
150      * @param param app parameter
151      * @return int @c 0 ok
152      *             @c -1 fail
153      */
154     int PullvCardListing(const RawAddress &device, const IPbapPullvCardListingParam &param) override;
155 
156     /**
157      * @brief Pull vCard entry from remote device after connected.
158      * @param param app parameter
159      * @return int @c 0 ok
160      *             @c -1 fail
161      */
162     int PullvCardEntry(const RawAddress &device, const IPbapPullvCardEntryParam &param) override;
163 
164     /**
165      * @brief IsDownloading
166      * @param device remote address
167      * @return int @c 0 ok
168      *             @c -1 fail
169      */
170     bool IsDownloading(const RawAddress &device) override;
171 
172     /**
173      * @brief AbortDownloading.
174      * @param device remote address
175      * @return int @c 0 ok
176      *             @c -1 fail
177      */
178     int AbortDownloading(const RawAddress &device) override;
179 
180     /**
181      * @brief constructor
182      * @details constructor
183      */
184     explicit PbapPceService();
185 
186     /**
187      * @brief deconstructor
188      * @details deconstructor
189      */
190     ~PbapPceService();
191 
192     /**
193      * @brief get context
194      * @details get context
195      * @return Context*
196      */
197     utility::Context *GetContext() override;
198 
199     /**
200      * @brief start client
201      * @details start phone book client
202      */
203     void Enable() override;
204 
205     /**
206      * @brief shutdown client
207      * @details shutdown phone book client
208      */
209     void Disable() override;
210 
211     /**
212      * @brief connect to server
213      * @details connect to phone book server
214      * @param device  remote device
215      * @return int  @c  0 success
216      *              @c -1 failure
217      */
218     int Connect(const RawAddress &device) override;
219 
220     /**
221      * @brief disconnect device
222      * @details disconnect from remote device
223      * @param device  remote device
224      * @return int @c  0 success
225      *              @c -1 failure
226      */
227     int Disconnect(const RawAddress &device) override;
228 
229     /**
230      * @brief get the remote devices
231      * @details get devices
232      * @return std::list remote devices
233      */
234     std::list<RawAddress> GetConnectDevices() override;
235 
236     /**
237      * @brief get connection state
238      * @details get connection state
239      * @return int connection state, multiple states by bit operation OR
240      */
241     int GetConnectState() override;
242 
243     /**
244      * @brief get max number of connections
245      * @details get max number of connections
246      * @return int max number
247      */
248     int GetMaxConnectNum() override;
249 
250     /**
251      * @brief ForwardMsgToStm
252      * @details Forward Msg To StateMachine
253      * @param device RawAddress
254      * @param msg message
255      * @return void
256      */
257     void ForwardMsgToStm(const RawAddress &device, const utility::Message &msg);
258 
259     /**
260      * @brief post message
261      * @details post message to self thread
262      * @param device RawAddress
263      * @param msg message from other threads
264      * @return void
265      */
266     void PostMessage(const RawAddress &device, const utility::Message &msg);
267 
268     /**
269      * @brief Process Obex Response message
270      * @details Process Obex Response message
271      * @param device RawAddress
272      * @param msg message from other threads
273      * @return void
274      */
275     void ProcessObexRespMessage(const RawAddress &device, const utility::Message &msg);
276 
277     /**
278      * @brief get the connected devices
279      * @details get the remote device with the connected states
280      * @return std::vector connected remote devices
281      */
282     std::vector<RawAddress> GetConnectedDevices();
283 
284     /**
285      * @brief GetDownloadFileName
286      * @details Get Download File Name
287      * @param device remote address
288      * @return string download file name
289      */
290     std::string GetDownloadFileName(const RawAddress &device);
291 
292     /**
293      * @brief GetPceConfig
294      * @details Get PceConfig
295      * @return PbapPceConfig
296      */
297     const PbapPceConfig &GetPceConfig() const;
298 
299 private:
300     /**
301      * @brief sdp gap failed process
302      * @details when sdp gap search failed, call it
303      * @param device remote address
304      * @param msg message from other threads
305      * @return void
306      */
307     void ProcessSdpGapFailed(const RawAddress &device, const utility::Message &msg);
308 
309     /**
310      * @brief sdp gap finish process
311      * @details when sdp gap search success, call it
312      * @param device remote address
313      * @param sdpMsg message from other threads
314      * @return void
315      */
316     void ProcessSdpGapFinish(const RawAddress &device, const utility::Message &msg);
317 
318     /**
319      * @brief  unauthorized process
320      * @details when obex return unauthorized, call it
321      * @param device remote address
322      * @param msg message from other threads
323      * @return void
324      */
325     void ProcessConnectFailed(const RawAddress &device, const utility::Message &msg);
326 
327     /**
328      * @brief connected process
329      * @details when connected obex server, call it
330      * @param device remote address
331      * @param msg message from other threads
332      * @return void
333      */
334     void ProcessConnected(const RawAddress &device, const utility::Message &msg);
335 
336     /**
337      * @brief disconnected process
338      * @details when disconnected obex server, call it
339      * @param device remote address
340      * @param msg message from other threads
341      * @return void
342      */
343     void ProcessDisconnected(const RawAddress &device, const utility::Message &msg);
344 
345     /**
346      * @brief transported failed process
347      * @details when transported failed obex server, call it
348      * @param device remote address
349      * @param msg message from other threads
350      * @return void
351      */
352     void ProcessTransportFailed(const RawAddress &device, const utility::Message &msg);
353 
354     /**
355      * @brief user input process
356      * @details when user input password, call it
357      * @param device remote address
358      * @param msg message from other threads
359      * @return void
360      */
361     void ProcessUserInput(const RawAddress &device, const utility::Message &msg);
362 
363     /**
364      * @brief pull phone book process
365      * @details when call pull phone book, call it
366      * @param device remote address
367      * @param msg message from other threads
368      * @return void
369      */
370     void ProcessPhoneBookActionCompleted(const RawAddress &device, const utility::Message &msg);
371 
372     /**
373      * @brief enable service
374      * @details enable service
375      * @return void
376      */
377     void EnableService();
378 
379     /**
380      * @brief disable service
381      * @details disable service
382      * @return void
383      */
384     void DisableService();
385 
386     /**
387      * @brief connect to server
388      * @details connect to phone book server
389      * @param device  remote device
390      * @return int @c  0 success
391      *             @c other failure
392      */
393     int ConnectInternal(const RawAddress &device);
394 
395     /**
396      * @brief PullPhoneBook Internal
397      * @details PullPhoneBook Internal
398      */
399     int PullPhoneBookInternal(const RawAddress &device, const IPbapPullPhoneBookParam &param);
400 
401     /**
402      * @brief SetPhoneBook Internal
403      * @details SetPhoneBook Internal
404      */
405     int SetPhoneBookInternal(const RawAddress &device, const std::u16string &name, int flag);
406 
407     /**
408      * @brief PullvCardListing Internal
409      * @details PullvCardListing Internal
410      */
411     int PullvCardListingInternal(const RawAddress &device, const IPbapPullvCardListingParam &param);
412 
413     /**
414      * @brief PullvCardEntry Internal
415      * @details PullvCardEntry Internal
416      */
417     int PullvCardEntryInternal(const RawAddress &device, const IPbapPullvCardEntryParam &param);
418 
419     /**
420      * @brief Abort Downloading Internal
421      * @details Abort Downloading Internal
422      */
423     int AbortDownloadingInternal(const RawAddress &device);
424 
425     /**
426      * @brief disconnect to server
427      * @details disconnect to phone book server
428      * @param device  remote device
429      * @return int  @c  0 success
430      *              @c other failure
431      */
432     int DisconnectInternal(const RawAddress &device);
433 
434     /**
435      * @brief disconnect all server
436      * @details disconnect all server
437      * @return int  @c  0 success
438      *              @c -1 failure
439      */
440     int DisconnectAllDevices();
441 
442     /**
443      * @brief is connected
444      * @details check connected
445      * @param device  remote device
446      * @return bool @c  true connected
447      *              @c false not connected
448      */
449     bool IsConnected(const RawAddress &device);
450 
451 private:
452     /**
453      * @brief Load PceConfig
454      * @details Load PceConfig
455      * @return
456      */
457     void LoadPceConfig();
458 
459     /**
460      * @brief Try to ShutDown
461      * @details Try to ShutDown
462      * @return bool @c true success
463      *              @c false failure
464      */
465     bool TryShutDown(bool ret);
466 
467     /**
468      * @brief Is busy
469      * @details Is busy, check if executing action
470      * @param device  remote device
471      * @return bool @c true busy
472      *              @c false not busy
473      */
474     bool IsBusy(const RawAddress &device);
475 
476     /**
477      * @brief set busy
478      * @details set busy, when executing action
479      * @param device  remote device
480      * @param busy  set busy
481      * @return bool @c true success
482      *              @c false failure
483      */
484     bool SetBusy(const RawAddress &device, bool busy);
485 
486     /**
487      * @brief SetPowerStatusBusy
488      * @details SetPowerStatusBusy, when executing action
489      * @param device  remote device
490      * @param busy  set busy
491      */
492     void SetPowerStatusBusy(const RawAddress &device, bool busy);
493 
494     /**
495      * @brief CheckAndSetDeviceBusy
496      * @details CheckAndSetDeviceBusy
497      * @param device  remote device
498      * @return bool @c true busy
499      *              @c false not busy
500      */
501     bool CheckAndSetDeviceBusy(const RawAddress &device);
502 
503     /**
504      * @brief CheckDeviceStatusForPbapAction
505      * @details CheckDeviceStatusForPbapAction
506      * @param device  remote device
507      * @return int  @c  0 success
508      *              @c other failure
509      */
510     int CheckDeviceStatusForPbapAction(const RawAddress &device);
511 
512     /**
513      * @brief save pbap connection policy
514      * @details save pbap connection policy
515      * @param device  remote device
516      * @param strategy connection policy,
517      *     @c UNKNOWN : the connection policy for unkown state.
518      *     @c ALLOWED : the connection policy for allowed state.
519      *     @c FORBIDDEN : the connection policy for forbidden state.
520      * @return bool  @c  true success
521      *               @c false failure
522      */
523     bool SaveConnectPolicy(const std::string &addr, int strategy);
524 
525     /**
526      * @brief get pbap connection policy
527      * @details get pbap connection policy
528      * @param addr  addr
529      * @param strategy Reference to the connection policy, return strategy,
530      *     @c UNKNOWN : the connection policy for unkown state.
531      *     @c ALLOWED : the connection policy for allowed state.
532      *     @c FORBIDDEN : the connection policy for forbidden state.
533      * @return bool  @c  true success
534      *               @c false failure
535      */
536     bool LoadConnectPolicy(const std::string &addr, int &strategy);
537 
538     // statemachine for remote device
539     std::map<std::string, std::unique_ptr<utility::StateMachine>> machineMap_ {};
540 
541     // pce observer
542     BaseObserverList<IPbapPceObserver> observerMgrList_ {};
543 
544     // Pbap Pce Service State
545     PbapPceServiceStateType serviceState_ = PBAP_PCE_STATE_SHUTDOWN;
546 
547     // sdp service
548     std::unique_ptr<PbapPceSdp> pbapPceSdp_ {nullptr};
549 
550     // machine map mutex
551     std::recursive_mutex machineMapMutex_ {};
552 
553     // Pbap Pce Service config
554     PbapPceConfig pbapPceConfig_ {};
555 
556     BT_DISALLOW_COPY_AND_ASSIGN(PbapPceService);
557 };
558 }  // namespace bluetooth
559 }  // namespace OHOS
560 
561 #endif  // PCE_SERVICE_H
562