• 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_H
17 #define SENSOR_SERVICE_H
18 
19 #include "system_ability.h"
20 
21 #include "death_recipient_template.h"
22 #include "sensor_delayed_sp_singleton.h"
23 #include "sensor_power_policy.h"
24 #include "sensor_service_stub.h"
25 #include "stream_server.h"
26 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
27 #include "sensor_hdi_connection.h"
28 #endif // HDF_DRIVERS_INTERFACE_SENSOR
29 
30 namespace OHOS {
31 namespace Sensors {
32 enum class SensorServiceState {
33     STATE_STOPPED,
34     STATE_RUNNING,
35 };
36 
37 class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub {
38     DECLARE_SYSTEM_ABILITY(SensorService)
39     SENSOR_DECLARE_DELAYED_SP_SINGLETON(SensorService);
40 
41 public:
42     void OnDump() override;
43     void OnStart() override;
44     void OnStop() override;
45     int Dump(int fd, const std::vector<std::u16string> &args) override;
46     ErrCode EnableSensor(const SensorDescriptionIPC &sensorDesc, int64_t samplingPeriodNs,
47         int64_t maxReportDelayNs) override;
48     ErrCode DisableSensor(const SensorDescriptionIPC &sensorDesc) override;
49     ErrCode GetSensorList(std::vector<Sensor> &sensorList) override;
50     ErrCode GetSensorListByDevice(int32_t deviceId, std::vector<Sensor> &sensorList) override;
51     ErrCode TransferDataChannel(int32_t sendFd, const sptr<IRemoteObject> &sensorClient) override;
52     ErrCode DestroySensorChannel(const sptr<IRemoteObject> &sensorClient) override;
53     void ProcessDeathObserver(const wptr<IRemoteObject> &object);
54     ErrCode SuspendSensors(int32_t pid) override;
55     ErrCode ResumeSensors(int32_t pid) override;
56     ErrCode GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList) override;
57     ErrCode CreateSocketChannel(const sptr<IRemoteObject> &sensorClient, int32_t &clientFd) override;
58     ErrCode DestroySocketChannel(const sptr<IRemoteObject> &sensorClient) override;
59     ErrCode EnableActiveInfoCB() override;
60     ErrCode DisableActiveInfoCB() override;
61     ErrCode ResetSensors() override;
62     ErrCode SetDeviceStatus(uint32_t deviceStatus) override;
63     ErrCode TransferClientRemoteObject(const sptr<IRemoteObject> &sensorClient) override;
64     ErrCode DestroyClientRemoteObject(const sptr<IRemoteObject> &sensorClient) override;
65 
66 private:
67     DISALLOW_COPY_AND_MOVE(SensorService);
68     std::vector<Sensor> GetSensorList();
69     std::vector<Sensor> GetSensorListByDevice(int32_t deviceId);
70     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
71     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
72     ErrCode CheckAuthAndParameter(const SensorDescription &sensorDesc, int64_t samplingPeriodNs,
73         int64_t maxReportDelayNs);
74     void ReportPlugEventCallback(const SensorPlugInfo &sensorPlugInfo);
75     ErrCode SensorReportEvent(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs,
76         int32_t pid);
77 
78     class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize {
79     public:
PermStateChangeCb(const Security::AccessToken::PermStateChangeScope & scope,sptr<SensorService> server)80         PermStateChangeCb(const Security::AccessToken::PermStateChangeScope &scope,
81             sptr<SensorService> server) : PermStateChangeCallbackCustomize(scope), server_(server) {}
82         void PermStateChangeCallback(PermStateChangeInfo &result) override;
83 
84     private:
85         sptr<SensorService> server_ = nullptr;
86     };
87 
88     void RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient, int32_t pid);
89     void UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient);
90     bool InitSensorPolicy();
91     void ReportOnChangeData(const SensorDescription &sensorDesc);
92     void ReportSensorSysEvent(int32_t sensorType, bool enable, int32_t pid, int64_t samplingPeriodNs = 0,
93         int64_t maxReportDelayNs = 0);
94     ErrCode DisableSensor(const SensorDescription &sensorDesc, int32_t pid);
95     bool RegisterPermCallback(int32_t sensorType);
96     void UnregisterPermCallback();
97     bool CheckSensorId(const SensorDescription &sensorDesc);
98     void ReportActiveInfo(const SensorDescription &sensorDesc, int32_t pid);
99     bool IsSystemServiceCalling();
100     bool IsSystemCalling();
101     bool IsNeedLoadMotionLib();
102     void SetCritical();
103     SensorServiceState state_;
104     std::mutex serviceLock_;
105     std::mutex sensorsMutex_;
106     std::mutex sensorMapMutex_;
107     std::vector<Sensor> sensors_;
108     std::unordered_map<SensorDescription, Sensor> sensorMap_;
109 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
110     bool InitInterface();
111     bool InitDataCallback();
112     bool InitSensorList();
113     bool InitPlugCallback();
114     SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance();
115     sptr<SensorDataProcesser> sensorDataProcesser_ = nullptr;
116     sptr<ReportDataCallback> reportDataCallback_ = nullptr;
117 #endif // HDF_DRIVERS_INTERFACE_SENSOR
118     ClientInfo &clientInfo_ = ClientInfo::GetInstance();
119     SensorManager &sensorManager_ = SensorManager::GetInstance();
120     std::mutex uidLock_;
121     // death recipient of sensor client
122     std::mutex clientDeathObserverMutex_;
123     sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr;
124     std::shared_ptr<PermStateChangeCb> permStateChangeCb_ = nullptr;
125     ErrCode SaveSubscriber(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs);
126     std::atomic_bool isReportActiveInfo_ = false;
127     static std::atomic_bool isAccessTokenServiceActive_;
128     static std::atomic_bool isMemoryMgrServiceActive_;
129     static std::atomic_bool isCritical_;
130 };
131 
132 #define POWER_POLICY SensorPowerPolicy::GetInstance()
133 } // namespace Sensors
134 } // namespace OHOS
135 #endif // SENSOR_SERVICE_H
136