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 DISTRIBUTEDDATAMGR_ROUTE_HEAD_HANDLER_H
17 #define DISTRIBUTEDDATAMGR_ROUTE_HEAD_HANDLER_H
18 #include "process_communicator_impl.h"
19 #include "route_head_handler.h"
20 #include "serializable/serializable.h"
21 #include "session_manager.h"
22
23 namespace OHOS::DistributedData {
GET_ALIGNED_SIZE(T x,int alignWidth)24 template<typename T> constexpr T GET_ALIGNED_SIZE(T x, int alignWidth)
25 {
26 return (x + (alignWidth - 1)) & ~(alignWidth - 1);
27 }
28
29 #pragma pack(1)
30 // format: head + device pair + user pair + appid
31 struct RouteHead {
32 static constexpr uint16_t MAGIC_NUMBER = 0x8421;
33 static constexpr uint16_t VERSION = 0x1;
34 uint16_t magic = MAGIC_NUMBER;
35 uint16_t version = VERSION;
36 uint64_t checkSum;
37 uint32_t dataLen;
38 };
39
40 struct SessionDevicePair {
41 static constexpr int32_t MAX_DEVICE_ID = 65;
42 char sourceId[MAX_DEVICE_ID];
43 char targetId[MAX_DEVICE_ID];
44 };
45
46 struct SessionUserPair {
47 uint32_t sourceUserId;
48 uint8_t targetUserCount;
49 uint32_t targetUserIds[0];
50 };
51
52 struct SessionAppId {
53 uint32_t len;
54 char appId[0];
55 };
56
57 struct SessionStoreId {
58 uint32_t len;
59 char storeId[0];
60 };
61
62 struct SessionAccountId {
63 uint32_t len;
64 char accountId[0];
65 };
66 #pragma pack()
67
68 class RouteHeadHandlerImpl : public DistributedData::RouteHeadHandler {
69 public:
70 static std::shared_ptr<RouteHeadHandler> Create(const ExtendInfo &info);
71 explicit RouteHeadHandlerImpl(const ExtendInfo &info);
72 DBStatus GetHeadDataSize(uint32_t &headSize) override;
73 DBStatus FillHeadData(uint8_t *data, uint32_t headSize, uint32_t totalLen) override;
74 bool ParseHeadDataLen(const uint8_t *data, uint32_t totalLen, uint32_t &headSize,
75 const std::string &device) override;
76 bool ParseHeadDataUser(const uint8_t *data, uint32_t totalLen, const std::string &label,
77 std::vector<UserInfo> &userInfos) override;
78 std::string GetTargetUserId() override;
79
80 private:
81 void Init();
82 bool PackData(uint8_t *data, uint32_t totalLen);
83 bool PackDataHead(uint8_t *data, uint32_t totalLen);
84 bool PackDataBody(uint8_t *data, uint32_t totalLen);
85 bool PackAppId(uint8_t **data, const uint8_t *end);
86 bool PackStoreId(uint8_t **data, const uint8_t *end);
87 bool PackAccountId(uint8_t **data, const uint8_t *end);
88 bool UnPackData(const uint8_t *data, uint32_t totalLen, uint32_t &unpackedSize);
89 bool UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead);
90 bool UnPackDataBody(const uint8_t *data, uint32_t totalLen);
91 bool UnPackAppId(uint8_t **data, uint32_t leftSize);
92 bool UnPackStoreId(uint8_t **data, uint32_t leftSize);
93 bool UnPackAccountId(uint8_t **data, uint32_t leftSize);
94 std::string ParseStoreId(const std::string &deviceId, const std::string &label);
95 std::string userId_;
96 std::string appId_;
97 std::string storeId_;
98 std::string deviceId_;
99 Session session_;
100 uint32_t headSize_;
101
102 static constexpr int32_t OH_OS_TYPE = 10;
103 };
104 } // namespace OHOS::DistributedData
105 #endif // DISTRIBUTEDDATAMGR_ROUTE_HEAD_HANDLER_H
106