diff --git a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp index 06c444312a7e5853fa7199c062f7625424c9af2d..b24a95a6135f97502dd21b75d6eca911e45d52f7 100644 --- a/frameworks/native/sensor/src/my_file_descriptor_listener.cpp +++ b/frameworks/native/sensor/src/my_file_descriptor_listener.cpp @@ -32,9 +32,7 @@ MyFileDescriptorListener::MyFileDescriptorListener() channel_ = nullptr; receiveDataBuff_ = new (std::nothrow) TransferSensorEvents[sizeof(struct TransferSensorEvents) * RECEIVE_DATA_SIZE]; - if (receiveDataBuff_ == nullptr) { - SEN_HILOGE("receiveDataBuff_ memory request failed"); - } + CHKPL(receiveDataBuff_); } MyFileDescriptorListener::~MyFileDescriptorListener() diff --git a/frameworks/native/sensor/src/sensor_agent_proxy.cpp b/frameworks/native/sensor/src/sensor_agent_proxy.cpp index afb5bafc1885793068caeb09221b4a2a9b5a6e3d..a87511f47265f3013b5dea3ca4533dffa9d540d6 100644 --- a/frameworks/native/sensor/src/sensor_agent_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_agent_proxy.cpp @@ -92,22 +92,19 @@ const SensorAgentProxy *SensorAgentProxy::GetSensorsObj() void SensorAgentProxy::HandleSensorData(struct SensorEvent *events, int32_t num, void *data) { - if (events == nullptr || num <= 0) { + CHKPV(events); + if (num <= 0) { SEN_HILOGE("events is null or num is invalid"); return; } struct SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - CHKPV(eventStream.data); if (g_subscribeMap.find(eventStream.sensorTypeId) == g_subscribeMap.end()) { SEN_HILOGE("sensorTypeId not in g_subscribeMap"); return; } - if (g_subscribeMap[eventStream.sensorTypeId] == nullptr) { - SEN_HILOGE("sensor user is null"); - return; - } + CHKPV(g_subscribeMap[eventStream.sensorTypeId]); g_subscribeMap[eventStream.sensorTypeId]->callback(&eventStream); } } @@ -163,18 +160,20 @@ int32_t SensorAgentProxy::DestroySensorDataChannel() const int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *user) const { - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); - return OHOS::Sensors::ERROR; + return ERROR; } if (g_samplingInterval < 0 || g_reportInterval < 0) { SEN_HILOGE("samplingPeroid or g_reportInterval is invalid"); - return OHOS::Sensors::ERROR; + return ERROR; } std::lock_guard subscribeLock(subscribeMutex_); if ((g_subscribeMap.find(sensorId) == g_subscribeMap.end()) || (g_subscribeMap[sensorId] != user)) { SEN_HILOGE("subscribe sensorId first"); - return OHOS::Sensors::ERROR; + return ERROR; } SensorServiceClient &client = SensorServiceClient::GetInstance(); int32_t ret = client.EnableSensor(sensorId, g_samplingInterval, g_reportInterval); @@ -191,7 +190,9 @@ int32_t SensorAgentProxy::ActivateSensor(int32_t sensorId, const SensorUser *use int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *user) const { - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -214,7 +215,8 @@ int32_t SensorAgentProxy::DeactivateSensor(int32_t sensorId, const SensorUser *u int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval) const { - if (user == nullptr || sensorId < 0) { + CHKPR(user, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -235,7 +237,9 @@ int32_t SensorAgentProxy::SetBatch(int32_t sensorId, const SensorUser *user, int int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *user) const { SEN_HILOGI("in, sensorId: %{public}d", sensorId); - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -252,7 +256,9 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) const { SEN_HILOGI("in, sensorId: %{public}d", sensorId); - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -274,7 +280,9 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) const { - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -288,7 +296,9 @@ int32_t SensorAgentProxy::SetMode(int32_t sensorId, const SensorUser *user, int3 int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, int32_t option) const { - if (user == nullptr || sensorId < 0 || user->callback == nullptr) { + CHKPR(user, OHOS::Sensors::ERROR); + CHKPR(user->callback, OHOS::Sensors::ERROR); + if (sensorId < 0) { SEN_HILOGE("user is null or sensorId is invalid"); return OHOS::Sensors::ERROR; } @@ -302,10 +312,8 @@ int32_t SensorAgentProxy::SetOption(int32_t sensorId, const SensorUser *user, in int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const { - if (sensorInfo == nullptr || count == nullptr) { - SEN_HILOGE("sensorInfo or count is null"); - return OHOS::Sensors::ERROR; - } + CHKPR(sensorInfo, OHOS::Sensors::ERROR); + CHKPR(count, OHOS::Sensors::ERROR); SensorServiceClient &client = SensorServiceClient::GetInstance(); std::vector sensorList_ = client.GetSensorList(); if (sensorList_.empty()) { diff --git a/frameworks/native/sensor/src/sensor_data_channel.cpp b/frameworks/native/sensor/src/sensor_data_channel.cpp index ccbd8bfc6f37a0978ba5a8efaa85daaf50cccc0e..fc96e209ea7d0102e9f35c964e292ddd11efc883 100755 --- a/frameworks/native/sensor/src/sensor_data_channel.cpp +++ b/frameworks/native/sensor/src/sensor_data_channel.cpp @@ -45,10 +45,7 @@ constexpr uint32_t STOP_EVENT_ID = 0; int32_t SensorDataChannel::CreateSensorDataChannel(DataChannelCB callBack, void *data) { - if (callBack == nullptr) { - SEN_HILOGE("callBack cannot be null"); - return SENSOR_NATIVE_REGSITER_CB_ERR; - } + CHKPR(callBack, SENSOR_NATIVE_REGSITER_CB_ERR); dataCB_ = callBack; privateData_ = data; return InnerSensorDataChannel(); @@ -56,10 +53,7 @@ int32_t SensorDataChannel::CreateSensorDataChannel(DataChannelCB callBack, void int32_t SensorDataChannel::RestoreSensorDataChannel() { - if (dataCB_ == nullptr) { - SEN_HILOGE("dataCB_ cannot be null"); - return SENSOR_CHANNEL_RESTORE_CB_ERR; - } + CHKPR(dataCB_, SENSOR_NATIVE_REGSITER_CB_ERR); if (GetReceiveDataFd() != -1) { SEN_HILOGE("fd not close"); return SENSOR_CHANNEL_RESTORE_FD_ERR; @@ -79,15 +73,9 @@ int32_t SensorDataChannel::InnerSensorDataChannel() auto listener = std::make_shared(); listener->SetChannel(this); auto myRunner = AppExecFwk::EventRunner::Create(true); - if (myRunner == nullptr) { - SEN_HILOGE("myRunner is null"); - return ERROR; - } + CHKPR(myRunner, ERROR); auto handler = std::make_shared(myRunner); - if (handler == nullptr) { - SEN_HILOGE("handler is null"); - return ERROR; - } + CHKPR(handler, ERROR); int32_t receiveFd = GetReceiveDataFd(); auto inResult = handler->AddFileDescriptorListener(receiveFd, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener); if (inResult != 0) { @@ -114,10 +102,8 @@ int32_t SensorDataChannel::InnerSensorDataChannel() int32_t SensorDataChannel::DestroySensorDataChannel() { std::lock_guard eventRunnerLock(eventRunnerMutex_); - if (eventHandler_ == nullptr || eventRunner_ == nullptr) { - SEN_HILOGE("handler or eventRunner is null"); - return ERROR; - } + CHKPR(eventHandler_, ERROR); + CHKPR(eventRunner_, ERROR); int32_t receiveFd = GetReceiveDataFd(); eventHandler_->RemoveFileDescriptorListener(receiveFd); eventHandler_ = nullptr; diff --git a/frameworks/native/sensor/src/sensor_service_client.cpp b/frameworks/native/sensor/src/sensor_service_client.cpp index dbc904604c349c33f3616a599f723ca55d086585..92fbb2ba0eb996a81afebde50b6782a278502958 100755 --- a/frameworks/native/sensor/src/sensor_service_client.cpp +++ b/frameworks/native/sensor/src/sensor_service_client.cpp @@ -50,10 +50,7 @@ int32_t SensorServiceClient::InitServiceClient() sensorClientStub_ = new (std::nothrow) SensorClientStub(); } auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - SEN_HILOGE("systemAbilityManager cannot be null"); - return SENSOR_NATIVE_SAM_ERR; - } + CHKPR(systemAbilityManager, SENSOR_NATIVE_SAM_ERR); int32_t retry = 0; while (retry < GET_SERVICE_MAX_COUNT) { sensorServer_ = iface_cast(systemAbilityManager->GetSystemAbility(SENSOR_SERVICE_ABILITY_ID)); @@ -188,10 +185,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object { CALL_LOG_ENTER; (void)object; - if (dataChannel_ == nullptr) { - SEN_HILOGE("dataChannel_ cannot be null"); - return; - } + CHKPL(dataChannel_); // STEP1 : Destroy revious data channel dataChannel_->DestroySensorDataChannel(); diff --git a/frameworks/native/sensor/src/sensor_service_proxy.cpp b/frameworks/native/sensor/src/sensor_service_proxy.cpp index b6dad630defa3093858d87a735413413e7259231..459cca58633444004ec43608193fdb72e18fdb60 100755 --- a/frameworks/native/sensor/src/sensor_service_proxy.cpp +++ b/frameworks/native/sensor/src/sensor_service_proxy.cpp @@ -180,10 +180,8 @@ std::vector SensorServiceProxy::GetSensorList() ErrCode SensorServiceProxy::TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) { - if (sensorBasicDataChannel == nullptr || sensorClient == nullptr) { - SEN_HILOGE("sensorBasicDataChannel or sensorClient cannot be null"); - return OBJECT_NULL; - } + CHKPR(sensorBasicDataChannel, OBJECT_NULL); + CHKPR(sensorClient, OBJECT_NULL); MessageParcel data; MessageParcel reply; MessageOption option; @@ -207,10 +205,7 @@ ErrCode SensorServiceProxy::TransferDataChannel(const sptr sensorClient) { - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return OBJECT_NULL; - } + CHKPR(sensorClient, OBJECT_NULL); MessageParcel data; MessageParcel reply; MessageOption option; diff --git a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp index a0a9aa4bee8ffb777ed7d2802c94bed3370b1970..b4e0b79859981d49ff1d3ee12a4ae7a4ae18352f 100644 --- a/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/compatible_connection.cpp @@ -124,14 +124,13 @@ int32_t CompatibleConnection::SetOption(int32_t sensorId, int32_t option) int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *event) { - if ((event == nullptr) || (event->dataLen == 0)) { + CHKPR(event, ERR_INVALID_VALUE); + if ((event->dataLen == 0)) { SEN_HILOGE("event is NULL"); return ERR_INVALID_VALUE; } - if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { - SEN_HILOGE("reportDataCb_ cannot be null"); - return ERR_NO_INIT; - } + CHKPR(reportDataCallback_, ERR_NO_INIT); + CHKPR(reportDataCb_, ERR_NO_INIT); struct SensorEvent sensorEvent = { .sensorTypeId = event->sensorId, @@ -155,10 +154,7 @@ int32_t CompatibleConnection::SensorDataCallback(const struct SensorEvents *even int32_t CompatibleConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) { - if (reportDataCallback == nullptr) { - SEN_HILOGE("failed, reportDataCallback cannot be null"); - return ERR_INVALID_VALUE; - } + CHKPR(reportDataCallback, ERR_INVALID_VALUE); int32_t ret = hdiServiceImpl_.Register(SensorDataCallback); if (ret < 0) { SEN_HILOGE("Register is failed"); diff --git a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp index 31b844ae11c9b666834d5f308cb0ecc17b4d407b..8c07da00c971549f3f14d4472c564105414bf02a 100644 --- a/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp +++ b/services/sensor/hdi_connection/adapter/src/hdi_connection.cpp @@ -51,10 +51,7 @@ int32_t HdiConnection::ConnectHdi() if (sensorInterface_ != nullptr) { SEN_HILOGI("connect v1_0 hdi success"); eventCallback_ = new (std::nothrow) SensorEventCallback(); - if (eventCallback_ == nullptr) { - SEN_HILOGE("failed to initialize eventCallback"); - return ERR_NO_INIT; - } + CHKPR(eventCallback_, ERR_NO_INIT); RegisterHdiDeathRecipient(); return ERR_OK; } @@ -69,10 +66,7 @@ int32_t HdiConnection::ConnectHdi() int32_t HdiConnection::GetSensorList(std::vector& sensorList) { CALL_LOG_ENTER; - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); std::vector sensorInfos; int32_t ret = sensorInterface_->GetAllSensorInfo(sensorInfos); if (ret != 0) { @@ -97,10 +91,7 @@ int32_t HdiConnection::GetSensorList(std::vector& sensorList) int32_t HdiConnection::EnableSensor(int32_t sensorId) { - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->Enable(sensorId); if (ret < 0) { SEN_HILOGE("connect v1_0 hdi failed"); @@ -112,10 +103,7 @@ int32_t HdiConnection::EnableSensor(int32_t sensorId) int32_t HdiConnection::DisableSensor(int32_t sensorId) { - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->Disable(sensorId); if (ret < 0) { SEN_HILOGE("Disable is failed"); @@ -127,10 +115,7 @@ int32_t HdiConnection::DisableSensor(int32_t sensorId) int32_t HdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) { - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->SetBatch(sensorId, samplingInterval, reportInterval); if (ret < 0) { SEN_HILOGE("SetBatch is failed"); @@ -143,10 +128,7 @@ int32_t HdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int6 int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) { CALL_LOG_ENTER; - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->SetMode(sensorId, mode); if (ret < 0) { SEN_HILOGE("SetMode is failed"); @@ -158,10 +140,7 @@ int32_t HdiConnection::SetMode(int32_t sensorId, int32_t mode) int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) { CALL_LOG_ENTER; - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return ERR_NO_INIT; - } + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->SetOption(sensorId, option); if (ret < 0) { SEN_HILOGE("SetOption is failed"); @@ -173,10 +152,8 @@ int32_t HdiConnection::SetOption(int32_t sensorId, int32_t option) int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptr reportDataCallback) { CALL_LOG_ENTER; - if (reportDataCallback == nullptr || sensorInterface_ == nullptr) { - SEN_HILOGE("failed, reportDataCallback or sensorInterface_ cannot be null"); - return ERR_NO_INIT; - } + CHKPR(reportDataCallback, ERR_NO_INIT); + CHKPR(sensorInterface_, ERR_NO_INIT); int32_t ret = sensorInterface_->Register(0, eventCallback_); if (ret < 0) { SEN_HILOGE("Register is failed"); @@ -190,10 +167,7 @@ int32_t HdiConnection::RegisteDataReport(ZReportDataCb cb, sptrUnregister(0, eventCallback_); if (ret < 0) { SEN_HILOGE("Unregister is failed"); @@ -259,25 +233,17 @@ void HdiConnection::deleteSensorBasicInfoState(int32_t sensorId) void HdiConnection::RegisterHdiDeathRecipient() { CALL_LOG_ENTER; - if (sensorInterface_ == nullptr) { - SEN_HILOGE("connect v1_0 hdi failed"); - return; - } + CHKPV(sensorInterface_); hdiDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); - if (hdiDeathObserver_ == nullptr) { - SEN_HILOGE("hdiDeathObserver_ cannot be null"); - return; - } + CHKPV(hdiDeathObserver_); sensorInterface_->AsObject()->AddDeathRecipient(hdiDeathObserver_); } void HdiConnection::UnregisterHdiDeathRecipient() { CALL_LOG_ENTER; - if (sensorInterface_ == nullptr || hdiDeathObserver_ == nullptr) { - SEN_HILOGE("sensorInterface_ or hdiDeathObserver_ is null"); - return; - } + CHKPV(sensorInterface_); + CHKPV(hdiDeathObserver_); sensorInterface_->AsObject()->RemoveDeathRecipient(hdiDeathObserver_); } @@ -285,10 +251,7 @@ void HdiConnection::ProcessDeathObserver(const wptr &object) { CALL_LOG_ENTER; sptr hdiService = object.promote(); - if (hdiService == nullptr) { - SEN_HILOGE("invalid remote object"); - return; - } + CHKPV(hdiService); hdiService->RemoveDeathRecipient(hdiDeathObserver_); eventCallback_ = nullptr; reconnect(); diff --git a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp index 719c7973b1637ec4c7f68e5809bc8b1a549ca7ba..587331fb9fe4e251aa0409a4ec3c8f03478b0209 100644 --- a/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/sensor/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -29,10 +29,8 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents& event) { ZReportDataCb reportDataCb_ = HdiConnection_->getReportDataCb(); sptr reportDataCallback_ = HdiConnection_->getReportDataCallback(); - if (reportDataCb_ == nullptr || reportDataCallback_ == nullptr) { - SEN_HILOGE("reportDataCb_ or reportDataCallback_ cannot be null"); - return ERR_NO_INIT; - } + CHKPR(reportDataCb_, ERR_NO_INIT); + CHKPR(reportDataCallback_, ERR_NO_INIT); int32_t dataSize = static_cast(event.data.size()); if (dataSize == 0) { SEN_HILOGI("data is empty"); diff --git a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp index 7a2bb7eb97ba2183a64c6f71e6ef893852d52d5d..3916dc7642556edc733888ebff3ae30ff1fb3b25 100644 --- a/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp +++ b/services/sensor/hdi_connection/hardware/src/hdi_service_impl.cpp @@ -66,10 +66,7 @@ void HdiServiceImpl::DataReportThread() int32_t HdiServiceImpl::EnableSensor(uint32_t sensorId) { CALL_LOG_ENTER; - if (g_callback == nullptr) { - SEN_HILOGE("enable sensor failed"); - return -1; - } + CHKPR(g_callback, ERROR); if (std::find(supportSensors.begin(), supportSensors.end(), sensorId) == supportSensors.end()) { SEN_HILOGE("not support enable sensorId: %{public}d", sensorId); return ERR_NO_INIT; @@ -143,10 +140,7 @@ int32_t HdiServiceImpl::SetOption(int32_t sensorId, uint32_t option) int32_t HdiServiceImpl::Register(RecordDataCallback cb) { - if (cb == nullptr) { - SEN_HILOGE("cb cannot be null"); - return -1; - } + CHKPR(cb, ERROR); g_callback = cb; return ERR_OK; } diff --git a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp index f3fda2941946e8715fdfaf9edc7c332bdc74c2d8..43306461770d106bd9b4f9c4d7feb3d17d7aa3f7 100644 --- a/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/sensor/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -29,10 +29,7 @@ constexpr HiLogLabel LABEL = { LOG_CORE, SensorsLogDomain::SENSOR_SERVICE, "Sens int32_t SensorHdiConnection::ConnectHdi() { iSensorHdiConnection_ = std::make_unique(); - if (iSensorHdiConnection_ == nullptr) { - SEN_HILOGE("failed, iSensorHdiConnection_ cannot be null"); - return ERROR; - } + CHKPR(iSensorHdiConnection_, ERROR); int32_t ret = connectHdiService(); if (ret != ERR_OK) { SEN_HILOGE("connect hdi service failed, try to connect compatible connection"); diff --git a/services/sensor/src/client_info.cpp b/services/sensor/src/client_info.cpp index 3576e3c69ce29d17b2b94ab0b60bac11e5882f45..3b99d8738667c63394ddb8e8f777f6a29cb2ef84 100644 --- a/services/sensor/src/client_info.cpp +++ b/services/sensor/src/client_info.cpp @@ -264,7 +264,8 @@ void ClientInfo::RemoveSubscriber(uint32_t sensorId, uint32_t pid) bool ClientInfo::UpdateSensorChannel(int32_t pid, const sptr &channel) { CALL_LOG_ENTER; - if (pid <= INVALID_PID || channel == nullptr) { + CHKPR(channel, false); + if (pid <= INVALID_PID) { SEN_HILOGE("pid or channel is invalid or channel cannot be null"); return false; } @@ -488,10 +489,7 @@ void ClientInfo::StoreEvent(const struct SensorEvent &event) bool ClientInfo::SaveClientPid(const sptr &sensorClient, int32_t pid) { CALL_LOG_ENTER; - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return false; - } + CHKPF(sensorClient); std::lock_guard lock(clientPidMutex_); auto it = clientPidMap_.find(sensorClient); if (it == clientPidMap_.end()) { @@ -505,10 +503,7 @@ bool ClientInfo::SaveClientPid(const sptr &sensorClient, int32_t int32_t ClientInfo::FindClientPid(const sptr &sensorClient) { CALL_LOG_ENTER; - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return INVALID_PID; - } + CHKPR(sensorClient, INVALID_PID); std::lock_guard lock(clientPidMutex_); auto it = clientPidMap_.find(sensorClient); if (it == clientPidMap_.end()) { @@ -521,10 +516,7 @@ int32_t ClientInfo::FindClientPid(const sptr &sensorClient) void ClientInfo::DestroyClientPid(const sptr &sensorClient) { CALL_LOG_ENTER; - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return; - } + CHKPV(sensorClient); std::lock_guard lock(clientPidMutex_); auto it = clientPidMap_.find(sensorClient); if (it == clientPidMap_.end()) { diff --git a/services/sensor/src/flush_info_record.cpp b/services/sensor/src/flush_info_record.cpp index f76987ff8de2876a43906729c6debc7cf839dc8f..54f7ea1bfd2c471a460a179860775cd81cd78e14 100644 --- a/services/sensor/src/flush_info_record.cpp +++ b/services/sensor/src/flush_info_record.cpp @@ -51,10 +51,7 @@ void FlushInfoRecord::ClearFlushInfoItem(uint32_t sensorId) ErrCode FlushInfoRecord::SetFlushInfo(uint32_t sensorId, const sptr &channel, bool isFirstFlush) { SEN_HILOGD("sensorId : %{public}u", sensorId); - if (channel == nullptr) { - SEN_HILOGE("failed, channel cannot be null"); - return INVALID_POINTER; - } + CHKPR(channel, INVALID_POINTER); struct FlushInfo flush(channel, isFirstFlush); std::lock_guard flushLock(flushInfoMutex_); /* If the sensorId can be found, it indicates that other processes have flushed on this sensor, @@ -103,10 +100,7 @@ ErrCode FlushInfoRecord::FlushProcess(const uint32_t sensorId, const uint32_t fl return ret; } sptr channel = clientInfo_.GetSensorChannelByPid(pid); - if (channel == nullptr) { - SEN_HILOGE("channel cannot be null"); - return ERROR; - } + CHKPR(channel, ERROR); ret = SetFlushInfo(sensorId, channel, false); if (ret != ERR_OK) { SEN_HILOGE("set flush info failed"); diff --git a/services/sensor/src/sensor_data_processer.cpp b/services/sensor/src/sensor_data_processer.cpp index 0a543ffc9527cc78bbbc6d1510a8ff8d2dedaa1a..86e75338df3d4bdc255dff19248a65240c836dfc 100644 --- a/services/sensor/src/sensor_data_processer.cpp +++ b/services/sensor/src/sensor_data_processer.cpp @@ -74,10 +74,7 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map> channelFifoList; sptr fifoCacheData = new (std::nothrow) FifoCacheData(); - if (fifoCacheData == nullptr) { - SEN_HILOGE("fifoCacheData cannot be null"); - return; - } + CHKPV(fifoCacheData); fifoCacheData->SetChannel(channel); channelFifoList.push_back(fifoCacheData); dataCountMap_.insert(std::make_pair(sensorId, channelFifoList)); @@ -102,10 +99,7 @@ void SensorDataProcesser::SendNoneFifoCacheData(std::unordered_map fifoCacheData = new (std::nothrow) FifoCacheData(); - if (fifoCacheData == nullptr) { - SEN_HILOGE("failed, fifoCacheData cannot be null"); - return; - } + CHKPV(fifoCacheData); fifoCacheData->SetChannel(channel); dataCountIt->second.push_back(fifoCacheData); SendRawData(cacheBuf, channel, sendEvents); @@ -126,10 +120,7 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map> channelFifoList; sptr fifoCacheData = new (std::nothrow) FifoCacheData(); - if (fifoCacheData == nullptr) { - SEN_HILOGE("fifoCacheData cannot be null"); - return; - } + CHKPV(fifoCacheData); fifoCacheData->SetChannel(channel); channelFifoList.push_back(fifoCacheData); dataCountMap_.insert(std::make_pair(sensorId, channelFifoList)); @@ -162,10 +153,7 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map fifoCacheData = new (std::nothrow) FifoCacheData(); - if (fifoCacheData == nullptr) { - SEN_HILOGE("failed, fifoCacheData cannot be null"); - return; - } + CHKPV(fifoCacheData); fifoCacheData->SetChannel(channel); dataCountIt->second.push_back(fifoCacheData); } @@ -173,10 +161,7 @@ void SensorDataProcesser::SendFifoCacheData(std::unordered_map &channel, struct SensorEvent &event) { - if (channel == nullptr) { - SEN_HILOGE("channel cannot be null"); - return; - } + CHKPV(channel); uint32_t sensorId = static_cast(event.sensorTypeId); if (sensorId == FLUSH_COMPLETE_ID) { sensorId = static_cast(event.sensorTypeId); @@ -231,8 +216,8 @@ bool SensorDataProcesser::CheckSendDataPermission(sptr c void SensorDataProcesser::SendRawData(std::unordered_map &cacheBuf, sptr channel, std::vector event) { - if (channel == nullptr || event.empty()) { - SEN_HILOGE("channel cannot be null or event cannot be empty"); + CHKPV(channel); + if (event.empty()) { return; } if (!CheckSendDataPermission(channel, event[0].sensorTypeId)) { @@ -271,10 +256,7 @@ void SensorDataProcesser::SendRawData(std::unordered_map &channel) { - if (channel == nullptr) { - SEN_HILOGE("channel cannot be null"); - return INVALID_POINTER; - } + CHKPR(channel, INVALID_POINTER); int32_t ret = ERR_OK; auto &cacheBuf = const_cast &>(channel->GetDataCacheBuf()); uint32_t sensorId = static_cast(event.sensorTypeId); @@ -355,10 +337,7 @@ void SensorDataProcesser::EventFilter(struct CircularEventBuf &eventsBuf) int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback) { - if (dataCallback == nullptr) { - SEN_HILOGE("dataCallback cannot be null"); - return INVALID_POINTER; - } + CHKPR(dataCallback, INVALID_POINTER); std::unique_lock lk(ISensorHdiConnection::dataMutex_); ISensorHdiConnection::dataCondition_.wait(lk); auto &eventsBuf = dataCallback->GetEventData(); @@ -381,10 +360,7 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback int32_t SensorDataProcesser::SendEvents(sptr &channel, struct SensorEvent &event) { - if (channel == nullptr) { - SEN_HILOGE("channel cannot be null"); - return INVALID_POINTER; - } + CHKPR(channel, INVALID_POINTER); clientInfo_.UpdateDataQueue(event.sensorTypeId, event); auto &cacheBuf = channel->GetDataCacheBuf(); if (cacheBuf.empty()) { diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index a3ac9a78e38d3487609be2130cb226784931c777..df09f2d9ceb608b35b1f1ee7eacdd5e2e342f938 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -214,10 +214,7 @@ bool SensorDump::DumpSensorData(int32_t fd, ClientInfo &clientInfo, const std::v sensorData.second.pop(); timespec time = { 0, 0 }; struct tm *timeinfo = localtime(&(time.tv_sec)); - if (timeinfo == nullptr) { - SEN_HILOGE("timeinfo cannot be null"); - return false; - } + CHKPF(timeinfo); dprintf(fd, " %2d (ts=%.9f, time=%02d:%02d:%02d.%03d) | data:%s", ++j, data.timestamp / 1e9, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, int32_t { (time.tv_nsec / MS_NS) }, GetDataBySensorId(sensorId, data).c_str()); @@ -231,10 +228,7 @@ void SensorDump::DumpCurrentTime(int32_t fd) timespec curTime = { 0, 0 }; clock_gettime(CLOCK_REALTIME, &curTime); struct tm *timeinfo = localtime(&(curTime.tv_sec)); - if (timeinfo == nullptr) { - SEN_HILOGE("timeinfo cannot be null"); - return; - } + CHKPV(timeinfo); dprintf(fd, "Current time: %02d:%02d:%02d.%03d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, int32_t { (curTime.tv_nsec / MS_NS) }); } diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 333b008c56f2df14aa75240192259d2a3c590d93..2d5c0f16915b7b11795660fe52b7503a46f53027 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -78,10 +78,7 @@ void SensorService::OnStart() return; } sensorDataProcesser_ = new (std::nothrow) SensorDataProcesser(sensorMap_); - if (sensorDataProcesser_ == nullptr) { - SEN_HILOGE("failed, sensorDataProcesser_ cannot be null"); - return; - } + CHKPV(sensorDataProcesser_); if (!InitSensorPolicy()) { SEN_HILOGE("Init sensor policy error"); } @@ -107,10 +104,7 @@ bool SensorService::InitInterface() bool SensorService::InitDataCallback() { reportDataCallback_ = new (std::nothrow) ReportDataCallback(); - if (reportDataCallback_ == nullptr) { - SEN_HILOGE("failed, reportDataCallback_ cannot be null"); - return false; - } + CHKPF(reportDataCallback_); ZReportDataCb cb = &ReportDataCallback::ReportEventCallback; auto ret = sensorHdiConnection_.RegisteDataReport(cb, reportDataCallback_); if (ret != ERR_OK) { @@ -208,10 +202,7 @@ void SensorService::ReportOnChangeData(uint32_t sensorId) return; } sptr channel = clientInfo_.GetSensorChannelByPid(this->GetCallingPid()); - if (channel == nullptr) { - SEN_HILOGE("there is no channel to be reported"); - return; - } + CHKPV(channel); auto sendRet = channel->SendData(&event, sizeof(event)); if (sendRet != ERR_OK) { SEN_HILOGE("send data failed"); @@ -365,10 +356,7 @@ std::vector SensorService::GetSensorList() ErrCode SensorService::TransferDataChannel(const sptr &sensorBasicDataChannel, const sptr &sensorClient) { - if ((sensorBasicDataChannel == nullptr)) { - SEN_HILOGE("sensorBasicDataChannel cannot be null"); - return ERR_NO_INIT; - } + CHKPR(sensorBasicDataChannel, ERR_NO_INIT); auto pid = this->GetCallingPid(); auto uid = this->GetCallingUid(); auto callerToken = this->GetCallingTokenID(); @@ -409,10 +397,7 @@ void SensorService::ProcessDeathObserver(const wptr &object) { CALL_LOG_ENTER; sptr client = object.promote(); - if (client == nullptr) { - SEN_HILOGE("client cannot be null"); - return; - } + CHKPV(client); int32_t pid = clientInfo_.FindClientPid(client); if (pid == INVALID_PID) { SEN_HILOGE("pid is -1"); @@ -429,10 +414,7 @@ void SensorService::RegisterClientDeathRecipient(sptr sensorClien CALL_LOG_ENTER; sptr client = iface_cast(sensorClient); clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); - if (clientDeathObserver_ == nullptr) { - SEN_HILOGE("clientDeathObserver_ cannot be null"); - return; - } + CHKPV(clientDeathObserver_); client->AsObject()->AddDeathRecipient(clientDeathObserver_); clientInfo_.SaveClientPid(sensorClient, pid); } @@ -442,10 +424,7 @@ void SensorService::UnregisterClientDeathRecipient(sptr sensorCli CALL_LOG_ENTER; sptr client = iface_cast(sensorClient); clientDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast(this)); - if (clientDeathObserver_ == nullptr) { - SEN_HILOGE("clientDeathObserver_ cannot be null"); - return; - } + CHKPV(clientDeathObserver_); client->AsObject()->RemoveDeathRecipient(clientDeathObserver_); clientInfo_.DestroyClientPid(sensorClient); } diff --git a/services/sensor/src/sensor_service_stub.cpp b/services/sensor/src/sensor_service_stub.cpp index a1265430562003ed0842a8d4971d4188cc7a3a6b..d401e797bfc336e73d195cceae7eb89b2dcaa8f8 100644 --- a/services/sensor/src/sensor_service_stub.cpp +++ b/services/sensor/src/sensor_service_stub.cpp @@ -142,30 +142,21 @@ ErrCode SensorServiceStub::CreateDataChannelInner(MessageParcel &data, MessagePa { (void)reply; sptr sensorChannel = new (std::nothrow) SensorBasicDataChannel(); - if (sensorChannel == nullptr) { - SEN_HILOGE("sensorChannel cannot be null"); - return OBJECT_NULL; - } + CHKPR(sensorChannel, OBJECT_NULL); auto ret = sensorChannel->CreateSensorBasicChannel(data); if (ret != ERR_OK) { SEN_HILOGE("CreateSensorBasicChannel ret : %{public}d", ret); return OBJECT_NULL; } sptr sensorClient = data.ReadRemoteObject(); - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return OBJECT_NULL; - } + CHKPR(sensorClient, OBJECT_NULL); return TransferDataChannel(sensorChannel, sensorClient); } ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply) { sptr sensorClient = data.ReadRemoteObject(); - if (sensorClient == nullptr) { - SEN_HILOGE("sensorClient cannot be null"); - return OBJECT_NULL; - } + CHKPR(sensorClient, OBJECT_NULL); return DestroySensorChannel(sensorClient); } } // namespace Sensors diff --git a/utils/src/report_data_callback.cpp b/utils/src/report_data_callback.cpp index 3f00ae3552ae46dc2714dcded49d94cb3273c574..82dc130df6793345e5c6b798392f82d850982912 100644 --- a/utils/src/report_data_callback.cpp +++ b/utils/src/report_data_callback.cpp @@ -53,10 +53,7 @@ ReportDataCallback::~ReportDataCallback() int32_t ReportDataCallback::ReportEventCallback(const struct SensorEvent* event, sptr cb) { - if (event == nullptr) { - SEN_HILOGE("sensor data is null"); - return ERROR; - } + CHKPR(event, ERROR); if (cb == nullptr || cb->eventsBuf_.circularBuf == nullptr) { SEN_HILOGE("callback or circularBuf or event cannot be null"); if (event->data != nullptr) { diff --git a/utils/src/sensor.cpp b/utils/src/sensor.cpp index 76f72481ff30103d45b1faa7b28a35bd60c29777..7fa75910e29e496923ad386640f10e0cbde92e6f 100644 --- a/utils/src/sensor.cpp +++ b/utils/src/sensor.cpp @@ -231,10 +231,7 @@ bool Sensor::Marshalling(Parcel &parcel) const std::unique_ptr Sensor::Unmarshalling(Parcel &parcel) { auto sensor = std::make_unique(); - if (sensor == nullptr) { - SEN_HILOGE("sensor cannot be null"); - return nullptr; - } + CHKPP(sensor); if (!sensor->ReadFromParcel(parcel)) { SEN_HILOGE("ReadFromParcel is failed"); diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 2b513af97fa3cf9c8cb86faca28c4d8585c79551..c26b4e54a7411d721c1847140600075f0981aad1 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -148,7 +148,8 @@ void SensorBasicDataChannel::CloseSendFd() int32_t SensorBasicDataChannel::SendData(const void *vaddr, size_t size) { - if (vaddr == nullptr || sendFd_ < 0) { + CHKPR(vaddr, SENSOR_CHANNEL_SEND_ADDR_ERR); + if (sendFd_ < 0) { SEN_HILOGE("failed, param is invalid"); return SENSOR_CHANNEL_SEND_ADDR_ERR; } @@ -165,10 +166,9 @@ int32_t SensorBasicDataChannel::SendData(const void *vaddr, size_t size) int32_t SensorBasicDataChannel::ReceiveData(void *vaddr, size_t size) { - if (vaddr == nullptr || (receiveFd_ == -1)) { + CHKPR(vaddr, SENSOR_CHANNEL_SEND_ADDR_ERR); SEN_HILOGE("failed, vaddr is null or receiveFd_ invalid"); return SENSOR_CHANNEL_RECEIVE_ADDR_ERR; - } ssize_t length; do { length = recv(receiveFd_, vaddr, size, MSG_DONTWAIT);