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 == nullptr) {
46         LBSLOGE(LOCATOR_CALLBACK, "OnLocationReport: locator background proxy is nullptr.");
47         return;
48     }
49     if (locatorBackgroundProxy->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     if (error != ERR_OK) {
58         LBSLOGI(LOCATOR_CALLBACK, "OnLocationReport Transact ErrCode = %{public}d", error);
59     }
60 }
61 
OnLocatingStatusChange(const int status)62 void LocatorCallbackProxy::OnLocatingStatusChange(const int status)
63 {
64     MessageParcel data;
65     MessageParcel reply;
66     if (!data.WriteInterfaceToken(GetDescriptor())) {
67         return;
68     }
69     data.WriteInt32(status);
70     MessageOption option = { MessageOption::TF_ASYNC };
71     int error = Remote()->SendRequest(RECEIVE_LOCATION_STATUS_EVENT, data, reply, option);
72     if (error != ERR_OK) {
73         LBSLOGI(LOCATOR_CALLBACK, "OnLocatingStatusChange Transact ErrCode = %{public}d", error);
74     }
75 }
76 
OnErrorReport(const int errorCode)77 void LocatorCallbackProxy::OnErrorReport(const int errorCode)
78 {
79     MessageParcel data;
80     MessageParcel reply;
81     if (!data.WriteInterfaceToken(GetDescriptor())) {
82         return;
83     }
84     data.WriteInt32(errorCode);
85     MessageOption option = { MessageOption::TF_ASYNC };
86     int error = Remote()->SendRequest(RECEIVE_ERROR_INFO_EVENT, data, reply, option);
87     if (error != ERR_OK) {
88         LBSLOGI(LOCATOR_CALLBACK, "OnErrorReport:%{public}d, Transact ErrCode = %{public}d", errorCode, error);
89     }
90 }
91 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)92 int LocatorCallbackStub::OnRemoteRequest(uint32_t code,
93     MessageParcel &data, MessageParcel &reply, MessageOption &option)
94 {
95     if (data.ReadInterfaceToken() != GetDescriptor()) {
96         LBSLOGE(LOCATOR_CALLBACK, "invalid token.");
97         return REPLY_CODE_EXCEPTION;
98     }
99     pid_t callingPid = IPCSkeleton::GetCallingPid();
100     pid_t callingUid = IPCSkeleton::GetCallingUid();
101     LBSLOGI(LOCATOR_CALLBACK, "OnReceived cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
102         code, option.GetFlags(), callingPid, callingUid);
103     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
104 }
105 
OnLocationReport(const std::unique_ptr<Location> & location)106 void LocatorCallbackStub::OnLocationReport(const std::unique_ptr<Location>& location)
107 {
108 }
109 
OnLocatingStatusChange(const int status)110 void LocatorCallbackStub::OnLocatingStatusChange(const int status)
111 {
112 }
113 
OnErrorReport(const int errorCode)114 void LocatorCallbackStub::OnErrorReport(const int errorCode)
115 {
116 }
117 } // namespace Location
118 } // namespace OHOS
119