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