• 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_proxy.h"
17 
18 #include <cstring>
19 
20 #include "securec.h"
21 #include "sensor_catalog.h"
22 #include "sensor_service_client.h"
23 #include "sensors_errors.h"
24 
25 using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace Sensors {
28 namespace {
29 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorAgentProxy" };
30 constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff;
31 }  // namespace
32 
33 #define SenClient SensorServiceClient::GetInstance()
34 OHOS::sptr<SensorAgentProxy> SensorAgentProxy::sensorObj_ = nullptr;
35 bool SensorAgentProxy::g_isChannelCreated;
36 int64_t SensorAgentProxy::g_samplingInterval;
37 int64_t SensorAgentProxy::g_reportInterval;
38 std::recursive_mutex SensorAgentProxy::subscribeMutex_;
39 std::mutex SensorAgentProxy::chanelMutex_;
40 std::mutex sensorInfoMutex_;
41 SensorInfo *sensorInfos_ = nullptr;
42 int32_t sensorInfoCount_ = 0;
43 std::map<int32_t, const SensorUser *> SensorAgentProxy::g_subscribeMap;
44 std::map<int32_t, const SensorUser *> SensorAgentProxy::g_unsubscribeMap;
45 
SensorAgentProxy()46 SensorAgentProxy::SensorAgentProxy()
47     : dataChannel_(new (std::nothrow) SensorDataChannel())
48 {}
49 
~SensorAgentProxy()50 SensorAgentProxy::~SensorAgentProxy()
51 {
52     CALL_LOG_ENTER;
53     ClearSensorInfos();
54 }
55 
GetSensorsObj()56 const SensorAgentProxy *SensorAgentProxy::GetSensorsObj()
57 {
58     CALL_LOG_ENTER;
59     if (sensorObj_ == nullptr) {
60         SEN_HILOGD("sensorObj_ new object");
61         sensorObj_ = new (std::nothrow) SensorAgentProxy();
62     }
63     return sensorObj_;
64 }
65 
HandleSensorData(SensorEvent * events,int32_t num,void * data)66 void SensorAgentProxy::HandleSensorData(SensorEvent *events, int32_t num, void *data)
67 {
68     CHKPV(events);
69     if (num <= 0) {
70         SEN_HILOGE("events is null or num is invalid");
71         return;
72     }
73     SensorEvent eventStream;
74     for (int32_t i = 0; i < num; ++i) {
75         eventStream = events[i];
76         std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
77         auto iter = g_subscribeMap.find(eventStream.sensorTypeId);
78         if (iter == g_subscribeMap.end()) {
79             SEN_HILOGE("sensor is not subscribed");
80             return;
81         }
82         const SensorUser *user = iter->second;
83         CHKPV(user);
84         user->callback(&eventStream);
85     }
86 }
87 
CreateSensorDataChannel() const88 int32_t SensorAgentProxy::CreateSensorDataChannel() const
89 {
90     CALL_LOG_ENTER;
91     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
92     if (g_isChannelCreated) {
93         SEN_HILOGI("the channel has already been created");
94         return ERR_OK;
95     }
96     CHKPR(dataChannel_, INVALID_POINTER);
97     auto ret = dataChannel_->CreateSensorDataChannel(HandleSensorData, nullptr);
98     if (ret != ERR_OK) {
99         SEN_HILOGE("create data channel failed, ret:%{public}d", ret);
100         return ret;
101     }
102     ret = SenClient.TransferDataChannel(dataChannel_);
103     if (ret != ERR_OK) {
104         auto destoryRet = dataChannel_->DestroySensorDataChannel();
105         SEN_HILOGE("transfer data channel failed, ret:%{public}d,destoryRet:%{public}d", ret, destoryRet);
106         return ret;
107     }
108     g_isChannelCreated = true;
109     return ERR_OK;
110 }
111 
DestroySensorDataChannel() const112 int32_t SensorAgentProxy::DestroySensorDataChannel() const
113 {
114     CALL_LOG_ENTER;
115     std::lock_guard<std::mutex> chanelLock(chanelMutex_);
116     if (!g_isChannelCreated) {
117         SEN_HILOGI("channel has been destroyed");
118         return ERR_OK;
119     }
120     CHKPR(dataChannel_, INVALID_POINTER);
121     int32_t ret = dataChannel_->DestroySensorDataChannel();
122     if (ret != ERR_OK) {
123         SEN_HILOGE("destory data channel failed, ret:%{public}d", ret);
124         return ret;
125     }
126     ret = SenClient.DestroyDataChannel();
127     if (ret != ERR_OK) {
128         SEN_HILOGE("destory service data channel fail, ret:%{public}d", ret);
129         return ret;
130     }
131     g_isChannelCreated = false;
132     return ERR_OK;
133 }
134 
ActivateSensor(int32_t sensorId,const SensorUser * user) const135 int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user) const
136 {
137     CHKPR(user, OHOS::Sensors::ERROR);
138     CHKPR(user->callback, OHOS::Sensors::ERROR);
139     if (g_samplingInterval < 0 || g_reportInterval < 0) {
140         SEN_HILOGE("samplingPeroid or g_reportInterval is invalid");
141         return ERROR;
142     }
143     if (!SenClient.IsValid(sensorId)) {
144         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
145         return PARAMETER_ERROR;
146     }
147     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
148     if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) {
149         SEN_HILOGE("subscribe sensorId first");
150         return ERROR;
151     }
152     int32_t ret = SenClient.EnableSensor(sensorId, g_samplingInterval, g_reportInterval);
153     g_samplingInterval = -1;
154     g_reportInterval = -1;
155     if (ret != 0) {
156         SEN_HILOGE("enable sensor failed, ret:%{public}d", ret);
157         g_subscribeMap.erase(sensorId);
158         return ret;
159     }
160     return ret;
161 }
162 
DeactivateSensor(int32_t sensorId,const SensorUser * user) const163 int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user) const
164 {
165     CHKPR(user, OHOS::Sensors::ERROR);
166     CHKPR(user->callback, OHOS::Sensors::ERROR);
167     if (!SenClient.IsValid(sensorId)) {
168         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
169         return PARAMETER_ERROR;
170     }
171     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
172     if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) {
173         SEN_HILOGE("subscribe sensorId first");
174         return OHOS::Sensors::ERROR;
175     }
176     g_subscribeMap.erase(sensorId);
177     g_unsubscribeMap[sensorId] = user;
178     int32_t ret = SenClient.DisableSensor(sensorId);
179     if (ret != 0) {
180         SEN_HILOGE("DisableSensor failed, ret:%{public}d", ret);
181         return ret;
182     }
183     return ret;
184 }
185 
SetBatch(int32_t sensorId,const SensorUser * user,int64_t samplingInterval,int64_t reportInterval) const186 int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval,
187                                    int64_t reportInterval) const
188 {
189     CHKPR(user, OHOS::Sensors::ERROR);
190     if (!SenClient.IsValid(sensorId)) {
191         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
192         return PARAMETER_ERROR;
193     }
194     if (samplingInterval < 0 || reportInterval < 0) {
195         SEN_HILOGE("samplingInterval or reportInterval is invalid");
196         return OHOS::Sensors::ERROR;
197     }
198     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
199     if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) {
200         SEN_HILOGE("subscribe sensorId first");
201         return OHOS::Sensors::ERROR;
202     }
203     g_samplingInterval = samplingInterval;
204     g_reportInterval = reportInterval;
205     return OHOS::Sensors::SUCCESS;
206 }
207 
SubscribeSensor(int32_t sensorId,const SensorUser * user) const208 int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *user) const
209 {
210     SEN_HILOGI("in, sensorId:%{public}d", sensorId);
211     CHKPR(user, OHOS::Sensors::ERROR);
212     CHKPR(user->callback, OHOS::Sensors::ERROR);
213     if (!SenClient.IsValid(sensorId)) {
214         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
215         return PARAMETER_ERROR;
216     }
217     int32_t ret = CreateSensorDataChannel();
218     if (ret != ERR_OK) {
219         SEN_HILOGE("create sensor data chanel failed");
220         return OHOS::Sensors::ERROR;
221     }
222     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
223     g_subscribeMap[sensorId] = user;
224     return OHOS::Sensors::SUCCESS;
225 }
226 
UnsubscribeSensor(int32_t sensorId,const SensorUser * user) const227 int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) const
228 {
229     SEN_HILOGI("in, sensorId: %{public}d", sensorId);
230     CHKPR(user, OHOS::Sensors::ERROR);
231     CHKPR(user->callback, OHOS::Sensors::ERROR);
232     if (!SenClient.IsValid(sensorId)) {
233         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
234         return PARAMETER_ERROR;
235     }
236     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
237     if (g_unsubscribeMap.find(sensorId) == g_unsubscribeMap.end() || g_unsubscribeMap[sensorId] != user) {
238         SEN_HILOGE("deactivate sensorId first");
239         return OHOS::Sensors::ERROR;
240     }
241     if (g_subscribeMap.empty()) {
242         int32_t ret = DestroySensorDataChannel();
243         if (ret != ERR_OK) {
244             SEN_HILOGE("destory data channel fail, ret:%{public}d", ret);
245             return ret;
246         }
247     }
248     g_unsubscribeMap.erase(sensorId);
249     return OHOS::Sensors::SUCCESS;
250 }
251 
SetMode(int32_t sensorId,const SensorUser * user,int32_t mode) const252 int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) const
253 {
254     CHKPR(user, OHOS::Sensors::ERROR);
255     CHKPR(user->callback, OHOS::Sensors::ERROR);
256     if (!SenClient.IsValid(sensorId)) {
257         SEN_HILOGE("sensorId is invalid, %{public}d", sensorId);
258         return ERROR;
259     }
260     std::lock_guard<std::recursive_mutex> subscribeLock(subscribeMutex_);
261     if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap.at(sensorId) != user)) {
262         SEN_HILOGE("subscribe sensorId first");
263         return OHOS::Sensors::ERROR;
264     }
265     return OHOS::Sensors::SUCCESS;
266 }
267 
ClearSensorInfos() const268 void SensorAgentProxy::ClearSensorInfos() const
269 {
270     CHKPV(sensorInfos_);
271     free(sensorInfos_);
272     sensorInfos_ = nullptr;
273 }
274 
ConvertSensorInfos() const275 int32_t SensorAgentProxy::ConvertSensorInfos() const
276 {
277     CALL_LOG_ENTER;
278     std::vector<Sensor> sensorList = SenClient.GetSensorList();
279     if (sensorList.empty()) {
280         SEN_HILOGE("get sensor lists failed");
281         return ERROR;
282     }
283     size_t count = sensorList.size();
284     if (count > MAX_SENSOR_LIST_SIZE) {
285         SEN_HILOGE("The number of sensors exceeds the maximum value");
286         return ERROR;
287     }
288     sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count);
289     CHKPR(sensorInfos_, ERROR);
290     for (size_t i = 0; i < count; ++i) {
291         errno_t ret = strcpy_s((sensorInfos_ + i)->sensorName, NAME_MAX_LEN,
292             sensorList[i].GetSensorName().c_str());
293         CHKCR(ret == EOK, ERROR);
294         ret = strcpy_s((sensorInfos_ + i)->vendorName, NAME_MAX_LEN,
295             sensorList[i].GetVendorName().c_str());
296         CHKCR(ret == EOK, ERROR);
297         ret = strcpy_s((sensorInfos_ + i)->hardwareVersion, VERSION_MAX_LEN,
298             sensorList[i].GetHardwareVersion().c_str());
299         CHKCR(ret == EOK, ERROR);
300         ret = strcpy_s((sensorInfos_ + i)->firmwareVersion, VERSION_MAX_LEN,
301             sensorList[i].GetFirmwareVersion().c_str());
302         CHKCR(ret == EOK, ERROR);
303         (sensorInfos_ + i)->sensorId = static_cast<int32_t>(sensorList[i].GetSensorId());
304         (sensorInfos_ + i)->sensorTypeId = static_cast<int32_t>(sensorList[i].GetSensorTypeId());
305         (sensorInfos_ + i)->maxRange = sensorList[i].GetMaxRange();
306         (sensorInfos_ + i)->precision = sensorList[i].GetResolution();
307         (sensorInfos_ + i)->power = sensorList[i].GetPower();
308         (sensorInfos_ + i)->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs();
309         (sensorInfos_ + i)->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs();
310     }
311     sensorInfoCount_ = static_cast<int32_t>(count);
312     return SUCCESS;
313 }
314 
GetAllSensors(SensorInfo ** sensorInfo,int32_t * count) const315 int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const
316 {
317     CALL_LOG_ENTER;
318     CHKPR(sensorInfo, OHOS::Sensors::ERROR);
319     CHKPR(count, OHOS::Sensors::ERROR);
320     std::lock_guard<std::mutex> listLock(sensorInfoMutex_);
321     if (sensorInfos_ == nullptr) {
322         int32_t ret = ConvertSensorInfos();
323         if (ret != SUCCESS) {
324             SEN_HILOGE("convert sensor lists failed");
325             ClearSensorInfos();
326             return ERROR;
327         }
328     }
329     *sensorInfo = sensorInfos_;
330     *count = sensorInfoCount_;
331     return SUCCESS;
332 }
333 }  // namespace Sensors
334 }  // namespace OHOS