• 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 #include "get_iface_stats_context.h"
17 
18 #include "constant.h"
19 #include "napi_constant.h"
20 #include "napi_utils.h"
21 #include "netmanager_base_log.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 constexpr const char *IFACE = "iface";
27 constexpr const char *START_TIME = "startTime";
28 constexpr const char *END_TIME = "endTime";
29 } // namespace
30 
GetIfaceStatsContext(napi_env env,EventManager * manager)31 GetIfaceStatsContext::GetIfaceStatsContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
32 
ParseParams(napi_value * params,size_t paramsCount)33 void GetIfaceStatsContext::ParseParams(napi_value *params, size_t paramsCount)
34 {
35     if (!CheckParamsType(params, paramsCount)) {
36         return;
37     }
38 
39     bool hasIface = NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_0], IFACE);
40     bool hasStart = NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_0], START_TIME);
41     bool hasEnd = NapiUtils::HasNamedProperty(GetEnv(), params[ARG_INDEX_0], END_TIME);
42     if (!(hasIface && hasStart && hasEnd)) {
43         NETMANAGER_BASE_LOGE("param error hasIface is %{public}d, hasStart is %{public}d, hasEnd is %{public}d",
44                              hasIface, hasStart, hasEnd);
45         SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
46         SetNeedThrowException(true);
47         return;
48     }
49 
50     bool checkIfaceType = NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0],
51                                                                                         IFACE)) == napi_string;
52     bool checkStartType = NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0],
53                                                                                         START_TIME)) == napi_number;
54     bool checkEndType = NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), params[ARG_INDEX_0],
55                                                                                       END_TIME)) == napi_number;
56     if (!(checkIfaceType && checkStartType && checkEndType)) {
57         NETMANAGER_BASE_LOGE("param napi_type error");
58         SetErrorCode(NETMANAGER_ERR_PARAMETER_ERROR);
59         SetNeedThrowException(true);
60         return;
61     }
62 
63     interfaceName_ = NapiUtils::GetStringPropertyUtf8(GetEnv(), params[ARG_INDEX_0], IFACE);
64     start_ = NapiUtils::GetUint32Property(GetEnv(), params[ARG_INDEX_0], START_TIME);
65     end_ = NapiUtils::GetUint32Property(GetEnv(), params[ARG_INDEX_0], END_TIME);
66 
67     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
68         SetParseOK(SetCallback(params[ARG_INDEX_1]) == napi_ok);
69         return;
70     }
71     SetParseOK(true);
72 }
73 
CheckParamsType(napi_value * params,size_t paramsCount)74 bool GetIfaceStatsContext::CheckParamsType(napi_value *params, size_t paramsCount)
75 {
76     if (paramsCount == PARAM_JUST_OPTIONS) {
77         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
78     }
79     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
80         return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_1]) == napi_function &&
81                NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_object;
82     }
83     return false;
84 }
85 
SetInterfaceName(std::string interfaceName)86 void GetIfaceStatsContext::SetInterfaceName(std::string interfaceName)
87 {
88     interfaceName_ = interfaceName;
89 }
90 
SetStatsInfo(NetStatsInfo statsInfo)91 void GetIfaceStatsContext::SetStatsInfo(NetStatsInfo statsInfo)
92 {
93     statsInfo_ = statsInfo;
94 }
95 
SetStart(uint32_t start)96 void GetIfaceStatsContext::SetStart(uint32_t start)
97 {
98     start_ = start;
99 }
100 
SetEnd(uint32_t end)101 void GetIfaceStatsContext::SetEnd(uint32_t end)
102 {
103     end_ = end;
104 }
105 
GetInterfaceName() const106 std::string GetIfaceStatsContext::GetInterfaceName() const
107 {
108     return interfaceName_;
109 }
110 
GetStatsInfo()111 NetStatsInfo &GetIfaceStatsContext::GetStatsInfo()
112 {
113     return statsInfo_;
114 }
115 
GetStart() const116 uint32_t GetIfaceStatsContext::GetStart() const
117 {
118     return start_;
119 }
120 
GetEnd() const121 uint32_t GetIfaceStatsContext::GetEnd() const
122 {
123     return end_;
124 }
125 } // namespace NetManagerStandard
126 } // namespace OHOS
127