From 0a619783b82e28a17e1f2e47d2bae3afe3d6a9d6 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 6 Jan 2022 18:21:56 +0800 Subject: [PATCH 1/6] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- services/sensor/BUILD.gn | 14 +- .../include/compatible_connection.h | 59 +++ .../src/compatible_connection.cpp | 163 +++++++ .../include/direct_connection.h} | 124 +++--- .../src/direct_connection.cpp} | 398 +++++++++--------- .../include/hdi_connection_v1_0.h | 60 +++ .../include/sensor_event_callback.h | 35 ++ .../src/hdi_connection_v1_0.cpp | 172 ++++++++ .../src/sensor_event_callback.cpp | 57 +++ .../hardware/include/hdi_service_impl.h | 64 +++ .../hardware/src/hdi_service_impl.cpp | 154 +++++++ .../include/i_sensor_hdi_connection.h | 61 +++ .../interface/include/sensor_hdi_connection.h | 57 +++ .../interface/src/sensor_hdi_connection.cpp | 145 +++++++ services/sensor/include/flush_info_record.h | 4 +- .../sensor/include/sensor_data_processer.h | 2 +- services/sensor/include/sensor_manager.h | 4 +- services/sensor/include/sensor_service.h | 7 +- services/sensor/src/client_info.cpp | 19 +- services/sensor/src/flush_info_record.cpp | 2 +- services/sensor/src/sensor_data_processer.cpp | 4 +- services/sensor/src/sensor_manager.cpp | 8 +- services/sensor/src/sensor_service.cpp | 25 +- utils/include/report_data_callback.h | 2 +- utils/include/sensors_errors.h | 3 + utils/src/report_data_callback.cpp | 18 +- 26 files changed, 1345 insertions(+), 316 deletions(-) mode change 100755 => 100644 services/sensor/BUILD.gn create mode 100644 services/sensor/hdi_connection/adapter/compatible_connection/include/compatible_connection.h create mode 100644 services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp rename services/sensor/{include/sensor_service_impl.h => hdi_connection/adapter/direct_connection/include/direct_connection.h} (48%) mode change 100755 => 100644 rename services/sensor/{src/sensor_service_impl.cpp => hdi_connection/adapter/direct_connection/src/direct_connection.cpp} (44%) mode change 100755 => 100644 create mode 100644 services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h create mode 100644 services/sensor/hdi_connection/adapter/v1_0_connection/include/sensor_event_callback.h create mode 100644 services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp create mode 100644 services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp create mode 100644 services/sensor/hdi_connection/hardware/include/hdi_service_impl.h create mode 100644 services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp create mode 100644 services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h create mode 100644 services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h create mode 100644 services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp mode change 100755 => 100644 services/sensor/include/flush_info_record.h mode change 100755 => 100644 services/sensor/include/sensor_data_processer.h mode change 100755 => 100644 services/sensor/include/sensor_manager.h mode change 100755 => 100644 services/sensor/include/sensor_service.h mode change 100755 => 100644 services/sensor/src/client_info.cpp mode change 100755 => 100644 services/sensor/src/flush_info_record.cpp mode change 100755 => 100644 services/sensor/src/sensor_data_processer.cpp mode change 100755 => 100644 services/sensor/src/sensor_manager.cpp mode change 100755 => 100644 services/sensor/src/sensor_service.cpp mode change 100755 => 100644 utils/include/report_data_callback.h mode change 100755 => 100644 utils/include/sensors_errors.h mode change 100755 => 100644 utils/src/report_data_callback.cpp diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn old mode 100755 new mode 100644 index 91a8ddf5..43251565 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -16,6 +16,12 @@ import("//build/ohos.gni") SUBSYSTEM_DIR = "//base/sensors" ohos_shared_library("libsensor_service") { sources = [ + "hdi_connection/interface/src/sensor_hdi_connection.cpp", + "hdi_connection/adapter/direct_connection/src/direct_connection.cpp", + "hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp", + "hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp", + "hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp", + "hdi_connection/hardware/src/hdi_service_impl.cpp", "src/client_info.cpp", "src/fifo_cache_data.cpp", "src/flush_info_record.cpp", @@ -24,7 +30,6 @@ ohos_shared_library("libsensor_service") { "src/sensor_dump.cpp", "src/sensor_manager.cpp", "src/sensor_service.cpp", - "src/sensor_service_impl.cpp", "src/sensor_service_stub.cpp", ] @@ -37,19 +42,24 @@ ohos_shared_library("libsensor_service") { "$SUBSYSTEM_DIR/sensor/utils/include", "$SUBSYSTEM_DIR/sensor/services/sensor/include", "//drivers/peripheral/sensor/interfaces/include", + "hdi_connection/interface/include", + "hdi_connection/adapter/direct_connection/include", + "hdi_connection/adapter/v1_0_connection/include", + "hdi_connection/hardware/include", + "hdi_connection/adapter/compatible_connection/include" ] deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", "//drivers/peripheral/sensor/hal:hdi_sensor", "//utils/native/base:utils", + "//drivers/peripheral/sensor/interfaces/hdi/sensor/v1_0:libsensor_proxy", ] external_deps = [ "aafwk_standard:base", "aafwk_standard:intent", "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/sensor/hdi_connection/adapter/compatible_connection/include/compatible_connection.h b/services/sensor/hdi_connection/adapter/compatible_connection/include/compatible_connection.h new file mode 100644 index 00000000..cf5d1900 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/compatible_connection/include/compatible_connection.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMPATIBLE_CONNECTION_H +#define COMPATIBLE_CONNECTION_H + +#include "hdi_service_impl.h" +#include "i_sensor_hdi_connection.h" + +namespace OHOS { +namespace Sensors { +class CompatibleConnection : public ISensorHdiConnection { +public: + CompatibleConnection() = default; + + virtual ~CompatibleConnection() {} + + int32_t ConnectHdi() override; + + int32_t GetSensorList(std::vector& sensorList) override; + + int32_t EnableSensor(uint32_t sensorId) override; + + int32_t DisableSensor(uint32_t sensorId) override; + + int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; + + int32_t SetMode(int32_t sensorId, int32_t mode) override; + + int32_t SetOption(int32_t sensorId, uint32_t option) override; + + int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + + int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; + + int32_t DestroyHdiConnection() override; + +private: + DISALLOW_COPY_AND_MOVE(CompatibleConnection); + static int32_t SensorDataCallback(const struct SensorEvents *event); + static ZReportDataCb reportDataCb_; + static sptr reportDataCallback_; + HdiServiceImpl &hdiServiceImpl_ = HdiServiceImpl::GetInstance(); +}; +} // namespace Sensors +} // namespace OHOS +#endif // COMPATIBLE_CONNECTION_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp new file mode 100644 index 00000000..b390bb52 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "compatible_connection.h" + +#include +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "CompatibleConnection" }; +} + +ZReportDataCb CompatibleConnection::reportDataCb_ = nullptr; +sptr CompatibleConnection::reportDataCallback_ = nullptr; + +int32_t CompatibleConnection::ConnectHdi() +{ + return ERR_OK; +} + +int32_t CompatibleConnection::GetSensorList(std::vector& sensorList) +{ + std::vector sensorInfos; + int32_t ret = hdiServiceImpl_.GetSensorList(sensorInfos); + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); + return ret; + } + HiLog::Info(LABEL, "%{public}s sensor size: %{public}d", __func__, sensorInfos.size()); + for (int32_t i = 0; i < sensorInfos.size(); i++) { + const std::string sensorName(sensorInfos[i].sensorName); + const std::string vendorName(sensorInfos[i].vendorName); + const int32_t sensorId = sensorInfos[i].sensorId; + const float power = sensorInfos[i].power; + const float maxRange = sensorInfos[i].maxRange; + HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", + __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); + Sensor sensor; + sensor.SetSensorId(sensorId); + sensor.SetMaxRange(maxRange); + sensor.SetName(sensorName.c_str()); + sensor.SetVendor(vendorName.c_str()); + sensorList.push_back(sensor); + } + return ERR_OK; +} + +int32_t CompatibleConnection::EnableSensor(uint32_t sensorId) +{ + int32_t ret = hdiServiceImpl_.EnableSensor(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s enable sensor failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +}; + +int32_t CompatibleConnection::DisableSensor(uint32_t sensorId) +{ + int32_t ret = hdiServiceImpl_.DisableSensor(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s disable sensor failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t CompatibleConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +{ + int32_t ret = hdiServiceImpl_.SetBatch(sensorId, samplingInterval, reportInterval); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s set batch failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode) +{ + int32_t ret = hdiServiceImpl_.SetMode(sensorId, mode); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set mode failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t CompatibleConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + return ERR_OK; +} + +int32_t CompatibleConnection::SetOption(int32_t sensorId, uint32_t option) +{ + int32_t ret = hdiServiceImpl_.SetOption(sensorId, option); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set option failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *event) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if ((event == nullptr) || (event->dataLen == 0)) { + HiLog::Error(LABEL, "%{public}s event is NULL", __func__); + return ERR_INVALID_VALUE; + } + + if (reportDataCb_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); + return ERR_NO_INIT; + } + (void)(reportDataCallback_->*reportDataCb_)(reinterpret_cast(event), + reportDataCallback_); + ISensorHdiConnection::dataCondition_.notify_one(); + return ERR_OK; +} + +int32_t CompatibleConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) +{ + if (reportDataCallback == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); + return ERR_INVALID_VALUE; + } + int32_t ret = hdiServiceImpl_.Register(SensorDataCallback); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + reportDataCb_ = cb; + reportDataCallback_ = reportDataCallback; + return ERR_OK; +} + +int32_t CompatibleConnection::DestroyHdiConnection() +{ + int32_t ret = hdiServiceImpl_.Unregister(); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/services/sensor/include/sensor_service_impl.h b/services/sensor/hdi_connection/adapter/direct_connection/include/direct_connection.h old mode 100755 new mode 100644 similarity index 48% rename from services/sensor/include/sensor_service_impl.h rename to services/sensor/hdi_connection/adapter/direct_connection/include/direct_connection.h index ba425f4b..1bfc5c86 --- a/services/sensor/include/sensor_service_impl.h +++ b/services/sensor/hdi_connection/adapter/direct_connection/include/direct_connection.h @@ -1,63 +1,61 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SENSOR_INTERFACE_H -#define SENSOR_INTERFACE_H - -#include "report_data_callback.h" -#include "errors.h" -#include "refbase.h" -#include "singleton.h" -#include "sensor.h" -#include "sensor_if.h" -#include "sensor_agent_type.h" -#include "nocopyable.h" - -namespace OHOS { -namespace Sensors { -class SensorServiceImpl : public Singleton { -public: - SensorServiceImpl() = default; - virtual ~SensorServiceImpl() = default; - std::vector GetSensorList() const; - - ErrCode EnableSensor(uint32_t sensorId) const; - - ErrCode DisableSensor(uint32_t sensorId) const; - - ErrCode RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) const; - - ErrCode SetSensorConfig(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) const; - - ErrCode InitSensorServiceImpl(); - - ErrCode RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback); - - ErrCode Unregister(void) const; - - static int32_t SensorDataCallback(const struct SensorEvents *event); - static std::mutex dataMutex_; - static std::condition_variable dataCondition_; - -private: - DISALLOW_COPY_AND_MOVE(SensorServiceImpl); - const struct SensorInterface *sensorInterface_ = nullptr; - ErrCode Register(RecordDataCallback cb) const; - static ZReportDataCb reportDataCb_; - static sptr reportDataCallback_; -}; -} // namespace Sensors -} // namespace OHOS -#endif // SENSOR_INTERFACE_H +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DIRECT_CONNECTION_H +#define DIRECT_CONNECTION_H + +#include "i_sensor_hdi_connection.h" +#include "sensor_agent_type.h" +#include "sensor_if.h" + +namespace OHOS { +namespace Sensors { +class DirectConnection : public ISensorHdiConnection { +public: + DirectConnection() = default; + + virtual ~DirectConnection() {} + + int32_t ConnectHdi() override; + + int32_t GetSensorList(std::vector& sensorList) override; + + int32_t EnableSensor(uint32_t sensorId) override; + + int32_t DisableSensor(uint32_t sensorId) override; + + int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; + + int32_t SetMode(int32_t sensorId, int32_t mode) override; + + int32_t SetOption(int32_t sensorId, uint32_t option) override; + + int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + + int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; + + int32_t DestroyHdiConnection() override; + +private: + DISALLOW_COPY_AND_MOVE(DirectConnection); + const struct SensorInterface *sensorInterface_ = nullptr; + int32_t Register(RecordDataCallback cb) const; + static int32_t SensorDataCallback(const struct SensorEvents *event); + static ZReportDataCb reportDataCb_; + static sptr reportDataCallback_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // DIRECT_CONNECTION_H \ No newline at end of file diff --git a/services/sensor/src/sensor_service_impl.cpp b/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp old mode 100755 new mode 100644 similarity index 44% rename from services/sensor/src/sensor_service_impl.cpp rename to services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp index 7d3ba250..5f5aa2aa --- a/services/sensor/src/sensor_service_impl.cpp +++ b/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp @@ -1,200 +1,198 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "sensor_service_impl.h" -#include -#include -#include "sensors_errors.h" -#include "sensors_log_domain.h" - -namespace OHOS { -namespace Sensors { -using namespace OHOS::HiviewDFX; - -namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "SensorServiceImpl" }; -} - -ZReportDataCb SensorServiceImpl::reportDataCb_ = nullptr; -sptr SensorServiceImpl::reportDataCallback_ = nullptr; -std::mutex SensorServiceImpl::dataMutex_; -std::condition_variable SensorServiceImpl::dataCondition_; - -ErrCode SensorServiceImpl::InitSensorServiceImpl() -{ - HiLog::Info(LABEL, "%{public}s begin", "InitSensorServiceImpl"); - sensorInterface_ = NewSensorInterfaceInstance(); - if (sensorInterface_ == nullptr) { - HiLog::Error(LABEL, " %{public}s,", "test sensorHdi get Module instance failed\n\r"); - return ERR_INVALID_VALUE; - } - HiLog::Info(LABEL, "%{public}s end", "InitSensorServiceImpl"); - return ERR_OK; -} - -std::vector SensorServiceImpl::GetSensorList() const -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - struct SensorInformation *sensorInfo = nullptr; - int32_t count = 0; - int32_t ret = sensorInterface_->GetAllSensors(&sensorInfo, &count); - if (ret != 0) { - HiLog::Error(LABEL, "Get sensor list failed!"); - return std::vector(); - } - HiLog::Info(LABEL, "GetAllSensors result: %{public}d, count: %{public}d", ret, count); - std::vector list; - for (int i = 0; i < count; i++) { - const std::string sensorName(sensorInfo->sensorName); - const std::string vendorName(sensorInfo->vendorName); - const int32_t sensorId = sensorInfo->sensorId; - const float power = sensorInfo->power; - const float maxRange = sensorInfo->maxRange; - HiLog::Info(LABEL, " %{public}d, %{public}s, %{public}s, %{public}f", sensorId, sensorName.c_str(), - vendorName.c_str(), power); - Sensor sensor; - sensor.SetSensorId(sensorId); - sensor.SetMaxRange(maxRange); - sensor.SetName(sensorName.c_str()); - sensor.SetVendor(vendorName.c_str()); - list.push_back(sensor); - sensorInfo++; - } - HiLog::Info(LABEL, "%{public}s end", __func__); - return list; -} - -ErrCode SensorServiceImpl::EnableSensor(uint32_t sensorId) const -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t ret = sensorInterface_->Enable(sensorId); - if (ret < 0) { - HiLog::Error(LABEL, "%{public}s is failed", __func__); - return -1; - } - HiLog::Info(LABEL, "%{public}s end", __func__); - return ERR_OK; -} - -ErrCode SensorServiceImpl::DisableSensor(uint32_t sensorId) const -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t ret = sensorInterface_->Disable(sensorId); - if (ret < 0) { - HiLog::Error(LABEL, "%{public}s is failed", __func__); - return -1; - } - HiLog::Info(LABEL, "%{public}s end", __func__); - return ERR_OK; -} - -ErrCode SensorServiceImpl::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) const -{ - return ERR_OK; -} - -ErrCode SensorServiceImpl::SetSensorConfig(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) const -{ - HiLog::Debug(LABEL, "%{public}s begin", __func__); - int32_t ret = sensorInterface_->SetBatch(sensorId, samplingPeriodNs, maxReportDelayNs); - if (SENSOR_NOT_SUPPORT == ret) { - HiLog::Debug(LABEL, "%{public}s : The operation is not supported!", __func__); - return SENSOR_NOT_SUPPORT; - } - if (SENSOR_INVALID_PARAM == ret) { - HiLog::Debug(LABEL, "%{public}s : The sensor parameter is invalid", __func__); - return SENSOR_INVALID_PARAM; - } - HiLog::Debug(LABEL, "%{public}s end", __func__); - return ret; -} - -int32_t SensorServiceImpl::SensorDataCallback(const struct SensorEvents *event) -{ - HiLog::Debug(LABEL, "%{public}s begin", __func__); - const int32_t SENSOR_AXISZ = 2; - if ((event == nullptr) || (event->dataLen == 0)) { - HiLog::Error(LABEL, "%{public}s event is NULL", __func__); - return ERR_INVALID_VALUE; - } - - float *data = (float*)event->data; - if (event->sensorId == 0) { - printf("sensor id [%d] data [%f]\n\r", event->sensorId, *(data)); - HiLog::Info(LABEL, "sensor id: %{public}d, data: %{public}f", event->sensorId, *(data)); - if (fabs(*data) > 1e-5) { - } - } else if (event->sensorId == 1) { - printf("sensor id [%d] x-[%f] y-[%f] z-[%f]\n\r", - event->sensorId, (*data), *(data + 1), *(data + SENSOR_AXISZ)); - HiLog::Info(LABEL, "sensor id: %{public}d x-%{public}f y-%{public}f z-%{public}f\n\r", - event->sensorId, (*data), *(data + 1), *(data + SENSOR_AXISZ)); - } - - if (reportDataCb_ == nullptr) { - HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); - return ERR_INVALID_VALUE; - } - (void)(reportDataCallback_->*reportDataCb_)(reinterpret_cast(event), - reportDataCallback_); - dataCondition_.notify_one(); - return ERR_OK; -} - -ErrCode SensorServiceImpl::Register(RecordDataCallback cb) const -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - if (sensorInterface_ == nullptr) { - HiLog::Error(LABEL, " %{public}s,", "test sensorHdi get Module instance failed\n\r"); - return ERR_INVALID_VALUE; - } - int32_t ret = sensorInterface_->Register(0, cb); - if (ret < 0) { - HiLog::Error(LABEL, "%{public}s failed", __func__); - return ERR_INVALID_VALUE; - } - HiLog::Info(LABEL, "%{public}s end", __func__); - return ERR_OK; -} - -ErrCode SensorServiceImpl::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - if (reportDataCallback == nullptr) { - HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); - return ERR_NO_INIT; - } - - Register(SensorDataCallback); - - reportDataCb_ = cb; - reportDataCallback_ = reportDataCallback; - HiLog::Info(LABEL, "%{public}s end", __func__); - return ERR_OK; -} - -ErrCode SensorServiceImpl::Unregister(void) const -{ - HiLog::Info(LABEL, "%{public}s begin", __func__); - int32_t ret = sensorInterface_->Unregister(0); - if (ret < 0) { - HiLog::Error(LABEL, "%{public}s failed", __func__); - return ERR_INVALID_VALUE; - } - HiLog::Info(LABEL, "%{public}s end", __func__); - return ERR_OK; -} -} // namespace Sensors -} // namespace OHOS +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "direct_connection.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "securec.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "DirectConnection" }; +} + +ZReportDataCb DirectConnection::reportDataCb_ = nullptr; +sptr DirectConnection::reportDataCallback_ = nullptr; +std::mutex ISensorHdiConnection::dataMutex_; +std::condition_variable ISensorHdiConnection::dataCondition_; + +int32_t DirectConnection::ConnectHdi() +{ + sensorInterface_ = NewSensorInterfaceInstance(); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect hdi failed", __func__); + return ERR_NO_INIT; + } + return ERR_OK; +} + +int32_t DirectConnection::GetSensorList(std::vector& sensorList) +{ + HiLog::Info(LABEL, "%{public}s begin", __func__); + struct SensorInformation *sensorInfo = nullptr; + int32_t count = 0; + int32_t ret = sensorInterface_->GetAllSensors(&sensorInfo, &count); + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); + return ret; + } + for (int32_t i = 0; i < count; i++) { + const std::string sensorName(sensorInfo->sensorName); + const std::string vendorName(sensorInfo->vendorName); + const int32_t sensorId = sensorInfo->sensorId; + const float power = sensorInfo->power; + const float maxRange = sensorInfo->maxRange; + HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", + __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); + Sensor sensor; + sensor.SetSensorId(sensorId); + sensor.SetMaxRange(maxRange); + sensor.SetName(sensorName.c_str()); + sensor.SetVendor(vendorName.c_str()); + sensorList.push_back(sensor); + sensorInfo++; + } + return ERR_OK; +} + +int32_t DirectConnection::EnableSensor(uint32_t sensorId) +{ + int32_t ret = sensorInterface_->Enable(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +}; + +int32_t DirectConnection::DisableSensor(uint32_t sensorId) +{ + int32_t ret = sensorInterface_->Disable(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t DirectConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +{ + int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set batch failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t DirectConnection::SetMode(int32_t sensorId, int32_t mode) +{ + int32_t ret = sensorInterface_->SetMode(sensorId, mode); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set mode failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t DirectConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + return ERR_OK; +} + +int32_t DirectConnection::SetOption(int32_t sensorId, uint32_t option) +{ + int32_t ret = sensorInterface_->SetOption(sensorId, option); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set option failed, sensorId: %{public}d", __func__, sensorId); + return ret; + } + return ERR_OK; +} + +int32_t DirectConnection::SensorDataCallback(const struct SensorEvents *event) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + if ((event == nullptr) || (event->dataLen == 0)) { + HiLog::Error(LABEL, "%{public}s event is NULL", __func__); + return ERR_INVALID_VALUE; + } + + if (reportDataCb_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); + return ERR_NO_INIT; + } + struct SensorEvent sensorEvent = { + .sensorTypeId = event->sensorId, + .version = event->version, + .timestamp = event->timestamp, + .option = event->option, + .mode = event->mode, + .dataLen = event->dataLen + }; + sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; + if (memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen) != EOK) { + HiLog::Error(LABEL, "%{public}s copy data failed", __func__); + return COPY_ERR; + } + (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); + ISensorHdiConnection::dataCondition_.notify_one(); + return ERR_OK; +} + +int32_t DirectConnection::Register(RecordDataCallback cb) const +{ + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, " %{public}s,", "test sensorHdi get Module instance failed\n\r"); + return ERR_INVALID_VALUE; + } + int32_t ret = sensorInterface_->Register(0, cb); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t DirectConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) +{ + if (reportDataCallback == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); + return ERR_NO_INIT; + } + int32_t ret = Register(SensorDataCallback); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + reportDataCb_ = cb; + reportDataCallback_ = reportDataCallback; + return ERR_OK; +} + +int32_t DirectConnection::DestroyHdiConnection() +{ + int32_t ret = sensorInterface_->Unregister(0); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h new file mode 100644 index 00000000..3b249490 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_CONNECTION_V1_0_H +#define HDI_CONNECTION_V1_0_H + +#include "i_sensor_hdi_connection.h" + +namespace OHOS { +namespace Sensors { +class HdiConnectionV1_0 : public ISensorHdiConnection { +public: + HdiConnectionV1_0() = default; + + virtual ~HdiConnectionV1_0() {} + + int32_t ConnectHdi() override; + + int32_t GetSensorList(std::vector& sensorList) override; + + int32_t EnableSensor(uint32_t sensorId) override; + + int32_t DisableSensor(uint32_t sensorId) override; + + int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; + + int32_t SetMode(int32_t sensorId, int32_t mode) override; + + int32_t SetOption(int32_t sensorId, uint32_t option) override; + + int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + + int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; + + int32_t DestroyHdiConnection() override; + + ZReportDataCb getReportDataCb(); + + sptr getReportDataCallback(); + +private: + DISALLOW_COPY_AND_MOVE(HdiConnectionV1_0); + static ZReportDataCb reportDataCb_; + static sptr reportDataCallback_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // HDI_CONNECTION_V1_0_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/include/sensor_event_callback.h b/services/sensor/hdi_connection/adapter/v1_0_connection/include/sensor_event_callback.h new file mode 100644 index 00000000..de14db78 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/include/sensor_event_callback.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_EVENT_CALLBACK_H +#define SENSOR_EVENT_CALLBACK_H + +#include "sensor_callback_service.h" +using hdi::sensor::v1_0::HdfSensorEvents; +using hdi::sensor::v1_0::SensorCallbackStub; + +namespace OHOS { +namespace Sensors { +class SensorEventCallback : public SensorCallbackStub { +public: + + virtual ~SensorEventCallback() {} + + int32_t OnDataEvent(const HdfSensorEvents& event) override; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_EVENT_CALLBACK_H + diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp new file mode 100644 index 00000000..ce11e12c --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hdi_connection_v1_0.h" + +#include "sensor_interface_proxy.h" +#include "sensor_event_callback.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +using hdi::sensor::v1_0::ISensorInterface; +using hdi::sensor::v1_0::ISensorCallback; +using hdi::sensor::v1_0::HdfSensorInformation; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnectionV1_0" }; +sptr sensorInterface_ = nullptr; +sptr eventCallback_ = new SensorEventCallback(); +} + +ZReportDataCb HdiConnectionV1_0::reportDataCb_ = nullptr; +sptr HdiConnectionV1_0::reportDataCallback_ = nullptr; + +int32_t HdiConnectionV1_0::ConnectHdi() +{ + sensorInterface_ = ISensorInterface::Get(); + if (sensorInterface_ == nullptr) { + HiLog::Error(LABEL, "%{public}s connect v1_0 hdi failed", __func__); + return ERR_NO_INIT; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::GetSensorList(std::vector& sensorList) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + std::vector sensorInfos; + int32_t ret = sensorInterface_->GetAllSensorInfo(sensorInfos); + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); + return ret; + } + HiLog::Info(LABEL, "%{public}s sensor size: %{public}d", __func__, sensorInfos.size()); + for (int32_t i = 0; i < sensorInfos.size(); i++) { + const std::string sensorName(sensorInfos[i].sensorName); + const std::string vendorName(sensorInfos[i].vendorName); + const int32_t sensorId = sensorInfos[i].sensorId; + const float power = sensorInfos[i].power; + const float maxRange = sensorInfos[i].maxRange; + HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", + __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); + Sensor sensor; + sensor.SetSensorId(sensorId); + sensor.SetMaxRange(maxRange); + sensor.SetName(sensorName.c_str()); + sensor.SetVendor(vendorName.c_str()); + sensorList.push_back(sensor); + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::EnableSensor(uint32_t sensorId) +{ + int32_t ret = sensorInterface_->Enable(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::DisableSensor(uint32_t sensorId) +{ + int32_t ret = sensorInterface_->Disable(sensorId); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +{ + int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::SetMode(int32_t sensorId, int32_t mode) +{ + int32_t ret = sensorInterface_->SetMode(sensorId, mode); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::SetOption(int32_t sensorId, uint32_t option) +{ + int32_t ret = sensorInterface_->SetOption(sensorId, option); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s is failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) +{ + if (reportDataCallback == nullptr) { + HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); + return ERR_NO_INIT; + } + int32_t ret = sensorInterface_->Register(0, eventCallback_); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + reportDataCb_ = cb; + reportDataCallback_ = reportDataCallback; + return ERR_OK; +} + +int32_t HdiConnectionV1_0::DestroyHdiConnection() +{ + int32_t ret = sensorInterface_->Unregister(0); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnectionV1_0::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + return 0; +} + +ZReportDataCb HdiConnectionV1_0::getReportDataCb() +{ + if (reportDataCb_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); + } + return reportDataCb_; +} + +sptr HdiConnectionV1_0::getReportDataCallback() +{ + if (reportDataCallback_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCallback_ cannot be null", __func__); + } + return reportDataCallback_; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp new file mode 100644 index 00000000..03536e40 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sensor_event_callback.h" + +#include "hdi_connection_v1_0.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { + +using namespace OHOS::HiviewDFX; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnectionV1_0" }; +std::unique_ptr hdiConnectionV1_0_ = std::make_unique(); +} + +int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) +{ + HiLog::Debug(LABEL, "%{public}s begin", __func__); + struct SensorEvent sensorEvent = { + .sensorTypeId = event.sensorId, + .version = event.version, + .timestamp = event.timestamp, + .option = event.option, + .mode = event.mode, + .dataLen = event.dataLen + }; + sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; + for (int32_t i = 0; i < event.data.size(); i ++ ) { + sensorEvent.data[i] = event.data[i]; + } + ZReportDataCb reportDataCb_ = hdiConnectionV1_0_->getReportDataCb(); + sptr reportDataCallback_ = hdiConnectionV1_0_->getReportDataCallback(); + if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); + return ERR_NO_INIT; + } + (void)(reportDataCallback_->*(reportDataCb_))(&sensorEvent, reportDataCallback_); + ISensorHdiConnection::dataCondition_.notify_one(); + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h new file mode 100644 index 00000000..09ec5559 --- /dev/null +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HDI_SERVICE_IMPL_H +#define HDI_SERVICE_IMPL_H + +#include "sensor_if.h" +#include "sensor_agent_type.h" +#include "singleton.h" +#include +#include +#include + +namespace OHOS { +namespace Sensors { +class HdiServiceImpl : public Singleton{ +public: + HdiServiceImpl() = default; + + virtual ~HdiServiceImpl() {} + + int32_t GetSensorList(std::vector& sensorList); + + int32_t EnableSensor(uint32_t sensorId); + + int32_t DisableSensor(uint32_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 SetOption(int32_t sensorId, uint32_t option); + + int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params); + + int32_t Register(RecordDataCallback cb); + + int32_t Unregister(); + +private: + DISALLOW_COPY_AND_MOVE(HdiServiceImpl); + static void DataReportThread(); + std::vector g_enableSensos; + std::thread dataReportThread_; + static RecordDataCallback g_callback; + static int64_t g_samplingInterval; + static int64_t g_reportInterval; + static std::atomic_bool g_isStop; +}; +} // namespace Sensors +} // namespace OHOS +#endif // HDI_SERVICE_IMPL_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp new file mode 100644 index 00000000..2fa03849 --- /dev/null +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hdi_service_impl.h" + +#include "sensors_errors.h" +#include "sensors_log_domain.h" +#include "unistd.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiServiceImpl" }; + +std::vector g_sensorInfos = { + {"sensor_test", "default", "1.0.0", "1.0.0", 0, 0, 9999.0, 0.000001, 23.0}, +}; +std::vector supportSensors = {0}; +uint8_t testData[] = {(uint8_t)9806.649414}; +constexpr struct SensorEvents testEvent = { + .sensorId = 0, + .data = testData, + .dataLen = 4 +}; +} +RecordDataCallback HdiServiceImpl::g_callback; +int64_t HdiServiceImpl::g_samplingInterval = -1; +int64_t HdiServiceImpl::g_reportInterval = -1; +std::atomic_bool HdiServiceImpl::g_isStop = false; + +int32_t HdiServiceImpl::GetSensorList(std::vector& sensorList) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + sensorList.assign(g_sensorInfos.begin(), g_sensorInfos.end()); + return ERR_OK; +} + +void HdiServiceImpl::DataReportThread() +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + while (true) { + usleep(g_samplingInterval / 1000); + g_callback(&testEvent); + if (g_isStop) { + break; + } + } + HiLog::Info(LABEL, "%{public}s thread stop", __func__); + return; +} + +int32_t HdiServiceImpl::EnableSensor(uint32_t sensorId) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + if (g_callback == nullptr) { + HiLog::Error(LABEL, "%{public}s enable sensor failed", __func__); + return -1; + } + if (std::count(supportSensors.begin(), supportSensors.end(), sensorId) == 0) { + HiLog::Error(LABEL, "%{public}s not support enable sensorId: %{public}d", __func__, sensorId); + return -1; + } + if (std::count(g_enableSensos.begin(), g_enableSensos.end(), sensorId) != 0) { + HiLog::Info(LABEL, "%{public}s sensorId: %{public}d has been enabled", __func__, sensorId); + return ERR_OK; + } + g_enableSensos.push_back(sensorId); + if (!dataReportThread_.joinable() || g_isStop) { + if (dataReportThread_.joinable()) { + dataReportThread_.join(); + } + std::thread senocdDataThread(HdiServiceImpl::DataReportThread); + dataReportThread_ = std::move(senocdDataThread); + g_isStop = false; + } + return ERR_OK; +}; + +int32_t HdiServiceImpl::DisableSensor(uint32_t sensorId) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + if (std::count(supportSensors.begin(), supportSensors.end(), sensorId) == 0) { + HiLog::Error(LABEL, "%{public}s not support disable sensorId: %{public}d", __func__, sensorId); + return -1; + } + if (std::count(g_enableSensos.begin(), g_enableSensos.end(), sensorId) == 0) { + HiLog::Error(LABEL, "%{public}s sensorId: %{public}d should be enable first", __func__, sensorId); + return -1; + } + auto iter = std::remove(g_enableSensos.begin(), g_enableSensos.end(), sensorId); + g_enableSensos.erase(iter, g_enableSensos.end()); + if (g_enableSensos.empty()) { + g_isStop = true; + } + return ERR_OK; +} + +int32_t HdiServiceImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +{ + HiLog::Info(LABEL, "%{public}s in", __func__); + if (samplingInterval < 0 || reportInterval < 0) { + samplingInterval = 200000000; + reportInterval = 0; + } + g_samplingInterval = samplingInterval; + g_reportInterval = reportInterval; + return ERR_OK; +} + +int32_t HdiServiceImpl::SetMode(int32_t sensorId, int32_t mode) +{ + return ERR_OK; +} + +int32_t HdiServiceImpl::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + return ERR_OK; +} + +int32_t HdiServiceImpl::SetOption(int32_t sensorId, uint32_t option) +{ + return ERR_OK; +} + +int32_t HdiServiceImpl::Register(RecordDataCallback cb) +{ + if (cb == nullptr) { + HiLog::Error(LABEL, "%{public}s cb cannot be null", __func__); + return -1; + } + g_callback = cb; + return ERR_OK; +} + +int32_t HdiServiceImpl::Unregister() +{ + g_isStop = true; + return ERR_OK; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h new file mode 100644 index 00000000..66c6cc49 --- /dev/null +++ b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef I_SENSOR_HDI_CONNECTION_H +#define I_SENSOR_HDI_CONNECTION_H + +#include "report_data_callback.h" +#include "sensor.h" +#include +#include + +namespace OHOS { +namespace Sensors { +class ISensorHdiConnection { +public: + ISensorHdiConnection() = default; + + virtual ~ISensorHdiConnection() = default; + + virtual int32_t ConnectHdi() = 0; + + virtual int32_t GetSensorList(std::vector& sensorList) = 0; + + virtual int32_t EnableSensor(uint32_t sensorId) = 0; + + virtual int32_t DisableSensor(uint32_t sensorId) = 0; + + virtual int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) = 0; + + virtual int32_t SetMode(int32_t sensorId, int32_t mode) = 0; + + virtual int32_t SetOption(int32_t sensorId, uint32_t option) = 0; + + virtual int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) = 0; + + virtual int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) = 0; + + virtual int32_t DestroyHdiConnection() = 0; + + static std::mutex dataMutex_; + + static std::condition_variable dataCondition_; + +private: + DISALLOW_COPY_AND_MOVE(ISensorHdiConnection); +}; +} // namespace Sensors +} // namespace OHOS +#endif // I_SENSOR_HDI_CONNECTION_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h new file mode 100644 index 00000000..787d250a --- /dev/null +++ b/services/sensor/hdi_connection/interface/include/sensor_hdi_connection.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SENSOR_HDI_CONNECTION_H +#define SENSOR_HDI_CONNECTION_H + +#include "i_sensor_hdi_connection.h" +#include "singleton.h" + +namespace OHOS { +namespace Sensors { +class SensorHdiConnection : public ISensorHdiConnection, public Singleton { +public: + SensorHdiConnection() = default; + + virtual ~SensorHdiConnection() {} + + int32_t ConnectHdi() override; + + int32_t GetSensorList(std::vector& sensorList) override; + + int32_t EnableSensor(uint32_t sensorId) override; + + int32_t DisableSensor(uint32_t sensorId) override; + + int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; + + int32_t SetMode(int32_t sensorId, int32_t mode) override; + + int32_t SetOption(int32_t sensorId, uint32_t option) override; + + int32_t RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) override; + + int32_t RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) override; + + int32_t DestroyHdiConnection() override; + +private: + DISALLOW_COPY_AND_MOVE(SensorHdiConnection); + std::unique_ptr iSensorHdiConnection_; + std::vector sensorList_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_HDI_CONNECTION_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp new file mode 100644 index 00000000..a41258ee --- /dev/null +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sensor_hdi_connection.h" + +#include "compatible_connection.h" +#include "direct_connection.h" +#include "hdi_connection_v1_0.h" +#include "sensors_errors.h" +#include "sensors_log_domain.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "SensorHdiConnection" }; +} + +int32_t SensorHdiConnection::ConnectHdi() +{ + iSensorHdiConnection_ = std::make_unique(); + int32_t ret = iSensorHdiConnection_->ConnectHdi(); + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s connect hdi v1_0 failed", __func__); + iSensorHdiConnection_ = std::make_unique(); + ret = iSensorHdiConnection_->ConnectHdi(); + } + + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s hdi direct connection failed", __func__); + iSensorHdiConnection_ = std::make_unique(); + ret = iSensorHdiConnection_->ConnectHdi(); + } + + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s hdi connection failed", __func__); + return CONNECT_SENSOR_HDF_ERR; + } + ret = iSensorHdiConnection_->GetSensorList(sensorList_); + if (ret != 0) { + HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); + return GET_SENSOR_LIST_ERR; + } + return ERR_OK; +} + +int32_t SensorHdiConnection::GetSensorList(std::vector& sensorList) +{ + sensorList.assign(sensorList_.begin(), sensorList_.end()); + return ERR_OK; +} + +int32_t SensorHdiConnection::EnableSensor(uint32_t sensorId) +{ + int32_t ret = iSensorHdiConnection_->EnableSensor(sensorId); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s enable sensor failed, sensorId: %{public}d", __func__, sensorId); + return ENABLE_SENSOR_ERR; + } + return ret; +}; + +int32_t SensorHdiConnection::DisableSensor(uint32_t sensorId) +{ + int32_t ret = iSensorHdiConnection_->DisableSensor(sensorId); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s disable sensor failed, sensorId: %{public}d", __func__, sensorId); + return DISABLE_SENSOR_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +{ + int32_t ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set batch failed, sensorId: %{public}d", __func__, sensorId); + return SET_SENSOR_CONFIG_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode) +{ + int32_t ret = iSensorHdiConnection_->SetMode(sensorId, mode); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set mode failed, sensorId: %{public}d", __func__, sensorId); + return SET_SENSOR_MODE_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::SetOption(int32_t sensorId, uint32_t option) +{ + int32_t ret = iSensorHdiConnection_->SetOption(sensorId, option); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s set option failed, sensorId: %{public}d", __func__, sensorId); + return SET_SENSOR_OPTION_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + int32_t ret = iSensorHdiConnection_->RunCommand(sensorId, cmd, params); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s run command failed, sensorId: %{public}d", __func__, sensorId); + return RUN_COMMAND_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) +{ + int32_t ret = iSensorHdiConnection_->RegisteDataReport(cb, reportDataCallback); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s registe dataReport failed", __func__); + return REGIST_CALLBACK_ERR; + } + return ret; +} + +int32_t SensorHdiConnection::DestroyHdiConnection() +{ + int32_t ret = iSensorHdiConnection_->DestroyHdiConnection(); + if (ret != 0) { + HiLog::Info(LABEL, "%{public}s destroy hdi connectionr failed", __func__); + return DEVICE_ERR; + } + return ret; +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/sensor/include/flush_info_record.h b/services/sensor/include/flush_info_record.h old mode 100755 new mode 100644 index c908430b..2b074c00 --- a/services/sensor/include/flush_info_record.h +++ b/services/sensor/include/flush_info_record.h @@ -23,7 +23,7 @@ #include "client_info.h" #include "refbase.h" #include "sensor_basic_data_channel.h" -#include "sensor_service_impl.h" +#include "sensor_hdi_connection.h" #include "sensors_errors.h" #include "singleton.h" #include "nocopyable.h" @@ -58,7 +58,7 @@ public: private: DISALLOW_COPY_AND_MOVE(FlushInfoRecord); - SensorServiceImpl &sensorServiceImpl_ = SensorServiceImpl::GetInstance(); + SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); // sensorId, channel pointer for pending flush. std::unordered_map> flushInfo_; diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h old mode 100755 new mode 100644 index ecee8b14..a4daddff --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -28,7 +28,7 @@ #include "report_data_callback.h" #include "sensor.h" #include "sensors_log_domain.h" -#include "sensor_service_impl.h" +#include "sensor_hdi_connection.h" #include "sensor_agent_type.h" namespace OHOS { diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h old mode 100755 new mode 100644 index cd17052b..dac1ec39 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -23,7 +23,7 @@ #include "client_info.h" #include "flush_info_record.h" #include "sensor_data_processer.h" -#include "sensor_service_impl.h" +#include "sensor_hdi_connection.h" #include "sensor_agent_type.h" namespace OHOS { @@ -43,7 +43,7 @@ public: void GetPackageNameFromUid(int32_t uid, std::string &packageName); private: - SensorServiceImpl &sensorServiceImpl_ = SensorServiceImpl::GetInstance(); + SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); std::thread dataThread_; sptr sensorDataProcesser_; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h old mode 100755 new mode 100644 index 141a385b..a6be0bd2 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -21,14 +21,13 @@ #include #include "client_info.h" -#include "sensor_service_impl.h" - #include "death_recipient_template.h" #include "nocopyable.h" +#include "sensor_agent_type.h" +#include "sensor_hdi_connection.h" #include "sensor_manager.h" #include "sensor_service_stub.h" #include "system_ability.h" -#include "sensor_agent_type.h" namespace OHOS { namespace Sensors { @@ -85,7 +84,7 @@ private: std::mutex sensorMapMutex_; std::vector sensors_; std::unordered_map sensorMap_; - SensorServiceImpl &sensorServiceImpl_ = SensorServiceImpl::GetInstance(); + SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp old mode 100755 new mode 100644 index 9ebb3c03..bd19f197 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -18,7 +18,7 @@ #include #include "securec.h" -#include "sensor_service_impl.h" +#include "sensor_hdi_connection.h" #include "sensors_errors.h" #include "sensors_log_domain.h" @@ -476,24 +476,23 @@ void ClientInfo::StoreEvent(const struct SensorEvent &event) { bool foundSensor = false; struct SensorEvent storedEvent; - auto sensorServiceImpl = &SensorServiceImpl::GetInstance(); - if (sensorServiceImpl == nullptr) { - HiLog::Error(LABEL, "%{public}s sensorServiceImpl cannot be null", __func__); + auto sensorHdiConnection = &SensorHdiConnection::GetInstance(); + if (sensorHdiConnection == nullptr) { + HiLog::Error(LABEL, "%{public}s sensorHdiConnection cannot be null", __func__); return; } - auto sensors = sensorServiceImpl->GetSensorList(); - size_t len = sensors.size(); - if (len == 0) { + std::vector sensors; + int32_t ret = sensorHdiConnection->GetSensorList(sensors); + if (ret < 0) { HiLog::Error(LABEL, "%{public}s GetSensorList failed", __func__); return; } - errno_t ret = memcpy_s(&storedEvent, sizeof(storedEvent), &event, sizeof(event)); - if (ret != EOK) { + if (memcpy_s(&storedEvent, sizeof(storedEvent), &event, sizeof(event)) != EOK) { HiLog::Error(LABEL, "%{public}s memcpy_s failed", __func__); return; } - for (size_t i = 0; i < len; i++) { + for (size_t i = 0; i < sensors.size(); i++) { if ((int32_t)(sensors[i].GetSensorId()) == storedEvent.sensorTypeId) { HiLog::Debug(LABEL, "%{public}s sensorFlags : %{public}u", __func__, sensors[i].GetFlags()); foundSensor = true; diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp old mode 100755 new mode 100644 index 80a1ba5c..9f349918 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -99,7 +99,7 @@ int32_t FlushInfoRecord::GetFlushChannelIndex(const std::vector dataCallback HiLog::Error(LABEL, "%{public}s dataCallback cannot be null", __func__); return INVALID_POINTER; } - std::unique_lock lk(SensorServiceImpl::dataMutex_); - SensorServiceImpl::dataCondition_.wait(lk); + std::unique_lock lk(ISensorHdiConnection::dataMutex_); + ISensorHdiConnection::dataCondition_.wait(lk); auto &eventsBuf = dataCallback->GetEventData(); if (eventsBuf.eventNum <= 0) { HiLog::Error(LABEL, "%{public}s data cannot be empty", __func__); diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp old mode 100755 new mode 100644 index 2413a6b9..b4c65e9c --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -69,9 +69,9 @@ bool SensorManager::SetBestSensorParams(uint32_t sensorId, int64_t samplingPerio bestSamplingPeriodNs = (samplingPeriodNs < bestSamplingPeriodNs) ? samplingPeriodNs : bestSamplingPeriodNs; bestReportDelayNs = (maxReportDelayNs < bestReportDelayNs) ? maxReportDelayNs : bestReportDelayNs; HiLog::Debug(LABEL, "%{public}s bestSamplingPeriodNs : %{public}d", __func__, int32_t { bestSamplingPeriodNs }); - auto ret = sensorServiceImpl_.SetSensorConfig(sensorId, bestSamplingPeriodNs, bestReportDelayNs); + auto ret = sensorHdiConnection_.SetBatch(sensorId, bestSamplingPeriodNs, bestReportDelayNs); if (ret != ERR_OK) { - HiLog::Error(LABEL, "%{public}s SetSensorConfig failed", __func__); + HiLog::Error(LABEL, "%{public}s SetBatch failed", __func__); return false; } HiLog::Debug(LABEL, "%{public}s end", __func__); @@ -86,10 +86,10 @@ bool SensorManager::ResetBestSensorParams(uint32_t sensorId) return false; } SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId); - auto ret = sensorServiceImpl_.SetSensorConfig(sensorId, sensorInfo.GetSamplingPeriodNs(), + auto ret = sensorHdiConnection_.SetBatch(sensorId, sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs()); if (ret != ERR_OK) { - HiLog::Error(LABEL, "%{public}s SetSensorConfig failed", __func__); + HiLog::Error(LABEL, "%{public}s SetBatch failed", __func__); return false; } HiLog::Debug(LABEL, "%{public}s end", __func__); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp old mode 100755 new mode 100644 index f33cb447..598060ee --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -99,9 +99,9 @@ void SensorService::OnStart() bool SensorService::InitInterface() { - auto ret = sensorServiceImpl_.InitSensorServiceImpl(); + auto ret = sensorHdiConnection_.ConnectHdi(); if (ret != ERR_OK) { - HiLog::Error(LABEL, "%{public}s InitSensorServiceImpl failed", __func__); + HiLog::Error(LABEL, "%{public}s connect hdi failed", __func__); return false; } return true; @@ -115,7 +115,7 @@ bool SensorService::InitDataCallback() return false; } ZReportDataCb cb = &ReportDataCallback::ZReportDataCallback; - auto ret = sensorServiceImpl_.RegisteDataReport(cb, reportDataCallback_); + auto ret = sensorHdiConnection_.RegisteDataReport(cb, reportDataCallback_); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s RegisterDataReport failed", __func__); return false; @@ -126,7 +126,12 @@ bool SensorService::InitDataCallback() bool SensorService::InitSensorList() { std::lock_guard sensorLock(sensorsMutex_); - sensors_ = sensorServiceImpl_.GetSensorList(); + int32_t ret = sensorHdiConnection_.GetSensorList(sensors_); + HiLog::Info(LABEL, "%{public}s HYH_TEST list size: %{public}d", __func__, sensors_.size()); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s GetSensorList failed", __func__); + return false; + } { std::lock_guard sensorMapLock(sensorMapMutex_); for (const auto &it : sensors_) { @@ -262,7 +267,7 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, return ret; } - ret = sensorServiceImpl_.EnableSensor(sensorId); + ret = sensorHdiConnection_.EnableSensor(sensorId); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s EnableSensor failed", __func__); clientInfo_.RemoveSubscriber(sensorId, this->GetCallingPid()); @@ -294,7 +299,7 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) HiLog::Warn(LABEL, "%{public}s other client is using this sensor now, cannot disable", __func__); return ERR_OK; } - if (sensorServiceImpl_.DisableSensor(sensorId) != ERR_OK) { + if (sensorHdiConnection_.DisableSensor(sensorId) != ERR_OK) { HiLog::Error(LABEL, "%{public}s DisableSensor failed", __func__); return DISABLE_SENSOR_ERR; } @@ -331,7 +336,7 @@ ErrCode SensorService::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t } return retFlush; } - if (sensorServiceImpl_.RunCommand(sensorId, cmdType, params) != ERR_OK) { + if (sensorHdiConnection_.RunCommand(sensorId, cmdType, params) != ERR_OK) { HiLog::Error(LABEL, "%{public}s RunCommand failed", __func__); return RUN_COMMAND_ERR; } @@ -343,7 +348,11 @@ ErrCode SensorService::RunCommand(uint32_t sensorId, uint32_t cmdType, uint32_t std::vector SensorService::GetSensorList() { std::lock_guard sensorLock(sensorsMutex_); - sensors_ = sensorServiceImpl_.GetSensorList(); + int32_t ret = sensorHdiConnection_.GetSensorList(sensors_); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s GetSensorList failed", __func__); + return sensors_; + } for (const auto &it : sensors_) { std::lock_guard sensorMapLock(sensorMapMutex_); sensorMap_.insert(std::make_pair(it.GetSensorId(), it)); diff --git a/utils/include/report_data_callback.h b/utils/include/report_data_callback.h old mode 100755 new mode 100644 index dc2ca69c..2ede0bac --- a/utils/include/report_data_callback.h +++ b/utils/include/report_data_callback.h @@ -24,7 +24,7 @@ namespace OHOS { namespace Sensors { constexpr int32_t CIRCULAR_BUF_LEN = 1024; -constexpr int32_t SENSOR_DATA_LENGHT = 1024; +constexpr int32_t SENSOR_DATA_LENGHT = 64; struct CircularEventBuf { struct SensorEvent *circularBuf; diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h old mode 100755 new mode 100644 index 114e1ec6..38a2d16b --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -76,6 +76,9 @@ enum { REGIST_PERMISSION_CHANGED_ERR = COPY_ERR + 1, DUMP_PARAM_ERR = REGIST_PERMISSION_CHANGED_ERR + 1, WRITE_MSG_ERR = DUMP_PARAM_ERR + 1, + SET_SENSOR_MODE_ERR = WRITE_MSG_ERR + 1, + SET_SENSOR_OPTION_ERR = SET_SENSOR_MODE_ERR + 1, + REGIST_CALLBACK_ERR = SET_SENSOR_OPTION_ERR + 1, }; // Error code for Sensor uitls diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp old mode 100755 new mode 100644 index c409d352..513de8e9 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -57,27 +57,13 @@ int32_t ReportDataCallback::ZReportDataCallback(const struct SensorEvent* event, HiLog::Error(LABEL, "%{public}s callback or circularBuf or event cannot be null", __func__); return ERROR; } - struct SensorEvent eventCopy = { - .sensorTypeId = event->sensorTypeId, - .version = event->version, - .timestamp = event->timestamp, - .option = event->option, - .mode = event->mode, - .dataLen = event->dataLen - }; - eventCopy.data = new uint8_t[SENSOR_DATA_LENGHT]; - HiLog::Info(LABEL, "%{public}s dataLength: %{public}d", __func__, event->dataLen); - if (memcpy_s(eventCopy.data, event->dataLen, event->data, event->dataLen) != EOK) { - HiLog::Error(LABEL, "%{public}s copy data failed", __func__); - return COPY_ERR; - } int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition; if (toEndLen == 0) { - cb->eventsBuf_.circularBuf[0] = eventCopy; + cb->eventsBuf_.circularBuf[0] = *event; cb->eventsBuf_.writePosition = 1 - toEndLen; } else { - cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = eventCopy; + cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *event; cb->eventsBuf_.writePosition += 1; } if (leftSize < 1) { -- Gitee From 230a0322e464c14be2e561c0d93338cae2b875c7 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 6 Jan 2022 18:43:48 +0800 Subject: [PATCH 2/6] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- services/sensor/BUILD.gn | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 43251565..0da6a980 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -16,12 +16,12 @@ import("//build/ohos.gni") SUBSYSTEM_DIR = "//base/sensors" ohos_shared_library("libsensor_service") { sources = [ - "hdi_connection/interface/src/sensor_hdi_connection.cpp", + "hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp", "hdi_connection/adapter/direct_connection/src/direct_connection.cpp", "hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp", "hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp", - "hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp", "hdi_connection/hardware/src/hdi_service_impl.cpp", + "hdi_connection/interface/src/sensor_hdi_connection.cpp", "src/client_info.cpp", "src/fifo_cache_data.cpp", "src/flush_info_record.cpp", @@ -46,14 +46,14 @@ ohos_shared_library("libsensor_service") { "hdi_connection/adapter/direct_connection/include", "hdi_connection/adapter/v1_0_connection/include", "hdi_connection/hardware/include", - "hdi_connection/adapter/compatible_connection/include" + "hdi_connection/adapter/compatible_connection/include", ] deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", "//drivers/peripheral/sensor/hal:hdi_sensor", - "//utils/native/base:utils", "//drivers/peripheral/sensor/interfaces/hdi/sensor/v1_0:libsensor_proxy", + "//utils/native/base:utils", ] external_deps = [ -- Gitee From 89be8b0f31bd3522afae828208fed9841c2fc751 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 6 Jan 2022 21:52:02 +0800 Subject: [PATCH 3/6] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- .../src/compatible_connection.cpp | 19 +++++++++++++++++-- .../hardware/src/hdi_service_impl.cpp | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp index b390bb52..4e5ef4df 100644 --- a/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp @@ -15,6 +15,7 @@ #include "compatible_connection.h" #include +#include "securec.h" #include "sensors_errors.h" #include "sensors_log_domain.h" @@ -128,8 +129,22 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); return ERR_NO_INIT; } - (void)(reportDataCallback_->*reportDataCb_)(reinterpret_cast(event), - reportDataCallback_); + + struct SensorEvent sensorEvent = { + .sensorTypeId = event->sensorId, + .version = event->version, + .timestamp = event->timestamp, + .option = event->option, + .mode = event->mode, + .dataLen = event->dataLen + }; + sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; + if (memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen) != EOK) { + HiLog::Error(LABEL, "%{public}s copy data failed", __func__); + return COPY_ERR; + } + + (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } 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 2fa03849..1932ff6a 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -29,10 +29,10 @@ std::vector g_sensorInfos = { {"sensor_test", "default", "1.0.0", "1.0.0", 0, 0, 9999.0, 0.000001, 23.0}, }; std::vector supportSensors = {0}; -uint8_t testData[] = {(uint8_t)9806.649414}; -constexpr struct SensorEvents testEvent = { +float testData[] = {9.8}; +struct SensorEvents testEvent = { .sensorId = 0, - .data = testData, + .data = (uint8_t *)testData, .dataLen = 4 }; } -- Gitee From 11b244dffe44eff52b8959b0477a1bcd1a2c2c2f Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Thu, 6 Jan 2022 23:19:57 +0800 Subject: [PATCH 4/6] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- services/sensor/BUILD.gn | 2 +- .../src/compatible_connection.cpp | 8 +--- .../src/direct_connection.cpp | 3 -- ...hdi_connection_v1_0.h => hdi_connection.h} | 14 +++---- ...connection_v1_0.cpp => hdi_connection.cpp} | 38 +++++++++---------- .../src/sensor_event_callback.cpp | 14 +++---- .../hardware/include/hdi_service_impl.h | 4 +- .../hardware/src/hdi_service_impl.cpp | 7 ++-- .../include/i_sensor_hdi_connection.h | 4 +- .../interface/src/sensor_hdi_connection.cpp | 4 +- services/sensor/src/sensor_manager.cpp | 4 +- 11 files changed, 45 insertions(+), 57 deletions(-) rename services/sensor/hdi_connection/adapter/v1_0_connection/include/{hdi_connection_v1_0.h => hdi_connection.h} (83%) rename services/sensor/hdi_connection/adapter/v1_0_connection/src/{hdi_connection_v1_0.cpp => hdi_connection.cpp} (71%) diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 0da6a980..e18affb2 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -18,7 +18,7 @@ ohos_shared_library("libsensor_service") { sources = [ "hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp", "hdi_connection/adapter/direct_connection/src/direct_connection.cpp", - "hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp", + "hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp", "hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp", "hdi_connection/hardware/src/hdi_service_impl.cpp", "hdi_connection/interface/src/sensor_hdi_connection.cpp", diff --git a/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp index 4e5ef4df..dc09e145 100644 --- a/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp @@ -43,15 +43,11 @@ int32_t CompatibleConnection::GetSensorList(std::vector& sensorList) HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); return ret; } - HiLog::Info(LABEL, "%{public}s sensor size: %{public}d", __func__, sensorInfos.size()); - for (int32_t i = 0; i < sensorInfos.size(); i++) { + for (int32_t i = 0; i < static_cast(sensorInfos.size()); i++) { const std::string sensorName(sensorInfos[i].sensorName); const std::string vendorName(sensorInfos[i].vendorName); const int32_t sensorId = sensorInfos[i].sensorId; - const float power = sensorInfos[i].power; const float maxRange = sensorInfos[i].maxRange; - HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", - __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); Sensor sensor; sensor.SetSensorId(sensorId); sensor.SetMaxRange(maxRange); @@ -122,7 +118,7 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even HiLog::Debug(LABEL, "%{public}s begin", __func__); if ((event == nullptr) || (event->dataLen == 0)) { HiLog::Error(LABEL, "%{public}s event is NULL", __func__); - return ERR_INVALID_VALUE; + return ERR_INVALID_VALUE; } if (reportDataCb_ == nullptr) { diff --git a/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp b/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp index 5f5aa2aa..1e9b9e67 100644 --- a/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp +++ b/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp @@ -55,10 +55,7 @@ int32_t DirectConnection::GetSensorList(std::vector& sensorList) const std::string sensorName(sensorInfo->sensorName); const std::string vendorName(sensorInfo->vendorName); const int32_t sensorId = sensorInfo->sensorId; - const float power = sensorInfo->power; const float maxRange = sensorInfo->maxRange; - HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", - __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); Sensor sensor; sensor.SetSensorId(sensorId); sensor.SetMaxRange(maxRange); diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection.h similarity index 83% rename from services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h rename to services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection.h index 3b249490..8444072c 100644 --- a/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection_v1_0.h +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection.h @@ -13,18 +13,18 @@ * limitations under the License. */ -#ifndef HDI_CONNECTION_V1_0_H -#define HDI_CONNECTION_V1_0_H +#ifndef HDI_CONNECTION_H +#define HDI_CONNECTION_H #include "i_sensor_hdi_connection.h" namespace OHOS { namespace Sensors { -class HdiConnectionV1_0 : public ISensorHdiConnection { +class HdiConnection : public ISensorHdiConnection { public: - HdiConnectionV1_0() = default; + HdiConnection() = default; - virtual ~HdiConnectionV1_0() {} + virtual ~HdiConnection() {} int32_t ConnectHdi() override; @@ -51,10 +51,10 @@ public: sptr getReportDataCallback(); private: - DISALLOW_COPY_AND_MOVE(HdiConnectionV1_0); + DISALLOW_COPY_AND_MOVE(HdiConnection); static ZReportDataCb reportDataCb_; static sptr reportDataCallback_; }; } // namespace Sensors } // namespace OHOS -#endif // HDI_CONNECTION_V1_0_H \ No newline at end of file +#endif // HDI_CONNECTION_H \ No newline at end of file diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp similarity index 71% rename from services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp rename to services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp index ce11e12c..50b2f6b0 100644 --- a/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection_v1_0.cpp +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "hdi_connection_v1_0.h" +#include "hdi_connection.h" #include "sensor_interface_proxy.h" #include "sensor_event_callback.h" @@ -26,15 +26,15 @@ using hdi::sensor::v1_0::ISensorInterface; using hdi::sensor::v1_0::ISensorCallback; using hdi::sensor::v1_0::HdfSensorInformation; namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnectionV1_0" }; +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnection" }; sptr sensorInterface_ = nullptr; sptr eventCallback_ = new SensorEventCallback(); } -ZReportDataCb HdiConnectionV1_0::reportDataCb_ = nullptr; -sptr HdiConnectionV1_0::reportDataCallback_ = nullptr; +ZReportDataCb HdiConnection::reportDataCb_ = nullptr; +sptr HdiConnection::reportDataCallback_ = nullptr; -int32_t HdiConnectionV1_0::ConnectHdi() +int32_t HdiConnection::ConnectHdi() { sensorInterface_ = ISensorInterface::Get(); if (sensorInterface_ == nullptr) { @@ -44,7 +44,7 @@ int32_t HdiConnectionV1_0::ConnectHdi() return ERR_OK; } -int32_t HdiConnectionV1_0::GetSensorList(std::vector& sensorList) +int32_t HdiConnection::GetSensorList(std::vector& sensorList) { HiLog::Info(LABEL, "%{public}s in", __func__); std::vector sensorInfos; @@ -53,15 +53,11 @@ int32_t HdiConnectionV1_0::GetSensorList(std::vector& sensorList) HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__); return ret; } - HiLog::Info(LABEL, "%{public}s sensor size: %{public}d", __func__, sensorInfos.size()); - for (int32_t i = 0; i < sensorInfos.size(); i++) { + for (int32_t i = 0; i < static_cast(sensorInfos.size()); i++) { const std::string sensorName(sensorInfos[i].sensorName); const std::string vendorName(sensorInfos[i].vendorName); const int32_t sensorId = sensorInfos[i].sensorId; - const float power = sensorInfos[i].power; const float maxRange = sensorInfos[i].maxRange; - HiLog::Info(LABEL, "%{public}s i: %{public}d sensorid: %{public}d sensorName: %{public}s, vendorName: %{public}s, power: %{public}f, maxRange: %{public}f", - __func__, i, sensorId, sensorName.c_str(), vendorName.c_str(), power, maxRange); Sensor sensor; sensor.SetSensorId(sensorId); sensor.SetMaxRange(maxRange); @@ -72,7 +68,7 @@ int32_t HdiConnectionV1_0::GetSensorList(std::vector& sensorList) return ERR_OK; } -int32_t HdiConnectionV1_0::EnableSensor(uint32_t sensorId) +int32_t HdiConnection::EnableSensor(uint32_t sensorId) { int32_t ret = sensorInterface_->Enable(sensorId); if (ret < 0) { @@ -82,7 +78,7 @@ int32_t HdiConnectionV1_0::EnableSensor(uint32_t sensorId) return ERR_OK; } -int32_t HdiConnectionV1_0::DisableSensor(uint32_t sensorId) +int32_t HdiConnection::DisableSensor(uint32_t sensorId) { int32_t ret = sensorInterface_->Disable(sensorId); if (ret < 0) { @@ -92,7 +88,7 @@ int32_t HdiConnectionV1_0::DisableSensor(uint32_t sensorId) return ERR_OK; } -int32_t HdiConnectionV1_0::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) +int32_t HdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) { int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); if (ret < 0) { @@ -102,7 +98,7 @@ int32_t HdiConnectionV1_0::SetBatch(int32_t sensorId, int64_t samplingInterval, return ERR_OK; } -int32_t HdiConnectionV1_0::SetMode(int32_t sensorId, int32_t mode) +int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) { int32_t ret = sensorInterface_->SetMode(sensorId, mode); if (ret < 0) { @@ -112,7 +108,7 @@ int32_t HdiConnectionV1_0::SetMode(int32_t sensorId, int32_t mode) return ERR_OK; } -int32_t HdiConnectionV1_0::SetOption(int32_t sensorId, uint32_t option) +int32_t HdiConnection::SetOption(int32_t sensorId, uint32_t option) { int32_t ret = sensorInterface_->SetOption(sensorId, option); if (ret < 0) { @@ -122,7 +118,7 @@ int32_t HdiConnectionV1_0::SetOption(int32_t sensorId, uint32_t option) return ERR_OK; } -int32_t HdiConnectionV1_0::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) +int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) { if (reportDataCallback == nullptr) { HiLog::Error(LABEL, "%{public}s failed, reportDataCallback cannot be null", __func__); @@ -138,7 +134,7 @@ int32_t HdiConnectionV1_0::RegisteDataReport(ZReportDataCb cb, sptrUnregister(0); if (ret < 0) { @@ -148,12 +144,12 @@ int32_t HdiConnectionV1_0::DestroyHdiConnection() return ERR_OK; } -int32_t HdiConnectionV1_0::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +int32_t HdiConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) { return 0; } -ZReportDataCb HdiConnectionV1_0::getReportDataCb() +ZReportDataCb HdiConnection::getReportDataCb() { if (reportDataCb_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); @@ -161,7 +157,7 @@ ZReportDataCb HdiConnectionV1_0::getReportDataCb() return reportDataCb_; } -sptr HdiConnectionV1_0::getReportDataCallback() +sptr HdiConnection::getReportDataCallback() { if (reportDataCallback_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCallback_ cannot be null", __func__); diff --git a/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp index 03536e40..fd705680 100644 --- a/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp @@ -14,18 +14,16 @@ */ #include "sensor_event_callback.h" - -#include "hdi_connection_v1_0.h" +#include "hdi_connection.h" #include "sensors_errors.h" #include "sensors_log_domain.h" namespace OHOS { namespace Sensors { - using namespace OHOS::HiviewDFX; namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnectionV1_0" }; -std::unique_ptr hdiConnectionV1_0_ = std::make_unique(); +constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiConnection" }; +std::unique_ptr HdiConnection_ = std::make_unique(); } int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) @@ -40,11 +38,11 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) .dataLen = event.dataLen }; sensorEvent.data = new uint8_t[SENSOR_DATA_LENGHT]; - for (int32_t i = 0; i < event.data.size(); i ++ ) { + for (int32_t i = 0; i < static_cast(event.data.size()); i++) { sensorEvent.data[i] = event.data[i]; } - ZReportDataCb reportDataCb_ = hdiConnectionV1_0_->getReportDataCb(); - sptr reportDataCallback_ = hdiConnectionV1_0_->getReportDataCallback(); + ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); + sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); return ERR_NO_INIT; 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 09ec5559..7a16ce7b 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -16,8 +16,8 @@ #ifndef HDI_SERVICE_IMPL_H #define HDI_SERVICE_IMPL_H -#include "sensor_if.h" #include "sensor_agent_type.h" +#include "sensor_if.h" #include "singleton.h" #include #include @@ -25,7 +25,7 @@ namespace OHOS { namespace Sensors { -class HdiServiceImpl : public Singleton{ +class HdiServiceImpl : public Singleton { public: HdiServiceImpl() = default; 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 1932ff6a..65842fb9 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -24,7 +24,8 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "HdiServiceImpl" }; - +constexpr int64_t SAMPLING_INTERVAL_NS = 200000000; +constexpr int32_t CONVERT_MULTIPLES = 1000; std::vector g_sensorInfos = { {"sensor_test", "default", "1.0.0", "1.0.0", 0, 0, 9999.0, 0.000001, 23.0}, }; @@ -52,7 +53,7 @@ void HdiServiceImpl::DataReportThread() { HiLog::Info(LABEL, "%{public}s in", __func__); while (true) { - usleep(g_samplingInterval / 1000); + usleep(g_samplingInterval / CONVERT_MULTIPLES); g_callback(&testEvent); if (g_isStop) { break; @@ -112,7 +113,7 @@ int32_t HdiServiceImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int { HiLog::Info(LABEL, "%{public}s in", __func__); if (samplingInterval < 0 || reportInterval < 0) { - samplingInterval = 200000000; + samplingInterval = SAMPLING_INTERVAL_NS; reportInterval = 0; } g_samplingInterval = samplingInterval; diff --git a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h index 66c6cc49..85ef4c1c 100644 --- a/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h +++ b/services/sensor/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -16,10 +16,10 @@ #ifndef I_SENSOR_HDI_CONNECTION_H #define I_SENSOR_HDI_CONNECTION_H +#include +#include #include "report_data_callback.h" #include "sensor.h" -#include -#include namespace OHOS { namespace Sensors { 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 a41258ee..0a22d925 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -16,7 +16,7 @@ #include "compatible_connection.h" #include "direct_connection.h" -#include "hdi_connection_v1_0.h" +#include "hdi_connection.h" #include "sensors_errors.h" #include "sensors_log_domain.h" @@ -30,7 +30,7 @@ constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "Sens int32_t SensorHdiConnection::ConnectHdi() { - iSensorHdiConnection_ = std::make_unique(); + iSensorHdiConnection_ = std::make_unique(); int32_t ret = iSensorHdiConnection_->ConnectHdi(); if (ret != 0) { HiLog::Error(LABEL, "%{public}s connect hdi v1_0 failed", __func__); diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index b4c65e9c..1476c1af 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -86,8 +86,8 @@ bool SensorManager::ResetBestSensorParams(uint32_t sensorId) return false; } SensorBasicInfo sensorInfo = clientInfo_.GetBestSensorInfo(sensorId); - auto ret = sensorHdiConnection_.SetBatch(sensorId, sensorInfo.GetSamplingPeriodNs(), - sensorInfo.GetMaxReportDelayNs()); + auto ret = sensorHdiConnection_.SetBatch(sensorId, + sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs()); if (ret != ERR_OK) { HiLog::Error(LABEL, "%{public}s SetBatch failed", __func__); return false; -- Gitee From 05e60f8d0a25cc895acd28cad58b55b538c5b679 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 7 Jan 2022 08:58:12 +0800 Subject: [PATCH 5/6] Signed-off-by:hellohyh001 Signed-off-by: hellohyh001 --- .../hdi_connection/hardware/include/hdi_service_impl.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 7a16ce7b..46399700 100644 --- a/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h +++ b/services/sensor/hdi_connection/hardware/include/hdi_service_impl.h @@ -16,12 +16,12 @@ #ifndef HDI_SERVICE_IMPL_H #define HDI_SERVICE_IMPL_H -#include "sensor_agent_type.h" -#include "sensor_if.h" -#include "singleton.h" #include #include #include +#include "sensor_agent_type.h" +#include "sensor_if.h" +#include "singleton.h" namespace OHOS { namespace Sensors { -- Gitee From 6a3a19e3077af8839e95c012c191f55c85aff46c Mon Sep 17 00:00:00 2001 From: SUE Date: Fri, 7 Jan 2022 01:29:01 +0000 Subject: [PATCH 6/6] Signed-off-by:hellohyh001 --- services/sensor/src/sensor_service.cpp | 1 - services/sensor/src/sensor_service_stub.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 598060ee..d2fffa49 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -127,7 +127,6 @@ bool SensorService::InitSensorList() { std::lock_guard sensorLock(sensorsMutex_); int32_t ret = sensorHdiConnection_.GetSensorList(sensors_); - HiLog::Info(LABEL, "%{public}s HYH_TEST list size: %{public}d", __func__, sensors_.size()); if (ret < 0) { HiLog::Error(LABEL, "%{public}s GetSensorList failed", __func__); return false; diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 31e6476d..b7dbfe73 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -155,7 +155,6 @@ ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel (void)data; std::vector sensors(GetSensorList()); int32_t sensorCount = int32_t { sensors.size() }; - HiLog::Debug(LABEL, "%{public}s sensorCount : %{public}d", __func__, sensorCount); reply.WriteInt32(sensorCount); for (int32_t i = 0; i < sensorCount; i++) { bool flag = sensors[i].Marshalling(reply); -- Gitee