diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn old mode 100755 new mode 100644 index 91a8ddf5af38dd9c1699f9086c02b586f1aa5e1f..e18affb2583c7d8b902cee71f53240577ef60773 --- 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/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.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", "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,11 +42,17 @@ 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", + "//drivers/peripheral/sensor/interfaces/hdi/sensor/v1_0:libsensor_proxy", "//utils/native/base:utils", ] @@ -49,7 +60,6 @@ ohos_shared_library("libsensor_service") { "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 0000000000000000000000000000000000000000..cf5d1900fd92450398bb76d67ab724e0f4b147eb --- /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 0000000000000000000000000000000000000000..dc09e145d02e300da158abdc448d5f3c26ff2ffc --- /dev/null +++ b/services/sensor/hdi_connection/adapter/compatible_connection/src/compatible_connection.cpp @@ -0,0 +1,174 @@ +/* + * 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 "securec.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, "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; + } + 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 maxRange = sensorInfos[i].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; + } + + 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 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 ba425f4b7d2813abea917f8eb6350f0e85ec59c8..1bfc5c86746b7637c604a391c0288946f72231bc --- 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 43% rename from services/sensor/src/sensor_service_impl.cpp rename to services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp index 7d3ba250afa58af56d97b97ef132caeca233cbfc..1e9b9e67ba2ab865ac4b8e99a3e83efb7caf7c78 --- a/services/sensor/src/sensor_service_impl.cpp +++ b/services/sensor/hdi_connection/adapter/direct_connection/src/direct_connection.cpp @@ -1,200 +1,195 @@ -/* - * 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 maxRange = sensorInfo->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.h b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection.h new file mode 100644 index 0000000000000000000000000000000000000000..8444072c0016ccd887b8903937f28eaf37620ee0 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/include/hdi_connection.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_H +#define HDI_CONNECTION_H + +#include "i_sensor_hdi_connection.h" + +namespace OHOS { +namespace Sensors { +class HdiConnection : public ISensorHdiConnection { +public: + HdiConnection() = default; + + virtual ~HdiConnection() {} + + 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(HdiConnection); + static ZReportDataCb reportDataCb_; + static sptr reportDataCallback_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // HDI_CONNECTION_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 0000000000000000000000000000000000000000..de14db781d580309f5c8521476b4e5b963f13dc0 --- /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.cpp b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50b2f6b011afd6e1b6591372ea43431f19660308 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/hdi_connection.cpp @@ -0,0 +1,168 @@ +/* + * 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.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, "HdiConnection" }; +sptr sensorInterface_ = nullptr; +sptr eventCallback_ = new SensorEventCallback(); +} + +ZReportDataCb HdiConnection::reportDataCb_ = nullptr; +sptr HdiConnection::reportDataCallback_ = nullptr; + +int32_t HdiConnection::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 HdiConnection::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; + } + 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 maxRange = sensorInfos[i].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 HdiConnection::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 HdiConnection::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 HdiConnection::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 HdiConnection::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 HdiConnection::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 HdiConnection::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 HdiConnection::DestroyHdiConnection() +{ + int32_t ret = sensorInterface_->Unregister(0); + if (ret < 0) { + HiLog::Error(LABEL, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t HdiConnection::RunCommand(uint32_t sensorId, int32_t cmd, int32_t params) +{ + return 0; +} + +ZReportDataCb HdiConnection::getReportDataCb() +{ + if (reportDataCb_ == nullptr) { + HiLog::Error(LABEL, "%{public}s reportDataCb_ cannot be null", __func__); + } + return reportDataCb_; +} + +sptr HdiConnection::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 0000000000000000000000000000000000000000..fd705680ec15c0efe6964f60b27321ec74db2c45 --- /dev/null +++ b/services/sensor/hdi_connection/adapter/v1_0_connection/src/sensor_event_callback.cpp @@ -0,0 +1,55 @@ +/* + * 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.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, "HdiConnection" }; +std::unique_ptr HdiConnection_ = 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 < static_cast(event.data.size()); i++) { + sensorEvent.data[i] = event.data[i]; + } + 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; + } + (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 0000000000000000000000000000000000000000..46399700411f1f276d7ddc762b98893b2a735b45 --- /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 +#include +#include +#include "sensor_agent_type.h" +#include "sensor_if.h" +#include "singleton.h" + +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 0000000000000000000000000000000000000000..65842fb96b0a8bf70696d508121559c2ab1bac1d --- /dev/null +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -0,0 +1,155 @@ +/* + * 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" }; +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}, +}; +std::vector supportSensors = {0}; +float testData[] = {9.8}; +struct SensorEvents testEvent = { + .sensorId = 0, + .data = (uint8_t *)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 / CONVERT_MULTIPLES); + 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 = SAMPLING_INTERVAL_NS; + 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 0000000000000000000000000000000000000000..85ef4c1c7914e4f9cb99757d29e07b356e07bf7b --- /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 +#include +#include "report_data_callback.h" +#include "sensor.h" + +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 0000000000000000000000000000000000000000..787d250a271520e0c26d3ef69a1f52a7ecfcc33d --- /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 0000000000000000000000000000000000000000..0a22d925a3836fd6b5f4f4a2d77641b17bb9e79c --- /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.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 c908430bf3bceaa90284214de78a0e10f7b1694c..2b074c00cd9d269c8098999b7ae97e6e6b7ef44b --- 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 ecee8b14f85013380217cb40329877d12a38cfea..a4daddff5f25e5f3c5bc3157a34c69dc4385f558 --- 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 cd17052b3f8f75cba8f2b6f1a582d0fac8cdcc13..dac1ec3909ae554f29062cc332b0e9927d91805b --- 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 141a385b9ca1f71966a3bd6f4183cee43c9f2272..a6be0bd2bc0e2cca033374cda4095a434612260c --- 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 9ebb3c0356cf785f7e7fe37330b07327e04ecbc3..bd19f1970ca464a3f087e42b9899dbf59212ff9b --- 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 80a1ba5c120a6add58309efd335a40c5c1fd0ffa..9f3499189d26e655d4cc37ccd9fe291f0162c840 --- 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 2413a6b92794ba7e4d151185ba5ba2de477488b2..1476c1aff8dbdb792b2b8d0c7014b25592a9df29 --- 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(), - sensorInfo.GetMaxReportDelayNs()); + 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 f33cb447c5f1333b71e28e24904a7ddd40c57ec9..d2fffa4998d7b9c6766cf93f9240bf7b307593c7 --- 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,11 @@ bool SensorService::InitDataCallback() bool SensorService::InitSensorList() { 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 false; + } { std::lock_guard sensorMapLock(sensorMapMutex_); for (const auto &it : sensors_) { @@ -262,7 +266,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 +298,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 +335,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 +347,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/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 31e6476df8c06dba66e3256e98f0593e8c95d5d9..b7dbfe73dc3ab0fe514a656c9a52fbb04c345568 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); diff --git a/utils/include/report_data_callback.h b/utils/include/report_data_callback.h old mode 100755 new mode 100644 index dc2ca69ce759694a945cd89de7bb9ad7fdfa3839..2ede0bac6f7ed1e64b353f7b4c68d5e56a877844 --- 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 114e1ec63154bffd7ee5eca56b412d28e8778660..38a2d16b83317df5138c3ecb6e4b5c337724ae8a --- 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 c409d3527f037be4965a9b5637b97a03f75b5d57..513de8e9a2357b011164ca469d4cc99479c106bd --- 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) {