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