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 "gnss_ability_skeleton.h"
17
18 #include "ipc_skeleton.h"
19
20 #include "common_utils.h"
21
22 namespace OHOS {
23 namespace Location {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int GnssAbilityStub::OnRemoteRequest(uint32_t code,
25 MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27 pid_t lastCallingPid = IPCSkeleton::GetCallingPid();
28 pid_t lastCallinguid = IPCSkeleton::GetCallingUid();
29 LBSLOGI(GNSS, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
30 code, option.GetFlags(), lastCallingPid, lastCallinguid);
31 if (lastCallinguid > SYSTEM_UID) {
32 LBSLOGE(GNSS, "this remote request is not allowed");
33 return EXCEPTION;
34 }
35 if (data.ReadInterfaceToken() != GetDescriptor()) {
36 LBSLOGE(GNSS, "invalid token.");
37 return EXCEPTION;
38 }
39
40 int ret = REPLY_NO_EXCEPTION;
41 switch (code) {
42 case SEND_LOCATION_REQUEST: {
43 int64_t interval = data.ReadInt64();
44 std::unique_ptr<WorkRecord> workrecord = WorkRecord::Unmarshalling(data);
45 SendLocationRequest((uint64_t)interval, *workrecord);
46 break;
47 }
48 case GET_CACHED_LOCATION: {
49 std::unique_ptr<Location> location = GetCachedLocation();
50 if (location != nullptr) {
51 location->Marshalling(reply);
52 }
53 break;
54 }
55 case SET_ENABLE: {
56 SetEnable(data.ReadBool());
57 break;
58 }
59 case HANDLE_REMOTE_REQUEST: {
60 bool state = data.ReadBool();
61 RemoteRequest(state);
62 break;
63 }
64 case REFRESH_REQUESTS: {
65 RefrashRequirements();
66 break;
67 }
68 case REG_GNSS_STATUS: {
69 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
70 RegisterGnssStatusCallback(client, lastCallinguid);
71 break;
72 }
73 case UNREG_GNSS_STATUS: {
74 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
75 UnregisterGnssStatusCallback(client);
76 break;
77 }
78 case REG_NMEA: {
79 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
80 RegisterNmeaMessageCallback(client, lastCallinguid);
81 break;
82 }
83 case UNREG_NMEA: {
84 sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
85 UnregisterNmeaMessageCallback(client);
86 break;
87 }
88 case REG_CACHED: {
89 std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
90 requestConfig->reportingPeriodSec = data.ReadInt32();
91 requestConfig->wakeUpCacheQueueFull = data.ReadBool();
92 sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
93 RegisterCachedCallback(requestConfig, callback);
94 break;
95 }
96 case UNREG_CACHED: {
97 sptr<IRemoteObject> callback = data.ReadObject<IRemoteObject>();
98 UnregisterCachedCallback(callback);
99 break;
100 }
101 case GET_CACHED_SIZE: {
102 reply.WriteInt32(GetCachedGnssLocationsSize());
103 break;
104 }
105 case FLUSH_CACHED: {
106 ret = FlushCachedGnssLocations();
107 break;
108 }
109 case SEND_COMMANDS: {
110 std::unique_ptr<LocationCommand> locationCommand = std::make_unique<LocationCommand>();
111 locationCommand->scenario = data.ReadInt32();
112 locationCommand->command = data.ReadBool();
113 SendCommand(locationCommand);
114 break;
115 }
116 default:
117 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
118 }
119 return ret;
120 }
121 } // namespace Location
122 } // namespace OHOS