• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 SOFT_BUS_CHANNEL_H
17 #define SOFT_BUS_CHANNEL_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <mutex>
22 #include <memory>
23 #include <map>
24 #include <securec.h>
25 #include <string>
26 #include <zlib.h>
27 
28 #include "accesstoken_log.h"
29 #include "nlohmann/json.hpp"
30 #include "rpc_channel.h"
31 #include "session.h"
32 #include "random.h"
33 
34 namespace OHOS {
35 namespace Security {
36 namespace AccessToken {
37 class SoftBusChannel final : public RpcChannel, public std::enable_shared_from_this<SoftBusChannel> {
38 public:
39     SoftBusChannel(const std::string &deviceId);
40     virtual ~SoftBusChannel();
41 
42     /**
43      * @brief Build connection with peer device.
44      *
45      * @return Result code, 0 indicated build successfully, -1 indicates failure.
46      * @since 1.0
47      * @version 1.0
48      * @see Release
49      */
50     int BuildConnection() override;
51 
52     /**
53      * @brief Execute BaseRemoteCommand at peer device.
54      *
55      * @param commandName The name of Command.
56      * @param jsonPayload The json payload of command.
57      * @return Executed result response string.
58      * @since 1.0
59      * @version 1.0
60      */
61     std::string ExecuteCommand(const std::string &commandName, const std::string &jsonPayload) override;
62 
63     /**
64      * @brief Handle data received. This interface only use for soft bus channel.
65      *
66      * @param session Session with peer device.
67      * @param bytes Data sent from the peer device.
68      * @param length Data length sent from the peer device.
69      * @since 1.0
70      * @version 1.0
71      */
72     void HandleDataReceived(int session, const unsigned char *bytes, int length) override;
73 
74     /**
75      * @brief Close rpc connection when no data is being transmitted. it will run in a delayed task.
76      *
77      * @since 1.0
78      * @version 1.0
79      */
80     void CloseConnection() override;
81 
82     /**
83      * @brief Release resources when the device offline.
84      *
85      * @since 1.0
86      * @version 1.0
87      */
88     void Release() override;
89 
90 private:
91     /**
92      * @brief compress json command to char array command.
93      *
94      * @param type request or response
95      * @param id unique message id
96      * @param commandName command name
97      * @param jsonPayload command notated by json string
98      * @param bytes transfer data array
99      * @param bytesLength transfer data length
100      * @return The execute result, SUCCESS: 0; FAILURE: -1.
101      * @see Compress
102      * @since 1.0
103      * @version 1.0
104      */
105     int PrepareBytes(const std::string &type, const std::string &id, const std::string &commandName,
106         const std::string &jsonPayload, const unsigned char *bytes, int &bytesLength);
107 
108     /**
109      * @brief compress string to char array.
110      *
111      * @param json string to be compressed
112      * @param compressedBytes compressed data array
113      * @param compressedLength compressed data length
114      * @return The execute result, SUCCESS: 0; FAILURE: -1.
115      * @since 1.0
116      * @version 1.0
117      */
118     int Compress(const std::string &json, const unsigned char *compressedBytes, int &compressedLength);
119 
120     /**
121      * @brief decompress char array to string.
122      *
123      * @param bytes compressed data array
124      * @param length compressed data length
125      * @return decompressed string
126      * @since 1.0
127      * @version 1.0
128      */
129     std::string Decompress(const unsigned char *bytes, const int length);
130 
131     /**
132      * @brief transfer request data to soft bus.
133      *
134      * @param bytes data array to transfer
135      * @param bytesLength data length
136      * @return The execute result, SUCCESS: 0; FAILURE: -1.
137      * @since 1.0
138      * @version 1.0
139      */
140     int SendRequestBytes(const unsigned char *bytes, const int bytesLength);
141 
142     /**
143      * @brief transfer response data to soft bus.
144      *
145      * @param session response session id
146      * @param bytes data array to transfer
147      * @param bytesLength data length
148      * @return The execute result, SUCCESS: 0; FAILURE: -1.
149      * @since 1.0
150      * @version 1.0
151      */
152     int SendResponseBytes(int session, const unsigned char *bytes, const int bytesLength);
153 
154     /**
155      * @brief enforce session is available. if session is opened, reopen it.
156      *
157      * @return The execute result, SUCCESS: 0; FAILURE: -1.
158      * @since 1.0
159      * @version 1.0
160      */
161     int CheckSessionMayReopenLocked();
162 
163     /**
164      * @brief check session is available.
165      *
166      * @return The execute result, available: true, otherwise: false.
167      * @since 1.0
168      * @version 1.0
169      */
170     bool IsSessionAvailable();
171 
172     /**
173      * @brief cancel closing connection.
174      *
175      * @since 1.0
176      * @version 1.0
177      */
178     void CancelCloseConnectionIfNeeded();
179 
180     /**
181      * @brief request callback for HandleDataReceived
182      *
183      * @param id unique message id
184      * @param commandName command name
185      * @param jsonPayload command notated by json string
186      * @return decompressed string
187      * @see HandleDataReceived
188      * @since 1.0
189      * @version 1.0
190      */
191     void HandleRequest(
192         int session, const std::string &id, const std::string &commandName, const std::string &jsonPayload);
193 
194     /**
195      * @brief response callback for HandleDataReceived
196      *
197      * @param id unique message id
198      * @param jsonPayload command notated by json string
199      * @return decompressed string
200      * @see HandleDataReceived
201      * @since 1.0
202      * @version 1.0
203      */
204     void HandleResponse(const std::string &id, const std::string &jsonPayload);
205 
206     /**
207      * @brief temp function to generate uuid.
208      *
209      * @param buf uuid string
210      * @param bufSize uuid string size
211      * @since 1.0
212      * @version 1.0
213      */
random_uuid(char buf[37],int bufSize)214     void random_uuid(char buf[37], int bufSize)
215     {
216         const int xbase = 15;
217         const int bbase = 255;
218         const int index6 = 6;
219         const int index8 = 8;
220         const int index3 = 3;
221         const int index5 = 5;
222         const int index7 = 7;
223         const int index9 = 9;
224         const int blen = 2;
225         const int uuidlen = 16;
226         const char *c = "89ab";
227         char *p = buf;
228         int n;
229 
230         for (n = 0; n < uuidlen; ++n) {
231             int b = (int)(GetRandomUint32() % bbase);
232             switch (n) {
233                 case index6:
234                     if (sprintf_s(p, bufSize, "4%x", b % xbase) < 0) {
235                         return;
236                     }
237                     break;
238                 case index8:
239                     if (sprintf_s(p, bufSize, "%c%x", c[GetRandomUint32() % strlen(c)], b % xbase) < 0) {
240                         return;
241                     }
242                     break;
243                 default:
244                     if (sprintf_s(p, bufSize, "%02x", b) < 0) {
245                         return;
246                     }
247                     break;
248             }
249             p += blen;
250             if (n == index3 || n == index5 || n == index7 || n == index9) {
251                 *p++ = '-';
252                 break;
253             }
254         }
255         *p = 0;
256         // prevent array length warning
257         if (p - buf == bufSize) {
258             return;
259         }
260         n = 0;
261     }
262 
263     // bind device id for this channel
264     std::string deviceId_;
265 
266     // channel mutex
267     std::mutex mutex_;
268 
269     // connection closing state. true: in closing, false: otherwise
270     bool isDelayClosing_;
271 
272     // soft bus session mutex
273     std::mutex sessionMutex_;
274 
275     // soft bus session id, -1 for invalid session id.
276     int session_;
277 
278     // soft bus session busy flag, true: busy, false: otherwise
279     bool isSessionUsing_;
280 
281     // communication callbacks map. key: unique message id, value: response callback.
282     std::map<std::string, std::function<void(std::string)>> callbacks_;
283 
284     // callback function arguments: response string variable
285     std::string responseResult_;
286     // callback function execute variable
287     std::condition_variable loadedCond_;
288 };
289 
290 class SoftBusMessage {
291 public:
SoftBusMessage(const std::string & type,const std::string & id,const std::string & commandName,const std::string & jsonPayload)292     SoftBusMessage(
293         const std::string &type, const std::string &id, const std::string &commandName, const std::string &jsonPayload)
294         : type_(type), id_(id), commandName_(commandName), jsonPayload_(jsonPayload)
295     {}
296     ~SoftBusMessage() = default;
297 
IsValid()298     bool IsValid() const
299     {
300         if (this->type_.empty()) {
301             return false;
302         }
303         if (this->id_.empty()) {
304             return false;
305         }
306         if (this->commandName_.empty()) {
307             return false;
308         }
309         return !(this->jsonPayload_.empty());
310     }
311 
312     /**
313      * Convert SoftBusMessage object to corresponding json string.
314      *
315      * @return Soft bus message json string.
316      */
ToJson()317     std::string ToJson() const
318     {
319         nlohmann::json json;
320         json["type"] = this->type_;
321         json["id"] = this->id_;
322         json["commandName"] = this->commandName_;
323         json["jsonPayload"] = this->jsonPayload_;
324         return json.dump();
325     }
326 
GetType()327     const std::string &GetType() const
328     {
329         return type_;
330     }
GetId()331     const std::string &GetId() const
332     {
333         return id_;
334     }
GetCommandName()335     const std::string &GetCommandName() const
336     {
337         return commandName_;
338     }
GetJsonPayload()339     const std::string &GetJsonPayload() const
340     {
341         return jsonPayload_;
342     }
343 
344     static std::shared_ptr<SoftBusMessage> FromJson(const std::string &jsonString);
345 private:
346     std::string type_;
347     std::string id_;
348     std::string commandName_;
349     std::string jsonPayload_;
350 };
351 }  // namespace AccessToken
352 }  // namespace Security
353 }  // namespace OHOS
354 
355 #endif  // SOFT_BUS_CHANNEL_H
356