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 #pragma pack()
57
58 class RouteHeadHandlerImpl : public DistributedData::RouteHeadHandler {
59 public:
60 static std::shared_ptr<RouteHeadHandler> Create(const ExtendInfo &info);
61 explicit RouteHeadHandlerImpl(const ExtendInfo &info);
62 DBStatus GetHeadDataSize(uint32_t &headSize) override;
63 DBStatus FillHeadData(uint8_t *data, uint32_t headSize, uint32_t totalLen) override;
64 bool ParseHeadData(const uint8_t *data, uint32_t len, uint32_t &headSize, std::vector<std::string> &users) override;
65
66 private:
67 void Init();
68 bool PackData(uint8_t *data, uint32_t totalLen);
69 bool PackDataHead(uint8_t *data, uint32_t totalLen);
70 bool PackDataBody(uint8_t *data, uint32_t totalLen);
71 bool UnPackData(const uint8_t *data, uint32_t totalLen, uint32_t &unpackedSize);
72 bool UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead);
73 bool UnPackDataBody(const uint8_t *data, uint32_t totalLen);
74
75 std::string userId_;
76 std::string appId_;
77 std::string storeId_;
78 std::string deviceId_;
79 Session session_;
80 uint32_t headSize_;
81
82 static constexpr int32_t OH_OS_TYPE = 10;
83 };
84 } // namespace OHOS::DistributedData
85 #endif // DISTRIBUTEDDATAMGR_ROUTE_HEAD_HANDLER_H
86