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_proxy.h"
18 #include "location_log.h"
19 #include "locationhub_ipc_interface_code.h"
20 
21 namespace OHOS {
22 namespace Location {
GeoConvertProxy(const sptr<IRemoteObject> & impl)23 GeoConvertProxy::GeoConvertProxy(const sptr<IRemoteObject> &impl)
24     : IRemoteProxy<IGeoConvert>(impl)
25 {
26 }
27 
IsGeoConvertAvailable(MessageParcel & reply)28 int GeoConvertProxy::IsGeoConvertAvailable(MessageParcel &reply)
29 {
30     return SendSimpleMsg(static_cast<int>(GeoConvertInterfaceCode::IS_AVAILABLE), reply);
31 }
32 
GetAddressByCoordinate(MessageParcel & data,MessageParcel & reply)33 int GeoConvertProxy::GetAddressByCoordinate(MessageParcel &data, MessageParcel &reply)
34 {
35     int error = ERRCODE_SERVICE_UNAVAILABLE;
36     MessageOption option;
37     if (!data.WriteInterfaceToken(GetDescriptor())) {
38         LBSLOGE(GEO_CONVERT, "write interfaceToken fail!");
39         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
40         return ERRCODE_SERVICE_UNAVAILABLE;
41     }
42     error = SendMsgWithDataReply(static_cast<int>(GeoConvertInterfaceCode::GET_FROM_COORDINATE), data, reply);
43     LBSLOGI(GEO_CONVERT, "GetAddressByCoordinate result from server.");
44     return error;
45 }
46 
GetAddressByLocationName(MessageParcel & data,MessageParcel & reply)47 int GeoConvertProxy::GetAddressByLocationName(MessageParcel &data, MessageParcel &reply)
48 {
49     int error = ERRCODE_SERVICE_UNAVAILABLE;
50     MessageOption option;
51     if (!data.WriteInterfaceToken(GetDescriptor())) {
52         LBSLOGE(GEO_CONVERT, "write interfaceToken fail!");
53         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
54         return ERRCODE_SERVICE_UNAVAILABLE;
55     }
56     error = SendMsgWithDataReply(static_cast<int>(GeoConvertInterfaceCode::GET_FROM_LOCATION_NAME_BY_BOUNDARY),
57                                  data,
58                                  reply);
59     LBSLOGI(GEO_CONVERT, "GetAddressByLocationName result from server.");
60     return error;
61 }
62 
SendSimpleMsg(const int msgId,MessageParcel & reply)63 int GeoConvertProxy::SendSimpleMsg(const int msgId, MessageParcel& reply)
64 {
65     int error = ERRCODE_SERVICE_UNAVAILABLE;
66     MessageParcel data;
67     MessageOption option;
68     if (!data.WriteInterfaceToken(GetDescriptor())) {
69         LBSLOGE(GEO_CONVERT, "write interfaceToken fail!");
70         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
71         return ERRCODE_SERVICE_UNAVAILABLE;
72     }
73     error = SendMsgWithDataReply(msgId, data, reply);
74     return error;
75 }
76 
SendMsgWithDataReply(const int msgId,MessageParcel & data,MessageParcel & reply)77 int GeoConvertProxy::SendMsgWithDataReply(const int msgId, MessageParcel& data, MessageParcel& reply)
78 {
79     int error = ERRCODE_SERVICE_UNAVAILABLE;
80     MessageOption option;
81     sptr<IRemoteObject> remote = Remote();
82     if (remote == nullptr) {
83         LBSLOGE(GEO_CONVERT, "SendMsgWithDataReply remote is null");
84         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
85         return ERRCODE_SERVICE_UNAVAILABLE;
86     }
87     error = remote->SendRequest(msgId, data, reply, option);
88     LBSLOGD(GEO_CONVERT, "Proxy::SendMsgWithDataReply result from server.");
89     return error;
90 }
91 
SendSimpleMsgAndParseResult(const int msgId)92 bool GeoConvertProxy::SendSimpleMsgAndParseResult(const int msgId)
93 {
94     bool result = false;
95     MessageParcel reply;
96     int error = SendSimpleMsg(msgId, reply);
97     if (error == NO_ERROR) {
98         result = (reply.ReadInt32() == ERRCODE_SUCCESS);
99     }
100     return result;
101 }
102 
EnableReverseGeocodingMock()103 bool GeoConvertProxy::EnableReverseGeocodingMock()
104 {
105     return SendSimpleMsgAndParseResult(static_cast<int>(GeoConvertInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK));
106 }
107 
DisableReverseGeocodingMock()108 bool GeoConvertProxy::DisableReverseGeocodingMock()
109 {
110     return SendSimpleMsgAndParseResult(static_cast<int>(GeoConvertInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK));
111 }
112 
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)113 LocationErrCode GeoConvertProxy::SetReverseGeocodingMockInfo(
114     std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
115 {
116     MessageParcel data;
117     MessageParcel reply;
118     MessageOption option;
119     if (!data.WriteInterfaceToken(GetDescriptor())) {
120         LBSLOGE(GEO_CONVERT, "write interfaceToken fail!");
121         return ERRCODE_SERVICE_UNAVAILABLE;
122     }
123     data.WriteInt32(mockInfo.size());
124     for (size_t i = 0; i < mockInfo.size(); i++) {
125         mockInfo[i]->Marshalling(data);
126     }
127     SendMsgWithDataReply(static_cast<int>(GeoConvertInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO), data, reply);
128     return LocationErrCode(reply.ReadInt32());
129 }
130 } // namespace Location
131 } // namespace OHOS
132 #endif
133