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