Modify licenses
[scp/ocu/5gnr.git] / Include / vos_sockMgr.h
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 ICT/CAS.
4 *
5 *   Licensed under the O-RAN Software License, Version 1.0 (the "Software License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       https://www.o-ran.org/software
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19
20
21
22 #ifndef __VOS_SOCKMGR_H__
23 #define __VOS_SOCKMGR_H__
24
25 #include "vos_types.h"
26 #include "vos_timer.h"
27 #include "vos_que.h"
28 #include "vos_socket.h"
29 #include "vos_module.h"
30
31 #define COMM_SCTP_ADDR_MAX 5
32
33
34 #define VOS_SOCKET_MSG_MAX 9000
35
36
37 /** 采用socket mgr 需要提供的handle函数,用于接收数据
38  * @param[in ]   buf         用于消息接收的缓冲区
39  * @param[in ]   size        buf 大小
40  * @param[in ]   opval       额外信息,类型为 vos_mod_com_op_t
41  * @param[in ]   opvalLen    opval的大小
42  *
43 */
44 typedef LONG (*receive_handler)(VOID *buf,LONG size,VOID *opval,LONG opvalLen);
45
46
47 /** tcp/sctp one-to-one 用于接收客户端accept 事件的函数
48 * @param[in ]   info           客户端信息,实际为vos_sock_info_t
49 * @param[in ]   infoLen        信息长度
50 *
51 */
52 typedef LONG (*accept_handler)(VOID *info,LONG infoLen);
53
54
55 typedef struct sctp_para_s {
56     vos_sockaddr_t                  addrs[COMM_SCTP_ADDR_MAX];  ///< ip 地址
57     LONG                            addrsCnt;                   ///< ip 个数
58     struct sctp_sndrcvinfo          sinfo;                      ///< sctp_sndrcvinfo
59     struct sctp_initmsg             initmsg;                    ///< sctp_initmsg
60     struct sctp_event_subscribe     events;                     ///< sctp_event_subscribe
61 }sctp_para_t;
62
63 typedef union {
64         vos_sockaddr_t   addr;
65         sctp_para_t      sctp;
66 }___addr_u;
67
68 /** 创建socket需要提供的参数 */
69 typedef struct vos_sock_para_s
70 {
71     module_comm_type_t type;                ///< 通信方式
72     union{
73         vos_sockaddr_t addr;                ///< socket 通信
74         sctp_para_t    sctp;                ///< sctp   通信
75     }u;
76     ___addr_u          client_bind_addr;    ///< 创建客户端时如果也需要绑定地址,填写该变量
77     receive_handler  recv_handle;           ///< 接收函数
78     accept_handler   accept_handle;         ///< 接收accept 事件
79     LONG             maxClient;             ///< tcp server 允许的连接数
80     BOOL             soloMode;              ///< 如果为真,则单独创建接收线程;如果为假,则由 VOS socket代理任务负责接收
81     LONG             fdIdx;                 ///< 无需关心
82 }vos_sock_para_t;
83
84 typedef struct vos_sock_info_s{
85     module_comm_type_t type;     ///< 通信方式
86     LONG fd;                     ///< socket
87     LONG fdIdx;                  ///< 无需关心
88     LONG c_flag;                 ///< 无需关心
89     struct sctp_sndrcvinfo sri;  ///< sctp_recvmsg()/sctp_send() 中的 sinfo,sctp_sendmsg() 中ppid,flags,stream_no,timetolive,context也从该结构中获得
90     LONG msg_flags;              ///< sctp_recvmsg() 中的 msg_flags
91     vos_sockaddr_t addr;         ///< udp/sctp one-to-many 接收到的对端地址,同时也是udp/sctp one-to-many 发送的目的地址
92 }vos_sock_info_t;
93
94
95 /**
96  * 创建socket通信
97  * @param[in ]   name         模块名,大小为 MODULE_NAME_LEN
98  * @param[in ]   pPara        额外参数数组
99  * @param[in ]   size         数组大小
100  * @param[out]   info           socket 相关信息,用于发送消息和删除该socket
101  * @return       VOS_OK - 成功,其他 - 失败
102  */
103 LONG VOS_SOCKET_create(const CHAR *name,vos_sock_para_t *pPara,LONG size,vos_sock_info_t *info);
104
105
106 /**
107  * 删除 socket
108  * @param[in ]   info           创建时获得的socket 相关信息
109  * @return       VOS_OK - 成功,其他 - 失败
110  */
111 LONG VOS_SOCKET_delete(vos_sock_info_t *info);
112
113
114 /**
115  * 判断是否连接成功(只判断tcp 和sctp one-to-one 是否connect成功)
116  * @param[in ]   info           创建时获得的socket 相关信息
117  * @return       连接正常返回 VOS_TRUE,否则返回 VOS_FALSE
118  */
119 BOOL VOS_SOCKET_isLink(vos_sock_info_t *info);
120
121
122 /**
123  * socket 发送数据
124  * @param[in ]   info           socket 相关信息
125  * @param[in ]   msgData        数据
126  * @param[in ]   msgDataLen     数据长度
127  * @return       成功返回 发送的字节数 ,失败则返回 VOS_ERROR
128  */
129 LONG VOS_SOCKET_send(vos_sock_info_t *info,VOID *msgData,LONG msgDataLen);
130
131
132
133 #endif /* __VOS_SOCKMGR_H__ */