• 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 "report_data_callback.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 
21 #include "errors.h"
22 #include "securec.h"
23 #include "sensors_errors.h"
24 #include "sensors_log_domain.h"
25 
26 namespace OHOS {
27 namespace Sensors {
28 using namespace OHOS::HiviewDFX;
29 
30 namespace {
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
32     LOG_CORE, SensorsLogDomain::SENSOR_UTILS, "ReportDataCallback"
33 };
34 }  // namespace
ReportDataCallback()35 ReportDataCallback::ReportDataCallback()
36 {
37     eventsBuf_.circularBuf = new struct SensorEvent[CIRCULAR_BUF_LEN];
38     eventsBuf_.readPosition = 0;
39     eventsBuf_.writePosition = 0;
40     eventsBuf_.eventNum = 0;
41 }
42 
~ReportDataCallback()43 ReportDataCallback::~ReportDataCallback()
44 {
45     if (eventsBuf_.circularBuf != nullptr) {
46         delete[] eventsBuf_.circularBuf;
47     }
48     eventsBuf_.circularBuf = nullptr;
49     eventsBuf_.readPosition = 0;
50     eventsBuf_.writePosition = 0;
51     eventsBuf_.eventNum = 0;
52 }
53 
ZReportDataCallback(const struct SensorEvent * event,sptr<ReportDataCallback> cb)54 int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, sptr<ReportDataCallback> cb)
55 {
56     if (event == nullptr) {
57         HiLog::Error(LABEL, "%{public}s sensor data is null", __func__);
58         return ERROR;
59     }
60     if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) {
61         HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__);
62         delete[] event->data;
63         return ERROR;
64     }
65     int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum;
66     int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition;
67     if (toEndLen == 0) {
68             cb->eventsBuf_.circularBuf[0] = *event;
69             cb->eventsBuf_.writePosition = 1 - toEndLen;
70     } else {
71             cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *event;
72             cb->eventsBuf_.writePosition += 1;
73     }
74     if (leftSize < 1) {
75         cb->eventsBuf_.readPosition = cb->eventsBuf_.writePosition;
76     }
77     cb->eventsBuf_.eventNum += 1;
78     if (cb->eventsBuf_.eventNum >= CIRCULAR_BUF_LEN) {
79         cb->eventsBuf_.eventNum = CIRCULAR_BUF_LEN;
80     }
81     if (cb->eventsBuf_.writePosition == CIRCULAR_BUF_LEN) {
82         cb->eventsBuf_.writePosition = 0;
83     }
84     return ERR_OK;
85 }
86 
GetEventData()87 struct CircularEventBuf &ReportDataCallback::GetEventData()
88 {
89     return eventsBuf_;
90 }
91 }  // namespace Sensors
92 }  // namespace OHOS
93