• 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 #ifndef OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
16 #define OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
17 #include <map>
18 #include <mutex>
19 #include "eventcenter/event.h"
20 #include "lru_bucket.h"
21 #include "metadata/matrix_meta_data.h"
22 #include "metadata/store_meta_data.h"
23 #include "visibility.h"
24 namespace OHOS::DistributedData {
25 class API_EXPORT DeviceMatrix {
26 public:
27     static constexpr uint16_t META_STORE_MASK = 0x0001;
28     enum : int32_t {
29         MATRIX_ONLINE = Event::EVT_CUSTOM,
30         MATRIX_META_FINISHED,
31         MATRIX_BROADCAST,
32         MATRIX_BUTT
33     };
34     static DeviceMatrix &GetInstance();
35     bool Initialize(uint32_t token, std::string storeId);
36     void Online(const std::string &device);
37     void Offline(const std::string &device);
38     void OnBroadcast(const std::string &device, uint16_t code);
39     void OnChanged(uint16_t code);
40     void OnExchanged(const std::string &device, uint16_t code, bool isRemote = false);
41     uint16_t GetCode(const StoreMetaData &metaData);
42 
43 private:
44     static constexpr uint32_t CURRENT_VERSION = 1;
45     static constexpr uint32_t CURRENT_MASK = 0x0002;
46     static constexpr size_t MAX_DEVICES = 16;
47 
48     DeviceMatrix();
49     ~DeviceMatrix();
50     DeviceMatrix(const DeviceMatrix &) = delete;
51     DeviceMatrix(DeviceMatrix &&) noexcept = delete;
52     DeviceMatrix &operator=(const DeviceMatrix &) = delete;
53     DeviceMatrix &operator=(DeviceMatrix &&) noexcept = delete;
SetMask(size_t index)54     static inline uint16_t SetMask(size_t index)
55     {
56         return 0x1 << (index + 1);
57     }
58 
59     uint16_t ConvertMask(const std::string &device, uint16_t code);
60     MatrixMetaData GetMatrixMeta(const std::string &device);
61 
62     struct Mask {
63         uint16_t bitset = META_STORE_MASK | CURRENT_MASK;
64     };
65 
66     uint32_t tokenId_ = 0;
67     std::string storeId_;
68     std::mutex mutex_;
69     std::map<std::string, Mask> onLines_;
70     std::map<std::string, Mask> offLines_;
71     std::map<std::string, Mask> remotes_;
72     std::vector<std::string> maskApps_ = { "distributed_device_profile_service" };
73     LRUBucket<std::string, MatrixMetaData> versions_{ MAX_DEVICES };
74 };
75 } // namespace OHOS::DistributedData
76 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
77