90 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			90 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
|  | /****************************************************************************
 | ||
|  | 
 | ||
|  | Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED. | ||
|  | 
 | ||
|  | This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT | ||
|  | be copied by any method or incorporated into another program without | ||
|  | the express written consent of Aerospace C.Power. This Information or any portion | ||
|  | thereof remains the property of Aerospace C.Power. The Information contained herein | ||
|  | is believed to be accurate and Aerospace C.Power assumes no responsibility or | ||
|  | liability for its use in any way and conveys no license or title under | ||
|  | any patent or copyright and makes no representation or warranty that this | ||
|  | Information is free from patent or copyright infringement. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | #ifndef IOT_SOCKET_API_H
 | ||
|  | #define IOT_SOCKET_API_H
 | ||
|  | 
 | ||
|  | #include "os_types_api.h"
 | ||
|  | #include "iot_pkt_api.h"
 | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | extern "C" { | ||
|  | #endif
 | ||
|  | 
 | ||
|  | /* reason that socket callback was called. */ | ||
|  | /* data received on udp or tcp socket. */ | ||
|  | #define IOT_SOCKET_CB_DATA_RECV     0
 | ||
|  | 
 | ||
|  | /* type of socket to be created. */ | ||
|  | /* udp socket. */ | ||
|  | #define IOT_SOCKET_TYPE_UDP         0
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief: callback method to receive data or event on the socket | ||
|  |  * @param cb_type: type of event that trigger the callback | ||
|  |  * @param socket: the socket on which event occur | ||
|  |  * @param pkt: iot_pkt contains data for the callback. | ||
|  |  *             this method shall free the pkt. | ||
|  |  */ | ||
|  | typedef void(*iot_socket_cb_func_t)(uint32_t cb_type, | ||
|  |     int32_t socket, iot_pkt_t *pkt); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief: create a socket. | ||
|  |  * @param socket_type: type of the socket, see IOT_SOCKET_TYPE_XXX. | ||
|  |  * @param is_ipv6: if it's a ipv6 address. | ||
|  |  * @param ip: local ip address in string. | ||
|  |  *            for ipv4, it's like "127.0.0.1", | ||
|  |  *            for ipv6, it's like "::1". | ||
|  |  *            NULL means IP_ADDR_ANY. When a socket bind with IP_ADDR_ANY, | ||
|  |  *            data with matching port number will be reported to the socket. | ||
|  |  * @param port: local port to bind with new created socket. | ||
|  |  *              if port is 0, system choose a port at random. | ||
|  |  * @param recv_cb: callback method to receive data or event on the socket. | ||
|  |  * @param pkt_headroom: required headroom for received data of | ||
|  |  *                      iot_socket_cb_func_t. | ||
|  |  * @param ret_sock: the new created socket if succeed. | ||
|  |  * @return: ERR_OK for succeed case. other ERR_XXX for failed case. | ||
|  |  */ | ||
|  | uint32_t iot_socket_create(uint8_t socket_type, uint8_t is_ipv6, | ||
|  |     const char *ip, uint16_t port, iot_socket_cb_func_t recv_cb, | ||
|  |     uint8_t pkt_headroom, int32_t *ret_sock); | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * @brief: delete a socket. | ||
|  |  * @param socket: the socket to be deleted. | ||
|  |  */ | ||
|  | void iot_socket_delete(int32_t socket); | ||
|  | 
 | ||
|  | /*
 | ||
|  |  * @brief: send data to specific peer through udp socket. | ||
|  |  * @param socket: socket to send data. | ||
|  |  * @param data: data to be sent. | ||
|  |  * @param len: length of data to be send. | ||
|  |  * @param is_ipv6: if it's a ipv6 address. | ||
|  |  * @param ip: peer device's ip address in string. | ||
|  |  *            for ipv4, it's like "127.0.0.1", | ||
|  |  *            for ipv6, it's like "::1". | ||
|  |  * @param port: port of the peer device. | ||
|  |  * @return: ERR_OK for successful case. ERR_XX for failed case. | ||
|  |  */ | ||
|  | uint32_t iot_socket_udp_send(int32_t socket, uint8_t *data, uint32_t len, | ||
|  |     uint32_t is_ipv6, const char *ip, uint16_t port); | ||
|  | 
 | ||
|  | #ifdef __cplusplus
 | ||
|  | } | ||
|  | #endif
 | ||
|  | 
 | ||
|  | #endif /* IOT_SOCKET_API_H */
 |