1 /*
2 * Copyright (C) 2021 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 "net_manager_call_back.h"
17
18 #include "cellular_data_constant.h"
19 #include "cellular_data_error.h"
20 #include "cellular_data_service.h"
21 #include "cellular_data_types.h"
22
23 namespace OHOS {
24 namespace Telephony {
RequestNetwork(const std::string & ident,const std::set<NetCap> & netCaps,const NetManagerStandard::NetRequest & netrequest)25 int32_t NetManagerCallBack::RequestNetwork(const std::string &ident,
26 const std::set<NetCap> &netCaps,
27 const NetManagerStandard::NetRequest &netrequest)
28 {
29 if (netCaps.empty()) {
30 TELEPHONY_LOGI("netCaps is empty[%{public}s]", ident.c_str());
31 return CELLULAR_DATA_INVALID_PARAM;
32 }
33 NetRequest request;
34 for (const auto &netCap : netCaps) {
35 request.capability |= 1L << netCap;
36 }
37 request.ident = ident;
38 request.registerType = netrequest.registerType;
39 request.uid = netrequest.uid;
40 for (const auto &netBearType : netrequest.bearTypes) {
41 request.bearTypes |= 1L << netBearType;
42 }
43 int32_t result = DelayedRefSingleton<CellularDataService>::GetInstance().RequestNet(request);
44 return result;
45 }
46
ReleaseNetwork(const NetManagerStandard::NetRequest & netrequest)47 int32_t NetManagerCallBack::ReleaseNetwork(const NetManagerStandard::NetRequest &netrequest)
48 {
49 if (netrequest.netCaps.empty()) {
50 TELEPHONY_LOGE("netCaps is empty[%{public}s]", netrequest.ident.c_str());
51 return CELLULAR_DATA_INVALID_PARAM;
52 }
53 NetRequest request;
54 for (const auto &netCap : netrequest.netCaps) {
55 request.capability |= 1L << netCap;
56 }
57 request.uid = netrequest.uid;
58 request.ident = netrequest.ident;
59 int32_t result = DelayedRefSingleton<CellularDataService>::GetInstance().RemoveUid(request);
60 if (result != static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS)) {
61 TELEPHONY_LOGE("RemoveUid Request Fail");
62 return result;
63 }
64 result = DelayedRefSingleton<CellularDataService>::GetInstance().ReleaseNet(request);
65 return result;
66 }
67
AddRequest(const NetManagerStandard::NetRequest & netrequest)68 int32_t NetManagerCallBack::AddRequest(const NetManagerStandard::NetRequest &netrequest)
69 {
70 NetRequest request;
71 request.ident = netrequest.ident;
72 for (const auto &netCap : netrequest.netCaps) {
73 request.capability |= 1L << netCap;
74 }
75 request.uid = netrequest.uid;
76 return DelayedRefSingleton<CellularDataService>::GetInstance().AddUid(request);
77 }
78
79 } // namespace Telephony
80 } // namespace OHOS