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