• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "multicast_get_ttl_context.h"
17 
18 #include "context_key.h"
19 #include "event_manager.h"
20 #include "napi_utils.h"
21 #include "netstack_log.h"
22 #include "socket_constant.h"
23 
24 namespace OHOS::NetStack::Socket {
MulticastGetTTLContext(napi_env env,const std::shared_ptr<EventManager> & manager)25 MulticastGetTTLContext::MulticastGetTTLContext(napi_env env, const std::shared_ptr<EventManager> &manager)
26     : BaseContext(env, manager) {}
27 
ParseParams(napi_value * params,size_t paramsCount)28 void MulticastGetTTLContext::ParseParams(napi_value *params, size_t paramsCount)
29 {
30     if (!CheckParamsType(params, paramsCount)) {
31         return;
32     }
33 
34     if (paramsCount != PARAM_NONE) {
35         SetParseOK(SetCallback(params[0]) == napi_ok);
36         return;
37     }
38     SetParseOK(true);
39 }
40 
CheckParamsType(napi_value * params,size_t paramsCount)41 bool MulticastGetTTLContext::CheckParamsType(napi_value *params, size_t paramsCount)
42 {
43     if (paramsCount == PARAM_NONE) {
44         return true;
45     }
46 
47     if (paramsCount == PARAM_JUST_CALLBACK) {
48         if (NapiUtils::GetValueType(GetEnv(), params[0]) == napi_function) {
49             return true;
50         } else {
51             NETSTACK_LOGE("first param is not callback");
52             SetNeedThrowException(true);
53             SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
54         }
55     }
56     return false;
57 }
58 
SetMulticastTTL(int ttl)59 void MulticastGetTTLContext::SetMulticastTTL(int ttl)
60 {
61     ttl_ = ttl;
62 }
63 
GetMulticastTTL() const64 int MulticastGetTTLContext::GetMulticastTTL() const
65 {
66     return ttl_;
67 }
68 
GetSocketFd() const69 int MulticastGetTTLContext::GetSocketFd() const
70 {
71     return sharedManager_->GetData() ? static_cast<int>(reinterpret_cast<uint64_t>(sharedManager_->GetData())) : -1;
72 }
73 
GetErrorCode() const74 int32_t MulticastGetTTLContext::GetErrorCode() const
75 {
76     auto err = BaseContext::GetErrorCode();
77     if (err == PARSE_ERROR_CODE) {
78         return PARSE_ERROR_CODE;
79     }
80 
81     if (BaseContext::IsPermissionDenied()) {
82         return PERMISSION_DENIED_CODE;
83     }
84 
85 #if defined(IOS_PLATFORM)
86     err = ErrCodePlatformAdapter::GetOHOSErrCode(err);
87 #endif
88     return err + SOCKET_ERROR_CODE_BASE;
89 }
90 
GetErrorMessage() const91 std::string MulticastGetTTLContext::GetErrorMessage() const
92 {
93     if (BaseContext::IsPermissionDenied()) {
94         return PERMISSION_DENIED_MSG;
95     }
96 
97     auto errCode = BaseContext::GetErrorCode();
98     if (errCode == PARSE_ERROR_CODE) {
99         return PARSE_ERROR_MSG;
100     }
101 #if defined(IOS_PLATFORM)
102     std::string errMessage;
103     ErrCodePlatformAdapter::GetOHOSErrMessage(errCode, errMessage);
104     return errMessage;
105 #else
106     char err[MAX_ERR_NUM] = {0};
107     (void)strerror_r(errCode, err, MAX_ERR_NUM);
108     return err;
109 #endif
110 }
111 } // namespace OHOS::NetStack::Socket
112