From 3595176614f2890237ddea07f804626d21a0a028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E9=BE=99?= Date: Mon, 29 Apr 2024 22:11:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=E5=9B=B4=E6=A0=8F=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李文龙 --- .../js/napi/include/location_napi_adapter.h | 4 +- .../js/napi/source/location_napi_adapter.cpp | 31 ++++++---- .../common/source/geofence_request.cpp | 29 +++++---- .../location_gnss_geofence_callback_host.cpp | 36 +++++++---- frameworks/native/source/locator_impl.cpp | 2 +- frameworks/native/source/locator_proxy.cpp | 31 ++-------- frameworks/native/source/napi_util.cpp | 24 ++++---- .../inner_api/include/geofence_request.h | 15 +++-- .../include/i_gnss_geofence_callback.h | 4 +- .../location_gnss_geofence_callback_host.h | 3 +- .../include/locationhub_ipc_interface_code.h | 4 +- interfaces/inner_api/include/napi_util.h | 4 +- services/location_gnss/gnss/BUILD.gn | 1 - .../location_gnss/gnss/include/gnss_ability.h | 5 +- .../gnss/source/gnss_ability.cpp | 59 +++++++------------ .../gnss/source/gnss_ability_skeleton.cpp | 26 +------- .../location_gnss_geofence_callback_proxy.h | 0 .../location_gnss_geofence_callback_proxy.cpp | 0 services/location_locator/locator/BUILD.gn | 1 + .../locator/source/gnss_ability_proxy.cpp | 22 +------ .../locator/source/locator_ability.cpp | 22 +------ .../locator/source/locator_skeleton.cpp | 14 +---- .../source/gnss_ability_test.cpp | 20 +++---- .../location_without_permission_test.cpp | 10 ++-- .../source/locator_impl_test.cpp | 10 ++-- .../source/locator_service_test.cpp | 10 ++-- 26 files changed, 159 insertions(+), 228 deletions(-) rename services/{location_gnss/gnss => location_locator/callback}/include/location_gnss_geofence_callback_proxy.h (100%) rename services/{location_gnss/gnss => location_locator/callback}/source/location_gnss_geofence_callback_proxy.cpp (100%) diff --git a/frameworks/js/napi/include/location_napi_adapter.h b/frameworks/js/napi/include/location_napi_adapter.h index 6a52898a..989b15f7 100644 --- a/frameworks/js/napi/include/location_napi_adapter.h +++ b/frameworks/js/napi/include/location_napi_adapter.h @@ -50,10 +50,10 @@ napi_value HandleGetCachedLocation(napi_env env); LocationErrCode CheckLocationSwitchState(); napi_value GetLocatingRequiredData(napi_env env, napi_callback_info info); napi_value AddGnssGeofence(napi_env env, napi_callback_info info); -GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForAdd(const napi_env& env, +GnssGeofenceAsyncContext* CreateAsyncContextForAddGnssGeofence(const napi_env& env, std::shared_ptr& request, sptr callback); napi_value RemoveGnssGeofence(napi_env env, napi_callback_info info); -GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForRemove(const napi_env& env, int fenceId); +GnssGeofenceAsyncContext* CreateAsyncContextForRemoveGnssGeofence(const napi_env& env, int fenceId); napi_value GetGeofenceSupportedCoordTypes(napi_env env, napi_callback_info info); #endif } // namespace Location diff --git a/frameworks/js/napi/source/location_napi_adapter.cpp b/frameworks/js/napi/source/location_napi_adapter.cpp index 9bf57af6..601b2643 100644 --- a/frameworks/js/napi/source/location_napi_adapter.cpp +++ b/frameworks/js/napi/source/location_napi_adapter.cpp @@ -22,6 +22,7 @@ namespace OHOS { namespace Location { auto g_locatorClient = Locator::GetInstance(); std::map> g_gnssGeofenceCallbackHostMap; +std::mutex g_gnssGeofenceCallbackHostMutex; napi_value GetLastLocation(napi_env env, napi_callback_info info) { @@ -1061,7 +1062,7 @@ napi_value AddGnssGeofence(napi_env env, napi_callback_info info) JsObjToGeofenceTransitionCallback(env, argv[0], locationGnssGeofenceCallbackHost); auto callbackPtr = sptr(locationGnssGeofenceCallbackHost); gnssGeofenceRequest->SetGeofenceTransitionCallback(callbackPtr->AsObject()); - auto asyncContext = CreateGnssGeofenceAsyncContextForAdd( + auto asyncContext = CreateAsyncContextForAddGnssGeofence( env, gnssGeofenceRequest, locationGnssGeofenceCallbackHost); if (asyncContext == nullptr) { HandleSyncErrCode(env, ERRCODE_INVALID_PARAM); @@ -1070,7 +1071,7 @@ napi_value AddGnssGeofence(napi_env env, napi_callback_info info) return DoAsyncWork(env, asyncContext, argc, argv, 1); } -GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForAdd(const napi_env& env, +GnssGeofenceAsyncContext* CreateAsyncContextForAddGnssGeofence(const napi_env& env, std::shared_ptr& request, sptr callback) { auto asyncContext = new (std::nothrow) GnssGeofenceAsyncContext(env); @@ -1097,7 +1098,10 @@ GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForAdd(const napi_env& e context->errCode = ERRCODE_SERVICE_UNAVAILABLE; } callbackHost->SetCount(1); + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); + lock.lock(); g_gnssGeofenceCallbackHostMap.insert(std::make_pair(callbackHost->GetFenceId(), callbackHost)); + lock.unlock(); } }; asyncContext->completeFunc = [&](void* data) -> void { @@ -1106,14 +1110,13 @@ GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForAdd(const napi_env& e } auto context = static_cast(data); auto callbackHost = context->callbackHost_; - if (callbackHost != nullptr && + if (callbackHost != nullptr && context->errCode == ERRCODE_SUCCESS && callbackHost->GetGeofenceOperationType() == GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_ADD) { LocationErrCode errCode = callbackHost->DealGeofenceOperationResult(); if (errCode == ERRCODE_SUCCESS) { int fenceId = callbackHost->GetFenceId(); napi_create_object(context->env, &context->result[PARAM1]); napi_create_int64(context->env, fenceId, &context->result[PARAM1]); - callbackHost->ClearFenceId(); } else { context->errCode = errCode; } @@ -1143,28 +1146,27 @@ napi_value RemoveGnssGeofence(napi_env env, napi_callback_info info) return UndefinedNapiValue(env); } NAPI_CALL(env, napi_get_value_int32(env, argv[0], &fenceId)); - auto asyncContext = CreateGnssGeofenceAsyncContextForRemove(env, fenceId); + auto asyncContext = CreateAsyncContextForRemoveGnssGeofence(env, fenceId); if (asyncContext == nullptr) { HandleSyncErrCode(env, ERRCODE_INVALID_PARAM); return UndefinedNapiValue(env); } - auto iterForDelete = g_gnssGeofenceCallbackHostMap.find(fenceId); - if (iterForDelete != g_gnssGeofenceCallbackHostMap.end()) { - g_gnssGeofenceCallbackHostMap.erase(iterForDelete); - } size_t objectArgsNum = 1; return DoAsyncWork(env, asyncContext, argc, argv, objectArgsNum); } -GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForRemove(const napi_env& env, int fenceId) +GnssGeofenceAsyncContext* CreateAsyncContextForRemoveGnssGeofence(const napi_env& env, int fenceId) { auto asyncContext = new (std::nothrow) GnssGeofenceAsyncContext(env); NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); asyncContext->fenceId_ = fenceId; + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); + lock.lock(); auto iter = g_gnssGeofenceCallbackHostMap.find(fenceId); if (iter != g_gnssGeofenceCallbackHostMap.end()) { asyncContext->callbackHost_ = iter->second; } + lock.unlock(); NAPI_CALL(env, napi_create_string_latin1(env, "removeGnssGeofence", NAPI_AUTO_LENGTH, &asyncContext->resourceName)); @@ -1191,7 +1193,7 @@ GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForRemove(const napi_env asyncContext->completeFunc = [&](void* data) -> void { auto context = static_cast(data); auto callbackHost = context->callbackHost_; - if (callbackHost != nullptr && + if (callbackHost != nullptr && context->errCode == ERRCODE_SUCCESS && callbackHost->GetGeofenceOperationType() == GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_DELETE) { LocationErrCode errCode = callbackHost->DealGeofenceOperationResult(); @@ -1204,6 +1206,13 @@ GnssGeofenceAsyncContext* CreateGnssGeofenceAsyncContextForRemove(const napi_env } else { context->errCode = ERRCODE_GEOFENCE_INCORRECT_ID; } + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); + lock.lock(); + auto iterForDelete = g_gnssGeofenceCallbackHostMap.find(context->fenceId_); + if (iterForDelete != g_gnssGeofenceCallbackHostMap.end()) { + g_gnssGeofenceCallbackHostMap.erase(iterForDelete); + } + lock.unlock(); LBSLOGD(LOCATOR_STANDARD, "Push RemoveGnssGeofence result to client"); }; return asyncContext; diff --git a/frameworks/location_common/common/source/geofence_request.cpp b/frameworks/location_common/common/source/geofence_request.cpp index e62c0c8f..72f9b419 100644 --- a/frameworks/location_common/common/source/geofence_request.cpp +++ b/frameworks/location_common/common/source/geofence_request.cpp @@ -20,17 +20,19 @@ #include "notification_request.h" #endif #include "iremote_object.h" +#include "want_agent.h" namespace OHOS { namespace Location { void GeofenceRequest::ReadFromParcel(Parcel& data) { std::unique_lock lock(geofenceRequestMutex_); - geofence_->latitude = data.ReadDouble(); - geofence_->longitude = data.ReadDouble(); - geofence_->radius = data.ReadDouble(); - geofence_->expiration = data.ReadDouble(); - geofence_->coordinateSystemType = static_cast(data.ReadInt32()); + scenario_ = data.ReadInt32(); + geofence_.latitude = data.ReadDouble(); + geofence_.longitude = data.ReadDouble(); + geofence_.radius = data.ReadDouble(); + geofence_.expiration = data.ReadDouble(); + geofence_.coordinateSystemType = static_cast(data.ReadInt32()); int monitorGeofenceTransitionSize = data.ReadInt32(); if (monitorGeofenceTransitionSize > MAX_TRANSITION_SIZE) { LBSLOGE(LOCATOR, "fence transition list size should not be greater than 3"); @@ -47,21 +49,23 @@ void GeofenceRequest::ReadFromParcel(Parcel& data) } for (int i = 0; i < requestSize; i++) { auto request = Notification::NotificationRequest::Unmarshalling(data); - notificationRequestList_.push_back(std::make_shared(*request)); + notificationRequestList_.push_back(*request); } #endif callback_ = data.ReadObject(); bundleName_ = data.ReadString(); + wantAgent_ = *(data.ReadParcelable()); } bool GeofenceRequest::Marshalling(Parcel& parcel) const { std::unique_lock lock(geofenceRequestMutex_); - parcel.WriteDouble(geofence_->latitude); - parcel.WriteDouble(geofence_->longitude); - parcel.WriteDouble(geofence_->radius); - parcel.WriteDouble(geofence_->expiration); - parcel.WriteInt32(static_cast(geofence_->coordinateSystemType)); + parcel.WriteInt32(scenario_); + parcel.WriteDouble(geofence_.latitude); + parcel.WriteDouble(geofence_.longitude); + parcel.WriteDouble(geofence_.radius); + parcel.WriteDouble(geofence_.expiration); + parcel.WriteInt32(static_cast(geofence_.coordinateSystemType)); if (transitionStatusList_.size() > MAX_TRANSITION_SIZE) { LBSLOGE(LOCATOR, "fence transition list size should not be greater than 3"); return false; @@ -77,11 +81,12 @@ bool GeofenceRequest::Marshalling(Parcel& parcel) const } parcel.WriteInt32(notificationRequestList_.size()); for (int i = 0; i < notificationRequestList_.size(); i++) { - notificationRequestList_[i]->Marshalling(parcel); + notificationRequestList_[i].Marshalling(parcel); } #endif parcel.WriteRemoteObject(callback_); parcel.WriteString(bundleName_); + parcel.WriteParcelable(&wantAgent_); return true; } diff --git a/frameworks/native/source/location_gnss_geofence_callback_host.cpp b/frameworks/native/source/location_gnss_geofence_callback_host.cpp index b183cde6..3c2b3743 100644 --- a/frameworks/native/source/location_gnss_geofence_callback_host.cpp +++ b/frameworks/native/source/location_gnss_geofence_callback_host.cpp @@ -61,7 +61,11 @@ int LocationGnssGeofenceCallbackHost::OnRemoteRequest( } switch (code) { case RECEIVE_TRANSITION_STATUS_EVENT: { - Send(code, data); + GeofenceTransition transition; + transition.fenceId = data.ReadInt32(); + transition.event = + static_cast(data.ReadInt32()); + OnTransitionStatusChange(transition); break; } case REPORT_OPERATION_RESULT_EVENT: { @@ -70,7 +74,7 @@ int LocationGnssGeofenceCallbackHost::OnRemoteRequest( int result = data.ReadInt32(); SetFenceId(fenceId); SetGeofenceOperationType(static_cast(type)); - SetGeofenceOperationResult(static_cast(result)); + OnReportOperationResult(fenceId, type, result); CountDown(); break; } @@ -82,7 +86,8 @@ int LocationGnssGeofenceCallbackHost::OnRemoteRequest( return 0; } -void LocationGnssGeofenceCallbackHost::Send(int code, MessageParcel& data) +void LocationGnssGeofenceCallbackHost::OnTransitionStatusChange( + GeofenceTransition transition) { std::unique_lock guard(mutex_); uv_loop_s *loop = nullptr; @@ -103,13 +108,24 @@ void LocationGnssGeofenceCallbackHost::Send(int code, MessageParcel& data) } context->env = env_; context->callback[SUCCESS_CALLBACK] = handlerCb_; - if (code == static_cast(RECEIVE_TRANSITION_STATUS_EVENT)) { - context->transition_.fenceId = data.ReadInt32(); - context->transition_.event = - static_cast(data.ReadInt32()); - context->code_ = code; - work->data = context; - UvQueueWork(loop, work); + context->transition_ = transition; + work->data = context; + UvQueueWork(loop, work); +} + +void LocationGnssGeofenceCallbackHost::OnReportOperationResult(int fenceId, int type, int result) +{ + int addValue = static_cast(GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_ADD); + if ((type != addValue && fenceId == GetFenceId()) || + (type == addValue)) { + GnssGeofenceOperateResult optResult = static_cast(result); + GnssGeofenceOperateType optType = static_cast(type); + if (result == GnssGeofenceOperateResult::GNSS_GEOFENCE_OPERATION_SUCCESS && + optType == GnssGeofenceOperateType::GNSS_GEOFENCE_OPT_TYPE_ADD) { + SetFenceId(fenceId); + } + SetGeofenceOperationType(optType); + SetGeofenceOperationResult(optResult); } } diff --git a/frameworks/native/source/locator_impl.cpp b/frameworks/native/source/locator_impl.cpp index 970f75e5..6bed88b2 100644 --- a/frameworks/native/source/locator_impl.cpp +++ b/frameworks/native/source/locator_impl.cpp @@ -1020,7 +1020,7 @@ LocationErrCode LocatorImpl::RemoveGnssGeofence(std::shared_ptr if (!LocationSaLoadManager::InitLocationSa(LOCATION_LOCATOR_SA_ID)) { return ERRCODE_SERVICE_UNAVAILABLE; } - LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::AddGnssGeofence()"); + LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RemoveGnssGeofence()"); sptr proxy = GetProxy(); if (proxy == nullptr) { LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__); diff --git a/frameworks/native/source/locator_proxy.cpp b/frameworks/native/source/locator_proxy.cpp index 6ddd2026..abc821b9 100644 --- a/frameworks/native/source/locator_proxy.cpp +++ b/frameworks/native/source/locator_proxy.cpp @@ -820,15 +820,7 @@ LocationErrCode LocatorProxy::HandleGnssfenceRequest( LBSLOGE(LOCATOR_STANDARD, "%{public}s WriteInterfaceToken failed", __func__); return ERRCODE_SERVICE_UNAVAILABLE; } - data.WriteInt32(request->GetScenario()); - auto geofence = request->GetGeofence(); - data.WriteDouble(geofence->latitude); - data.WriteDouble(geofence->longitude); - data.WriteDouble(geofence->radius); - data.WriteDouble(geofence->expiration); - data.WriteInt32(static_cast(geofence->coordinateSystemType)); - auto wantAgent = request->GetWantAgent(); - data.WriteParcelable(&wantAgent); + request->Marshalling(data); LocationErrCode errorCode = SendMsgWithDataReplyV9(static_cast(code), data, reply); LBSLOGD(LOCATOR_STANDARD, "Transact ErrCodes = %{public}d", errorCode); return errorCode; @@ -836,25 +828,10 @@ LocationErrCode LocatorProxy::HandleGnssfenceRequest( LocationErrCode LocatorProxy::AddGnssGeofence(std::shared_ptr& request) { - if (request == nullptr) { - LBSLOGE(LOCATOR_STANDARD, "request is nullptr"); - return ERRCODE_INVALID_PARAM; - } - MessageParcel data; - MessageParcel reply; - if (!data.WriteInterfaceToken(GetDescriptor())) { - LBSLOGE(LOCATOR_STANDARD, "can not write descriptor"); - return ERRCODE_SERVICE_UNAVAILABLE; - } - request->Marshalling(data); - LocationErrCode errorCode = SendMsgWithDataReplyV9( - static_cast(LocatorInterfaceCode::ADD_GNSS_GEOFENCE), data, reply); - LBSLOGD(LOCATOR_STANDARD, "Transact ErrCodes = %{public}d", errorCode); - return errorCode; + return HandleGnssfenceRequest(LocatorInterfaceCode::ADD_GNSS_GEOFENCE, request); } -LocationErrCode LocatorProxy::RemoveGnssGeofence( - std::shared_ptr& request) +LocationErrCode LocatorProxy::RemoveGnssGeofence(std::shared_ptr& request) { if (request == nullptr) { return ERRCODE_INVALID_PARAM; @@ -1018,7 +995,7 @@ LocationErrCode LocatorProxy::GetGeofenceSupportedCoordTypes( } MessageParcel reply; LocationErrCode errorCode = SendMsgWithDataReplyV9( - static_cast(LocatorInterfaceCode::QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply); + static_cast(LocatorInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply); LBSLOGD(LOCATOR_STANDARD, "Proxy::%{public}s Transact ErrCodes = %{public}d", __func__, errorCode); int size = reply.ReadInt32(); size = size > COORDINATE_SYSTEM_TYPE_SIZE ? COORDINATE_SYSTEM_TYPE_SIZE : size; diff --git a/frameworks/native/source/napi_util.cpp b/frameworks/native/source/napi_util.cpp index f95c004e..3f50496e 100644 --- a/frameworks/native/source/napi_util.cpp +++ b/frameworks/native/source/napi_util.cpp @@ -250,23 +250,23 @@ void JsObjToGeoFenceRequest(const napi_env& env, const napi_value& object, LBSLOGE(LOCATOR_STANDARD, "parse geofence failed"); return; } - std::shared_ptr geofence = std::make_shared(); + GeoFence geofence; if (JsObjectToDouble(env, geofenceValue, "latitude", doubleValue) == SUCCESS) { - geofence->latitude = doubleValue; + geofence.latitude = doubleValue; } if (JsObjectToDouble(env, geofenceValue, "longitude", doubleValue) == SUCCESS) { - geofence->longitude = doubleValue; + geofence.longitude = doubleValue; } if (JsObjectToInt(env, geofenceValue, "coordinateSystemType", value) == SUCCESS) { - geofence->coordinateSystemType = static_cast(value); + geofence.coordinateSystemType = static_cast(value); } else { - geofence->coordinateSystemType = CoordinateSystemType::WGS84; + geofence.coordinateSystemType = CoordinateSystemType::WGS84; } if (JsObjectToDouble(env, geofenceValue, "radius", doubleValue) == SUCCESS) { - geofence->radius = doubleValue; + geofence.radius = doubleValue; } if (JsObjectToDouble(env, geofenceValue, "expiration", doubleValue) == SUCCESS) { - geofence->expiration = doubleValue; + geofence.expiration = doubleValue; } request->SetGeofence(geofence); } @@ -467,7 +467,7 @@ bool GenGnssGeofenceRequest( JsObjToGeofenceTransitionEventList(env, value, geofenceTransitionStatusList); geofenceRequest->SetGeofenceTransitionEventList(geofenceTransitionStatusList); #ifdef NOTIFICATION_ENABLE - std::vector> notificationRequestList; + std::vector notificationRequestList; JsObjToNotificationRequestList(env, value, notificationRequestList); geofenceRequest->SetNotificationRequestList(notificationRequestList); #endif @@ -513,14 +513,14 @@ napi_value GetArrayProperty(const napi_env& env, const napi_value& object, std:: #ifdef NOTIFICATION_ENABLE void JsObjToNotificationRequestList(const napi_env& env, const napi_value& object, - std::vector>& notificationRequestList) + std::vector& notificationRequestList) { napi_value notificationRequest = GetArrayProperty(env, object, "notifications"); GetNotificationRequestArray(env, notificationRequest, notificationRequestList); } void GetNotificationRequestArray(const napi_env& env, const napi_value& notificationRequestValue, - std::vector>& notificationRequestList) + std::vector& notificationRequestList) { napi_valuetype valueType; NAPI_CALL_RETURN_VOID(env, napi_typeof(env, notificationRequestValue, &valueType)); @@ -545,9 +545,7 @@ void GetNotificationRequestArray(const napi_env& env, const napi_value& notifica } NotificationRequest notificationRequest; GenNotificationRequest(env, elementValue, notificationRequest); - std::shared_ptr request = - std::make_shared(notificationRequest); - notificationRequestList.push_back(request); + notificationRequestList.push_back(notificationRequest); } } diff --git a/interfaces/inner_api/include/geofence_request.h b/interfaces/inner_api/include/geofence_request.h index d21dfd33..53c83225 100644 --- a/interfaces/inner_api/include/geofence_request.h +++ b/interfaces/inner_api/include/geofence_request.h @@ -38,7 +38,6 @@ public: GeofenceRequest() { callback_ = nullptr; - geofence_ = std::make_shared(); scenario_ = -1; fenceId_ = -1; } @@ -57,12 +56,12 @@ public: ~GeofenceRequest() {} - inline std::shared_ptr GetGeofence() + inline GeoFence GetGeofence() { return geofence_; } - inline void SetGeofence(std::shared_ptr geofence) + inline void SetGeofence(GeoFence geofence) { geofence_ = geofence; } @@ -107,19 +106,19 @@ public: } } - inline std::vector> GetNotificationRequestList() + inline std::vector GetNotificationRequestList() { std::unique_lock lock(geofenceRequestMutex_); return notificationRequestList_; } - inline void SetNotificationRequest(std::shared_ptr request) + inline void SetNotificationRequest(Notification::NotificationRequest request) { std::unique_lock lock(geofenceRequestMutex_); notificationRequestList_.push_back(request); } - inline void SetNotificationRequestList(std::vector> requestList) + inline void SetNotificationRequestList(std::vector requestList) { std::unique_lock lock(geofenceRequestMutex_); for (auto it = requestList.begin(); it != requestList.end(); ++it) { @@ -162,9 +161,9 @@ public: static std::shared_ptr Unmarshalling(Parcel& parcel); private: std::vector transitionStatusList_; - std::vector> notificationRequestList_; + std::vector notificationRequestList_; sptr callback_ = nullptr; - std::shared_ptr geofence_ = nullptr; + GeoFence geofence_; int scenario_; int fenceId_; AbilityRuntime::WantAgent::WantAgent wantAgent_; diff --git a/interfaces/inner_api/include/i_gnss_geofence_callback.h b/interfaces/inner_api/include/i_gnss_geofence_callback.h index bbb42e9d..c8bee7ae 100644 --- a/interfaces/inner_api/include/i_gnss_geofence_callback.h +++ b/interfaces/inner_api/include/i_gnss_geofence_callback.h @@ -18,7 +18,7 @@ #include "iremote_broker.h" -#include "constant_definition.h" +#include "geofence_definition.h" namespace OHOS { namespace Location { @@ -29,6 +29,8 @@ public: RECEIVE_TRANSITION_STATUS_EVENT }; DECLARE_INTERFACE_DESCRIPTOR(u"location.IGnssGeofenceCallback"); + virtual void OnTransitionStatusChange(GeofenceTransition transition) = 0; + virtual void OnReportOperationResult(int fenceId, int type, int result) = 0; }; } // namespace Location } // namespace OHOS diff --git a/interfaces/inner_api/include/location_gnss_geofence_callback_host.h b/interfaces/inner_api/include/location_gnss_geofence_callback_host.h index 54a5fd30..aa18287d 100644 --- a/interfaces/inner_api/include/location_gnss_geofence_callback_host.h +++ b/interfaces/inner_api/include/location_gnss_geofence_callback_host.h @@ -47,6 +47,8 @@ public: void SetGeofenceOperationType(GnssGeofenceOperateType type); GnssGeofenceOperateResult GetGeofenceOperationResult(); void SetGeofenceOperationResult(GnssGeofenceOperateResult result); + void OnTransitionStatusChange(GeofenceTransition transition) override; + void OnReportOperationResult(int fenceId, int type, int result) override; inline napi_env GetEnv() const { @@ -79,7 +81,6 @@ public: } private: - void Send(int code, MessageParcel& data); void UvQueueWork(uv_loop_s* loop, uv_work_t* work); void InitLatch(); diff --git a/interfaces/inner_api/include/locationhub_ipc_interface_code.h b/interfaces/inner_api/include/locationhub_ipc_interface_code.h index 728dee29..b014c4df 100644 --- a/interfaces/inner_api/include/locationhub_ipc_interface_code.h +++ b/interfaces/inner_api/include/locationhub_ipc_interface_code.h @@ -66,7 +66,7 @@ enum class LocatorInterfaceCode { UNREG_LOCATING_REQUIRED_DATA_CALLBACK = 44, ADD_GNSS_GEOFENCE = 45, REMOVE_GNSS_GEOFENCE = 46, - QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE = 47, + GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE = 47, }; enum class GeoConvertInterfaceCode { @@ -107,7 +107,7 @@ enum class GnssInterfaceCode { DISABLE_REV_GEOCODE_MOCK = 26, ADD_GNSS_GEOFENCE = 27, REMOVE_GNSS_GEOFENCE = 28, - QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE = 29, + GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE = 29, }; enum class NetworkInterfaceCode { diff --git a/interfaces/inner_api/include/napi_util.h b/interfaces/inner_api/include/napi_util.h index 2f0bfbf8..ee8e85b9 100644 --- a/interfaces/inner_api/include/napi_util.h +++ b/interfaces/inner_api/include/napi_util.h @@ -97,9 +97,9 @@ bool GenGnssGeofenceRequest( napi_value GetArrayProperty(const napi_env& env, const napi_value& object, std::string propertyName); #ifdef NOTIFICATION_ENABLE void JsObjToNotificationRequestList(const napi_env& env, const napi_value& object, - std::vector>& notificationRequestList); + std::vector& notificationRequestList); void GetNotificationRequestArray(const napi_env& env, const napi_value& notificationRequest, - std::vector>& notificationRequestList); + std::vector& notificationRequestList); #endif void JsObjToGeofenceTransitionEventList(const napi_env& env, const napi_value& object, std::vector& geofenceTransitionStatusList); diff --git a/services/location_gnss/gnss/BUILD.gn b/services/location_gnss/gnss/BUILD.gn index 99897ec1..a4ab8242 100644 --- a/services/location_gnss/gnss/BUILD.gn +++ b/services/location_gnss/gnss/BUILD.gn @@ -24,7 +24,6 @@ if (location_feature_with_gnss) { "$LOCATION_GNSS_ROOT/source/gnss_ability_skeleton.cpp", "$LOCATION_GNSS_ROOT/source/gnss_common_event_subscriber.cpp", "$LOCATION_GNSS_ROOT/source/gnss_event_callback.cpp", - "$LOCATION_GNSS_ROOT/source/location_gnss_geofence_callback_proxy.cpp", "$LOCATION_GNSS_ROOT/source/string_utils.cpp", "$LOCATION_GNSS_ROOT/source/ui_extension_ability_connection.cpp", "$LOCATION_LOCATOR_ROOT/source/location_config_manager.cpp", diff --git a/services/location_gnss/gnss/include/gnss_ability.h b/services/location_gnss/gnss/include/gnss_ability.h index abb5dfbe..358e4827 100644 --- a/services/location_gnss/gnss/include/gnss_ability.h +++ b/services/location_gnss/gnss/include/gnss_ability.h @@ -82,7 +82,9 @@ using HDI::Location::Geofence::V2_0::GeofenceInfo; typedef struct { std::shared_ptr request; +#ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE sptr callback; +#endif int requestCode; int retCode; std::vector coordinateSystemTypes; @@ -181,10 +183,11 @@ public: const sptr& callback); bool UnregisterGnssGeofenceCallback(int fenceId); std::shared_ptr GetGeofenceRequestByFenceId(int fenceId); - sptr GetGnssGeofenceCallbackByFenceId(int fenceId); +#ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE void ReportGeofenceEvent(int fenceId, GeofenceEvent event); void ReportGeofenceOperationResult( int fenceId, GeofenceOperateType type, GeofenceOperateResult result); +#endif bool RemoveGnssGeofenceRequestByCallback(sptr callbackObj); LocationErrCode QuerySupportCoordinateSystemType( std::vector& coordinateSystemTypes) override; diff --git a/services/location_gnss/gnss/source/gnss_ability.cpp b/services/location_gnss/gnss/source/gnss_ability.cpp index 4388a70c..6c76717a 100644 --- a/services/location_gnss/gnss/source/gnss_ability.cpp +++ b/services/location_gnss/gnss/source/gnss_ability.cpp @@ -43,7 +43,6 @@ #include "location_data_rdb_manager.h" #include "hook_utils.h" -#include "location_gnss_geofence_callback_proxy.h" #include "geofence_definition.h" namespace OHOS { @@ -486,10 +485,6 @@ LocationErrCode GnssAbility::SetPositionMode() LocationErrCode GnssAbility::AddFence(std::shared_ptr& request) { auto geofence = request->GetGeofence(); - if (geofence == nullptr) { - LBSLOGE(GNSS, "geofence is nullptr"); - return ERRCODE_INVALID_PARAM; - } int fenceId = GenerateFenceId(); request->SetFenceId(fenceId); #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE @@ -499,14 +494,14 @@ LocationErrCode GnssAbility::AddFence(std::shared_ptr& request) } GeofenceInfo fenceInfo; fenceInfo.fenceIndex = fenceId; - fenceInfo.latitude = geofence->latitude; - fenceInfo.longitude = geofence->longitude; - fenceInfo.radius = geofence->radius; + fenceInfo.latitude = geofence.latitude; + fenceInfo.longitude = geofence.longitude; + fenceInfo.radius = geofence.radius; int monitorEvent = static_cast(GeofenceTransitionEvent::GEOFENCE_TRANSITION_EVENT_ENTER) | static_cast(GeofenceTransitionEvent::GEOFENCE_TRANSITION_EVENT_EXIT); int32_t ret = geofenceInterface_->AddGnssGeofence(fenceInfo, static_cast(monitorEvent)); -#endif LBSLOGD(GNSS, "Successfully AddFence!, %{public}d", ret); +#endif if (ExecuteFenceProcess(GnssInterfaceCode::ADD_FENCE_INFO, request)) { return ERRCODE_SUCCESS; } @@ -521,8 +516,8 @@ LocationErrCode GnssAbility::RemoveFence(std::shared_ptr& reque return ERRCODE_SERVICE_UNAVAILABLE; } int32_t ret = geofenceInterface_->DeleteGnssGeofence(request->GetFenceId()); -#endif LBSLOGD(GNSS, "Successfully RemoveFence!, %{public}d", ret); +#endif if (ExecuteFenceProcess(GnssInterfaceCode::REMOVE_FENCE_INFO, request)) { return ERRCODE_SUCCESS; } @@ -544,10 +539,6 @@ int32_t GnssAbility::GenerateFenceId() LocationErrCode GnssAbility::AddGnssGeofence(std::shared_ptr& request) { auto geofence = request->GetGeofence(); - if (geofence == nullptr) { - LBSLOGE(GNSS, "geofence is nullptr"); - return ERRCODE_INVALID_PARAM; - } int fenceId = GenerateFenceId(); request->SetFenceId(fenceId); #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE @@ -557,9 +548,9 @@ LocationErrCode GnssAbility::AddGnssGeofence(std::shared_ptr& r } GeofenceInfo fenceInfo; fenceInfo.fenceIndex = fenceId; - fenceInfo.latitude = geofence->latitude; - fenceInfo.longitude = geofence->longitude; - fenceInfo.radius = geofence->radius; + fenceInfo.latitude = geofence.latitude; + fenceInfo.longitude = geofence.longitude; + fenceInfo.radius = geofence.radius; auto transitionList = request->GetGeofenceTransitionEventList(); int monitorEvent = 0; for (int i = 0; i < transitionList.size(); i++) { @@ -623,6 +614,7 @@ bool GnssAbility::UnregisterGnssGeofenceCallback(int fenceId) auto fenceIdInMap = requestInMap->GetFenceId(); if (fenceId == fenceIdInMap) { iter = gnssGeofenceRequestMap_.erase(iter); + break; } else { iter++; } @@ -667,6 +659,7 @@ bool GnssAbility::RemoveGnssGeofenceRequestByCallback(sptr callba return true; } +#ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE void GnssAbility::ReportGeofenceOperationResult( int fenceId, GeofenceOperateType type, GeofenceOperateResult result) { @@ -676,19 +669,21 @@ void GnssAbility::ReportGeofenceOperationResult( LBSLOGE(GNSS, "request is nullptr"); return; } - auto callback = GetGnssGeofenceCallbackByFenceId(fenceId); + auto callback = geofenceRequest->GetGeofenceTransitionCallback(); if (callback == nullptr) { LBSLOGE(GNSS, "callback is nullptr"); return; } - auto gnssGeofenceCallback = std::make_unique(callback); + sptr gnssGeofenceCallback = iface_cast(callback); gnssGeofenceCallback->OnReportOperationResult( fenceId, static_cast(type), static_cast(result)); if (type == GeofenceOperateType::TYPE_DELETE) { UnregisterGnssGeofenceCallback(fenceId); } } +#endif +#ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE void GnssAbility::ReportGeofenceEvent(int fenceIndex, GeofenceEvent event) { std::unique_lock lock(gnssGeofenceRequestMapMutex_); @@ -702,7 +697,7 @@ void GnssAbility::ReportGeofenceEvent(int fenceIndex, GeofenceEvent event) LBSLOGE(GNSS, "callback is nullptr"); return; } - auto gnssGeofenceCallback = std::make_unique(callback); + sptr gnssGeofenceCallback = iface_cast(callback); auto transitionStatusList = request->GetGeofenceTransitionEventList(); #ifdef NOTIFICATION_ENABLE auto notificationRequestList = request->GetNotificationRequestList(); @@ -718,16 +713,16 @@ void GnssAbility::ReportGeofenceEvent(int fenceIndex, GeofenceEvent event) gnssGeofenceCallback->OnTransitionStatusChange(geofenceTransition); #ifdef NOTIFICATION_ENABLE auto notificationRequest = notificationRequestList[i]; - if (notificationRequest != nullptr && - transitionStatusList.size() == notificationRequestList.size()) { - notificationRequest->SetCreatorUid(IPCSkeleton::GetCallingUid()); - Notification::NotificationHelper::PublishNotification(*notificationRequest); + if (transitionStatusList.size() == notificationRequestList.size()) { + notificationRequest.SetCreatorUid(IPCSkeleton::GetCallingUid()); + Notification::NotificationHelper::PublishNotification(notificationRequest); } else { LBSLOGE(GNSS, "transitionStatusList size does not equals to notificationRequestList size"); } #endif } } +#endif std::shared_ptr GnssAbility::GetGeofenceRequestByFenceId(int fenceId) { @@ -740,18 +735,6 @@ std::shared_ptr GnssAbility::GetGeofenceRequestByFenceId(int fe return nullptr; } -sptr GnssAbility::GetGnssGeofenceCallbackByFenceId(int fenceId) -{ - for (auto iter = gnssGeofenceRequestMap_.begin(); iter != gnssGeofenceRequestMap_.end(); iter++) { - auto request = iter->first; - auto callback = iter->second; - if (request->GetFenceId() == fenceId) { - return callback; - } - } - return nullptr; -} - bool GnssAbility::ExecuteFenceProcess( GnssInterfaceCode code, std::shared_ptr& request) { @@ -949,7 +932,7 @@ bool GnssAbility::ConnectHdi() #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE geofenceInterface_ = IGeofenceInterface::Get(); if (geofenceInterface_ != nullptr) { - geofenceCallback_ = new (std::nothrow) GeofenceEventCallback(); + geofenceCallback_ = sptr(new (std::nothrow) GeofenceEventCallback); SetGeofenceCallback(); } #endif @@ -1163,7 +1146,7 @@ LocationErrCode GnssAbility::QuerySupportCoordinateSystemType( supportedTypes.push_back(CoordinateSystemType::WGS84); FenceStruct fenceStruct; fenceStruct.requestCode = - static_cast(GnssInterfaceCode::QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE); + static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE); fenceStruct.coordinateSystemTypes = supportedTypes; HookUtils::ExecuteHook( LocationProcessStage::FENCE_REQUEST_PROCESS, (void *)&fenceStruct, nullptr); diff --git a/services/location_gnss/gnss/source/gnss_ability_skeleton.cpp b/services/location_gnss/gnss/source/gnss_ability_skeleton.cpp index 22784d5f..4ee96c53 100644 --- a/services/location_gnss/gnss/source/gnss_ability_skeleton.cpp +++ b/services/location_gnss/gnss/source/gnss_ability_skeleton.cpp @@ -70,7 +70,7 @@ void GnssAbilityStub::InitGnssMsgHandleMap() &GnssAbilityStub::AddGnssGeofenceInner; GnssMsgHandleMap_[static_cast(GnssInterfaceCode::REMOVE_GNSS_GEOFENCE)] = &GnssAbilityStub::RemoveGnssGeofenceInner; - GnssMsgHandleMap_[static_cast(GnssInterfaceCode::QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE)] = + GnssMsgHandleMap_[static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE)] = &GnssAbilityStub::QuerySupportCoordinateSystemTypeInner; } @@ -236,17 +236,7 @@ int GnssAbilityStub::AddFenceInner(MessageParcel &data, MessageParcel &reply, Ap if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) { return ERRCODE_PERMISSION_DENIED; } - std::shared_ptr geofence = std::make_shared(); - std::shared_ptr request = std::make_shared(); - request->SetScenario(data.ReadInt32()); - geofence->latitude = data.ReadDouble(); - geofence->longitude = data.ReadDouble(); - geofence->radius = data.ReadDouble(); - geofence->expiration = data.ReadDouble(); - geofence->coordinateSystemType = static_cast(data.ReadInt32()); - request->SetGeofence(geofence); - auto agent = data.ReadParcelable(); - request->SetWantAgent(*agent); + auto request = GeofenceRequest::Unmarshalling(data); reply.WriteInt32(AddFence(request)); return ERRCODE_SUCCESS; } @@ -256,17 +246,7 @@ int GnssAbilityStub::RemoveFenceInner(MessageParcel &data, MessageParcel &reply, if (!PermissionManager::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) { return ERRCODE_PERMISSION_DENIED; } - std::shared_ptr geofence = std::make_shared(); - std::shared_ptr request = std::make_shared(); - request->SetScenario(data.ReadInt32()); - geofence->latitude = data.ReadDouble(); - geofence->longitude = data.ReadDouble(); - geofence->radius = data.ReadDouble(); - geofence->expiration = data.ReadDouble(); - geofence->coordinateSystemType = static_cast(data.ReadInt32()); - request->SetGeofence(geofence); - auto agent = data.ReadParcelable(); - request->SetWantAgent(*agent); + auto request = GeofenceRequest::Unmarshalling(data); reply.WriteInt32(RemoveFence(request)); return ERRCODE_SUCCESS; } diff --git a/services/location_gnss/gnss/include/location_gnss_geofence_callback_proxy.h b/services/location_locator/callback/include/location_gnss_geofence_callback_proxy.h similarity index 100% rename from services/location_gnss/gnss/include/location_gnss_geofence_callback_proxy.h rename to services/location_locator/callback/include/location_gnss_geofence_callback_proxy.h diff --git a/services/location_gnss/gnss/source/location_gnss_geofence_callback_proxy.cpp b/services/location_locator/callback/source/location_gnss_geofence_callback_proxy.cpp similarity index 100% rename from services/location_gnss/gnss/source/location_gnss_geofence_callback_proxy.cpp rename to services/location_locator/callback/source/location_gnss_geofence_callback_proxy.cpp diff --git a/services/location_locator/locator/BUILD.gn b/services/location_locator/locator/BUILD.gn index 4a315e42..3cbe422b 100644 --- a/services/location_locator/locator/BUILD.gn +++ b/services/location_locator/locator/BUILD.gn @@ -26,6 +26,7 @@ ohos_shared_library("lbsservice_locator") { "$SUBSYSTEM_DIR/location_locator/callback/source/country_code_callback_proxy.cpp", "$SUBSYSTEM_DIR/location_locator/callback/source/gnss_status_callback_proxy.cpp", "$SUBSYSTEM_DIR/location_locator/callback/source/locating_required_data_callback_proxy.cpp", + "$SUBSYSTEM_DIR/location_locator/callback/source/location_gnss_geofence_callback_proxy.cpp", "$SUBSYSTEM_DIR/location_locator/callback/source/locator_callback_proxy.cpp", "$SUBSYSTEM_DIR/location_locator/callback/source/nmea_message_callback_proxy.cpp", "$SUBSYSTEM_DIR/location_locator/locator/source/fusion_controller.cpp", diff --git a/services/location_locator/locator/source/gnss_ability_proxy.cpp b/services/location_locator/locator/source/gnss_ability_proxy.cpp index 197b1d49..f727ec79 100644 --- a/services/location_locator/locator/source/gnss_ability_proxy.cpp +++ b/services/location_locator/locator/source/gnss_ability_proxy.cpp @@ -266,15 +266,7 @@ LocationErrCode GnssAbilityProxy::AddFence(std::shared_ptr& req LBSLOGE(GNSS, "write interfaceToken fail!"); return ERRCODE_SERVICE_UNAVAILABLE; } - data.WriteInt32(request->GetScenario()); - auto geofence = request->GetGeofence(); - data.WriteDouble(geofence->latitude); - data.WriteDouble(geofence->longitude); - data.WriteDouble(geofence->radius); - data.WriteDouble(geofence->expiration); - data.WriteInt32(static_cast(geofence->coordinateSystemType)); - auto wantAgent = request->GetWantAgent(); - data.WriteParcelable(&wantAgent); + request->Marshalling(data); sptr remote = Remote(); if (remote == nullptr) { LBSLOGE(GNSS, "AddFence remote is null"); @@ -295,15 +287,7 @@ LocationErrCode GnssAbilityProxy::RemoveFence(std::shared_ptr& LBSLOGE(GNSS, "write interfaceToken fail!"); return ERRCODE_SERVICE_UNAVAILABLE; } - data.WriteInt32(request->GetScenario()); - auto geofence = request->GetGeofence(); - data.WriteDouble(geofence->latitude); - data.WriteDouble(geofence->longitude); - data.WriteDouble(geofence->radius); - data.WriteDouble(geofence->expiration); - data.WriteInt32(static_cast(geofence->coordinateSystemType)); - auto wantAgent = request->GetWantAgent(); - data.WriteParcelable(&wantAgent); + request->Marshalling(data); sptr remote = Remote(); if (remote == nullptr) { LBSLOGE(GNSS, "RemoveFence remote is null"); @@ -444,7 +428,7 @@ LocationErrCode GnssAbilityProxy::QuerySupportCoordinateSystemType( return ERRCODE_SERVICE_UNAVAILABLE; } int error = - remote->SendRequest(static_cast(GnssInterfaceCode::DISABLE_LOCATION_MOCK), data, reply, option); + remote->SendRequest(static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply, option); LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error); return LocationErrCode(reply.ReadInt32()); } diff --git a/services/location_locator/locator/source/locator_ability.cpp b/services/location_locator/locator/source/locator_ability.cpp index 017acb7c..4a2f08d9 100644 --- a/services/location_locator/locator/source/locator_ability.cpp +++ b/services/location_locator/locator/source/locator_ability.cpp @@ -600,15 +600,7 @@ LocationErrCode LocatorAbility::AddFence(std::shared_ptr& reque if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) { return ERRCODE_SERVICE_UNAVAILABLE; } - dataToStub.WriteInt32(request->GetScenario()); - auto geofence = request->GetGeofence(); - dataToStub.WriteDouble(geofence->latitude); - dataToStub.WriteDouble(geofence->longitude); - dataToStub.WriteDouble(geofence->radius); - dataToStub.WriteDouble(geofence->expiration); - dataToStub.WriteInt32(static_cast(geofence->coordinateSystemType)); - auto wantAgent = request->GetWantAgent(); - dataToStub.WriteParcelable(&wantAgent); + request->Marshalling(dataToStub); return SendGnssRequest( static_cast(GnssInterfaceCode::ADD_FENCE_INFO), dataToStub, replyToStub); } @@ -622,15 +614,7 @@ LocationErrCode LocatorAbility::RemoveFence(std::shared_ptr& re if (!dataToStub.WriteInterfaceToken(GnssAbilityProxy::GetDescriptor())) { return ERRCODE_SERVICE_UNAVAILABLE; } - dataToStub.WriteInt32(request->GetScenario()); - auto geofence = request->GetGeofence(); - dataToStub.WriteDouble(geofence->latitude); - dataToStub.WriteDouble(geofence->longitude); - dataToStub.WriteDouble(geofence->radius); - dataToStub.WriteDouble(geofence->expiration); - dataToStub.WriteInt32(static_cast(geofence->coordinateSystemType)); - auto wantAgent = request->GetWantAgent(); - dataToStub.WriteParcelable(&wantAgent); + request->Marshalling(dataToStub); return SendGnssRequest( static_cast(GnssInterfaceCode::REMOVE_FENCE_INFO), dataToStub, replyToStub); } @@ -1351,7 +1335,7 @@ LocationErrCode LocatorAbility::QuerySupportCoordinateSystemType( return ERRCODE_SERVICE_UNAVAILABLE; } auto errCode = SendGnssRequest( - static_cast(GnssInterfaceCode::QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE), + static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), dataToStub, replyToStub); if (errCode == ERRCODE_SUCCESS) { int size = replyToStub.ReadInt32(); diff --git a/services/location_locator/locator/source/locator_skeleton.cpp b/services/location_locator/locator/source/locator_skeleton.cpp index 2d457779..df193f9b 100644 --- a/services/location_locator/locator/source/locator_skeleton.cpp +++ b/services/location_locator/locator/source/locator_skeleton.cpp @@ -110,7 +110,7 @@ void LocatorAbilityStub::ConstructGnssHandleMap() &LocatorAbilityStub::PreUnregisterNmeaMessageCallbackV9; locatorHandleMap_[LocatorInterfaceCode::ADD_GNSS_GEOFENCE] = &LocatorAbilityStub::PreAddGnssGeofence; locatorHandleMap_[LocatorInterfaceCode::REMOVE_GNSS_GEOFENCE] = &LocatorAbilityStub::PreRemoveGnssGeofence; - locatorHandleMap_[LocatorInterfaceCode::QUERY_SUPPORT_COORDINATE_SYSTEM_TYPE] = + locatorHandleMap_[LocatorInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE] = &LocatorAbilityStub::PreQuerySupportCoordinateSystemType; #endif } @@ -651,17 +651,7 @@ int LocatorAbilityStub::DoProcessFenceRequest( reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE); return ERRCODE_SERVICE_UNAVAILABLE; } - std::shared_ptr geofence = std::make_shared(); - std::shared_ptr request = std::make_shared(); - request->SetScenario(data.ReadInt32()); - geofence->latitude = data.ReadDouble(); - geofence->longitude = data.ReadDouble(); - geofence->radius = data.ReadDouble(); - geofence->expiration = data.ReadDouble(); - geofence->coordinateSystemType = static_cast(data.ReadInt32()); - request->SetGeofence(geofence); - auto wantAgent = data.ReadParcelable(); - request->SetWantAgent(*wantAgent); + auto request = GeofenceRequest::Unmarshalling(data); if (code == LocatorInterfaceCode::ADD_FENCE) { reply.WriteInt32(locatorAbility->AddFence(request)); } else if (code == LocatorInterfaceCode::REMOVE_FENCE) { diff --git a/test/location_gnss/source/gnss_ability_test.cpp b/test/location_gnss/source/gnss_ability_test.cpp index 3fd5ab83..9a06eb57 100644 --- a/test/location_gnss/source/gnss_ability_test.cpp +++ b/test/location_gnss/source/gnss_ability_test.cpp @@ -510,11 +510,11 @@ HWTEST_F(GnssAbilityTest, AddFence001, TestSize.Level1) /* * @tc.steps: step1. build geo fence request */ - std::shared_ptr geofence = std::make_shared(); - geofence->latitude = 35.1; - geofence->longitude = 40.2; - geofence->radius = 2.2; - geofence->expiration = 12.2; + GeoFence geofence; + geofence.latitude = 35.1; + geofence.longitude = 40.2; + geofence.radius = 2.2; + geofence.expiration = 12.2; std::shared_ptr request = std::make_shared(); request->SetGeofence(geofence); @@ -539,11 +539,11 @@ HWTEST_F(GnssAbilityTest, RemoveFence001, TestSize.Level1) /* * @tc.steps: step1. build geo fence request */ - std::shared_ptr geofence = std::make_shared(); - geofence->latitude = 35.1; - geofence->longitude = 40.2; - geofence->radius = 2.2; - geofence->expiration = 12.2; + GeoFence geofence; + geofence.latitude = 35.1; + geofence.longitude = 40.2; + geofence.radius = 2.2; + geofence.expiration = 12.2; std::shared_ptr request = std::make_shared(); request->SetGeofence(geofence); diff --git a/test/location_locator/source/location_without_permission_test.cpp b/test/location_locator/source/location_without_permission_test.cpp index 340cae78..fe562a21 100644 --- a/test/location_locator/source/location_without_permission_test.cpp +++ b/test/location_locator/source/location_without_permission_test.cpp @@ -276,11 +276,11 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9003, T bool state = false; EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state)); #ifdef FEATURE_GNSS_SUPPORT - std::shared_ptr geofence = std::make_shared(); - geofence->latitude = 35.1; - geofence->longitude = 40.2; - geofence->radius = 2.2; - geofence->expiration = 12.2; + GeoFence geofence; + geofence.latitude = 35.1; + geofence.longitude = 40.2; + geofence.radius = 2.2; + geofence.expiration = 12.2; std::shared_ptr fenceRequest = std::make_shared(); fenceRequest->SetGeofence(geofence); if (state) { diff --git a/test/location_locator/source/locator_impl_test.cpp b/test/location_locator/source/locator_impl_test.cpp index 2ab14b91..ebb750e8 100644 --- a/test/location_locator/source/locator_impl_test.cpp +++ b/test/location_locator/source/locator_impl_test.cpp @@ -305,11 +305,11 @@ HWTEST_F(LocatorImplTest, locatorImplRequestFenceV9, TestSize.Level1) GTEST_LOG_(INFO) << "LocatorImplTest, locatorImplRequestFenceV9, TestSize.Level1"; LBSLOGI(LOCATOR, "[LocatorImplTest] locatorImplRequestFenceV9 begin"); - std::shared_ptr geofence = std::make_shared(); - geofence->latitude = 35.1; - geofence->longitude = 40.2; - geofence->radius = 2.2; - geofence->expiration = 12.2; + GeoFence geofence; + geofence.latitude = 35.1; + geofence.longitude = 40.2; + geofence.radius = 2.2; + geofence.expiration = 12.2; std::shared_ptr fenceRequest = std::make_shared(); fenceRequest->SetGeofence(geofence); EXPECT_EQ(ERRCODE_NOT_SUPPORTED, locatorImpl_->AddFenceV9(fenceRequest)); diff --git a/test/location_locator/source/locator_service_test.cpp b/test/location_locator/source/locator_service_test.cpp index c5abdd29..63c59bf1 100644 --- a/test/location_locator/source/locator_service_test.cpp +++ b/test/location_locator/source/locator_service_test.cpp @@ -1661,11 +1661,11 @@ HWTEST_F(LocatorServiceTest, locatorServiceFence001, TestSize.Level1) LBSLOGI(LOCATOR, "[LocatorServiceTest] locatorServiceFence001 begin"); auto locatorAbility = sptr(new (std::nothrow) LocatorAbility()); - std::shared_ptr geofence = std::make_shared(); - geofence->latitude = 35.1; - geofence->longitude = 40.2; - geofence->radius = 2.2; - geofence->expiration = 12.2; + GeoFence geofence; + geofence.latitude = 35.1; + geofence.longitude = 40.2; + geofence.radius = 2.2; + geofence.expiration = 12.2; std::shared_ptr fenceRequest = std::make_shared(); fenceRequest->SetGeofence(geofence); // uid pid not match locationhub process -- Gitee From 5195c80ce64b9c89453d8e416c06a360ee1905c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E9=BE=99?= Date: Tue, 30 Apr 2024 09:20:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=E8=B6=85=E5=A4=A7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李文龙 --- .../js/napi/include/location_napi_adapter.h | 3 ++ .../js/napi/source/location_napi_adapter.cpp | 46 +++++++++++-------- .../common/source/geofence_request.cpp | 10 +++- .../location_gnss_geofence_callback_host.cpp | 10 ++-- .../gnss/source/gnss_ability.cpp | 4 +- .../locator/source/gnss_ability_proxy.cpp | 4 +- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/frameworks/js/napi/include/location_napi_adapter.h b/frameworks/js/napi/include/location_napi_adapter.h index 989b15f7..30fc58c3 100644 --- a/frameworks/js/napi/include/location_napi_adapter.h +++ b/frameworks/js/napi/include/location_napi_adapter.h @@ -55,6 +55,9 @@ GnssGeofenceAsyncContext* CreateAsyncContextForAddGnssGeofence(const napi_env& e napi_value RemoveGnssGeofence(napi_env env, napi_callback_info info); GnssGeofenceAsyncContext* CreateAsyncContextForRemoveGnssGeofence(const napi_env& env, int fenceId); napi_value GetGeofenceSupportedCoordTypes(napi_env env, napi_callback_info info); +void AddCallbackToGnssGeofenceCallbackHostMap(int fenceId, sptr callbackHost); +void RemoveCallbackToGnssGeofenceCallbackHostMap(int fenceId); +sptr FindCallbackInGnssGeofenceCallbackHostMap(int fenceId); #endif } // namespace Location } // namespace OHOS diff --git a/frameworks/js/napi/source/location_napi_adapter.cpp b/frameworks/js/napi/source/location_napi_adapter.cpp index 601b2643..876cc75d 100644 --- a/frameworks/js/napi/source/location_napi_adapter.cpp +++ b/frameworks/js/napi/source/location_napi_adapter.cpp @@ -1098,10 +1098,7 @@ GnssGeofenceAsyncContext* CreateAsyncContextForAddGnssGeofence(const napi_env& e context->errCode = ERRCODE_SERVICE_UNAVAILABLE; } callbackHost->SetCount(1); - std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); - lock.lock(); - g_gnssGeofenceCallbackHostMap.insert(std::make_pair(callbackHost->GetFenceId(), callbackHost)); - lock.unlock(); + AddCallbackToGnssGeofenceCallbackHostMap(gnssGeofenceRequest->GetFenceId(), callbackHost); } }; asyncContext->completeFunc = [&](void* data) -> void { @@ -1160,13 +1157,7 @@ GnssGeofenceAsyncContext* CreateAsyncContextForRemoveGnssGeofence(const napi_env auto asyncContext = new (std::nothrow) GnssGeofenceAsyncContext(env); NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); asyncContext->fenceId_ = fenceId; - std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); - lock.lock(); - auto iter = g_gnssGeofenceCallbackHostMap.find(fenceId); - if (iter != g_gnssGeofenceCallbackHostMap.end()) { - asyncContext->callbackHost_ = iter->second; - } - lock.unlock(); + asyncContext->callbackHost_ = FindCallbackInGnssGeofenceCallbackHostMap(fenceId); NAPI_CALL(env, napi_create_string_latin1(env, "removeGnssGeofence", NAPI_AUTO_LENGTH, &asyncContext->resourceName)); @@ -1206,13 +1197,7 @@ GnssGeofenceAsyncContext* CreateAsyncContextForRemoveGnssGeofence(const napi_env } else { context->errCode = ERRCODE_GEOFENCE_INCORRECT_ID; } - std::unique_lock lock(g_gnssGeofenceCallbackHostMutex, std::defer_lock); - lock.lock(); - auto iterForDelete = g_gnssGeofenceCallbackHostMap.find(context->fenceId_); - if (iterForDelete != g_gnssGeofenceCallbackHostMap.end()) { - g_gnssGeofenceCallbackHostMap.erase(iterForDelete); - } - lock.unlock(); + RemoveCallbackToGnssGeofenceCallbackHostMap(context->fenceId_); LBSLOGD(LOCATOR_STANDARD, "Push RemoveGnssGeofence result to client"); }; return asyncContext; @@ -1245,6 +1230,31 @@ napi_value GetGeofenceSupportedCoordTypes(napi_env env, napi_callback_info info) } return res; } + +void AddCallbackToGnssGeofenceCallbackHostMap(int fenceId, sptr callbackHost) +{ + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex); + g_gnssGeofenceCallbackHostMap.insert(std::make_pair(fenceId, callbackHost)); +} + +void RemoveCallbackToGnssGeofenceCallbackHostMap(int fenceId) +{ + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex); + auto iterForDelete = g_gnssGeofenceCallbackHostMap.find(fenceId); + if (iterForDelete != g_gnssGeofenceCallbackHostMap.end()) { + g_gnssGeofenceCallbackHostMap.erase(iterForDelete); + } +} + +sptr FindCallbackInGnssGeofenceCallbackHostMap(int fenceId) +{ + std::unique_lock lock(g_gnssGeofenceCallbackHostMutex); + auto iter = g_gnssGeofenceCallbackHostMap.find(fenceId); + if (iter != g_gnssGeofenceCallbackHostMap.end()) { + return iter->second; + } + return nullptr; +} #endif } // namespace Location } // namespace OHOS diff --git a/frameworks/location_common/common/source/geofence_request.cpp b/frameworks/location_common/common/source/geofence_request.cpp index 72f9b419..b3c5695b 100644 --- a/frameworks/location_common/common/source/geofence_request.cpp +++ b/frameworks/location_common/common/source/geofence_request.cpp @@ -49,12 +49,18 @@ void GeofenceRequest::ReadFromParcel(Parcel& data) } for (int i = 0; i < requestSize; i++) { auto request = Notification::NotificationRequest::Unmarshalling(data); - notificationRequestList_.push_back(*request); + if (request != nullptr) { + notificationRequestList_.push_back(*request); + } } #endif callback_ = data.ReadObject(); bundleName_ = data.ReadString(); - wantAgent_ = *(data.ReadParcelable()); + auto wantAgent = data.ReadParcelable(); + if (wantAgent != nullptr) { + wantAgent_ = *(wantAgent); + delete wantAgent; + } } bool GeofenceRequest::Marshalling(Parcel& parcel) const diff --git a/frameworks/native/source/location_gnss_geofence_callback_host.cpp b/frameworks/native/source/location_gnss_geofence_callback_host.cpp index 3c2b3743..02c1c391 100644 --- a/frameworks/native/source/location_gnss_geofence_callback_host.cpp +++ b/frameworks/native/source/location_gnss_geofence_callback_host.cpp @@ -72,8 +72,6 @@ int LocationGnssGeofenceCallbackHost::OnRemoteRequest( int fenceId = data.ReadInt32(); int type = data.ReadInt32(); int result = data.ReadInt32(); - SetFenceId(fenceId); - SetGeofenceOperationType(static_cast(type)); OnReportOperationResult(fenceId, type, result); CountDown(); break; @@ -160,11 +158,9 @@ void LocationGnssGeofenceCallbackHost::UvQueueWork(uv_loop_s* loop, uv_work_t* w napi_value jsEvent[PARAM2]; CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_create_object(context->env, &jsEvent[PARAM1]), scope, context, work); - if (context->code_ == RECEIVE_TRANSITION_STATUS_EVENT) { - CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &jsEvent[PARAM0]), - scope, context, work); - GeofenceTransitionToJs(context->env, context->transition_, jsEvent[PARAM1]); - } + CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &jsEvent[PARAM0]), + scope, context, work); + GeofenceTransitionToJs(context->env, context->transition_, jsEvent[PARAM1]); if (context->callback[SUCCESS_CALLBACK] != nullptr) { napi_value undefine; napi_value handler = nullptr; diff --git a/services/location_gnss/gnss/source/gnss_ability.cpp b/services/location_gnss/gnss/source/gnss_ability.cpp index 6c76717a..1abe521a 100644 --- a/services/location_gnss/gnss/source/gnss_ability.cpp +++ b/services/location_gnss/gnss/source/gnss_ability.cpp @@ -484,10 +484,10 @@ LocationErrCode GnssAbility::SetPositionMode() LocationErrCode GnssAbility::AddFence(std::shared_ptr& request) { - auto geofence = request->GetGeofence(); int fenceId = GenerateFenceId(); request->SetFenceId(fenceId); #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE + auto geofence = request->GetGeofence(); if (geofenceInterface_ == nullptr) { LBSLOGE(GNSS, "geofenceInterface_ is nullptr"); return ERRCODE_SERVICE_UNAVAILABLE; @@ -538,10 +538,10 @@ int32_t GnssAbility::GenerateFenceId() LocationErrCode GnssAbility::AddGnssGeofence(std::shared_ptr& request) { - auto geofence = request->GetGeofence(); int fenceId = GenerateFenceId(); request->SetFenceId(fenceId); #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE + auto geofence = request->GetGeofence(); if (geofenceInterface_ == nullptr) { LBSLOGE(GNSS, "geofenceInterface_ is nullptr"); return ERRCODE_SERVICE_UNAVAILABLE; diff --git a/services/location_locator/locator/source/gnss_ability_proxy.cpp b/services/location_locator/locator/source/gnss_ability_proxy.cpp index f727ec79..b092bccf 100644 --- a/services/location_locator/locator/source/gnss_ability_proxy.cpp +++ b/services/location_locator/locator/source/gnss_ability_proxy.cpp @@ -427,8 +427,8 @@ LocationErrCode GnssAbilityProxy::QuerySupportCoordinateSystemType( LBSLOGE(GNSS, "write interfaceToken fail!"); return ERRCODE_SERVICE_UNAVAILABLE; } - int error = - remote->SendRequest(static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply, option); + int error = remote->SendRequest( + static_cast(GnssInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE), data, reply, option); LBSLOGD(GNSS, "%{public}s Transact Error = %{public}d", __func__, error); return LocationErrCode(reply.ReadInt32()); } -- Gitee