1 /*
2 * Copyright (c) 2024 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 <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include "hdf_base.h"
22 #include "osal_time.h"
23 #include "v2_0/isensor_interface.h"
24 #include "sensor_type.h"
25 #include "sensor_callback_impl.h"
26 #include "sensor_uhdf_log.h"
27 #include "sensor_hdi_dump.h"
28 #include "sensor_clients_manager.h"
29
30 using namespace OHOS::HDI::Sensor;
31 using namespace OHOS::HDI::Sensor::V2_0;
32 using namespace testing::ext;
33
34 namespace {
35 sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
36 std::vector<HdfSensorInformation> g_info;
37 constexpr int64_t SAMPLING_INTERVAL = 10000000;
38 constexpr int64_t REPORT_INTERVAL = 1;
39 int32_t g_serviceId = 1314;
40 constexpr int32_t SENSOR_ID_START = 1;
41 constexpr int32_t SENSOR_ID_END = 10;
42 constexpr int32_t MAX_RANGE = 1000;
43 constexpr int32_t ACCURACY = 100;
44 constexpr int32_t POWER = 1;
45 constexpr int32_t MIN_DELAY = 10;
46 constexpr int32_t MAX_DELAY = 1000000000;
47 constexpr int32_t FIFO_MAX_EVENT_COUNT = 4;
48 constexpr int32_t COPY_FLAG = 1;
49 constexpr uint32_t INIT_DATA_NUM = 1u;
50 const std::string SENSOR_NAME = "test_sensor";
51 }
52
53 class HdfSensorDumpTest : public testing::Test {
54 public:
55 static void SetUpTestCase();
56 static void TearDownTestCase();
57 void SetUp();
58 void TearDown();
59 void GetAllSensorInfo(std::vector<HdfSensorInformation> &info);
60 void Register(int32_t groupId, const sptr<ISensorCallback> &callbackObj);
61 void Unregister(int32_t groupId, const sptr<ISensorCallback> &callbackObj);
62 void SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval);
63 void Enable(int32_t sensorId);
64 void Disable(int32_t sensorId);
65 void OnDataEvent(const V2_0::HdfSensorEvents& event);
66 void PrintDumpResult(struct HdfSBuf* reply);
67 V2_0::HdfSensorEvents g_event = {1, 1, 100000000, 1, 1, {1, 2, 3, 4}, 4};
68 };
69
SetUpTestCase()70 void HdfSensorDumpTest::SetUpTestCase()
71 {
72 g_serviceId = getpid();
73 }
74
TearDownTestCase()75 void HdfSensorDumpTest::TearDownTestCase()
76 {
77 }
78
SetUp()79 void HdfSensorDumpTest::SetUp()
80 {
81 }
82
TearDown()83 void HdfSensorDumpTest::TearDown()
84 {
85 }
86
GetAllSensorInfo(std::vector<HdfSensorInformation> & info)87 void HdfSensorDumpTest::GetAllSensorInfo(std::vector<HdfSensorInformation> &info)
88 {
89 SENSOR_TRACE;
90 struct HdfSensorInformation sensorInfo = {};
91 for (int32_t sensorId = SENSOR_ID_START; sensorId <= SENSOR_ID_END; sensorId++) {
92 sensorInfo.sensorName = SENSOR_NAME + std::to_string(sensorId);
93 sensorInfo.vendorName = SENSOR_NAME + std::to_string(sensorId);
94 sensorInfo.firmwareVersion = SENSOR_NAME + std::to_string(sensorId);
95 sensorInfo.hardwareVersion = SENSOR_NAME + std::to_string(sensorId);
96 sensorInfo.sensorTypeId = sensorId;
97 sensorInfo.sensorId = sensorId;
98 sensorInfo.maxRange = MAX_RANGE + sensorId;
99 sensorInfo.accuracy = ACCURACY + sensorId;
100 sensorInfo.power = POWER + sensorId;
101 sensorInfo.minDelay = MIN_DELAY + sensorId;
102 sensorInfo.maxDelay = MAX_DELAY + sensorId;
103 sensorInfo.fifoMaxEventCount = FIFO_MAX_EVENT_COUNT + sensorId;
104 info.push_back(std::move(sensorInfo));
105 SensorClientsManager::GetInstance()->CopySensorInfo(info, COPY_FLAG);
106 }
107 }
108
Register(int32_t groupId,const sptr<ISensorCallback> & callbackObj)109 void HdfSensorDumpTest::Register(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
110 {
111 SENSOR_TRACE;
112 SensorClientsManager::GetInstance()->ReportDataCbRegister(groupId, g_serviceId, callbackObj);
113 }
114
Unregister(int32_t groupId,const sptr<ISensorCallback> & callbackObj)115 void HdfSensorDumpTest::Unregister(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
116 {
117 SENSOR_TRACE;
118 SensorClientsManager::GetInstance()->ReportDataCbUnRegister(groupId, g_serviceId, callbackObj);
119 }
120
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)121 void HdfSensorDumpTest::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
122 {
123 SENSOR_TRACE;
124 SensorClientsManager::GetInstance()->SetClientSenSorConfig(sensorId, g_serviceId, samplingInterval, reportInterval);
125 SensorClientsManager::GetInstance()->UpdateSensorConfig(sensorId, samplingInterval, reportInterval);
126 SensorClientsManager::GetInstance()->UpdateClientPeriodCount(sensorId, samplingInterval, reportInterval);
127 }
128
Enable(int32_t sensorId)129 void HdfSensorDumpTest::Enable(int32_t sensorId)
130 {
131 SENSOR_TRACE;
132 SensorClientsManager::GetInstance()->OpenSensor(sensorId, g_serviceId);
133 }
134
Disable(int32_t sensorId)135 void HdfSensorDumpTest::Disable(int32_t sensorId)
136 {
137 SENSOR_TRACE;
138 SensorClientsManager::GetInstance()->IsUpadateSensorState(sensorId, g_serviceId, 0);
139 }
140
OnDataEvent(const V2_0::HdfSensorEvents & event)141 void HdfSensorDumpTest::OnDataEvent(const V2_0::HdfSensorEvents& event)
142 {
143 SENSOR_TRACE;
144 SensorClientsManager::GetInstance()->CopyEventData(event);
145 }
146
PrintDumpResult(struct HdfSBuf * reply)147 void HdfSensorDumpTest::PrintDumpResult(struct HdfSBuf* reply)
148 {
149 SENSOR_TRACE;
150 const char* value = nullptr;
151 while ((value = HdfSbufReadString(reply)) != nullptr) {
152 printf("%s", value);
153 }
154 }
155
156 /**
157 * @tc.name: SensorDumpHelpTest
158 * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -h'"
159 * @tc.type: FUNC
160 * @tc.require: #I4L3LF
161 */
162 HWTEST_F(HdfSensorDumpTest, SensorDumpHelpTest, TestSize.Level1)
163 {
164 SENSOR_TRACE;
165 struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
166 struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
167 HdfSbufWriteUint32(data, INIT_DATA_NUM);
168 HdfSbufWriteString(data, "-h");
169 GetSensorDump(data, reply);
170 PrintDumpResult(reply);
171 }
172
173 /**
174 * @tc.name: SensorShowClientTest
175 * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -c'"
176 * @tc.type: FUNC
177 * @tc.require: #I4L3LF
178 */
179 HWTEST_F(HdfSensorDumpTest, SensorShowClientTest, TestSize.Level1)
180 {
181 SENSOR_TRACE;
182 GetAllSensorInfo(g_info);
183 Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
184 for (auto it : g_info) {
185 SetBatch(it.sensorId, REPORT_INTERVAL, SAMPLING_INTERVAL);
186 Enable(it.sensorId);
187 }
188
189 struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
190 struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
191 HdfSbufWriteUint32(data, INIT_DATA_NUM);
192 HdfSbufWriteString(data, "-c");
193 GetSensorDump(data, reply);
194
195 PrintDumpResult(reply);
196
197 Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
198 }
199
200 /**
201 * @tc.name: SensorShowDataTest
202 * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -d'"
203 * @tc.type: FUNC
204 * @tc.require: #I4L3LF
205 */
206 HWTEST_F(HdfSensorDumpTest, SensorShowDataTest, TestSize.Level1)
207 {
208 SENSOR_TRACE;
209 Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
210
211 V2_0::HdfSensorEvents event;
212 for (auto it : g_info) {
213 g_event.sensorId = it.sensorId;
214 OnDataEvent(g_event);
215 }
216
217 struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
218 struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
219 HdfSbufWriteUint32(data, INIT_DATA_NUM);
220 HdfSbufWriteString(data, "-d");
221 GetSensorDump(data, reply);
222 PrintDumpResult(reply);
223
224 Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
225 }
226
227 /**
228 * @tc.name: SensorShowListTest
229 * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -l'"
230 * @tc.type: FUNC
231 * @tc.require: #I4L3LF
232 */
233 HWTEST_F(HdfSensorDumpTest, SensorShowListTest, TestSize.Level1)
234 {
235 SENSOR_TRACE;
236 struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
237 struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
238 HdfSbufWriteUint32(data, INIT_DATA_NUM);
239 HdfSbufWriteString(data, "-l");
240 GetSensorDump(data, reply);
241 PrintDumpResult(reply);
242 }