From 4b8e4eac60437f298cbb32f93ee1c63692c2b424 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 2 Mar 2023 13:19:21 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ia8d80511af179525d28ee159d5f0b4c25e64c4ea --- frameworks/native/sensor/BUILD.gn | 6 +- interfaces/native/BUILD.gn | 4 +- interfaces/native/test/BUILD.gn | 10 +- interfaces/plugin/BUILD.gn | 4 +- services/sensor/BUILD.gn | 6 +- test/unittest/common/include/system_info.h | 4 +- utils/BUILD.gn | 36 +-- utils/common/BUILD.gn | 48 ++++ utils/common/include/app_sensor.h | 61 +++++ utils/{ => common}/include/app_thread_info.h | 0 .../include/death_recipient_template.h | 0 .../{ => common}/include/miscdevice_common.h | 0 utils/{ => common}/include/permission_util.h | 1 + .../include/report_data_callback.h | 0 utils/{ => common}/include/sensor.h | 0 .../include/sensor_basic_data_channel.h | 0 .../{ => common}/include/sensor_basic_info.h | 0 .../include/sensor_channel_info.h | 0 .../{ => common}/include/sensor_data_event.h | 0 utils/{ => common}/include/sensor_log.h | 0 utils/{ => common}/include/sensors_errors.h | 3 +- utils/common/src/app_sensor.cpp | 132 +++++++++++ utils/{ => common}/src/permission_util.cpp | 10 + .../{ => common}/src/report_data_callback.cpp | 0 utils/{ => common}/src/sensor.cpp | 0 .../src/sensor_basic_data_channel.cpp | 0 utils/{ => common}/src/sensor_basic_info.cpp | 0 .../{ => common}/src/sensor_channel_info.cpp | 0 utils/ipc/include/circle_stream_buffer.h | 37 +++ utils/ipc/include/net_packet.h | 50 ++++ utils/ipc/include/proto.h | 47 ++++ utils/ipc/include/stream_buffer.h | 171 ++++++++++++++ utils/ipc/include/stream_client.h | 55 +++++ utils/ipc/include/stream_session.h | 74 ++++++ utils/ipc/include/stream_socket.h | 59 +++++ utils/ipc/src/circle_stream_buffer.cpp | 58 +++++ utils/ipc/src/net_packet.cpp | 63 +++++ utils/ipc/src/stream_buffer.cpp | 217 ++++++++++++++++++ utils/ipc/src/stream_client.cpp | 122 ++++++++++ utils/ipc/src/stream_session.cpp | 169 ++++++++++++++ utils/ipc/src/stream_socket.cpp | 159 +++++++++++++ 41 files changed, 1554 insertions(+), 52 deletions(-) create mode 100644 utils/common/BUILD.gn create mode 100644 utils/common/include/app_sensor.h rename utils/{ => common}/include/app_thread_info.h (100%) rename utils/{ => common}/include/death_recipient_template.h (100%) rename utils/{ => common}/include/miscdevice_common.h (100%) rename utils/{ => common}/include/permission_util.h (96%) rename utils/{ => common}/include/report_data_callback.h (100%) rename utils/{ => common}/include/sensor.h (100%) rename utils/{ => common}/include/sensor_basic_data_channel.h (100%) rename utils/{ => common}/include/sensor_basic_info.h (100%) rename utils/{ => common}/include/sensor_channel_info.h (100%) rename utils/{ => common}/include/sensor_data_event.h (100%) rename utils/{ => common}/include/sensor_log.h (100%) rename utils/{ => common}/include/sensors_errors.h (99%) create mode 100644 utils/common/src/app_sensor.cpp rename utils/{ => common}/src/permission_util.cpp (90%) rename utils/{ => common}/src/report_data_callback.cpp (100%) rename utils/{ => common}/src/sensor.cpp (100%) rename utils/{ => common}/src/sensor_basic_data_channel.cpp (100%) rename utils/{ => common}/src/sensor_basic_info.cpp (100%) rename utils/{ => common}/src/sensor_channel_info.cpp (100%) create mode 100644 utils/ipc/include/circle_stream_buffer.h create mode 100644 utils/ipc/include/net_packet.h create mode 100644 utils/ipc/include/proto.h create mode 100644 utils/ipc/include/stream_buffer.h create mode 100644 utils/ipc/include/stream_client.h create mode 100644 utils/ipc/include/stream_session.h create mode 100644 utils/ipc/include/stream_socket.h create mode 100644 utils/ipc/src/circle_stream_buffer.cpp create mode 100644 utils/ipc/src/net_packet.cpp create mode 100644 utils/ipc/src/stream_buffer.cpp create mode 100644 utils/ipc/src/stream_client.cpp create mode 100644 utils/ipc/src/stream_session.cpp create mode 100644 utils/ipc/src/stream_socket.cpp diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn index 9f9c6c3f..ab23c922 100755 --- a/frameworks/native/sensor/BUILD.gn +++ b/frameworks/native/sensor/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -31,10 +31,10 @@ ohos_shared_library("libsensor_native") { "include", "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/utils/common/include", "$SUBSYSTEM_DIR/sensor/interfaces/native/include", ] - deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils" ] + deps = [ "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils" ] external_deps = [ "c_utils:utils", diff --git a/interfaces/native/BUILD.gn b/interfaces/native/BUILD.gn index 1050549d..9e1bece6 100644 --- a/interfaces/native/BUILD.gn +++ b/interfaces/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -27,7 +27,7 @@ config("sensor_private_config") { "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "$SUBSYSTEM_DIR/frameworks/native/sensor/include", - "$SUBSYSTEM_DIR/utils/include", + "$SUBSYSTEM_DIR/utils/common/include", "$SUBSYSTEM_DIR/interfaces/native/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//base/notification/eventhandler/interfaces/inner_api", diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 5a175be2..45e418f6 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -27,14 +27,14 @@ ohos_unittest("SensorAgentTest") { include_dirs = [ "//commonlibrary/c_utils/base/include", - "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/utils/common/include", "$SUBSYSTEM_DIR/sensor/interfaces/native/include", "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", ] deps = [ "$SUBSYSTEM_DIR/sensor/interfaces/native:sensor_ndk_target", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] @@ -55,14 +55,14 @@ ohos_unittest("SensorAlgorithmTest") { include_dirs = [ "//commonlibrary/c_utils/base/include", - "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/utils/common/include", "$SUBSYSTEM_DIR/sensor/interfaces/native/include", "$SUBSYSTEM_DIR/sensor/test/unittest/common/include", ] deps = [ "$SUBSYSTEM_DIR/sensor/interfaces/native:sensor_ndk_target", - "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index d8445259..cd864d86 100644 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -21,7 +21,7 @@ ohos_shared_library("libsensor") { "//third_party/libuv/include", "//commonlibrary/c_utils/base/include", "./include", - "//base/sensors/sensor/utils/include", + "//base/sensors/sensor/utils/common/include", "//foundation/arkui/napi/interfaces/inner_api/napi", ] defines = [ diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index de3715a3..eb574276 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -37,7 +37,7 @@ ohos_shared_library("libsensor_service") { "$SUBSYSTEM_DIR/sensor/interfaces/native/include", "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", - "$SUBSYSTEM_DIR/sensor/utils/include", + "$SUBSYSTEM_DIR/sensor/utils/common/include", "$SUBSYSTEM_DIR/sensor/services/sensor/include", "//drivers/peripheral/sensor/interfaces/include", "hdi_connection/interface/include", @@ -45,7 +45,7 @@ ohos_shared_library("libsensor_service") { "hdi_connection/hardware/include", ] - deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils" ] + deps = [ "$SUBSYSTEM_DIR/sensor/utils/common:libsensor_utils" ] external_deps = [ "access_token:libaccesstoken_sdk", diff --git a/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h index b48b8fda..76ad756a 100644 --- a/test/unittest/common/include/system_info.h +++ b/test/unittest/common/include/system_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -36,6 +36,7 @@ class CpuInfo : public SystemInfo { public: double GetSystemCpuUsage(); double GetProcCpuUsage(const std::string& process_name); + int32_t GetTaskPidFile(const std::string& process_name); private: struct Total_Cpu_Occupy { char name[20] { 0 }; @@ -56,7 +57,6 @@ private: int32_t cutime { 0 }; int32_t cstime { 0 }; }; - int32_t GetTaskPidFile(const std::string& process_name); int32_t GetProcOccupy(int32_t pid); int32_t GetSystemCpuStatInfo(Total_Cpu_Occupy& info); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 7a971bb7..6e470778 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 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 @@ -11,38 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//build/ohos.gni") -SUBSYSTEM_DIR = "//base/sensors" -ohos_shared_library("libsensor_utils") { - sources = [ - "src/permission_util.cpp", - "src/report_data_callback.cpp", - "src/sensor.cpp", - "src/sensor_basic_data_channel.cpp", - "src/sensor_basic_info.cpp", - "src/sensor_channel_info.cpp", - ] - - include_dirs = [ - "include", - "//commonlibrary/c_utils/base/include", - "//utils/system/safwk/native/include", - "//drivers/peripheral/sensor/interfaces/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", - ] - - external_deps = [ - "access_token:libaccesstoken_sdk", - "access_token:libprivacy_sdk", - "c_utils:utils", - "hisysevent_native:libhisysevent", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - ] - part_name = "sensor" - subsystem_name = "sensors" -} - group("sensor_utils_target") { - deps = [ ":libsensor_utils" ] + deps = [ "common:libsensor_utils" ] } diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn new file mode 100644 index 00000000..c5b02656 --- /dev/null +++ b/utils/common/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2023 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. + +import("//build/ohos.gni") + +SUBSYSTEM_DIR = "//base/sensors" + +ohos_shared_library("libsensor_utils") { + sources = [ + "src/app_sensor.cpp", + "src/permission_util.cpp", + "src/report_data_callback.cpp", + "src/sensor.cpp", + "src/sensor_basic_data_channel.cpp", + "src/sensor_basic_info.cpp", + "src/sensor_channel_info.cpp", + ] + + include_dirs = [ + "include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//drivers/peripheral/sensor/interfaces/include", + "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libprivacy_sdk", + "c_utils:utils", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + part_name = "sensor" + subsystem_name = "sensors" +} diff --git a/utils/common/include/app_sensor.h b/utils/common/include/app_sensor.h new file mode 100644 index 00000000..18558e94 --- /dev/null +++ b/utils/common/include/app_sensor.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 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 APP_SENSOR_H +#define APP_SENSOR_H + +#include "parcel.h" + +namespace OHOS { +namespace Sensors { +struct SensorStatus { + int32_t sensorId {-1}; + bool isActive {false}; + int64_t samplingPeriodNs {-1}; + int64_t maxReportDelayNs {-1}; + SensorStatus() {} + SensorStatus(int32_t sensorId, bool isActive, int64_t samplingPeriodNs, int64_t maxReportDelayNs) + : sensorId(sensorId), isActive(isActive), samplingPeriodNs(samplingPeriodNs), + maxReportDelayNs(maxReportDelayNs) {} +}; + +class AppSensor : public Parcelable { +public: + AppSensor() = default; + AppSensor(int32_t pid, SensorStatus sensorStatus); + virtual ~AppSensor() = default; + int32_t GetPid() const; + void SetPid(int32_t pid); + int32_t GetSensorId() const; + void SetSensorId(int32_t sensorId); + bool IsActive() const; + void Enable(bool isActive); + int64_t GetSamplingPeriodNs() const; + void SetSamplingPeriodNs(int64_t samplingPeriodNs); + int64_t GetMaxReportDelayNs() const; + void SetMaxReportDelayNs(int64_t maxReportDelayNs); + bool Marshalling(Parcel &parcel) const; + std::unique_ptr Unmarshalling(Parcel &parcel); + +private: + int32_t pid_ {-1}; + int32_t sensorId_ {-1}; + bool isActive_ {-1}; + int64_t samplingPeriodNs_ {-1}; + int64_t maxReportDelayNs_ {-1}; +}; +} // namespace Sensors +} // namespace OHOS +#endif // APP_SENSOR_H \ No newline at end of file diff --git a/utils/include/app_thread_info.h b/utils/common/include/app_thread_info.h similarity index 100% rename from utils/include/app_thread_info.h rename to utils/common/include/app_thread_info.h diff --git a/utils/include/death_recipient_template.h b/utils/common/include/death_recipient_template.h similarity index 100% rename from utils/include/death_recipient_template.h rename to utils/common/include/death_recipient_template.h diff --git a/utils/include/miscdevice_common.h b/utils/common/include/miscdevice_common.h similarity index 100% rename from utils/include/miscdevice_common.h rename to utils/common/include/miscdevice_common.h diff --git a/utils/include/permission_util.h b/utils/common/include/permission_util.h similarity index 96% rename from utils/include/permission_util.h rename to utils/common/include/permission_util.h index ba61b2f8..dadbacbd 100644 --- a/utils/include/permission_util.h +++ b/utils/common/include/permission_util.h @@ -30,6 +30,7 @@ public: PermissionUtil() = default; virtual ~PermissionUtil() {}; int32_t CheckSensorPermission(AccessTokenID callerToken, int32_t sensorTypeId); + bool IsNativeToken(AccessTokenID tokenID); private: void AddPermissionRecord(AccessTokenID tokenID, const std::string& permissionName, bool status); diff --git a/utils/include/report_data_callback.h b/utils/common/include/report_data_callback.h similarity index 100% rename from utils/include/report_data_callback.h rename to utils/common/include/report_data_callback.h diff --git a/utils/include/sensor.h b/utils/common/include/sensor.h similarity index 100% rename from utils/include/sensor.h rename to utils/common/include/sensor.h diff --git a/utils/include/sensor_basic_data_channel.h b/utils/common/include/sensor_basic_data_channel.h similarity index 100% rename from utils/include/sensor_basic_data_channel.h rename to utils/common/include/sensor_basic_data_channel.h diff --git a/utils/include/sensor_basic_info.h b/utils/common/include/sensor_basic_info.h similarity index 100% rename from utils/include/sensor_basic_info.h rename to utils/common/include/sensor_basic_info.h diff --git a/utils/include/sensor_channel_info.h b/utils/common/include/sensor_channel_info.h similarity index 100% rename from utils/include/sensor_channel_info.h rename to utils/common/include/sensor_channel_info.h diff --git a/utils/include/sensor_data_event.h b/utils/common/include/sensor_data_event.h similarity index 100% rename from utils/include/sensor_data_event.h rename to utils/common/include/sensor_data_event.h diff --git a/utils/include/sensor_log.h b/utils/common/include/sensor_log.h similarity index 100% rename from utils/include/sensor_log.h rename to utils/common/include/sensor_log.h diff --git a/utils/include/sensors_errors.h b/utils/common/include/sensors_errors.h similarity index 99% rename from utils/include/sensors_errors.h rename to utils/common/include/sensors_errors.h index 55e11c3c..35335518 100644 --- a/utils/include/sensors_errors.h +++ b/utils/common/include/sensors_errors.h @@ -88,7 +88,8 @@ 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, + READ_MSG_ERR = WRITE_MSG_ERR + 1, + SET_SENSOR_MODE_ERR = READ_MSG_ERR + 1, SET_SENSOR_OPTION_ERR = SET_SENSOR_MODE_ERR + 1, REGIST_CALLBACK_ERR = SET_SENSOR_OPTION_ERR + 1, }; diff --git a/utils/common/src/app_sensor.cpp b/utils/common/src/app_sensor.cpp new file mode 100644 index 00000000..18668359 --- /dev/null +++ b/utils/common/src/app_sensor.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2023 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 "app_sensor.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "AppSensor" }; +} + +AppSensor::AppSensor(int32_t pid, SensorStatus sensorStatus) +{ + pid_ = pid; + sensorId_ = sensorStatus.sensorId; + isActive_ = sensorStatus.isActive; + samplingPeriodNs_ = sensorStatus.samplingPeriodNs; + maxReportDelayNs_ = sensorStatus.maxReportDelayNs; +} + +int32_t AppSensor::GetPid() const +{ + return pid_; +} + +void AppSensor::SetPid(int32_t pid) +{ + pid_ = pid; +} + +int32_t AppSensor::GetSensorId() const +{ + return sensorId_; +} + +void AppSensor::SetSensorId(int32_t sensorId) +{ + sensorId_ = sensorId; +} + +bool AppSensor::IsActive() const +{ + return isActive_; +} + +void AppSensor::Enable(bool isActive) +{ + isActive_ = isActive; +} + +int64_t AppSensor::GetSamplingPeriodNs() const +{ + return samplingPeriodNs_; +} + +void AppSensor::SetSamplingPeriodNs(int64_t samplingPeriodNs) +{ + samplingPeriodNs_ = samplingPeriodNs; +} + +int64_t AppSensor::GetMaxReportDelayNs() const +{ + return maxReportDelayNs_; +} + +void AppSensor::SetMaxReportDelayNs(int64_t maxReportDelayNs) +{ + maxReportDelayNs_ = maxReportDelayNs; +} + +bool AppSensor::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteInt32(pid_)) { + SEN_HILOGE("Write pid failed"); + return false; + } + if (!parcel.WriteInt32(sensorId_)) { + SEN_HILOGE("Write sensorId failed"); + return false; + } + if (!parcel.WriteBool(isActive_)) { + SEN_HILOGE("Write isActive failed"); + return false; + } + if (!parcel.WriteInt64(samplingPeriodNs_)) { + SEN_HILOGE("Write samplingPeriodNs failed"); + return false; + } + if (!parcel.WriteInt64(maxReportDelayNs_)) { + SEN_HILOGE("Write maxReportDelayNs failed"); + return false; + } + return true; +} + +std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) +{ + int32_t pid = -1; + int32_t sensorId = -1; + bool isActive = false; + int64_t samplingPeriodNs = -1; + int64_t maxReportDelayNs = -1; + if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) && parcel.ReadBool(isActive) && + parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("ReadFromParcel is failed"); + return nullptr; + } + auto appSensor = std::make_unique(); + appSensor->SetPid(pid); + appSensor->SetSensorId(sensorId); + appSensor->Enable(isActive); + appSensor->SetSamplingPeriodNs(samplingPeriodNs); + appSensor->SetMaxReportDelayNs(maxReportDelayNs); + return appSensor; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/utils/src/permission_util.cpp b/utils/common/src/permission_util.cpp similarity index 90% rename from utils/src/permission_util.cpp rename to utils/common/src/permission_util.cpp index 4e2d91e0..9e5bd305 100644 --- a/utils/src/permission_util.cpp +++ b/utils/common/src/permission_util.cpp @@ -67,5 +67,15 @@ void PermissionUtil::AddPermissionRecord(AccessTokenID tokenID, const std::strin permissionName.c_str(), successCount, failCount); } } + +bool PermissionUtil::IsNativeToken(AccessTokenID tokenID) +{ + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenID); + if (tokenType != ATokenTypeEnum::TOKEN_NATIVE) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE, tokenType:%{public}d", tokenType); + return false; + } + return true; +} } // namespace Sensors } // namespace OHOS diff --git a/utils/src/report_data_callback.cpp b/utils/common/src/report_data_callback.cpp similarity index 100% rename from utils/src/report_data_callback.cpp rename to utils/common/src/report_data_callback.cpp diff --git a/utils/src/sensor.cpp b/utils/common/src/sensor.cpp similarity index 100% rename from utils/src/sensor.cpp rename to utils/common/src/sensor.cpp diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/common/src/sensor_basic_data_channel.cpp similarity index 100% rename from utils/src/sensor_basic_data_channel.cpp rename to utils/common/src/sensor_basic_data_channel.cpp diff --git a/utils/src/sensor_basic_info.cpp b/utils/common/src/sensor_basic_info.cpp similarity index 100% rename from utils/src/sensor_basic_info.cpp rename to utils/common/src/sensor_basic_info.cpp diff --git a/utils/src/sensor_channel_info.cpp b/utils/common/src/sensor_channel_info.cpp similarity index 100% rename from utils/src/sensor_channel_info.cpp rename to utils/common/src/sensor_channel_info.cpp diff --git a/utils/ipc/include/circle_stream_buffer.h b/utils/ipc/include/circle_stream_buffer.h new file mode 100644 index 00000000..3aaea490 --- /dev/null +++ b/utils/ipc/include/circle_stream_buffer.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 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 CIRCLE_STREAM_BUFFER_H +#define CIRCLE_STREAM_BUFFER_H + +#include "stream_buffer.h" + +namespace OHOS { +namespace Sensors { +class CircleStreamBuffer : public StreamBuffer { + static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "CircleStreamBuffer" }; +public: + CircleStreamBuffer() = default; + virtual ~CircleStreamBuffer() = default; + bool CheckWrite(size_t size); + virtual bool Write(const char *buf, size_t size) override; + DISALLOW_MOVE(CircleStreamBuffer); + +protected: + void CopyDataToBegin(); +}; +} // namespace Sensors +} // namespace OHOS +#endif // CIRCLE_STREAM_BUFFER_H \ No newline at end of file diff --git a/utils/ipc/include/net_packet.h b/utils/ipc/include/net_packet.h new file mode 100644 index 00000000..33d3545e --- /dev/null +++ b/utils/ipc/include/net_packet.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 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 NET_PACKET_H +#define NET_PACKET_H + +#include "proto.h" +#include "stream_buffer.h" + +#pragma pack(1) +using PACKHEAD = struct PackHead { + OHOS::Sensors::MessageId idMsg; + int32_t size; +}; + +#pragma pack() +namespace OHOS { +namespace Sensors { +class NetPacket : public StreamBuffer { + static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "NetPacket" }; +public: + explicit NetPacket(MessageId msgId); + NetPacket(const NetPacket &pkt); + NetPacket &operator = (const NetPacket &pkt); + virtual ~NetPacket(); + virtual void MakeData(StreamBuffer &buf) const; + size_t GetSize() const; + int32_t GetPacketLength() const; + const char* GetData() const; + MessageId GetMsgId() const; + DISALLOW_MOVE(NetPacket); + +protected: + MessageId msgId_ = MessageId::INVALID; +}; +} // namespace Sensors +} // namespace OHOS +#endif // NET_PACKET_H \ No newline at end of file diff --git a/utils/ipc/include/proto.h b/utils/ipc/include/proto.h new file mode 100644 index 00000000..207b8640 --- /dev/null +++ b/utils/ipc/include/proto.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 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 PROTO_H +#define PROTO_H + +#include + +namespace OHOS { +namespace Sensors { +#define PROTO_SEND_RETRY_LIMIT 32 +#define PROTO_SEND_RETRY_SLEEP_TIME 10000 + +static const int32_t PROTO_STREAM_BUF_READ_FAIL = 1; +static const int32_t PROTO_STREAM_BUF_WRITE_FAIL = 2; +static const int32_t PROTO_MEM_OUT_OF_BOUNDS = 3; +static const int32_t PROTO_MEMCPY_SEC_FUN_FAIL = 4; +static const int32_t PROTO_PARAM_INPUT_INVALID = 5; +static const int32_t PROTO_SESSION_NOT_FOUND = 8; +static const int32_t PROTO_MAX_VECTOR_SIZE = 10; +static const int32_t PROTO_ADD_SESSION_FAIL = 11; +static const int32_t PROTO_MAX_SESSON_ALARM = 12; +static const int32_t PROTO_MAX_RECV_LIMIT = 13; + +static const int32_t PROTO_MAX_STREAM_BUF_SIZE = 256; +static const int32_t PROTO_MAX_PACKET_BUF_SIZE = 256; +static const int32_t PROTO_ONCE_PROCESS_NETPACKET_LIMIT = 100; + +enum class MessageId : int32_t { + INVALID, + CLIENT_INFO, +}; +} // namespace Sensors +} // namespace OHOS +#endif // PROTO_H \ No newline at end of file diff --git a/utils/ipc/include/stream_buffer.h b/utils/ipc/include/stream_buffer.h new file mode 100644 index 00000000..0cea6fae --- /dev/null +++ b/utils/ipc/include/stream_buffer.h @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2023 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 STREAM_BUFFER_H +#define STREAM_BUFFER_H + +#include +#include +#include + +#include "nocopyable.h" +#include "securec.h" + +#include "proto.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +class StreamBuffer { + static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamBuffer" }; +public: + StreamBuffer() = default; + explicit StreamBuffer(const StreamBuffer &buf); + virtual StreamBuffer &operator=(const StreamBuffer &other); + virtual ~StreamBuffer() = default; + void Reset(); + void Clean(); + bool SeekReadPos(int32_t n); + bool Read(std::string &buf); + bool Read(StreamBuffer &buf); + bool Read(char *buf, size_t size); + bool Write(const std::string &buf); + bool Write(const StreamBuffer &buf); + virtual bool Write(const char *buf, size_t size); + bool IsEmpty() const; + bool ChkRWError() const; + size_t Size() const; + int32_t UnreadSize() const; + int32_t GetAvailableBufSize() const; + const std::string& GetErrorStatusRemark() const; + const char* Data() const; + + template + bool Read(T &data); + template + bool Write(const T &data); + template + bool Read(std::vector &data); + template + bool Write(const std::vector &data); + const char *ReadBuf() const; + const char *WriteBuf() const; + template + StreamBuffer &operator >> (T &data); + template + StreamBuffer &operator << (const T &data); + DISALLOW_MOVE(StreamBuffer); + +protected: + bool Clone(const StreamBuffer &buf); + enum class ErrorStatus { + ERROR_STATUS_OK, + ERROR_STATUS_READ, + ERROR_STATUS_WRITE, + }; + ErrorStatus rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK; + int32_t rCount_ { 0 }; + int32_t wCount_ { 0 }; + int32_t rPos_ { 0 }; + int32_t wPos_ { 0 }; + char szBuff_[PROTO_MAX_STREAM_BUF_SIZE + 1] = {}; +}; + +template +bool StreamBuffer::Read(T &data) +{ + if (!Read(reinterpret_cast(&data), sizeof(data))) { + SEN_HILOGE("[%{public}s] size:%{public}zu count:%{public}d,errCode:%{public}d", + GetErrorStatusRemark().c_str(), sizeof(data), rCount_ + 1, PROTO_STREAM_BUF_READ_FAIL); + return false; + } + return true; +} + +template +bool StreamBuffer::Write(const T &data) +{ + if (!Write(reinterpret_cast(&data), sizeof(data))) { + SEN_HILOGE("[%{public}s] size:%{public}zu,count:%{public}d,errCode:%{public}d", + GetErrorStatusRemark().c_str(), sizeof(data), wCount_ + 1, PROTO_STREAM_BUF_WRITE_FAIL); + return false; + } + return true; +} + +template +bool StreamBuffer::Read(std::vector &data) +{ + int32_t size = 0; + if (!Read(size)) { + SEN_HILOGE("Read vector size error"); + return false; + } + if (size < 0 || size > PROTO_MAX_VECTOR_SIZE) { + SEN_HILOGE("Read vector size:%{public}d error", size); + return false; + } + for (int32_t i = 0; i < size; i++) { + T val; + if (!Read(val)) { + SEN_HILOGE("Read vector data error"); + return false; + } + data.push_back(val); + } + return true; +} + +template +bool StreamBuffer::Write(const std::vector &data) +{ + if (data.size() > INT32_MAX) { + SEN_HILOGE("Vector exceeds the max range"); + return false; + } + int32_t size = static_cast(data.size()); + if (!Write(size)) { + SEN_HILOGE("Write vector size error"); + return false; + } + for (const auto &item : data) { + if (!Write(item)) { + SEN_HILOGE("Write vector data error"); + return false; + } + } + return true; +} + +template +StreamBuffer &StreamBuffer::operator>>(T &data) +{ + if (!Read(data)) { + SEN_HILOGW("Read data failed"); + } + return *this; +} + +template +StreamBuffer &StreamBuffer::operator<<(const T &data) +{ + if (!Write(data)) { + SEN_HILOGW("Write data failed"); + } + return *this; +} +} // namespace Sensors +} // namespace OHOS +#endif // STREAM_BUFFER_H \ No newline at end of file diff --git a/utils/ipc/include/stream_client.h b/utils/ipc/include/stream_client.h new file mode 100644 index 00000000..4dd9a1e9 --- /dev/null +++ b/utils/ipc/include/stream_client.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 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 STREAM_CLIENT_H +#define STREAM_CLIENT_H + +#include +#include +#include + +#include "nocopyable.h" + +#include "net_packet.h" +#include "stream_socket.h" + +namespace OHOS { +namespace Sensors { +class StreamClient; +using MsgClientFunCallback = std::function; +class StreamClient : public StreamSocket { +public: + StreamClient(); + virtual ~StreamClient(); + virtual int32_t Socket() = 0; + virtual void Stop(); + int32_t ConnectTo(); + bool SendMsg(const char *buf, size_t size) const; + bool SendMsg(const NetPacket &pkt) const; + bool GetConnectedStatus() const; + DISALLOW_COPY_AND_MOVE(StreamClient); + +protected: + virtual void OnConnected() {} + virtual void OnDisconnected() {} + bool StartClient(MsgClientFunCallback fun); + bool isExit { false }; + bool isRunning_ { false }; + bool isConnected_ { false }; + MsgClientFunCallback recvFun_; +}; +} // namespace Sensors +} // namespace OHOS +#endif // STREAM_CLIENT_H \ No newline at end of file diff --git a/utils/ipc/include/stream_session.h b/utils/ipc/include/stream_session.h new file mode 100644 index 00000000..adbf4dc1 --- /dev/null +++ b/utils/ipc/include/stream_session.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 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 STREAM_SESSION_H +#define STREAM_SESSION_H + +#include +#include +#include + +#include +#include + +#include "accesstoken_kit.h" +#include "nocopyable.h" + +#include "net_packet.h" +#include "proto.h" + +namespace OHOS { +namespace Sensors { +class StreamSession; +using SessionPtr = std::shared_ptr; +using namespace Security::AccessToken; +class StreamSession : public std::enable_shared_from_this { +public: + StreamSession(const std::string &programName, const int32_t moduleType, const int32_t fd, const int32_t uid, + const int32_t pid); + virtual ~StreamSession() = default; + bool SendMsg(const char *buf, size_t size) const; + bool SendMsg(NetPacket &pkt) const; + void Close(); + int32_t GetUid() const; + int32_t GetPid() const; + int32_t GetModuleType() const; + SessionPtr GetSharedPtr(); + int32_t GetFd() const; + const std::string& GetDescript() const; + const std::string GetProgramName() const; + void SetTokenType(int32_t type); + int32_t GetTokenType() const; + void UpdateDescript(); + DISALLOW_COPY_AND_MOVE(StreamSession); + +protected: + struct EventTime { + int32_t id { 0 }; + int64_t eventTime { 0 }; + int32_t timerId { -1 }; + }; + std::map> events_; + std::string descript_; + const std::string programName_; + const int32_t moduleType_ { -1 }; + int32_t fd_ { -1 }; + const int32_t uid_ { -1 }; + const int32_t pid_ { -1 }; + int32_t tokenType_ { ATokenTypeEnum::TOKEN_INVALID }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // STREAM_SESSION_H \ No newline at end of file diff --git a/utils/ipc/include/stream_socket.h b/utils/ipc/include/stream_socket.h new file mode 100644 index 00000000..acae2006 --- /dev/null +++ b/utils/ipc/include/stream_socket.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 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 STREAM_SOCKET_H +#define STREAM_SOCKET_H + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "nocopyable.h" + +#include "circle_stream_buffer.h" +#include "net_packet.h" + +namespace OHOS { +namespace Sensors { +class StreamSocket { +public: + using PacketCallBackFun = std::function; + StreamSocket(); + virtual ~StreamSocket(); + int32_t EpollCreate(int32_t size); + int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd = -1); + int32_t EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd = -1); + void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun); + void EpollClose(); + void Close(); + int32_t GetFd() const; + int32_t GetEpollFd() const; + + DISALLOW_COPY_AND_MOVE(StreamSocket); + +protected: + int32_t fd_ { -1 }; + int32_t epollFd_ { -1 }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // STREAM_SOCKET_H \ No newline at end of file diff --git a/utils/ipc/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp new file mode 100644 index 00000000..a2c91f54 --- /dev/null +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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 "circle_stream_buffer.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +bool CircleStreamBuffer::CheckWrite(size_t size) +{ + int32_t bufferSize = static_cast(size); + int32_t availSize = GetAvailableBufSize(); + if (bufferSize > availSize && rPos_ > 0) { + CopyDataToBegin(); + availSize = GetAvailableBufSize(); + } + return (availSize >= bufferSize); +} + +bool CircleStreamBuffer::Write(const char *buf, size_t size) +{ + if (!CheckWrite(size)) { + SEN_HILOGE("Out of buffer memory, availableSize:%{public}d, size:%{public}zu," + "unreadSize:%{public}d, rPos:%{public}d, wPos:%{public}d", + GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_); + return false; + } + return StreamBuffer::Write(buf, size); +} + +void CircleStreamBuffer::CopyDataToBegin() +{ + int32_t unreadSize = UnreadSize(); + if (unreadSize > 0 && rPos_ > 0) { + int32_t pos = 0; + for (int32_t i = rPos_; i <= wPos_;) { + szBuff_[pos++] = szBuff_[i++]; + } + } + SEN_HILOGD("UnreadSize:%{public}d, rPos:%{public}d, wPos:%{public}d", unreadSize, rPos_, wPos_); + rPos_ = 0; + wPos_ = unreadSize; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/net_packet.cpp b/utils/ipc/src/net_packet.cpp new file mode 100644 index 00000000..7e3a3bae --- /dev/null +++ b/utils/ipc/src/net_packet.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 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 "net_packet.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +NetPacket::NetPacket(MessageId msgId) : msgId_(msgId) {} + +NetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId()) +{ + Clone(pkt); +} + +NetPacket::~NetPacket() {} + +void NetPacket::MakeData(StreamBuffer &buf) const +{ + PACKHEAD head = {msgId_, wPos_}; + buf << head; + if (wPos_ > 0) { + if (!buf.Write(&szBuff_[0], wPos_)) { + SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); + return; + } + } +} + +size_t NetPacket::GetSize() const +{ + return Size(); +} + +int32_t NetPacket::GetPacketLength() const +{ + return (static_cast(sizeof(PackHead)) + wPos_); +} + +const char* NetPacket::GetData() const +{ + return Data(); +} + +MessageId NetPacket::GetMsgId() const +{ + return msgId_; +} +} // namespace Sensors +} // namespace OHOS diff --git a/utils/ipc/src/stream_buffer.cpp b/utils/ipc/src/stream_buffer.cpp new file mode 100644 index 00000000..a586ef72 --- /dev/null +++ b/utils/ipc/src/stream_buffer.cpp @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2023 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 "stream_buffer.h" + +#include +#include + +namespace OHOS { +namespace Sensors { +StreamBuffer::StreamBuffer(const StreamBuffer &buf) +{ + Clone(buf); +} + +StreamBuffer &StreamBuffer::operator=(const StreamBuffer &other) +{ + Clone(other); + return *this; +} + +void StreamBuffer::Reset() +{ + rPos_ = 0; + wPos_ = 0; + rCount_ = 0; + wCount_ = 0; + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK; +} + +void StreamBuffer::Clean() +{ + Reset(); + errno_t ret = memset_sp(&szBuff_, sizeof(szBuff_), 0, sizeof(szBuff_)); + if (ret != EOK) { + SEN_HILOGE("Call memset_s fail"); + return; + } +} + +bool StreamBuffer::SeekReadPos(int32_t n) +{ + int32_t pos = rPos_ + n; + if (pos < 0 || pos > wPos_) { + SEN_HILOGE("The position in the calculation is not as expected. pos:%{public}d [0, %{public}d]", pos, wPos_); + return false; + } + rPos_ = pos; + return true; +} + +bool StreamBuffer::Read(std::string &buf) +{ + if (rPos_ == wPos_) { + SEN_HILOGE("Not enough memory to read, errCode:%{public}d", PROTO_STREAM_BUF_READ_FAIL); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; + return false; + } + buf = ReadBuf(); + rPos_ += static_cast(buf.length()) + 1; + return (buf.length() > 0); +} + +bool StreamBuffer::Write(const std::string &buf) +{ + return Write(buf.c_str(), buf.length()+1); +} + +bool StreamBuffer::Read(StreamBuffer &buf) +{ + return buf.Write(Data(), Size()); +} + +bool StreamBuffer::Write(const StreamBuffer &buf) +{ + return Write(buf.Data(), buf.Size()); +} + +bool StreamBuffer::Read(char *buf, size_t size) +{ + if (ChkRWError()) { + return false; + } + if (buf == nullptr) { + SEN_HILOGE("Invalid input parameter buf=nullptr errCode:%{public}d", PROTO_PARAM_INPUT_INVALID); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; + return false; + } + if (size == 0) { + SEN_HILOGE("Invalid input parameter size=%{public}zu errCode:%{public}d", size, PROTO_PARAM_INPUT_INVALID); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; + return false; + } + if (rPos_ + static_cast(size) > wPos_) { + SEN_HILOGE("Memory out of bounds on read... errCode:%{public}d", PROTO_MEM_OUT_OF_BOUNDS); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; + return false; + } + errno_t ret = memcpy_sp(buf, size, ReadBuf(), size); + if (ret != EOK) { + SEN_HILOGE("Failed to call memcpy_sp. errCode:%{public}d", PROTO_MEMCPY_SEC_FUN_FAIL); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; + return false; + } + rPos_ += static_cast(size); + rCount_ += 1; + return true; +} + +bool StreamBuffer::Write(const char *buf, size_t size) +{ + if (ChkRWError()) { + return false; + } + if (buf == nullptr) { + SEN_HILOGE("Invalid input parameter buf=nullptr errCode:%{public}d", PROTO_PARAM_INPUT_INVALID); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; + return false; + } + if (size == 0) { + SEN_HILOGE("Invalid input parameter size=%{public}zu errCode:%{public}d", size, PROTO_PARAM_INPUT_INVALID); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; + return false; + } + if (wPos_ + static_cast(size) > PROTO_MAX_STREAM_BUF_SIZE) { + SEN_HILOGE("The write length exceeds buffer. wIdx:%{public}d size:%{public}zu maxBufSize:%{public}d" + "errCode:%{public}d", wPos_, size, PROTO_MAX_STREAM_BUF_SIZE, PROTO_MEM_OUT_OF_BOUNDS); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; + return false; + } + errno_t ret = memcpy_sp(&szBuff_[wPos_], GetAvailableBufSize(), buf, size); + if (ret != EOK) { + SEN_HILOGE("Failed to call memcpy_sp. errCode:%{public}d", PROTO_MEMCPY_SEC_FUN_FAIL); + rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; + return false; + } + wPos_ += static_cast(size); + wCount_ += 1; + return true; +} + +bool StreamBuffer::IsEmpty() const +{ + return (rPos_ == wPos_); +} + +size_t StreamBuffer::Size() const +{ + return static_cast(wPos_); +} + +int32_t StreamBuffer::UnreadSize() const +{ + return ((wPos_ <= rPos_) ? 0 : (wPos_ - rPos_)); +} + +int32_t StreamBuffer::GetAvailableBufSize() const +{ + return ((wPos_ >= PROTO_MAX_STREAM_BUF_SIZE) ? 0 : (PROTO_MAX_STREAM_BUF_SIZE - wPos_)); +} + +bool StreamBuffer::ChkRWError() const +{ + return (rwErrorStatus_ != ErrorStatus::ERROR_STATUS_OK); +} + +const std::string &StreamBuffer::GetErrorStatusRemark() const +{ + static const std::vector> remark { + {ErrorStatus::ERROR_STATUS_OK, "OK"}, + {ErrorStatus::ERROR_STATUS_READ, "READ_ERROR"}, + {ErrorStatus::ERROR_STATUS_WRITE, "WRITE_ERROR"}, + }; + static const std::string invalidStatus { "UNKNOWN" }; + + auto tIter = std::find_if(remark.cbegin(), remark.cend(), + [this](const auto &item) { + return (item.first == rwErrorStatus_); + } + ); + return (tIter != remark.cend() ? tIter->second : invalidStatus); +} + +const char *StreamBuffer::Data() const +{ + return &szBuff_[0]; +} + +const char *StreamBuffer::ReadBuf() const +{ + return &szBuff_[rPos_]; +} + +const char *StreamBuffer::WriteBuf() const +{ + return &szBuff_[wPos_]; +} + +bool StreamBuffer::Clone(const StreamBuffer &buf) +{ + Clean(); + return Write(buf.Data(), buf.Size()); +} +} // namespace Sensors +} // namespace OHOS diff --git a/utils/ipc/src/stream_client.cpp b/utils/ipc/src/stream_client.cpp new file mode 100644 index 00000000..4e1fedf9 --- /dev/null +++ b/utils/ipc/src/stream_client.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2023 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 "stream_client.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamClient" }; +} + +StreamClient::StreamClient() {} + +StreamClient::~StreamClient() {} + +int32_t StreamClient::ConnectTo() +{ + CALL_LOG_ENTER; + if (Socket() < 0) { + SEN_HILOGE("Socket failed"); + return ERROR; + } + OnConnected(); + return SUCCESS; +} + +bool StreamClient::SendMsg(const char *buf, size_t size) const +{ + CALL_LOG_ENTER; + CHKPF(buf); + if ((size == 0) || (size > PROTO_MAX_PACKET_BUF_SIZE)) { + SEN_HILOGE("Stream buffer size out of range"); + return false; + } + if (fd_ < 0) { + SEN_HILOGE("The fd_ is less than 0"); + return false; + } + int32_t idx = 0; + int32_t retryCount = 0; + const int32_t bufSize = static_cast(size); + int32_t remSize = bufSize; + while (remSize > 0 && retryCount < PROTO_SEND_RETRY_LIMIT) { + retryCount += 1; + auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL); + if (count < 0) { + if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { + SEN_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno); + continue; + } + SEN_HILOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_); + return false; + } + idx += count; + remSize -= count; + if (remSize > 0) { + usleep(PROTO_SEND_RETRY_SLEEP_TIME); + } + } + if (retryCount >= PROTO_SEND_RETRY_LIMIT || remSize != 0) { + SEN_HILOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d", + retryCount, PROTO_SEND_RETRY_LIMIT, idx, bufSize, fd_); + return false; + } + return true; +} + +bool StreamClient::SendMsg(const NetPacket &pkt) const +{ + CALL_LOG_ENTER; + if (pkt.ChkRWError()) { + SEN_HILOGE("Read and write status is error"); + return false; + } + StreamBuffer buf; + pkt.MakeData(buf); + return SendMsg(buf.Data(), buf.Size()); +} + +bool StreamClient::StartClient(MsgClientFunCallback fun) +{ + CALL_LOG_ENTER; + if (isRunning_ || isConnected_) { + SEN_HILOGE("Client is connected or started"); + return false; + } + isExit = false; + recvFun_ = fun; + if (ConnectTo() < 0) { + SEN_HILOGW("Client connection failed, Try again later"); + } + return true; +} + +void StreamClient::Stop() +{ + CALL_LOG_ENTER; + isExit = true; + isRunning_ = false; + Close(); +} + +bool StreamClient::GetConnectedStatus() const +{ + return isConnected_; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/stream_session.cpp b/utils/ipc/src/stream_session.cpp new file mode 100644 index 00000000..dd837974 --- /dev/null +++ b/utils/ipc/src/stream_session.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2023 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 "stream_session.h" + +#include +#include + +#include +#include +#include +#include + +#include "proto.h" +#include "stream_socket.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamSession" }; +} + +StreamSession::StreamSession(const std::string &programName, const int32_t moduleType, const int32_t fd, + const int32_t uid, const int32_t pid) + : programName_(programName), + moduleType_(moduleType), + fd_(fd), + uid_(uid), + pid_(pid) +{ + UpdateDescript(); +} + +bool StreamSession::SendMsg(const char *buf, size_t size) const +{ + CHKPF(buf); + if ((size == 0) || (size > PROTO_MAX_PACKET_BUF_SIZE)) { + SEN_HILOGE("Buf size:%{public}zu", size); + return false; + } + if (fd_ < 0) { + SEN_HILOGE("The fd_ is less than 0"); + return false; + } + + int32_t idx = 0; + int32_t retryCount = 0; + const int32_t bufSize = static_cast(size); + int32_t remSize = bufSize; + while (remSize > 0 && retryCount < PROTO_SEND_RETRY_LIMIT) { + retryCount += 1; + auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL); + if (count < 0) { + if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { + usleep(PROTO_SEND_RETRY_SLEEP_TIME); + SEN_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno); + continue; + } + SEN_HILOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_); + return false; + } + idx += count; + remSize -= count; + if (remSize > 0) { + usleep(PROTO_SEND_RETRY_SLEEP_TIME); + } + } + if (retryCount >= PROTO_SEND_RETRY_LIMIT || remSize != 0) { + SEN_HILOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d", + retryCount, PROTO_SEND_RETRY_LIMIT, idx, bufSize, fd_); + return false; + } + return true; +} + +void StreamSession::Close() +{ + SEN_HILOGD("Enter fd_:%{public}d.", fd_); + if (fd_ >= 0) { + close(fd_); + fd_ = -1; + UpdateDescript(); + } +} + +void StreamSession::UpdateDescript() +{ + std::ostringstream oss; + oss << "fd = " << fd_ + << ", programName = " << programName_ + << ", moduleType = " << moduleType_ + << ((fd_ < 0) ? ", closed" : ", opened") + << ", uid = " << uid_ + << ", pid = " << pid_ + << ", tokenType = " << tokenType_ + << std::endl; + descript_ = oss.str().c_str(); +} + +bool StreamSession::SendMsg(NetPacket &pkt) const +{ + if (pkt.ChkRWError()) { + SEN_HILOGE("Read and write status is error"); + return false; + } + StreamBuffer buf; + pkt.MakeData(buf); + return SendMsg(buf.Data(), buf.Size()); +} + +int32_t StreamSession::GetUid() const +{ + return uid_; +} + +int32_t StreamSession::GetPid() const +{ + return pid_; +} + +int32_t StreamSession::GetModuleType() const +{ + return moduleType_; +} + +SessionPtr StreamSession::GetSharedPtr() +{ + return shared_from_this(); +} + +int32_t StreamSession::GetFd() const +{ + return fd_; +} + +const std::string& StreamSession::GetDescript() const +{ + return descript_; +} + +const std::string StreamSession::GetProgramName() const +{ + return programName_; +} + +void StreamSession::SetTokenType(int32_t type) +{ + tokenType_ = type; +} + +int32_t StreamSession::GetTokenType() const +{ + return tokenType_; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/stream_socket.cpp b/utils/ipc/src/stream_socket.cpp new file mode 100644 index 00000000..1a2044df --- /dev/null +++ b/utils/ipc/src/stream_socket.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2023 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 "stream_socket.h" + +#include + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamSocket" }; +} // namespace + +StreamSocket::StreamSocket() {} + +StreamSocket::~StreamSocket() +{ + Close(); + EpollClose(); +} + +int32_t StreamSocket::EpollCreate(int32_t size) +{ + epollFd_ = epoll_create(size); + if (epollFd_ < 0) { + SEN_HILOGE("Epoll_create return %{public}d", epollFd_); + } else { + SEN_HILOGI("Epoll_create, epollFd_:%{public}d", epollFd_); + } + return epollFd_; +} + +int32_t StreamSocket::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd) +{ + if (fd < 0) { + SEN_HILOGE("Invalid fd"); + return ERROR; + } + if (epollFd < 0) { + epollFd = epollFd_; + } + if (epollFd < 0) { + SEN_HILOGE("Invalid param epollFd"); + return ERROR; + } + int32_t ret; + if (op == EPOLL_CTL_DEL) { + ret = epoll_ctl(epollFd, op, fd, NULL); + } else { + ret = epoll_ctl(epollFd, op, fd, &event); + } + if (ret < 0) { + SEN_HILOGE("Epoll_ctl return %{public}d,epollFd_:%{public}d, op:%{public}d,fd:%{public}d,errno:%{public}d", + ret, epollFd, op, fd, errno); + } + return ret; +} + +int32_t StreamSocket::EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd) +{ + if (epollFd < 0) { + epollFd = epollFd_; + } + if (epollFd < 0) { + SEN_HILOGE("Invalid param epollFd"); + return ERROR; + } + auto ret = epoll_wait(epollFd, &events, maxevents, timeout); + if (ret < 0) { + SEN_HILOGE("Epoll_wait ret:%{public}d,errno:%{public}d", ret, errno); + } + return ret; +} + +void StreamSocket::OnReadPackets(CircleStreamBuffer &circBuf, StreamSocket::PacketCallBackFun callbackFun) +{ + constexpr int32_t headSize = static_cast(sizeof(PackHead)); + for (int32_t i = 0; i < PROTO_ONCE_PROCESS_NETPACKET_LIMIT; i++) { + const int32_t unreadSize = circBuf.UnreadSize(); + if (unreadSize < headSize) { + break; + } + int32_t dataSize = unreadSize - headSize; + char *buf = const_cast(circBuf.ReadBuf()); + CHKPB(buf); + PackHead *head = reinterpret_cast(buf); + CHKPB(head); + if (head->size < 0 || head->size > PROTO_MAX_PACKET_BUF_SIZE) { + SEN_HILOGE("Packet header parsing error, and this error cannot be recovered. The buffer will be reset." + " head->size:%{public}d, unreadSize:%{public}d", head->size, unreadSize); + circBuf.Reset(); + break; + } + if (head->size > dataSize) { + break; + } + NetPacket pkt(head->idMsg); + if ((head->size > 0) && (!pkt.Write(&buf[headSize], head->size))) { + SEN_HILOGW("Error writing data in the NetPacket. It will be retried next time. messageid:%{public}d," + "size:%{public}d", head->idMsg, head->size); + break; + } + if (!circBuf.SeekReadPos(pkt.GetPacketLength())) { + SEN_HILOGW("Set read position error, and this error cannot be recovered, and the buffer will be reset." + " packetSize:%{public}d unreadSize:%{public}d", pkt.GetPacketLength(), unreadSize); + circBuf.Reset(); + break; + } + callbackFun(pkt); + if (circBuf.IsEmpty()) { + circBuf.Reset(); + break; + } + } +} + +void StreamSocket::EpollClose() +{ + if (epollFd_ >= 0) { + close(epollFd_); + epollFd_ = -1; + } +} + +void StreamSocket::Close() +{ + if (fd_ >= 0) { + auto rf = close(fd_); + if (rf > 0) { + SEN_HILOGE("Socket close failed rf:%{public}d", rf); + } + } + fd_ = -1; +} + +int32_t StreamSocket::GetFd() const +{ + return fd_; +} +int32_t StreamSocket::GetEpollFd() const +{ + return epollFd_; +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file -- Gitee From c94884c817bfa41b1243564a428ac16f010db36d Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 2 Mar 2023 13:24:29 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I10817ab8aef3353e161723c8a5afb1918672a04a --- utils/ipc/include/proto.h | 47 ------- utils/ipc/include/stream_buffer.h | 171 ----------------------- utils/ipc/include/stream_client.h | 55 -------- utils/ipc/include/stream_session.h | 74 ---------- utils/ipc/include/stream_socket.h | 59 -------- utils/ipc/src/stream_buffer.cpp | 217 ----------------------------- utils/ipc/src/stream_client.cpp | 122 ---------------- utils/ipc/src/stream_session.cpp | 169 ---------------------- utils/ipc/src/stream_socket.cpp | 159 --------------------- 9 files changed, 1073 deletions(-) delete mode 100644 utils/ipc/include/proto.h delete mode 100644 utils/ipc/include/stream_buffer.h delete mode 100644 utils/ipc/include/stream_client.h delete mode 100644 utils/ipc/include/stream_session.h delete mode 100644 utils/ipc/include/stream_socket.h delete mode 100644 utils/ipc/src/stream_buffer.cpp delete mode 100644 utils/ipc/src/stream_client.cpp delete mode 100644 utils/ipc/src/stream_session.cpp delete mode 100644 utils/ipc/src/stream_socket.cpp diff --git a/utils/ipc/include/proto.h b/utils/ipc/include/proto.h deleted file mode 100644 index 207b8640..00000000 --- a/utils/ipc/include/proto.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2023 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 PROTO_H -#define PROTO_H - -#include - -namespace OHOS { -namespace Sensors { -#define PROTO_SEND_RETRY_LIMIT 32 -#define PROTO_SEND_RETRY_SLEEP_TIME 10000 - -static const int32_t PROTO_STREAM_BUF_READ_FAIL = 1; -static const int32_t PROTO_STREAM_BUF_WRITE_FAIL = 2; -static const int32_t PROTO_MEM_OUT_OF_BOUNDS = 3; -static const int32_t PROTO_MEMCPY_SEC_FUN_FAIL = 4; -static const int32_t PROTO_PARAM_INPUT_INVALID = 5; -static const int32_t PROTO_SESSION_NOT_FOUND = 8; -static const int32_t PROTO_MAX_VECTOR_SIZE = 10; -static const int32_t PROTO_ADD_SESSION_FAIL = 11; -static const int32_t PROTO_MAX_SESSON_ALARM = 12; -static const int32_t PROTO_MAX_RECV_LIMIT = 13; - -static const int32_t PROTO_MAX_STREAM_BUF_SIZE = 256; -static const int32_t PROTO_MAX_PACKET_BUF_SIZE = 256; -static const int32_t PROTO_ONCE_PROCESS_NETPACKET_LIMIT = 100; - -enum class MessageId : int32_t { - INVALID, - CLIENT_INFO, -}; -} // namespace Sensors -} // namespace OHOS -#endif // PROTO_H \ No newline at end of file diff --git a/utils/ipc/include/stream_buffer.h b/utils/ipc/include/stream_buffer.h deleted file mode 100644 index 0cea6fae..00000000 --- a/utils/ipc/include/stream_buffer.h +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (c) 2023 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 STREAM_BUFFER_H -#define STREAM_BUFFER_H - -#include -#include -#include - -#include "nocopyable.h" -#include "securec.h" - -#include "proto.h" -#include "sensors_errors.h" - -namespace OHOS { -namespace Sensors { -class StreamBuffer { - static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamBuffer" }; -public: - StreamBuffer() = default; - explicit StreamBuffer(const StreamBuffer &buf); - virtual StreamBuffer &operator=(const StreamBuffer &other); - virtual ~StreamBuffer() = default; - void Reset(); - void Clean(); - bool SeekReadPos(int32_t n); - bool Read(std::string &buf); - bool Read(StreamBuffer &buf); - bool Read(char *buf, size_t size); - bool Write(const std::string &buf); - bool Write(const StreamBuffer &buf); - virtual bool Write(const char *buf, size_t size); - bool IsEmpty() const; - bool ChkRWError() const; - size_t Size() const; - int32_t UnreadSize() const; - int32_t GetAvailableBufSize() const; - const std::string& GetErrorStatusRemark() const; - const char* Data() const; - - template - bool Read(T &data); - template - bool Write(const T &data); - template - bool Read(std::vector &data); - template - bool Write(const std::vector &data); - const char *ReadBuf() const; - const char *WriteBuf() const; - template - StreamBuffer &operator >> (T &data); - template - StreamBuffer &operator << (const T &data); - DISALLOW_MOVE(StreamBuffer); - -protected: - bool Clone(const StreamBuffer &buf); - enum class ErrorStatus { - ERROR_STATUS_OK, - ERROR_STATUS_READ, - ERROR_STATUS_WRITE, - }; - ErrorStatus rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK; - int32_t rCount_ { 0 }; - int32_t wCount_ { 0 }; - int32_t rPos_ { 0 }; - int32_t wPos_ { 0 }; - char szBuff_[PROTO_MAX_STREAM_BUF_SIZE + 1] = {}; -}; - -template -bool StreamBuffer::Read(T &data) -{ - if (!Read(reinterpret_cast(&data), sizeof(data))) { - SEN_HILOGE("[%{public}s] size:%{public}zu count:%{public}d,errCode:%{public}d", - GetErrorStatusRemark().c_str(), sizeof(data), rCount_ + 1, PROTO_STREAM_BUF_READ_FAIL); - return false; - } - return true; -} - -template -bool StreamBuffer::Write(const T &data) -{ - if (!Write(reinterpret_cast(&data), sizeof(data))) { - SEN_HILOGE("[%{public}s] size:%{public}zu,count:%{public}d,errCode:%{public}d", - GetErrorStatusRemark().c_str(), sizeof(data), wCount_ + 1, PROTO_STREAM_BUF_WRITE_FAIL); - return false; - } - return true; -} - -template -bool StreamBuffer::Read(std::vector &data) -{ - int32_t size = 0; - if (!Read(size)) { - SEN_HILOGE("Read vector size error"); - return false; - } - if (size < 0 || size > PROTO_MAX_VECTOR_SIZE) { - SEN_HILOGE("Read vector size:%{public}d error", size); - return false; - } - for (int32_t i = 0; i < size; i++) { - T val; - if (!Read(val)) { - SEN_HILOGE("Read vector data error"); - return false; - } - data.push_back(val); - } - return true; -} - -template -bool StreamBuffer::Write(const std::vector &data) -{ - if (data.size() > INT32_MAX) { - SEN_HILOGE("Vector exceeds the max range"); - return false; - } - int32_t size = static_cast(data.size()); - if (!Write(size)) { - SEN_HILOGE("Write vector size error"); - return false; - } - for (const auto &item : data) { - if (!Write(item)) { - SEN_HILOGE("Write vector data error"); - return false; - } - } - return true; -} - -template -StreamBuffer &StreamBuffer::operator>>(T &data) -{ - if (!Read(data)) { - SEN_HILOGW("Read data failed"); - } - return *this; -} - -template -StreamBuffer &StreamBuffer::operator<<(const T &data) -{ - if (!Write(data)) { - SEN_HILOGW("Write data failed"); - } - return *this; -} -} // namespace Sensors -} // namespace OHOS -#endif // STREAM_BUFFER_H \ No newline at end of file diff --git a/utils/ipc/include/stream_client.h b/utils/ipc/include/stream_client.h deleted file mode 100644 index 4dd9a1e9..00000000 --- a/utils/ipc/include/stream_client.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023 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 STREAM_CLIENT_H -#define STREAM_CLIENT_H - -#include -#include -#include - -#include "nocopyable.h" - -#include "net_packet.h" -#include "stream_socket.h" - -namespace OHOS { -namespace Sensors { -class StreamClient; -using MsgClientFunCallback = std::function; -class StreamClient : public StreamSocket { -public: - StreamClient(); - virtual ~StreamClient(); - virtual int32_t Socket() = 0; - virtual void Stop(); - int32_t ConnectTo(); - bool SendMsg(const char *buf, size_t size) const; - bool SendMsg(const NetPacket &pkt) const; - bool GetConnectedStatus() const; - DISALLOW_COPY_AND_MOVE(StreamClient); - -protected: - virtual void OnConnected() {} - virtual void OnDisconnected() {} - bool StartClient(MsgClientFunCallback fun); - bool isExit { false }; - bool isRunning_ { false }; - bool isConnected_ { false }; - MsgClientFunCallback recvFun_; -}; -} // namespace Sensors -} // namespace OHOS -#endif // STREAM_CLIENT_H \ No newline at end of file diff --git a/utils/ipc/include/stream_session.h b/utils/ipc/include/stream_session.h deleted file mode 100644 index adbf4dc1..00000000 --- a/utils/ipc/include/stream_session.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2023 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 STREAM_SESSION_H -#define STREAM_SESSION_H - -#include -#include -#include - -#include -#include - -#include "accesstoken_kit.h" -#include "nocopyable.h" - -#include "net_packet.h" -#include "proto.h" - -namespace OHOS { -namespace Sensors { -class StreamSession; -using SessionPtr = std::shared_ptr; -using namespace Security::AccessToken; -class StreamSession : public std::enable_shared_from_this { -public: - StreamSession(const std::string &programName, const int32_t moduleType, const int32_t fd, const int32_t uid, - const int32_t pid); - virtual ~StreamSession() = default; - bool SendMsg(const char *buf, size_t size) const; - bool SendMsg(NetPacket &pkt) const; - void Close(); - int32_t GetUid() const; - int32_t GetPid() const; - int32_t GetModuleType() const; - SessionPtr GetSharedPtr(); - int32_t GetFd() const; - const std::string& GetDescript() const; - const std::string GetProgramName() const; - void SetTokenType(int32_t type); - int32_t GetTokenType() const; - void UpdateDescript(); - DISALLOW_COPY_AND_MOVE(StreamSession); - -protected: - struct EventTime { - int32_t id { 0 }; - int64_t eventTime { 0 }; - int32_t timerId { -1 }; - }; - std::map> events_; - std::string descript_; - const std::string programName_; - const int32_t moduleType_ { -1 }; - int32_t fd_ { -1 }; - const int32_t uid_ { -1 }; - const int32_t pid_ { -1 }; - int32_t tokenType_ { ATokenTypeEnum::TOKEN_INVALID }; -}; -} // namespace Sensors -} // namespace OHOS -#endif // STREAM_SESSION_H \ No newline at end of file diff --git a/utils/ipc/include/stream_socket.h b/utils/ipc/include/stream_socket.h deleted file mode 100644 index acae2006..00000000 --- a/utils/ipc/include/stream_socket.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2023 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 STREAM_SOCKET_H -#define STREAM_SOCKET_H - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "nocopyable.h" - -#include "circle_stream_buffer.h" -#include "net_packet.h" - -namespace OHOS { -namespace Sensors { -class StreamSocket { -public: - using PacketCallBackFun = std::function; - StreamSocket(); - virtual ~StreamSocket(); - int32_t EpollCreate(int32_t size); - int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd = -1); - int32_t EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd = -1); - void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun); - void EpollClose(); - void Close(); - int32_t GetFd() const; - int32_t GetEpollFd() const; - - DISALLOW_COPY_AND_MOVE(StreamSocket); - -protected: - int32_t fd_ { -1 }; - int32_t epollFd_ { -1 }; -}; -} // namespace Sensors -} // namespace OHOS -#endif // STREAM_SOCKET_H \ No newline at end of file diff --git a/utils/ipc/src/stream_buffer.cpp b/utils/ipc/src/stream_buffer.cpp deleted file mode 100644 index a586ef72..00000000 --- a/utils/ipc/src/stream_buffer.cpp +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (c) 2023 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 "stream_buffer.h" - -#include -#include - -namespace OHOS { -namespace Sensors { -StreamBuffer::StreamBuffer(const StreamBuffer &buf) -{ - Clone(buf); -} - -StreamBuffer &StreamBuffer::operator=(const StreamBuffer &other) -{ - Clone(other); - return *this; -} - -void StreamBuffer::Reset() -{ - rPos_ = 0; - wPos_ = 0; - rCount_ = 0; - wCount_ = 0; - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK; -} - -void StreamBuffer::Clean() -{ - Reset(); - errno_t ret = memset_sp(&szBuff_, sizeof(szBuff_), 0, sizeof(szBuff_)); - if (ret != EOK) { - SEN_HILOGE("Call memset_s fail"); - return; - } -} - -bool StreamBuffer::SeekReadPos(int32_t n) -{ - int32_t pos = rPos_ + n; - if (pos < 0 || pos > wPos_) { - SEN_HILOGE("The position in the calculation is not as expected. pos:%{public}d [0, %{public}d]", pos, wPos_); - return false; - } - rPos_ = pos; - return true; -} - -bool StreamBuffer::Read(std::string &buf) -{ - if (rPos_ == wPos_) { - SEN_HILOGE("Not enough memory to read, errCode:%{public}d", PROTO_STREAM_BUF_READ_FAIL); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; - return false; - } - buf = ReadBuf(); - rPos_ += static_cast(buf.length()) + 1; - return (buf.length() > 0); -} - -bool StreamBuffer::Write(const std::string &buf) -{ - return Write(buf.c_str(), buf.length()+1); -} - -bool StreamBuffer::Read(StreamBuffer &buf) -{ - return buf.Write(Data(), Size()); -} - -bool StreamBuffer::Write(const StreamBuffer &buf) -{ - return Write(buf.Data(), buf.Size()); -} - -bool StreamBuffer::Read(char *buf, size_t size) -{ - if (ChkRWError()) { - return false; - } - if (buf == nullptr) { - SEN_HILOGE("Invalid input parameter buf=nullptr errCode:%{public}d", PROTO_PARAM_INPUT_INVALID); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; - return false; - } - if (size == 0) { - SEN_HILOGE("Invalid input parameter size=%{public}zu errCode:%{public}d", size, PROTO_PARAM_INPUT_INVALID); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; - return false; - } - if (rPos_ + static_cast(size) > wPos_) { - SEN_HILOGE("Memory out of bounds on read... errCode:%{public}d", PROTO_MEM_OUT_OF_BOUNDS); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; - return false; - } - errno_t ret = memcpy_sp(buf, size, ReadBuf(), size); - if (ret != EOK) { - SEN_HILOGE("Failed to call memcpy_sp. errCode:%{public}d", PROTO_MEMCPY_SEC_FUN_FAIL); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ; - return false; - } - rPos_ += static_cast(size); - rCount_ += 1; - return true; -} - -bool StreamBuffer::Write(const char *buf, size_t size) -{ - if (ChkRWError()) { - return false; - } - if (buf == nullptr) { - SEN_HILOGE("Invalid input parameter buf=nullptr errCode:%{public}d", PROTO_PARAM_INPUT_INVALID); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; - return false; - } - if (size == 0) { - SEN_HILOGE("Invalid input parameter size=%{public}zu errCode:%{public}d", size, PROTO_PARAM_INPUT_INVALID); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; - return false; - } - if (wPos_ + static_cast(size) > PROTO_MAX_STREAM_BUF_SIZE) { - SEN_HILOGE("The write length exceeds buffer. wIdx:%{public}d size:%{public}zu maxBufSize:%{public}d" - "errCode:%{public}d", wPos_, size, PROTO_MAX_STREAM_BUF_SIZE, PROTO_MEM_OUT_OF_BOUNDS); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; - return false; - } - errno_t ret = memcpy_sp(&szBuff_[wPos_], GetAvailableBufSize(), buf, size); - if (ret != EOK) { - SEN_HILOGE("Failed to call memcpy_sp. errCode:%{public}d", PROTO_MEMCPY_SEC_FUN_FAIL); - rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE; - return false; - } - wPos_ += static_cast(size); - wCount_ += 1; - return true; -} - -bool StreamBuffer::IsEmpty() const -{ - return (rPos_ == wPos_); -} - -size_t StreamBuffer::Size() const -{ - return static_cast(wPos_); -} - -int32_t StreamBuffer::UnreadSize() const -{ - return ((wPos_ <= rPos_) ? 0 : (wPos_ - rPos_)); -} - -int32_t StreamBuffer::GetAvailableBufSize() const -{ - return ((wPos_ >= PROTO_MAX_STREAM_BUF_SIZE) ? 0 : (PROTO_MAX_STREAM_BUF_SIZE - wPos_)); -} - -bool StreamBuffer::ChkRWError() const -{ - return (rwErrorStatus_ != ErrorStatus::ERROR_STATUS_OK); -} - -const std::string &StreamBuffer::GetErrorStatusRemark() const -{ - static const std::vector> remark { - {ErrorStatus::ERROR_STATUS_OK, "OK"}, - {ErrorStatus::ERROR_STATUS_READ, "READ_ERROR"}, - {ErrorStatus::ERROR_STATUS_WRITE, "WRITE_ERROR"}, - }; - static const std::string invalidStatus { "UNKNOWN" }; - - auto tIter = std::find_if(remark.cbegin(), remark.cend(), - [this](const auto &item) { - return (item.first == rwErrorStatus_); - } - ); - return (tIter != remark.cend() ? tIter->second : invalidStatus); -} - -const char *StreamBuffer::Data() const -{ - return &szBuff_[0]; -} - -const char *StreamBuffer::ReadBuf() const -{ - return &szBuff_[rPos_]; -} - -const char *StreamBuffer::WriteBuf() const -{ - return &szBuff_[wPos_]; -} - -bool StreamBuffer::Clone(const StreamBuffer &buf) -{ - Clean(); - return Write(buf.Data(), buf.Size()); -} -} // namespace Sensors -} // namespace OHOS diff --git a/utils/ipc/src/stream_client.cpp b/utils/ipc/src/stream_client.cpp deleted file mode 100644 index 4e1fedf9..00000000 --- a/utils/ipc/src/stream_client.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2023 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 "stream_client.h" - -#include "sensors_errors.h" - -namespace OHOS { -namespace Sensors { -namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamClient" }; -} - -StreamClient::StreamClient() {} - -StreamClient::~StreamClient() {} - -int32_t StreamClient::ConnectTo() -{ - CALL_LOG_ENTER; - if (Socket() < 0) { - SEN_HILOGE("Socket failed"); - return ERROR; - } - OnConnected(); - return SUCCESS; -} - -bool StreamClient::SendMsg(const char *buf, size_t size) const -{ - CALL_LOG_ENTER; - CHKPF(buf); - if ((size == 0) || (size > PROTO_MAX_PACKET_BUF_SIZE)) { - SEN_HILOGE("Stream buffer size out of range"); - return false; - } - if (fd_ < 0) { - SEN_HILOGE("The fd_ is less than 0"); - return false; - } - int32_t idx = 0; - int32_t retryCount = 0; - const int32_t bufSize = static_cast(size); - int32_t remSize = bufSize; - while (remSize > 0 && retryCount < PROTO_SEND_RETRY_LIMIT) { - retryCount += 1; - auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL); - if (count < 0) { - if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { - SEN_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno); - continue; - } - SEN_HILOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_); - return false; - } - idx += count; - remSize -= count; - if (remSize > 0) { - usleep(PROTO_SEND_RETRY_SLEEP_TIME); - } - } - if (retryCount >= PROTO_SEND_RETRY_LIMIT || remSize != 0) { - SEN_HILOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d", - retryCount, PROTO_SEND_RETRY_LIMIT, idx, bufSize, fd_); - return false; - } - return true; -} - -bool StreamClient::SendMsg(const NetPacket &pkt) const -{ - CALL_LOG_ENTER; - if (pkt.ChkRWError()) { - SEN_HILOGE("Read and write status is error"); - return false; - } - StreamBuffer buf; - pkt.MakeData(buf); - return SendMsg(buf.Data(), buf.Size()); -} - -bool StreamClient::StartClient(MsgClientFunCallback fun) -{ - CALL_LOG_ENTER; - if (isRunning_ || isConnected_) { - SEN_HILOGE("Client is connected or started"); - return false; - } - isExit = false; - recvFun_ = fun; - if (ConnectTo() < 0) { - SEN_HILOGW("Client connection failed, Try again later"); - } - return true; -} - -void StreamClient::Stop() -{ - CALL_LOG_ENTER; - isExit = true; - isRunning_ = false; - Close(); -} - -bool StreamClient::GetConnectedStatus() const -{ - return isConnected_; -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/stream_session.cpp b/utils/ipc/src/stream_session.cpp deleted file mode 100644 index dd837974..00000000 --- a/utils/ipc/src/stream_session.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2023 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 "stream_session.h" - -#include -#include - -#include -#include -#include -#include - -#include "proto.h" -#include "stream_socket.h" -#include "sensors_errors.h" - -namespace OHOS { -namespace Sensors { -namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamSession" }; -} - -StreamSession::StreamSession(const std::string &programName, const int32_t moduleType, const int32_t fd, - const int32_t uid, const int32_t pid) - : programName_(programName), - moduleType_(moduleType), - fd_(fd), - uid_(uid), - pid_(pid) -{ - UpdateDescript(); -} - -bool StreamSession::SendMsg(const char *buf, size_t size) const -{ - CHKPF(buf); - if ((size == 0) || (size > PROTO_MAX_PACKET_BUF_SIZE)) { - SEN_HILOGE("Buf size:%{public}zu", size); - return false; - } - if (fd_ < 0) { - SEN_HILOGE("The fd_ is less than 0"); - return false; - } - - int32_t idx = 0; - int32_t retryCount = 0; - const int32_t bufSize = static_cast(size); - int32_t remSize = bufSize; - while (remSize > 0 && retryCount < PROTO_SEND_RETRY_LIMIT) { - retryCount += 1; - auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL); - if (count < 0) { - if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) { - usleep(PROTO_SEND_RETRY_SLEEP_TIME); - SEN_HILOGW("Continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno); - continue; - } - SEN_HILOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_); - return false; - } - idx += count; - remSize -= count; - if (remSize > 0) { - usleep(PROTO_SEND_RETRY_SLEEP_TIME); - } - } - if (retryCount >= PROTO_SEND_RETRY_LIMIT || remSize != 0) { - SEN_HILOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d", - retryCount, PROTO_SEND_RETRY_LIMIT, idx, bufSize, fd_); - return false; - } - return true; -} - -void StreamSession::Close() -{ - SEN_HILOGD("Enter fd_:%{public}d.", fd_); - if (fd_ >= 0) { - close(fd_); - fd_ = -1; - UpdateDescript(); - } -} - -void StreamSession::UpdateDescript() -{ - std::ostringstream oss; - oss << "fd = " << fd_ - << ", programName = " << programName_ - << ", moduleType = " << moduleType_ - << ((fd_ < 0) ? ", closed" : ", opened") - << ", uid = " << uid_ - << ", pid = " << pid_ - << ", tokenType = " << tokenType_ - << std::endl; - descript_ = oss.str().c_str(); -} - -bool StreamSession::SendMsg(NetPacket &pkt) const -{ - if (pkt.ChkRWError()) { - SEN_HILOGE("Read and write status is error"); - return false; - } - StreamBuffer buf; - pkt.MakeData(buf); - return SendMsg(buf.Data(), buf.Size()); -} - -int32_t StreamSession::GetUid() const -{ - return uid_; -} - -int32_t StreamSession::GetPid() const -{ - return pid_; -} - -int32_t StreamSession::GetModuleType() const -{ - return moduleType_; -} - -SessionPtr StreamSession::GetSharedPtr() -{ - return shared_from_this(); -} - -int32_t StreamSession::GetFd() const -{ - return fd_; -} - -const std::string& StreamSession::GetDescript() const -{ - return descript_; -} - -const std::string StreamSession::GetProgramName() const -{ - return programName_; -} - -void StreamSession::SetTokenType(int32_t type) -{ - tokenType_ = type; -} - -int32_t StreamSession::GetTokenType() const -{ - return tokenType_; -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/stream_socket.cpp b/utils/ipc/src/stream_socket.cpp deleted file mode 100644 index 1a2044df..00000000 --- a/utils/ipc/src/stream_socket.cpp +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2023 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 "stream_socket.h" - -#include - -#include "sensors_errors.h" - -namespace OHOS { -namespace Sensors { -namespace { -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamSocket" }; -} // namespace - -StreamSocket::StreamSocket() {} - -StreamSocket::~StreamSocket() -{ - Close(); - EpollClose(); -} - -int32_t StreamSocket::EpollCreate(int32_t size) -{ - epollFd_ = epoll_create(size); - if (epollFd_ < 0) { - SEN_HILOGE("Epoll_create return %{public}d", epollFd_); - } else { - SEN_HILOGI("Epoll_create, epollFd_:%{public}d", epollFd_); - } - return epollFd_; -} - -int32_t StreamSocket::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event, int32_t epollFd) -{ - if (fd < 0) { - SEN_HILOGE("Invalid fd"); - return ERROR; - } - if (epollFd < 0) { - epollFd = epollFd_; - } - if (epollFd < 0) { - SEN_HILOGE("Invalid param epollFd"); - return ERROR; - } - int32_t ret; - if (op == EPOLL_CTL_DEL) { - ret = epoll_ctl(epollFd, op, fd, NULL); - } else { - ret = epoll_ctl(epollFd, op, fd, &event); - } - if (ret < 0) { - SEN_HILOGE("Epoll_ctl return %{public}d,epollFd_:%{public}d, op:%{public}d,fd:%{public}d,errno:%{public}d", - ret, epollFd, op, fd, errno); - } - return ret; -} - -int32_t StreamSocket::EpollWait(struct epoll_event &events, int32_t maxevents, int32_t timeout, int32_t epollFd) -{ - if (epollFd < 0) { - epollFd = epollFd_; - } - if (epollFd < 0) { - SEN_HILOGE("Invalid param epollFd"); - return ERROR; - } - auto ret = epoll_wait(epollFd, &events, maxevents, timeout); - if (ret < 0) { - SEN_HILOGE("Epoll_wait ret:%{public}d,errno:%{public}d", ret, errno); - } - return ret; -} - -void StreamSocket::OnReadPackets(CircleStreamBuffer &circBuf, StreamSocket::PacketCallBackFun callbackFun) -{ - constexpr int32_t headSize = static_cast(sizeof(PackHead)); - for (int32_t i = 0; i < PROTO_ONCE_PROCESS_NETPACKET_LIMIT; i++) { - const int32_t unreadSize = circBuf.UnreadSize(); - if (unreadSize < headSize) { - break; - } - int32_t dataSize = unreadSize - headSize; - char *buf = const_cast(circBuf.ReadBuf()); - CHKPB(buf); - PackHead *head = reinterpret_cast(buf); - CHKPB(head); - if (head->size < 0 || head->size > PROTO_MAX_PACKET_BUF_SIZE) { - SEN_HILOGE("Packet header parsing error, and this error cannot be recovered. The buffer will be reset." - " head->size:%{public}d, unreadSize:%{public}d", head->size, unreadSize); - circBuf.Reset(); - break; - } - if (head->size > dataSize) { - break; - } - NetPacket pkt(head->idMsg); - if ((head->size > 0) && (!pkt.Write(&buf[headSize], head->size))) { - SEN_HILOGW("Error writing data in the NetPacket. It will be retried next time. messageid:%{public}d," - "size:%{public}d", head->idMsg, head->size); - break; - } - if (!circBuf.SeekReadPos(pkt.GetPacketLength())) { - SEN_HILOGW("Set read position error, and this error cannot be recovered, and the buffer will be reset." - " packetSize:%{public}d unreadSize:%{public}d", pkt.GetPacketLength(), unreadSize); - circBuf.Reset(); - break; - } - callbackFun(pkt); - if (circBuf.IsEmpty()) { - circBuf.Reset(); - break; - } - } -} - -void StreamSocket::EpollClose() -{ - if (epollFd_ >= 0) { - close(epollFd_); - epollFd_ = -1; - } -} - -void StreamSocket::Close() -{ - if (fd_ >= 0) { - auto rf = close(fd_); - if (rf > 0) { - SEN_HILOGE("Socket close failed rf:%{public}d", rf); - } - } - fd_ = -1; -} - -int32_t StreamSocket::GetFd() const -{ - return fd_; -} -int32_t StreamSocket::GetEpollFd() const -{ - return epollFd_; -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file -- Gitee From 9d4b939c3745b577b8cd324006fb5a9c17ab6bd9 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 3 Mar 2023 03:19:32 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9gn=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Id9de174955fcd6a0d513cbc753ad3b9c1029907e --- interfaces/plugin/BUILD.gn | 2 +- utils/common/BUILD.gn | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index cd864d86..4c3db194 100644 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -21,7 +21,7 @@ ohos_shared_library("libsensor") { "//third_party/libuv/include", "//commonlibrary/c_utils/base/include", "./include", - "//base/sensors/sensor/utils/common/include", + "./../../utils/common/include", "//foundation/arkui/napi/interfaces/inner_api/napi", ] defines = [ diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index c5b02656..a9a3d9a7 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -13,8 +13,6 @@ import("//build/ohos.gni") -SUBSYSTEM_DIR = "//base/sensors" - ohos_shared_library("libsensor_utils") { sources = [ "src/app_sensor.cpp", @@ -28,10 +26,7 @@ ohos_shared_library("libsensor_utils") { include_dirs = [ "include", - "//commonlibrary/c_utils/base/include", - "//utils/system/safwk/native/include", - "//drivers/peripheral/sensor/interfaces/include", - "$SUBSYSTEM_DIR/sensor/interfaces/native/include", + "./../../interfaces/native/include", ] external_deps = [ -- Gitee From 91a94a3fb420220dd83b689e477a714d1a11fe04 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 4 Mar 2023 08:10:36 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Icaad4802d037dd6eda7a9785a7c6f48c6f15abfe --- .../sensor/src/sensor_service_proxy.cpp | 20 +++---- interfaces/native/include/sensor_agent_type.h | 10 +++- test/unittest/common/include/system_info.h | 1 + utils/common/BUILD.gn | 2 +- utils/common/include/sensors_errors.h | 6 +-- .../{app_sensor.h => subscribe_info.h} | 39 ++++++-------- .../{app_sensor.cpp => subscribe_info.cpp} | 54 +++++++++---------- utils/ipc/src/circle_stream_buffer.cpp | 2 +- utils/ipc/src/net_packet.cpp | 11 ++-- 9 files changed, 73 insertions(+), 72 deletions(-) rename utils/common/include/{app_sensor.h => subscribe_info.h} (58%) rename utils/common/src/{app_sensor.cpp => subscribe_info.cpp} (60%) diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 5ed9c4b2..4cd49108 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -46,19 +46,19 @@ ErrCode SensorServiceProxy::EnableSensor(int32_t sensorId, int64_t samplingPerio MessageOption option; if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } if (!data.WriteInt32(sensorId)) { SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } if (!data.WriteInt64(samplingPeriodNs)) { SEN_HILOGE("write samplingPeriodNs failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } if (!data.WriteInt64(maxReportDelayNs)) { SEN_HILOGE("write maxReportDelayNs failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } sptr remote = Remote(); CHKPR(remote, ERROR); @@ -78,11 +78,11 @@ ErrCode SensorServiceProxy::DisableSensor(int32_t sensorId) MessageOption option; if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } if (!data.WriteInt32(sensorId)) { SEN_HILOGE("write sensorId failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } sptr remote = Remote(); CHKPR(remote, ERROR); @@ -147,12 +147,12 @@ ErrCode SensorServiceProxy::TransferDataChannel(const sptrSendToBinder(data); if (!data.WriteRemoteObject(sensorClient)) { SEN_HILOGE("sensorClient failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } sptr remote = Remote(); CHKPR(remote, ERROR); @@ -174,11 +174,11 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien MessageOption option; if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { SEN_HILOGE("write descriptor failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } if (!data.WriteRemoteObject(sensorClient)) { SEN_HILOGE("write sensorClient failed"); - return WRITE_MSG_ERR; + return WRITE_PARCEL_ERR; } sptr remote = Remote(); CHKPR(remote, ERROR); diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 37b0de0e..99c4d295 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -395,6 +395,14 @@ typedef struct WearDetectionData { float value; } WearDetectionData; +typedef struct SubscribeSensorInfo { + int32_t pid = -1; /**< PID */ + int32_t sensorId = -1; /**< Sensor ID */ + bool isActive = false; /**< Whether the process enable the sensor */ + int64_t samplingPeriodNs = -1; /**< Sample period, in ns */ + int64_t maxReportDelayNs = -1; /**< Maximum Report Delay, in ns */ +} SubscribeSensorInfo; + #ifdef __cplusplus #if __cplusplus } diff --git a/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h index 76ad756a..0a598969 100644 --- a/test/unittest/common/include/system_info.h +++ b/test/unittest/common/include/system_info.h @@ -37,6 +37,7 @@ public: double GetSystemCpuUsage(); double GetProcCpuUsage(const std::string& process_name); int32_t GetTaskPidFile(const std::string& process_name); + private: struct Total_Cpu_Occupy { char name[20] { 0 }; diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index a9a3d9a7..5e1ef88f 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -15,13 +15,13 @@ import("//build/ohos.gni") ohos_shared_library("libsensor_utils") { sources = [ - "src/app_sensor.cpp", "src/permission_util.cpp", "src/report_data_callback.cpp", "src/sensor.cpp", "src/sensor_basic_data_channel.cpp", "src/sensor_basic_info.cpp", "src/sensor_channel_info.cpp", + "src/subscribe_info.cpp", ] include_dirs = [ diff --git a/utils/common/include/sensors_errors.h b/utils/common/include/sensors_errors.h index 35335518..43edceab 100644 --- a/utils/common/include/sensors_errors.h +++ b/utils/common/include/sensors_errors.h @@ -87,9 +87,9 @@ enum { COPY_ERR = NO_EVENT + 1, REGIST_PERMISSION_CHANGED_ERR = COPY_ERR + 1, DUMP_PARAM_ERR = REGIST_PERMISSION_CHANGED_ERR + 1, - WRITE_MSG_ERR = DUMP_PARAM_ERR + 1, - READ_MSG_ERR = WRITE_MSG_ERR + 1, - SET_SENSOR_MODE_ERR = READ_MSG_ERR + 1, + WRITE_PARCEL_ERR = DUMP_PARAM_ERR + 1, + READ_PARCEL_ERR = WRITE_PARCEL_ERR + 1, + SET_SENSOR_MODE_ERR = READ_PARCEL_ERR + 1, SET_SENSOR_OPTION_ERR = SET_SENSOR_MODE_ERR + 1, REGIST_CALLBACK_ERR = SET_SENSOR_OPTION_ERR + 1, }; diff --git a/utils/common/include/app_sensor.h b/utils/common/include/subscribe_info.h similarity index 58% rename from utils/common/include/app_sensor.h rename to utils/common/include/subscribe_info.h index 18558e94..fbcd41b1 100644 --- a/utils/common/include/app_sensor.h +++ b/utils/common/include/subscribe_info.h @@ -13,29 +13,20 @@ * limitations under the License. */ -#ifndef APP_SENSOR_H -#define APP_SENSOR_H +#ifndef SUBSCRIBE_INFO_H +#define SUBSCRIBE_INFO_H #include "parcel.h" +#include "sensor_agent_type.h" + namespace OHOS { namespace Sensors { -struct SensorStatus { - int32_t sensorId {-1}; - bool isActive {false}; - int64_t samplingPeriodNs {-1}; - int64_t maxReportDelayNs {-1}; - SensorStatus() {} - SensorStatus(int32_t sensorId, bool isActive, int64_t samplingPeriodNs, int64_t maxReportDelayNs) - : sensorId(sensorId), isActive(isActive), samplingPeriodNs(samplingPeriodNs), - maxReportDelayNs(maxReportDelayNs) {} -}; - -class AppSensor : public Parcelable { +class SubscribeInfo : public Parcelable { public: - AppSensor() = default; - AppSensor(int32_t pid, SensorStatus sensorStatus); - virtual ~AppSensor() = default; + SubscribeInfo() = default; + SubscribeInfo(SubscribeSensorInfo subscribeSensorInfo); + virtual ~SubscribeInfo() = default; int32_t GetPid() const; void SetPid(int32_t pid); int32_t GetSensorId() const; @@ -47,15 +38,15 @@ public: int64_t GetMaxReportDelayNs() const; void SetMaxReportDelayNs(int64_t maxReportDelayNs); bool Marshalling(Parcel &parcel) const; - std::unique_ptr Unmarshalling(Parcel &parcel); + std::unique_ptr Unmarshalling(Parcel &parcel); private: - int32_t pid_ {-1}; - int32_t sensorId_ {-1}; - bool isActive_ {-1}; - int64_t samplingPeriodNs_ {-1}; - int64_t maxReportDelayNs_ {-1}; + int32_t pid_ { -1 }; + int32_t sensorId_ { -1 }; + bool isActive_ { -1 }; + int64_t samplingPeriodNs_ { -1 }; + int64_t maxReportDelayNs_ { -1 }; }; } // namespace Sensors } // namespace OHOS -#endif // APP_SENSOR_H \ No newline at end of file +#endif // SUBSCRIBE_INFO_H \ No newline at end of file diff --git a/utils/common/src/app_sensor.cpp b/utils/common/src/subscribe_info.cpp similarity index 60% rename from utils/common/src/app_sensor.cpp rename to utils/common/src/subscribe_info.cpp index 18668359..18c8605c 100644 --- a/utils/common/src/app_sensor.cpp +++ b/utils/common/src/subscribe_info.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "app_sensor.h" +#include "subscribe_info.h" #include "sensors_errors.h" @@ -21,69 +21,69 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "AppSensor" }; +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SubscribeInfo" }; } -AppSensor::AppSensor(int32_t pid, SensorStatus sensorStatus) +SubscribeInfo::SubscribeInfo(SubscribeSensorInfo subscribeSensorInfo) { - pid_ = pid; - sensorId_ = sensorStatus.sensorId; - isActive_ = sensorStatus.isActive; - samplingPeriodNs_ = sensorStatus.samplingPeriodNs; - maxReportDelayNs_ = sensorStatus.maxReportDelayNs; + pid_ = subscribeSensorInfo.pid; + sensorId_ = subscribeSensorInfo.sensorId; + isActive_ = subscribeSensorInfo.isActive; + samplingPeriodNs_ = subscribeSensorInfo.samplingPeriodNs; + maxReportDelayNs_ = subscribeSensorInfo.maxReportDelayNs; } -int32_t AppSensor::GetPid() const +int32_t SubscribeInfo::GetPid() const { return pid_; } -void AppSensor::SetPid(int32_t pid) +void SubscribeInfo::SetPid(int32_t pid) { pid_ = pid; } -int32_t AppSensor::GetSensorId() const +int32_t SubscribeInfo::GetSensorId() const { return sensorId_; } -void AppSensor::SetSensorId(int32_t sensorId) +void SubscribeInfo::SetSensorId(int32_t sensorId) { sensorId_ = sensorId; } -bool AppSensor::IsActive() const +bool SubscribeInfo::IsActive() const { return isActive_; } -void AppSensor::Enable(bool isActive) +void SubscribeInfo::Enable(bool isActive) { isActive_ = isActive; } -int64_t AppSensor::GetSamplingPeriodNs() const +int64_t SubscribeInfo::GetSamplingPeriodNs() const { return samplingPeriodNs_; } -void AppSensor::SetSamplingPeriodNs(int64_t samplingPeriodNs) +void SubscribeInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs) { samplingPeriodNs_ = samplingPeriodNs; } -int64_t AppSensor::GetMaxReportDelayNs() const +int64_t SubscribeInfo::GetMaxReportDelayNs() const { return maxReportDelayNs_; } -void AppSensor::SetMaxReportDelayNs(int64_t maxReportDelayNs) +void SubscribeInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs) { maxReportDelayNs_ = maxReportDelayNs; } -bool AppSensor::Marshalling(Parcel &parcel) const +bool SubscribeInfo::Marshalling(Parcel &parcel) const { if (!parcel.WriteInt32(pid_)) { SEN_HILOGE("Write pid failed"); @@ -108,7 +108,7 @@ bool AppSensor::Marshalling(Parcel &parcel) const return true; } -std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) +std::unique_ptr SubscribeInfo::Unmarshalling(Parcel &parcel) { int32_t pid = -1; int32_t sensorId = -1; @@ -120,13 +120,13 @@ std::unique_ptr AppSensor::Unmarshalling(Parcel &parcel) SEN_HILOGE("ReadFromParcel is failed"); return nullptr; } - auto appSensor = std::make_unique(); - appSensor->SetPid(pid); - appSensor->SetSensorId(sensorId); - appSensor->Enable(isActive); - appSensor->SetSamplingPeriodNs(samplingPeriodNs); - appSensor->SetMaxReportDelayNs(maxReportDelayNs); - return appSensor; + auto subscribeInfo = std::make_unique(); + subscribeInfo->SetPid(pid); + subscribeInfo->SetSensorId(sensorId); + subscribeInfo->Enable(isActive); + subscribeInfo->SetSamplingPeriodNs(samplingPeriodNs); + subscribeInfo->SetMaxReportDelayNs(maxReportDelayNs); + return subscribeInfo; } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/utils/ipc/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp index a2c91f54..1f1845b2 100644 --- a/utils/ipc/src/circle_stream_buffer.cpp +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -44,7 +44,7 @@ bool CircleStreamBuffer::Write(const char *buf, size_t size) void CircleStreamBuffer::CopyDataToBegin() { int32_t unreadSize = UnreadSize(); - if (unreadSize > 0 && rPos_ > 0) { + if (unreadSize > 0 && rPos_ > 0 && sizeof(szBuff_) > wPos_) { int32_t pos = 0; for (int32_t i = rPos_; i <= wPos_;) { szBuff_[pos++] = szBuff_[i++]; diff --git a/utils/ipc/src/net_packet.cpp b/utils/ipc/src/net_packet.cpp index 7e3a3bae..00acc027 100644 --- a/utils/ipc/src/net_packet.cpp +++ b/utils/ipc/src/net_packet.cpp @@ -32,11 +32,12 @@ void NetPacket::MakeData(StreamBuffer &buf) const { PACKHEAD head = {msgId_, wPos_}; buf << head; - if (wPos_ > 0) { - if (!buf.Write(&szBuff_[0], wPos_)) { - SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); - return; - } + if (wPos_ < 0) { + return; + } + if (!buf.Write(&szBuff_[0], wPos_)) { + SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); + return; } } -- Gitee From 13c058ac1a8af2d24d8084266e55b4863be5b972 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 6 Mar 2023 11:56:43 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I192b0c09597f03a5ddc1cc0fe5a245f87a4d92f8 --- interfaces/native/include/sensor_agent_type.h | 5 +- utils/common/BUILD.gn | 2 +- .../{subscribe_info.h => active_info.h} | 21 +++--- .../{subscribe_info.cpp => active_info.cpp} | 66 +++++++------------ 4 files changed, 33 insertions(+), 61 deletions(-) rename utils/common/include/{subscribe_info.h => active_info.h} (73%) rename utils/common/src/{subscribe_info.cpp => active_info.cpp} (52%) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 99c4d295..7b2cfbe1 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -395,13 +395,12 @@ typedef struct WearDetectionData { float value; } WearDetectionData; -typedef struct SubscribeSensorInfo { +typedef struct SensorActiveInfo { int32_t pid = -1; /**< PID */ int32_t sensorId = -1; /**< Sensor ID */ - bool isActive = false; /**< Whether the process enable the sensor */ int64_t samplingPeriodNs = -1; /**< Sample period, in ns */ int64_t maxReportDelayNs = -1; /**< Maximum Report Delay, in ns */ -} SubscribeSensorInfo; +} SensorActiveInfo; #ifdef __cplusplus #if __cplusplus diff --git a/utils/common/BUILD.gn b/utils/common/BUILD.gn index 5e1ef88f..0afec250 100644 --- a/utils/common/BUILD.gn +++ b/utils/common/BUILD.gn @@ -15,13 +15,13 @@ import("//build/ohos.gni") ohos_shared_library("libsensor_utils") { sources = [ + "src/active_info.cpp", "src/permission_util.cpp", "src/report_data_callback.cpp", "src/sensor.cpp", "src/sensor_basic_data_channel.cpp", "src/sensor_basic_info.cpp", "src/sensor_channel_info.cpp", - "src/subscribe_info.cpp", ] include_dirs = [ diff --git a/utils/common/include/subscribe_info.h b/utils/common/include/active_info.h similarity index 73% rename from utils/common/include/subscribe_info.h rename to utils/common/include/active_info.h index fbcd41b1..f068912a 100644 --- a/utils/common/include/subscribe_info.h +++ b/utils/common/include/active_info.h @@ -13,40 +13,35 @@ * limitations under the License. */ -#ifndef SUBSCRIBE_INFO_H -#define SUBSCRIBE_INFO_H +#ifndef ACTIVE_INFO_H +#define ACTIVE_INFO_H #include "parcel.h" -#include "sensor_agent_type.h" - namespace OHOS { namespace Sensors { -class SubscribeInfo : public Parcelable { +class ActiveInfo : public Parcelable { public: - SubscribeInfo() = default; - SubscribeInfo(SubscribeSensorInfo subscribeSensorInfo); - virtual ~SubscribeInfo() = default; + ActiveInfo() = default; + ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + virtual ~ActiveInfo() = default; int32_t GetPid() const; void SetPid(int32_t pid); int32_t GetSensorId() const; void SetSensorId(int32_t sensorId); - bool IsActive() const; - void Enable(bool isActive); int64_t GetSamplingPeriodNs() const; void SetSamplingPeriodNs(int64_t samplingPeriodNs); int64_t GetMaxReportDelayNs() const; void SetMaxReportDelayNs(int64_t maxReportDelayNs); bool Marshalling(Parcel &parcel) const; - std::unique_ptr Unmarshalling(Parcel &parcel); + std::unique_ptr Unmarshalling(Parcel &parcel); private: int32_t pid_ { -1 }; int32_t sensorId_ { -1 }; - bool isActive_ { -1 }; int64_t samplingPeriodNs_ { -1 }; int64_t maxReportDelayNs_ { -1 }; }; } // namespace Sensors } // namespace OHOS -#endif // SUBSCRIBE_INFO_H \ No newline at end of file +#endif // ACTIVE_INFO_H \ No newline at end of file diff --git a/utils/common/src/subscribe_info.cpp b/utils/common/src/active_info.cpp similarity index 52% rename from utils/common/src/subscribe_info.cpp rename to utils/common/src/active_info.cpp index 18c8605c..270b479d 100644 --- a/utils/common/src/subscribe_info.cpp +++ b/utils/common/src/active_info.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "subscribe_info.h" +#include "active_info.h" #include "sensors_errors.h" @@ -21,69 +21,53 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; namespace { -constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SubscribeInfo" }; +constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "ActiveInfo" }; } -SubscribeInfo::SubscribeInfo(SubscribeSensorInfo subscribeSensorInfo) -{ - pid_ = subscribeSensorInfo.pid; - sensorId_ = subscribeSensorInfo.sensorId; - isActive_ = subscribeSensorInfo.isActive; - samplingPeriodNs_ = subscribeSensorInfo.samplingPeriodNs; - maxReportDelayNs_ = subscribeSensorInfo.maxReportDelayNs; -} +ActiveInfo::ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) + :pid_(pid), sensorId_(sensorId), samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs) {}; -int32_t SubscribeInfo::GetPid() const +int32_t ActiveInfo::GetPid() const { return pid_; } -void SubscribeInfo::SetPid(int32_t pid) +void ActiveInfo::SetPid(int32_t pid) { pid_ = pid; } -int32_t SubscribeInfo::GetSensorId() const +int32_t ActiveInfo::GetSensorId() const { return sensorId_; } -void SubscribeInfo::SetSensorId(int32_t sensorId) +void ActiveInfo::SetSensorId(int32_t sensorId) { sensorId_ = sensorId; } -bool SubscribeInfo::IsActive() const -{ - return isActive_; -} - -void SubscribeInfo::Enable(bool isActive) -{ - isActive_ = isActive; -} - -int64_t SubscribeInfo::GetSamplingPeriodNs() const +int64_t ActiveInfo::GetSamplingPeriodNs() const { return samplingPeriodNs_; } -void SubscribeInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs) +void ActiveInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs) { samplingPeriodNs_ = samplingPeriodNs; } -int64_t SubscribeInfo::GetMaxReportDelayNs() const +int64_t ActiveInfo::GetMaxReportDelayNs() const { return maxReportDelayNs_; } -void SubscribeInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs) +void ActiveInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs) { maxReportDelayNs_ = maxReportDelayNs; } -bool SubscribeInfo::Marshalling(Parcel &parcel) const +bool ActiveInfo::Marshalling(Parcel &parcel) const { if (!parcel.WriteInt32(pid_)) { SEN_HILOGE("Write pid failed"); @@ -93,10 +77,6 @@ bool SubscribeInfo::Marshalling(Parcel &parcel) const SEN_HILOGE("Write sensorId failed"); return false; } - if (!parcel.WriteBool(isActive_)) { - SEN_HILOGE("Write isActive failed"); - return false; - } if (!parcel.WriteInt64(samplingPeriodNs_)) { SEN_HILOGE("Write samplingPeriodNs failed"); return false; @@ -108,25 +88,23 @@ bool SubscribeInfo::Marshalling(Parcel &parcel) const return true; } -std::unique_ptr SubscribeInfo::Unmarshalling(Parcel &parcel) +std::unique_ptr ActiveInfo::Unmarshalling(Parcel &parcel) { int32_t pid = -1; int32_t sensorId = -1; - bool isActive = false; int64_t samplingPeriodNs = -1; int64_t maxReportDelayNs = -1; - if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) && parcel.ReadBool(isActive) && + if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) && parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) { - SEN_HILOGE("ReadFromParcel is failed"); + SEN_HILOGE("Read from parcel is failed"); return nullptr; } - auto subscribeInfo = std::make_unique(); - subscribeInfo->SetPid(pid); - subscribeInfo->SetSensorId(sensorId); - subscribeInfo->Enable(isActive); - subscribeInfo->SetSamplingPeriodNs(samplingPeriodNs); - subscribeInfo->SetMaxReportDelayNs(maxReportDelayNs); - return subscribeInfo; + auto activeInfo = std::make_unique(); + activeInfo->SetPid(pid); + activeInfo->SetSensorId(sensorId); + activeInfo->SetSamplingPeriodNs(samplingPeriodNs); + activeInfo->SetMaxReportDelayNs(maxReportDelayNs); + return activeInfo; } } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From 3bf2295a1f2dc68b7158e141e34cba4c7572c96f Mon Sep 17 00:00:00 2001 From: hui1975 Date: Wed, 8 Mar 2023 02:48:23 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=E6=81=A2=E5=A4=8Dsocket=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I38b08eba94431c97a92219be881c5af62c5862cf --- utils/ipc/src/circle_stream_buffer.cpp | 2 +- utils/ipc/src/net_packet.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/utils/ipc/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp index 1f1845b2..a2c91f54 100644 --- a/utils/ipc/src/circle_stream_buffer.cpp +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -44,7 +44,7 @@ bool CircleStreamBuffer::Write(const char *buf, size_t size) void CircleStreamBuffer::CopyDataToBegin() { int32_t unreadSize = UnreadSize(); - if (unreadSize > 0 && rPos_ > 0 && sizeof(szBuff_) > wPos_) { + if (unreadSize > 0 && rPos_ > 0) { int32_t pos = 0; for (int32_t i = rPos_; i <= wPos_;) { szBuff_[pos++] = szBuff_[i++]; diff --git a/utils/ipc/src/net_packet.cpp b/utils/ipc/src/net_packet.cpp index 00acc027..7e3a3bae 100644 --- a/utils/ipc/src/net_packet.cpp +++ b/utils/ipc/src/net_packet.cpp @@ -32,12 +32,11 @@ void NetPacket::MakeData(StreamBuffer &buf) const { PACKHEAD head = {msgId_, wPos_}; buf << head; - if (wPos_ < 0) { - return; - } - if (!buf.Write(&szBuff_[0], wPos_)) { - SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); - return; + if (wPos_ > 0) { + if (!buf.Write(&szBuff_[0], wPos_)) { + SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); + return; + } } } -- Gitee From 1af33fe721fb4149902983ff358cd2dd5acea761 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 9 Mar 2023 11:30:34 +0000 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: Ib44652ddf0c19d07c92037c7dfef766f8cbd5fb5 --- utils/common/include/active_info.h | 2 +- utils/common/src/active_info.cpp | 3 ++- utils/ipc/include/circle_stream_buffer.h | 4 ++-- utils/ipc/include/net_packet.h | 2 +- utils/ipc/src/circle_stream_buffer.cpp | 21 ++++++++++----------- utils/ipc/src/net_packet.cpp | 9 +++++---- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/utils/common/include/active_info.h b/utils/common/include/active_info.h index f068912a..721e96c4 100644 --- a/utils/common/include/active_info.h +++ b/utils/common/include/active_info.h @@ -24,7 +24,7 @@ class ActiveInfo : public Parcelable { public: ActiveInfo() = default; ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); - virtual ~ActiveInfo() = default; + ~ActiveInfo() = default; int32_t GetPid() const; void SetPid(int32_t pid); int32_t GetSensorId() const; diff --git a/utils/common/src/active_info.cpp b/utils/common/src/active_info.cpp index 270b479d..6e2f836e 100644 --- a/utils/common/src/active_info.cpp +++ b/utils/common/src/active_info.cpp @@ -25,7 +25,8 @@ constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "ActiveInfo" }; } ActiveInfo::ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) - :pid_(pid), sensorId_(sensorId), samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs) {}; + :pid_(pid), sensorId_(sensorId), samplingPeriodNs_(samplingPeriodNs), maxReportDelayNs_(maxReportDelayNs) +{} int32_t ActiveInfo::GetPid() const { diff --git a/utils/ipc/include/circle_stream_buffer.h b/utils/ipc/include/circle_stream_buffer.h index 3aaea490..569ab5b1 100644 --- a/utils/ipc/include/circle_stream_buffer.h +++ b/utils/ipc/include/circle_stream_buffer.h @@ -24,9 +24,9 @@ class CircleStreamBuffer : public StreamBuffer { static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "CircleStreamBuffer" }; public: CircleStreamBuffer() = default; - virtual ~CircleStreamBuffer() = default; + ~CircleStreamBuffer() = default; bool CheckWrite(size_t size); - virtual bool Write(const char *buf, size_t size) override; + bool Write(const char *buf, size_t size) override; DISALLOW_MOVE(CircleStreamBuffer); protected: diff --git a/utils/ipc/include/net_packet.h b/utils/ipc/include/net_packet.h index 33d3545e..c88d1099 100644 --- a/utils/ipc/include/net_packet.h +++ b/utils/ipc/include/net_packet.h @@ -37,7 +37,7 @@ public: virtual ~NetPacket(); virtual void MakeData(StreamBuffer &buf) const; size_t GetSize() const; - int32_t GetPacketLength() const; + size_t GetPacketLength() const; const char* GetData() const; MessageId GetMsgId() const; DISALLOW_MOVE(NetPacket); diff --git a/utils/ipc/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp index a2c91f54..11bb9409 100644 --- a/utils/ipc/src/circle_stream_buffer.cpp +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -21,21 +21,20 @@ namespace OHOS { namespace Sensors { bool CircleStreamBuffer::CheckWrite(size_t size) { - int32_t bufferSize = static_cast(size); - int32_t availSize = GetAvailableBufSize(); - if (bufferSize > availSize && rPos_ > 0) { + size_t availSize = GetAvailableBufSize(); + if (size > availSize && rPos_ > 0) { CopyDataToBegin(); availSize = GetAvailableBufSize(); } - return (availSize >= bufferSize); + return (availSize >= size); } bool CircleStreamBuffer::Write(const char *buf, size_t size) { if (!CheckWrite(size)) { - SEN_HILOGE("Out of buffer memory, availableSize:%{public}d, size:%{public}zu," - "unreadSize:%{public}d, rPos:%{public}d, wPos:%{public}d", - GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_); + SEN_HILOGE("Out of buffer memory, availableSize:%{public}zu, size:%{public}zu," + "unreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", + GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_); return false; } return StreamBuffer::Write(buf, size); @@ -43,14 +42,14 @@ bool CircleStreamBuffer::Write(const char *buf, size_t size) void CircleStreamBuffer::CopyDataToBegin() { - int32_t unreadSize = UnreadSize(); + size_t unreadSize = UnreadSize(); if (unreadSize > 0 && rPos_ > 0) { - int32_t pos = 0; - for (int32_t i = rPos_; i <= wPos_;) { + size_t pos = 0; + for (size_t i = rPos_; i <= wPos_;) { szBuff_[pos++] = szBuff_[i++]; } } - SEN_HILOGD("UnreadSize:%{public}d, rPos:%{public}d, wPos:%{public}d", unreadSize, rPos_, wPos_); + SEN_HILOGD("UnreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", unreadSize, rPos_, wPos_); rPos_ = 0; wPos_ = unreadSize; } diff --git a/utils/ipc/src/net_packet.cpp b/utils/ipc/src/net_packet.cpp index 7e3a3bae..6a0b62fe 100644 --- a/utils/ipc/src/net_packet.cpp +++ b/utils/ipc/src/net_packet.cpp @@ -19,7 +19,8 @@ namespace OHOS { namespace Sensors { -NetPacket::NetPacket(MessageId msgId) : msgId_(msgId) {} +NetPacket::NetPacket(MessageId msgId) : msgId_(msgId) +{} NetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId()) { @@ -34,7 +35,7 @@ void NetPacket::MakeData(StreamBuffer &buf) const buf << head; if (wPos_ > 0) { if (!buf.Write(&szBuff_[0], wPos_)) { - SEN_HILOGE("Write data to stream failed, errCode:%{public}d", PROTO_STREAM_BUF_WRITE_FAIL); + SEN_HILOGE("Write data to stream failed"); return; } } @@ -45,9 +46,9 @@ size_t NetPacket::GetSize() const return Size(); } -int32_t NetPacket::GetPacketLength() const +size_t NetPacket::GetPacketLength() const { - return (static_cast(sizeof(PackHead)) + wPos_); + return sizeof(PackHead) + wPos_; } const char* NetPacket::GetData() const -- Gitee From 9388c0f0fc3fb153dd80a5564f3d2856158a9f76 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 9 Mar 2023 13:50:37 +0000 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I31cf925d337d497303b436a9420b08caa95467b2 --- utils/ipc/include/net_packet.h | 6 +++--- utils/ipc/src/circle_stream_buffer.cpp | 2 +- utils/ipc/src/net_packet.cpp | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/utils/ipc/include/net_packet.h b/utils/ipc/include/net_packet.h index c88d1099..6bf4e501 100644 --- a/utils/ipc/include/net_packet.h +++ b/utils/ipc/include/net_packet.h @@ -22,7 +22,7 @@ #pragma pack(1) using PACKHEAD = struct PackHead { OHOS::Sensors::MessageId idMsg; - int32_t size; + size_t size; }; #pragma pack() @@ -34,8 +34,8 @@ public: explicit NetPacket(MessageId msgId); NetPacket(const NetPacket &pkt); NetPacket &operator = (const NetPacket &pkt); - virtual ~NetPacket(); - virtual void MakeData(StreamBuffer &buf) const; + ~NetPacket() = default; + void MakeData(StreamBuffer &buf) const; size_t GetSize() const; size_t GetPacketLength() const; const char* GetData() const; diff --git a/utils/ipc/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp index 11bb9409..c086b695 100644 --- a/utils/ipc/src/circle_stream_buffer.cpp +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -32,7 +32,7 @@ bool CircleStreamBuffer::CheckWrite(size_t size) bool CircleStreamBuffer::Write(const char *buf, size_t size) { if (!CheckWrite(size)) { - SEN_HILOGE("Out of buffer memory, availableSize:%{public}zu, size:%{public}zu," + SEN_HILOGE("Buffer is overflow, availableSize:%{public}zu, size:%{public}zu," "unreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_); return false; diff --git a/utils/ipc/src/net_packet.cpp b/utils/ipc/src/net_packet.cpp index 6a0b62fe..27dec89b 100644 --- a/utils/ipc/src/net_packet.cpp +++ b/utils/ipc/src/net_packet.cpp @@ -27,8 +27,6 @@ NetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId()) Clone(pkt); } -NetPacket::~NetPacket() {} - void NetPacket::MakeData(StreamBuffer &buf) const { PACKHEAD head = {msgId_, wPos_}; -- Gitee