• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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_cache.h"
17 
18 #include <cstdlib>
19 #include <cstring>
20 
21 #include "errors.h"
22 #include "securec.h"
23 #include "medical_errors.h"
24 #include "medical_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, MedicalSensorLogDomain::MEDICAL_SENSOR_UTILS, "ReportDataCache"
33 };
34 }  // namespace
ReportDataCache()35 ReportDataCache::ReportDataCache()
36 {
37     eventsBuf_.circularBuf = new struct SensorEvent[CIRCULAR_BUF_LEN];
38     eventsBuf_.readPosition = 0;
39     eventsBuf_.writePosition = 0;
40     eventsBuf_.eventNum = 0;
41 }
42 
~ReportDataCache()43 ReportDataCache::~ReportDataCache()
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 
CacheData(const struct SensorEvent * event,sptr<ReportDataCache> cache)54 int32_t ReportDataCache::CacheData(const struct SensorEvent* event, sptr<ReportDataCache> cache)
55 {
56     if (cache == nullptr || cache->eventsBuf_.circularBuf == nullptr || event == nullptr) {
57         HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__);
58         return ERROR;
59     }
60     struct SensorEvent  eventCopy = {
61         .sensorTypeId = event->sensorTypeId,
62         .version = event->version,
63         .timestamp = event->timestamp,
64         .option = event->option,
65         .mode = event->mode,
66         .dataLen = event->dataLen
67     };
68     uint32_t len = (event->dataLen < SENSOR_DATA_LENGHT) ? event->dataLen : SENSOR_DATA_LENGHT;
69     eventCopy.data = new uint8_t[len];
70     HiLog::Info(LABEL, "%{public}s dataLength: %{public}d, len = %{public}d", __func__, event->dataLen, len);
71     if (memcpy_s(eventCopy.data, len, event->data, event->dataLen) != EOK) {
72         HiLog::Error(LABEL, "%{public}s copy data failed", __func__);
73         return COPY_ERR;
74     }
75     int32_t leftSize = CIRCULAR_BUF_LEN - cache->eventsBuf_.eventNum;
76     int32_t toEndLen = CIRCULAR_BUF_LEN - cache->eventsBuf_.writePosition;
77     if (toEndLen == 0) {
78             cache->eventsBuf_.circularBuf[0] = eventCopy;
79             cache->eventsBuf_.writePosition = 1 - toEndLen;
80     } else {
81             cache->eventsBuf_.circularBuf[cache->eventsBuf_.writePosition] = eventCopy;
82             cache->eventsBuf_.writePosition += 1;
83     }
84     if (leftSize < 1) {
85         cache->eventsBuf_.readPosition = cache->eventsBuf_.writePosition;
86     }
87     cache->eventsBuf_.eventNum += 1;
88     if (cache->eventsBuf_.eventNum >= CIRCULAR_BUF_LEN) {
89         cache->eventsBuf_.eventNum = CIRCULAR_BUF_LEN;
90     }
91     if (cache->eventsBuf_.writePosition == CIRCULAR_BUF_LEN) {
92         cache->eventsBuf_.writePosition = 0;
93     }
94     return ERR_OK;
95 }
96 
GetEventData()97 struct CircularEventBuf &ReportDataCache::GetEventData()
98 {
99     return eventsBuf_;
100 }
101 }  // namespace Sensors
102 }  // namespace OHOS
103