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 #ifndef AUTH_TCP_CONNECTION_H 17 #define AUTH_TCP_CONNECTION_H 18 19 #include "auth_connection.h" 20 21 #ifdef __cplusplus 22 #if __cplusplus 23 extern "C" { 24 #endif 25 #endif 26 27 #define AUTH_INVALID_FD (-1) 28 #define TCP_KEEPALIVE_INTERVAL 2 29 #define TCP_KEEPALIVE_HIGH_COUNT 3 30 #define TCP_KEEPALIVE_MID_COUNT 3 31 #define TCP_KEEPALIVE_LOW_COUNT 5 32 #define TCP_KEEPALIVE_DEFAULT_COUNT 5 33 #define TCP_KEEPALIVE_HIGH_USER_TIMEOUT (10 * 1000) 34 #define TCP_KEEPALIVE_MID_USER_TIMEOUT (10 * 1000) 35 #define TCP_KEEPALIVE_LOW_USER_TIMEOUT (15 * 1000) 36 #define TCP_KEEPALIVE_DEFAULT_USER_TIMEOUT (15 * 1000) 37 38 typedef struct { 39 void (*onConnected)(ListenerModule module, int32_t fd, bool isClient); 40 void (*onDisconnected)(int32_t fd); 41 void (*onDataReceived)(ListenerModule module, int32_t fd, const AuthDataHead *head, const uint8_t *data); 42 } SocketCallback; 43 int32_t SetSocketCallback(const SocketCallback *cb); 44 void UnsetSocketCallback(void); 45 46 // connect succ, return fd; otherwise, return -1. 47 int32_t SocketConnectDeviceWithAllIp(const char *localIp, const char *remoteIp, int32_t port, bool isBlockMode); 48 int32_t SocketConnectDevice(const char *ip, int32_t port, bool isBlockMode); 49 int32_t SocketSetDevice(int32_t fd, bool isBlockMode); 50 int32_t NipSocketConnectDevice(ListenerModule module, const char *addr, int32_t port, bool isBlockMode); 51 52 void SocketDisconnectDevice(ListenerModule module, int32_t fd); 53 54 int32_t SocketPostBytes(int32_t fd, const AuthDataHead *head, const uint8_t *data); 55 int32_t SocketGetConnInfo(int32_t fd, AuthConnInfo *connInfo, bool *isServer); 56 57 int32_t StartSocketListening(ListenerModule module, const LocalListenerInfo *info); 58 void StopSocketListening(ListenerModule moduleId); 59 60 int32_t AuthSetTcpKeepaliveOption(int32_t fd, ModeCycle cycle); 61 bool IsExistWifiConnItemByConnId(int32_t fd); 62 void DeleteWifiConnItemByConnId(int32_t fd); 63 int32_t WifiConnListLockInit(void); 64 void WifiConnListLockDeinit(void); 65 66 #ifdef __cplusplus 67 #if __cplusplus 68 } 69 #endif 70 #endif 71 #endif /* AUTH_TCP_CONNECTION_H */ 72