1 /*
2 * Copyright (c) 2024 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 "location_log.h"
17 #include "cj_lambda.h"
18 #include "location_switch_callback.h"
19
20 namespace OHOS {
21 namespace GeoLocationManager {
LocationSwitchCallback()22 LocationSwitchCallback::LocationSwitchCallback()
23 {
24 remoteDied_ = false;
25 }
26
LocationSwitchCallback(int64_t callbackId)27 LocationSwitchCallback::LocationSwitchCallback(int64_t callbackId)
28 {
29 remoteDied_ = false;
30 this->callbackId_ = callbackId;
31 auto cFunc = reinterpret_cast<void(*)(int32_t switchState)>(callbackId);
32 callback_ = [ lambda = CJLambda::Create(cFunc)](int switchState) ->
33 void { lambda(static_cast<bool>(switchState)); };
34 }
35
~LocationSwitchCallback()36 LocationSwitchCallback::~LocationSwitchCallback()
37 {
38 }
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int LocationSwitchCallback::OnRemoteRequest(
41 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
42 {
43 LBSLOGD(Location::SWITCH_CALLBACK, "LocatorCallback::OnRemoteRequest!");
44 if (data.ReadInterfaceToken() != GetDescriptor()) {
45 LBSLOGE(Location::SWITCH_CALLBACK, "invalid token.");
46 return -1;
47 }
48 if (remoteDied_) {
49 LBSLOGD(Location::SWITCH_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
50 return -1;
51 }
52
53 switch (code) {
54 case Location::ISwitchCallback::RECEIVE_SWITCH_STATE_EVENT: {
55 OnSwitchChange(data.ReadInt32());
56 break;
57 }
58 default: {
59 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 break;
61 }
62 }
63 return 0;
64 }
65
IsRemoteDied()66 bool LocationSwitchCallback::IsRemoteDied()
67 {
68 return remoteDied_;
69 }
70
OnSwitchChange(int switchState)71 void LocationSwitchCallback::OnSwitchChange(int switchState)
72 {
73 LBSLOGD(Location::SWITCH_CALLBACK, "LocatorCallback::OnSwitchChange");
74 std::unique_lock<std::mutex> guard(mutex_);
75 if (callback_ != nullptr) {
76 callback_(switchState);
77 }
78 }
79 }
80 }