1 /*
2  * Copyright (c) 2021-2023 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 SENSOR_DATA_CHANNEL_H
17 #define SENSOR_DATA_CHANNEL_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <unordered_set>
23 
24 #include "sensor_agent_type.h"
25 #include "sensor_basic_data_channel.h"
26 #include "sensor_event_handler.h"
27 
28 namespace OHOS {
29 namespace Sensors {
30 using DataChannelCB = std::function<void(SensorEvent *, int32_t, void *)>;
31 using ReceiveMessageFun = std::function<void(const char *, size_t)>;
32 using DisconnectFun = std::function<void()>;
33 class SensorDataChannel : public SensorBasicDataChannel {
34 public:
35     SensorDataChannel() = default;
36     ~SensorDataChannel();
37     int32_t CreateSensorDataChannel(DataChannelCB callBack, void *data);
38     int32_t DestroySensorDataChannel();
39     int32_t RestoreSensorDataChannel();
40     DataChannelCB dataCB_ = nullptr;
41     void *privateData_ = nullptr;
42     int32_t AddFdListener(int32_t fd, ReceiveMessageFun receiveMessage, DisconnectFun disconnect);
43     int32_t DelFdListener(int32_t fd);
44     ReceiveMessageFun GetReceiveMessageFun() const;
45     DisconnectFun GetDisconnectFun() const;
46 
47 private:
48     int32_t InnerSensorDataChannel();
49     std::mutex eventRunnerMutex_;
50     std::shared_ptr<SensorEventHandler> eventHandler_ = nullptr;
51     std::unordered_set<int32_t> listenedFdSet_;
52     ReceiveMessageFun receiveMessage_;
53     DisconnectFun disconnect_;
54 };
55 }  // namespace Sensors
56 }  // namespace OHOS
57 #endif // SENSOR_DATA_CHANNEL_H
58