1 /*
2 * Copyright (c) 2023 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 OHOS_FILEMGMT_MESSAGE_HANDLER_H
17 #define OHOS_FILEMGMT_MESSAGE_HANDLER_H
18
19 #include <string>
20
21 namespace OHOS::FileManagement::CloudSync {
22 constexpr int32_t NETWORK_ID_SIZE_MAX = 65;
23
24 template<typename T>
GET_ALIGNED_SIZE(T x,int alignWidth)25 constexpr T GET_ALIGNED_SIZE(T x, int alignWidth)
26 {
27 return (x + (alignWidth - 1)) & ~(alignWidth - 1);
28 }
29
30 #pragma pack(1)
31 struct MessageHeader {
32 uint8_t magic;
33 uint8_t version;
34 uint16_t msgType;
35 uint32_t dataLen;
36 int32_t errorCode;
37 };
38
39 struct SessionDeviceInfo {
40 char sourceNetworkId[NETWORK_ID_SIZE_MAX];
41 char tartgeNetworkId[NETWORK_ID_SIZE_MAX];
42 };
43
44 struct UserData {
45 uint64_t taskId;
46 uint32_t userId;
47 uint32_t uriLen;
48 char uri[0];
49 };
50 #pragma pack()
51
52 enum MsgType : uint16_t {
53 MSG_DOWNLOAD_FILE_REQ = 0,
54 MSG_DOWNLOAD_FILE_RSP,
55 MSG_FINISH_FILE_RECV,
56 };
57
58 struct MessageInputInfo {
59 std::string srcNetworkId;
60 std::string dstNetworkId;
61 std::string uri;
62 uint16_t msgType;
63 int32_t errorCode;
64 int32_t userId;
65 uint64_t taskId;
66 };
67
68 class MessageHandler {
69 public:
70 MessageHandler() = default;
71 MessageHandler(MessageInputInfo &info);
72
73 bool PackData(uint8_t *data, uint32_t totalLen);
74 bool UnPackData(uint8_t *data, uint32_t totalLen);
75 void GetMessageHeader(MessageHeader &header);
76 uint32_t GetDataSize();
77 uint64_t GetTaskId();
78 uint16_t GetMsgType();
79 int32_t GetUserId();
80 int32_t GetErrorCode();
81
82 std::string GetUri();
83 std::string GetSrcNetworkId();
84 std::string GetDstNetworkId();
85
86 private:
87 MessageHeader msgHdr_{0};
88 std::string srcNetworkId_;
89 std::string dstNetworkId_;
90 std::string uri_;
91 int32_t userId_{0};
92 uint64_t taskId_{0};
93 uint32_t dataSize_{0};
94 };
95 } // namespace OHOS::FileManagement::CloudSync
96
97 #endif // OHOS_FILEMGMT_MESSAGE_HANDLER_H