1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 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 #ifndef WM_SOCKET2_0_3_H 16 #define WM_SOCKET2_0_3_H 17 18 #include "wm_type_def.h" 19 #include "wm_netif.h" 20 21 /* socket state defination */ 22 #define NETCONN_STATE_NONE 0 23 #define NETCONN_STATE_WAITING 1 24 #define NETCONN_STATE_CONNECTED 2 25 #define NETCONN_STATE_CLOSED 3 26 27 /* socket event defination */ 28 #define NET_EVENT_TCP_JOINED 0 29 #define NET_EVENT_TCP_DISCONNECT 1 30 #define NET_EVENT_TCP_CONNECTED 2 31 #define NET_EVENT_TCP_CONNECT_FAILED 3 32 #define NET_EVENT_UDP_START 4 33 #define NET_EVENT_UDP_START_FAILED 5 34 35 #define TLS_MAX_SOCKET_NUM 4 36 #define TLS_MAX_NETCONN_NUM 20 37 38 /** Main packet buffer struct */ 39 struct pbuf { 40 /** next pbuf in singly linked pbuf chain */ 41 struct pbuf *next; 42 43 /** pointer to the actual data in the buffer */ 44 void *payload; 45 46 /** 47 * total length of this buffer and all next buffers in chain 48 * belonging to the same packet. 49 * 50 * For non-queue packet chains this is the invariant: 51 * p->tot_len == p->len + (p->next? p->next->tot_len: 0) 52 */ 53 u16_t tot_len; 54 55 /** length of this buffer */ 56 u16_t len; 57 58 /** pbuf_type as u8_t instead of enum to save space */ 59 u8_t type; 60 61 /** misc flags */ 62 u8_t flags; 63 64 /** 65 * the reference count always equals the number of pointers 66 * that refer to this pbuf. This can be pointers from an application, 67 * the stack itself, or pbuf->next pointers from a chain. 68 */ 69 u16_t ref; 70 }; 71 72 /** 73 * @brief This Function prototype for tcp error callback functions. Called when 74 * receives a RST or is unexpectedly closed for any other reason. 75 * The corresponding socket is already freed when this callback is called! 76 * 77 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 78 * 79 * @param[in] err Error code to indicate why the socket has been closed 80 * ERR_ABRT: aborted through returning ERR_ABRT from within others 81 * callback functions 82 * ERR_RST: the connection was reset by the remote host 83 */ 84 typedef void (*socket_err_fn)(u8 skt_num, err_t err); 85 86 /** 87 * @brief This Function prototype for socket receive callback functions. Called when data has 88 * been received. 89 * 90 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 91 * 92 * @param[in] p The received data (or NULL when the connection has been closed!) 93 * 94 * @param[in] err An error code if there has been an error receiving, always be ERR_OK 95 * when cs mode is udp. 96 * 97 * @retval The return value is only valid for tcp receive, for upd it means nothing. 98 * ERR_OK: Return this value after handling the received data. 99 * ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the 100 * callback function! 101 */ 102 typedef err_t (*socket_recv_fn)(u8 skt_num, struct pbuf *p, err_t err); 103 104 /** 105 * @brief This Function prototype for socket srce ip callback functions. Called when data has 106 * been received. 107 * 108 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 109 * 110 * @param[in] datalen The received data length 111 * 112 * @param[in] ipsrc source ip addr 113 * 114 * @param[in] port source port 115 * 116 * @param[in] err An error code if there has been an error receiving, always be ERR_OK 117 * when cs mode is udp. 118 * 119 * @retval The return value is only valid for UDP receive, for udp it means nothing. 120 * ERR_OK: Return this value after handling the received data. 121 * ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the 122 * callback function! 123 */ 124 typedef err_t (*socket_recv_ip_rpt_fn)(u8 skt_num, u16 datalen, u8 *ipsrc, u16 port, err_t err); 125 126 /** 127 * @brief This Function prototype for tcp connected callback functions. Called when 128 * connected to the remote side. 129 * 130 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 131 * 132 * @param[in] err An unused error code, always ERR_OK currently. 133 * 134 * @retval ERR_OK: Return this value after handling your logic. 135 * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the 136 * callback function! 137 */ 138 typedef err_t (*socket_connected_fn)(u8 skt_num, err_t err); 139 140 /** 141 * @brief This Function prototype for tcp poll callback functions. Called periodically. 142 * 143 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 144 * 145 * @retval ERR_OK: Try to do something periodically. 146 * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the 147 * callback function! 148 */ 149 typedef err_t (*socket_poll_fn)(u8 skt_num); 150 151 /** 152 * @brief This Function prototype for tcp accept callback functions. Called when a new 153 * connection can be accepted on a listening tcp. 154 * 155 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 156 * 157 * @param[in] err An error code if there has been an error accepting. 158 * 159 * @retval ERR_OK: Return this value after handling your logic. 160 * @retval ERR_ABRT: Only return ERR_ABRT if you want to abort the socket from within the 161 * callback function! 162 */ 163 typedef err_t (*socket_accept_fn)(u8 skt_num, err_t err); 164 165 /** 166 * @brief This Function prototype for socket state changed callback functions. 167 * Called when socket the sockte's state changed. 168 * 169 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 170 * 171 * @param[in] event Is the event number, see socket event defination. 172 * 173 * @param[in] state Is the socket state, see socket state defination. 174 */ 175 typedef void(*socket_state_changed_fn)(u8 skt_num, u8 event, u8 state); 176 177 /** Definitions for error constants. */ 178 typedef enum { 179 /** No error, everything OK. */ 180 ERR_OK = 0, 181 /** Out of memory error. */ 182 ERR_MEM = -1, 183 /** Buffer error. */ 184 ERR_BUF = -2, 185 /** Timeout. */ 186 ERR_TIMEOUT = -3, 187 /** Routing problem. */ 188 ERR_RTE = -4, 189 /** Operation in progress */ 190 ERR_INPROGRESS = -5, 191 /** Illegal value. */ 192 ERR_VAL = -6, 193 /** Operation would block. */ 194 ERR_WOULDBLOCK = -7, 195 /** Address in use. */ 196 ERR_USE = -8, 197 /** Already connecting. */ 198 ERR_ALREADY = -9, 199 /** Conn already established. */ 200 ERR_ISCONN = -10, 201 /** Not connected. */ 202 ERR_CONN = -11, 203 /** Low-level netif error */ 204 ERR_IF = -12, 205 /** Connection aborted. */ 206 ERR_ABRT = -13, 207 /** Connection reset. */ 208 ERR_RST = -14, 209 /** Connection closed. */ 210 ERR_CLSD = -15, 211 /** Illegal argument. */ 212 ERR_ARG = -16 213 }err_enum_t; 214 215 enum tls_socket_protocol { 216 SOCKET_PROTO_TCP, /* TCP Protocol */ 217 SOCKET_PROTO_UDP, /* UDP Protocol */ 218 }; 219 220 enum tls_socket_cs_mode { 221 SOCKET_CS_MODE_CLIENT, /* Client mode */ 222 SOCKET_CS_MODE_SERVER, /* Server mode */ 223 }; 224 225 struct tls_socket_desc { 226 enum tls_socket_cs_mode cs_mode; /* Server mode or Client mode, Only for tcp protocol is valid */ 227 enum tls_socket_protocol protocol; /* TCP Protocol or UDP Protocol */ 228 ip_addr_t ip_addr; /* Remote ip address, for tcp client mode is remote server's ip address; 229 for tcp server mode can be any address. */ 230 /* for udp is remote server's ip address */ 231 u16 port; /* port, for tcp client mode is remote server's port; 232 for tcp server mode is local listen port. for udp is remote server's port */ 233 u16 localport; /* local port, for udp and tcp client is local listen port, 234 for tcp server means nothing, tcp server always listen at port */ 235 char host_name[32]; /* remote host name, not support for now */ 236 u8 host_len; /* the length of host name */ 237 u32 timeout; /* poll timeout, not implemented for now */ 238 socket_err_fn errf; /* a pointer to socket_err_fn */ 239 socket_recv_fn recvf; /* a pointer to socket_recv_fn */ 240 socket_connected_fn connf; /* a pointer to socket_connected_fn */ 241 socket_poll_fn pollf; /* a pointer to socket_poll_fn */ 242 socket_accept_fn acceptf; /* a pointer to socket_accept_fn */ 243 socket_state_changed_fn state_changed; /* a pointer to socket_state_changed_fn */ 244 socket_recv_ip_rpt_fn recvwithipf; /* recv skt info report */ 245 }; 246 247 /** 248 * @brief This function is called by your application code to create a socket. 249 * 250 * @param[in] skd Is a pointer to an tls_socket_desc. 251 * 252 * @retval ERR_OK If create socket successfully. 253 * negative number If an error was detected. 254 */ 255 int tls_socket_create(struct tls_socket_desc *skd); 256 257 /** 258 * @brief This function is called by your application code to close the socket, 259 * and the related resources would be released. 260 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 261 * 262 * @retval ERR_OK If close socket successfully. 263 * negative number If an error was detected. 264 */ 265 int tls_socket_close(u8 skt_num); 266 267 struct tls_skt_status_ext_t { 268 u8 socket; 269 u8 status; 270 enum tls_socket_protocol protocol; 271 u8 host_ipaddr[4]; 272 u16 remote_port; 273 u16 local_port; 274 }; 275 276 struct tls_skt_status_t { 277 u32 socket_cnt; 278 struct tls_skt_status_ext_t skts_ext[1]; 279 }; 280 /** 281 * @brief This function is called by your application code to get the socket status of specified socket num. 282 * 283 * @param[in] skt_num Is the socket number that returned by tls_socket_create function. 284 * 285 * @param[in] buf Is a pointer to the data contains the socket status, if the socket is server, 286 * also contains it's client's status. 287 * @param[in] len The buf's length. At least, the len should be bigger than sizeof(struct tls_skt_status_t). 288 * 289 * @retval ERR_OK If send data successfully. 290 * negative number If an error was detected. 291 */ 292 int tls_socket_get_status(u8 skt_num, u8 *buf, u32 bufsize); 293 294 /** 295 * @ingroup pbuf 296 * Enumeration of pbuf layers 297 */ 298 typedef enum { 299 /** Includes spare room for transport layer header, e.g. UDP header. 300 * Use this if you intend to pass the pbuf to functions like udp_send(). 301 */ 302 PBUF_TRANSPORT, 303 /** Includes spare room for IP header. 304 * Use this if you intend to pass the pbuf to functions like raw_send(). 305 */ 306 PBUF_IP, 307 /** Includes spare room for link layer header (ethernet header). 308 * Use this if you intend to pass the pbuf to functions like ethernet_output(). 309 * @see PBUF_LINK_HLEN 310 */ 311 PBUF_LINK, 312 /** Includes spare room for additional encapsulation header before ethernet 313 * headers (e.g. 802.11). 314 * Use this if you intend to pass the pbuf to functions like netif->linkoutput(). 315 * @see PBUF_LINK_ENCAPSULATION_HLEN 316 */ 317 PBUF_RAW_TX, 318 /** Use this for input packets in a netif driver when calling netif->input() 319 * in the most common case - ethernet-layer netif driver. */ 320 PBUF_RAW 321 }pbuf_layer; 322 323 /** 324 * @ingroup pbuf 325 * Enumeration of pbuf types 326 */ 327 typedef enum { 328 /** pbuf data is stored in RAM, used for TX mostly, struct pbuf and its payload 329 are allocated in one piece of contiguous memory (so the first payload byte 330 can be calculated from struct pbuf). 331 pbuf_alloc() allocates PBUF_RAM pbufs as unchained pbufs (although that might 332 change in future versions). 333 This should be used for all OUTGOING packets (TX). */ 334 PBUF_RAM, 335 /** pbuf data is stored in ROM, i.e. struct pbuf and its payload are located in 336 totally different memory areas. Since it points to ROM, payload does not 337 have to be copied when queued for transmission. */ 338 PBUF_ROM, 339 /** pbuf comes from the pbuf pool. Much like PBUF_ROM but payload might change 340 so it has to be duplicated when queued before transmitting, depending on 341 who has a 'ref' to it. */ 342 PBUF_REF, 343 /** pbuf payload refers to RAM. This one comes from a pool and should be used 344 for RX. Payload can be chained (scatter-gather RX) but like PBUF_RAM, struct 345 pbuf and its payload are allocated in one piece of contiguous memory (so 346 the first payload byte can be calculated from struct pbuf). 347 Don't use this for TX, if the pool becomes empty e.g. because of TCP queuing, 348 you are unable to receive TCP acks! */ 349 PBUF_POOL 350 }pbuf_type; 351 352 /** 353 * @brief This Function allocates a pbuf of the given type (possibly a chain for PBUF_POOL type). 354 * 355 * The actual memory allocated for the pbuf is determined by the 356 * layer at which the pbuf is allocated and the requested size 357 * (from the size parameter). 358 * 359 * @param[in] l layer flag to define header size 360 * @param[in] length size of the pbuf's payload 361 * @param[in] type this parameter decides how and where the pbuf 362 * 363 * @retval The allocated pbuf. If multiple pbufs where allocated, this 364 * is the first pbuf of a pbuf chain. 365 */ 366 struct pbuf *pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type); 367 368 /** 369 * @brief This Function for release the buffer that you receive within the socket_recv_fn callback function. 370 * Attention please: If you return ERR_OK in the socket_recv_fn callback function, you must call this 371 * function to release the buffer by yourself. Otherwise, the buffer do not need be 372 * released by your code. 373 * 374 * @param[in] p The buffer you received in the socket_recv_fn callback function. 375 * 376 * @retval The number of de-allocated pbufs 377 */ 378 u8 pbuf_free(struct pbuf *p); 379 380 #endif 381 382