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