From 1c1338923d0e5779a2be7edfca2ea087391874a1 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 26 May 2023 08:44:22 +0000 Subject: [PATCH] add color and sar Signed-off-by: hellohyh001 Change-Id: Ie228e00fe8269b6c910792c72b6ac096b01b84cd --- interfaces/native/include/sensor_agent_type.h | 2 + interfaces/plugin/src/sensor_js.cpp | 4 + interfaces/plugin/src/sensor_napi_utils.cpp | 4 +- .../hardware/include/hdi_service_impl.h | 6 +- .../hardware/src/hdi_service_impl.cpp | 35 ++++++-- .../interface/include/sensor_hdi_connection.h | 2 + .../interface/src/sensor_hdi_connection.cpp | 85 ++++++++++++++++++- 7 files changed, 126 insertions(+), 12 deletions(-) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 46d94880..45659af3 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -76,6 +76,8 @@ typedef enum SensorTypeId { SENSOR_TYPE_ID_GESTURE = 11, /**< Gesture sensor */ SENSOR_TYPE_ID_PROXIMITY = 12, /**< Proximity sensor */ SENSOR_TYPE_ID_HUMIDITY = 13, /**< Humidity sensor */ + SENSOR_TYPE_ID_COLOR = 14, /**< Color sensor */ + SENSOR_TYPE_ID_SAR = 15, /**< Sar sensor */ SENSOR_TYPE_ID_PHYSICAL_MAX = 0xFF, /**< Maximum type ID of a physical sensor */ SENSOR_TYPE_ID_ORIENTATION = 256, /**< Orientation sensor */ SENSOR_TYPE_ID_GRAVITY = 257, /**< Gravity sensor */ diff --git a/interfaces/plugin/src/sensor_js.cpp b/interfaces/plugin/src/sensor_js.cpp index 2059d60b..05778388 100644 --- a/interfaces/plugin/src/sensor_js.cpp +++ b/interfaces/plugin/src/sensor_js.cpp @@ -1251,6 +1251,8 @@ static napi_value CreateEnumSensorType(napi_env env, napi_value exports) DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_HALL", GetNapiInt32(env, SENSOR_TYPE_ID_HALL)), DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_PROXIMITY", GetNapiInt32(env, SENSOR_TYPE_ID_PROXIMITY)), DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_HUMIDITY", GetNapiInt32(env, SENSOR_TYPE_ID_HUMIDITY)), + DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_COLOR", GetNapiInt32(env, SENSOR_TYPE_ID_COLOR)), + DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_SAR", GetNapiInt32(env, SENSOR_TYPE_ID_SAR)), DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_ORIENTATION", GetNapiInt32(env, SENSOR_TYPE_ID_ORIENTATION)), DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_GRAVITY", GetNapiInt32(env, SENSOR_TYPE_ID_GRAVITY)), DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_LINEAR_ACCELERATION", @@ -1291,6 +1293,8 @@ static napi_value CreateEnumSensorId(napi_env env, napi_value exports) DECLARE_NAPI_STATIC_PROPERTY("HALL", GetNapiInt32(env, SENSOR_TYPE_ID_HALL)), DECLARE_NAPI_STATIC_PROPERTY("PROXIMITY", GetNapiInt32(env, SENSOR_TYPE_ID_PROXIMITY)), DECLARE_NAPI_STATIC_PROPERTY("HUMIDITY", GetNapiInt32(env, SENSOR_TYPE_ID_HUMIDITY)), + DECLARE_NAPI_STATIC_PROPERTY("COLOR", GetNapiInt32(env, SENSOR_TYPE_ID_COLOR)), + DECLARE_NAPI_STATIC_PROPERTY("SAR", GetNapiInt32(env, SENSOR_TYPE_ID_SAR)), DECLARE_NAPI_STATIC_PROPERTY("ORIENTATION", GetNapiInt32(env, SENSOR_TYPE_ID_ORIENTATION)), DECLARE_NAPI_STATIC_PROPERTY("GRAVITY", GetNapiInt32(env, SENSOR_TYPE_ID_GRAVITY)), DECLARE_NAPI_STATIC_PROPERTY("LINEAR_ACCELEROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_LINEAR_ACCELERATION)), diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index fdfba387..bace7456 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -178,7 +178,9 @@ std::map> g_sensorAttributeList = { { SENSOR_TYPE_ID_PEDOMETER, { "steps" } }, { SENSOR_TYPE_ID_HEART_RATE, { "heartRate" } }, { SENSOR_TYPE_ID_WEAR_DETECTION, { "value" } }, - { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, { "x", "y", "z", "biasX", "biasY", "biasZ" } } + { SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, { "x", "y", "z", "biasX", "biasY", "biasZ" } }, + { SENSOR_TYPE_ID_COLOR, { "x", "y"} }, + { SENSOR_TYPE_ID_SAR, { "x" } } }; std::map g_convertfuncList = { diff --git a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h index 388f2b68..a7da7c9c 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -33,15 +33,15 @@ public: int32_t DisableSensor(int32_t sensorId); int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval); int32_t SetMode(int32_t sensorId, int32_t mode); - int32_t Register(RecordSensorCallback cb); + static int32_t Register(RecordSensorCallback cb); int32_t Unregister(); private: DISALLOW_COPY_AND_MOVE(HdiServiceImpl); static void DataReportThread(); - std::vector g_enableSensors; + static std::vector g_enableSensors; std::thread dataReportThread_; - static RecordSensorCallback callback_; + static std::vector callbacks_; static int64_t samplingInterval_; static int64_t reportInterval_; static std::atomic_bool isStop_; diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 0b6a4e3f..a05dcef2 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -30,15 +30,28 @@ constexpr int32_t CONVERT_MULTIPLES = 1000; std::vector g_sensorInfos = { {"sensor_test", "default", "1.0.0", "1.0.0", 0, 1, 9999.0, 0.000001, 23.0, 100000000, 1000000000}, }; -std::vector supportSensors = {1}; +std::vector supportSensors = {1, 14, 15}; float testData[] = {9.8, 0.0, 0.0}; +float colorData[] = {2.2, 3.3}; +float sarData[] = {8.8}; SensorEvent testEvent = { .sensorTypeId = 1, .data = (uint8_t *)testData, .dataLen = 12 }; +SensorEvent colorEvent = { + .sensorTypeId = 14, + .data = (uint8_t *)colorData, + .dataLen = 8 +}; +SensorEvent sarEvent = { + .sensorTypeId = 15, + .data = (uint8_t *)sarData, + .dataLen = 4 +}; } -RecordSensorCallback HdiServiceImpl::callback_; +std::vector HdiServiceImpl::g_enableSensors; +std::vector HdiServiceImpl::callbacks_; int64_t HdiServiceImpl::samplingInterval_ = -1; int64_t HdiServiceImpl::reportInterval_ = -1; std::atomic_bool HdiServiceImpl::isStop_ = false; @@ -55,7 +68,20 @@ void HdiServiceImpl::DataReportThread() CALL_LOG_ENTER; while (true) { usleep(samplingInterval_ / CONVERT_MULTIPLES); - callback_(&testEvent); + for (const auto &it : callbacks_) { + if (it == nullptr) { + continue; + } + for (const auto &iter : g_enableSensors) { + if (iter == 14) { + it(&colorEvent); + } else if (iter == 15) { + it(&sarEvent); + } else { + it(&testEvent); + } + } + } if (isStop_) { break; } @@ -67,7 +93,6 @@ void HdiServiceImpl::DataReportThread() int32_t HdiServiceImpl::EnableSensor(int32_t sensorId) { CALL_LOG_ENTER; - CHKPR(callback_, ERROR); if (std::find(supportSensors.begin(), supportSensors.end(), sensorId) == supportSensors.end()) { SEN_HILOGE("not support enable sensorId:%{public}d", sensorId); return ERR_NO_INIT; @@ -132,7 +157,7 @@ int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) int32_t HdiServiceImpl::Register(RecordSensorCallback cb) { CHKPR(cb, ERROR); - callback_ = cb; + callbacks_.push_back(cb); return ERR_OK; } diff --git a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h index f027c460..9c872ce3 100644 --- a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -37,8 +37,10 @@ public: private: DISALLOW_COPY_AND_MOVE(SensorHdiConnection); std::unique_ptr iSensorHdiConnection_; + std::unique_ptr iSensorCompatibleHdiConnection_; std::vector sensorList_; int32_t ConnectHdiService(); + int32_t ConnectCompatibleHdi(); }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index 4491611b..ddec2921 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -38,6 +38,10 @@ int32_t SensorHdiConnection::ConnectHdi() if (ret != ERR_OK) { SEN_HILOGE("connect hdi failed"); } + ret = ConnectCompatibleHdi(); + if (ret != ERR_OK) { + SEN_HILOGE("connect compatible hdi failed"); + } return ERR_OK; } @@ -56,16 +60,63 @@ int32_t SensorHdiConnection::ConnectHdiService() return ERR_OK; } +int32_t SensorHdiConnection::ConnectCompatibleHdi() +{ + iSensorCompatibleHdiConnection_ = std::make_unique(); + int32_t ret = iSensorCompatibleHdiConnection_->ConnectHdi(); + if (ret != ERR_OK) { + SEN_HILOGE("connect hdi compatible service failed"); + return CONNECT_SENSOR_HDF_ERR; + } + return ERR_OK; +} + int32_t SensorHdiConnection::GetSensorList(std::vector& sensorList) { sensorList.assign(sensorList_.begin(), sensorList_.end()); + Sensor sensor1; + sensor1.SetSensorId(14); + sensor1.SetSensorTypeId(14); + sensor1.SetFirmwareVersion("1.0.1"); + sensor1.SetHardwareVersion("1.0.1"); + sensor1.SetMaxRange(9999.0); + sensor1.SetSensorName("sensor_color"); + sensor1.SetVendorName("default_color"); + sensor1.SetResolution(0.000001); + sensor1.SetPower(20.0); + sensor1.SetMinSamplePeriodNs(100000000); + sensor1.SetMaxSamplePeriodNs(1000000000); + sensorList.push_back(sensor1); + Sensor sensor2; + sensor2.SetSensorId(15); + sensor2.SetSensorTypeId(15); + sensor2.SetFirmwareVersion("1.0.2"); + sensor2.SetHardwareVersion("1.0.2"); + sensor2.SetMaxRange(9999.0); + sensor2.SetSensorName("sensor_sar"); + sensor2.SetVendorName("default_sar"); + sensor2.SetResolution(0.000002); + sensor2.SetPower(21.0); + sensor2.SetMinSamplePeriodNs(100000000); + sensor2.SetMaxSamplePeriodNs(1000000000); + sensorList.push_back(sensor2); return ERR_OK; } int32_t SensorHdiConnection::EnableSensor(int32_t sensorId) { StartTrace(HITRACE_TAG_SENSORS, "EnableSensor"); - int32_t ret = iSensorHdiConnection_->EnableSensor(sensorId); + int32_t ret { ENABLE_SENSOR_ERR }; + if (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR) { + ret = iSensorCompatibleHdiConnection_->EnableSensor(sensorId); + FinishTrace(HITRACE_TAG_SENSORS); + if (ret != ERR_OK) { + SEN_HILOGE("enable sensor failed in compatible, sensorId:%{public}d", sensorId); + return ENABLE_SENSOR_ERR; + } + return ret; + } + ret = iSensorHdiConnection_->EnableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); if (ret != 0) { SEN_HILOGI("enable sensor failed, sensorId:%{public}d", sensorId); @@ -77,7 +128,17 @@ int32_t SensorHdiConnection::EnableSensor(int32_t sensorId) int32_t SensorHdiConnection::DisableSensor(int32_t sensorId) { StartTrace(HITRACE_TAG_SENSORS, "DisableSensor"); - int32_t ret = iSensorHdiConnection_->DisableSensor(sensorId); + int32_t ret { ENABLE_SENSOR_ERR }; + if (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR) { + ret = iSensorCompatibleHdiConnection_->DisableSensor(sensorId); + FinishTrace(HITRACE_TAG_SENSORS); + if (ret != ERR_OK) { + SEN_HILOGE("disable sensor failed in compatible, sensorId:%{public}d", sensorId); + return DISABLE_SENSOR_ERR; + } + return ret; + } + ret = iSensorHdiConnection_->DisableSensor(sensorId); FinishTrace(HITRACE_TAG_SENSORS); if (ret != 0) { SEN_HILOGI("disable sensor failed, sensorId:%{public}d", sensorId); @@ -89,7 +150,17 @@ int32_t SensorHdiConnection::DisableSensor(int32_t sensorId) int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) { StartTrace(HITRACE_TAG_SENSORS, "SetBatch"); - int32_t ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); + int32_t ret { ENABLE_SENSOR_ERR }; + if (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR) { + ret = iSensorCompatibleHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); + FinishTrace(HITRACE_TAG_SENSORS); + if (ret != ERR_OK) { + SEN_HILOGI("set batch failed in compatible, sensorId:%{public}d", sensorId); + return SET_SENSOR_CONFIG_ERR; + } + return ret; + } + ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); FinishTrace(HITRACE_TAG_SENSORS); if (ret != 0) { SEN_HILOGI("set batch failed, sensorId:%{public}d", sensorId); @@ -114,6 +185,10 @@ int32_t SensorHdiConnection::RegisteDataReport(ReportDataCb cb, sptrRegisteDataReport(cb, reportDataCallback); + int32_t compatibleRet = iSensorCompatibleHdiConnection_->RegisteDataReport(cb, reportDataCallback); + if (compatibleRet != ERR_OK) { + SEN_HILOGE("registe dataReport failed in compatible"); + } FinishTrace(HITRACE_TAG_SENSORS); if (ret != 0) { SEN_HILOGI("registe dataReport failed"); @@ -129,6 +204,10 @@ int32_t SensorHdiConnection::DestroyHdiConnection() SEN_HILOGI("destroy hdi connection failed"); return DEVICE_ERR; } + int32_t compatibleRet = iSensorCompatibleHdiConnection_->DestroyHdiConnection(); + if (compatibleRet != ERR_OK) { + SEN_HILOGE("destroy hdi connection failed in compatible"); + } return ret; } } // namespace Sensors -- Gitee