• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifdef FEATURE_GEOCODE_SUPPORT
17 #include "geo_convert_skeleton.h"
18 #include "common_utils.h"
19 #include "ipc_skeleton.h"
20 #include "location_log.h"
21 #include "locationhub_ipc_interface_code.h"
22 
23 namespace OHOS {
24 namespace Location {
InitGeoConvertHandleMap()25 void GeoConvertServiceStub::InitGeoConvertHandleMap()
26 {
27     if (geoConvertMsgHandleMap_.size() != 0) {
28         return;
29     }
30     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::IS_AVAILABLE)] =
31         &GeoConvertServiceStub::IsGeoConvertAvailableInner;
32     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_COORDINATE)] =
33         &GeoConvertServiceStub::GetAddressByCoordinateInner;
34     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::GET_FROM_LOCATION_NAME_BY_BOUNDARY)] =
35         &GeoConvertServiceStub::GetAddressByLocationNameInner;
36     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK)] =
37         &GeoConvertServiceStub::EnableReverseGeocodingMockInner;
38     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK)] =
39         &GeoConvertServiceStub::DisableReverseGeocodingMockInner;
40     geoConvertMsgHandleMap_[static_cast<uint32_t>(GeoConvertInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO)] =
41         &GeoConvertServiceStub::SetGeocodingMockInfoInner;
42 }
43 
GeoConvertServiceStub()44 GeoConvertServiceStub::GeoConvertServiceStub()
45 {
46     InitGeoConvertHandleMap();
47 }
48 
ParseGeocodingMockInfos(MessageParcel & data)49 std::vector<std::shared_ptr<GeocodingMockInfo>> GeoConvertServiceStub::ParseGeocodingMockInfos(MessageParcel &data)
50 {
51     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
52     int arraySize = data.ReadInt32();
53     arraySize = arraySize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX :
54         arraySize;
55     if (arraySize <= 0) {
56         return std::vector<std::shared_ptr<GeocodingMockInfo>>();
57     }
58     for (int i = 0; i < arraySize; i++) {
59         std::shared_ptr<GeocodingMockInfo> info = std::make_shared<GeocodingMockInfo>();
60         info->ReadFromParcel(data);
61         mockInfo.push_back(info);
62     }
63     return mockInfo;
64 }
65 
IsGeoConvertAvailableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)66 int GeoConvertServiceStub::IsGeoConvertAvailableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
67 {
68     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
69         return ERRCODE_PERMISSION_DENIED;
70     }
71     IsGeoConvertAvailable(reply);
72     return ERRCODE_SUCCESS;
73 }
74 
GetAddressByCoordinateInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)75 int GeoConvertServiceStub::GetAddressByCoordinateInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
76 {
77     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
78         return ERRCODE_PERMISSION_DENIED;
79     }
80     GetAddressByCoordinate(data, reply);
81     return ERRCODE_SUCCESS;
82 }
83 
GetAddressByLocationNameInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)84 int GeoConvertServiceStub::GetAddressByLocationNameInner(
85     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
86 {
87     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
88         return ERRCODE_PERMISSION_DENIED;
89     }
90     GetAddressByLocationName(data, reply);
91     return ERRCODE_SUCCESS;
92 }
93 
EnableReverseGeocodingMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)94 int GeoConvertServiceStub::EnableReverseGeocodingMockInner(
95     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
96 {
97     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
98         return ERRCODE_PERMISSION_DENIED;
99     }
100     EnableReverseGeocodingMock() ? reply.WriteInt32(ERRCODE_SUCCESS) :
101         reply.WriteInt32(ERRCODE_REVERSE_GEOCODING_FAIL);
102     return ERRCODE_SUCCESS;
103 }
104 
DisableReverseGeocodingMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)105 int GeoConvertServiceStub::DisableReverseGeocodingMockInner(
106     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
107 {
108     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
109         return ERRCODE_PERMISSION_DENIED;
110     }
111     DisableReverseGeocodingMock() ? reply.WriteInt32(ERRCODE_SUCCESS) :
112         reply.WriteInt32(ERRCODE_REVERSE_GEOCODING_FAIL);
113     return ERRCODE_SUCCESS;
114 }
115 
SetGeocodingMockInfoInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)116 int GeoConvertServiceStub::SetGeocodingMockInfoInner(
117     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
118 {
119     if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
120         return ERRCODE_PERMISSION_DENIED;
121     }
122     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo = ParseGeocodingMockInfos(data);
123     reply.WriteInt32(SetReverseGeocodingMockInfo(mockInfo));
124     return ERRCODE_SUCCESS;
125 }
126 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)127 int GeoConvertServiceStub::OnRemoteRequest(uint32_t code,
128     MessageParcel &data, MessageParcel &reply, MessageOption &option)
129 {
130     pid_t callingPid = IPCSkeleton::GetCallingPid();
131     pid_t callingUid = IPCSkeleton::GetCallingUid();
132     AppIdentity identity;
133     identity.SetPid(callingPid);
134     identity.SetUid(callingUid);
135     LBSLOGI(GEO_CONVERT, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
136         code, option.GetFlags(), callingPid, callingUid);
137     if (data.ReadInterfaceToken() != GetDescriptor()) {
138         LBSLOGE(GEO_CONVERT, "invalid token.");
139         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
140         return ERRCODE_SERVICE_UNAVAILABLE;
141     }
142 
143     int ret = ERRCODE_SUCCESS;
144     auto handleFunc = geoConvertMsgHandleMap_.find(code);
145     if (handleFunc != geoConvertMsgHandleMap_.end() && handleFunc->second != nullptr) {
146         auto memberFunc = handleFunc->second;
147         ret = (this->*memberFunc)(data, reply, identity);
148     } else {
149         LBSLOGE(GEO_CONVERT, "OnReceived cmd = %{public}u, unsupport service.", code);
150 #if !defined(FEATURE_GNSS_SUPPORT) || !defined(FEATURE_GEOCODE_SUPPORT)
151         reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
152 #endif
153         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
154     }
155     UnloadGeoConvertSystemAbility();
156     return ret;
157 }
158 } // namespace Location
159 } // namespace OHOS
160 #endif
161