1 /*
2  * Copyright (c) 2021 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_MANAGER_H
17 #define SENSOR_MANAGER_H
18 
19 #include <mutex>
20 #include <thread>
21 #include <unordered_map>
22 
23 #include "client_info.h"
24 #include "flush_info_record.h"
25 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
26 #include "sensor_data_processer.h"
27 #include "sensor_hdi_connection.h"
28 #else
29 #include "sensor.h"
30 #endif // HDF_DRIVERS_INTERFACE_SENSOR
31 
32 namespace OHOS {
33 namespace Sensors {
34 using namespace Security::AccessToken;
35 class SensorManager : public Singleton<SensorManager> {
36 public:
37 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
38     void InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap, sptr<SensorDataProcesser> dataProcesser,
39                        sptr<ReportDataCallback> dataCallback);
40     bool SetBestSensorParams(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs);
41     bool ResetBestSensorParams(int32_t sensorId);
42     void StartDataReportThread();
43 #else
44     void InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap);
45 #endif // HDF_DRIVERS_INTERFACE_SENSOR
46     bool SaveSubscriber(int32_t sensorId, uint32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs);
47     SensorBasicInfo GetSensorInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs);
48     bool IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid);
49     ErrCode AfterDisableSensor(int32_t sensorId);
50     void GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive = false);
51 
52 private:
53 #ifdef HDF_DRIVERS_INTERFACE_SENSOR
54     SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance();
55     std::thread dataThread_;
56     sptr<SensorDataProcesser> sensorDataProcesser_ = nullptr;
57     sptr<ReportDataCallback> reportDataCallback_ = nullptr;
58 #endif // HDF_DRIVERS_INTERFACE_SENSOR
59     ClientInfo &clientInfo_ = ClientInfo::GetInstance();
60     std::unordered_map<int32_t, Sensor> sensorMap_;
61     std::mutex sensorMapMutex_;
62 };
63 }  // namespace Sensors
64 }  // namespace OHOS
65 #endif  // SENSOR_MANAGER_H
66