• 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 MAP_MSE_SERVER_H
17 #define MAP_MSE_SERVER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include "../obex/obex_mp_server.h"
22 #include "../obex/obex_utils.h"
23 #include "dispatcher.h"
24 #include "log.h"
25 #include "map_mse_bmsg.h"
26 #include "map_mse_params.h"
27 #include "map_mse_resource.h"
28 #include "map_mse_types.h"
29 #include "raw_address.h"
30 
31 /**
32  * @brief bluetooth namespace
33  *
34  */
35 namespace OHOS {
36 namespace bluetooth {
37 /**
38  * @brief Callback function of MapMseServer
39  *
40  */
41 class MseObserver {
42 public:
43     /**
44      * @brief A destructor used to delete the Mse Observer instance.
45      *
46      */
47     virtual ~MseObserver() = default;
48 
49     /**
50      * @brief The function is called when new MCE connection available
51      *
52      * @param session Reference to the ObexServerSession Value
53      */
54     virtual void OnConnected(ObexServerSession &session) = 0;
55 
56     /**
57      * @brief The function is called when MCE connect is incoming
58      *
59      * @param incomingConnect Reference to the ObexIncomingConnect Value
60      */
61     virtual void OnIncomingConnect(ObexIncomingConnect &incomingConnect) = 0;
62 
63     /**
64      * @brief The function is called when MCE Transport Disconnect is incoming
65      *
66      * @param remoteAddr Remote Bluetooth address
67      */
68     virtual void OnTransportDisconnected(const RawAddress &remoteAddr) = 0;
69 
70     /**
71      * @brief The function is called when MCE Disconnect is incoming
72      *
73      * @param remoteAddr Remote Bluetooth address
74      */
75     virtual void OnDisconnect(const RawAddress &remoteAddr) = 0;
76 
77     /**
78      * @brief The function is called when Obex busy or not busy
79      *
80      * @param remoteAddr Remote Bluetooth address
81      * @param isBusy true:busy false:not busy
82      */
83     virtual void OnBusy(const RawAddress &remoteAddr, bool isBusy) = 0;
84 };
85 
86 /**
87  * @brief MSE server class
88  * used to start OBEX service and respond to MCE requests
89  *
90  */
91 class MapMseServer {
92 public:
93     /**
94      * @brief Construct a new Mse Server:: Mse Server object
95      *
96      * @param observer Callback function
97      */
98     explicit MapMseServer(MapMseResource &content, utility::Dispatcher &dispatcher, MseObserver &observer,
99         MapMseInstance &instance, uint8_t rfcommNo, uint16_t l2capPsm, std::string serviceName,
100         const MapAccountItem &accountItem, bool smsSupport);
101 
102     /**
103      * @brief Destroy the Mse Server:: Mse Server object
104      *
105      */
106     virtual ~MapMseServer();
107 
108     /**
109      * @brief The function start MSE stack in asynchronization mode
110      *
111      * @return int
112      */
113     int Enable(void);
114 
115     /**
116      * @brief The function shutdown MSE stack
117      *
118      */
119     void Disable(void);
120 
121     void SetRemoteFeatureMask(uint32_t features);
122 
123 private:
124     static const uint8_t TARGET_SIZE = 16;
125     static const uint8_t MAS_TARGET[TARGET_SIZE];
126     static const std::string HEADER_TYPE_GET_FOLDER_LISTING;
127     static const std::string HEADER_TYPE_GET_MESSAGE_LISTING;
128     static const std::string HEADER_TYPE_GET_CONVO_LISTING;
129     static const std::string HEADER_TYPE_MESSAGE;
130     static const std::string HEADER_TYPE_SET_MESSAGE_STATUS;
131     static const std::string HEADER_TYPE_SET_NOTIFICATION_REGISTRATION;
132     static const std::string HEADER_TYPE_MESSAGE_UPDATE;
133     static const std::string HEADER_TYPE_GET_MAS_INSTANCE_INFORMATION;
134     static const std::string HEADER_TYPE_OWNER_STATUS;
135     static const std::string HEADER_TYPE_SET_NOTIFICATION_FILTER;
136     static const int MAP_MAS_INSTANCE_INFORMATION_LENGTH = 200;
137     static const int READ_BODY_BUFF_SIZE = 100;
138     static const int DATABASE_VERSION_COUNTER_LENGTH = 128;
139     class MseObexServer : public ObexServerObserver {
140     public:
141         /**
142          * @brief Construct a new Mse Obex Server object
143          *
144          * @param mseServer Reference to the Mse Server
145          */
146         explicit MseObexServer(MapMseServer &mseServer);
147 
148         /**
149          * @brief Destroy the Mse Obex Server object
150          *
151          */
152         ~MseObexServer() override;
153 
154     private:
155         /**
156          * @brief Callback implementation for the OBex OnTransportConnect function
157          *
158          * @param incomingConnect Reference to the ObexIncomingConnect
159          */
160         void OnTransportConnect(ObexIncomingConnect &incomingConnect) override;
161 
162         void OnTransportDisconnected(const std::string &btAddr) override;
163 
164         void OnTransportError(const std::string &btAddr, int errCd, const std::string &msg) override;
165 
166         void OnError(const int errCd, const std::string &msg) override;
167 
168         /**
169          * @brief Response to MAS connection function and OBEX connection callback function
170          *
171          * @param session Obex session
172          * @param req Obex connection request
173          */
174         void OnConnect(ObexServerSession &session, const ObexHeader &req) override;
175 
176         void OnDisconnect(ObexServerSession &session, const ObexHeader &req) override;
177 
178         void OnPut(ObexServerSession &session, const ObexHeader &req) override;
179 
180         /**
181          * @brief Callback implementation for the OBex onGet function
182          *
183          * @param session obex session
184          * @param req obex headers request
185          */
186         void OnGet(ObexServerSession &session, const ObexHeader &req) override;
187 
188         void OnAbort(ObexServerSession &session, const ObexHeader &req) override;
189 
190         void OnSetPath(ObexServerSession &session, const ObexHeader &req) override;
191 
192         void OnBusy(ObexServerSession &session, bool isBusy) const override;
193 
194         MapMseServer *mseServer_;
195         BT_DISALLOW_COPY_AND_ASSIGN(MseObexServer);
196     };
197 
198     std::unique_ptr<MseObexServer> mseObexServer_ = nullptr;
199     /**
200      * @brief Response processing of messages
201      *
202      * @param session obex server session
203      * @param req obex headers request
204      */
205     void GetMessageResponse(ObexServerSession &session, const ObexHeader &req);
206     void GetMessageListing(ObexServerSession &session, const ObexHeader &req);
207     void CreateOutputAppPrarams(ObexTlvParamters &obexAppPrarams, const uint16_t &listSize, const uint8_t &unRead);
208     void GetConversationListing(ObexServerSession &session, const ObexHeader &req);
209     void GetFolderListing(ObexServerSession &session, const ObexHeader &req);
210     void GetOwnerStatus(const ObexServerSession &session, const ObexHeader &req);
211     void GetMASInstanceInformation(ObexServerSession &session, const ObexHeader &req);
212     void GetImEmailMasInfo(std::string &masStr, std::string &ownerUci);
213     void PushMessageResponse(ObexServerSession &session, const ObexHeader &req);
214     int CreateBMessage(ObexServerSession &session, const ObexHeader &req, const uint8_t charset,
215         std::string &folderName);
216     void SetNotificationRegistration(const ObexServerSession &session, const ObexHeader &req);
217     void SetNotificationFilter(const ObexServerSession &session, const ObexHeader &req);
218     void SetMessageStatus(const ObexServerSession &session, const ObexHeader &req);
219     void UpdateInbox(const ObexServerSession &session, const ObexHeader &req);
220     int SetStatusParam(const MapMseParams &appParams, stub::OwnerStatusParam &statusParam);
221     void SetOwnerStatus(const ObexServerSession &session, const ObexHeader &req);
222     void CreateFolder(void);
223     static void InitDefaultFolder(MapMseFolder &root);
224     static void InitSmsMmsFolder(MapMseFolder &root);
225     static void InitImFolder(MapMseFolder &root);
226     void InitEmailFolder(MapMseFolder &parentFolder, int depth = 0);
227     static std::unordered_map<int, std::string> GetEmailInfo(
228         std::unique_ptr<DataAccess> &dataAccess, MapMseFolder &parent);
229     static std::string ToUtf8String(std::u16string u16str);
230     static std::u16string ToU16String(std::string str);
231     static std::string GetMseTime(void);
232     static bool CheckConversationId(std::string id);
233     void LoadAdapterConfig();
234 
235     MapMseFolder *currentFolder_ = nullptr;
236 
237     std::unique_ptr<MapMseFolder> rootFolder_ = nullptr;
238 
239     MapMseResource &content_;
240 
241     utility::Dispatcher &dispatcher_;
242 
243     MseObserver &observer_;
244 
245     MapMseInstance &instance_;
246 
247     // SDP Register FRCOMM No
248     uint8_t rfcommNo_ = 0;
249 
250     // SDP Register GOEP L2cap Psm
251     uint16_t goepL2capPsm_ = 0;
252 
253     // SDP Register Service Name
254     std::string serviceName_ = "";
255 
256     MapAccountItem accountItem_ {};
257 
258     bool smsSupport_ = false;
259 
260     std::unique_ptr<ObexServer> obexServer_ = nullptr;
261 
262     uint32_t supportedFeatureMask_ = 0;
263 
264     std::string messageVersion_ = "";
265 
266     std::string messageListingVersion_ = "";
267 
268     static MessageType ParseHandle(uint64_t handle);
269 
270     std::unique_ptr<MapMseBmsg> bMessage_ = nullptr;
271 
272     std::atomic_uint32_t connectId_ = 0;
273 
274     bool srmEnable_ = true;
275 
276     int rfcommMtu_ = 0;
277 
278     int l2capMtu_ = 0;
279 
280     BT_DISALLOW_COPY_AND_ASSIGN(MapMseServer);
281 };
282 }  // namespace bluetooth
283 }  // namespace OHOS
284 
285 #endif  // MAP_MSE_SERVER_H
286