From e4094e12ad49e5c6db52bc53af9ee561b347cc7a Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Fri, 18 Nov 2022 16:20:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AFSensorEvent.data?= =?UTF-8?q?=E4=B8=BA=E5=8A=A8=E6=80=81=E5=86=85=E5=AD=98=EF=BC=8C=E5=AE=B9?= =?UTF-8?q?=E6=98=93=E9=80=A0=E6=88=90=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E9=9D=99=E6=80=81=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixiangpeng5 Change-Id: I6710245c440f4ff56ba73c9cb99b949d68e4234b --- .../sensor/include/sensor_data_channel.h | 1 + .../include/sensor_file_descriptor_listener.h | 2 +- .../src/sensor_file_descriptor_listener.cpp | 8 +- .../adapter/src/compatible_connection.cpp | 10 +- .../adapter/src/sensor_event_callback.cpp | 12 +- services/sensor/include/client_info.h | 14 +-- services/sensor/include/fifo_cache_data.h | 8 +- .../sensor/include/sensor_data_processer.h | 24 ++-- services/sensor/include/sensor_dump.h | 2 +- services/sensor/include/sensor_manager.h | 2 +- services/sensor/include/sensor_service.h | 2 +- services/sensor/src/client_info.cpp | 32 ++--- services/sensor/src/fifo_cache_data.cpp | 4 +- services/sensor/src/sensor_data_processer.cpp | 118 ++++++++---------- services/sensor/src/sensor_dump.cpp | 2 +- services/sensor/src/sensor_manager.cpp | 9 +- services/sensor/src/sensor_service.cpp | 6 +- utils/include/report_data_callback.h | 8 +- utils/include/sensor_basic_data_channel.h | 16 +-- utils/include/sensor_data_event.h | 10 +- utils/src/report_data_callback.cpp | 20 +-- utils/src/sensor_basic_data_channel.cpp | 4 +- 22 files changed, 134 insertions(+), 180 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_data_channel.h b/frameworks/native/sensor/include/sensor_data_channel.h index 8a9bb96d..ecf87bac 100644 --- a/frameworks/native/sensor/include/sensor_data_channel.h +++ b/frameworks/native/sensor/include/sensor_data_channel.h @@ -19,6 +19,7 @@ #include #include +#include "sensor_agent_type.h" #include "sensor_event_handler.h" #include "sensor_basic_data_channel.h" diff --git a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h index 165ba763..770c1dae 100644 --- a/frameworks/native/sensor/include/sensor_file_descriptor_listener.h +++ b/frameworks/native/sensor/include/sensor_file_descriptor_listener.h @@ -35,7 +35,7 @@ public: private: SensorDataChannel* channel_; - TransferSensorEvents *receiveDataBuff_ = nullptr; + SensorData *receiveDataBuff_ = nullptr; }; } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp index 8dc13614..e272d1d7 100644 --- a/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/sensor_file_descriptor_listener.cpp @@ -33,7 +33,7 @@ SensorFileDescriptorListener::SensorFileDescriptorListener() { channel_ = nullptr; receiveDataBuff_ = - new (std::nothrow) TransferSensorEvents[sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE]; + new (std::nothrow) SensorData[sizeof(SensorData) * RECEIVE_DATA_SIZE]; CHKPL(receiveDataBuff_); } @@ -57,8 +57,8 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) return; } int32_t len = - recv(fileDescriptor, receiveDataBuff_, sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE, 0); - int32_t eventSize = static_cast(sizeof(TransferSensorEvents)); + recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0); + int32_t eventSize = static_cast(sizeof(SensorData)); while (len > 0) { int32_t num = len / eventSize; for (int i = 0; i < num; i++) { @@ -73,7 +73,7 @@ void SensorFileDescriptorListener::OnReadable(int32_t fileDescriptor) }; channel_->dataCB_(&event, 1, channel_->privateData_); } - len = recv(fileDescriptor, receiveDataBuff_, sizeof(TransferSensorEvents) * RECEIVE_DATA_SIZE, 0); + len = recv(fileDescriptor, receiveDataBuff_, sizeof(SensorData) * RECEIVE_DATA_SIZE, 0); } } diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index 1863ffd6..f57f0b9a 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -132,7 +132,7 @@ int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) return ERR_INVALID_VALUE; } - SensorEvent sensorEvent = { + SensorData sensorData = { .sensorTypeId = event->sensorId, .version = event->version, .timestamp = event->timestamp, @@ -140,18 +140,16 @@ int32_t CompatibleConnection::SensorDataCallback(const SensorEvents *event) .mode = event->mode, .dataLen = event->dataLen }; - sensorEvent.data = new (std::nothrow) uint8_t[SENSOR_DATA_LENGHT]; - CHKPR(sensorEvent.data, ERR_INVALID_VALUE); - errno_t ret = memcpy_s(sensorEvent.data, event->dataLen, event->data, event->dataLen); + CHKPR(sensorData.data, ERR_INVALID_VALUE); + errno_t ret = memcpy_s(sensorData.data, event->dataLen, event->data, event->dataLen); if (ret != EOK) { SEN_HILOGE("copy data failed"); - delete[] sensorEvent.data; return COPY_ERR; } CHKPR(reportDataCallback_, ERR_NO_INIT); CHKPR(reportDataCb_, ERR_NO_INIT); std::unique_lock lk(ISensorHdiConnection::dataMutex_); - (void)(reportDataCallback_->*reportDataCb_)(&sensorEvent, reportDataCallback_); + (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index a5a9fbf8..75388312 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -12,11 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - #include "sensor_event_callback.h" #include "hdi_connection.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensors_errors.h" namespace OHOS { @@ -37,7 +36,7 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) SEN_HILOGI("data is empty"); return ERR_INVALID_VALUE; } - SensorEvent sensorEvent = { + SensorData sensorData = { .sensorTypeId = event.sensorId, .version = event.version, .timestamp = event.timestamp, @@ -45,13 +44,12 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) .mode = event.mode, .dataLen = event.dataLen }; - sensorEvent.data = new (std::nothrow) uint8_t[SENSOR_DATA_LENGHT]; - CHKPR(sensorEvent.data, ERR_NO_INIT); + CHKPR(sensorData.data, ERR_NO_INIT); for (int32_t i = 0; i < static_cast(dataSize); i++) { - sensorEvent.data[i] = event.data[i]; + sensorData.data[i] = event.data[i]; } std::unique_lock lk(ISensorHdiConnection::dataMutex_); - (void)(reportDataCallback_->*(reportDataCb_))(&sensorEvent, reportDataCallback_); + (void)(reportDataCallback_->*(reportDataCb_))(&sensorData, reportDataCallback_); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index f81cd4d9..e4797138 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -32,7 +32,7 @@ #include "sensor_basic_data_channel.h" #include "sensor_basic_info.h" #include "sensor_channel_info.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { @@ -58,8 +58,8 @@ public: SensorBasicInfo GetCurPidSensorInfo(uint32_t sensorId, int32_t pid); uint64_t ComputeBestPeriodCount(uint32_t sensorId, sptr &channel); uint64_t ComputeBestFifoCount(uint32_t sensorId, sptr &channel); - int32_t GetStoreEvent(int32_t sensorId, SensorEvent &event); - void StoreEvent(const SensorEvent &event); + int32_t GetStoreEvent(int32_t sensorId, SensorData &data); + void StoreEvent(const SensorData &data); void ClearEvent(); AppThreadInfo GetAppInfoByChannel(const sptr &channel); bool SaveClientPid(const sptr &sensorClient, int32_t pid); @@ -69,8 +69,8 @@ public: void GetSensorChannelInfo(std::vector &channelInfo); void UpdateCmd(uint32_t sensorId, int32_t uid, int32_t cmdType); void DestroyCmd(int32_t uid); - void UpdateDataQueue(int32_t sensorId, SensorEvent &event); - std::unordered_map> GetDumpQueue(); + void UpdateDataQueue(int32_t sensorId, SensorData &data); + std::unordered_map> GetDumpQueue(); void ClearDataQueue(int32_t sensorId); int32_t GetUidByPid(int32_t pid); AccessTokenID GetTokenIdByPid(int32_t pid); @@ -87,11 +87,11 @@ private: std::mutex dataQueueMutex_; std::unordered_map> clientMap_; std::unordered_map> channelMap_; - std::unordered_map storedEvent_; + std::unordered_map storedEvent_; std::unordered_map appThreadInfoMap_; std::map, int32_t> clientPidMap_; std::unordered_map>> cmdMap_; - std::unordered_map> dumpQueue_; + std::unordered_map> dumpQueue_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/fifo_cache_data.h b/services/sensor/include/fifo_cache_data.h index 7a1713b5..d6221735 100644 --- a/services/sensor/include/fifo_cache_data.h +++ b/services/sensor/include/fifo_cache_data.h @@ -20,7 +20,7 @@ #include "nocopyable.h" #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensor_basic_data_channel.h" namespace OHOS { @@ -31,8 +31,8 @@ public: virtual ~FifoCacheData(); void SetPeriodCount(uint64_t periodCount); uint64_t GetPeriodCount() const; - void SetFifoCacheData(const std::vector &fifoCacheData); - std::vector GetFifoCacheData() const; + void SetFifoCacheData(const std::vector &fifoCacheData); + std::vector GetFifoCacheData() const; void SetChannel(const sptr &channel); sptr GetChannel() const; void InitFifoCache(); @@ -41,7 +41,7 @@ private: DISALLOW_COPY_AND_MOVE(FifoCacheData); uint64_t periodCount_; sptr channel_; - std::vector fifoCacheData_; + std::vector fifoCacheData_; }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_data_processer.h b/services/sensor/include/sensor_data_processer.h index 8f2ff170..8a9d2125 100644 --- a/services/sensor/include/sensor_data_processer.h +++ b/services/sensor/include/sensor_data_processer.h @@ -29,7 +29,7 @@ #include "report_data_callback.h" #include "sensor.h" #include "sensor_hdi_connection.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { @@ -38,22 +38,22 @@ public: explicit SensorDataProcesser(const std::unordered_map &sensorMap); virtual ~SensorDataProcesser(); int32_t ProcessEvents(sptr dataCallback); - int32_t SendEvents(sptr &channel, SensorEvent &event); + int32_t SendEvents(sptr &channel, SensorData &data); static int DataThread(sptr dataProcesser, sptr dataCallback); - int32_t CacheSensorEvent(const SensorEvent &event, sptr &channel); + int32_t CacheSensorEvent(const SensorData &data, sptr &channel); private: DISALLOW_COPY_AND_MOVE(SensorDataProcesser); - void ReportData(sptr &channel, SensorEvent &event); - bool ReportNotContinuousData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event); - void SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, uint64_t periodCount); - void SendFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, uint64_t periodCount, + void ReportData(sptr &channel, SensorData &data); + bool ReportNotContinuousData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data); + void SendNoneFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount); + void SendFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount, uint64_t fifoCount); - void SendRawData(std::unordered_map &cacheBuf, sptr channel, - std::vector event); + void SendRawData(std::unordered_map &cacheBuf, sptr channel, + std::vector events); void EventFilter(CircularEventBuf &eventsBuf); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); diff --git a/services/sensor/include/sensor_dump.h b/services/sensor/include/sensor_dump.h index e636c0e6..8c729114 100644 --- a/services/sensor/include/sensor_dump.h +++ b/services/sensor/include/sensor_dump.h @@ -44,7 +44,7 @@ private: DISALLOW_COPY_AND_MOVE(SensorDump); void DumpCurrentTime(int32_t fd); int32_t DataSizeBySensorId(uint32_t sensorId); - std::string GetDataBySensorId(uint32_t sensorId, TransferSensorEvents &sensorData); + std::string GetDataBySensorId(uint32_t sensorId, SensorData &sensorData); static std::unordered_map sensorMap_; }; } // namespace Sensors diff --git a/services/sensor/include/sensor_manager.h b/services/sensor/include/sensor_manager.h index 2be11364..5bcb35df 100644 --- a/services/sensor/include/sensor_manager.h +++ b/services/sensor/include/sensor_manager.h @@ -24,7 +24,7 @@ #include "flush_info_record.h" #include "sensor_data_processer.h" #include "sensor_hdi_connection.h" -#include "sensor_agent_type.h" + namespace OHOS { namespace Sensors { using namespace Security::AccessToken; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index a436cf89..35fab928 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -25,7 +25,7 @@ #include "client_info.h" #include "death_recipient_template.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" #include "sensor_service_stub.h" diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 122c727f..15e68601 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -427,12 +427,12 @@ uint64_t ClientInfo::ComputeBestFifoCount(uint32_t sensorId, sptr lock(eventMutex_); auto storedEvent = storedEvent_.find(sensorId); if (storedEvent != storedEvent_.end()) { - errno_t ret = memcpy_s(&event, sizeof(SensorEvent), &storedEvent->second, sizeof(SensorEvent)); + errno_t ret = memcpy_s(&data, sizeof(SensorData), &storedEvent->second, sizeof(SensorData)); if (ret != EOK) { SEN_HILOGE("memcpy_s failed, sensorId:%{public}d", sensorId); return ret; @@ -444,10 +444,10 @@ int32_t ClientInfo::GetStoreEvent(int32_t sensorId, SensorEvent &event) return NO_STROE_EVENT; } -void ClientInfo::StoreEvent(const SensorEvent &event) +void ClientInfo::StoreEvent(const SensorData &data) { bool foundSensor = false; - SensorEvent storedEvent; + SensorData storedEvent; auto sensorHdiConnection = &SensorHdiConnection::GetInstance(); if (sensorHdiConnection == nullptr) { SEN_HILOGE("sensorHdiConnection cannot be null"); @@ -459,7 +459,7 @@ void ClientInfo::StoreEvent(const SensorEvent &event) SEN_HILOGE("GetSensorList is failed"); return; } - errno_t retVal = memcpy_s(&storedEvent, sizeof(storedEvent), &event, sizeof(event)); + errno_t retVal = memcpy_s(&storedEvent, sizeof(storedEvent), &data, sizeof(data)); if (retVal != EOK) { SEN_HILOGE("memcpy_s is failed"); return; @@ -653,38 +653,26 @@ std::vector ClientInfo::GetCmdList(uint32_t sensorId, int32_t uid) return uidIt->second; } -void ClientInfo::UpdateDataQueue(int32_t sensorId, SensorEvent &event) +void ClientInfo::UpdateDataQueue(int32_t sensorId, SensorData &data) { if (sensorId == HEART_RATE_SENSOR_ID) { return; } std::lock_guard queueLock(dataQueueMutex_); - TransferSensorEvents transferEvent = { - .sensorTypeId = event.sensorTypeId, - .version = event.version, - .timestamp = event.timestamp, - .option = event.option, - .mode = event.mode, - .dataLen = event.dataLen - }; - if (memcpy_s(transferEvent.data, event.dataLen, event.data, event.dataLen) != EOK) { - SEN_HILOGE("Copy data failed"); - return; - } auto it = dumpQueue_.find(sensorId); if (it == dumpQueue_.end()) { - std::queue q; - q.push(transferEvent); + std::queue q; + q.push(data); dumpQueue_.insert(std::make_pair(sensorId, q)); return; } - it->second.push(transferEvent); + it->second.push(data); if (it->second.size() > MAX_DUMP_DATA_SIZE) { it->second.pop(); } } -std::unordered_map> ClientInfo::GetDumpQueue() +std::unordered_map> ClientInfo::GetDumpQueue() { return dumpQueue_; } diff --git a/services/sensor/src/fifo_cache_data.cpp b/services/sensor/src/fifo_cache_data.cpp index b3aa44c8..db0174f6 100644 --- a/services/sensor/src/fifo_cache_data.cpp +++ b/services/sensor/src/fifo_cache_data.cpp @@ -41,12 +41,12 @@ uint64_t FifoCacheData::GetPeriodCount() const return periodCount_; } -void FifoCacheData::SetFifoCacheData(const std::vector &fifoCacheData) +void FifoCacheData::SetFifoCacheData(const std::vector &fifoCacheData) { fifoCacheData_ = fifoCacheData; } -std::vector FifoCacheData::GetFifoCacheData() const +std::vector FifoCacheData::GetFifoCacheData() const { return fifoCacheData_; } diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 5dc6a707..f2c1daaa 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -60,16 +60,16 @@ SensorDataProcesser::~SensorDataProcesser() sensorMap_.clear(); } -void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, +void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount) { - std::vector sendEvents; + std::vector sendEvents; std::lock_guard dataCountLock(dataCountMutex_); - sendEvents.push_back(event); - uint32_t sensorId = static_cast(event.sensorTypeId); + sendEvents.push_back(data); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } auto dataCountIt = dataCountMap_.find(sensorId); if (dataCountIt == dataCountMap_.end()) { @@ -107,13 +107,13 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event, +void SensorDataProcesser::SendFifoCacheData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data, uint64_t periodCount, uint64_t fifoCount) { - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } std::lock_guard dataCountLock(dataCountMutex_); auto dataCountIt = dataCountMap_.find(sensorId); @@ -141,8 +141,8 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_mapSetPeriodCount(0); - std::vector fifoDataList = fifoData->GetFifoCacheData(); - fifoDataList.push_back(event); + std::vector fifoDataList = fifoData->GetFifoCacheData(); + fifoDataList.push_back(data); fifoData->SetFifoCacheData(fifoDataList); if ((fifoData->GetFifoCacheData()).size() != fifoCount) { continue; @@ -160,15 +160,15 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map &channel, SensorEvent &event) +void SensorDataProcesser::ReportData(sptr &channel, SensorData &data) { CHKPV(channel); - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } - auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); - if (ReportNotContinuousData(cacheBuf, channel, event)) { + auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); + if (ReportNotContinuousData(cacheBuf, channel, data)) { return; } uint64_t periodCount = clientInfo_.ComputeBestPeriodCount(sensorId, channel); @@ -177,18 +177,18 @@ void SensorDataProcesser::ReportData(sptr &channel, Sens } auto fifoCount = clientInfo_.ComputeBestFifoCount(sensorId, channel); if (fifoCount <= 0) { - SendNoneFifoCacheData(cacheBuf, channel, event, periodCount); + SendNoneFifoCacheData(cacheBuf, channel, data, periodCount); return; } - SendFifoCacheData(cacheBuf, channel, event, periodCount, fifoCount); + SendFifoCacheData(cacheBuf, channel, data, periodCount, fifoCount); } -bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &cacheBuf, - sptr &channel, SensorEvent &event) +bool SensorDataProcesser::ReportNotContinuousData(std::unordered_map &cacheBuf, + sptr &channel, SensorData &data) { - uint32_t sensorId = static_cast(event.sensorTypeId); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } std::lock_guard sensorLock(sensorMutex_); auto sensor = sensorMap_.find(sensorId); @@ -196,81 +196,64 @@ bool SensorDataProcesser::ReportNotContinuousData(std::unordered_mapsecond.SetFlags(event.mode); + sensor->second.SetFlags(data.mode); if (((SENSOR_ON_CHANGE & sensor->second.GetFlags()) == SENSOR_ON_CHANGE) || ((SENSOR_ONE_SHOT & sensor->second.GetFlags()) == SENSOR_ONE_SHOT)) { - std::vector sendEvents; - sendEvents.push_back(event); + std::vector sendEvents; + sendEvents.push_back(data); SendRawData(cacheBuf, channel, sendEvents); return true; } return false; } -void SensorDataProcesser::SendRawData(std::unordered_map &cacheBuf, - sptr channel, std::vector event) +void SensorDataProcesser::SendRawData(std::unordered_map &cacheBuf, + sptr channel, std::vector events) { CHKPV(channel); - if (event.empty()) { + if (events.empty()) { return; } - size_t eventSize = event.size(); - std::vector transferEvents; - for (size_t i = 0; i < eventSize; i++) { - TransferSensorEvents transferEvent = { - .sensorTypeId = event[i].sensorTypeId, - .version = event[i].version, - .timestamp = event[i].timestamp, - .option = event[i].option, - .mode = event[i].mode, - .dataLen = event[i].dataLen - }; - errno_t ret = memcpy_s(transferEvent.data, SENSOR_MAX_LENGTH, event[i].data, event[i].dataLen); - if (ret != EOK) { - SEN_HILOGE("copy data failed"); - return; - } - transferEvents.push_back(transferEvent); - } - auto ret = channel->SendData(transferEvents.data(), eventSize * sizeof(TransferSensorEvents)); + size_t eventSize = events.size(); + auto ret = channel->SendData(events.data(), eventSize * sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("send data failed, ret:%{public}d", ret); - uint32_t sensorId = static_cast(event[eventSize - 1].sensorTypeId); + uint32_t sensorId = static_cast(events[eventSize - 1].sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event[eventSize - 1].sensorTypeId); + sensorId = static_cast(events[eventSize - 1].sensorTypeId); } - cacheBuf[sensorId] = event[eventSize - 1]; + cacheBuf[sensorId] = events[eventSize - 1]; } } -int32_t SensorDataProcesser::CacheSensorEvent(const SensorEvent &event, sptr &channel) +int32_t SensorDataProcesser::CacheSensorEvent(const SensorData &data, sptr &channel) { CHKPR(channel, INVALID_POINTER); int32_t ret = ERR_OK; - auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); - uint32_t sensorId = static_cast(event.sensorTypeId); + auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); + uint32_t sensorId = static_cast(data.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { - sensorId = static_cast(event.sensorTypeId); + sensorId = static_cast(data.sensorTypeId); } auto cacheEvent = cacheBuf.find(sensorId); if (cacheEvent != cacheBuf.end()) { // Try to send the last failed value, if it still fails, replace the previous cache directly - ret = channel->SendData(&cacheEvent->second, sizeof(SensorEvent)); + ret = channel->SendData(&cacheEvent->second, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); } - ret = channel->SendData(&event, sizeof(SensorEvent)); + ret = channel->SendData(&data, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); - cacheBuf[sensorId] = event; + cacheBuf[sensorId] = data; } else { cacheBuf.erase(cacheEvent); } } else { - ret = channel->SendData(&event, sizeof(SensorEvent)); + ret = channel->SendData(&data, sizeof(SensorData)); if (ret != ERR_OK) { SEN_HILOGE("ret:%{public}d", ret); - cacheBuf[sensorId] = event; + cacheBuf[sensorId] = data; } } return ret; @@ -337,10 +320,7 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t eventNum = eventsBuf.eventNum; for (int32_t i = 0; i < eventNum; i++) { EventFilter(eventsBuf); - if (eventsBuf.circularBuf[eventsBuf.readPos].data != nullptr) { - delete[] eventsBuf.circularBuf[eventsBuf.readPos].data; - eventsBuf.circularBuf[eventsBuf.readPos].data = nullptr; - } + eventsBuf.readPos++; if (eventsBuf.readPos == CIRCULAR_BUF_LEN) { eventsBuf.readPos = 0; @@ -350,17 +330,17 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback return SUCCESS; } -int32_t SensorDataProcesser::SendEvents(sptr &channel, SensorEvent &event) +int32_t SensorDataProcesser::SendEvents(sptr &channel, SensorData &data) { CHKPR(channel, INVALID_POINTER); - clientInfo_.UpdateDataQueue(event.sensorTypeId, event); + clientInfo_.UpdateDataQueue(data.sensorTypeId, data); auto &cacheBuf = channel->GetDataCacheBuf(); if (cacheBuf.empty()) { - ReportData(channel, event); + ReportData(channel, data); } else { - CacheSensorEvent(event, channel); + CacheSensorEvent(data, channel); } - clientInfo_.StoreEvent(event); + clientInfo_.StoreEvent(data); return SUCCESS; } diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index d1d298b9..c5c3ed26 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -295,7 +295,7 @@ int32_t SensorDump::DataSizeBySensorId(uint32_t sensorId) } } -std::string SensorDump::GetDataBySensorId(uint32_t sensorId, TransferSensorEvents &sensorData) +std::string SensorDump::GetDataBySensorId(uint32_t sensorId, SensorData &sensorData) { SEN_HILOGD("sensorId:%{public}u", sensorId); std::string str; diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 5b465e0b..3a8255c8 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -19,6 +19,7 @@ #include "iservice_registry.h" #include "sensor.h" +#include "sensor_data_event.h" #include "sensors_errors.h" namespace OHOS { @@ -168,12 +169,12 @@ ErrCode SensorManager::AfterDisableSensor(uint32_t sensorId) CALL_LOG_ENTER; clientInfo_.ClearSensorInfo(sensorId); if (sensorId == PROXIMITY_SENSOR_ID) { - SensorEvent event; - auto ret = clientInfo_.GetStoreEvent(sensorId, event); + SensorData sensorData; + auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData); if (ret == ERR_OK) { SEN_HILOGD("change the default state is far"); - event.data[0] = PROXIMITY_FAR; - clientInfo_.StoreEvent(event); + sensorData.data[0] = PROXIMITY_FAR; + clientInfo_.StoreEvent(sensorData); } } return ERR_OK; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 563427c5..c25ac318 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -178,15 +178,15 @@ void SensorService::ReportOnChangeData(uint32_t sensorId) SEN_HILOGW("it is not onchange data, no need to report"); return; } - SensorEvent event; - auto ret = clientInfo_.GetStoreEvent(sensorId, event); + SensorData sensorData; + auto ret = clientInfo_.GetStoreEvent(sensorId, sensorData); if (ret != ERR_OK) { SEN_HILOGE("there is no data to be reported"); return; } sptr channel = clientInfo_.GetSensorChannelByPid(GetCallingPid()); CHKPV(channel); - auto sendRet = channel->SendData(&event, sizeof(event)); + auto sendRet = channel->SendData(&sensorData, sizeof(sensorData)); if (sendRet != ERR_OK) { SEN_HILOGE("send data failed"); return; diff --git a/utils/include/report_data_callback.h b/utils/include/report_data_callback.h index 4c6acd5d..608e9a7b 100644 --- a/utils/include/report_data_callback.h +++ b/utils/include/report_data_callback.h @@ -17,7 +17,7 @@ #define REPORT_DATA_CALLBACK_H #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { @@ -25,7 +25,7 @@ constexpr int32_t CIRCULAR_BUF_LEN = 1024; constexpr int32_t SENSOR_DATA_LENGHT = 64; struct CircularEventBuf { - struct SensorEvent *circularBuf; + struct SensorData *circularBuf; int32_t readPos; int32_t writePosition; int32_t eventNum; @@ -35,12 +35,12 @@ class ReportDataCallback : public RefBase { public: ReportDataCallback(); ~ReportDataCallback(); - int32_t ReportEventCallback(SensorEvent *event, sptr cb); + int32_t ReportEventCallback(SensorData *sensorData, sptr cb); CircularEventBuf &GetEventData(); CircularEventBuf eventsBuf_; }; -using ReportDataCb = int32_t (ReportDataCallback::*)(SensorEvent *event, sptr cb); +using ReportDataCb = int32_t (ReportDataCallback::*)(SensorData *sensorData, sptr cb); } // namespace Sensors } // namespace OHOS #endif // REPORT_DATA_CALLBACK_H diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h index 30147e6b..a98543c2 100644 --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -22,20 +22,10 @@ #include "message_parcel.h" #include "refbase.h" -#include "sensor_agent_type.h" +#include "sensor_data_event.h" namespace OHOS { namespace Sensors { -constexpr int32_t SENSOR_MAX_LENGTH = 64; -struct TransferSensorEvents { - uint32_t sensorTypeId; - int32_t version; - int64_t timestamp; - int32_t option; - int32_t mode; - uint32_t dataLen; - uint8_t data[SENSOR_MAX_LENGTH]; -}; class SensorBasicDataChannel : public RefBase { public: SensorBasicDataChannel(); @@ -51,14 +41,14 @@ public: int32_t ReceiveData(void *vaddr, size_t size); bool GetSensorStatus() const; void SetSensorStatus(bool isActive); - const std::unordered_map &GetDataCacheBuf() const; + const std::unordered_map &GetDataCacheBuf() const; private: int32_t sendFd_; int32_t receiveFd_; bool isActive_; std::mutex statusLock_; - std::unordered_map dataCacheBuf_; + std::unordered_map dataCacheBuf_; }; } // namespace Sensors } // namespace OHOS diff --git a/utils/include/sensor_data_event.h b/utils/include/sensor_data_event.h index e0e3a349..cf6751ff 100755 --- a/utils/include/sensor_data_event.h +++ b/utils/include/sensor_data_event.h @@ -21,6 +21,7 @@ namespace Sensors { constexpr int32_t RESERVED_DATA_LEN = 3; constexpr int32_t EXTRA_INFO_DATA_LEN = 14; constexpr int32_t DEFAULT_SENSOR_DATA_DIMS = 16; +constexpr int32_t SENSOR_MAX_LENGTH = 64; enum { WAKE_UP_SENSOR = 1u, @@ -30,8 +31,13 @@ enum { }; struct SensorData { - float data[DEFAULT_SENSOR_DATA_DIMS]; - uint32_t reserved[RESERVED_DATA_LEN]; + int32_t sensorTypeId; /**< Sensor type ID */ + int32_t version; /**< Sensor algorithm version */ + int64_t timestamp; /**< Time when sensor data was reported */ + uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ + int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ + uint8_t data[SENSOR_MAX_LENGTH]; /**< Sensor data */ + uint32_t dataLen; /**< Sensor data length */ }; struct ExtraInfo { diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 21231e95..7b71fa2e 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -25,7 +25,7 @@ constexpr HiLogLabel LABEL = {LOG_CORE, SENSOR_LOG_DOMAIN, "ReportDataCallback"} } // namespace ReportDataCallback::ReportDataCallback() { - eventsBuf_.circularBuf = new (std::nothrow) SensorEvent[CIRCULAR_BUF_LEN]; + eventsBuf_.circularBuf = new (std::nothrow) SensorData[CIRCULAR_BUF_LEN]; CHKPL(eventsBuf_.circularBuf); eventsBuf_.readPos = 0; eventsBuf_.writePosition = 0; @@ -44,32 +44,24 @@ ReportDataCallback::~ReportDataCallback() eventsBuf_.eventNum = 0; } -int32_t ReportDataCallback::ReportEventCallback(SensorEvent* event, sptr cb) +int32_t ReportDataCallback::ReportEventCallback(SensorData* sensorData, sptr cb) { - CHKPR(event, ERROR); + CHKPR(sensorData, ERROR); if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { SEN_HILOGE("callback or circularBuf or event cannot be null"); - if (event->data != nullptr) { - delete[] event->data; - event->data = nullptr; - } return ERROR; } int32_t leftSize = CIRCULAR_BUF_LEN - cb->eventsBuf_.eventNum; int32_t toEndLen = CIRCULAR_BUF_LEN - cb->eventsBuf_.writePosition; if (leftSize < 0 || toEndLen < 0) { SEN_HILOGE("Leftsize and toendlen cannot be less than zero"); - if (event->data != nullptr) { - delete[] event->data; - event->data = nullptr; - } return ERROR; } if (toEndLen == 0) { - cb->eventsBuf_.circularBuf[0] = *event; - cb->eventsBuf_.writePosition = 1 - toEndLen; + cb->eventsBuf_.circularBuf[0] = *sensorData; + cb->eventsBuf_.writePosition = 1; } else { - cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *event; + cb->eventsBuf_.circularBuf[cb->eventsBuf_.writePosition] = *sensorData; cb->eventsBuf_.writePosition += 1; } if (leftSize < 1) { diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 0e7ef077..29bfd591 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -28,7 +28,7 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorBasicChannel" }; -constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(SensorEvent) * 100; +constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(SensorData) * 100; constexpr int32_t DEFAULT_CHANNEL_SIZE = 2 * 1024; constexpr int32_t SOCKET_PAIR_SIZE = 2; } // namespace @@ -202,7 +202,7 @@ int32_t SensorBasicDataChannel::DestroySensorBasicChannel() return ERR_OK; } -const std::unordered_map &SensorBasicDataChannel::GetDataCacheBuf() const +const std::unordered_map &SensorBasicDataChannel::GetDataCacheBuf() const { return dataCacheBuf_; } -- Gitee