• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sensor_agent.h"
17 
18 #include "sensor_agent_proxy.h"
19 #include "sensors_errors.h"
20 
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23 using OHOS::Sensors::SensorAgentProxy;
24 using OHOS::Sensors::SERVICE_EXCEPTION;
25 using OHOS::Sensors::PARAMETER_ERROR;
26 using OHOS::Sensors::PERMISSION_DENIED;
27 
28 static const HiLogLabel LABEL = {LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorNativeAPI"};
29 
GetInstance()30 static const SensorAgentProxy *GetInstance()
31 {
32     const SensorAgentProxy *obj = SensorAgentProxy::GetSensorsObj();
33     return obj;
34 }
35 
NormalizeErrCode(int32_t code)36 static int32_t NormalizeErrCode(int32_t code)
37 {
38     switch (code) {
39         case PERMISSION_DENIED: {
40             return PERMISSION_DENIED;
41         }
42         case PARAMETER_ERROR: {
43             return PARAMETER_ERROR;
44         }
45         default: {
46             return SERVICE_EXCEPTION;
47         }
48     }
49 }
50 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count)51 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count)
52 {
53     const SensorAgentProxy *proxy = GetInstance();
54     if (proxy == nullptr) {
55         SEN_HILOGE("proxy is nullptr");
56         return SERVICE_EXCEPTION;
57     }
58     int32_t ret = proxy->GetAllSensors(sensorInfo, count);
59     if (ret != OHOS::ERR_OK) {
60         SEN_HILOGE("GetAllSensors failed");
61         return NormalizeErrCode(ret);
62     }
63     return ret;
64 }
65 
ActivateSensor(int32_t sensorId,const SensorUser * user)66 int32_t ActivateSensor(int32_t sensorId, const SensorUser *user)
67 {
68     const SensorAgentProxy *proxy = GetInstance();
69     if (proxy == nullptr) {
70         SEN_HILOGE("proxy is nullptr");
71         return SERVICE_EXCEPTION;
72     }
73     int32_t ret = proxy->ActivateSensor(sensorId, user);
74     if (ret != OHOS::ERR_OK) {
75         SEN_HILOGE("ActivateSensor failed");
76         return NormalizeErrCode(ret);
77     }
78     return ret;
79 }
80 
DeactivateSensor(int32_t sensorId,const SensorUser * user)81 int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user)
82 {
83     const SensorAgentProxy *proxy = GetInstance();
84     if (proxy == nullptr) {
85         SEN_HILOGE("proxy is nullptr");
86         return SERVICE_EXCEPTION;
87     }
88     int32_t ret = proxy->DeactivateSensor(sensorId, user);
89     if (ret != OHOS::ERR_OK) {
90         SEN_HILOGE("DeactivateSensor failed");
91         return NormalizeErrCode(ret);
92     }
93     return ret;
94 }
95 
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval)96 int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval)
97 {
98     const SensorAgentProxy *proxy = GetInstance();
99     if (proxy == nullptr) {
100         SEN_HILOGE("proxy is nullptr");
101         return SERVICE_EXCEPTION;
102     }
103     int32_t ret = proxy->SetBatch(sensorId, user, samplingInterval, reportInterval);
104     if (ret != OHOS::ERR_OK) {
105         SEN_HILOGE("SetBatch failed");
106         return NormalizeErrCode(ret);
107     }
108     return ret;
109 }
110 
SubscribeSensor(int32_t sensorId,const SensorUser * user)111 int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user)
112 {
113     const SensorAgentProxy *proxy = GetInstance();
114     if (proxy == nullptr) {
115         SEN_HILOGE("proxy is nullptr");
116         return SERVICE_EXCEPTION;
117     }
118     int32_t ret = proxy->SubscribeSensor(sensorId, user);
119     if (ret != OHOS::ERR_OK) {
120         SEN_HILOGE("SubscribeSensor failed");
121         return NormalizeErrCode(ret);
122     }
123     return ret;
124 }
125 
UnsubscribeSensor(int32_t sensorId,const SensorUser * user)126 int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user)
127 {
128     const SensorAgentProxy *proxy = GetInstance();
129     if (proxy == nullptr) {
130         SEN_HILOGE("proxy is nullptr");
131         return SERVICE_EXCEPTION;
132     }
133     int32_t ret = proxy->UnsubscribeSensor(sensorId, user);
134     if (ret != OHOS::ERR_OK) {
135         SEN_HILOGE("UnsubscribeSensor failed");
136         return NormalizeErrCode(ret);
137     }
138     return ret;
139 }
140 
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode)141 int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode)
142 {
143     const SensorAgentProxy *proxy = GetInstance();
144     if (proxy == nullptr) {
145         SEN_HILOGE("proxy is nullptr");
146         return SERVICE_EXCEPTION;
147     }
148     return proxy->SetMode(sensorId, user, mode);
149 }
150