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