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