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