1 /*
2  * Copyright (c) 2021 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 FLUSH_INFO_RECORD_H
17 #define FLUSH_INFO_RECORD_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "nocopyable.h"
24 #include "refbase.h"
25 #include "singleton.h"
26 
27 #include "client_info.h"
28 #include "sensor_agent_type.h"
29 #include "sensor_basic_data_channel.h"
30 #include "sensor_errors.h"
31 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
32 #include "sensor_hdi_connection.h"
33 #endif // HDF_DRIVERS_INTERFACE_SENSOR
34 
35 namespace OHOS {
36 namespace Sensors {
37 struct FlushInfo {
38     wptr<SensorBasicDataChannel> flushChannel;
39     bool flushFromEnable;
FlushInfoFlushInfo40     FlushInfo(const sptr<SensorBasicDataChannel> &channel, bool enableFlush)
41         : flushChannel(channel), flushFromEnable(enableFlush){};
42 };
43 
44 class FlushInfoRecord : public Singleton<FlushInfoRecord> {
45 public:
46     FlushInfoRecord() = default;
~FlushInfoRecord()47     ~FlushInfoRecord()
48     {
49         flushInfo_.clear();
50     }
51     std::unordered_map<int32_t, std::vector<FlushInfo>> GetFlushInfo();
52     void ClearFlushInfoItem(int32_t sensorId);
53     ErrCode SetFlushInfo(int32_t sensorId, const sptr<SensorBasicDataChannel> &channel, bool isFirstFlush);
54     bool IsFlushChannelValid(const std::vector<sptr<SensorBasicDataChannel>> &currChannelList,
55                            const sptr<SensorBasicDataChannel> &flushChannel);
56     int32_t GetFlushChannelIndex(const std::vector<FlushInfo> &flushInfoList,
57                            const sptr<SensorBasicDataChannel> &channel);
58     ErrCode FlushProcess(const int32_t sensorId, const uint32_t flag, const int32_t pid, const bool isEnableFlush);
59 
60 private:
61     DISALLOW_COPY_AND_MOVE(FlushInfoRecord);
62     ClientInfo &clientInfo_ = ClientInfo::GetInstance();
63     // sensorId, channel pointer for pending flush.
64     std::unordered_map<int32_t, std::vector<FlushInfo>> flushInfo_;
65     std::mutex flushInfoMutex_;
66 };
67 } // namespace Sensors
68 } // namespace OHOS
69 #endif // FLUSH_INFO_RECORD_H
70