• 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_SENSOR_RDB_H
17 #define DEVICESTATUS_SENSOR_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_sensor_interface.h"
35 
36 namespace OHOS {
37 namespace Msdp {
38 class DevicestatusSensorRdb : public DevicestatusSensorInterface {
39 public:
40     enum EventType {
41         EVENT_UEVENT_FD,
42         EVENT_TIMER_FD,
43     };
44 
DevicestatusSensorRdb()45     DevicestatusSensorRdb() {}
~DevicestatusSensorRdb()46     virtual ~DevicestatusSensorRdb() {}
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<DevicestatusSensorHdiCallback>& 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<DevicestatusSensorHdiCallback> GetCallbacksImpl()
65     {
66         std::unique_lock lock(mutex_);
67         return callbacksImpl_;
68     }
69     void SubscribeHallSensor();
70     void UnSubscribeHallSensor();
71 
72 private:
73     using Callback = std::function<void(DevicestatusSensorRdb*)>;
74     std::shared_ptr<DevicestatusSensorHdiCallback> callbacksImpl_;
75     std::map<int32_t, Callback> callbacks_;
76     std::shared_ptr<NativeRdb::RdbStore> store_;
77     int32_t devicestatusType_ = -1;
78     int32_t devicestatusStatus_ = -1;
79     bool notifyFlag_ = false;
80     int32_t timerInterval_ = -1;
81     int32_t timerFd_ = -1;
82     int32_t epFd_ = -1;
83     std::map<DevicestatusDataUtils::DevicestatusType, DevicestatusDataUtils::DevicestatusValue> rdbDataMap_;
84     std::mutex mutex_;
85 };
86 
87 class HelperCallback : public NativeRdb::RdbOpenCallback {
88 public:
89     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
90     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
91 };
92 }
93 }
94 #endif // DEVICESTATUS_SENSOR_RDB_H
95