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