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 "locator_callback_proxy.h"
17
18 #include "ipc_skeleton.h"
19
20 #include "common_utils.h"
21 #include "location_log.h"
22 #include "locator_background_proxy.h"
23
24 namespace OHOS {
25 namespace Location {
LocatorCallbackProxy(const sptr<IRemoteObject> & impl)26 LocatorCallbackProxy::LocatorCallbackProxy(const sptr<IRemoteObject> &impl)
27 : IRemoteProxy<ILocatorCallback>(impl)
28 {
29 LBSLOGD(LOCATOR_CALLBACK, "construct");
30 }
31
OnLocationReport(const std::unique_ptr<Location> & location)32 void LocatorCallbackProxy::OnLocationReport(const std::unique_ptr<Location>& location)
33 {
34 if (location == nullptr) {
35 return;
36 }
37 MessageParcel data;
38 MessageParcel reply;
39 if (!data.WriteInterfaceToken(GetDescriptor())) {
40 return;
41 }
42 location->Marshalling(data);
43 MessageOption option;
44 auto locatorBackgroundProxy = LocatorBackgroundProxy::GetInstance();
45 if (locatorBackgroundProxy->IsCallbackInProxy(this)) {
46 // if callback is from locatorBackgroundProxy, should send sync message to wake up app
47 option = { MessageOption::TF_SYNC };
48 LBSLOGD(LOCATOR_CALLBACK, "OnLocationReport Transact TF_SYNC");
49 } else {
50 option = { MessageOption::TF_ASYNC };
51 }
52 int error = Remote()->SendRequest(ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT, data, reply, option);
53 if (error != ERR_OK) {
54 LBSLOGI(LOCATOR_CALLBACK, "OnLocationReport Transact ErrCode = %{public}d", error);
55 }
56 }
57
OnLocatingStatusChange(const int status)58 void LocatorCallbackProxy::OnLocatingStatusChange(const int status)
59 {
60 MessageParcel data;
61 MessageParcel reply;
62 if (!data.WriteInterfaceToken(GetDescriptor())) {
63 return;
64 }
65 data.WriteInt32(status);
66 MessageOption option = { MessageOption::TF_ASYNC };
67 int error = Remote()->SendRequest(RECEIVE_LOCATION_STATUS_EVENT, data, reply, option);
68 if (error != ERR_OK) {
69 LBSLOGI(LOCATOR_CALLBACK, "OnLocatingStatusChange Transact ErrCode = %{public}d", error);
70 }
71 }
72
OnErrorReport(const int errorCode)73 void LocatorCallbackProxy::OnErrorReport(const int errorCode)
74 {
75 MessageParcel data;
76 MessageParcel reply;
77 if (!data.WriteInterfaceToken(GetDescriptor())) {
78 return;
79 }
80 data.WriteInt32(errorCode);
81 MessageOption option = { MessageOption::TF_ASYNC };
82 int error = Remote()->SendRequest(RECEIVE_ERROR_INFO_EVENT, data, reply, option);
83 if (error != ERR_OK) {
84 LBSLOGI(LOCATOR_CALLBACK, "OnErrorReport:%{public}d, Transact ErrCode = %{public}d", errorCode, error);
85 }
86 }
87
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)88 int LocatorCallbackStub::OnRemoteRequest(uint32_t code,
89 MessageParcel &data, MessageParcel &reply, MessageOption &option)
90 {
91 if (data.ReadInterfaceToken() != GetDescriptor()) {
92 LBSLOGE(LOCATOR_CALLBACK, "invalid token.");
93 return REPLY_CODE_EXCEPTION;
94 }
95 pid_t callingPid = IPCSkeleton::GetCallingPid();
96 pid_t callingUid = IPCSkeleton::GetCallingUid();
97 LBSLOGI(LOCATOR_CALLBACK, "OnReceived cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
98 code, option.GetFlags(), callingPid, callingUid);
99 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
100 }
101
OnLocationReport(const std::unique_ptr<Location> & location)102 void LocatorCallbackStub::OnLocationReport(const std::unique_ptr<Location>& location)
103 {
104 }
105
OnLocatingStatusChange(const int status)106 void LocatorCallbackStub::OnLocatingStatusChange(const int status)
107 {
108 }
109
OnErrorReport(const int errorCode)110 void LocatorCallbackStub::OnErrorReport(const int errorCode)
111 {
112 }
113 } // namespace Location
114 } // namespace OHOS
115