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_NETWORK_SUPPORT
17 #include "network_ability_skeleton.h"
18
19 #include <vector>
20
21 #include "ipc_object_stub.h"
22 #include "ipc_skeleton.h"
23
24 #include "common_utils.h"
25 #include "location.h"
26 #include "location_log.h"
27 #include "subability_common.h"
28 #include "work_record.h"
29 #include "locationhub_ipc_interface_code.h"
30
31 namespace OHOS {
32 namespace Location {
InitNetworkMsgHandleMap()33 void NetworkAbilityStub::InitNetworkMsgHandleMap()
34 {
35 if (NetworkMsgHandleMap_.size() != 0) {
36 return;
37 }
38 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST)] =
39 &NetworkAbilityStub::SendLocationRequestInner;
40 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS)] =
41 &NetworkAbilityStub::SetMockLocationsInner;
42 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST)] =
43 &NetworkAbilityStub::SelfRequestInner;
44 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE)] =
45 &NetworkAbilityStub::SetEnableInner;
46 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK)] =
47 &NetworkAbilityStub::EnableMockInner;
48 NetworkMsgHandleMap_[static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK)] =
49 &NetworkAbilityStub::DisableMockInner;
50 }
51
NetworkAbilityStub()52 NetworkAbilityStub::NetworkAbilityStub()
53 {
54 InitNetworkMsgHandleMap();
55 }
56
SendLocationRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)57 int NetworkAbilityStub::SendLocationRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
58 {
59 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
60 return ERRCODE_PERMISSION_DENIED;
61 }
62 SendMessage(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST), data, reply);
63 isMessageRequest_ = true;
64 return ERRCODE_SUCCESS;
65 }
66
SetMockLocationsInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)67 int NetworkAbilityStub::SetMockLocationsInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
68 {
69 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
70 return ERRCODE_PERMISSION_DENIED;
71 }
72 SendMessage(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS), data, reply);
73 isMessageRequest_ = true;
74 return ERRCODE_SUCCESS;
75 }
76
SelfRequestInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)77 int NetworkAbilityStub::SelfRequestInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
78 {
79 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
80 return ERRCODE_PERMISSION_DENIED;
81 }
82 if (CheckLocationSwitchState(reply)) {
83 return ERRCODE_SWITCH_OFF;
84 }
85 SendMessage(static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST), data, reply);
86 isMessageRequest_ = true;
87 return ERRCODE_SUCCESS;
88 }
89
SetEnableInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)90 int NetworkAbilityStub::SetEnableInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
91 {
92 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
93 return ERRCODE_PERMISSION_DENIED;
94 }
95 reply.WriteInt32(SetEnable(data.ReadBool()));
96 return ERRCODE_SUCCESS;
97 }
98
EnableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)99 int NetworkAbilityStub::EnableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
100 {
101 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
102 return ERRCODE_PERMISSION_DENIED;
103 }
104 reply.WriteInt32(EnableMock());
105 return ERRCODE_SUCCESS;
106 }
107
DisableMockInner(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)108 int NetworkAbilityStub::DisableMockInner(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
109 {
110 if (!CommonUtils::CheckCallingPermission(identity.GetUid(), identity.GetPid(), reply)) {
111 return ERRCODE_PERMISSION_DENIED;
112 }
113 reply.WriteInt32(DisableMock());
114 return ERRCODE_SUCCESS;
115 }
116
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)117 int NetworkAbilityStub::OnRemoteRequest(uint32_t code,
118 MessageParcel &data, MessageParcel &reply, MessageOption &option)
119 {
120 pid_t callingPid = IPCSkeleton::GetCallingPid();
121 pid_t callingUid = IPCSkeleton::GetCallingUid();
122 AppIdentity identity;
123 identity.SetPid(callingPid);
124 identity.SetUid(callingUid);
125 LBSLOGD(NETWORK, "OnRemoteRequest cmd = %{public}u, flags= %{public}d, pid= %{public}d, uid= %{public}d",
126 code, option.GetFlags(), callingPid, callingUid);
127
128 if (data.ReadInterfaceToken() != GetDescriptor()) {
129 LBSLOGE(NETWORK, "invalid token.");
130 reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
131 return ERRCODE_SERVICE_UNAVAILABLE;
132 }
133 int ret = ERRCODE_SUCCESS;
134 isMessageRequest_ = false;
135 auto handleFunc = NetworkMsgHandleMap_.find(code);
136 if (handleFunc != NetworkMsgHandleMap_.end() && handleFunc->second != nullptr) {
137 auto memberFunc = handleFunc->second;
138 ret = (this->*memberFunc)(data, reply, identity);
139 } else {
140 LBSLOGE(NETWORK, "OnReceived cmd = %{public}u, unsupport service.", code);
141 reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
142 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
143 }
144 if (!isMessageRequest_) {
145 UnloadNetworkSystemAbility();
146 }
147 return ret;
148 }
149
CheckLocationSwitchState(MessageParcel & reply)150 bool NetworkAbilityStub::CheckLocationSwitchState(MessageParcel &reply)
151 {
152 if (CommonUtils::QuerySwitchState() == DISABLED) {
153 LBSLOGE(NETWORK, "%{public}s: %{public}d switch state is off.", __func__, __LINE__);
154 reply.WriteInt32(ERRCODE_SWITCH_OFF);
155 return false;
156 }
157 return true;
158 }
159 } // namespace Location
160 } // namespace OHOS
161 #endif // FEATURE_NETWORK_SUPPORT
162