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 #ifdef FEATURE_PASSIVE_SUPPORT
17 #include "passive_ability_skeleton.h"
18
19 #include "ipc_object_stub.h"
20 #include "ipc_skeleton.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 #include "common_utils.h"
25 #include "location.h"
26 #include "location_log.h"
27 #include "work_record.h"
28 #include "locationhub_ipc_interface_code.h"
29
30 namespace OHOS {
31 namespace Location {
InitPassiveMsgHandleMap()32 void PassiveAbilityStub::InitPassiveMsgHandleMap()
33 {
34 if (PassiveMsgHandleMap_.size() != 0) {
35 return;
36 }
37 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SEND_LOCATION_REQUEST)] =
38 &PassiveAbilityStub::SendLocationRequestInner;
39 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SET_ENABLE)] =
40 &PassiveAbilityStub::SetEnableInner;
41 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::ENABLE_LOCATION_MOCK)] =
42 &PassiveAbilityStub::EnableMockInner;
43 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::DISABLE_LOCATION_MOCK)] =
44 &PassiveAbilityStub::DisableMockInner;
45 PassiveMsgHandleMap_[static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS)] =
46 &PassiveAbilityStub::SetMockedLocationsInner;
47 }
48
PassiveAbilityStub()49 PassiveAbilityStub::PassiveAbilityStub()
50 {
51 InitPassiveMsgHandleMap();
52 }
53
SendLocationRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)54 int PassiveAbilityStub::SendLocationRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
55 {
56 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
57 return ERRCODE_PERMISSION_DENIED;
58 }
59 std::unique_ptr<WorkRecord> workrecord = WorkRecord::Unmarshalling(data);
60 reply.WriteInt32(SendLocationRequest(*workrecord));
61 return ERRCODE_SUCCESS;
62 }
63
SetEnableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)64 int PassiveAbilityStub::SetEnableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
65 {
66 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
67 return ERRCODE_PERMISSION_DENIED;
68 }
69 reply.WriteInt32(SetEnable(data.ReadBool()));
70 return ERRCODE_SUCCESS;
71 }
72
EnableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)73 int PassiveAbilityStub::EnableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
74 {
75 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
76 return ERRCODE_PERMISSION_DENIED;
77 }
78 reply.WriteInt32(EnableMock());
79 return ERRCODE_SUCCESS;
80 }
81
DisableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)82 int PassiveAbilityStub::DisableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
83 {
84 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
85 return ERRCODE_PERMISSION_DENIED;
86 }
87 reply.WriteInt32(DisableMock());
88 return ERRCODE_SUCCESS;
89 }
90
SetMockedLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)91 int PassiveAbilityStub::SetMockedLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
92 {
93 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
94 return ERRCODE_PERMISSION_DENIED;
95 }
96 SendMessage(static_cast<uint32_t>(PassiveInterfaceCode::SET_MOCKED_LOCATIONS), data, reply);
97 isMessageRequest_ = true;
98 return ERRCODE_SUCCESS;
99 }
100
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)101 int PassiveAbilityStub::OnRemoteRequest(uint32_t code,
102 MessageParcel &data, MessageParcel &reply, MessageOption &option)
103 {
104 pid_t callingPid = IPCSkeleton::GetCallingPid();
105 pid_t callingUid = IPCSkeleton::GetCallingUid();
106 AppIdentity identity;
107 identity.SetPid(callingPid);
108 identity.SetUid(callingUid);
109 LBSLOGD(PASSIVE, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
110 code, option.GetFlags(), callingPid, callingUid);
111
112 if (data.ReadInterfaceToken() != GetDescriptor()) {
113 LBSLOGE(PASSIVE, "invalid token.");
114 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
115 return ERRCODE_SERVICE_UNAVAILABLE;
116 }
117 int ret = ERRCODE_SUCCESS;
118 isMessageRequest_ = false;
119 auto handleFunc = PassiveMsgHandleMap_.find(code);
120 if (handleFunc != PassiveMsgHandleMap_.end() && handleFunc->second != nullptr) {
121 auto memberFunc = handleFunc->second;
122 ret = (this->*memberFunc)(data, reply, identity);
123 } else {
124 LBSLOGE(PASSIVE, "OnReceived cmd = %{public}u, unsupport service.", code);
125 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
126 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
127 }
128 if (!isMessageRequest_) {
129 UnloadPassiveSystemAbility();
130 }
131 return ret;
132 }
133 } // namespace Location
134 } // namespace OHOS
135 #endif // FEATURE_PASSIVE_SUPPORT
136