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 = DelayedSingleton<LocatorBackgroundProxy>::GetInstance();
45 if (locatorBackgroundProxy == nullptr) {
46 LBSLOGE(LOCATOR_CALLBACK, "OnLocationReport: locator background proxy is nullptr.");
47 return;
48 }
49 if (locatorBackgroundProxy.get()->IsCallbackInProxy(this)) {
50 // if callback is from locatorBackgroundProxy, should send sync message to wake up app
51 option = { MessageOption::TF_SYNC };
52 LBSLOGD(LOCATOR_CALLBACK, "OnLocationReport Transact TF_SYNC");
53 } else {
54 option = { MessageOption::TF_ASYNC };
55 }
56 int error = Remote()->SendRequest(ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT, data, reply, option);
57 LBSLOGD(LOCATOR_CALLBACK, "OnLocationReport Transact ErrCode = %{public}d", error);
58 }
59
OnLocatingStatusChange(const int status)60 void LocatorCallbackProxy::OnLocatingStatusChange(const int status)
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 if (!data.WriteInterfaceToken(GetDescriptor())) {
65 return;
66 }
67 data.WriteInt32(status);
68 MessageOption option = { MessageOption::TF_ASYNC };
69 int error = Remote()->SendRequest(RECEIVE_LOCATION_STATUS_EVENT, data, reply, option);
70 LBSLOGD(LOCATOR_CALLBACK, "OnLocatingStatusChange Transact ErrCode = %{public}d", error);
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 LBSLOGD(LOCATOR_CALLBACK, "OnErrorReport:%{public}d, Transact ErrCode = %{public}d", errorCode, error);
84 }
85
86
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)87 int LocatorCallbackStub::OnRemoteRequest(uint32_t code,
88 MessageParcel &data, MessageParcel &reply, MessageOption &option)
89 {
90 if (data.ReadInterfaceToken() != GetDescriptor()) {
91 LBSLOGE(LOCATOR_CALLBACK, "invalid token.");
92 return REPLY_CODE_EXCEPTION;
93 }
94 pid_t callingPid = IPCSkeleton::GetCallingPid();
95 pid_t callingUid = IPCSkeleton::GetCallingUid();
96 LBSLOGI(LOCATOR_CALLBACK, "OnReceived cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
97 code, option.GetFlags(), callingPid, callingUid);
98 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
99 }
100
OnLocationReport(const std::unique_ptr<Location> & location)101 void LocatorCallbackStub::OnLocationReport(const std::unique_ptr<Location>& location)
102 {
103 }
104
OnLocatingStatusChange(const int status)105 void LocatorCallbackStub::OnLocatingStatusChange(const int status)
106 {
107 }
108
OnErrorReport(const int errorCode)109 void LocatorCallbackStub::OnErrorReport(const int errorCode)
110 {
111 }
112 } // namespace Location
113 } // namespace OHOS
114