• 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 DEVICESTATUS_MSDP_RDB_H
17 #define DEVICESTATUS_MSDP_RDB_H
18 
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <thread>
23 #include <mutex>
24 #include <map>
25 #include <errors.h>
26 
27 #include "rdb_store.h"
28 #include "rdb_helper.h"
29 #include "rdb_open_callback.h"
30 #include "rdb_store_config.h"
31 #include "values_bucket.h"
32 #include "result_set.h"
33 #include "devicestatus_data_utils.h"
34 #include "devicestatus_msdp_interface.h"
35 
36 namespace OHOS {
37 namespace Msdp {
38 class DevicestatusMsdpRdb : public DevicestatusMsdpInterface {
39 public:
40     enum EventType {
41         EVENT_UEVENT_FD,
42         EVENT_TIMER_FD,
43     };
44 
DevicestatusMsdpRdb()45     DevicestatusMsdpRdb() {}
~DevicestatusMsdpRdb()46     virtual ~DevicestatusMsdpRdb() {}
47     bool Init();
48     void InitRdbStore();
49     void SetTimerInterval(int32_t interval);
50     void CloseTimer();
51     void InitTimer();
52     void TimerCallback();
53     int32_t RegisterTimerCallback(const int32_t fd, const EventType et);
54     void StartThread();
55     void LoopingThreadEntry();
56     void Enable() override;
57     void Disable() override;
58     void RegisterCallback(const std::shared_ptr<MsdpAlgorithmCallback>& callback) override;
59     void UnregisterCallback() override;
60     ErrCode NotifyMsdpImpl(const DevicestatusDataUtils::DevicestatusData& data);
61     int32_t TrigerData(const std::unique_ptr<NativeRdb::ResultSet> &resultSet);
62     int32_t TrigerDatabaseObserver();
63     DevicestatusDataUtils::DevicestatusData SaveRdbData(const DevicestatusDataUtils::DevicestatusData& data);
GetCallbacksImpl()64     std::shared_ptr<MsdpAlgorithmCallback> GetCallbacksImpl()
65     {
66         std::unique_lock lock(mutex_);
67         return callbacksImpl_;
68     }
69 
70 private:
71     using Callback = std::function<void(DevicestatusMsdpRdb*)>;
72     std::shared_ptr<MsdpAlgorithmCallback> callbacksImpl_;
73     std::map<int32_t, Callback> callbacks_;
74     std::shared_ptr<NativeRdb::RdbStore> store_;
75     int32_t devicestatusType_ = -1;
76     int32_t devicestatusStatus_ = -1;
77     bool notifyFlag_ = false;
78     int32_t timerInterval_ = -1;
79     int32_t timerFd_ = -1;
80     int32_t epFd_ = -1;
81     std::map<DevicestatusDataUtils::DevicestatusType, DevicestatusDataUtils::DevicestatusValue> rdbDataMap_;
82     std::mutex mutex_;
83 };
84 
85 class InsertOpenCallback : public NativeRdb::RdbOpenCallback {
86 public:
87     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
88     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
89 };
90 }
91 }
92 #endif // DEVICESTATUS_MSDP_RDB_H
93