diff --git a/frameworks/native/sensor/BUILD.gn b/frameworks/native/sensor/BUILD.gn index 9f9c6c3f30563805223a86e625fbc60b0b62ed08..ab23c92249e69dd26a7e473450e0e137293896f5 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/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 5ed9c4b2f3aeca04e6490e91f7cb97a1c91080ae..4cd491086e56a5d7a6c5e940eca216ed3f8ffdd1 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/BUILD.gn b/interfaces/native/BUILD.gn index 1050549d522b9148360c3a4b4ac6a2f606a5fb52..9e1bece6fcfc741575bed5145d525c32ad55a25b 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/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 37b0de0e57418519152160a8e4307367945e1d8f..7b2cfbe104521b1fd58630759dc29cd6c221b24a 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,13 @@ typedef struct WearDetectionData { float value; } WearDetectionData; +typedef struct SensorActiveInfo { + int32_t pid = -1; /**< PID */ + int32_t sensorId = -1; /**< Sensor ID */ + int64_t samplingPeriodNs = -1; /**< Sample period, in ns */ + int64_t maxReportDelayNs = -1; /**< Maximum Report Delay, in ns */ +} SensorActiveInfo; + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 5a175be21419441dd1c75a3cef2b693b9dfae6c5..45e418f6800301e9685db31a43217609d94b6e8d 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 d84452593cb927399ef09017acafd3ecc0080477..4c3db194fd74cad3b67d20f73a53347c94258705 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", + "./../../utils/common/include", "//foundation/arkui/napi/interfaces/inner_api/napi", ] defines = [ diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index de3715a3eaa3c506362f44685db6e83781cc344b..eb5742762f200b7aac577ae69afe0c563730464c 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 b48b8fdad025a93d9677b2581cd72edd083d8afe..0a59896935782f623a8e295918e64b30e17e5f09 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,8 @@ 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 +58,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 7a971bb7473fb530b345c66e18a97912ea76e55d..6e4707781174a4f1865742b6e685686d65006b30 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 0000000000000000000000000000000000000000..0afec250d7250e3a5b0bc16392c9bc9e7c05cee2 --- /dev/null +++ b/utils/common/BUILD.gn @@ -0,0 +1,43 @@ +# 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") + +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", + ] + + include_dirs = [ + "include", + "./../../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/active_info.h b/utils/common/include/active_info.h new file mode 100644 index 0000000000000000000000000000000000000000..721e96c4e56932c96a063df26e31ab0aa3e66440 --- /dev/null +++ b/utils/common/include/active_info.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 ACTIVE_INFO_H +#define ACTIVE_INFO_H + +#include "parcel.h" + +namespace OHOS { +namespace Sensors { +class ActiveInfo : public Parcelable { +public: + ActiveInfo() = default; + ActiveInfo(int32_t pid, int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + ~ActiveInfo() = default; + int32_t GetPid() const; + void SetPid(int32_t pid); + int32_t GetSensorId() const; + void SetSensorId(int32_t sensorId); + 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 }; + int64_t samplingPeriodNs_ { -1 }; + int64_t maxReportDelayNs_ { -1 }; +}; +} // namespace Sensors +} // namespace OHOS +#endif // ACTIVE_INFO_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 ba61b2f896dc9f6710ed433d8f406e56ed9e539a..dadbacbd87bea822a9349374eb8d4bedb306a844 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 98% rename from utils/include/sensors_errors.h rename to utils/common/include/sensors_errors.h index 55e11c3c1811330cd08f4607e528d3235cfae0ee..43edceab7f28f318574e95ec0dfa9506deb4539c 100644 --- a/utils/include/sensors_errors.h +++ b/utils/common/include/sensors_errors.h @@ -87,8 +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, - SET_SENSOR_MODE_ERR = WRITE_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/src/active_info.cpp b/utils/common/src/active_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e2f836e3e202d5c2c364d4c6bb63518d629082b --- /dev/null +++ b/utils/common/src/active_info.cpp @@ -0,0 +1,111 @@ +/* + * 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 "active_info.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +namespace { +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) +{} + +int32_t ActiveInfo::GetPid() const +{ + return pid_; +} + +void ActiveInfo::SetPid(int32_t pid) +{ + pid_ = pid; +} + +int32_t ActiveInfo::GetSensorId() const +{ + return sensorId_; +} + +void ActiveInfo::SetSensorId(int32_t sensorId) +{ + sensorId_ = sensorId; +} + +int64_t ActiveInfo::GetSamplingPeriodNs() const +{ + return samplingPeriodNs_; +} + +void ActiveInfo::SetSamplingPeriodNs(int64_t samplingPeriodNs) +{ + samplingPeriodNs_ = samplingPeriodNs; +} + +int64_t ActiveInfo::GetMaxReportDelayNs() const +{ + return maxReportDelayNs_; +} + +void ActiveInfo::SetMaxReportDelayNs(int64_t maxReportDelayNs) +{ + maxReportDelayNs_ = maxReportDelayNs; +} + +bool ActiveInfo::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.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 ActiveInfo::Unmarshalling(Parcel &parcel) +{ + int32_t pid = -1; + int32_t sensorId = -1; + int64_t samplingPeriodNs = -1; + int64_t maxReportDelayNs = -1; + if (!(parcel.ReadInt32(pid) && parcel.ReadInt32(sensorId) && + parcel.ReadInt64(samplingPeriodNs) && parcel.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("Read from parcel is failed"); + return nullptr; + } + 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 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 4e2d91e00fa3f6c7d3029739ca6cca88e7107d0a..9e5bd3050f1ea9e1479a29fa31e7e3b6aca336ba 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 0000000000000000000000000000000000000000..569ab5b10f338af9f230e81f57128a243656bff3 --- /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; + ~CircleStreamBuffer() = default; + bool CheckWrite(size_t size); + 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 0000000000000000000000000000000000000000..6bf4e501eede4befd27070311ed2c4cbff2c40f8 --- /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; + size_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); + ~NetPacket() = default; + void MakeData(StreamBuffer &buf) const; + size_t GetSize() const; + size_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/src/circle_stream_buffer.cpp b/utils/ipc/src/circle_stream_buffer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c086b695719ef5dcfe11925ea450a93cf63f9f53 --- /dev/null +++ b/utils/ipc/src/circle_stream_buffer.cpp @@ -0,0 +1,57 @@ +/* + * 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) +{ + size_t availSize = GetAvailableBufSize(); + if (size > availSize && rPos_ > 0) { + CopyDataToBegin(); + availSize = GetAvailableBufSize(); + } + return (availSize >= size); +} + +bool CircleStreamBuffer::Write(const char *buf, size_t size) +{ + if (!CheckWrite(size)) { + 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; + } + return StreamBuffer::Write(buf, size); +} + +void CircleStreamBuffer::CopyDataToBegin() +{ + size_t unreadSize = UnreadSize(); + if (unreadSize > 0 && rPos_ > 0) { + size_t pos = 0; + for (size_t i = rPos_; i <= wPos_;) { + szBuff_[pos++] = szBuff_[i++]; + } + } + SEN_HILOGD("UnreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", 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 0000000000000000000000000000000000000000..27dec89bc2f8d14faffdfdd21b2dcb86d7716efb --- /dev/null +++ b/utils/ipc/src/net_packet.cpp @@ -0,0 +1,62 @@ +/* + * 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); +} + +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"); + return; + } + } +} + +size_t NetPacket::GetSize() const +{ + return Size(); +} + +size_t NetPacket::GetPacketLength() const +{ + return sizeof(PackHead) + wPos_; +} + +const char* NetPacket::GetData() const +{ + return Data(); +} + +MessageId NetPacket::GetMsgId() const +{ + return msgId_; +} +} // namespace Sensors +} // namespace OHOS