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 std::string & ident,const std::set<NetCap> & netCaps)47 int32_t NetManagerCallBack::ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps)
48 {
49 if (netCaps.empty()) {
50 TELEPHONY_LOGI("netCaps is empty[%{public}s]", ident.c_str());
51 return CELLULAR_DATA_INVALID_PARAM;
52 }
53 NetRequest request;
54 for (const auto &netCap : netCaps) {
55 request.capability |= 1L << netCap;
56 }
57 request.ident = ident;
58 int32_t result = DelayedRefSingleton<CellularDataService>::GetInstance().ReleaseNet(request);
59 return result;
60 }
61
AddRequest(const NetManagerStandard::NetRequest & netrequest)62 int32_t NetManagerCallBack::AddRequest(const NetManagerStandard::NetRequest &netrequest)
63 {
64 NetRequest request;
65 request.ident = netrequest.ident;
66 for (const auto &netCap : netrequest.netCaps) {
67 request.capability |= 1L << netCap;
68 }
69 request.uid = netrequest.uid;
70 return DelayedRefSingleton<CellularDataService>::GetInstance().AddUid(request);
71 }
72
RemoveRequest(const NetManagerStandard::NetRequest & netrequest)73 int32_t NetManagerCallBack::RemoveRequest(const NetManagerStandard::NetRequest &netrequest)
74 {
75 NetRequest request;
76 request.ident = netrequest.ident;
77 for (const auto &netCap : netrequest.netCaps) {
78 request.capability |= 1L << netCap;
79 }
80 request.uid = netrequest.uid;
81 return DelayedRefSingleton<CellularDataService>::GetInstance().RemoveUid(request);
82 }
83
84 } // namespace Telephony
85 } // namespace OHOS