From e2605cbfad92d5d51c992fe0962eb27b5f44bfc5 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 25 Mar 2023 08:49:16 +0000 Subject: [PATCH 01/22] =?UTF-8?q?sensor=E7=9C=81=E5=8A=9F=E8=80=97?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E4=BB=A3=E7=A0=812?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I3deba8771f7333060887906b5bff327d16b0b798 --- services/sensor/include/sensor_service.h | 16 +- services/sensor/src/sensor_service.cpp | 112 +++++++++- services/sensor/src/sensor_service_stub.cpp | 121 ++++++++++- services/sensor/src/stream_server.cpp | 220 ++++++++++++++++++++ 4 files changed, 463 insertions(+), 6 deletions(-) create mode 100644 services/sensor/src/stream_server.cpp diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 3f1a3a9f..f2eaedd1 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.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 @@ -28,7 +28,9 @@ #include "sensor_data_event.h" #include "sensor_hdi_connection.h" #include "sensor_manager.h" +#include "sensor_power_policy.h" #include "sensor_service_stub.h" +#include "stream_server.h" namespace OHOS { namespace Sensors { @@ -37,7 +39,7 @@ enum class SensorServiceState { STATE_RUNNING, }; -class SensorService : public SystemAbility, public SensorServiceStub { +class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { DECLARE_SYSTEM_ABILITY(SensorService) public: @@ -54,6 +56,13 @@ public: const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; void ProcessDeathObserver(const wptr &object); + ErrCode SuspendSensors(int32_t pid) override; + ErrCode ResumeSensors(int32_t pid) override; + ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; + ErrCode CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) override; + ErrCode DestroySocketChannel(const sptr &sensorClient) override; + ErrCode EnableActiveInfoCB() override; + ErrCode DisableActiveInfoCB() override; private: DISALLOW_COPY_AND_MOVE(SensorService); @@ -88,6 +97,7 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); + SensorPowerPolicy &sensorPowerPolicy_ = SensorPowerPolicy::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; std::mutex uidLock_; @@ -95,6 +105,8 @@ private: sptr clientDeathObserver_ = nullptr; std::shared_ptr permStateChangeCb_; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); + std::atomic_bool isReportActiveInfo_ = false; + void ReportActiveInfo(int32_t sensorId, int32_t pid); }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index db7c977e..1a26a72b 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 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 @@ -20,6 +20,7 @@ #include #include +#include "accesstoken_kit.h" #include "hisysevent.h" #include "iservice_registry.h" #include "permission_util.h" @@ -32,7 +33,7 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; - +using namespace Security::AccessToken; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorService" }; constexpr int32_t INVALID_SENSOR_ID = -1; @@ -234,6 +235,9 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, SEN_HILOGE("ret : %{public}d", ret); } ReportOnChangeData(sensorId); + if (isReportActiveInfo_) { + ReportActiveInfo(sensorId, pid); + } return ERR_OK; } auto ret = SaveSubscriber(sensorId, samplingPeriodNs, maxReportDelayNs); @@ -249,6 +253,9 @@ ErrCode SensorService::EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, return ENABLE_SENSOR_ERR; } ReportSensorSysEvent(sensorId, true, pid); + if (isReportActiveInfo_) { + ReportActiveInfo(sensorId, pid); + } return ret; } @@ -358,6 +365,8 @@ void SensorService::ProcessDeathObserver(const wptr &object) SEN_HILOGE("disablesensor failed, ret:%{public}d", ret); } } + DelSession(pid); + clientInfo_.DelActiveInfoCBPid(pid); clientInfo_.DestroySensorChannel(pid); clientInfo_.DestroyClientPid(client); clientInfo_.DestroyCmd(clientInfo_.GetUidByPid(pid)); @@ -376,6 +385,15 @@ void SensorService::RegisterClientDeathRecipient(sptr sensorClien void SensorService::UnregisterClientDeathRecipient(sptr sensorClient) { CALL_LOG_ENTER; + int32_t pid = clientInfo_.FindClientPid(sensorClient); + if (pid == INVALID_PID) { + SEN_HILOGE("Pid is invalid"); + return; + } + if (!clientInfo_.IsUnregisterClientDeathRecipient(pid)) { + SEN_HILOGD("Client call other service, can not unregister client death recipient"); + return; + } sptr client = iface_cast(sensorClient); clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); CHKPV(clientDeathObserver_); @@ -406,6 +424,96 @@ int32_t SensorService::Dump(int32_t fd, const std::vector &args) return ERR_OK; } +ErrCode SensorService::SuspendSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid"); + return CLIENT_PID_INVALID_ERR; + } + return sensorPowerPolicy_.SuspendSensors(pid); +} + +ErrCode SensorService::ResumeSensors(int32_t pid) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid"); + return CLIENT_PID_INVALID_ERR; + } + return sensorPowerPolicy_.ResumeSensors(pid); +} + +ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) +{ + CALL_LOG_ENTER; + if (pid < 0) { + SEN_HILOGE("Pid is invalid"); + return CLIENT_PID_INVALID_ERR; + } + sensorPowerPolicy_.GetActiveInfoList(pid, activeInfoList); + return ERR_OK; +} + +ErrCode SensorService::CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) +{ + CALL_LOG_ENTER; + int32_t uid = GetCallingUid(); + int32_t pid = GetCallingPid(); + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); + int32_t serverFd = -1; + clientFd = -1; + int32_t ret = AddSocketPairInfo(uid, pid, tokenType, serverFd, std::ref(clientFd)); + if (ret != ERR_OK) { + SEN_HILOGE("Add socket pair info failed, ret:%{public}d", ret); + return ret; + } + RegisterClientDeathRecipient(sensorClient, pid); + return ERR_OK; +} + +ErrCode SensorService::DestroySocketChannel(const sptr &sensorClient) +{ + CALL_LOG_ENTER; + int32_t pid = GetCallingPid(); + DelSession(pid); + UnregisterClientDeathRecipient(sensorClient); + return ERR_OK; +} + +ErrCode SensorService::EnableActiveInfoCB() +{ + CALL_LOG_ENTER; + isReportActiveInfo_ = true; + int32_t pid = GetCallingPid(); + return clientInfo_.AddActiveInfoCBPid(pid); +} + +ErrCode SensorService::DisableActiveInfoCB() +{ + CALL_LOG_ENTER; + isReportActiveInfo_ = false; + int32_t pid = GetCallingPid(); + return clientInfo_.DelActiveInfoCBPid(pid); +} + +void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) +{ + CALL_LOG_ENTER; + std::vector sessionList; + auto pidSet = clientInfo_.GetActiveInfoCBPid(); + for (auto pid : pidSet) { + auto sess = GetSessionByPid(pid); + if (sess != nullptr) { + sessionList.push_back(sess); + } + } + SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); + ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), + sensorInfo.GetMaxReportDelayNs()); + sensorPowerPolicy_.ReportActiveInfo(activeInfo, sessionList); +} + bool SensorService::RegisterPermCallback() { Security::AccessToken::PermStateChangeScope scope = { diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 94c2a8af..f9d88729 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -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 @@ -30,7 +30,6 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; - namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceStub" }; } // namespace @@ -43,6 +42,13 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; + baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; + baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; + baseFuncs_[GET_ACTIVE_INFO_LIST] = &SensorServiceStub::GetActiveInfoListInner; + baseFuncs_[CREATE_SOCKET_CHANNEL] = &SensorServiceStub::CreateSocketChannelInner; + baseFuncs_[DESTROY_SOCKET_CHANNEL] = &SensorServiceStub::DestroySocketChannelInner; + baseFuncs_[ENABLE_ACTIVE_INFO_CB] = &SensorServiceStub::EnableActiveInfoCBInner; + baseFuncs_[DISABLE_ACTIVE_INFO_CB] = &SensorServiceStub::DisableActiveInfoCBInner; } SensorServiceStub::~SensorServiceStub() @@ -153,5 +159,116 @@ ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageP CHKPR(sensorClient, OBJECT_NULL); return DestroySensorChannel(sensorClient); } + +ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return SuspendSensors(pid); +} + +ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + (void)reply; + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + return ResumeSensors(pid); +} + +ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + int32_t pid; + if (!data.ReadInt32(pid)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } + std::vector activeInfoList; + GetActiveInfoList(pid, activeInfoList); + int32_t activeInfoCount = int32_t { activeInfoList.size() }; + reply.WriteInt32(activeInfoCount); + for (int32_t i = 0; i < activeInfoCount; i++) { + if (!activeInfoList[i].Marshalling(reply)) { + SEN_HILOGE("ActiveInfo %{public}d failed", i); + return ERROR; + } + } + return ERR_OK; +} + +ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, MessageParcel &reply) +{ + sptr sensorClient = data.ReadRemoteObject(); + CHKPR(sensorClient, OBJECT_NULL); + int32_t clientFd = -1; + int32_t ret = CreateSocketChannel(clientFd, sensorClient); + if (ret != ERR_OK) { + SEN_HILOGE("Create socket channel failed"); + if (clientFd >= 0) { + close(clientFd); + } + return ret; + } + if (!reply.WriteFileDescriptor(clientFd)) { + SEN_HILOGE("Write file descriptor failed"); + close(clientFd); + return ERROR; + } + close(clientFd); + return ERR_OK; +} + +ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, MessageParcel &reply) +{ + sptr sensorClient = data.ReadRemoteObject(); + CHKPR(sensorClient, OBJECT_NULL); + int32_t ret = DestroySocketChannel(sensorClient); + if (ret != ERR_OK) { + SEN_HILOGE("DestroySocketChannel failed"); + return ret; + } + return ERR_OK; +} + +ErrCode SensorServiceStub::EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + return EnableActiveInfoCB(); +} + +ErrCode SensorServiceStub::DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) +{ + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + return DisableActiveInfoCB(); +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp new file mode 100644 index 00000000..3441b99b --- /dev/null +++ b/services/sensor/src/stream_server.cpp @@ -0,0 +1,220 @@ +/* + * 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_server.h" + +#include +#include + +#include "accesstoken_kit.h" + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +namespace { +using namespace Security::AccessToken; +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamServer" }; +constexpr int32_t INVALID_PID = -1; +constexpr int32_t INVALID_FD = -1; +} // namespace +StreamServer::~StreamServer() +{ + CALL_LOG_ENTER; + idxPidMap_.clear(); + for (const auto &item : sessionsMap_) { + item.second->Close(); + } + sessionsMap_.clear(); +} + +bool StreamServer::SendMsg(int32_t fd, NetPacket& pkt) +{ + CALL_LOG_ENTER; + if (fd < 0) { + SEN_HILOGE("Fd is invalid"); + return false; + } + auto ses = GetSession(fd); + if (ses == nullptr) { + SEN_HILOGE("Fd not found, The message was discarded."); + return false; + } + return ses->SendMsg(pkt); +} + +void StreamServer::Multicast(const std::vector& fdList, NetPacket& pkt) +{ + CALL_LOG_ENTER; + for (const auto &item : fdList) { + SendMsg(item, pkt); + } +} + +int32_t StreamServer::GetClientFd(int32_t pid) +{ + std::lock_guard idxPidLock(idxPidMutex_); + auto it = idxPidMap_.find(pid); + if (it == idxPidMap_.end()) { + return INVALID_FD; + } + return it->second; +} + +int32_t StreamServer::GetClientPid(int32_t fd) +{ + std::lock_guard sessionLock(sessionMutex_); + auto it = sessionsMap_.find(fd); + if (it == sessionsMap_.end()) { + return INVALID_PID; + } + return it->second->GetPid(); +} + +SessionPtr StreamServer::GetSession(int32_t fd) +{ + std::lock_guard sessionLock(sessionMutex_); + auto it = sessionsMap_.find(fd); + if (it == sessionsMap_.end()) { + SEN_HILOGE("Session not found"); + return nullptr; + } + CHKPP(it->second); + return it->second->GetSharedPtr(); +} + +SessionPtr StreamServer::GetSessionByPid(int32_t pid) +{ + int32_t fd = GetClientFd(pid); + if (fd <= 0) { + SEN_HILOGE("Session not found"); + return nullptr; + } + return GetSession(fd); +} + +int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenType, + int32_t &serverFd, int32_t &clientFd) +{ + CALL_LOG_ENTER; + std::string programName = ""; + int32_t sockFds[2] = { -1 }; + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockFds) != 0) { + SEN_HILOGE("Call socketpair failed, errno:%{public}d", errno); + return ERROR; + } + serverFd = sockFds[0]; + clientFd = sockFds[1]; + if (serverFd < 0 || clientFd < 0) { + SEN_HILOGE("Call fcntl failed, errno:%{public}d", errno); + return ERROR; + } + static constexpr size_t bufferSize = 32 * 1024; + static constexpr size_t nativeBufferSize = 64 * 1024; + SessionPtr sess = nullptr; + if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + if (setsockopt(serverFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) { + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &nativeBufferSize, sizeof(nativeBufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &nativeBufferSize, sizeof(nativeBufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + } else { + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + } + sess = std::make_shared(programName, serverFd, uid, pid); + sess->SetTokenType(tokenType); + if (!AddSession(sess)) { + SEN_HILOGE("AddSession fail"); + goto CLOSE_SOCK; + } + return ERR_OK; + +CLOSE_SOCK: + close(serverFd); + serverFd = -1; + close(clientFd); + clientFd = -1; + return ERROR; +} + +bool StreamServer::AddSession(SessionPtr ses) +{ + CALL_LOG_ENTER; + CHKPF(ses); + auto fd = ses->GetFd(); + if (fd < 0) { + SEN_HILOGE("Fd is Invalid"); + return false; + } + auto pid = ses->GetPid(); + if (pid <= 0) { + SEN_HILOGE("Get process failed"); + return false; + } + if (sessionsMap_.size() > MAX_SESSON_ALARM) { + SEN_HILOGE("Too many clients. Warning Value:%{public}zu, Current Value:%{public}zu", + MAX_SESSON_ALARM, sessionsMap_.size()); + return false; + } + DelSession(pid); + std::lock_guard idxPidLock(idxPidMutex_); + idxPidMap_[pid] = fd; + std::lock_guard sessionLock(sessionMutex_); + sessionsMap_[fd] = ses; + return true; +} + +void StreamServer::DelSession(int32_t pid) +{ + CALL_LOG_ENTER; + std::lock_guard idxPidLock(idxPidMutex_); + auto pidIt = idxPidMap_.find(pid); + if (pidIt == idxPidMap_.end()) { + return; + } + int32_t fd = pidIt->second; + idxPidMap_.erase(pidIt); + std::lock_guard sessionLock(sessionMutex_); + auto fdIt = sessionsMap_.find(fd); + if (fdIt != sessionsMap_.end()) { + sessionsMap_.erase(fdIt); + } + if (fd >= 0) { + auto rf = close(fd); + if (rf > 0) { + SEN_HILOGE("Socket fd close failed, rf:%{public}d", rf); + } + } +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file -- Gitee From 49f049f935d51fb5f0456e19d71b9f7c0ca29e3d Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 27 Mar 2023 12:29:06 +0000 Subject: [PATCH 02/22] update Signed-off-by: hui1975 Change-Id: I115ba1a7f996647ec29148e48db64e443ca0d0c1 --- services/sensor/include/sensor_service.h | 4 ++-- services/sensor/src/sensor_service.cpp | 23 ++++++++------------- services/sensor/src/sensor_service_stub.cpp | 11 +++++----- services/sensor/src/stream_server.cpp | 4 ++-- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index f2eaedd1..f5f0afa5 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -59,7 +59,7 @@ public: ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; - ErrCode CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) override; + ErrCode CreateSocketChannel(const sptr &sensorClient, int32_t &clientFd) override; ErrCode DestroySocketChannel(const sptr &sensorClient) override; ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; @@ -88,6 +88,7 @@ private: ErrCode DisableSensor(int32_t sensorId, int32_t pid); bool RegisterPermCallback(); void UnregisterPermCallback(); + void ReportActiveInfo(int32_t sensorId, int32_t pid); SensorServiceState state_; std::mutex serviceLock_; std::mutex sensorsMutex_; @@ -106,7 +107,6 @@ private: std::shared_ptr permStateChangeCb_; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; - void ReportActiveInfo(int32_t sensorId, int32_t pid); }; } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 1a26a72b..1279aa85 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -455,28 +455,25 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a return ERR_OK; } -ErrCode SensorService::CreateSocketChannel(int32_t &clientFd, const sptr &sensorClient) +ErrCode SensorService::CreateSocketChannel(const sptr &sensorClient, int32_t &clientFd) { CALL_LOG_ENTER; - int32_t uid = GetCallingUid(); - int32_t pid = GetCallingPid(); - int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()); int32_t serverFd = -1; - clientFd = -1; - int32_t ret = AddSocketPairInfo(uid, pid, tokenType, serverFd, std::ref(clientFd)); + int32_t ret = AddSocketPairInfo(GetCallingUid(), GetCallingPid(), + AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()), + serverFd, std::ref(clientFd)); if (ret != ERR_OK) { SEN_HILOGE("Add socket pair info failed, ret:%{public}d", ret); return ret; } - RegisterClientDeathRecipient(sensorClient, pid); + RegisterClientDeathRecipient(sensorClient, GetCallingPid()); return ERR_OK; } ErrCode SensorService::DestroySocketChannel(const sptr &sensorClient) { CALL_LOG_ENTER; - int32_t pid = GetCallingPid(); - DelSession(pid); + DelSession(GetCallingPid()); UnregisterClientDeathRecipient(sensorClient); return ERR_OK; } @@ -485,16 +482,14 @@ ErrCode SensorService::EnableActiveInfoCB() { CALL_LOG_ENTER; isReportActiveInfo_ = true; - int32_t pid = GetCallingPid(); - return clientInfo_.AddActiveInfoCBPid(pid); + return clientInfo_.AddActiveInfoCBPid(GetCallingPid()); } ErrCode SensorService::DisableActiveInfoCB() { CALL_LOG_ENTER; isReportActiveInfo_ = false; - int32_t pid = GetCallingPid(); - return clientInfo_.DelActiveInfoCBPid(pid); + return clientInfo_.DelActiveInfoCBPid(GetCallingPid()); } void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) @@ -510,7 +505,7 @@ void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) } SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), - sensorInfo.GetMaxReportDelayNs()); + sensorInfo.GetMaxReportDelayNs()); sensorPowerPolicy_.ReportActiveInfo(activeInfo, sessionList); } diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index f9d88729..699bf1e6 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -205,7 +205,11 @@ ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessagePa return ERROR; } std::vector activeInfoList; - GetActiveInfoList(pid, activeInfoList); + int32_t ret = GetActiveInfoList(pid, activeInfoList); + if (ret != ERR_OK) { + SEN_HILOGE("Get activeInfo list failed"); + return ret; + } int32_t activeInfoCount = int32_t { activeInfoList.size() }; reply.WriteInt32(activeInfoCount); for (int32_t i = 0; i < activeInfoCount; i++) { @@ -222,12 +226,9 @@ ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, Message sptr sensorClient = data.ReadRemoteObject(); CHKPR(sensorClient, OBJECT_NULL); int32_t clientFd = -1; - int32_t ret = CreateSocketChannel(clientFd, sensorClient); + int32_t ret = CreateSocketChannel(sensorClient, clientFd); if (ret != ERR_OK) { SEN_HILOGE("Create socket channel failed"); - if (clientFd >= 0) { - close(clientFd); - } return ret; } if (!reply.WriteFileDescriptor(clientFd)) { diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index 3441b99b..ed7d02a5 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -106,7 +106,7 @@ SessionPtr StreamServer::GetSessionByPid(int32_t pid) } int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenType, - int32_t &serverFd, int32_t &clientFd) + int32_t &serverFd, int32_t &clientFd) { CALL_LOG_ENTER; std::string programName = ""; @@ -211,7 +211,7 @@ void StreamServer::DelSession(int32_t pid) } if (fd >= 0) { auto rf = close(fd); - if (rf > 0) { + if (rf != 0) { SEN_HILOGE("Socket fd close failed, rf:%{public}d", rf); } } -- Gitee From 623e2008baebc288749036dcbce84ac154aa4314 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 6 Apr 2023 07:55:01 +0000 Subject: [PATCH 03/22] update Signed-off-by: hui1975 Change-Id: I61499855cd59adbf87654cb77edd1dbef8632cfe --- services/sensor/include/sensor_service.h | 4 ++-- services/sensor/src/sensor_service.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 849c6dfc..dff3c41a 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -59,8 +59,8 @@ public: ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; - ErrCode CreateSocketChannel(const sptr &sensorClient, int32_t &clientFd) override; - ErrCode DestroySocketChannel(const sptr &sensorClient) override; + ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) override; + ErrCode DestroySocketChannel(sptr sensorClient) override; ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 2a7868ec..2fb69e69 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -456,7 +456,7 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a return ERR_OK; } -ErrCode SensorService::CreateSocketChannel(const sptr &sensorClient, int32_t &clientFd) +ErrCode SensorService::CreateSocketChannel(sptr sensorClient, int32_t &clientFd) { CALL_LOG_ENTER; int32_t serverFd = -1; @@ -471,7 +471,7 @@ ErrCode SensorService::CreateSocketChannel(const sptr &sensorClie return ERR_OK; } -ErrCode SensorService::DestroySocketChannel(const sptr &sensorClient) +ErrCode SensorService::DestroySocketChannel(sptr sensorClient) { CALL_LOG_ENTER; DelSession(GetCallingPid()); -- Gitee From 0534ef8f7869fb0be87bdcc117e1331b13d61a7c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 18 Apr 2023 02:46:12 +0000 Subject: [PATCH 04/22] update Signed-off-by: hui1975 Change-Id: I00e073d69cc49ac078fb9200be1639c55752de95 --- .../native/sensor/include/i_sensor_service.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index dab25cb4..7404ad58 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.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 @@ -21,6 +21,7 @@ #include "errors.h" #include "iremote_broker.h" +#include "active_info.h" #include "i_sensor_client.h" #include "sensor_basic_data_channel.h" #include "sensor.h" @@ -39,6 +40,13 @@ public: virtual ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) = 0; virtual ErrCode DestroySensorChannel(sptr sensorClient) = 0; + virtual ErrCode SuspendSensors(int32_t pid) = 0; + virtual ErrCode ResumeSensors(int32_t pid) = 0; + virtual ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) = 0; + virtual ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) = 0; + virtual ErrCode DestroySocketChannel(sptr sensorClient) = 0; + virtual ErrCode EnableActiveInfoCB() = 0; + virtual ErrCode DisableActiveInfoCB() = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -47,6 +55,13 @@ public: GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, + SUSPEND_SENSORS, + RESUME_SENSORS, + GET_ACTIVE_INFO_LIST, + CREATE_SOCKET_CHANNEL, + DESTROY_SOCKET_CHANNEL, + ENABLE_ACTIVE_INFO_CB, + DISABLE_ACTIVE_INFO_CB, }; }; } // namespace Sensors -- Gitee From 94f142977f0421097d9879e3d39004176155d8b9 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 18 Apr 2023 03:13:28 +0000 Subject: [PATCH 05/22] update Signed-off-by: hui1975 Change-Id: Icdad3191fff31417b30239c40e3949dd2aef063c --- services/sensor/src/sensor_service.cpp | 6 ++--- services/sensor/src/stream_server.cpp | 37 ++++++++------------------ 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 2fb69e69..e83f7f0d 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -452,7 +452,7 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - sensorPowerPolicy_.GetActiveInfoList(pid, activeInfoList); + activeInfoList = sensorPowerPolicy_.GetActiveInfoList(pid); return ERR_OK; } @@ -497,8 +497,8 @@ void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) { CALL_LOG_ENTER; std::vector sessionList; - auto pidSet = clientInfo_.GetActiveInfoCBPid(); - for (auto pid : pidSet) { + auto pidList = clientInfo_.GetActiveInfoCBPid(); + for (auto pid : pidList) { auto sess = GetSessionByPid(pid); if (sess != nullptr) { sessionList.push_back(sess); diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index ed7d02a5..ab59a8d3 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -18,18 +18,16 @@ #include #include -#include "accesstoken_kit.h" - #include "sensors_errors.h" namespace OHOS { namespace Sensors { namespace { -using namespace Security::AccessToken; constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamServer" }; constexpr int32_t INVALID_PID = -1; constexpr int32_t INVALID_FD = -1; } // namespace + StreamServer::~StreamServer() { CALL_LOG_ENTER; @@ -40,7 +38,7 @@ StreamServer::~StreamServer() sessionsMap_.clear(); } -bool StreamServer::SendMsg(int32_t fd, NetPacket& pkt) +bool StreamServer::SendMsg(int32_t fd, const NetPacket& pkt) { CALL_LOG_ENTER; if (fd < 0) { @@ -55,7 +53,7 @@ bool StreamServer::SendMsg(int32_t fd, NetPacket& pkt) return ses->SendMsg(pkt); } -void StreamServer::Multicast(const std::vector& fdList, NetPacket& pkt) +void StreamServer::Multicast(const std::vector& fdList, const NetPacket& pkt) { CALL_LOG_ENTER; for (const auto &item : fdList) { @@ -109,7 +107,6 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT int32_t &serverFd, int32_t &clientFd) { CALL_LOG_ENTER; - std::string programName = ""; int32_t sockFds[2] = { -1 }; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockFds) != 0) { SEN_HILOGE("Call socketpair failed, errno:%{public}d", errno); @@ -122,7 +119,6 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT return ERROR; } static constexpr size_t bufferSize = 32 * 1024; - static constexpr size_t nativeBufferSize = 64 * 1024; SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); @@ -132,26 +128,15 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); goto CLOSE_SOCK; } - if (tokenType == ATokenTypeEnum::TOKEN_NATIVE) { - if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &nativeBufferSize, sizeof(nativeBufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); - goto CLOSE_SOCK; - } - if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &nativeBufferSize, sizeof(nativeBufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); - goto CLOSE_SOCK; - } - } else { - if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); - goto CLOSE_SOCK; - } - if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); - goto CLOSE_SOCK; - } + if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; + } + if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { + SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + goto CLOSE_SOCK; } - sess = std::make_shared(programName, serverFd, uid, pid); + sess = std::make_shared("", serverFd, uid, pid); sess->SetTokenType(tokenType); if (!AddSession(sess)) { SEN_HILOGE("AddSession fail"); -- Gitee From f61e941561eaea707f987e8ec809355f486be5e3 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 18 Apr 2023 03:32:45 +0000 Subject: [PATCH 06/22] update Signed-off-by: hui1975 Change-Id: I1e7563c60ed68f6c79c3eed49629c28430506f47 --- .../sensor/include/sensor_service_proxy.h | 9 ++++- .../sensor/src/sensor_service_proxy.cpp | 37 ++++++++++++++++++- services/sensor/BUILD.gn | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index 01bfde3e..b652c538 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.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 @@ -35,6 +35,13 @@ public: ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; + ErrCode SuspendSensors(int32_t pid) override; + ErrCode ResumeSensors(int32_t pid) override; + ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; + ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) override; + ErrCode DestroySocketChannel(sptr sensorClient) override; + ErrCode EnableActiveInfoCB() override; + ErrCode DisableActiveInfoCB() override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 4cd49108..b9b9819b 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -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 @@ -190,5 +190,40 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien } return static_cast(ret); } + +ErrCode SensorServiceProxy::SuspendSensors(int32_t pid) +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::ResumeSensors(int32_t pid) +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::CreateSocketChannel(sptr sensorClient, int32_t &clientFd) +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::DestroySocketChannel(sptr sensorClient) +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::EnableActiveInfoCB() +{ + return SUCCESS; +} + +ErrCode SensorServiceProxy::DisableActiveInfoCB() +{ + return SUCCESS; +} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 6356df68..63762c03 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -30,6 +30,7 @@ ohos_shared_library("libsensor_service") { "src/sensor_power_policy.cpp", "src/sensor_service.cpp", "src/sensor_service_stub.cpp", + "src/stream_server.cpp", ] include_dirs = [ -- Gitee From 10b61782431c0763ba5d144f6c9da16b2e4d77a9 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 18 Apr 2023 03:43:25 +0000 Subject: [PATCH 07/22] update Signed-off-by: hui1975 Change-Id: I5005e4619788be360a7442f03e9f0255f36b6a5e --- services/sensor/src/sensor_service_stub.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 699bf1e6..597d201b 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -254,22 +254,12 @@ ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, Messag ErrCode SensorServiceStub::EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) { - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - return EnableActiveInfoCB(); + return SUCCESS; } ErrCode SensorServiceStub::DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) { - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - return DisableActiveInfoCB(); + return SUCCESS; } } // namespace Sensors } // namespace OHOS -- Gitee From eee8f13ac986d96f329929832866531192dd1b24 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 18 Apr 2023 09:49:34 +0000 Subject: [PATCH 08/22] update Signed-off-by: hui1975 Change-Id: Idb7c07eeaab23340b0a7c67f2c80a58c88c9c2c7 --- frameworks/native/sensor/src/sensor_service_proxy.cpp | 5 ----- services/sensor/src/flush_info_record.cpp | 5 ----- services/sensor/src/sensor_service.cpp | 5 ----- 3 files changed, 15 deletions(-) diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index b9b9819b..3273a6eb 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -29,11 +29,6 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceProxy" }; constexpr int32_t MAX_SENSOR_COUNT = 200; -enum { - FLUSH = 0, - SET_MODE, - RESERVED, -}; } // namespace SensorServiceProxy::SensorServiceProxy(const sptr &impl) : IRemoteProxy(impl) diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index ec19f8f9..4953bb12 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -24,11 +24,6 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "FlushInfoRecord" }; constexpr int32_t CHANNEL_NO_FLUSH = -1; -enum { - FLUSH = 0, - SET_MODE, - RESERVED, -}; } // namespace std::unordered_map> FlushInfoRecord::GetFlushInfo() diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index e83f7f0d..8d65fce2 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -40,11 +40,6 @@ constexpr int32_t INVALID_SENSOR_ID = -1; constexpr int32_t INVALID_PID = -1; constexpr int64_t MAX_EVENT_COUNT = 1000; std::atomic_bool g_isRegister = false; -enum { - FLUSH = 0, - SET_MODE, - RESERVED, -}; } // namespace REGISTER_SYSTEM_ABILITY_BY_ID(SensorService, SENSOR_SERVICE_ABILITY_ID, true); -- Gitee From 2428d622c8333ebf923150f23c9ca2ca3f1e6655 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 20 Apr 2023 12:05:43 +0000 Subject: [PATCH 09/22] update Signed-off-by: hui1975 Change-Id: I6e1a9be2b4d1f5c2a2160a640211e9ed2c684cd9 --- services/sensor/include/client_info.h | 2 +- services/sensor/include/stream_server.h | 3 +- services/sensor/src/client_info.cpp | 2 +- services/sensor/src/sensor_service.cpp | 8 ++-- services/sensor/src/sensor_service_stub.cpp | 49 +++++++++++++-------- services/sensor/src/stream_server.cpp | 10 ++--- utils/ipc/src/stream_buffer.cpp | 3 -- 7 files changed, 44 insertions(+), 33 deletions(-) diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index bf89eaaf..c6c020e3 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -79,7 +79,7 @@ public: int32_t AddActiveInfoCBPid(int32_t pid); int32_t DelActiveInfoCBPid(int32_t pid); std::vector GetActiveInfoCBPid(); - bool IsUnregisterClientDeathRecipient(int32_t pid); + bool CallingService(int32_t pid); int32_t GetPidByTokenId(AccessTokenID tokenId); void UpdatePermState(int32_t pid, int32_t sensorId, bool state); void ChangeSensorPerm(AccessTokenID tokenId, const std::string &permName, bool state); diff --git a/services/sensor/include/stream_server.h b/services/sensor/include/stream_server.h index ee32c2da..05b1a8b7 100644 --- a/services/sensor/include/stream_server.h +++ b/services/sensor/include/stream_server.h @@ -39,9 +39,8 @@ public: protected: bool AddSession(SessionPtr ses); void DelSession(int32_t pid); - std::mutex idxPidMutex_; - std::map idxPidMap_; std::mutex sessionMutex_; + std::map idxPidMap_; std::map sessionsMap_; }; } // namespace Sensors diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 495848bc..d02a3a26 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -725,7 +725,7 @@ std::vector ClientInfo::GetActiveInfoCBPid() return activeInfoCBPids; } -bool ClientInfo::IsUnregisterClientDeathRecipient(int32_t pid) +bool ClientInfo::CallingService(int32_t pid) { std::lock_guard channelLock(channelMutex_); auto channelIt = channelMap_.find(pid); diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 8d65fce2..e63a6440 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -386,8 +386,8 @@ void SensorService::UnregisterClientDeathRecipient(sptr sensorCli SEN_HILOGE("Pid is invalid"); return; } - if (!clientInfo_.IsUnregisterClientDeathRecipient(pid)) { - SEN_HILOGD("Client call other service, can not unregister client death recipient"); + if (!clientInfo_.CallingService(pid)) { + SEN_HILOGD("Cannot unregister client death recipient"); return; } sptr client = iface_cast(sensorClient); @@ -454,6 +454,7 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a ErrCode SensorService::CreateSocketChannel(sptr sensorClient, int32_t &clientFd) { CALL_LOG_ENTER; + CHKPR(sensorClient, INVALID_POINTER); int32_t serverFd = -1; int32_t ret = AddSocketPairInfo(GetCallingUid(), GetCallingPid(), AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()), @@ -469,6 +470,7 @@ ErrCode SensorService::CreateSocketChannel(sptr sensorClient, int ErrCode SensorService::DestroySocketChannel(sptr sensorClient) { CALL_LOG_ENTER; + CHKPR(sensorClient, INVALID_POINTER); DelSession(GetCallingPid()); UnregisterClientDeathRecipient(sensorClient); return ERR_OK; @@ -493,7 +495,7 @@ void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) CALL_LOG_ENTER; std::vector sessionList; auto pidList = clientInfo_.GetActiveInfoCBPid(); - for (auto pid : pidList) { + for (const auto &pid : pidList) { auto sess = GetSessionByPid(pid); if (sess != nullptr) { sessionList.push_back(sess); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 597d201b..42d877cf 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -170,8 +170,8 @@ ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParce (void)reply; int32_t pid; if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read failed"); - return ERROR; + SEN_HILOGE("Parcel read pid failed"); + return READ_PARCEL_ERR; } return SuspendSensors(pid); } @@ -186,8 +186,8 @@ ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel (void)reply; int32_t pid; if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read failed"); - return ERROR; + SEN_HILOGE("Parcel read pid failed"); + return READ_PARCEL_ERR; } return ResumeSensors(pid); } @@ -201,8 +201,8 @@ ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessagePa } int32_t pid; if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read failed"); - return ERROR; + SEN_HILOGE("Parcel read pid failed"); + return READ_PARCEL_ERR; } std::vector activeInfoList; int32_t ret = GetActiveInfoList(pid, activeInfoList); @@ -210,12 +210,15 @@ ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessagePa SEN_HILOGE("Get activeInfo list failed"); return ret; } - int32_t activeInfoCount = int32_t { activeInfoList.size() }; - reply.WriteInt32(activeInfoCount); - for (int32_t i = 0; i < activeInfoCount; i++) { + size_t activeInfoCount = activeInfoList.size(); + if (!reply.WriteUint32(activeInfoCount)) { + SEN_HILOGE("Parcel write activeInfoCount failed"); + return WRITE_PARCEL_ERR; + } + for (size_t i = 0; i < activeInfoCount; i++) { if (!activeInfoList[i].Marshalling(reply)) { - SEN_HILOGE("ActiveInfo %{public}d failed", i); - return ERROR; + SEN_HILOGE("ActiveInfo %{public}zu marshalling failed", i); + return WRITE_PARCEL_ERR; } } return ERR_OK; @@ -224,7 +227,7 @@ ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessagePa ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, MessageParcel &reply) { sptr sensorClient = data.ReadRemoteObject(); - CHKPR(sensorClient, OBJECT_NULL); + CHKPR(sensorClient, INVALID_POINTER); int32_t clientFd = -1; int32_t ret = CreateSocketChannel(sensorClient, clientFd); if (ret != ERR_OK) { @@ -232,9 +235,9 @@ ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, Message return ret; } if (!reply.WriteFileDescriptor(clientFd)) { - SEN_HILOGE("Write file descriptor failed"); + SEN_HILOGE("Parcel write file descriptor failed"); close(clientFd); - return ERROR; + return WRITE_PARCEL_ERR; } close(clientFd); return ERR_OK; @@ -243,10 +246,10 @@ ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, Message ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, MessageParcel &reply) { sptr sensorClient = data.ReadRemoteObject(); - CHKPR(sensorClient, OBJECT_NULL); + CHKPR(sensorClient, INVALID_POINTER); int32_t ret = DestroySocketChannel(sensorClient); if (ret != ERR_OK) { - SEN_HILOGE("DestroySocketChannel failed"); + SEN_HILOGE("Destroy socket channel failed"); return ret; } return ERR_OK; @@ -254,12 +257,22 @@ ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, Messag ErrCode SensorServiceStub::EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) { - return SUCCESS; + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + return EnableActiveInfoCB(); } ErrCode SensorServiceStub::DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) { - return SUCCESS; + PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); + if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + SEN_HILOGE("TokenType is not TOKEN_NATIVE"); + return PERMISSION_DENIED; + } + return DisableActiveInfoCB(); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index ab59a8d3..ec9a7efa 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -16,6 +16,7 @@ #include "stream_server.h" #include + #include #include "sensors_errors.h" @@ -31,6 +32,7 @@ constexpr int32_t INVALID_FD = -1; StreamServer::~StreamServer() { CALL_LOG_ENTER; + std::lock_guard sessionLock(sessionMutex_); idxPidMap_.clear(); for (const auto &item : sessionsMap_) { item.second->Close(); @@ -63,7 +65,7 @@ void StreamServer::Multicast(const std::vector& fdList, const NetPacket int32_t StreamServer::GetClientFd(int32_t pid) { - std::lock_guard idxPidLock(idxPidMutex_); + std::lock_guard sessionLock(sessionMutex_); auto it = idxPidMap_.find(pid); if (it == idxPidMap_.end()) { return INVALID_FD; @@ -172,9 +174,8 @@ bool StreamServer::AddSession(SessionPtr ses) return false; } DelSession(pid); - std::lock_guard idxPidLock(idxPidMutex_); - idxPidMap_[pid] = fd; std::lock_guard sessionLock(sessionMutex_); + idxPidMap_[pid] = fd; sessionsMap_[fd] = ses; return true; } @@ -182,14 +183,13 @@ bool StreamServer::AddSession(SessionPtr ses) void StreamServer::DelSession(int32_t pid) { CALL_LOG_ENTER; - std::lock_guard idxPidLock(idxPidMutex_); + std::lock_guard sessionLock(sessionMutex_); auto pidIt = idxPidMap_.find(pid); if (pidIt == idxPidMap_.end()) { return; } int32_t fd = pidIt->second; idxPidMap_.erase(pidIt); - std::lock_guard sessionLock(sessionMutex_); auto fdIt = sessionsMap_.find(fd); if (fdIt != sessionsMap_.end()) { sessionsMap_.erase(fdIt); diff --git a/utils/ipc/src/stream_buffer.cpp b/utils/ipc/src/stream_buffer.cpp index 6b9fe1de..e666585c 100644 --- a/utils/ipc/src/stream_buffer.cpp +++ b/utils/ipc/src/stream_buffer.cpp @@ -15,9 +15,6 @@ #include "stream_buffer.h" -#include -#include - namespace OHOS { namespace Sensors { StreamBuffer::StreamBuffer(const StreamBuffer &buf) -- Gitee From 84348fbcd6fc3ef96d494f9adb18e7c5ba2093c5 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 20 Apr 2023 12:29:12 +0000 Subject: [PATCH 10/22] update Signed-off-by: hui1975 Change-Id: I0926b6ca31a2da66b70d80e3999477920987af6e --- services/sensor/src/sensor_service.cpp | 2 -- services/sensor/src/sensor_service_stub.cpp | 22 --------------------- 2 files changed, 24 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index e63a6440..f5bbbc79 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -20,7 +20,6 @@ #include #include -#include "accesstoken_kit.h" #include "hisysevent.h" #include "iservice_registry.h" #include "permission_util.h" @@ -33,7 +32,6 @@ namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; -using namespace Security::AccessToken; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorService" }; constexpr int32_t INVALID_SENSOR_ID = -1; diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 42d877cf..a79419ee 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -47,8 +47,6 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[GET_ACTIVE_INFO_LIST] = &SensorServiceStub::GetActiveInfoListInner; baseFuncs_[CREATE_SOCKET_CHANNEL] = &SensorServiceStub::CreateSocketChannelInner; baseFuncs_[DESTROY_SOCKET_CHANNEL] = &SensorServiceStub::DestroySocketChannelInner; - baseFuncs_[ENABLE_ACTIVE_INFO_CB] = &SensorServiceStub::EnableActiveInfoCBInner; - baseFuncs_[DISABLE_ACTIVE_INFO_CB] = &SensorServiceStub::DisableActiveInfoCBInner; } SensorServiceStub::~SensorServiceStub() @@ -254,25 +252,5 @@ ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, Messag } return ERR_OK; } - -ErrCode SensorServiceStub::EnableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) -{ - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - return EnableActiveInfoCB(); -} - -ErrCode SensorServiceStub::DisableActiveInfoCBInner(MessageParcel &data, MessageParcel &reply) -{ - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - return DisableActiveInfoCB(); -} } // namespace Sensors } // namespace OHOS -- Gitee From fcebddc1d03bcd5a8dc9d6e5595b4680d91b5924 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 20 Apr 2023 12:51:10 +0000 Subject: [PATCH 11/22] update Signed-off-by: hui1975 Change-Id: I38a83008945501d11f445a30d8e9499dd87bc55f --- services/sensor/src/sensor_service.cpp | 2 +- services/sensor/src/sensor_service_stub.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index f5bbbc79..b125383c 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -455,7 +455,7 @@ ErrCode SensorService::CreateSocketChannel(sptr sensorClient, int CHKPR(sensorClient, INVALID_POINTER); int32_t serverFd = -1; int32_t ret = AddSocketPairInfo(GetCallingUid(), GetCallingPid(), - AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()), + AccessTokenKit::GetTokenTypeFlag(GetCallingTokenID()), serverFd, std::ref(clientFd)); if (ret != ERR_OK) { SEN_HILOGE("Add socket pair info failed, ret:%{public}d", ret); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index a79419ee..5ce19860 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -161,7 +161,7 @@ ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageP ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply) { PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { SEN_HILOGE("TokenType is not TOKEN_NATIVE"); return PERMISSION_DENIED; } @@ -177,7 +177,7 @@ ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParce ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply) { PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { SEN_HILOGE("TokenType is not TOKEN_NATIVE"); return PERMISSION_DENIED; } @@ -193,7 +193,7 @@ ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessageParcel &reply) { PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if(!permissionUtil.IsNativeToken(GetCallingTokenID())) { + if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { SEN_HILOGE("TokenType is not TOKEN_NATIVE"); return PERMISSION_DENIED; } -- Gitee From fb43abdc87f2d1a88b69caea492a3a7b1ae7f873 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 21 Apr 2023 09:19:43 +0000 Subject: [PATCH 12/22] update Signed-off-by: hui1975 Change-Id: Ie0d6016aaf574f3f76d1c73e48d6f7583a1aee58 --- .../native/sensor/include/i_sensor_service.h | 10 -- .../sensor/include/sensor_service_proxy.h | 5 - .../sensor/src/sensor_service_proxy.cpp | 92 +++++----- services/sensor/include/sensor_service.h | 10 +- services/sensor/src/sensor_service_stub.cpp | 110 ++---------- services/sensor/src/stream_server.cpp | 12 +- utils/common/include/sensor_parcel.h | 160 ++++++++++++++++++ 7 files changed, 224 insertions(+), 175 deletions(-) create mode 100644 utils/common/include/sensor_parcel.h diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index 7404ad58..5c51e4fc 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -42,11 +42,6 @@ public: virtual ErrCode DestroySensorChannel(sptr sensorClient) = 0; virtual ErrCode SuspendSensors(int32_t pid) = 0; virtual ErrCode ResumeSensors(int32_t pid) = 0; - virtual ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) = 0; - virtual ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) = 0; - virtual ErrCode DestroySocketChannel(sptr sensorClient) = 0; - virtual ErrCode EnableActiveInfoCB() = 0; - virtual ErrCode DisableActiveInfoCB() = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -57,11 +52,6 @@ public: DESTROY_SENSOR_CHANNEL, SUSPEND_SENSORS, RESUME_SENSORS, - GET_ACTIVE_INFO_LIST, - CREATE_SOCKET_CHANNEL, - DESTROY_SOCKET_CHANNEL, - ENABLE_ACTIVE_INFO_CB, - DISABLE_ACTIVE_INFO_CB, }; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index b652c538..e3944af7 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -37,11 +37,6 @@ public: ErrCode DestroySensorChannel(sptr sensorClient) override; ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; - ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; - ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) override; - ErrCode DestroySocketChannel(sptr sensorClient) override; - ErrCode EnableActiveInfoCB() override; - ErrCode DisableActiveInfoCB() override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 3273a6eb..80ecd094 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -20,6 +20,7 @@ #include "hisysevent.h" #include "message_parcel.h" #include "sensor_client_proxy.h" +#include "sensor_parcel.h" #include "sensors_errors.h" namespace OHOS { @@ -43,18 +44,9 @@ ErrCode SensorServiceProxy::EnableSensor(int32_t sensorId, int64_t samplingPerio SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - if (!data.WriteInt32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_PARCEL_ERR; - } - if (!data.WriteInt64(samplingPeriodNs)) { - SEN_HILOGE("write samplingPeriodNs failed"); - return WRITE_PARCEL_ERR; - } - if (!data.WriteInt64(maxReportDelayNs)) { - SEN_HILOGE("write maxReportDelayNs failed"); - return WRITE_PARCEL_ERR; - } + WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); + WRITEINT64(data, samplingPeriodNs, WRITE_PARCEL_ERR); + WRITEINT64(data, maxReportDelayNs, WRITE_PARCEL_ERR); sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::ENABLE_SENSOR, data, reply, option); @@ -75,10 +67,7 @@ ErrCode SensorServiceProxy::DisableSensor(int32_t sensorId) SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - if (!data.WriteInt32(sensorId)) { - SEN_HILOGE("write sensorId failed"); - return WRITE_PARCEL_ERR; - } + WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::DISABLE_SENSOR, data, reply, option); @@ -145,10 +134,7 @@ ErrCode SensorServiceProxy::TransferDataChannel(const sptrSendToBinder(data); - if (!data.WriteRemoteObject(sensorClient)) { - SEN_HILOGE("sensorClient failed"); - return WRITE_PARCEL_ERR; - } + WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR); sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::TRANSFER_DATA_CHANNEL, data, reply, option); @@ -171,10 +157,7 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - if (!data.WriteRemoteObject(sensorClient)) { - SEN_HILOGE("write sensorClient failed"); - return WRITE_PARCEL_ERR; - } + WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR); sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::DESTROY_SENSOR_CHANNEL, data, reply, option); @@ -188,37 +171,44 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien ErrCode SensorServiceProxy::SuspendSensors(int32_t pid) { - return SUCCESS; + MessageParcel data; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("Parcel write descriptor failed"); + return WRITE_PARCEL_ERR; + } + WRITEINT32(data, pid, WRITE_PARCEL_ERR); + sptr remote = Remote(); + CHKPR(remote, ERROR); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ISensorService::SUSPEND_SENSORS, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "SuspendSensors", "ERROR_CODE", ret); + SEN_HILOGE("Failed, ret:%{public}d", ret); + } + return static_cast(ret); } ErrCode SensorServiceProxy::ResumeSensors(int32_t pid) { - return SUCCESS; -} - -ErrCode SensorServiceProxy::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) -{ - return SUCCESS; -} - -ErrCode SensorServiceProxy::CreateSocketChannel(sptr sensorClient, int32_t &clientFd) -{ - return SUCCESS; -} - -ErrCode SensorServiceProxy::DestroySocketChannel(sptr sensorClient) -{ - return SUCCESS; -} - -ErrCode SensorServiceProxy::EnableActiveInfoCB() -{ - return SUCCESS; -} - -ErrCode SensorServiceProxy::DisableActiveInfoCB() -{ - return SUCCESS; + MessageParcel data; + if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { + SEN_HILOGE("Parcel write descriptor failed"); + return WRITE_PARCEL_ERR; + } + WRITEINT32(data, pid, WRITE_PARCEL_ERR); + sptr remote = Remote(); + CHKPR(remote, ERROR); + MessageParcel reply; + MessageOption option; + int32_t ret = remote->SendRequest(ISensorService::RESUME_SENSORS, data, reply, option); + if (ret != NO_ERROR) { + HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", + HiSysEvent::EventType::FAULT, "PKG_NAME", "ResumeSensors", "ERROR_CODE", ret); + SEN_HILOGE("Failed, ret:%{public}d", ret); + } + return static_cast(ret); } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index dff3c41a..11399a69 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -58,11 +58,11 @@ public: void ProcessDeathObserver(const wptr &object); ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; - ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; - ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd) override; - ErrCode DestroySocketChannel(sptr sensorClient) override; - ErrCode EnableActiveInfoCB() override; - ErrCode DisableActiveInfoCB() override; + ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList); + ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd); + ErrCode DestroySocketChannel(sptr sensorClient); + ErrCode EnableActiveInfoCB(); + ErrCode DisableActiveInfoCB(); private: DISALLOW_COPY_AND_MOVE(SensorService); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 5ce19860..7996b08d 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -25,6 +25,7 @@ #include "message_parcel.h" #include "permission_util.h" #include "sensor_client_proxy.h" +#include "sensor_parcel.h" #include "sensors_errors.h" namespace OHOS { @@ -44,9 +45,6 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; - baseFuncs_[GET_ACTIVE_INFO_LIST] = &SensorServiceStub::GetActiveInfoListInner; - baseFuncs_[CREATE_SOCKET_CHANNEL] = &SensorServiceStub::CreateSocketChannelInner; - baseFuncs_[DESTROY_SOCKET_CHANNEL] = &SensorServiceStub::DestroySocketChannelInner; } SensorServiceStub::~SensorServiceStub() @@ -80,24 +78,19 @@ ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel { (void)reply; int32_t sensorId; - if (!data.ReadInt32(sensorId)) { - SEN_HILOGE("Parcel read failed"); - return ERROR; - } + READINT32(data, sensorId, READ_PARCEL_ERR); PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL", HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorEnableInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); + SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } int64_t samplingPeriodNs; int64_t maxReportDelayNs; - if ((!data.ReadInt64(samplingPeriodNs)) || (!data.ReadInt64(maxReportDelayNs))) { - SEN_HILOGE("Parcel read failed"); - return ERROR; - } + READINT64(data, samplingPeriodNs, READ_PARCEL_ERR); + READINT64(data, maxReportDelayNs, READ_PARCEL_ERR); return EnableSensor(sensorId, samplingPeriodNs, maxReportDelayNs); } @@ -105,16 +98,13 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel { (void)reply; int32_t sensorId; - if (!data.ReadInt32(sensorId)) { - SEN_HILOGE("Parcel read failed"); - return ERROR; - } + READINT32(data, sensorId, READ_PARCEL_ERR); PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL", HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorDisableInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); + SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } return DisableSensor(sensorId); @@ -123,13 +113,12 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply) { (void)data; - std::vector sensors(GetSensorList()); - int32_t sensorCount = int32_t { sensors.size() }; - reply.WriteInt32(sensorCount); - for (int32_t i = 0; i < sensorCount; i++) { - bool flag = sensors[i].Marshalling(reply); - if (!flag) { - SEN_HILOGE("sensor %{public}d failed", i); + std::vector sensors = GetSensorList(); + int32_t sensorCount = sensors.size(); + WRITEINT32(reply, sensorCount, WRITE_PARCEL_ERR); + for (int32_t i = 0; i < sensorCount; ++i) { + if (!sensors[i].Marshalling(reply)) { + SEN_HILOGE("Sensor %{public}d marshalling failed", i); return GET_SENSOR_LIST_ERR; } } @@ -167,10 +156,7 @@ ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParce } (void)reply; int32_t pid; - if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read pid failed"); - return READ_PARCEL_ERR; - } + READINT32(data, pid, READ_PARCEL_ERR); return SuspendSensors(pid); } @@ -183,74 +169,8 @@ ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel } (void)reply; int32_t pid; - if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read pid failed"); - return READ_PARCEL_ERR; - } + READINT32(data, pid, READ_PARCEL_ERR); return ResumeSensors(pid); } - -ErrCode SensorServiceStub::GetActiveInfoListInner(MessageParcel &data, MessageParcel &reply) -{ - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - int32_t pid; - if (!data.ReadInt32(pid)) { - SEN_HILOGE("Parcel read pid failed"); - return READ_PARCEL_ERR; - } - std::vector activeInfoList; - int32_t ret = GetActiveInfoList(pid, activeInfoList); - if (ret != ERR_OK) { - SEN_HILOGE("Get activeInfo list failed"); - return ret; - } - size_t activeInfoCount = activeInfoList.size(); - if (!reply.WriteUint32(activeInfoCount)) { - SEN_HILOGE("Parcel write activeInfoCount failed"); - return WRITE_PARCEL_ERR; - } - for (size_t i = 0; i < activeInfoCount; i++) { - if (!activeInfoList[i].Marshalling(reply)) { - SEN_HILOGE("ActiveInfo %{public}zu marshalling failed", i); - return WRITE_PARCEL_ERR; - } - } - return ERR_OK; -} - -ErrCode SensorServiceStub::CreateSocketChannelInner(MessageParcel &data, MessageParcel &reply) -{ - sptr sensorClient = data.ReadRemoteObject(); - CHKPR(sensorClient, INVALID_POINTER); - int32_t clientFd = -1; - int32_t ret = CreateSocketChannel(sensorClient, clientFd); - if (ret != ERR_OK) { - SEN_HILOGE("Create socket channel failed"); - return ret; - } - if (!reply.WriteFileDescriptor(clientFd)) { - SEN_HILOGE("Parcel write file descriptor failed"); - close(clientFd); - return WRITE_PARCEL_ERR; - } - close(clientFd); - return ERR_OK; -} - -ErrCode SensorServiceStub::DestroySocketChannelInner(MessageParcel &data, MessageParcel &reply) -{ - sptr sensorClient = data.ReadRemoteObject(); - CHKPR(sensorClient, INVALID_POINTER); - int32_t ret = DestroySocketChannel(sensorClient); - if (ret != ERR_OK) { - SEN_HILOGE("Destroy socket channel failed"); - return ret; - } - return ERR_OK; -} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index ec9a7efa..a679ddf9 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -67,20 +67,14 @@ int32_t StreamServer::GetClientFd(int32_t pid) { std::lock_guard sessionLock(sessionMutex_); auto it = idxPidMap_.find(pid); - if (it == idxPidMap_.end()) { - return INVALID_FD; - } - return it->second; + return it == idxPidMap_.end() ? INVALID_FD : it->second; } int32_t StreamServer::GetClientPid(int32_t fd) { std::lock_guard sessionLock(sessionMutex_); auto it = sessionsMap_.find(fd); - if (it == sessionsMap_.end()) { - return INVALID_PID; - } - return it->second->GetPid(); + return it == sessionsMap_.end() ? INVALID_PID : it->second->GetPid(); } SessionPtr StreamServer::GetSession(int32_t fd) @@ -197,7 +191,7 @@ void StreamServer::DelSession(int32_t pid) if (fd >= 0) { auto rf = close(fd); if (rf != 0) { - SEN_HILOGE("Socket fd close failed, rf:%{public}d", rf); + SEN_HILOGE("Socket fd close failed, rf:%{public}d, errno:%{public}d", rf, errno); } } } diff --git a/utils/common/include/sensor_parcel.h b/utils/common/include/sensor_parcel.h new file mode 100644 index 00000000..f2aaa065 --- /dev/null +++ b/utils/common/include/sensor_parcel.h @@ -0,0 +1,160 @@ +/* + * 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 SENSOR_PARCEL_H +#define SENSOR_PARCEL_H + +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { + +#define WRITEBOOL(parcel, data, ...) \ + do { \ + if (!(parcel).WriteBool(data)) { \ + SEN_HILOGE("Parcel writeBool "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEINT32(parcel, data, ...) \ + do { \ + if (!(parcel).WriteInt32(data)) { \ + SEN_HILOGE("Parcel writeInt32 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEINT64(parcel, data, ...) \ + do { \ + if (!(parcel).WriteInt64(data)) { \ + SEN_HILOGE("Parcel writeInt64 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEUINT32(parcel, data, ...) \ + do { \ + if (!(parcel).WriteUint32(data)) { \ + SEN_HILOGE("Parcel writeUint32 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEDOUBLE(parcel, data, ...) \ + do { \ + if (!(parcel).WriteDouble(data)) { \ + SEN_HILOGE("Parcel writeDouble "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITESTRING(parcel, data, ...) \ + do { \ + if (!(parcel).WriteString(data)) { \ + SEN_HILOGE("Parcel writeString "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITESTRING16(parcel, data, ...) \ + do { \ + if (!(parcel).WriteString16(data)) { \ + SEN_HILOGE("Parcel writeString16 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEREMOTEOBJECT(parcel, data, ...) \ + do { \ + if (!(parcel).WriteRemoteObject(data)) { \ + SEN_HILOGE("Parcel writeRemoteObject "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define WRITEUINT8VECTOR(parcel, data, ...) \ + do { \ + if (!(parcel).WriteUInt8Vector(data)) { \ + SEN_HILOGE("Parcel writeUInt8Vector "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READBOOL(parcel, data, ...) \ + do { \ + if (!(parcel).ReadBool(data)) { \ + SEN_HILOGE("Parcel readBool "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READINT32(parcel, data, ...) \ + do { \ + if (!(parcel).ReadInt32(data)) { \ + SEN_HILOGE("Parcel readInt32 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READINT64(parcel, data, ...) \ + do { \ + if (!(parcel).ReadInt64(data)) { \ + SEN_HILOGE("Parcel readInt64 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READUINT32(parcel, data, ...) \ + do { \ + if (!(parcel).ReadUint32(data)) { \ + SEN_HILOGE("Parcel readUint32 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READDOUBLE(parcel, data, ...) \ + do { \ + if (!(parcel).ReadDouble(data)) { \ + SEN_HILOGE("Parcel readDouble "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READSTRING(parcel, data, ...) \ + do { \ + if (!(parcel).ReadString(data)) { \ + SEN_HILOGE("Parcel readString "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READUINT8VECTOR(parcel, data, ...) \ + do { \ + if (!(parcel).ReadUInt8Vector(&data)) { \ + SEN_HILOGE("Parcel readUInt8Vector "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) + +#define READSTRING16(parcel, data, ...) \ + do { \ + if (!(parcel).ReadString16(data)) { \ + SEN_HILOGE("Parcel readString16 "#data" failed"); \ + return __VA_ARGS__; \ + } \ + } while (0) +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_PARCEL_H \ No newline at end of file -- Gitee From 8874cd8e1e7aee146568fbfc990756e5355c8066 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 21 Apr 2023 09:32:13 +0000 Subject: [PATCH 13/22] update Signed-off-by: hui1975 Change-Id: Ib938eaac6b655e382ffc265457c7cc7761a75726 --- .../native/sensor/include/i_sensor_service.h | 4 -- .../sensor/include/sensor_service_proxy.h | 2 - .../sensor/src/sensor_service_proxy.cpp | 42 ------------------- services/sensor/include/sensor_service.h | 4 +- services/sensor/src/sensor_service_stub.cpp | 28 ------------- 5 files changed, 2 insertions(+), 78 deletions(-) diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index 5c51e4fc..bde488dd 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -40,8 +40,6 @@ public: virtual ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) = 0; virtual ErrCode DestroySensorChannel(sptr sensorClient) = 0; - virtual ErrCode SuspendSensors(int32_t pid) = 0; - virtual ErrCode ResumeSensors(int32_t pid) = 0; enum { ENABLE_SENSOR = 0, DISABLE_SENSOR, @@ -50,8 +48,6 @@ public: GET_SENSOR_LIST, TRANSFER_DATA_CHANNEL, DESTROY_SENSOR_CHANNEL, - SUSPEND_SENSORS, - RESUME_SENSORS, }; }; } // namespace Sensors diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index e3944af7..af7d0391 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -35,8 +35,6 @@ public: ErrCode TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; - ErrCode SuspendSensors(int32_t pid) override; - ErrCode ResumeSensors(int32_t pid) override; private: DISALLOW_COPY_AND_MOVE(SensorServiceProxy); diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 80ecd094..5582a3a2 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -168,47 +168,5 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien } return static_cast(ret); } - -ErrCode SensorServiceProxy::SuspendSensors(int32_t pid) -{ - MessageParcel data; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("Parcel write descriptor failed"); - return WRITE_PARCEL_ERR; - } - WRITEINT32(data, pid, WRITE_PARCEL_ERR); - sptr remote = Remote(); - CHKPR(remote, ERROR); - MessageParcel reply; - MessageOption option; - int32_t ret = remote->SendRequest(ISensorService::SUSPEND_SENSORS, data, reply, option); - if (ret != NO_ERROR) { - HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "SuspendSensors", "ERROR_CODE", ret); - SEN_HILOGE("Failed, ret:%{public}d", ret); - } - return static_cast(ret); -} - -ErrCode SensorServiceProxy::ResumeSensors(int32_t pid) -{ - MessageParcel data; - if (!data.WriteInterfaceToken(SensorServiceProxy::GetDescriptor())) { - SEN_HILOGE("Parcel write descriptor failed"); - return WRITE_PARCEL_ERR; - } - WRITEINT32(data, pid, WRITE_PARCEL_ERR); - sptr remote = Remote(); - CHKPR(remote, ERROR); - MessageParcel reply; - MessageOption option; - int32_t ret = remote->SendRequest(ISensorService::RESUME_SENSORS, data, reply, option); - if (ret != NO_ERROR) { - HiSysEventWrite(HiSysEvent::Domain::SENSOR, "SENSOR_SERVICE_IPC_EXCEPTION", - HiSysEvent::EventType::FAULT, "PKG_NAME", "ResumeSensors", "ERROR_CODE", ret); - SEN_HILOGE("Failed, ret:%{public}d", ret); - } - return static_cast(ret); -} } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 11399a69..25347017 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -56,8 +56,8 @@ public: const sptr &sensorClient) override; ErrCode DestroySensorChannel(sptr sensorClient) override; void ProcessDeathObserver(const wptr &object); - ErrCode SuspendSensors(int32_t pid) override; - ErrCode ResumeSensors(int32_t pid) override; + ErrCode SuspendSensors(int32_t pid); + ErrCode ResumeSensors(int32_t pid); ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList); ErrCode CreateSocketChannel(sptr sensorClient, int32_t &clientFd); ErrCode DestroySocketChannel(sptr sensorClient); diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 7996b08d..37d4d6ca 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -43,8 +43,6 @@ SensorServiceStub::SensorServiceStub() baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner; baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner; baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner; - baseFuncs_[SUSPEND_SENSORS] = &SensorServiceStub::SuspendSensorsInner; - baseFuncs_[RESUME_SENSORS] = &SensorServiceStub::ResumeSensorsInner; } SensorServiceStub::~SensorServiceStub() @@ -146,31 +144,5 @@ ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageP CHKPR(sensorClient, OBJECT_NULL); return DestroySensorChannel(sensorClient); } - -ErrCode SensorServiceStub::SuspendSensorsInner(MessageParcel &data, MessageParcel &reply) -{ - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - (void)reply; - int32_t pid; - READINT32(data, pid, READ_PARCEL_ERR); - return SuspendSensors(pid); -} - -ErrCode SensorServiceStub::ResumeSensorsInner(MessageParcel &data, MessageParcel &reply) -{ - PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); - if (!permissionUtil.IsNativeToken(GetCallingTokenID())) { - SEN_HILOGE("TokenType is not TOKEN_NATIVE"); - return PERMISSION_DENIED; - } - (void)reply; - int32_t pid; - READINT32(data, pid, READ_PARCEL_ERR); - return ResumeSensors(pid); -} } // namespace Sensors } // namespace OHOS -- Gitee From 356286fbd77c535d31fb6887e69a8ac07a43d35c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 21 Apr 2023 09:38:17 +0000 Subject: [PATCH 14/22] update Signed-off-by: hui1975 Change-Id: I2f89965a5ede7ce073630012237ac97b4621e80b --- .../native/sensor/include/i_sensor_service.h | 3 +- .../sensor/src/sensor_service_proxy.cpp | 38 +++++++++++++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/frameworks/native/sensor/include/i_sensor_service.h b/frameworks/native/sensor/include/i_sensor_service.h index bde488dd..dab25cb4 100755 --- a/frameworks/native/sensor/include/i_sensor_service.h +++ b/frameworks/native/sensor/include/i_sensor_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,7 +21,6 @@ #include "errors.h" #include "iremote_broker.h" -#include "active_info.h" #include "i_sensor_client.h" #include "sensor_basic_data_channel.h" #include "sensor.h" diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index 5582a3a2..4cd49108 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,7 +20,6 @@ #include "hisysevent.h" #include "message_parcel.h" #include "sensor_client_proxy.h" -#include "sensor_parcel.h" #include "sensors_errors.h" namespace OHOS { @@ -30,6 +29,11 @@ using namespace OHOS::HiviewDFX; namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceProxy" }; constexpr int32_t MAX_SENSOR_COUNT = 200; +enum { + FLUSH = 0, + SET_MODE, + RESERVED, +}; } // namespace SensorServiceProxy::SensorServiceProxy(const sptr &impl) : IRemoteProxy(impl) @@ -44,9 +48,18 @@ ErrCode SensorServiceProxy::EnableSensor(int32_t sensorId, int64_t samplingPerio SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); - WRITEINT64(data, samplingPeriodNs, WRITE_PARCEL_ERR); - WRITEINT64(data, maxReportDelayNs, WRITE_PARCEL_ERR); + if (!data.WriteInt32(sensorId)) { + SEN_HILOGE("write sensorId failed"); + return WRITE_PARCEL_ERR; + } + if (!data.WriteInt64(samplingPeriodNs)) { + SEN_HILOGE("write samplingPeriodNs failed"); + return WRITE_PARCEL_ERR; + } + if (!data.WriteInt64(maxReportDelayNs)) { + SEN_HILOGE("write maxReportDelayNs failed"); + return WRITE_PARCEL_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::ENABLE_SENSOR, data, reply, option); @@ -67,7 +80,10 @@ ErrCode SensorServiceProxy::DisableSensor(int32_t sensorId) SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - WRITEINT32(data, sensorId, WRITE_PARCEL_ERR); + if (!data.WriteInt32(sensorId)) { + SEN_HILOGE("write sensorId failed"); + return WRITE_PARCEL_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::DISABLE_SENSOR, data, reply, option); @@ -134,7 +150,10 @@ ErrCode SensorServiceProxy::TransferDataChannel(const sptrSendToBinder(data); - WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR); + if (!data.WriteRemoteObject(sensorClient)) { + SEN_HILOGE("sensorClient failed"); + return WRITE_PARCEL_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::TRANSFER_DATA_CHANNEL, data, reply, option); @@ -157,7 +176,10 @@ ErrCode SensorServiceProxy::DestroySensorChannel(sptr sensorClien SEN_HILOGE("write descriptor failed"); return WRITE_PARCEL_ERR; } - WRITEREMOTEOBJECT(data, sensorClient, WRITE_PARCEL_ERR); + if (!data.WriteRemoteObject(sensorClient)) { + SEN_HILOGE("write sensorClient failed"); + return WRITE_PARCEL_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); int32_t ret = remote->SendRequest(ISensorService::DESTROY_SENSOR_CHANNEL, data, reply, option); -- Gitee From 4c70df7ae3538f328fc127275f8104ce342a9adb Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 21 Apr 2023 09:51:53 +0000 Subject: [PATCH 15/22] update Signed-off-by: hui1975 Change-Id: Ia86b8a261a7d9f9ae96f80752e30078c53fceee4 --- .../sensor/include/sensor_service_proxy.h | 2 +- services/sensor/src/sensor_service_stub.cpp | 37 ++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/frameworks/native/sensor/include/sensor_service_proxy.h b/frameworks/native/sensor/include/sensor_service_proxy.h index af7d0391..01bfde3e 100755 --- a/frameworks/native/sensor/include/sensor_service_proxy.h +++ b/frameworks/native/sensor/include/sensor_service_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index 37d4d6ca..94c2a8af 100755 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -25,12 +25,12 @@ #include "message_parcel.h" #include "permission_util.h" #include "sensor_client_proxy.h" -#include "sensor_parcel.h" #include "sensors_errors.h" namespace OHOS { namespace Sensors { using namespace OHOS::HiviewDFX; + namespace { constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceStub" }; } // namespace @@ -76,19 +76,24 @@ ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel { (void)reply; int32_t sensorId; - READINT32(data, sensorId, READ_PARCEL_ERR); + if (!data.ReadInt32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL", HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorEnableInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret); + SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } int64_t samplingPeriodNs; int64_t maxReportDelayNs; - READINT64(data, samplingPeriodNs, READ_PARCEL_ERR); - READINT64(data, maxReportDelayNs, READ_PARCEL_ERR); + if ((!data.ReadInt64(samplingPeriodNs)) || (!data.ReadInt64(maxReportDelayNs))) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } return EnableSensor(sensorId, samplingPeriodNs, maxReportDelayNs); } @@ -96,13 +101,16 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel { (void)reply; int32_t sensorId; - READINT32(data, sensorId, READ_PARCEL_ERR); + if (!data.ReadInt32(sensorId)) { + SEN_HILOGE("Parcel read failed"); + return ERROR; + } PermissionUtil &permissionUtil = PermissionUtil::GetInstance(); int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId); if (ret != PERMISSION_GRANTED) { HiSysEventWrite(HiSysEvent::Domain::SENSOR, "VERIFY_ACCESS_TOKEN_FAIL", HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorDisableInner", "ERROR_CODE", ret); - SEN_HILOGE("sensorId:%{public}d grant failed, result:%{public}d", sensorId, ret); + SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret); return PERMISSION_DENIED; } return DisableSensor(sensorId); @@ -111,12 +119,13 @@ ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply) { (void)data; - std::vector sensors = GetSensorList(); - int32_t sensorCount = sensors.size(); - WRITEINT32(reply, sensorCount, WRITE_PARCEL_ERR); - for (int32_t i = 0; i < sensorCount; ++i) { - if (!sensors[i].Marshalling(reply)) { - SEN_HILOGE("Sensor %{public}d marshalling failed", i); + std::vector sensors(GetSensorList()); + int32_t sensorCount = int32_t { sensors.size() }; + reply.WriteInt32(sensorCount); + for (int32_t i = 0; i < sensorCount; i++) { + bool flag = sensors[i].Marshalling(reply); + if (!flag) { + SEN_HILOGE("sensor %{public}d failed", i); return GET_SENSOR_LIST_ERR; } } -- Gitee From 2b12181ead69f95a31c34f057be790c65340c178 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 24 Apr 2023 03:13:26 +0000 Subject: [PATCH 16/22] update Signed-off-by: hui1975 Change-Id: Ic1bc64ba7bb41cb4bfc30e0228a36131db55c38c --- services/sensor/src/stream_server.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index a679ddf9..ffecd5a3 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -111,25 +111,25 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT serverFd = sockFds[0]; clientFd = sockFds[1]; if (serverFd < 0 || clientFd < 0) { - SEN_HILOGE("Call fcntl failed, errno:%{public}d", errno); + SEN_HILOGE("Call socketpair failed, errno:%{public}d", errno); return ERROR; } static constexpr size_t bufferSize = 32 * 1024; SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt serverFd buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(serverFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt serverFd failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt serverFd buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt clientFd buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt clientFd buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } sess = std::make_shared("", serverFd, uid, pid); -- Gitee From 2e2f11f0a5aea175b0c3a7b41f801afd6ff9cf3c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 8 May 2023 07:12:10 +0000 Subject: [PATCH 17/22] update Signed-off-by: hui1975 Change-Id: I99e16bd24abd6e7377a7f42c3b8353157b737e1b --- services/sensor/include/sensor_service.h | 3 +-- services/sensor/include/stream_server.h | 2 +- services/sensor/src/sensor_service.cpp | 10 ++++--- services/sensor/src/stream_server.cpp | 34 ++++++++++++------------ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 25347017..27ad3785 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -98,13 +98,12 @@ private: SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); - SensorPowerPolicy &sensorPowerPolicy_ = SensorPowerPolicy::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; std::mutex uidLock_; // death recipient of sensor client sptr clientDeathObserver_ = nullptr; - std::shared_ptr permStateChangeCb_; + std::shared_ptr permStateChangeCb_ = nullptr; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; }; diff --git a/services/sensor/include/stream_server.h b/services/sensor/include/stream_server.h index 05b1a8b7..f0cf5302 100644 --- a/services/sensor/include/stream_server.h +++ b/services/sensor/include/stream_server.h @@ -37,7 +37,7 @@ public: int32_t AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenType, int32_t &serverFd, int32_t &clientFd); protected: - bool AddSession(SessionPtr ses); + bool AddSession(SessionPtr sess); void DelSession(int32_t pid); std::mutex sessionMutex_; std::map idxPidMap_; diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index b125383c..de889a6f 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -40,6 +40,8 @@ constexpr int64_t MAX_EVENT_COUNT = 1000; std::atomic_bool g_isRegister = false; } // namespace +#define PowerPolicy SensorPowerPolicy::GetInstance() + REGISTER_SYSTEM_ABILITY_BY_ID(SensorService, SENSOR_SERVICE_ABILITY_ID, true); SensorService::SensorService(int32_t systemAbilityId, bool runOnCreate) @@ -425,7 +427,7 @@ ErrCode SensorService::SuspendSensors(int32_t pid) SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - return sensorPowerPolicy_.SuspendSensors(pid); + return PowerPolicy.SuspendSensors(pid); } ErrCode SensorService::ResumeSensors(int32_t pid) @@ -435,7 +437,7 @@ ErrCode SensorService::ResumeSensors(int32_t pid) SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - return sensorPowerPolicy_.ResumeSensors(pid); + return PowerPolicy.ResumeSensors(pid); } ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) @@ -445,7 +447,7 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - activeInfoList = sensorPowerPolicy_.GetActiveInfoList(pid); + activeInfoList = PowerPolicy.GetActiveInfoList(pid); return ERR_OK; } @@ -502,7 +504,7 @@ void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs()); - sensorPowerPolicy_.ReportActiveInfo(activeInfo, sessionList); + PowerPolicy.ReportActiveInfo(activeInfo, sessionList); } bool SensorService::RegisterPermCallback(int32_t sensorId) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index ffecd5a3..51b1dbc0 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -47,12 +47,12 @@ bool StreamServer::SendMsg(int32_t fd, const NetPacket& pkt) SEN_HILOGE("Fd is invalid"); return false; } - auto ses = GetSession(fd); - if (ses == nullptr) { - SEN_HILOGE("Fd not found, The message was discarded."); + auto sess = GetSession(fd); + if (sess == nullptr) { + SEN_HILOGE("sess is nullptr"); return false; } - return ses->SendMsg(pkt); + return sess->SendMsg(pkt); } void StreamServer::Multicast(const std::vector& fdList, const NetPacket& pkt) @@ -117,19 +117,19 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT static constexpr size_t bufferSize = 32 * 1024; SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt serverFd buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(serverFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt serverFd buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt clientFd buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } sess = std::make_shared("", serverFd, uid, pid); @@ -148,29 +148,28 @@ CLOSE_SOCK: return ERROR; } -bool StreamServer::AddSession(SessionPtr ses) +bool StreamServer::AddSession(SessionPtr sess) { CALL_LOG_ENTER; - CHKPF(ses); - auto fd = ses->GetFd(); + CHKPF(sess); + auto fd = sess->GetFd(); if (fd < 0) { SEN_HILOGE("Fd is Invalid"); return false; } - auto pid = ses->GetPid(); + auto pid = sess->GetPid(); if (pid <= 0) { - SEN_HILOGE("Get process failed"); + SEN_HILOGE("Pid is invalid"); return false; } + std::lock_guard sessionLock(sessionMutex_); if (sessionsMap_.size() > MAX_SESSON_ALARM) { SEN_HILOGE("Too many clients. Warning Value:%{public}zu, Current Value:%{public}zu", MAX_SESSON_ALARM, sessionsMap_.size()); return false; } - DelSession(pid); - std::lock_guard sessionLock(sessionMutex_); idxPidMap_[pid] = fd; - sessionsMap_[fd] = ses; + sessionsMap_[fd] = sess; return true; } @@ -180,6 +179,7 @@ void StreamServer::DelSession(int32_t pid) std::lock_guard sessionLock(sessionMutex_); auto pidIt = idxPidMap_.find(pid); if (pidIt == idxPidMap_.end()) { + SEN_HILOGW("Pid session not exist"); return; } int32_t fd = pidIt->second; @@ -189,7 +189,7 @@ void StreamServer::DelSession(int32_t pid) sessionsMap_.erase(fdIt); } if (fd >= 0) { - auto rf = close(fd); + int32_t rf = close(fd); if (rf != 0) { SEN_HILOGE("Socket fd close failed, rf:%{public}d, errno:%{public}d", rf, errno); } -- Gitee From e9c20bb508a5d4a343fcc92885a9bb04d7555a6c Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 8 May 2023 08:42:01 +0000 Subject: [PATCH 18/22] update Signed-off-by: hui1975 Change-Id: Ia15648c91df8915624e3ae4dffd297e0ba4b680f --- services/sensor/src/stream_server.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index 51b1dbc0..95eddf68 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -105,31 +105,31 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT CALL_LOG_ENTER; int32_t sockFds[2] = { -1 }; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockFds) != 0) { - SEN_HILOGE("Call socketpair failed, errno:%{public}d", errno); + SEN_HILOGE("Socketpair failed, errno:%{public}d", errno); return ERROR; } serverFd = sockFds[0]; clientFd = sockFds[1]; if (serverFd < 0 || clientFd < 0) { - SEN_HILOGE("Call socketpair failed, errno:%{public}d", errno); + SEN_HILOGE("ServerFd or clientFd is invalid"); return ERROR; } static constexpr size_t bufferSize = 32 * 1024; SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt send buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt serverFd send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(serverFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt recv buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt serverFd recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt send buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt clientFd send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } if (setsockopt(clientFd, SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)) != 0) { - SEN_HILOGE("Setsockopt recv buffer size failed, errno: %{public}d", errno); + SEN_HILOGE("Setsockopt clientFd recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } sess = std::make_shared("", serverFd, uid, pid); @@ -164,8 +164,7 @@ bool StreamServer::AddSession(SessionPtr sess) } std::lock_guard sessionLock(sessionMutex_); if (sessionsMap_.size() > MAX_SESSON_ALARM) { - SEN_HILOGE("Too many clients. Warning Value:%{public}zu, Current Value:%{public}zu", - MAX_SESSON_ALARM, sessionsMap_.size()); + SEN_HILOGE("Too many clients, size:%{public}zu", sessionsMap_.size()); return false; } idxPidMap_[pid] = fd; -- Gitee From 75f8633da219c04fa6399954d80a238ffe3d373b Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 8 May 2023 11:03:23 +0000 Subject: [PATCH 19/22] update Signed-off-by: hui1975 Change-Id: I9fc79899b4870c5efe62338d36e4c8d7a105341e --- services/sensor/include/sensor_service.h | 2 ++ services/sensor/src/sensor_service.cpp | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 27ad3785..7a9a9257 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -107,6 +107,8 @@ private: ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; }; + +#define PowerPolicy SensorPowerPolicy::GetInstance() } // namespace Sensors } // namespace OHOS #endif // SENSOR_SERVICE_H diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index de889a6f..08770df0 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -40,8 +40,6 @@ constexpr int64_t MAX_EVENT_COUNT = 1000; std::atomic_bool g_isRegister = false; } // namespace -#define PowerPolicy SensorPowerPolicy::GetInstance() - REGISTER_SYSTEM_ABILITY_BY_ID(SensorService, SENSOR_SERVICE_ABILITY_ID, true); SensorService::SensorService(int32_t systemAbilityId, bool runOnCreate) -- Gitee From 68951e5aa3783b85b95b4e18ab463e74e6d52efa Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 8 May 2023 12:21:32 +0000 Subject: [PATCH 20/22] update Signed-off-by: hui1975 Change-Id: Ie7d9a983bb32f20a8d0d836e8380afcabbcb6136 --- services/sensor/src/stream_server.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index 95eddf68..d626649a 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -142,9 +142,7 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT CLOSE_SOCK: close(serverFd); - serverFd = -1; close(clientFd); - clientFd = -1; return ERROR; } @@ -188,8 +186,8 @@ void StreamServer::DelSession(int32_t pid) sessionsMap_.erase(fdIt); } if (fd >= 0) { - int32_t rf = close(fd); - if (rf != 0) { + int32_t ret = close(fd); + if (ret != 0) { SEN_HILOGE("Socket fd close failed, rf:%{public}d, errno:%{public}d", rf, errno); } } -- Gitee From beadbd62287c50ea2fa0f95187aeb693bf5f832a Mon Sep 17 00:00:00 2001 From: hui1975 Date: Mon, 8 May 2023 14:02:23 +0000 Subject: [PATCH 21/22] update Signed-off-by: hui1975 Change-Id: Ide87bf25c24f2c8a2c1cdd42273ab4c728ce74e9 --- services/sensor/src/stream_server.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index d626649a..0ddd514a 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -115,7 +115,6 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT return ERROR; } static constexpr size_t bufferSize = 32 * 1024; - SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { SEN_HILOGE("Setsockopt serverFd send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; @@ -132,7 +131,7 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT SEN_HILOGE("Setsockopt clientFd recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } - sess = std::make_shared("", serverFd, uid, pid); + SessionPtr sess = std::make_shared("", serverFd, uid, pid); sess->SetTokenType(tokenType); if (!AddSession(sess)) { SEN_HILOGE("AddSession fail"); -- Gitee From 58fd766ba6579221a55bb00d11602f3479c92338 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Tue, 9 May 2023 02:23:21 +0000 Subject: [PATCH 22/22] update Signed-off-by: hui1975 Change-Id: Ic13b05b93830612b2da767d6537432eda5a2c5ea --- services/sensor/include/sensor_service.h | 2 +- services/sensor/src/sensor_service.cpp | 8 ++++---- services/sensor/src/stream_server.cpp | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index 7a9a9257..2c949b55 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -108,7 +108,7 @@ private: std::atomic_bool isReportActiveInfo_ = false; }; -#define PowerPolicy SensorPowerPolicy::GetInstance() +#define POWER_POLICY SensorPowerPolicy::GetInstance() } // namespace Sensors } // namespace OHOS #endif // SENSOR_SERVICE_H diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 08770df0..7df0bb93 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -425,7 +425,7 @@ ErrCode SensorService::SuspendSensors(int32_t pid) SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - return PowerPolicy.SuspendSensors(pid); + return POWER_POLICY.SuspendSensors(pid); } ErrCode SensorService::ResumeSensors(int32_t pid) @@ -435,7 +435,7 @@ ErrCode SensorService::ResumeSensors(int32_t pid) SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - return PowerPolicy.ResumeSensors(pid); + return POWER_POLICY.ResumeSensors(pid); } ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &activeInfoList) @@ -445,7 +445,7 @@ ErrCode SensorService::GetActiveInfoList(int32_t pid, std::vector &a SEN_HILOGE("Pid is invalid"); return CLIENT_PID_INVALID_ERR; } - activeInfoList = PowerPolicy.GetActiveInfoList(pid); + activeInfoList = POWER_POLICY.GetActiveInfoList(pid); return ERR_OK; } @@ -502,7 +502,7 @@ void SensorService::ReportActiveInfo(int32_t sensorId, int32_t pid) SensorBasicInfo sensorInfo = clientInfo_.GetCurPidSensorInfo(sensorId, pid); ActiveInfo activeInfo(pid, sensorId, sensorInfo.GetSamplingPeriodNs(), sensorInfo.GetMaxReportDelayNs()); - PowerPolicy.ReportActiveInfo(activeInfo, sessionList); + POWER_POLICY.ReportActiveInfo(activeInfo, sessionList); } bool SensorService::RegisterPermCallback(int32_t sensorId) diff --git a/services/sensor/src/stream_server.cpp b/services/sensor/src/stream_server.cpp index 0ddd514a..a749cbdd 100644 --- a/services/sensor/src/stream_server.cpp +++ b/services/sensor/src/stream_server.cpp @@ -115,6 +115,7 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT return ERROR; } static constexpr size_t bufferSize = 32 * 1024; + SessionPtr sess = nullptr; if (setsockopt(serverFd, SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)) != 0) { SEN_HILOGE("Setsockopt serverFd send buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; @@ -131,7 +132,7 @@ int32_t StreamServer::AddSocketPairInfo(int32_t uid, int32_t pid, int32_t tokenT SEN_HILOGE("Setsockopt clientFd recv buffer size failed, errno: %{public}d", errno); goto CLOSE_SOCK; } - SessionPtr sess = std::make_shared("", serverFd, uid, pid); + sess = std::make_shared("", serverFd, uid, pid); sess->SetTokenType(tokenType); if (!AddSession(sess)) { SEN_HILOGE("AddSession fail"); @@ -187,7 +188,7 @@ void StreamServer::DelSession(int32_t pid) if (fd >= 0) { int32_t ret = close(fd); if (ret != 0) { - SEN_HILOGE("Socket fd close failed, rf:%{public}d, errno:%{public}d", rf, errno); + SEN_HILOGE("Socket fd close failed, ret:%{public}d, errno:%{public}d", ret, errno); } } } -- Gitee