diff --git a/README.md b/README.md index fe90d5a778fc6d42d16ee02cde223641d6ec269d..cdada0161cb6edd825183f0b6342c1d4cfe847d3 100755 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ The sensor JS APIs listen for sensor data changes. If an API is called multiple 1. Import the sensor package. 2. Subscribe to and listen for data changes of an acceleration sensor. 3. Unsubscribe from data changes of the acceleration sensor. -4. Subscribe to and listen for a data change of a acceleration sensor. +4. Subscribe to and listen for a data change of an acceleration sensor. Example: diff --git a/frameworks/native/sensor/include/sensor_data_channel.h b/frameworks/native/sensor/include/sensor_data_channel.h index 8a9bb96d649ee7fb93dca29cef5214637e86981d..ecf87bac672e816fa1cf0d4d350a88a7f9817c2d 100644 --- a/frameworks/native/sensor/include/sensor_data_channel.h +++ b/frameworks/native/sensor/include/sensor_data_channel.h @@ -19,6 +19,7 @@ #include #include +#include "sensor_agent_type.h" #include "sensor_event_handler.h" #include "sensor_basic_data_channel.h" diff --git a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h index 165ba763029db259ad4121f83d8f151b02cedae0..770c1dae5aa13e3b5354f3a2e4aeec582c4d3ff2 100644 --- a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h @@ -35,7 +35,7 @@ public: private: SensorDataChannel* channel_; - TransferSensorEvents *receiveDataBuff_ = nullptr; + SensorData *receiveDataBuff_ = nullptr; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/include/sensor_service_client.h b/frameworks/native/sensor/include/sensor_service_client.h index 38c29496254fc7b8bf22ea06e62ea6ca6654caad..e964c03dec64e556d9d6607103d50bc8af2b29f9 100755 --- a/frameworks/native/sensor/include/sensor_service_client.h +++ b/frameworks/native/sensor/include/sensor_service_client.h @@ -36,7 +36,7 @@ namespace Sensors { class SensorServiceClient : public Singleton { public: std::vector GetSensorList(); - int32_t EnableSensor(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); + int32_t EnableSensor(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay); int32_t DisableSensor(uint32_t sensorId); int32_t RunCommand(uint32_t sensorId, int32_t cmdType, int32_t parms); int32_t TransferDataChannel(sptr sensorDataChannel); @@ -46,7 +46,7 @@ public: private: int32_t InitServiceClient(); - void UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeroid, int64_t maxReportDelay); + void UpdateSensorInfoMap(uint32_t sensorId, int64_t samplingPeriod, int64_t maxReportDelay); void DeleteSensorInfoItem(uint32_t sensorId); std::mutex clientMutex_; sptr serviceDeathObserver_; diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index 7b28161cdaf8042a64f53dd307d59d4e68446dbd..23cfa0672cdf52dca6745ffc92660250444df5db 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -98,8 +98,8 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() const } ret = SenClient.TransferDataChannel(dataChannel_); if (ret != ERR_OK) { - auto destoryRet = dataChannel_->DestroySensorDataChannel(); - SEN_HILOGE("transfer data channel failed, ret:%{public}d,destoryRet:%{public}d", ret, destoryRet); + auto destroyRet = dataChannel_->DestroySensorDataChannel(); + SEN_HILOGE("transfer data channel failed, ret:%{public}d,destroyRet:%{public}d", ret, destroyRet); return ret; } g_isChannelCreated = true; @@ -117,12 +117,12 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() const CHKPR(dataChannel_, INVALID_POINTER); int32_t ret = dataChannel_->DestroySensorDataChannel(); if (ret != ERR_OK) { - SEN_HILOGE("destory data channel failed, ret:%{public}d", ret); + SEN_HILOGE("destroy data channel failed, ret:%{public}d", ret); return ret; } ret = SenClient.DestroyDataChannel(); if (ret != ERR_OK) { - SEN_HILOGE("destory service data channel fail, ret:%{public}d", ret); + SEN_HILOGE("destroy service data channel fail, ret:%{public}d", ret); return ret; } g_isChannelCreated = false; @@ -135,7 +135,7 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use CHKPR(user->callback, OHOS::Sensors::ERROR); std::lock_guard subscribeLock(subscribeMutex_); if (g_samplingInterval < 0 || g_reportInterval < 0) { - SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); + SEN_HILOGE("samplingPeriod or g_reportInterval is invalid"); return ERROR; } if (!SenClient.IsValid(sensorId)) { @@ -238,7 +238,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * if (g_subscribeMap.empty()) { int32_t ret = DestroySensorDataChannel(); if (ret != ERR_OK) { - SEN_HILOGE("destory data channel fail, ret:%{public}d", ret); + SEN_HILOGE("destroy data channel fail, ret:%{public}d", ret); return ret; } } @@ -301,25 +301,26 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); CHKPR(sensorInfos_, ERROR); for (size_t i = 0; i < count; ++i) { - errno_t ret = strcpy_s((sensorInfos_ + i)->sensorName, NAME_MAX_LEN, + SensorInfo *sensorInfo = sensorInfos_ + i; + errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN, sensorList[i].GetSensorName().c_str()); CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->vendorName, NAME_MAX_LEN, + ret = strcpy_s(sensorInfo->vendorName, NAME_MAX_LEN, sensorList[i].GetVendorName().c_str()); CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->hardwareVersion, VERSION_MAX_LEN, + ret = strcpy_s(sensorInfo->hardwareVersion, VERSION_MAX_LEN, sensorList[i].GetHardwareVersion().c_str()); CHKCR(ret == EOK, ERROR); - ret = strcpy_s((sensorInfos_ + i)->firmwareVersion, VERSION_MAX_LEN, + ret = strcpy_s(sensorInfo->firmwareVersion, VERSION_MAX_LEN, sensorList[i].GetFirmwareVersion().c_str()); CHKCR(ret == EOK, ERROR); - (sensorInfos_ + i)->sensorId = static_cast(sensorList[i].GetSensorId()); - (sensorInfos_ + i)->sensorTypeId = static_cast(sensorList[i].GetSensorTypeId()); - (sensorInfos_ + i)->maxRange = sensorList[i].GetMaxRange(); - (sensorInfos_ + i)->precision = sensorList[i].GetResolution(); - (sensorInfos_ + i)->power = sensorList[i].GetPower(); - (sensorInfos_ + i)->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); - (sensorInfos_ + i)->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); + sensorInfo->sensorId = static_cast(sensorList[i].GetSensorId()); + sensorInfo->sensorTypeId = static_cast(sensorList[i].GetSensorTypeId()); + sensorInfo->maxRange = sensorList[i].GetMaxRange(); + sensorInfo->precision = sensorList[i].GetResolution(); + sensorInfo->power = sensorList[i].GetPower(); + sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); + sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); } sensorInfoCount_ = static_cast(count); return SUCCESS; diff --git a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp index 8dc136149f60440b9ee3d3a40b76c5cc23767360..e272d1d78aa18303c74884589a6ed3300677babc 100644 --- a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp @@ -33,7 +33,7 @@ SensorFileDescriptorListener::SensorFileDescriptorListener() { channel_ = nullptr; receiveDataBuff_ = - new (std::nothrow) TransferSensorEvents[sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE]; + new (std::nothrow) SensorData[sizeof(SensorData) * RECEIVE_DATA_SIZE]; CHKPL(receiveDataBuff_); } @@ -57,8 +57,8 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) return; } int32_t len = - recv(fileDescriptor, receiveDataBuff_, sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE, 0); - int32_t eventSize = static_cast(sizeof(TransferSensorEvents)); + recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0); + int32_t eventSize = static_cast(sizeof(SensorData)); while (len > 0) { int32_t num = len / eventSize; for (int i = 0; i < num; i++) { @@ -73,7 +73,7 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) }; channel_->dataCB_(&event, 1, channel_->privateData_); } - len = recv(fileDescriptor, receiveDataBuff_, sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE, 0); + len = recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0); } } diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index df26d269e3bcd3edfb2659d1a6bcbc1cd6d55779..4d41a198e82bf0a23d2e864b5b63d9eb8a05fc5b 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -209,7 +209,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object CALL_LOG_ENTER; (void)object; CHKPV(dataChannel_); - // STEP1 : Destroy revious data channel + // STEP1 : Destroy previous data channel dataChannel_->DestroySensorDataChannel(); // STEP2 : Restore data channel dataChannel_->RestoreSensorDataChannel(); diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index b0406b7a1ab6b066779538011780cffac1b1a41c..da7c39b69594c8e2ecbfc5974a0581d6026446c0 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -65,7 +65,7 @@ ErrCode SensorServiceProxy::EnableSensor(uint32_t sensorId, int64_t samplingPeri int32_t ret = remote->SendRequest(ISensorService::ENABLE_SENSOR, data, reply, option); if (ret != NO_ERROR) { HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "EnaleSensor", "ERROR_CODE", ret); + HiSysEvent::EventType::FAULT, "PKG_NAME", "EnableSensor", "ERROR_CODE", ret); SEN_HILOGE("failed, ret:%{public}d", ret); } return static_cast(ret); @@ -177,7 +177,11 @@ std::vector SensorServiceProxy::GetSensorList() SEN_HILOGE("failed, ret:%{public}d", ret); return sensors; } - int32_t sensorCount = reply.ReadInt32(); + int32_t sensorCount; + if (!reply.ReadInt32(sensorCount)) { + SEN_HILOGE("Parcel read failed"); + return sensors; + } SEN_HILOGD("sensorCount:%{public}d", sensorCount); if (sensorCount > MAX_SENSOR_COUNT) { sensorCount = MAX_SENSOR_COUNT; diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 85531a585d586719192e43621f5fc0bf4775ef02..f780e3e5e818aac4462df839dd6b713ac98a19c2 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -112,7 +112,7 @@ typedef struct SensorInfo { float precision; /**< Sensor accuracy */ float power; /**< Sensor power */ int64_t minSamplePeriod; /**< Minimum sample period allowed, in ns */ - int64_t maxSamplePeriod; /**< Maxmum sample period allowed, in ns */ + int64_t maxSamplePeriod; /**< Maximum sample period allowed, in ns */ } SensorInfo; /** @@ -126,7 +126,7 @@ typedef struct SensorEvent { int64_t timestamp; /**< Time when sensor data was reported */ uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ - uint8_t *data; /**< Sensor data */ + uint8_t *data = nullptr; /**< Sensor data */ uint32_t dataLen; /**< Sensor data length */ } SensorEvent; @@ -154,7 +154,7 @@ typedef struct UserData { typedef struct SensorUser { char name[NAME_MAX_LEN]; /**< Name of the sensor data subscriber */ RecordSensorCallback callback; /**< Callback for reporting sensor data */ - UserData *userData; /**< Reserved field for the sensor data subscriber */ + UserData *userData = nullptr; /**< Reserved field for the sensor data subscriber */ } SensorUser; /** diff --git a/interfaces/native/test/unittest/sensor_agent_test.cpp b/interfaces/native/test/unittest/sensor_agent_test.cpp index 4dc06b25735c79aa8be106cecacbd2711b45246d..412af3711de19502d9539f94a9ffd770d26a30bc 100755 --- a/interfaces/native/test/unittest/sensor_agent_test.cpp +++ b/interfaces/native/test/unittest/sensor_agent_test.cpp @@ -326,7 +326,7 @@ HWTEST_F(SensorAgentTest, SetModeTest_003, TestSize.Level1) * Function: SubscribeSensor * FunctionPoints: Check the interface function * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the senser service framework process. + * CaseDescription: Verify the sensor service framework process. */ HWTEST_F(SensorAgentTest, SensorNativeApiTest_001, TestSize.Level1) { @@ -361,7 +361,7 @@ HWTEST_F(SensorAgentTest, GetProcCpuUsageTest_001, TestSize.Level1) const std::string process_name = "sensors"; auto usage = cpuInfo.GetProcCpuUsage(process_name); SEN_HILOGD("The CPU usage of the %{public}s process is %{public}.2f", process_name.c_str(), usage); - ASSERT_TRUE(usage < SYSTEM_INFO::CPU_USAGE_LOAD && usage != SYSTEM_INFO::CPU_USAGE_UNKONW); + ASSERT_TRUE(usage < SYSTEM_INFO::CPU_USAGE_LOAD && usage != SYSTEM_INFO::CPU_USAGE_UNKNOWN); } /* @@ -369,7 +369,7 @@ HWTEST_F(SensorAgentTest, GetProcCpuUsageTest_001, TestSize.Level1) * Function: GetAllSensors * FunctionPoints: Check the interface function * EnvConditions: mobile that can run ohos test framework - * CaseDescription: Verify the senser service framework process. + * CaseDescription: Verify the sensor service framework process. */ HWTEST_F(SensorAgentTest, SensorListTest_001, TestSize.Level1) { diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index 5b3f39094f2d7aaae44d51c8c1f8f222cc16f4dd..d84452593cb927399ef09017acafd3ecc0080477 100644 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -25,7 +25,7 @@ ohos_shared_library("libsensor") { "//foundation/arkui/napi/interfaces/inner_api/napi", ] defines = [ - "APP_LOG_TAG = \"sensroJs\"", + "APP_LOG_TAG = \"sensorJs\"", "LOG_DOMAIN = 0xD002700", ] sources = [ diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 2d515f213513d9b470ce0ae31ae4a4c4ab3d56db..5119e5cb0b4fa33dffcd0cfea2b45f57dc1b866f 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -41,7 +41,7 @@ enum CallbackDataType { ONCE_CALLBACK = 2, GET_GEOMAGNETIC_FIELD = 3, GET_ALTITUDE = 4, - GET_GEOMAGNITIC_DIP = 5, + GET_GEOMAGNETIC_DIP = 5, GET_ANGLE_MODIFY = 6, CREATE_ROTATION_MATRIX = 7, TRANSFORM_COORDINATE_SYSTEM = 8, @@ -121,10 +121,6 @@ public: napi_delete_reference(env, callback[i]); } } - if (work != nullptr) { - delete work; - work = nullptr; - } } private: diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index c87bb402b5c270b25c7cc5f51f19247e3c2ae99d..1f374d97808c594afd92df02ffef0345a0d57cbb 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -40,7 +40,7 @@ constexpr int32_t INVALID_SENSOR_ID = -1; constexpr int32_t SENSOR_SUBSCRIBE_FAILURE = 1001; constexpr int32_t INPUT_ERROR = 202; constexpr float BODY_STATE_EXCEPT = 1.0f; -constexpr float THREESHOLD = 0.000001f; +constexpr float THRESHOLD = 0.000001f; } static std::map g_samplingPeriod = { {"normal", 200000000}, @@ -77,10 +77,11 @@ static bool copySensorData(sptr callbackInfo, SensorEvent *ev std::lock_guard onBodyLock(bodyMutex_); g_bodyState = *data; callbackInfo->data.sensorData.data[0] = - (fabs(g_bodyState - BODY_STATE_EXCEPT) < THREESHOLD) ? true : false; + (fabs(g_bodyState - BODY_STATE_EXCEPT) < THRESHOLD) ? true : false; return true; } - if (memcpy_s(callbackInfo->data.sensorData.data, event->dataLen, data, event->dataLen) != EOK) { + if (memcpy_s(callbackInfo->data.sensorData.data, sizeof(callbackInfo->data.sensorData.data), + data, event->dataLen) != EOK) { SEN_HILOGE("Copy data failed"); return false; } @@ -137,14 +138,17 @@ static void EmitOnceCallback(SensorEvent *event) if (iter == g_onceCallbackInfos.end()) { return; } - for (auto &onceCallbackInfo : iter->second) { + auto& onceCallbackInfos = iter->second; + while (!onceCallbackInfos.empty()) { + auto onceCallbackInfo = onceCallbackInfos.front(); + auto beginIter = onceCallbackInfos.begin(); + onceCallbackInfos.erase(beginIter); if (!copySensorData(onceCallbackInfo, event)) { SEN_HILOGE("Copy sensor data failed"); continue; } EmitUvEventLoop(onceCallbackInfo); } - g_onceCallbackInfos[sensorTypeId].clear(); g_onceCallbackInfos.erase(sensorTypeId); CHKCV((!CheckSubscribe(sensorTypeId)), "Has client subscribe, not need cancel subscribe"); @@ -843,7 +847,7 @@ static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info) return nullptr; } sptr asyncCallbackInfo = - new (std::nothrow) AsyncCallbackInfo(env, GET_GEOMAGNITIC_DIP); + new (std::nothrow) AsyncCallbackInfo(env, GET_GEOMAGNETIC_DIP); CHKPP(asyncCallbackInfo); std::vector inclinationMatrix; if (!GetFloatArray(env, args[0], inclinationMatrix)) { @@ -1212,7 +1216,7 @@ napi_value GetBodyState(napi_env env, napi_callback_info info) "register success callback fail"); std::lock_guard onBodyLock(bodyMutex_); asyncCallbackInfo->data.sensorData.data[0] = - (fabs(g_bodyState - BODY_STATE_EXCEPT) < THREESHOLD) ? true : false; + (fabs(g_bodyState - BODY_STATE_EXCEPT) < THRESHOLD) ? true : false; EmitUvEventLoop(asyncCallbackInfo); return nullptr; } diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 4cfecb6827b2522fc6282b6e9610e95323881325..86f749120ba1578ac843189e3f6e999644000602 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -187,7 +187,7 @@ std::map g_convertfuncList = { {ONCE_CALLBACK, ConvertToSensorData}, {GET_GEOMAGNETIC_FIELD, ConvertToGeomagneticData}, {GET_ALTITUDE, ConvertToNumber}, - {GET_GEOMAGNITIC_DIP, ConvertToNumber}, + {GET_GEOMAGNETIC_DIP, ConvertToNumber}, {GET_ANGLE_MODIFY, ConvertToArray}, {CREATE_ROTATION_MATRIX, ConvertToArray}, {TRANSFORM_COORDINATE_SYSTEM, ConvertToArray}, @@ -318,8 +318,8 @@ bool ConvertToSensorData(const napi_env &env, sptr asyncCallb return ConvertToBodyData(env, asyncCallbackInfo, result); } size_t size = g_sensorAttributeList[sensorTypeId].size(); - uint32_t dataLenth = asyncCallbackInfo->data.sensorData.dataLength / sizeof(float); - CHKNCF(env, (size <= dataLenth), "Data length mismatch"); + uint32_t dataLength = asyncCallbackInfo->data.sensorData.dataLength / sizeof(float); + CHKNCF(env, (size <= dataLength), "Data length mismatch"); CHKNRF(env, napi_create_object(env, &result[1]), "napi_create_object"); napi_value message = nullptr; @@ -434,7 +434,7 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) sptr asyncCallbackInfo(static_cast(data)); /** * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the * asynchronous task is created, and subtract 1 from the reference count after the naked * pointer is converted to a pointer when the asynchronous task is executed, the reference * count of the smart pointer is guaranteed to be 1. @@ -471,7 +471,7 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) } } -void freeWork(uv_work_t *work) +void DeleteWork(uv_work_t *work) { CHKPV(work); delete work; @@ -486,15 +486,15 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(loop); uv_work_t *work = new(std::nothrow) uv_work_t; CHKPV(work); - asyncCallbackInfo->work = work; asyncCallbackInfo->IncStrongRef(nullptr); work->data = asyncCallbackInfo.GetRefPtr(); int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { CHKPV(work); sptr asyncCallbackInfo(static_cast(work->data)); + DeleteWork(work); /** * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the * asynchronous task is created, and subtract 1 from the reference count after the naked * pointer is converted to a pointer when the asynchronous task is executed, the reference * count of the smart pointer is guaranteed to be 1. @@ -530,14 +530,11 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) return; } napi_close_handle_scope(asyncCallbackInfo->env, scope); - asyncCallbackInfo->work = nullptr; - freeWork(work); }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); asyncCallbackInfo->DecStrongRef(nullptr); - asyncCallbackInfo->work = nullptr; - freeWork(work); + DeleteWork(work); } } @@ -557,7 +554,7 @@ void EmitPromiseWork(sptr asyncCallbackInfo) sptr asyncCallbackInfo(static_cast(data)); /** * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the * asynchronous task is created, and subtract 1 from the reference count after the naked * pointer is converted to a pointer when the asynchronous task is executed, the reference * count of the smart pointer is guaranteed to be 1. diff --git a/interfaces/plugin/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/test/unittest/ExampleJsunit.test.js index 3ab3807c9233f15f752c173507ef308428fa2a46..c235c0f25a4f384d48b5e2fcda3a63cb7716e807 100755 --- a/interfaces/plugin/test/unittest/ExampleJsunit.test.js +++ b/interfaces/plugin/test/unittest/ExampleJsunit.test.js @@ -33,21 +33,21 @@ describe("SensorJsTest", function () { */ console.info('beforeAll caled') }) - + afterAll(function() { /* * @tc.teardown: teardown invoked after all testcases */ console.info('afterAll caled') }) - + beforeEach(function() { /* * @tc.setup: setup invoked before each testcases */ console.info('beforeEach caled') }) - + afterEach(function() { /* * @tc.teardown: teardown invoked after each testcases @@ -1078,7 +1078,7 @@ describe("SensorJsTest", function () { */ it("Sensor_GetGeomagneticField_025", 0, async function (done) { console.info("---------------------------Sensor_GetGeomagneticField_025----------------------------------"); - for (var i = 0; i < timeMillis.length; i++) { + for (let i = 0; i < timeMillis.length; i++) { await sensor.getGeomagneticInfo({'latitude':80, 'longitude':0, 'altitude':0}, timeMillis[i]).then((data) => { console.info('Sensor_GetGeomagneticField_025 x: ' + data.x + ',y: ' + data.y + ',z: ' + data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity); @@ -1343,7 +1343,7 @@ describe("SensorJsTest", function () { * @tc.require: Issue Number */ it('Sensor_GetGeomagneticField_037', 0, async function (done) { - for (var i = 0; i < timeMillis.length; i++) { + for (let i = 0; i < timeMillis.length; i++) { console.info('----------------------Sensor_GetGeomagneticField_037---------------------------'); await sensor.getGeomagneticInfo({'latitude':80, 'longitude':0, 'altitude':0}, timeMillis[i]).then((data) => { console.info('Sensor_GetGeomagneticField_037 x: ' + data.x + ',y: ' + data.y + ',z: ' + data.z + ',geomagneticDip: ' + data.geomagneticDip @@ -1460,7 +1460,7 @@ describe("SensorJsTest", function () { */ it('Sensor_GetGeomagneticField_043', 0, async function (done) { console.info('----------------------Sensor_GetGeomagneticField_043---------------------------'); - for (var i = 0; i < GEOMAGNETIC_COORDINATES.length; i++) { + for (let i = 0; i < GEOMAGNETIC_COORDINATES.length; i++) { await sensor.getGeomagneticInfo({'latitude':GEOMAGNETIC_COORDINATES[i][0], 'longitude':GEOMAGNETIC_COORDINATES[i][1], 'altitude':GEOMAGNETIC_COORDINATES[i][2]}, timeMillis[0]).then((data) => { console.info('Sensor_GetGeomagneticField_043 x: ' + data.x + ',y: ' + data.y + ',z: ' + data.z + ',geomagneticDip: ' + data.geomagneticDip + ',deflectionAngle: ' + data.deflectionAngle + ',levelIntensity: ' + data.levelIntensity + ',totalIntensity: ' + data.totalIntensity) @@ -1762,7 +1762,7 @@ describe("SensorJsTest", function () { } }) - var SENSOR_DATA_MATRIX = [ + const SENSOR_DATA_MATRIX = [ { "rotation": [-0.7980074882507324, 0.5486301183700562, 0.24937734007835388, -0.17277367413043976, -0.6047078967094421, 0.7774815559387207, 0.5773502588272095, 0.5773502588272095,0.5773502588272095], @@ -1786,7 +1786,7 @@ describe("SensorJsTest", function () { "rotation":[null, null, null, null, null, null,0, 0, 0], "inclination":[1, 0, 0, 0, null, 0, 0, 0, null] } - ] + ]; let GET_DIRECTION = [ [ 0.38050639629364014, -0.9783217310905457, -0.6610431671142578], @@ -2052,7 +2052,7 @@ describe("SensorJsTest", function () { ] /* * @tc.name: Sensor_CreateRotationMatrix_012 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -2064,7 +2064,7 @@ describe("SensorJsTest", function () { console.info('Sensor_CreateRotationMatrix_012 failed'); expect(false).assertTrue(); } else { - for(var i = 0;i < data.length; i++) { + for(let i = 0;i < data.length; i++) { console.info("Sensor_CreateRotationMatrix_012 [" + i + ") = " + data[i]); expect(data[i]).assertEqual(createRotationMatrixResult[0][i]) } @@ -2076,14 +2076,14 @@ describe("SensorJsTest", function () { /* * tc.name: Sensor_CreateRotationMatrix_013 - * tc.desc: Verfication results of the incorrect parameters of test interface. + * tc.desc: Verification results of the incorrect parameters of test interface. * tc.require: SR000GH2A2 * @tc.author: */ it('Sensor_CreateRotationMatrix_013', 0,async function (done) { console.info('Sensor_CreateRotationMatrix_013 start') sensor.getRotationMatrix([-0.0245, 0.402, 0.0465]).then((data) => { - for(var i = 0;i < data.length; i++) { + for(let i = 0;i < data.length; i++) { console.info("Sensor_CreateRotationMatrix_013 [" + i + "] : " + data[i]); expect(data[i]).assertEqual(createRotationMatrixResult[0][i]) } @@ -2098,14 +2098,14 @@ describe("SensorJsTest", function () { /* * tc.name: Sensor_CreateRotationMatrix_014 - * tc.desc: Verfication results of the incorrect parameters of test interface. + * tc.desc: Verification results of the incorrect parameters of test interface. * tc.require: I5SWJI * @tc.author: */ it('Sensor_CreateRotationMatrix_014', 0,async function (done) { console.info('Sensor_CreateRotationMatrix_014 start') sensor.getRotationMatrix([0, 0, 0]).then((data) => { - for(var i = 0;i < data.length; i++) { + for(let i = 0;i < data.length; i++) { console.info("Sensor_CreateRotationMatrix_014 [" + i + "] : " + data[i]); expect(data[i]).assertEqual(createRotationMatrixResult[1][i]) } @@ -2132,7 +2132,7 @@ describe("SensorJsTest", function () { console.info('Sensor_GetDirection_001 failed'); expect(false).assertTrue(); } else { - for (var i = 1; i < data.length; i++) { + for (let i = 1; i < data.length; i++) { console.info("Sensor_GetDirection_001" + data[i]) expect(data[i]).assertEqual(GET_DIRECTION[0][i]) } @@ -2155,7 +2155,7 @@ describe("SensorJsTest", function () { console.info('Sensor_GetDirection_002 failed'); expect(false).assertTrue(); } else { - for (var i = 1; i < data.length; i++) { + for (let i = 1; i < data.length; i++) { console.info("Sensor_GetDirection_002" + data[i]) expect(data[i]).assertEqual(GET_DIRECTION[1][i]) } @@ -2174,7 +2174,7 @@ describe("SensorJsTest", function () { */ it('Sensor_GetDirection_003', 0, async function (done) { sensor.getOrientation(rotationMatrix[0]).then((data) => { - for (var i = 0; i { - for (var i = 0; i { - for (var i = 0; i { - for(var i = 0; i < data.length; i++) { + for(let i = 0; i < data.length; i++) { console.info("Sensor_GetAngleModify_006 [" + i + "] = " + data[i]); expect(data[0]).assertEqual(ANGLECHANGE_9_RESULT[0][0]) && expect(Number.isNaN(data[1])).assertTrue() && expect(data[2]).assertEqual(ANGLECHANGE_9_RESULT[0][2]); @@ -2534,7 +2534,7 @@ describe("SensorJsTest", function () { console.info("Sensor_GetAngleModify_007 in"); sensor.getAngleVariation([3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38], [3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38, 3.40282e+38]).then((data) => { - for(var i = 0; i < data.length; i++) { + for(let i = 0; i < data.length; i++) { console.info("Sensor_GetAngleModify_007 [" + i + "] = " + data[i]); expect(data[0]).assertEqual(ANGLECHANGE_9_RESULT[1][0]) && expect(Number.isNaN(data[1])).assertTrue() && expect(data[2]).assertEqual(ANGLECHANGE_9_RESULT[1][2]); } @@ -2557,7 +2557,7 @@ describe("SensorJsTest", function () { console.info("Sensor_GetAngleModify_008 in"); sensor.getAngleVariation([1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38], [1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38, 1.17549e-38]).then((data) => { - for(var i = 0; i < data.length; i++) { + for(let i = 0; i < data.length; i++) { console.info("Sensor_GetAngleModify_008 [" + i + "] = " + data[i]); expect(data[0]).assertEqual(ANGLECHANGE_9_RESULT[2][0]) && expect(data[1]).assertEqual(ANGLECHANGE_9_RESULT[2][1]) @@ -2583,7 +2583,7 @@ describe("SensorJsTest", function () { sensor.getAngleVariation([3.40282e+38 + 1,3.40282e+38 + 1,3.40282e+38 + 1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1], [3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1,3.40282e+38+1]) .then((data) => { - for(var i = 0; i < data.length; i++) { + for(let i = 0; i < data.length; i++) { console.info("Sensor_GetAngleModify_009 [" + i + "] = " + data[i]); expect(data[0]).assertEqual(ANGLECHANGE_9_RESULT[3][0]) && expect(Number.isNaN(data[1])).assertTrue() && expect(data[2]).assertEqual(ANGLECHANGE_9_RESULT[3][2]); } @@ -2606,7 +2606,7 @@ describe("SensorJsTest", function () { console.info("Sensor_GetAngleModify_010 in"); sensor.getAngleVariation([0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0], [0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0]).then((data) => { - for(var i = 0; i < data.length; i++) { + for(let i = 0; i < data.length; i++) { console.info("Sensor_GetAngleModify_010 [" + i + "] = " + data[i]); expect(Number.isNaN(data[0]) && Number.isNaN(data[1]) && Number.isNaN(data[2])).assertTrue(); } @@ -2749,29 +2749,29 @@ describe("SensorJsTest", function () { } }) - var result = [ + const result = [ [0.7441122531890869, 0.5199999809265137, -0.335999995470047, -0.25099998712539673], [0, 3.402820018375656e+38, 3.402820018375656e+38, 3.402820018375656e+38], [1, 0, 0, 0], [0.7183529734611511, -0.32499998807907104, -0.5619999766349792, -0.25], [0, 0, 0, 0] - ] + ]; /* - * @tc.name: Sensor_CreateQuaterniont_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_001 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_001', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_001 start') + it('Sensor_CreateQuaternion_001', 0,async function (done) { + console.info('Sensor_CreateQuaternion_001 start') sensor.getQuaternion([0.52, -0.336, -0.251], (error, data) =>{ - console.info('Sensor_CreateQuaterniont_001' + 'lengh:' + data.length); + console.info('Sensor_CreateQuaternion_001' + 'length:' + data.length); if (error) { - console.info('Sensor_CreateQuaterniont_001 failed'); + console.info('Sensor_CreateQuaternion_001 failed'); expect(false).assertTrue(); } else { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]) expect(data[i]).assertEqual(result[0][i]) } @@ -2781,19 +2781,19 @@ describe("SensorJsTest", function () { }) /* - * @tc.name: Sensor_CreateQuaterniont_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_002 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_002', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_002 start') + it('Sensor_CreateQuaternion_002', 0,async function (done) { + console.info('Sensor_CreateQuaternion_002 start') sensor.getQuaternion([3.40282e+38, 3.40282e+38, 3.40282e+38], (error, data) =>{ if (error) { - console.info('Sensor_CreateQuaterniont_002 failed'); + console.info('Sensor_CreateQuaternion_002 failed'); expect(false).assertTrue(); } else { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]) expect(data[i]).assertEqual(result[1][i]) } @@ -2803,59 +2803,59 @@ describe("SensorJsTest", function () { }) /* - * @tc.name: Sensor_CreateQuaterniont_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_003 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_003', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_003 start') + it('Sensor_CreateQuaternion_003', 0,async function (done) { + console.info('Sensor_CreateQuaternion_003 start') sensor.getQuaternion([0, 0, 0], (error, data) =>{ if (error) { - console.info('Sensor_CreateQuaterniont_003 failed'); + console.info('Sensor_CreateQuaternion_003 failed'); expect(false).assertTrue(); } else { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]) expect(data[i]).assertEqual(result[2][i]) } } done() }) - console.info("Sensor_CreateQuaterniont_003 end") + console.info("Sensor_CreateQuaternion_003 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_004 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_004', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_004 start') + it('Sensor_CreateQuaternion_004', 0,async function (done) { + console.info('Sensor_CreateQuaternion_004 start') sensor.getQuaternion([-0.325, -0.562, -0.25], (error, data) =>{ if (error) { - console.info('Sensor_CreateQuaterniont_004 failed'); + console.info('Sensor_CreateQuaternion_004 failed'); expect(false).assertTrue(); } else { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]) expect(data[i]).assertEqual(result[3][i]) } } done() }) - console.info("Sensor_CreateQuaterniont_004 end") + console.info("Sensor_CreateQuaternion_004 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_005 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_005 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_005', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_005 start') + it('Sensor_CreateQuaternion_005', 0,async function (done) { + console.info('Sensor_CreateQuaternion_005 start') try { sensor.getQuaternion([0.25, 0.14], (error, data) =>{ if (error) { @@ -2870,20 +2870,20 @@ describe("SensorJsTest", function () { expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() } - console.info("Sensor_CreateQuaterniont_005 end") + console.info("Sensor_CreateQuaternion_005 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_006 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_006 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_006', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_006 start') + it('Sensor_CreateQuaternion_006', 0,async function (done) { + console.info('Sensor_CreateQuaternion_006 start') sensor.getQuaternion([0.52, -0.336, -0.251]).then((data) => { - console.info('Sensor_CreateQuaterniont_006'); - for (var i = 0; i < data.length; i++) { + console.info('Sensor_CreateQuaternion_006'); + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]); expect(data[i]).assertEqual(result[0][i]) } @@ -2893,20 +2893,20 @@ describe("SensorJsTest", function () { console.info('promise failed') done() }) - console.info("Sensor_CreateQuaterniont_006 end") + console.info("Sensor_CreateQuaternion_006 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_007 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_007 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_007', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_007 start') + it('Sensor_CreateQuaternion_007', 0,async function (done) { + console.info('Sensor_CreateQuaternion_007 start') try { sensor.getQuaternion([0, 0]).then((data) => { - console.info('Sensor_CreateQuaterniont_007'); + console.info('Sensor_CreateQuaternion_007'); expect(false).assertTrue(); done() }, (error) => { @@ -2919,20 +2919,20 @@ describe("SensorJsTest", function () { expect(err.message).assertEqual(PARAMETER_ERROR_MSG) done() } - console.info("Sensor_CreateQuaterniont_007 end") + console.info("Sensor_CreateQuaternion_007 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_008 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_008 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_008', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_008 start') + it('Sensor_CreateQuaternion_008', 0,async function (done) { + console.info('Sensor_CreateQuaternion_008 start') sensor.getQuaternion([0, 0, 0]).then((data) => { - console.info('Sensor_CreateQuaterniont_008'); - for (var i = 0; i < data.length; i++) { + console.info('Sensor_CreateQuaternion_008'); + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]); expect(data[i]).assertEqual(result[2][i]) } @@ -2942,20 +2942,20 @@ describe("SensorJsTest", function () { console.info('promise failed') done() }) - console.info("Sensor_CreateQuaterniont_008 end") + console.info("Sensor_CreateQuaternion_008 end") }) /* - * @tc.name: Sensor_CreateQuaterniont_009 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_009 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_009', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_009 start') + it('Sensor_CreateQuaternion_009', 0,async function (done) { + console.info('Sensor_CreateQuaternion_009 start') sensor.getQuaternion([-0.325, -0.562, -0.25]).then((data) => { - console.info('Sensor_CreateQuaterniont_009'); - for (var i = 0; i < data.length; i++) { + console.info('Sensor_CreateQuaternion_009'); + for (let i = 0; i < data.length; i++) { console.info("data[" + i + "]: " + data[i]); expect(data[i]).assertEqual(result[3][i]) } @@ -2968,16 +2968,16 @@ describe("SensorJsTest", function () { }) /* - * @tc.name: Sensor_CreateQuaterniont_010 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.name: Sensor_CreateQuaternion_010 + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2RP * @tc.author: */ - it('Sensor_CreateQuaterniont_010', 0,async function (done) { - console.info('Sensor_CreateQuaterniont_010 start') + it('Sensor_CreateQuaternion_010', 0,async function (done) { + console.info('Sensor_CreateQuaternion_010 start') try { sensor.getQuaternion([0.25, 0.14]).then((data) => { - console.info('Sensor_CreateQuaterniont_010'); + console.info('Sensor_CreateQuaternion_010'); expect(false).assertTrue(); done() },(error) => { @@ -2995,12 +2995,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_011 + * @tc.name: Sensor_CreateQuaternion_011 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_011', 0, async function (done) { + it('Sensor_CreateQuaternion_011', 0, async function (done) { try { sensor.createQuaternion() } catch (err) { @@ -3013,12 +3013,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_012 + * @tc.name: Sensor_CreateQuaternion_012 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_012', 0, async function (done) { + it('Sensor_CreateQuaternion_012', 0, async function (done) { try { sensor.createQuaternion(-1) } catch (err) { @@ -3031,12 +3031,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_013 + * @tc.name: Sensor_CreateQuaternion_013 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_013', 0, async function (done) { + it('Sensor_CreateQuaternion_013', 0, async function (done) { try { sensor.createQuaternion([0.52, -0.336, -0.251], -1) } catch (err) { @@ -3049,12 +3049,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_014 + * @tc.name: Sensor_CreateQuaternion_014 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_014', 0, async function (done) { + it('Sensor_CreateQuaternion_014', 0, async function (done) { try { sensor.createQuaternion().then((data) => { expect(true).assertfalse() @@ -3073,12 +3073,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_015 + * @tc.name: Sensor_CreateQuaternion_015 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_015', 0, async function (done) { + it('Sensor_CreateQuaternion_015', 0, async function (done) { try { sensor.createQuaternion(-1).then((data) => { expect(true).assertfalse() @@ -3097,12 +3097,12 @@ describe("SensorJsTest", function () { /* * test * - * @tc.name: Sensor_CreateQuaterniont_016 + * @tc.name: Sensor_CreateQuaternion_016 * @tc.desc: Verification results of the incorrect parameters of the test interface. * @tc.require: I5SWJI * @tc.author: */ - it('Sensor_CreateQuaterniont_016', 0, async function (done) { + it('Sensor_CreateQuaternion_016', 0, async function (done) { try { sensor.createQuaternion([0.52, -0.336, -0.251], -1).then((data) => { expect(true).assertfalse() @@ -3118,11 +3118,11 @@ describe("SensorJsTest", function () { } }) - var getGeomagneticDipResult = [ 0.8760581016540527, 0.862170, -Infinity, 44330] + const getGeomagneticDipResult = [ 0.8760581016540527, 0.862170, -Infinity, 44330]; /* * @tc.name: Sensor_GetGeomagneticDip_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3137,14 +3137,14 @@ describe("SensorJsTest", function () { expect(data).assertEqual(getGeomagneticDipResult[0]) } done() - console.info('Sensor_GetGeomagneticDip_001' + 'lengh:' + data.length); + console.info('Sensor_GetGeomagneticDip_001' + 'length:' + data.length); }) console.info("Sensor_GetGeomagneticDip_001 end") }) /* * @tc.name: Sensor_GetGeomagneticDip_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3169,7 +3169,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetGeomagneticDip_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3188,7 +3188,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetGeomagneticDip_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3204,7 +3204,7 @@ describe("SensorJsTest", function () { expect(data).assertEqual(getGeomagneticDipResult[0]) } done() - console.info('Sensor_GetGeomagneticDip_004' + 'lengh:' + data.length); + console.info('Sensor_GetGeomagneticDip_004' + 'length:' + data.length); }) } catch(error) { console.info(error); @@ -3217,7 +3217,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetGeomagneticDip_005 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3243,7 +3243,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetGeomagneticDip_006 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3269,7 +3269,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3290,7 +3290,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3311,7 +3311,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3329,7 +3329,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2OG * @tc.author: */ @@ -3347,7 +3347,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_005 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3366,7 +3366,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_006 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3382,7 +3382,7 @@ describe("SensorJsTest", function () { expect(data).assertEqual(getGeomagneticDipResult[0]) } done() - console.info('Sensor_GetAltitude_006' + 'lengh:' + data.length); + console.info('Sensor_GetAltitude_006' + 'length:' + data.length); }) } catch(error) { console.info(error); @@ -3395,7 +3395,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_007 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3421,7 +3421,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetAltitude_008 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3454,7 +3454,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3474,7 +3474,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3494,14 +3494,14 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ it("Sensor_TransformCoordinateSystem_003", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_003----------------------------------"); sensor.transformRotationMatrix([1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5], {'axisX':1, 'axisY':2}).then((data) => { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_003 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[0][i]); } @@ -3515,14 +3515,14 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ it("Sensor_TransformCoordinateSystem_004", 0, async function (done) { console.info("---------------------------Sensor_TransformCoordinateSystem_004----------------------------------"); sensor.transformRotationMatrix([3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39, 3.40282e+39], {'axisX':1, 'axisY':3}).then((data) => { - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("Sensor_TransformCoordinateSystem_004 data[ " + i + "] = " + data[i]); expect(data[i]).assertEqual(transformCoordinateSystemResult[2][i]); } @@ -3536,7 +3536,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_005 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3555,7 +3555,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_006 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3583,7 +3583,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_007 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3609,7 +3609,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_008 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3635,7 +3635,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_009 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3661,7 +3661,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_TransformCoordinateSystem_010 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: I5SWJI * @tc.author: */ @@ -3689,7 +3689,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSensorList_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3697,7 +3697,7 @@ describe("SensorJsTest", function () { console.info("---------------------------Sensor_GetSensorList_001----------------------------------"); sensor.getSensorList().then((data) => { console.info("---------------------------Sensor_GetSensorList_001 callback in-----------" + data.length); - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("Sensor_GetSensorList_001 " + JSON.stringify(data[i])); } expect(true).assertTrue(); @@ -3711,7 +3711,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSensorList_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3723,7 +3723,7 @@ describe("SensorJsTest", function () { expect(false).assertTrue(); } else { console.info("---------------------------Sensor_GetSensorList_002 callback in-----------" + data.length); - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { console.info("Sensor_GetSensorList_002 " + JSON.stringify(data[i])); } expect(true).assertTrue(); @@ -3734,7 +3734,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSensorList_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3752,7 +3752,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSensorList_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3779,7 +3779,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSingleSensor_001 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3800,7 +3800,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSingleSensor_002 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3828,7 +3828,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSingleSensor_003 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ @@ -3847,7 +3847,7 @@ describe("SensorJsTest", function () { /* * @tc.name: Sensor_GetSingleSensor_004 - * @tc.desc: Verfication results of the incorrect parameters of test interface. + * @tc.desc: Verification results of the incorrect parameters of test interface. * @tc.require: AR000GH2TR * @tc.author: */ diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 1863ffd63f61687771c48168caa34188248daba0..7218e86f06d0d647cba28c5ff2c8036c7ba822ea 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -132,7 +132,7 @@ int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) return ERR_INVALID_VALUE; } - SensorEvent sensorEvent = { + SensorData sensorData = { .sensorTypeId = event->sensorId, .version = event->version, .timestamp = event->timestamp, @@ -140,18 +140,16 @@ int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) .mode = event->mode, .dataLen = event->dataLen }; - sensorEvent.data = new (std::nothrow) uint8_t[SENSOR_DATA_LENGHT]; - CHKPR(sensorEvent.data, ERR_INVALID_VALUE); - errno_t ret = memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen); + CHKPR(sensorData.data, ERR_INVALID_VALUE); + errno_t ret = memcpy_s(sensorData.data, sizeof(sensorData.data), event->data, event->dataLen); if (ret != EOK) { SEN_HILOGE("copy data failed"); - delete[] sensorEvent.data; return COPY_ERR; } CHKPR(reportDataCallback_, ERR_NO_INIT); CHKPR(reportDataCb_, ERR_NO_INIT); std::unique_lock lk(ISensorHdiConnection::dataMutex_); - (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); + (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index a5a9fbf82f6d06e9c66a3d371b4cda4d0e892860..7538831290d1fa4fc4f7f959332d3ac644c0e356 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -12,11 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sensor_event_callback.h" #include "hdi_connection.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensors_errors.h" namespace OHOS { @@ -37,7 +36,7 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) SEN_HILOGI("data is empty"); return ERR_INVALID_VALUE; } - SensorEvent sensorEvent = { + SensorData sensorData = { .sensorTypeId = event.sensorId, .version = event.version, .timestamp = event.timestamp, @@ -45,13 +44,12 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) .mode = event.mode, .dataLen = event.dataLen }; - sensorEvent.data = new (std::nothrow) uint8_t[SENSOR_DATA_LENGHT]; - CHKPR(sensorEvent.data, ERR_NO_INIT); + CHKPR(sensorData.data, ERR_NO_INIT); for (int32_t i = 0; i < static_cast(dataSize); i++) { - sensorEvent.data[i] = event.data[i]; + sensorData.data[i] = event.data[i]; } std::unique_lock lk(ISensorHdiConnection::dataMutex_); - (void)(reportDataCallback_->*(reportDataCb_))(&sensorEvent, reportDataCallback_); + (void)(reportDataCallback_->*(reportDataCb_))(&sensorData, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index f81cd4d9ad882d17fc36a854714e93ee43aa0d3f..e479713829c49939c2f749fc95fc723f935757aa 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -32,7 +32,7 @@ #include "sensor_basic_data_channel.h" #include "sensor_basic_info.h" #include "sensor_channel_info.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { @@ -58,8 +58,8 @@ public: SensorBasicInfo GetCurPidSensorInfo(uint32_t sensorId, int32_t pid); uint64_t ComputeBestPeriodCount(uint32_t sensorId, sptr &channel); uint64_t ComputeBestFifoCount(uint32_t sensorId, sptr &channel); - int32_t GetStoreEvent(int32_t sensorId, SensorEvent &event); - void StoreEvent(const SensorEvent &event); + int32_t GetStoreEvent(int32_t sensorId, SensorData &data); + void StoreEvent(const SensorData &data); void ClearEvent(); AppThreadInfo GetAppInfoByChannel(const sptr &channel); bool SaveClientPid(const sptr &sensorClient, int32_t pid); @@ -69,8 +69,8 @@ public: void GetSensorChannelInfo(std::vector &channelInfo); void UpdateCmd(uint32_t sensorId, int32_t uid, int32_t cmdType); void DestroyCmd(int32_t uid); - void UpdateDataQueue(int32_t sensorId, SensorEvent &event); - std::unordered_map> GetDumpQueue(); + void UpdateDataQueue(int32_t sensorId, SensorData &data); + std::unordered_map> GetDumpQueue(); void ClearDataQueue(int32_t sensorId); int32_t GetUidByPid(int32_t pid); AccessTokenID GetTokenIdByPid(int32_t pid); @@ -87,11 +87,11 @@ private: std::mutex dataQueueMutex_; std::unordered_map> clientMap_; std::unordered_map> channelMap_; - std::unordered_map storedEvent_; + std::unordered_map storedEvent_; std::unordered_map appThreadInfoMap_; std::map, int32_t> clientPidMap_; std::unordered_map>> cmdMap_; - std::unordered_map> dumpQueue_; + std::unordered_map> dumpQueue_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index 7a1713b59ec8a572aa9083f6e4ccb1f3624d062e..d6221735b656d422c2635c4bc6fcc8fbd88c9a2b 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -20,7 +20,7 @@ #include "nocopyable.h" #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensor_basic_data_channel.h" namespace OHOS { @@ -31,8 +31,8 @@ public: virtual ~FifoCacheData(); void SetPeriodCount(uint64_t periodCount); uint64_t GetPeriodCount() const; - void SetFifoCacheData(const std::vector &fifoCacheData); - std::vector GetFifoCacheData() const; + void SetFifoCacheData(const std::vector &fifoCacheData); + std::vector GetFifoCacheData() const; void SetChannel(const sptr &channel); sptr GetChannel() const; void InitFifoCache(); @@ -41,7 +41,7 @@ private: DISALLOW_COPY_AND_MOVE(FifoCacheData); uint64_t periodCount_; sptr channel_; - std::vector fifoCacheData_; + std::vector fifoCacheData_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h index 8f2ff1702ff34ea072982a5189144aef94150c99..8a9d21250543f1c518908902e80504dd74eec1cf 100644 --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -29,7 +29,7 @@ #include "report_data_callback.h" #include "sensor.h" #include "sensor_hdi_connection.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { @@ -38,22 +38,22 @@ public: explicit SensorDataProcesser(const std::unordered_map &sensorMap); virtual ~SensorDataProcesser(); int32_t ProcessEvents(sptr dataCallback); - int32_t SendEvents(sptr &channel, SensorEvent &event); + int32_t SendEvents(sptr &channel, SensorData &data); static int DataThread(sptr dataProcesser, sptr dataCallback); - int32_t CacheSensorEvent(const SensorEvent &event, sptr &channel); + int32_t CacheSensorEvent(const SensorData &data, sptr &channel); private: DISALLOW_COPY_AND_MOVE(SensorDataProcesser); - void ReportData(sptr &channel, SensorEvent &event); - bool ReportNotContinuousData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event); - void SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, uint64_t periodCount); - void SendFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, uint64_t periodCount, + void ReportData(sptr &channel, SensorData &data); + bool ReportNotContinuousData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data); + void SendNoneFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount); + void SendFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount, uint64_t fifoCount); - void SendRawData(std::unordered_map &cacheBuf, sptr channel, - std::vector event); + void SendRawData(std::unordered_map &cacheBuf, sptr channel, + std::vector events); void EventFilter(CircularEventBuf &eventsBuf); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); diff --git a/services/sensor/include/sensor_dump.h b/services/sensor/include/sensor_dump.h index e636c0e619afdfd702208b787c01cfafec31066d..8c72911446efdbc83a6da17eaf0a4e86e55ec1f3 100644 --- a/services/sensor/include/sensor_dump.h +++ b/services/sensor/include/sensor_dump.h @@ -44,7 +44,7 @@ private: DISALLOW_COPY_AND_MOVE(SensorDump); void DumpCurrentTime(int32_t fd); int32_t DataSizeBySensorId(uint32_t sensorId); - std::string GetDataBySensorId(uint32_t sensorId, TransferSensorEvents &sensorData); + std::string GetDataBySensorId(uint32_t sensorId, SensorData &sensorData); static std::unordered_map sensorMap_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h index 2be11364ac40b2ff91b485a397105d3f99294660..5bcb35dff35fa844482fcacba4c00bc2f77cd15c 100644 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -24,7 +24,7 @@ #include "flush_info_record.h" #include "sensor_data_processer.h" #include "sensor_hdi_connection.h" -#include "sensor_agent_type.h" + namespace OHOS { namespace Sensors { using namespace Security::AccessToken; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index a436cf89adc7c5377f994b9f4f67c6af91248997..77fe863028cbc35c4d05cc19f689b3df8a5f07b0 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -25,7 +25,7 @@ #include "client_info.h" #include "death_recipient_template.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" #include "sensor_service_stub.h" @@ -78,11 +78,11 @@ private: ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); - sptr sensorDataProcesser_; - sptr reportDataCallback_; + sptr sensorDataProcesser_ = nullptr; + sptr reportDataCallback_ = nullptr; std::mutex uidLock_; // death recipient of sensor client - sptr clientDeathObserver_; + sptr clientDeathObserver_ = nullptr; ErrCode SaveSubscriber(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); }; } // namespace Sensors diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 122c727fd2dfe01dbb1007b4e3bad6669dd53d4c..f0374d7d9dcda411256e59771ab1eb7cc2c84d65 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -33,7 +33,7 @@ constexpr uint32_t INVALID_SENSOR_ID = -1; constexpr int32_t INVALID_PID = -1; constexpr int32_t INVALID_UID = -1; constexpr int32_t MIN_MAP_SIZE = 0; -constexpr uint32_t NO_STROE_EVENT = -2; +constexpr uint32_t NO_STORE_EVENT = -2; constexpr uint32_t MAX_SUPPORT_CHANNEL = 200; constexpr uint32_t MAX_DUMP_DATA_SIZE = 10; constexpr uint32_t HEART_RATE_SENSOR_ID = 83886336; @@ -427,12 +427,12 @@ uint64_t ClientInfo::ComputeBestFifoCount(uint32_t sensorId, sptr lock(eventMutex_); auto storedEvent = storedEvent_.find(sensorId); if (storedEvent != storedEvent_.end()) { - errno_t ret = memcpy_s(&event, sizeof(SensorEvent), &storedEvent->second, sizeof(SensorEvent)); + errno_t ret = memcpy_s(&data, sizeof(data), &storedEvent->second, sizeof(storedEvent->second)); if (ret != EOK) { SEN_HILOGE("memcpy_s failed, sensorId:%{public}d", sensorId); return ret; @@ -441,13 +441,13 @@ int32_t ClientInfo::GetStoreEvent(int32_t sensorId, SensorEvent &event) } SEN_HILOGE("can't get store event, sensorId:%{public}u", sensorId); - return NO_STROE_EVENT; + return NO_STORE_EVENT; } -void ClientInfo::StoreEvent(const SensorEvent &event) +void ClientInfo::StoreEvent(const SensorData &data) { bool foundSensor = false; - SensorEvent storedEvent; + SensorData storedEvent; auto sensorHdiConnection = &SensorHdiConnection::GetInstance(); if (sensorHdiConnection == nullptr) { SEN_HILOGE("sensorHdiConnection cannot be null"); @@ -459,7 +459,7 @@ void ClientInfo::StoreEvent(const SensorEvent &event) SEN_HILOGE("GetSensorList is failed"); return; } - errno_t retVal = memcpy_s(&storedEvent, sizeof(storedEvent), &event, sizeof(event)); + errno_t retVal = memcpy_s(&storedEvent, sizeof(storedEvent), &data, sizeof(data)); if (retVal != EOK) { SEN_HILOGE("memcpy_s is failed"); return; @@ -653,38 +653,26 @@ std::vector ClientInfo::GetCmdList(uint32_t sensorId, int32_t uid) return uidIt->second; } -void ClientInfo::UpdateDataQueue(int32_t sensorId, SensorEvent &event) +void ClientInfo::UpdateDataQueue(int32_t sensorId, SensorData &data) { if (sensorId == HEART_RATE_SENSOR_ID) { return; } std::lock_guard queueLock(dataQueueMutex_); - TransferSensorEvents transferEvent = { - .sensorTypeId = event.sensorTypeId, - .version = event.version, - .timestamp = event.timestamp, - .option = event.option, - .mode = event.mode, - .dataLen = event.dataLen - }; - if (memcpy_s(transferEvent.data, event.dataLen, event.data, event.dataLen) != EOK) { - SEN_HILOGE("Copy data failed"); - return; - } auto it = dumpQueue_.find(sensorId); if (it == dumpQueue_.end()) { - std::queue q; - q.push(transferEvent); + std::queue q; + q.push(data); dumpQueue_.insert(std::make_pair(sensorId, q)); return; } - it->second.push(transferEvent); + it->second.push(data); if (it->second.size() > MAX_DUMP_DATA_SIZE) { it->second.pop(); } } -std::unordered_map> ClientInfo::GetDumpQueue() +std::unordered_map> ClientInfo::GetDumpQueue() { return dumpQueue_; } diff --git a/services/sensor/src/fifo_cache_data.cpp b/services/sensor/src/fifo_cache_data.cpp index b3aa44c816f6b84cf95153cd0af666a581d45c24..db0174f68fa45d3b4845467a4881c87cfd99c5f6 100644 --- a/services/sensor/src/fifo_cache_data.cpp +++ b/services/sensor/src/fifo_cache_data.cpp @@ -41,12 +41,12 @@ uint64_t FifoCacheData::GetPeriodCount() const return periodCount_; } -void FifoCacheData::SetFifoCacheData(const std::vector &fifoCacheData) +void FifoCacheData::SetFifoCacheData(const std::vector &fifoCacheData) { fifoCacheData_ = fifoCacheData; } -std::vector FifoCacheData::GetFifoCacheData() const +std::vector FifoCacheData::GetFifoCacheData() const { return fifoCacheData_; } diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 5dc6a707c8a61735d35ecd833284d7033c6d8602..466cd693c0e6733b180fb34bc7fac4b0a3ec7eca 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -41,9 +41,9 @@ enum { constexpr uint32_t SENSOR_INDEX_SHIFT = 8; constexpr uint32_t SENSOR_TYPE_SHIFT = 16; -constexpr uint32_t SENSOR_CATAGORY_SHIFT = 24; +constexpr uint32_t SENSOR_CATEGORY_SHIFT = 24; -constexpr uint32_t FLUSH_COMPLETE_ID = ((uint32_t)OTHER << SENSOR_CATAGORY_SHIFT) | +constexpr uint32_t FLUSH_COMPLETE_ID = ((uint32_t)OTHER << SENSOR_CATEGORY_SHIFT) | ((uint32_t)SENSOR_TYPE_FLUSH << SENSOR_TYPE_SHIFT) | ((uint32_t)FIRST_INDEX << SENSOR_INDEX_SHIFT); } // namespace @@ -60,16 +60,16 @@ SensorDataProcesser::~SensorDataProcesser() sensorMap_.clear(); } -void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, +void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount) { - std::vector sendEvents; + std::vector sendEvents; std::lock_guard dataCountLock(dataCountMutex_); - sendEvents.push_back(event); - uint32_t sensorId = static_cast(event.sensorTypeId); + sendEvents.push_back(data); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } auto dataCountIt = dataCountMap_.find(sensorId); if (dataCountIt == dataCountMap_.end()) { @@ -107,13 +107,13 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, +void SensorDataProcesser::SendFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount, uint64_t fifoCount) { - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } std::lock_guard dataCountLock(dataCountMutex_); auto dataCountIt = dataCountMap_.find(sensorId); @@ -141,8 +141,8 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_mapSetPeriodCount(0); - std::vector fifoDataList = fifoData->GetFifoCacheData(); - fifoDataList.push_back(event); + std::vector fifoDataList = fifoData->GetFifoCacheData(); + fifoDataList.push_back(data); fifoData->SetFifoCacheData(fifoDataList); if ((fifoData->GetFifoCacheData()).size() != fifoCount) { continue; @@ -160,15 +160,15 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map &channel, SensorEvent &event) +void SensorDataProcesser::ReportData(sptr &channel, SensorData &data) { CHKPV(channel); - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } - auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); - if (ReportNotContinuousData(cacheBuf, channel, event)) { + auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); + if (ReportNotContinuousData(cacheBuf, channel, data)) { return; } uint64_t periodCount = clientInfo_.ComputeBestPeriodCount(sensorId, channel); @@ -177,18 +177,18 @@ void SensorDataProcesser::ReportData(sptr &channel, Sens } auto fifoCount = clientInfo_.ComputeBestFifoCount(sensorId, channel); if (fifoCount <= 0) { - SendNoneFifoCacheData(cacheBuf, channel, event, periodCount); + SendNoneFifoCacheData(cacheBuf, channel, data, periodCount); return; } - SendFifoCacheData(cacheBuf, channel, event, periodCount, fifoCount); + SendFifoCacheData(cacheBuf, channel, data, periodCount, fifoCount); } -bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event) +bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data) { - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } std::lock_guard sensorLock(sensorMutex_); auto sensor = sensorMap_.find(sensorId); @@ -196,81 +196,64 @@ bool SensorDataProcesser::ReportNotContinuousData(std::unordered_mapsecond.SetFlags(event.mode); + sensor->second.SetFlags(data.mode); if (((SENSOR_ON_CHANGE & sensor->second.GetFlags()) == SENSOR_ON_CHANGE) || ((SENSOR_ONE_SHOT & sensor->second.GetFlags()) == SENSOR_ONE_SHOT)) { - std::vector sendEvents; - sendEvents.push_back(event); + std::vector sendEvents; + sendEvents.push_back(data); SendRawData(cacheBuf, channel, sendEvents); return true; } return false; } -void SensorDataProcesser::SendRawData(std::unordered_map &cacheBuf, - sptr channel, std::vector event) +void SensorDataProcesser::SendRawData(std::unordered_map &cacheBuf, + sptr channel, std::vector events) { CHKPV(channel); - if (event.empty()) { + if (events.empty()) { return; } - size_t eventSize = event.size(); - std::vector transferEvents; - for (size_t i = 0; i < eventSize; i++) { - TransferSensorEvents transferEvent = { - .sensorTypeId = event[i].sensorTypeId, - .version = event[i].version, - .timestamp = event[i].timestamp, - .option = event[i].option, - .mode = event[i].mode, - .dataLen = event[i].dataLen - }; - errno_t ret = memcpy_s(transferEvent.data, SENSOR_MAX_LENGTH, event[i].data, event[i].dataLen); - if (ret != EOK) { - SEN_HILOGE("copy data failed"); - return; - } - transferEvents.push_back(transferEvent); - } - auto ret = channel->SendData(transferEvents.data(), eventSize * sizeof(TransferSensorEvents)); + size_t eventSize = events.size(); + auto ret = channel->SendData(events.data(), eventSize * sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("send data failed, ret:%{public}d", ret); - uint32_t sensorId = static_cast(event[eventSize - 1].sensorTypeId); + uint32_t sensorId = static_cast(events[eventSize - 1].sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event[eventSize - 1].sensorTypeId); + sensorId = static_cast(events[eventSize - 1].sensorTypeId); } - cacheBuf[sensorId] = event[eventSize - 1]; + cacheBuf[sensorId] = events[eventSize - 1]; } } -int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr &channel) +int32_t SensorDataProcesser::CacheSensorEvent(const SensorData &data, sptr &channel) { CHKPR(channel, INVALID_POINTER); int32_t ret = ERR_OK; - auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); - uint32_t sensorId = static_cast(event.sensorTypeId); + auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } auto cacheEvent = cacheBuf.find(sensorId); if (cacheEvent != cacheBuf.end()) { // Try to send the last failed value, if it still fails, replace the previous cache directly - ret = channel->SendData(&cacheEvent->second, sizeof(SensorEvent)); + ret = channel->SendData(&cacheEvent->second, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); } - ret = channel->SendData(&event, sizeof(SensorEvent)); + ret = channel->SendData(&data, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); - cacheBuf[sensorId] = event; + cacheBuf[sensorId] = data; } else { cacheBuf.erase(cacheEvent); } } else { - ret = channel->SendData(&event, sizeof(SensorEvent)); + ret = channel->SendData(&data, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); - cacheBuf[sensorId] = event; + cacheBuf[sensorId] = data; } } return ret; @@ -337,10 +320,7 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t eventNum = eventsBuf.eventNum; for (int32_t i = 0; i < eventNum; i++) { EventFilter(eventsBuf); - if (eventsBuf.circularBuf[eventsBuf.readPos].data != nullptr) { - delete[] eventsBuf.circularBuf[eventsBuf.readPos].data; - eventsBuf.circularBuf[eventsBuf.readPos].data = nullptr; - } + eventsBuf.readPos++; if (eventsBuf.readPos == CIRCULAR_BUF_LEN) { eventsBuf.readPos = 0; @@ -350,17 +330,17 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback return SUCCESS; } -int32_t SensorDataProcesser::SendEvents(sptr &channel, SensorEvent &event) +int32_t SensorDataProcesser::SendEvents(sptr &channel, SensorData &data) { CHKPR(channel, INVALID_POINTER); - clientInfo_.UpdateDataQueue(event.sensorTypeId, event); + clientInfo_.UpdateDataQueue(data.sensorTypeId, data); auto &cacheBuf = channel->GetDataCacheBuf(); if (cacheBuf.empty()) { - ReportData(channel, event); + ReportData(channel, data); } else { - CacheSensorEvent(event, channel); + CacheSensorEvent(data, channel); } - clientInfo_.StoreEvent(event); + clientInfo_.StoreEvent(data); return SUCCESS; } diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index d1d298b904f82bf1703aa9447056f37af4612768..ced0de4d4c63954c282a7d37b8b329d23f8e8b19 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -173,7 +173,9 @@ void SensorDump::ParseCommand(int32_t fd, const std::vector &args, } RELEASE_RES: for (size_t i = 0; i < args.size(); ++i) { - delete[] argv[i]; + if (argv[i] != nullptr) { + delete[] argv[i]; + } } delete[] argv; } @@ -194,6 +196,9 @@ bool SensorDump::DumpSensorList(int32_t fd, const std::vector &sensors) dprintf(fd, "Total sensor:%d, Sensor list:\n", int32_t { sensors.size() }); for (const auto &sensor : sensors) { auto sensorId = sensor.GetSensorId(); + if (sensorMap_.find(sensorId) == sensorMap_.end()) { + continue; + } dprintf(fd, "sensorId:%8u | sensorType:%s | sensorName:%s | vendorName:%s | maxRange:%f" "| fifoMaxEventCount:%d | minSamplePeriodNs:%" PRId64 " | maxSamplePeriodNs:%" PRId64 "\n", @@ -212,6 +217,9 @@ bool SensorDump::DumpSensorChannel(int32_t fd, ClientInfo &clientInfo) clientInfo.GetSensorChannelInfo(channelInfo); for (const auto &channel : channelInfo) { auto sensorId = channel.GetSensorId(); + if (sensorMap_.find(sensorId) == sensorMap_.end()) { + continue; + } dprintf(fd, "uid:%d | packageName:%s | sensorId:%8u | sensorType:%s | samplingPeriodNs:%" PRId64 "" "| fifoCount:%u\n", @@ -227,6 +235,9 @@ bool SensorDump::DumpOpeningSensor(int32_t fd, const std::vector &sensor dprintf(fd, "Opening sensors:\n"); for (const auto &sensor : sensors) { uint32_t sensorId = sensor.GetSensorId(); + if (sensorMap_.find(sensorId) == sensorMap_.end()) { + continue; + } if (clientInfo.GetSensorState(sensorId)) { dprintf(fd, "sensorId: %8u | sensorType: %s | channelSize: %lu\n", sensorId, sensorMap_[sensorId].c_str(), clientInfo.GetSensorChannel(sensorId).size()); @@ -242,6 +253,9 @@ bool SensorDump::DumpSensorData(int32_t fd, ClientInfo &clientInfo) int32_t j = 0; for (auto &sensorData : dataMap) { uint32_t sensorId = sensorData.first; + if (sensorMap_.find(sensorId) == sensorMap_.end()) { + continue; + } dprintf(fd, "sensorId: %8u | sensorType: %s:\n", sensorId, sensorMap_[sensorId].c_str()); for (uint32_t i = 0; i < MAX_DUMP_DATA_SIZE && (!sensorData.second.empty()); i++) { auto data = sensorData.second.front(); @@ -295,7 +309,7 @@ int32_t SensorDump::DataSizeBySensorId(uint32_t sensorId) } } -std::string SensorDump::GetDataBySensorId(uint32_t sensorId, TransferSensorEvents &sensorData) +std::string SensorDump::GetDataBySensorId(uint32_t sensorId, SensorData &sensorData) { SEN_HILOGD("sensorId:%{public}u", sensorId); std::string str; diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 5b465e0bf17aa015d6a5768532e37a6416a9eb1f..3a8255c8deba8f55d6e2dac88d540f4905188830 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -19,6 +19,7 @@ #include "iservice_registry.h" #include "sensor.h" +#include "sensor_data_event.h" #include "sensors_errors.h" namespace OHOS { @@ -168,12 +169,12 @@ ErrCode SensorManager::AfterDisableSensor(uint32_t sensorId) CALL_LOG_ENTER; clientInfo_.ClearSensorInfo(sensorId); if (sensorId == PROXIMITY_SENSOR_ID) { - SensorEvent event; - auto ret = clientInfo_.GetStoreEvent(sensorId, event); + SensorData sensorData; + auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData); if (ret == ERR_OK) { SEN_HILOGD("change the default state is far"); - event.data[0] = PROXIMITY_FAR; - clientInfo_.StoreEvent(event); + sensorData.data[0] = PROXIMITY_FAR; + clientInfo_.StoreEvent(sensorData); } } return ERR_OK; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 563427c50c359bf0b08ee5df1d543a68ab09ef0e..377e739d17c3192f2d7b5fe9c2811395f87c53b5 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -178,15 +178,15 @@ void SensorService::ReportOnChangeData(uint32_t sensorId) SEN_HILOGW("it is not onchange data, no need to report"); return; } - SensorEvent event; - auto ret = clientInfo_.GetStoreEvent(sensorId, event); + SensorData sensorData; + auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData); if (ret != ERR_OK) { SEN_HILOGE("there is no data to be reported"); return; } sptr channel = clientInfo_.GetSensorChannelByPid(GetCallingPid()); CHKPV(channel); - auto sendRet = channel->SendData(&event, sizeof(event)); + auto sendRet = channel->SendData(&sensorData, sizeof(sensorData)); if (sendRet != ERR_OK) { SEN_HILOGE("send data failed"); return; @@ -366,8 +366,8 @@ ErrCode SensorService::DestroySensorChannel(sptr sensorClient) return CLIENT_PID_INVALID_ERR; } std::lock_guard serviceLock(serviceLock_); - bool destoryRet = clientInfo_.DestroySensorChannel(clientPid); - if (!destoryRet) { + bool destroyRet = clientInfo_.DestroySensorChannel(clientPid); + if (!destroyRet) { SEN_HILOGE("DestroySensorChannel is failed"); return DESTROY_SENSOR_CHANNEL_ERR; } @@ -386,12 +386,12 @@ void SensorService::ProcessDeathObserver(const wptr &object) SEN_HILOGE("pid is -1"); return; } - SEN_HILOGI("pid is %{pubilc}d", pid); + SEN_HILOGI("pid is %{public}d", pid); std::vector activeSensors = clientInfo_.GetSensorIdByPid(pid); for (size_t i = 0; i < activeSensors.size(); ++i) { int32_t ret = DisableSensor(activeSensors[i], pid); if (ret != ERR_OK) { - SEN_HILOGE("disablesensor failed, ret:%{pubilc}d", ret); + SEN_HILOGE("disablesensor failed, ret:%{public}d", ret); } } clientInfo_.DestroySensorChannel(pid); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index bed7a011e211b46817073be3ed2477cebf20ca01..554843f2f27157912924e4b6545b2ef996ff8c17 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -77,7 +77,11 @@ int32_t SensorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, M ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel &reply) { (void)reply; - uint32_t sensorId = data.ReadUint32(); + uint32_t sensorId; + if (!data.ReadUint32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { @@ -86,13 +90,23 @@ ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } - return EnableSensor(sensorId, data.ReadInt64(), data.ReadInt64()); + int64_t samplingPeriodNs; + int64_t maxReportDelayNs; + if ((!data.ReadInt64(samplingPeriodNs)) || (!data.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return EnableSensor(sensorId, samplingPeriodNs, maxReportDelayNs); } ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel &reply) { (void)reply; - uint32_t sensorId = data.ReadUint32(); + uint32_t sensorId; + if (!data.ReadUint32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { @@ -107,7 +121,11 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel ErrCode SensorServiceStub::GetSensorStateInner(MessageParcel &data, MessageParcel &reply) { (void)reply; - uint32_t sensorId = data.ReadUint32(); + uint32_t sensorId; + if (!data.ReadUint32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { @@ -122,7 +140,11 @@ ErrCode SensorServiceStub::GetSensorStateInner(MessageParcel &data, MessageParce ErrCode SensorServiceStub::RunCommandInner(MessageParcel &data, MessageParcel &reply) { (void)reply; - uint32_t sensorId = data.ReadUint32(); + uint32_t sensorId; + if (!data.ReadUint32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { @@ -131,7 +153,13 @@ ErrCode SensorServiceStub::RunCommandInner(MessageParcel &data, MessageParcel &r SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } - return RunCommand(sensorId, data.ReadUint32(), data.ReadUint32()); + uint32_t cmdType; + uint32_t params; + if ((!data.ReadUint32(cmdType)) || (!data.ReadUint32(params))) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return RunCommand(sensorId, cmdType, params); } ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply) diff --git a/services/sensor/src/sensor_suspend_policy.cpp b/services/sensor/src/sensor_suspend_policy.cpp index 217e25400b86fc58a93063c824e6f6db0e60c39d..1e9f2444062ab558b0b26e8d8a023caf417cc843 100644 --- a/services/sensor/src/sensor_suspend_policy.cpp +++ b/services/sensor/src/sensor_suspend_policy.cpp @@ -27,10 +27,10 @@ namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorSuspendPolicy" }; constexpr uint32_t INVALID_SENSOR_ID = -1; constexpr int64_t MAX_EVENT_COUNT = 1000; -constexpr int64_t DEFAULT_SAMPLEING_RATE = 200000000; +constexpr int64_t DEFAULT_SAMPLING_RATE = 200000000; constexpr int64_t DEFAULT_REPORT_DELAY = 0; constexpr uint32_t STEP_COUNTER_ID = 524544; -constexpr uint32_t STEP_DETECOTR_ID = 590080; +constexpr uint32_t STEP_DETECTOR_ID = 590080; } // namespace SensorSuspendPolicy::~SensorSuspendPolicy() @@ -38,7 +38,7 @@ SensorSuspendPolicy::~SensorSuspendPolicy() bool SensorSuspendPolicy::CheckFreezingSensor(uint32_t sensorId) { - return ((sensorId == STEP_COUNTER_ID) || (sensorId == STEP_DETECOTR_ID)); + return ((sensorId == STEP_COUNTER_ID) || (sensorId == STEP_DETECTOR_ID)); } ErrCode SensorSuspendPolicy::DisableSensor(uint32_t sensorId, int32_t pid) @@ -150,7 +150,7 @@ std::vector SensorSuspendPolicy::GetSensorIdByPid(int32_t pid) void SensorSuspendPolicy::DoActive(const std::shared_ptr &info) { CALL_LOG_ENTER; - int64_t samplePeriod = DEFAULT_SAMPLEING_RATE; + int64_t samplePeriod = DEFAULT_SAMPLING_RATE; int64_t maxReportDelay = DEFAULT_REPORT_DELAY; std::vector sensorIdList; std::lock_guard suspendLock(suspendMutex_); diff --git a/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h old mode 100755 new mode 100644 index a3ad5de74d5db04960fc44b3d1d99fb9a677c126..b48b8fdad025a93d9677b2581cd72edd083d8afe --- a/test/unittest/common/include/system_info.h +++ b/test/unittest/common/include/system_info.h @@ -21,7 +21,7 @@ namespace OHOS { namespace Sensors { namespace SYSTEM_INFO { -static constexpr double CPU_USAGE_UNKONW = -1.0; +static constexpr double CPU_USAGE_UNKNOWN = -1.0; static constexpr double CPU_USAGE_LOAD = 20.0; static constexpr double CPU_USAGE_MAX = 100.0; class SystemInfo { @@ -57,7 +57,6 @@ private: int32_t cstime { 0 }; }; int32_t GetTaskPidFile(const std::string& process_name); - int32_t GetTaskPidCmd(const std::string& process_name, int32_t flag = 0, std::string user = ""); int32_t GetProcOccupy(int32_t pid); int32_t GetSystemCpuStatInfo(Total_Cpu_Occupy& info); diff --git a/test/unittest/common/src/system_info.cpp b/test/unittest/common/src/system_info.cpp old mode 100755 new mode 100644 index f21a27ccc692ec0b483493464034f8c8cec5119f..2688c4f7aac9d90ae23807fb3fb610bd629bea8b --- a/test/unittest/common/src/system_info.cpp +++ b/test/unittest/common/src/system_info.cpp @@ -65,7 +65,11 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) continue; } std::string strLine; - std::getline(filePath, strLine); + if (!std::getline(filePath, strLine)) { + SEN_HILOGE("getline fail"); + filePath.close(); + continue; + } if (strLine.empty()) { filePath.close(); continue; @@ -76,7 +80,7 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) } while (std::getline(filePath, strLine)) { if ((strLine.find("Pid")) != std::string::npos) { - if (sscanf_s(strLine.c_str(), "%*s%d", &pid) == -1) { + if (::sscanf_s(strLine.c_str(), "%*s%d", &pid, sizeof(pid)) != 1) { SEN_HILOGE("sscanf_s failed"); } break; @@ -90,34 +94,6 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) return pid; } -int32_t CpuInfo::GetTaskPidCmd(const std::string& process_name, int32_t flag, std::string user) -{ - std::string command; - if (flag) { - if (user.empty()) { - user = ::getlogin(); - } - command = "pgrep " + process_name + " -u " + user; - } else { - command = "pidof -s " + process_name; - } - ::FILE *fp = nullptr; - if ((fp = ::popen(command.c_str(), "r")) == nullptr) { - SEN_HILOGE("Failed to open, cmd:%{public}s", command.c_str()); - fp = nullptr; - return DEFAULT_PID; - } - char buf[100] = { 0 }; - if (::fgets(buf, sizeof(buf), fp) == nullptr) { - SEN_HILOGE("Failed to read content"); - ::pclose(fp); - fp = nullptr; - return DEFAULT_PID; - } - ::pclose(fp); - return ::atoi(buf); -} - int32_t CpuInfo::GetProcOccupy(int32_t pid) { Proc_Cpu_Occupy info; @@ -129,7 +105,11 @@ int32_t CpuInfo::GetProcOccupy(int32_t pid) } std::string strLine; - std::getline(file, strLine); + if (!std::getline(file, strLine)) { + SEN_HILOGE("getline fail"); + file.close(); + return OHOS::Sensors::ERROR; + } if (strLine.empty()) { SEN_HILOGE("Failed to read content"); file.close(); @@ -170,7 +150,11 @@ int32_t CpuInfo::GetSystemCpuStatInfo(Total_Cpu_Occupy& info) return FILE_OPEN_FAIL; } std::string strLine; - std::getline(statFile, strLine); + if (!std::getline(statFile, strLine)) { + SEN_HILOGE("getline fail"); + statFile.close(); + return STREAM_BUF_READ_FAIL; + } if (strLine.empty()) { SEN_HILOGE("No valid content was read"); statFile.close(); @@ -195,14 +179,14 @@ double CpuInfo::GetSystemCpuUsage() int32_t ret = GetSystemCpuStatInfo(first); if (ret != OHOS::Sensors::SUCCESS) { SEN_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP)); Total_Cpu_Occupy second {}; ret = GetSystemCpuStatInfo(second); if (ret != OHOS::Sensors::SUCCESS) { SEN_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } return GetCpuUsage(first, second); @@ -229,22 +213,22 @@ double CpuInfo::GetProcCpuUsage(const std::string& process_name) if ((totalTime1 = GetSystemTotalOccupy()) == OHOS::Sensors::ERROR) { SEN_HILOGE("Failed to obtain CPU occupy"); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } if ((procTime1 = GetProcOccupy(pid)) == OHOS::Sensors::ERROR) { SEN_HILOGE("Failed to obtain process CPU information"); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP)); if ((totalTime2 = GetSystemTotalOccupy()) == OHOS::Sensors::ERROR) { SEN_HILOGE("Failed to obtain CPU occupy"); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } if ((procTime2 = GetProcOccupy(pid)) == OHOS::Sensors::ERROR) { SEN_HILOGE("Failed to obtain process CPU information"); - return CPU_USAGE_UNKONW; + return CPU_USAGE_UNKNOWN; } return CHK_RATE(CPU_USAGE_MAX * (procTime2 - procTime1) / (totalTime2 - totalTime1)); diff --git a/utils/include/miscdevice_common.h b/utils/include/miscdevice_common.h index 9932d560ab22cd7ab571f70b3e648905835637f4..ad742270fe9868c8cc5c96b3dde794825667a283 100755 --- a/utils/include/miscdevice_common.h +++ b/utils/include/miscdevice_common.h @@ -21,7 +21,7 @@ namespace OHOS { namespace Sensors { -// These LightId correspand to logical lights +// These LightId correspond to logical lights enum LightId { LIGHT_ID_LED = 0, LIGHT_ID_KEYBOARD, diff --git a/utils/include/report_data_callback.h b/utils/include/report_data_callback.h index 4c6acd5dbd5ea755f3c6d2d8d51e332f791c37ac..c7c6fa3794180bd82f5ea13378f640cbd2287f7f 100644 --- a/utils/include/report_data_callback.h +++ b/utils/include/report_data_callback.h @@ -17,15 +17,15 @@ #define REPORT_DATA_CALLBACK_H #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { constexpr int32_t CIRCULAR_BUF_LEN = 1024; -constexpr int32_t SENSOR_DATA_LENGHT = 64; +constexpr int32_t SENSOR_DATA_LENGTH = 64; struct CircularEventBuf { - struct SensorEvent *circularBuf; + struct SensorData *circularBuf; int32_t readPos; int32_t writePosition; int32_t eventNum; @@ -35,12 +35,12 @@ class ReportDataCallback : public RefBase { public: ReportDataCallback(); ~ReportDataCallback(); - int32_t ReportEventCallback(SensorEvent *event, sptr cb); + int32_t ReportEventCallback(SensorData *sensorData, sptr cb); CircularEventBuf &GetEventData(); CircularEventBuf eventsBuf_; }; -using ReportDataCb = int32_t (ReportDataCallback::*)(SensorEvent *event, sptr cb); +using ReportDataCb = int32_t (ReportDataCallback::*)(SensorData *sensorData, sptr cb); } // namespace Sensors } // namespace OHOS #endif // REPORT_DATA_CALLBACK_H diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h index 30147e6b4e9b65b735133e82b319fcf24875371c..a98543c28e7a8ba8cb9fda9cd160421e39fd01aa 100644 --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -22,20 +22,10 @@ #include "message_parcel.h" #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { -constexpr int32_t SENSOR_MAX_LENGTH = 64; -struct TransferSensorEvents { - uint32_t sensorTypeId; - int32_t version; - int64_t timestamp; - int32_t option; - int32_t mode; - uint32_t dataLen; - uint8_t data[SENSOR_MAX_LENGTH]; -}; class SensorBasicDataChannel : public RefBase { public: SensorBasicDataChannel(); @@ -51,14 +41,14 @@ public: int32_t ReceiveData(void *vaddr, size_t size); bool GetSensorStatus() const; void SetSensorStatus(bool isActive); - const std::unordered_map &GetDataCacheBuf() const; + const std::unordered_map &GetDataCacheBuf() const; private: int32_t sendFd_; int32_t receiveFd_; bool isActive_; std::mutex statusLock_; - std::unordered_map dataCacheBuf_; + std::unordered_map dataCacheBuf_; }; } // namespace Sensors } // namespace OHOS diff --git a/utils/include/sensor_catalog.h b/utils/include/sensor_catalog.h index 5856f6d5ea99b927e7404cc7cbe3bd8e901312fa..37b70458b52254606c82880795cbc5c967df9696 100755 --- a/utils/include/sensor_catalog.h +++ b/utils/include/sensor_catalog.h @@ -82,7 +82,7 @@ enum GroupBody { }; struct SensorCombination { - SensorGroup sensorCatagory; + SensorGroup sensorCategory; uint8_t sensorType; uint8_t sensorIndex; }; diff --git a/utils/include/sensor_data_event.h b/utils/include/sensor_data_event.h index e0e3a349f6c1fba7d7370d70233b335987766133..cf6751ff6f3c50fb07f98ead0f82db0781336eb9 100755 --- a/utils/include/sensor_data_event.h +++ b/utils/include/sensor_data_event.h @@ -21,6 +21,7 @@ namespace Sensors { constexpr int32_t RESERVED_DATA_LEN = 3; constexpr int32_t EXTRA_INFO_DATA_LEN = 14; constexpr int32_t DEFAULT_SENSOR_DATA_DIMS = 16; +constexpr int32_t SENSOR_MAX_LENGTH = 64; enum { WAKE_UP_SENSOR = 1u, @@ -30,8 +31,13 @@ enum { }; struct SensorData { - float data[DEFAULT_SENSOR_DATA_DIMS]; - uint32_t reserved[RESERVED_DATA_LEN]; + int32_t sensorTypeId; /**< Sensor type ID */ + int32_t version; /**< Sensor algorithm version */ + int64_t timestamp; /**< Time when sensor data was reported */ + uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ + int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ + uint8_t data[SENSOR_MAX_LENGTH]; /**< Sensor data */ + uint32_t dataLen; /**< Sensor data length */ }; struct ExtraInfo { diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 7c20589372b5d7b240138e20a6b3bf177a422ae7..7279ab75070f9f809c369118db7f28cb298906bc 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -93,7 +93,7 @@ enum { REGIST_CALLBACK_ERR = SET_SENSOR_OPTION_ERR + 1, }; -// Error code for Sensor uitls +// Error code for Sensor utils constexpr ErrCode SENSOR_UTILS_ERR_OFFSET = ErrCodeOffset(SUBSYS_SENSORS, MODULE_SENSORS_UTILS); enum { SENSOR_CHANNEL_SOCKET_CREATE_ERR = SENSOR_UTILS_ERR_OFFSET, diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 21231e95803c766dc63283a0f51f1764039a3388..7b71fa2ed516bb8fd4ad2b8602660df61e01f451 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -25,7 +25,7 @@ constexpr HiLogLabel LABEL = {LOG_CORE, SENSOR_LOG_DOMAIN, "ReportDataCallback"} } // namespace ReportDataCallback::ReportDataCallback() { - eventsBuf_.circularBuf = new (std::nothrow) SensorEvent[CIRCULAR_BUF_LEN]; + eventsBuf_.circularBuf = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; CHKPL(eventsBuf_.circularBuf); eventsBuf_.readPos = 0; eventsBuf_.writePosition = 0; @@ -44,32 +44,24 @@ ReportDataCallback::~ReportDataCallback() eventsBuf_.eventNum = 0; } -int32_t ReportDataCallback::ReportEventCallback(SensorEvent* event, sptr cb) +int32_t ReportDataCallback::ReportEventCallback(SensorData* sensorData, sptr cb) { - CHKPR(event, ERROR); + CHKPR(sensorData, ERROR); if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { SEN_HILOGE("callback or circularBuf or event cannot be null"); - if (event->data != nullptr) { - delete[] event->data; - event->data = nullptr; - } return ERROR; } int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition; if (leftSize < 0 || toEndLen < 0) { SEN_HILOGE("Leftsize and toendlen cannot be less than zero"); - if (event->data != nullptr) { - delete[] event->data; - event->data = nullptr; - } return ERROR; } if (toEndLen == 0) { - cb->eventsBuf_.circularBuf[0] = *event; - cb->eventsBuf_.writePosition = 1 - toEndLen; + cb->eventsBuf_.circularBuf[0] = *sensorData; + cb->eventsBuf_.writePosition = 1; } else { - cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *event; + cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *sensorData; cb->eventsBuf_.writePosition += 1; } if (leftSize < 1) { diff --git a/utils/src/sensor.cpp b/utils/src/sensor.cpp index 3c0c2b7c38a25c65cab8881b580366f6b8d78722..69d5a5125575e16338362357dad6aa6d9b4a4ba6 100644 --- a/utils/src/sensor.cpp +++ b/utils/src/sensor.cpp @@ -241,19 +241,21 @@ std::unique_ptr Sensor::Unmarshalling(Parcel &parcel) bool Sensor::ReadFromParcel(Parcel &parcel) { - sensorId_ = parcel.ReadUint32(); - sensorTypeId_ = parcel.ReadUint32(); - sensorName_ = parcel.ReadString(); - vendorName_ = parcel.ReadString(); - firmwareVersion_ = parcel.ReadString(); - hardwareVersion_ = parcel.ReadString(); - power_ = parcel.ReadFloat(); - maxRange_ = parcel.ReadFloat(); - resolution_ = parcel.ReadFloat(); - flags_ = parcel.ReadUint32(); - fifoMaxEventCount_ = parcel.ReadInt32(); - minSamplePeriodNs_ = parcel.ReadInt64(); - maxSamplePeriodNs_ = parcel.ReadInt64(); + if ((!parcel.ReadUint32(sensorId_)) || + (!parcel.ReadUint32(sensorTypeId_)) || + (!parcel.ReadString(sensorName_)) || + (!parcel.ReadString(vendorName_)) || + (!parcel.ReadString(firmwareVersion_)) || + (!parcel.ReadString(hardwareVersion_)) || + (!parcel.ReadFloat(power_)) || + (!parcel.ReadFloat(maxRange_)) || + (!parcel.ReadFloat(resolution_)) || + (!parcel.ReadUint32(flags_)) || + (!parcel.ReadInt32(fifoMaxEventCount_)) || + (!parcel.ReadInt64(minSamplePeriodNs_)) || + (!parcel.ReadInt64(maxSamplePeriodNs_))) { + return false; + } return true; } } // namespace Sensors diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 0e7ef0777eaf5c1609e4b19e50d3a17995c80444..3532b9cfb6f135c153e88f0c96db617cdd935844 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -28,7 +28,7 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorBasicChannel" }; -constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(SensorEvent) * 100; +constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(SensorData) * 100; constexpr int32_t DEFAULT_CHANNEL_SIZE = 2 * 1024; constexpr int32_t SOCKET_PAIR_SIZE = 2; } // namespace @@ -55,48 +55,46 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel() return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } // set socket attr - int32_t ret = setsockopt(socketPair[0], SOL_SOCKET, SO_SNDBUF, &SENSOR_READ_DATA_SIZE, - sizeof(SENSOR_READ_DATA_SIZE)); - if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 0 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (setsockopt(socketPair[0], SOL_SOCKET, SO_SNDBUF, &SENSOR_READ_DATA_SIZE, sizeof(SENSOR_READ_DATA_SIZE)) != 0) { + SEN_HILOGE("setsockopt socketpair 0, SNDBUF failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - ret = setsockopt(socketPair[1], SOL_SOCKET, SO_RCVBUF, &SENSOR_READ_DATA_SIZE, sizeof(SENSOR_READ_DATA_SIZE)); - if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 1 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (setsockopt(socketPair[1], SOL_SOCKET, SO_RCVBUF, &SENSOR_READ_DATA_SIZE, sizeof(SENSOR_READ_DATA_SIZE)) != 0) { + SEN_HILOGE("setsockopt socketpair 1, RCVBUF failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - int32_t bufferSize = DEFAULT_CHANNEL_SIZE; - ret = setsockopt(socketPair[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); - if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 0 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (setsockopt(socketPair[0], SOL_SOCKET, SO_RCVBUF, &DEFAULT_CHANNEL_SIZE, sizeof(DEFAULT_CHANNEL_SIZE)) != 0) { + SEN_HILOGE("setsockopt socketpair 0, RCVBUF failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - ret = setsockopt(socketPair[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); - if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 1 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (setsockopt(socketPair[1], SOL_SOCKET, SO_SNDBUF, &DEFAULT_CHANNEL_SIZE, sizeof(DEFAULT_CHANNEL_SIZE)) != 0) { + SEN_HILOGE("setsockopt socketpair 1, SNDBUF failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - ret = fcntl(socketPair[0], F_SETFL, O_NONBLOCK); - if (ret != 0) { - SEN_HILOGE("fcntl socketpair 0 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (fcntl(socketPair[0], F_SETFL, O_NONBLOCK) != 0) { + SEN_HILOGE("fcntl socketpair 0 failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - ret = fcntl(socketPair[1], F_SETFL, O_NONBLOCK); - if (ret != 0) { - SEN_HILOGE("fcntl socketpair 1 failed"); - return SENSOR_CHANNEL_SOCKET_CREATE_ERR; + if (fcntl(socketPair[1], F_SETFL, O_NONBLOCK) != 0) { + SEN_HILOGE("fcntl socketpair 1 failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } sendFd_ = socketPair[0]; receiveFd_ = socketPair[1]; - SEN_HILOGD("create socketpair success,receiveFd_:%{public}d,sendFd_:%{public}d", receiveFd_, sendFd_); return ERR_OK; + + CLOSE_SOCK: + close(socketPair[0]); + close(socketPair[1]); + sendFd_ = -1; + receiveFd_ = -1; + return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } int32_t SensorBasicDataChannel::CreateSensorBasicChannel(MessageParcel &data) { CALL_LOG_ENTER; - if ((sendFd_ != -1) || (receiveFd_ != -1)) { + if (sendFd_ != -1) { SEN_HILOGD("already create socketpair"); return ERR_OK; } @@ -202,7 +200,7 @@ int32_t SensorBasicDataChannel::DestroySensorBasicChannel() return ERR_OK; } -const std::unordered_map &SensorBasicDataChannel::GetDataCacheBuf() const +const std::unordered_map &SensorBasicDataChannel::GetDataCacheBuf() const { return dataCacheBuf_; }