• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_1/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_1;
32 using namespace testing::ext;
33 
34 namespace {
35     sptr<V2_0::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<V2_0::ISensorCallback> &callbackObj);
61         void Unregister(int32_t groupId, const sptr<V2_0::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<V2_0::ISensorCallback> & callbackObj)109     void HdfSensorDumpTest::Register(int32_t groupId, const sptr<V2_0::ISensorCallback> &callbackObj)
110     {
111         SENSOR_TRACE;
112         SensorClientsManager::GetInstance()->ReportDataCbRegister(groupId, g_serviceId, callbackObj);
113     }
114 
Unregister(int32_t groupId,const sptr<V2_0::ISensorCallback> & callbackObj)115     void HdfSensorDumpTest::Unregister(int32_t groupId, const sptr<V2_0::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,
125             reportInterval);
126         SensorClientsManager::GetInstance()->UpdateSensorConfig(sensorId, samplingInterval, reportInterval);
127         SensorClientsManager::GetInstance()->UpdateClientPeriodCount(sensorId, samplingInterval, reportInterval);
128     }
129 
Enable(int32_t sensorId)130     void HdfSensorDumpTest::Enable(int32_t sensorId)
131     {
132         SENSOR_TRACE;
133         SensorClientsManager::GetInstance()->OpenSensor(sensorId, g_serviceId);
134     }
135 
Disable(int32_t sensorId)136     void HdfSensorDumpTest::Disable(int32_t sensorId)
137     {
138         SENSOR_TRACE;
139         SensorClientsManager::GetInstance()->IsUpadateSensorState(sensorId, g_serviceId, 0);
140     }
141 
OnDataEvent(const V2_0::HdfSensorEvents & event)142     void HdfSensorDumpTest::OnDataEvent(const V2_0::HdfSensorEvents& event)
143     {
144         SENSOR_TRACE;
145         SensorClientsManager::GetInstance()->CopyEventData(event);
146     }
147 
PrintDumpResult(struct HdfSBuf * reply)148     void HdfSensorDumpTest::PrintDumpResult(struct HdfSBuf* reply)
149     {
150         SENSOR_TRACE;
151         const char* value = nullptr;
152         while ((value = HdfSbufReadString(reply)) != nullptr) {
153             printf("%s", value);
154         }
155     }
156 
157     /**
158      * @tc.name: SensorDumpHelpTest
159      * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -h'"
160      * @tc.type: FUNC
161      * @tc.require: #I4L3LF
162      */
163     HWTEST_F(HdfSensorDumpTest, SensorDumpHelpTest, TestSize.Level1)
164     {
165         SENSOR_TRACE;
166         struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
167         struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
168         HdfSbufWriteUint32(data, INIT_DATA_NUM);
169         HdfSbufWriteString(data, "-h");
170         int32_t ret = GetSensorDump(data, reply);
171         EXPECT_EQ(HDF_SUCCESS, ret);
172         PrintDumpResult(reply);
173     }
174 
175     /**
176      * @tc.name: SensorShowClientTest
177      * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -c'"
178      * @tc.type: FUNC
179      * @tc.require: #I4L3LF
180      */
181     HWTEST_F(HdfSensorDumpTest, SensorShowClientTest, TestSize.Level1)
182     {
183         SENSOR_TRACE;
184         GetAllSensorInfo(g_info);
185         Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
186         for (auto it : g_info) {
187             SetBatch(it.sensorId, REPORT_INTERVAL, SAMPLING_INTERVAL);
188             Enable(it.sensorId);
189         }
190 
191         struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
192         struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
193         HdfSbufWriteUint32(data, INIT_DATA_NUM);
194         HdfSbufWriteString(data, "-c");
195         int32_t ret = GetSensorDump(data, reply);
196         EXPECT_EQ(HDF_SUCCESS, ret);
197         PrintDumpResult(reply);
198 
199         Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
200     }
201 
202     /**
203      * @tc.name: SensorShowDataTest
204      * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -d'"
205      * @tc.type: FUNC
206      * @tc.require: #I4L3LF
207      */
208     HWTEST_F(HdfSensorDumpTest, SensorShowDataTest, TestSize.Level1)
209     {
210         SENSOR_TRACE;
211         Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
212 
213         V2_0::HdfSensorEvents event;
214         for (auto it : g_info) {
215             g_event.sensorId = it.sensorId;
216             OnDataEvent(g_event);
217         }
218 
219         struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
220         struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
221         HdfSbufWriteUint32(data, INIT_DATA_NUM);
222         HdfSbufWriteString(data, "-d");
223         int32_t ret = GetSensorDump(data, reply);
224         EXPECT_EQ(HDF_SUCCESS, ret);
225         PrintDumpResult(reply);
226 
227         Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
228     }
229 
230     /**
231      * @tc.name: SensorShowListTest
232      * @tc.desc: hdc shell "hidumper -s 5100 -a '-host sensor_host -l'"
233      * @tc.type: FUNC
234      * @tc.require: #I4L3LF
235      */
236     HWTEST_F(HdfSensorDumpTest, SensorShowListTest, TestSize.Level1)
237     {
238         SENSOR_TRACE;
239         struct HdfSBuf* reply = HdfSbufTypedObtain(SBUF_IPC);
240         struct HdfSBuf* data = HdfSbufTypedObtain(SBUF_IPC);
241         HdfSbufWriteUint32(data, INIT_DATA_NUM);
242         HdfSbufWriteString(data, "-l");
243         int32_t ret = GetSensorDump(data, reply);
244         EXPECT_EQ(HDF_SUCCESS, ret);
245         PrintDumpResult(reply);
246     }
247 }