Commu
>w## Notice
> * please include the header file "mmi_commu.h" first when you want to use commu module
`#include "mmi_commu.h"`
### data structure
```
/**
* @brief Communication mode type definition[socket, http or https]
*/
typedef enum COMMU_MODE_ENUM
{
COMMU_SOCKET = 0, ///<socket
COMMU_HTTP, ///<http
COMMU_HTTPS, ///<https
COMMU_SSL_SOCKET, ///<ssl socket
} COMMU_MODE;
/**
* @brief socket type definition[TCP or UDP]
*/
typedef enum SOCKET_TYPE_ENUM
{
TCP_SOCKET = 0, ///<tcp
UDP_SOCKET, ///<udp
MAX_SOCKET
} SOCKET_TYPE;
/**
* @brief host addr type definition[IP or DOMAIN]
*/
typedef enum ADDR_TYPE_ENUM
{
IP_ADDR = 0, ///<ip
DOMAIN_ADDR ///<domain
} ADDR_TYPE;
/**
* @brief http method type definition[GET,POST,PUT,DELETE,HEAD and so on]
*/
typedef enum HTTP_METHOD_ENUM
{
HTTP_GET = 0, ///<get
HTTP_POST, ///<post
HTTP_PUT, ///<put
HTTP_DELETE, ///<delete
HTTP_HEAD ///<head
} HTTP_METHOD;
//----------------------------------------------
// struct definition
//----------------------------------------------
/**
* @brief sever parameter structure definition
*/
typedef struct SERVER_PARA_STRU
{
char host[128]; /**<server address: IP or DOMAIN*/
unsigned short port; /**<server port*/
} SERVER_PARA;
/**
* @brief socket parameter structure definition
*/
typedef struct SOCKET_PARA_STRU
{
SOCKET_TYPE socket_type; /**<socket type, UDP or TCP*/
ADDR_TYPE addr_type; /**<server address type, IP or DOMAIN*/
} SOCKET_PARA;
/**
* @brief https parameter structure definition
*/
typedef struct CACRT_PARA_STRU
{
char *p_cacrt; /**<ca crt*/
int cacrt_len; /**<ca crt length*/
} CACRT_PARA;
/**
* @brief callback function for display receiving countdown time
* @code
void display_timeout(const int rest_time) {
mmi_display(DISP_LINE3, DISP_Right, "%d", rest_time);
}
* @endcode
*/
typedef void (*commu_display)(const int rest_time);
/**
* @brief Communication parameter structure definition
*/
typedef struct COMMU_PARA_STRU
{
COMMU_MODE commu_mode; /**<communication mode*/
int is_auto_switch_net; /**<auto switch net GPRS or WIFI, 0-NO 1-YES*/
int commu_timeout; /**<the time of recv data timeout(>= 5)*/
SERVER_PARA server_para; /**<remote server para*/
SOCKET_PARA socket_para; /**<socket communication para*/
CACRT_PARA cacrt_para; /**<ca crt para*/
commu_display display_timeout; /**<display receiving countdown time,http/s unsupported*/
} COMMU_PARA;
/**
* @brief callback function for setting headers of\n
http or https parameters\n
implemented by caller.
* @param hmc [in] http handle for httpHeadPar(hmc, ...) of sdk api
* @retval =0 : success
* @retval !0 : fail
*/
typedef void (*commu_set_http_header)(const HTTP_MODE_CTX * hmc);
/**
* @brief send parameter structure definition
*/
typedef struct HTTP_SEND_PARA_STRU
{
HTTP_METHOD http_method; /**<http request type*/
char *http_url; /**<http url*/
int http_url_len; /**<http url length*/
int is_keep_alive; /**<http keep alive flag(1-yes,0-no)*/
commu_set_http_header set_http_header; /**<set http header call back function pointer*/
} HTTP_SEND_PARA;
//----------------------------------------------
// retval definition
//----------------------------------------------
/**
* @brief communication retval code definition
*/
#define COMMU_RET_OK (0) ///<success
/**
* @brief Communication fail error code definition
*/
#define COMMU_ERR_COMMU_MIN (-100)
#define COMMU_ERR_NULL_ARG (COMMU_ERR_COMMU_MIN - 1) ///<null parameter
#define COMMU_ERR_COMMU_UNSET (COMMU_ERR_COMMU_MIN - 2) ///<communication parameters unseted
#define COMMU_ERR_UNCONNECTED (COMMU_ERR_COMMU_MIN - 3) ///<unconnected
#define COMMU_ERR_MODE (COMMU_ERR_COMMU_MIN - 4) ///<communication type error
#define COMMU_ERR_NET_UNAVAILABLE (COMMU_ERR_COMMU_MIN - 5) ///<Network unavailable
#define COMMU_ERR_CREATE_THREAD (COMMU_ERR_COMMU_MIN - 6) ///<create thread error
#define COMMU_ERR_SOCKET_BREAK (COMMU_ERR_COMMU_MIN - 7) ///<Socket disconnected
#define COMMU_ERR_HTTP_URL (COMMU_ERR_COMMU_MIN - 8) ///<url is null
#define COMMU_ERR_NEED_DISCONNECT (COMMU_ERR_COMMU_MIN - 9) ///<need to call disconnect first
/**
* @brief error code of set communication parameters failed
*/
#define COMMU_SET_ERR_MIN (-200)
#define COMMU_SET_ERR_SOCKET_TYPE (COMMU_SET_ERR_MIN - 2) ///<Socket type error
#define COMMU_SET_ERR_ADDR_TYPE (COMMU_SET_ERR_MIN - 3) ///<host addr error
#define COMMU_SET_ERR_CACRT (COMMU_SET_ERR_MIN - 4) ///<cacrt error
#define COMMU_SET_ERR_HTTP_METHOD (COMMU_SET_ERR_MIN - 5) ///<HTTP METHOD error
#define COMMU_SET_ERR_SERVER_ARG (COMMU_SET_ERR_MIN - 6) ///<host ip or port error
#define COMMU_SET_ERR_SWITCH_FLAG (COMMU_SET_ERR_MIN - 7) ///<error of switch network parameter
#define COMMU_SET_ERR_TIMEOUT (COMMU_SET_ERR_MIN - 8) ///<error of receive timeout parameter
#define COMMU_SET_ERR_MALLOC (COMMU_SET_ERR_MIN - 9) ///<malloc failed
/**
* @brief error code of connection failed
*/
#define COMMU_CONNECT_ERR_MIN (-300)
#define COMMU_CONNECT_ERR_HTTPSHAKE (COMMU_CONNECT_ERR_MIN - 1) ///<httpsInit failed
#define COMMU_CONNECT_ERR_CREATE_SOCKET (COMMU_CONNECT_ERR_MIN - 2) ///<create socket failed
#define COMMU_CONNECT_ERR_CONNECT (COMMU_CONNECT_ERR_MIN - 3) ///<connect host failed
#define COMMU_CONNECT_ERR_HTTP_HEADER (COMMU_CONNECT_ERR_MIN - 4) ///<set HTTP Header failed
#define COMMU_CONNECT_ERR_HTTP_INIT (COMMU_CONNECT_ERR_MIN - 5) ///<http init failed
#define COMMU_CONNECT_ERR_ESTABLISH_SSL (COMMU_CONNECT_ERR_MIN - 6) ///<ssl establish failed
/**
* @brief error code of sending failed
*/
#define COMMU_SEND_ERR_MIN (-400)
#define COMMU_SEND_ERR (COMMU_SEND_ERR_MIN - 1) ///<send failed
/**
* @brief error code of receiving failed
*/
#define COMMU_RECV_ERR_MIN (-500)
#define COMMU_RECV_ERR (COMMU_RECV_ERR_MIN - 1) ///<receive failed
#define COMMU_RECV_TIMEOUT (COMMU_RECV_ERR_MIN - 2) ///<receive timeout
#define COMMU_RECV_ERR_SSL_CONNECT (COMMU_RECV_ERR_MIN - 3) ///<ssl connect error
#define COMMU_RECV_ERR_REMOTE_CLOSED (COMMU_RECV_ERR_MIN - 4) ///<remote server close connection
```
>i## commu_set_para
### Prototype
`int commu_set_para(COMMU_PARA *pCommuPara);`
### Function
* set communication parameter
### Parameter
|Name|Type|description|
|-|-|-|
|pCommuPara|COMMU_PARA *|communiction parameter structure|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>i## commu_preconnect
### Prototype
`int commu_preconnect(void);`
### Function
* preconnect server
called after function commu_set_para return immediately after the call and run in the background
you can get the error code by function
`int commu_get_preconnect_errcode(void);`
### Parameter
|Name|Type|description|
|-|-|-|
|none|||
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>i## commu_connect
### Prototype
`int commu_connect(void);`
### Function
* connect server
called after function `commu_set_para`
blocking call until connect success or error
**!!!can not be called after commu_preconnect!!!**
### Parameter
|Name|Type|description|
|-|-|-|
|none|||
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>i## commu_send
### Prototype
`int commu_send(const HTTP_SEND_PARA *p_http_send_para, const char *send_buff,
const int send_len);`
### Function
* send data
called after commu_connect or commu_preconnect
### Parameter
|Name|Type|description|
|-|-|-|
|p_http_send_para|const HTTP_SEND_PARA *|send parameter struct|
|send_buff|const char *|send data buffer|
|send_len|const int|send data length|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>i## commu_recv
### Prototype
`int commu_recv(char *recv_buff, int *p_recv_len, const int max_recv_len);`
### Function
* receive data
### Parameter
|Name|Type|description|
|-|-|-|
|recv_buff|char *|receive data buffer|
|p_recv_len|int *|receive data length|
|max_recv_len|const int|the maximum data length you want to receive|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>i## commu_get_header
### Prototype
`const char *commu_get_header(const char *header, int size);`
### Function
* get http or https header data
### Parameter
|Name|Type|description|
|-|-|-|
|header|const char *|header key|
|size|int|header key length|
### Retval
|Value|Type|Description|
|-|-|-|
|!NULL|const char *|header data pointer|
|=NULL|const char *|fail|
>i## commu_disconnect
### Prototype
`int commu_disconnect(void);`
### Function
* disconnect from server
### Parameter
|Name|Type|description|
|-|-|-|
|none|||
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|SUCCESS|
|!0|int|fail,see the error code macro|
>s## Example
```
/**
* @brief display recieving countdown time
*/
void transOnline_DisplayTimeout(const int rest_time)
{
mmi_display(DISP_Line3, DISP_Right, "%d", rest_time);
return;
}
///< set the communication parameter
COMMU_PARA commuPara;
int iRet = 0;
memset(&commuPara, 0, sizeof(COMMU_PARA));
commuPara.commu_mode = COMMU_SOCKET;
commuPara.is_auto_switch_net = 0;
commuPara.commu_timeout = 60;
strcpy(commuPara.server_para.host, "192.168.1.1");
commuPara.server_para.port = 8000;
commuPara.socket_para.socket_type = TCP_SOCKET;
commuPara.socket_para.addr_type = IP_ADDR;
commuPara.display_timeout = transOnline_DisplayTimeout;
iRet = commu_set_para(&commuPara);
///< preconnect server before online trans
int iRet = 0;
iRet = commu_preconnect();
///< connect server
int iRet = 0;
iRet = commu_connect();
///< send data to server by socket
int iRet = 0;
char sendBuff[] = "smartpeak";
iRet = commu_send(NULL, sendBuff, strlen(sendBuff));
///< receive data from server
int iRet = 0;
char recvBuff[1024] = {0};
int recvLen = 0;
iRet = commu_recv(recvBuff, &recvLen, 256);
///< get http header data of key "Date"
char *pHeaderData = NULL;
pHeaderData = commu_get_header("Date", sizeof("Date"));
if (NULL != pHeaderData)
{
LOGD("http header data = %s\r\n", pHeaderData);
}
///< disconnect from server
int iRet = 0;
iRet = commu_disconnect();
```