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