• 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 #include "hdi_service_impl.h"
16 
17 #include "medical_errors.h"
18 #include "medical_log_domain.h"
19 #include "unistd.h"
20 
21 namespace OHOS {
22 namespace Sensors {
23 using namespace OHOS::HiviewDFX;
24 
25 namespace {
26 constexpr HiLogLabel LABEL = {
27     LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_HDI_HARDWARE, "MedicalSensor_HdiServiceImpl"
28 };
29 constexpr int64_t SAMPLING_INTERVAL_NS = 200000000;
30 constexpr int32_t CONVERT_MULTIPLES = 1000;
31 std::vector<SensorInformation> g_sensorInfos = {
32     {"sensor_test", "default", "1.0.0", "1.0.0", 0, 0, 9999.0, 0.000001, 23.0},
33 };
34 std::vector<int32_t> supportSensors = {0};
35 float g_testData[] = {9.8F};
36 SensorEvents testEvent = {
37     .sensorId = 0,
38     .data = static_cast<uint8_t *>(g_testData),
39     .dataLen = 4
40 };
41 }
42 RecordDataCallback HdiServiceImpl::g_callback;
43 int64_t HdiServiceImpl::g_samplingInterval = -1;
44 int64_t HdiServiceImpl::g_reportInterval = -1;
45 std::atomic_bool HdiServiceImpl::g_isStop = false;
46 
GetSensorList(std::vector<SensorInformation> & sensorList)47 int32_t HdiServiceImpl::GetSensorList(std::vector<SensorInformation>& sensorList)
48 {
49     HiLog::Info(LABEL, "%{public}s in", __func__);
50     sensorList.assign(g_sensorInfos.begin(), g_sensorInfos.end());
51     return ERR_OK;
52 }
53 
DataReportThread()54 void HdiServiceImpl::DataReportThread()
55 {
56     HiLog::Info(LABEL, "%{public}s in", __func__);
57     while (true) {
58         usleep(g_samplingInterval / CONVERT_MULTIPLES);
59         g_callback(&testEvent);
60         if (g_isStop) {
61             break;
62         }
63     }
64     HiLog::Info(LABEL, "%{public}s thread stop", __func__);
65     return;
66 }
67 
EnableSensor(uint32_t sensorId)68 int32_t HdiServiceImpl::EnableSensor(uint32_t sensorId)
69 {
70     HiLog::Info(LABEL, "%{public}s in", __func__);
71     if (g_callback == nullptr) {
72         HiLog::Error(LABEL, "%{public}s enable sensor failed", __func__);
73         return -1;
74     }
75     if (std::count(supportSensors.begin(), supportSensors.end(), sensorId) == 0) {
76         HiLog::Error(LABEL, "%{public}s not support enable sensorId: %{public}d", __func__, sensorId);
77         return -1;
78     }
79     if (std::count(g_enableSensos.begin(), g_enableSensos.end(), sensorId) != 0) {
80         HiLog::Info(LABEL, "%{public}s sensorId: %{public}d has been enabled", __func__, sensorId);
81         return ERR_OK;
82     }
83     g_enableSensos.push_back(sensorId);
84     if (!dataReportThread_.joinable() || g_isStop) {
85         if (dataReportThread_.joinable()) {
86             dataReportThread_.join();
87         }
88         std::thread senocdDataThread(HdiServiceImpl::DataReportThread);
89         dataReportThread_ = std::move(senocdDataThread);
90         g_isStop = false;
91     }
92     return ERR_OK;
93 };
94 
DisableSensor(uint32_t sensorId)95 int32_t HdiServiceImpl::DisableSensor(uint32_t sensorId)
96 {
97     HiLog::Info(LABEL, "%{public}s in", __func__);
98     if (std::count(supportSensors.begin(), supportSensors.end(), sensorId) == 0) {
99         HiLog::Error(LABEL, "%{public}s not support disable sensorId: %{public}d", __func__, sensorId);
100         return -1;
101     }
102     if (std::count(g_enableSensos.begin(), g_enableSensos.end(), sensorId) == 0) {
103         HiLog::Error(LABEL, "%{public}s sensorId: %{public}d should be enable first", __func__, sensorId);
104         return -1;
105     }
106     auto iter = std::remove(g_enableSensos.begin(), g_enableSensos.end(), sensorId);
107     g_enableSensos.erase(iter, g_enableSensos.end());
108     if (g_enableSensos.empty()) {
109         g_isStop = true;
110     }
111     return ERR_OK;
112 }
113 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)114 int32_t HdiServiceImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
115 {
116     HiLog::Info(LABEL, "%{public}s in", __func__);
117     if (samplingInterval < 0 || reportInterval < 0) {
118         samplingInterval = SAMPLING_INTERVAL_NS;
119         reportInterval = 0;
120     }
121     g_samplingInterval = samplingInterval;
122     g_reportInterval = reportInterval;
123     return ERR_OK;
124 }
125 
SetMode(int32_t sensorId,int32_t mode)126 int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode)
127 {
128     return ERR_OK;
129 }
130 
RunCommand(uint32_t sensorId,int32_t cmd,int32_t params)131 int32_t HdiServiceImpl::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params)
132 {
133     return ERR_OK;
134 }
135 
SetOption(int32_t sensorId,uint32_t option)136 int32_t HdiServiceImpl::SetOption(int32_t sensorId, uint32_t option)
137 {
138     return ERR_OK;
139 }
140 
Register(RecordDataCallback cb)141 int32_t HdiServiceImpl::Register(RecordDataCallback cb)
142 {
143     if (cb == nullptr) {
144         HiLog::Error(LABEL, "%{public}s cb cannot be null", __func__);
145         return -1;
146     }
147     g_callback = cb;
148     return ERR_OK;
149 }
150 
Unregister()151 int32_t HdiServiceImpl::Unregister()
152 {
153     g_isStop = true;
154     return ERR_OK;
155 }
156 }  // namespace Sensors
157 }  // namespace OHOS