1 /* 2 * Copyright (c) 2025 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_websocket_ffi.h" 17 18 #include <vector> 19 20 #include "netstack_log.h" 21 #include "net_websocket_impl.h" 22 #include "cj_lambda.h" 23 24 using namespace OHOS::FFI; 25 namespace OHOS::NetStack::NetWebSocket { 26 EXTERN_C_START 27 FfiOHOSWebSocketCreateWebSocket()28 int64_t FfiOHOSWebSocketCreateWebSocket() 29 { 30 auto instance = FFI::FFIData::Create<CJWebsocketProxy>(); 31 if (!instance) { 32 NETSTACK_LOGE("CJWebSocket Create CJWebsocketProxy failed."); 33 return ERR_INVALID_INSTANCE_CODE; 34 } 35 return instance->GetID(); 36 } 37 FfiOHOSWebSocketConnect(int64_t id,char * url,CWebSocketRequestOptions * opt)38 RetDataBool FfiOHOSWebSocketConnect(int64_t id, char* url, CWebSocketRequestOptions* opt) 39 { 40 RetDataBool ret = { .code = 0, .data = false }; 41 auto instance = FFIData::GetData<CJWebsocketProxy>(id); 42 if (instance == nullptr) { 43 // destroyed 44 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketConnect failed. instance is null."); 45 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 46 return ret; 47 } 48 49 auto context = CJWebsocketImpl::Connect(std::string(url), opt, instance); 50 if (context == nullptr) { 51 // websocket initialize failed 52 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketConnect failed. context is null."); 53 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 54 return ret; 55 } 56 if (context->IsPermissionDenied() || !context->IsParseOK()) { 57 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketConnect failed. Permission denied."); 58 ret.code = context->GetErrorCode(); 59 delete context; 60 return ret; 61 } 62 ret.data = true; 63 delete context; 64 return ret; 65 } 66 FfiOHOSWebSocketSend(int64_t id,CArrUI8 data,bool stringType)67 RetDataBool FfiOHOSWebSocketSend(int64_t id, CArrUI8 data, bool stringType) 68 { 69 RetDataBool ret = { .code = 0, .data = false }; 70 auto instance = FFIData::GetData<CJWebsocketProxy>(id); 71 if (instance == nullptr) { 72 // destroyed 73 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketSend failed. instance is null."); 74 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 75 return ret; 76 } 77 if (data.size == 0) { 78 ret.code = WEBSOCKET_PARSE_ERROR_CODE; 79 return ret; 80 } 81 auto context = CJWebsocketImpl::Send(data, instance, stringType); 82 if (context == nullptr) { 83 // websocket initialize failed 84 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketSend failed. context is null."); 85 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 86 return ret; 87 } 88 if (context->IsPermissionDenied() || !context->IsParseOK()) { 89 ret.code = context->GetErrorCode(); 90 delete context; 91 return ret; 92 } 93 ret.data = true; 94 delete context; 95 return ret; 96 } 97 FfiOHOSWebSocketClose(int64_t id,CWebSocketCloseOptions * opt)98 RetDataBool FfiOHOSWebSocketClose(int64_t id, CWebSocketCloseOptions* opt) 99 { 100 RetDataBool ret = { .code = 0, .data = false }; 101 auto instance = FFIData::GetData<CJWebsocketProxy>(id); 102 if (instance == nullptr) { 103 // destroyed 104 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 105 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketClose failed. instance is null."); 106 return ret; 107 } 108 auto context = CJWebsocketImpl::Close(opt, instance); 109 if (context == nullptr) { 110 // websocket initialize failed 111 ret.code = WEBSOCKET_UNKNOWN_OTHER_ERROR; 112 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketClose failed. context is null."); 113 return ret; 114 } 115 if (context->IsPermissionDenied() || !context->IsParseOK()) { 116 ret.code = context->GetErrorCode(); 117 delete context; 118 return ret; 119 } 120 ret.data = true; 121 delete context; 122 return ret; 123 } 124 FfiOHOSWebSocketOnController(int64_t id,int32_t typeId,void (* callback)(CWebSocketCallbackData * data))125 int32_t FfiOHOSWebSocketOnController(int64_t id, int32_t typeId, void (*callback)(CWebSocketCallbackData *data)) 126 { 127 int32_t ret = WEBSOCKET_UNKNOWN_OTHER_ERROR; 128 auto instance = FFIData::GetData<CJWebsocketProxy>(id); 129 if (instance == nullptr) { 130 // destroyed 131 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketOnController failed. instance is null."); 132 return ret; 133 } 134 if (CJWebsocketImpl::OnWithProxy(typeId, callback, instance)) { 135 ret = 0; 136 } 137 return ret; 138 } 139 FfiOHOSWebSocketOffController(int64_t id,int32_t typeId)140 int32_t FfiOHOSWebSocketOffController(int64_t id, int32_t typeId) 141 { 142 int32_t ret = WEBSOCKET_UNKNOWN_OTHER_ERROR; 143 auto instance = FFIData::GetData<CJWebsocketProxy>(id); 144 if (instance == nullptr) { 145 // destroyed 146 NETSTACK_LOGE("CJWebSocket FfiOHOSWebSocketOffController failed. instance is null."); 147 return ret; 148 } 149 if (CJWebsocketImpl::OffWithProxy(typeId, instance)) { 150 ret = 0; 151 } 152 return ret; 153 } 154 EXTERN_C_END 155 }