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_SERVICE_CLIENT_H
17 #define SENSOR_SERVICE_CLIENT_H
18 
19 #include <map>
20 #include <set>
21 #include <vector>
22 
23 #include "iservice_registry.h"
24 #include "singleton.h"
25 
26 #include "active_info.h"
27 #include "sensor_agent_type.h"
28 #include "sensor_basic_data_channel.h"
29 #include "sensor_basic_info.h"
30 #include "sensor_client_stub.h"
31 #include "sensor_data_channel.h"
32 #include "sensor.h"
33 #include "sensor_service_proxy.h"
34 #include "stream_socket.h"
35 
36 namespace OHOS {
37 namespace Sensors {
38 class SensorServiceClient : public StreamSocket, public Singleton<SensorServiceClient> {
39 public:
40     ~SensorServiceClient() override;
41     std::vector<Sensor> GetSensorList();
42     int32_t EnableSensor(int32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay);
43     int32_t DisableSensor(int32_t sensorId);
44     int32_t TransferDataChannel(sptr<SensorDataChannel> sensorDataChannel);
45     int32_t DestroyDataChannel();
46     void ProcessDeathObserver(const wptr<IRemoteObject> &object);
47     bool IsValid(int32_t sensorId);
48     int32_t SuspendSensors(int32_t pid);
49     int32_t ResumeSensors(int32_t pid);
50     int32_t GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList);
51     int32_t Register(SensorActiveInfoCB callback, sptr<SensorDataChannel> sensorDataChannel);
52     int32_t Unregister(SensorActiveInfoCB callback);
53     int32_t ResetSensors();
54     void ReceiveMessage(const char *buf, size_t size);
55     void Disconnect();
56     void HandleNetPacke(NetPacket &pkt);
57 
58 private:
59     int32_t InitServiceClient();
60     void UpdateSensorInfoMap(int32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay);
61     void DeleteSensorInfoItem(int32_t sensorId);
62     int32_t CreateSocketChannel();
63     void ReenableSensor();
64     std::mutex clientMutex_;
65     sptr<IRemoteObject::DeathRecipient> serviceDeathObserver_ = nullptr;
66     sptr<ISensorService> sensorServer_ = nullptr;
67     std::vector<Sensor> sensorList_;
68     std::mutex channelMutex_;
69     sptr<SensorDataChannel> dataChannel_ = nullptr;
70     sptr<SensorClientStub> sensorClientStub_ = nullptr;
71     std::mutex mapMutex_;
72     std::map<int32_t, SensorBasicInfo> sensorInfoMap_;
73     std::atomic_bool isConnected_ = false;
74     CircleStreamBuffer circBuf_;
75     std::mutex activeInfoCBMutex_;
76     std::set<SensorActiveInfoCB> activeInfoCBSet_;
77 };
78 } // namespace Sensors
79 } // namespace OHOS
80 #endif // SENSOR_SERVICE_CLIENT_H