• 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 #include "sensor_errors.h"
18 
19 #undef LOG_TAG
20 #define LOG_TAG "ReportDataCallback"
21 
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
ReportDataCallback()25 ReportDataCallback::ReportDataCallback()
26 {
27     eventsBuf_.circularBuf = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN];
28     CHKPL(eventsBuf_.circularBuf);
29     eventsBuf_.readPos = 0;
30     eventsBuf_.writePosition = 0;
31     eventsBuf_.eventNum = 0;
32 }
33 
~ReportDataCallback()34 ReportDataCallback::~ReportDataCallback()
35 {
36     if (eventsBuf_.circularBuf != nullptr) {
37         delete[] eventsBuf_.circularBuf;
38         eventsBuf_.circularBuf = nullptr;
39     }
40     eventsBuf_.circularBuf = nullptr;
41     eventsBuf_.readPos = 0;
42     eventsBuf_.writePosition = 0;
43     eventsBuf_.eventNum = 0;
44 }
45 
ReportEventCallback(SensorData * sensorData,sptr<ReportDataCallback> cb)46 int32_t ReportDataCallback::ReportEventCallback(SensorData *sensorData, sptr<ReportDataCallback> cb)
47 {
48     CHKPR(sensorData, ERROR);
49     if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) {
50         SEN_HILOGE("Callback or circularBuf or event cannot be null");
51         return ERROR;
52     }
53     int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum;
54     int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition;
55     if (leftSize < 0 || toEndLen < 0) {
56         SEN_HILOGE("Leftsize and toendlen cannot be less than zero");
57         return ERROR;
58     }
59     if (toEndLen == 0) {
60             cb->eventsBuf_.circularBuf[0] = *sensorData;
61             cb->eventsBuf_.writePosition = 1;
62     } else {
63             cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *sensorData;
64             cb->eventsBuf_.writePosition += 1;
65     }
66     cb->eventsBuf_.eventNum += 1;
67     if (cb->eventsBuf_.eventNum >= CIRCULAR_BUF_LEN) {
68         cb->eventsBuf_.eventNum = CIRCULAR_BUF_LEN;
69     }
70     if (cb->eventsBuf_.writePosition >= CIRCULAR_BUF_LEN) {
71         cb->eventsBuf_.writePosition = 0;
72     }
73     if (leftSize < 1) {
74         cb->eventsBuf_.readPos = cb->eventsBuf_.writePosition;
75     }
76     return ERR_OK;
77 }
78 
GetEventData()79 CircularEventBuf &ReportDataCallback::GetEventData()
80 {
81     return eventsBuf_;
82 }
83 } // namespace Sensors
84 } // namespace OHOS
85