WE310F5  39.00.000
M2MB HTTPC

This section describes the M2MB APIs to HTTP Client operations. More...

#define M2MB_HTTPCLIENT_MAX_URL_LENGTH   1024
 
#define M2MB_HTTPCLIENT_MAX_HOST_LENGTH   64
 
enum  M2MB_HTTPC_METHODS {
  M2MB_HTTPC_METHOD_INVALID = 0,
  M2MB_HTTPC_METHOD_GET = 1,
  M2MB_HTTPC_METHOD_HEAD,
  M2MB_HTTPC_METHOD_POST,
  M2MB_HTTPC_METHOD_PUT,
  M2MB_HTTPC_METHOD_MAX
}
 supported http request method for use with m2mb_httpc_request. More...
 
enum  M2MB_HTTPC_STATES {
  M2MB_HTTPC_RX_ERROR_SERVER_CLOSED = -8,
  M2MB_HTTPC_RX_ERROR_RX_PROCESS = -7,
  M2MB_HTTPC_RX_ERROR_RX_HTTP_HEADER = -6,
  M2MB_HTTPC_RX_ERROR_INVALID_RESPONSECODE = -5,
  M2MB_HTTPC_RX_ERROR_CLIENT_TIMEOUT = -4,
  M2MB_HTTPC_RX_ERROR_NO_BUFFER = -3,
  M2MB_HTTPC_RX_CONNECTION_CLOSED = -2,
  M2MB_HTTPC_RX_ERROR_CONNECTION_CLOSED = -1,
  M2MB_HTTPC_RX_FINISHED = 0,
  M2MB_HTTPC_RX_MORE_DATA = 1
}
 HTTP client callback state. More...
 
enum  M2MB_HTTPC_CONF_ID {
  M2MB_HTTPC_CONF_INVALID = 0,
  M2MB_HTTPC_CONF_SET_HEADER,
  M2MB_HTTPC_CONF_SET_CGI_PARAMS,
  M2MB_HTTPC_CONF_SET_ENABLE_RESP_HEADER,
  M2MB_HTTPC_CONF_SET_CLEAR_ALL_HEADER,
  M2MB_HTTPC_CONF_SET_TIMEOUT,
  M2MB_HTTPC_CONF_GET_SERVER_IP,
  M2MB_HTTPC_CONF_GET_SOCK_ID
}
 HTTP client confg id for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs. More...
 
typedef HANDLE M2MB_HTTPC_CTXT_HANDLE
 HTTPC context handle.
This is obtained from m2mb_httpc_open API and then used in subsequent HTTPC APIs. More...
 
typedef enum M2MB_HTTPC_METHODS M2MB_HTTPC_METHODS_E
 supported http request method for use with m2mb_httpc_request. More...
 
typedef enum M2MB_HTTPC_STATES M2MB_HTTPC_STATES_E
 HTTP client callback state. More...
 
typedef enum M2MB_HTTPC_CONF_ID M2MB_HTTPC_CONF_ID_E
 HTTP client confg id for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs. More...
 
typedef struct M2MB_HTTPC_RESPONSE M2MB_HTTPC_RESPONSE_T
 HTTP client response for use in user callback. More...
 
typedef VOID(* M2MB_HTTPC_CB_T) (VOID *arg, M2MB_HTTPC_STATES_E state, M2MB_HTTPC_RESPONSE_T *value)
 User registered callback for returning response message. More...
 
typedef union M2MB_HTTPC_CONF_PARAMS M2MB_HTTPC_CONF_PARAMS_T
 HTTP client configuration params structure for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs. More...
 
typedef struct M2MB_HTTPC_OPEN_PARAMS M2MB_HTTPC_OPEN_PARAMS_T
 HTTPC parameters required to open a client connection. More...
 
M2MB_STATUS_T m2mb_httpc_open (HANDLE *hndl, M2MB_HTTPC_OPEN_PARAMS_T *params)
 create http client context More...
 
M2MB_STATUS_T m2mb_httpc_set_cfg (HANDLE handle, M2MB_HTTPC_CONF_ID_E conf_id, M2MB_HTTPC_CONF_PARAMS_T *conf_params)
 update the http client session configurations More...
 
M2MB_STATUS_T m2mb_httpc_get_cfg (HANDLE handle, M2MB_HTTPC_CONF_ID_E conf_id, M2MB_HTTPC_CONF_PARAMS_T *conf_params)
 get the http client session information More...
 
M2MB_STATUS_T m2mb_httpc_connect (HANDLE handle, UINT8 *url, UINT16 port)
 Connects to an HTTP server in Blocking mode. More...
 
M2MB_STATUS_T m2mb_httpc_set_request (VOID *handle, M2MB_HTTPC_METHODS_E method, UINT8 *url)
 Set the http request. More...
 
M2MB_STATUS_T m2mb_httpc_send (VOID *handle, UINT8 isFirstPacket, UINT8 *body, UINT32 body_length, UINT32 total_length)
 Send HTTP information from the http client session. More...
 
M2MB_STATUS_T m2mb_httpc_close (HANDLE handle)
 closes an HTTP client session. More...
 

Detailed Description

This section describes the M2MB APIs to HTTP Client operations.

WE310F5_HTTPC.png
HTTPC

HTTP Client APIs can be used to communicate with HTTP server. The diagram shows the sequence of API call to be performed. To connect to a HTTP server and exchange information, the following steps must be performed:

Macro Definition Documentation

◆ M2MB_HTTPCLIENT_MAX_HOST_LENGTH

#define M2MB_HTTPCLIENT_MAX_HOST_LENGTH   64

Definition at line 78 of file m2mb_httpc.h.

◆ M2MB_HTTPCLIENT_MAX_URL_LENGTH

#define M2MB_HTTPCLIENT_MAX_URL_LENGTH   1024

Definition at line 77 of file m2mb_httpc.h.

Typedef Documentation

◆ M2MB_HTTPC_CB_T

typedef VOID(* M2MB_HTTPC_CB_T) (VOID *arg, M2MB_HTTPC_STATES_E state, M2MB_HTTPC_RESPONSE_T *value)

User registered callback for returning response message.

This callback function is called when http response is received from the server. This callback function is called from internal http client thread. The data must be consumed during the callback function.

The received data can be chunked encoded or normal. case 1: if enable_rsp_header enabled: 1st callback : Contains response headers with M2MB_HTTPC_RX_MORE_DATA 2nd and subsequent calls : Contains the data with with M2MB_HTTPC_RX_MORE_DATA. last callback : Contains the data with M2MB_HTTPC_RX_FINISHED. case 2: if enable_rsp_header disabled :
1nd and subsequent calls : Contains the data with with M2MB_HTTPC_RX_MORE_DATA. last callback : Contains the data with M2MB_HTTPC_RX_FINISHED.

Parameters
[in]argArgument user provided callback context during m2mb_httpc_open()
[in]stateState in M2MB_HTTPC_STATES_E
[in]valuePointer to M2MB_HTTPC_RESPONSE_T Example
void m2m_s2w_httpc_cb(VOID* arg, M2MB_HTTPC_STATES_E state, M2MB_HTTPC_RESPONSE_T* value);
{
M2MB_HTTPC_RESPONSE_T rsp = {0 };
if( (NULL == arg) || (value == NULL))
return;
// data finished or more data
{
rsp = ( M2MB_HTTPC_RESPONSE_T *)value;
// got the response from the server
M2MB_DBG_INFO("response code %d\r\n", rsp->resp_Code);
// print the response data if length is valid
for(int i=0; i< rsp->length; i++)
M2MB_DBG_INFO("%c",rsp->data[i]);
}
else
{
M2MB_DBG_INFO("Error recevied %d", state);
}
}

Definition at line 213 of file m2mb_httpc.h.

◆ M2MB_HTTPC_CONF_ID_E

HTTP client confg id for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs.

◆ M2MB_HTTPC_CONF_PARAMS_T

HTTP client configuration params structure for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs.

◆ M2MB_HTTPC_CTXT_HANDLE

HTTPC context handle.
This is obtained from m2mb_httpc_open API and then used in subsequent HTTPC APIs.

Definition at line 82 of file m2mb_httpc.h.

◆ M2MB_HTTPC_METHODS_E

supported http request method for use with m2mb_httpc_request.

◆ M2MB_HTTPC_OPEN_PARAMS_T

HTTPC parameters required to open a client connection.

The structure is used in m2mb_httpc_open() for setting http client context.

◆ M2MB_HTTPC_RESPONSE_T

HTTP client response for use in user callback.

◆ M2MB_HTTPC_STATES_E

HTTP client callback state.

Enumeration Type Documentation

◆ M2MB_HTTPC_CONF_ID

HTTP client confg id for use in m2mb_http_set_cfg and m2mb_httpc_get_cfg APIs.

Enumerator
M2MB_HTTPC_CONF_INVALID 

invalid Param

M2MB_HTTPC_CONF_SET_HEADER 

set header

M2MB_HTTPC_CONF_SET_CGI_PARAMS 

set cgi params

M2MB_HTTPC_CONF_SET_ENABLE_RESP_HEADER 

enable response header in callback

M2MB_HTTPC_CONF_SET_CLEAR_ALL_HEADER 

clear all headers

M2MB_HTTPC_CONF_SET_TIMEOUT 

To set timeout

M2MB_HTTPC_CONF_GET_SERVER_IP 

To get the http server IP address

M2MB_HTTPC_CONF_GET_SOCK_ID 

To get the connection socket ID

Definition at line 127 of file m2mb_httpc.h.

◆ M2MB_HTTPC_METHODS

supported http request method for use with m2mb_httpc_request.

Enumerator
M2MB_HTTPC_METHOD_INVALID 
M2MB_HTTPC_METHOD_GET 
M2MB_HTTPC_METHOD_HEAD 
M2MB_HTTPC_METHOD_POST 
M2MB_HTTPC_METHOD_PUT 
M2MB_HTTPC_METHOD_MAX 

Definition at line 86 of file m2mb_httpc.h.

◆ M2MB_HTTPC_STATES

HTTP client callback state.

Enumerator
M2MB_HTTPC_RX_ERROR_SERVER_CLOSED 

Server closes the connection when the response has not been completely received yet.

M2MB_HTTPC_RX_ERROR_RX_PROCESS 

Size section of a chunk is longer than the RX buffer length.

M2MB_HTTPC_RX_ERROR_RX_HTTP_HEADER 

Header section is longer than the RX buffer length.

M2MB_HTTPC_RX_ERROR_INVALID_RESPONSECODE 

Status code is less than 100 or greater than 999.

M2MB_HTTPC_RX_ERROR_CLIENT_TIMEOUT 

Request times out.

M2MB_HTTPC_RX_ERROR_NO_BUFFER 

RESERVED.

M2MB_HTTPC_RX_CONNECTION_CLOSED 

RESERVED.

M2MB_HTTPC_RX_ERROR_CONNECTION_CLOSED 

Connection is closed due to error on socket read.

M2MB_HTTPC_RX_FINISHED 

Response is completely received.

M2MB_HTTPC_RX_MORE_DATA 

Response is partially received.

Definition at line 99 of file m2mb_httpc.h.

Function Documentation

◆ m2mb_httpc_close()

M2MB_STATUS_T m2mb_httpc_close ( HANDLE  handle)

closes an HTTP client session.

Closes from the server and frees the memory. This function internally takes case of disconnection from server. This API can be called anytime after m2mb_httpc_open()

Parameters
[in]handle
HTTP client session handle.
Returns
Returns M2MB_OK on success, a different value on error.

Example

void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
...
if(HTTP with SSL)
{
// Do all ssl configurations
// Load the CA Certificate
M2MB_SSL_CONF_PARAMS_T ssl_conf_params;
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cacert = (char*)"ca_list.bin";
status = m2mb_ssl_set_cfg( pM2mbSslCtx, M2MB_SSL_CONF_CACERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
// Load the client certificate
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cert = (char *)"clientCert";
status = m2mb_ssl_set_cfg( pM2mbSslCtx, M2MB_SSL_CONF_CERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
}
// Connect to HTTP Server
char *SrvAddr = "192.168.2.100";// or "hostname";
UINT16 SrvPort = 80;
status = m2mb_httpc_connect( pHTTPcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return NULL;
}
...
status = m2mb_httpc_close( pHTTPcHandle );
if (status != M2MB_OK )
{
return NULL;
}
...
}

◆ m2mb_httpc_connect()

M2MB_STATUS_T m2mb_httpc_connect ( HANDLE  handle,
UINT8 url,
UINT16  port 
)

Connects to an HTTP server in Blocking mode.

Parameters
[in]handleHTTP client session handle.
[in]urlPointer to server or proxy, e.g. "192.168.2.100" or "www.example.com"
[in]portPort of the server.
Returns
On success: M2MB_OK. On Error : Error code. Example
void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
if(HTTP with SSL)
{
// Do all ssl configurations before http connect
// Load the CA Certificate
M2MB_SSL_CONF_PARAMS_T ssl_conf_params;
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cacert = (char*)"ca_list.bin";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CACERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
// Load the client certificate
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cert = (char *)"clientCert";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
}
// Connect to HTTP Server
char *SrvAddr = "192.168.2.100";// or "hostname";
UINT16 SrvPort = 80;
status = m2mb_httpc_connect( pHTTPcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return NULL;
}
...
}

◆ m2mb_httpc_get_cfg()

M2MB_STATUS_T m2mb_httpc_get_cfg ( HANDLE  handle,
M2MB_HTTPC_CONF_ID_E  conf_id,
M2MB_HTTPC_CONF_PARAMS_T conf_params 
)

get the http client session information

This function is used to get various HTTP Client options. Based on the conf id the configuration value is used. This api must be used after m2mb_httpc_connect().

Parameters
[in]handleHTTP client session handle.
[in]conf_idHTTP Configuration ID. The following configuration ID's are supported :
    M2MB_HTTPC_CONF_GET_SERVER_IP : To get the http server IP address 
    M2MB_HTTPC_CONF_GET_SOCK_ID : To get the connection socket ID 
[out]conf_paramspointer to M2MB_HTTPC_CONF_PARAMS_T struct.
                    The information will be filled based on the CONF ID provided. 

            Mapping of CONF_ID to CONF_PARAMS struct:
                    M2MB_HTTPC_CONF_GET_SERVER_IP : conf_params.IP46_addr
                    M2MB_HTTPC_CONF_GET_SOCK_ID   : conf_params.sockId
Returns
Returns M2MB_OK on success, a different value on error. Example
void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
M2MB_STATUS_T httpc_status;
...
...
// to enabled response headers in the callback
memset(&confParams, 0 , sizeof(M2MB_HTTPC_CONF_PARAMS_T));
confParams.enable_rsp_header = 1; // 1: enable, 0: disable
status = m2mb_httpc_set_cfg(pHTTPcHandle, M2MB_HTTPC_CONF_CLEAR_ALL_HEADER, &confParams);
if (status != M2MB_OK )
{
return M2MB_ERROR;
}
...
// Connect to HTTP Server
char *SrvAddr = "test.com";// or "hostname";
UINT16 SrvPort = 80;
status = m2mb_httpc_connect( pHTTPcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return M2MB_ERROR;
}
...
// to get the ip address of the server
memset(&confParams, 0 , sizeof(M2MB_HTTPC_CONF_PARAMS_T));
status = m2mb_httpc_get_cfg(pHTTPcHandle, M2MB_HTTPC_CONF_GET_SERVER_IP, &confParams);
if (status != M2MB_OK )
{
return M2MB_ERROR;
}
printf(" Destination server ip %d", confParams.IP46_addr);
}

◆ m2mb_httpc_open()

M2MB_STATUS_T m2mb_httpc_open ( HANDLE hndl,
M2MB_HTTPC_OPEN_PARAMS_T params 
)

create http client context

This API creates a new http client context.

Parameters
[out]hndl
pointer to M2MB_HTTPC_CTXT_HANDLE
[in]paramspointer to structure of type M2MB_HTTPC_OPEN_PARAMS_T.
Returns
Returns M2MB_OK on success, a different value on error. Example
void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
}

◆ m2mb_httpc_send()

M2MB_STATUS_T m2mb_httpc_send ( VOID handle,
UINT8  isFirstPacket,
UINT8 body,
UINT32  body_length,
UINT32  total_length 
)

Send HTTP information from the http client session.

Parameters
[in]handleHTTP client session handle.
[in]isFirstPacketIf isFirstPacket set to TRUE then HTTP hraders will be sent in the packet.
[in]bodyis data body
[in]body_lengthis data body length
[in]total_lengthis total body length
Returns
Returns M2MB_OK on success, a different value on error.

Example

// callback function usuage
VOID m2m_s2w_httpc_cb(VOID* arg, M2MB_HTTPC_STATES_E state, M2MB_HTTPC_RESPONSE_T* value)
{
M2MB_HTTPC_RESPONSE_T request_rsp = {0 };
if( (NULL == arg) || (value == NULL))
return;
// data finished or more data
{
request_rsp = ( M2MB_HTTPC_RESPONSE_T *)value;
// got the response from the server
M2MB_DBG_INFO("response code %d\r\n", rsp->resp_Code);
// print the response data if length is valid
for(int i=0; i< rsp->length; i++)
M2MB_DBG_INFO("%c",rsp->data[i]);
}
else
{
M2MB_DBG_INFO("Error recevied %d", state);
}
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
// Connect to HTTP Server
status = m2mb_httpc_connect( pHTTPcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return NULL;
}
if(HTTP with SSL)
{
// Do all ssl configurations before http connect
// Load the CA Certificate
M2MB_SSL_CONF_PARAMS_T ssl_conf_params;
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cacert = (char*)"ca_list.bin";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CACERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
// Load the client certificate
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cert = (char *)"clientCert";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
}
// Connect to HTTP Server
char *SrvAddr = "192.168.2.100";// or "hostname";
UINT16 SrvPort = 80;
status = m2mb_httpc_connect( pHttpcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return NULL;
}
// Use Case-1: GET Request to server
// Once the response is received, user will receive all the data in the registered callback
// The received data can be chunked encoded or normal.
// For SET request from Uri, METHOD TYPE M2MB_HTTPC_METHODS_E
status = m2mb_httpc_set_request( pHttpcHandle, M2MB_HTTPC_METHOD_GET_E, "/index.html");
if (status != M2MB_OK )
{
return NULL;
}
...
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,NULL, 0, 0);
if (status != M2MB_OK )
{
return NULL;
}
// Use Case-2: POST 3000 Bytes to server without chunked encoding
// For SET request from Uri, METHOD TYPE M2MB_HTTPC_METHODS_E
status = m2mb_httpc_set_request( pHttpcHandle, M2MB_HTTPC_METHOD_POST_E, "/cgi-bin/posthere1.pl");
if (status != M2MB_OK )
{
return NULL;
}
...
char *pBodyPtr = data// pointer to first 1400 Bytes of data
UINT* isFirstPkt = M2MB_FRAGMENT_FIRST;
//body length of 1400 bytes, total length 3000 bytes
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 1400, 3000 );
if (status != M2MB_OK )
{
return NULL;
}
pBodyPtr = data// pointer to next 1400 Bytes of data
isFirstPkt = M2MB_FRAGMENT_MIDDLE;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 1400, 3000);
if (status != M2MB_OK )
{
return NULL;
}
pBodyPtr = data// pointer to next 200 Bytes of data
isFirstPkt = M2MB_FRAGMENT_LAST;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 200, 3000);
if (status != M2MB_OK )
{
return NULL;
}
// Use Case-3: POST 3000 Bytes to server with chunked encoding
// to set http header
memset(&confParams, 0 , sizeof(M2MB_HTTPC_CONF_PARAMS_T));
confParams.param.header_name = "Transfer-Encoding";
confParams.param.header_val = "chunked";
status = m2mb_httpc_set_cfg(pHTTPcHandle, M2MB_HTTPC_CONF_SET_HEADER, &confParams);
if (status != M2MB_OK )
{
return NULL;
}
// For SET request from Uri, METHOD TYPE M2MB_HTTPC_METHODS_E
status = m2mb_httpc_set_request( pHTTPcHandle, M2MB_HTTPC_METHOD_POST_E, "/cgi-bin/posthere1.pl");
if (status != M2MB_OK )
{
return M2MB_ERROR;
}
...
char *pBodyPtr = data// pointer to first 1400 Bytes of data
UINT* isFirstPkt = M2MB_FRAGMENT_FIRST;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 1400, 3000);
if (status != M2MB_OK )
{
return NULL;
}
pBodyPtr = data// pointer to next 1400 Bytes of data
isFirstPkt = M2MB_FRAGMENT_MIDDLE;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 1400, 3000);
if (status != M2MB_OK )
{
return NULL;
}
pBodyPtr = data// pointer to last 200 Bytes of data
isFirstPkt = M2MB_FRAGMENT_LAST;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,pBodyPtr, 200, 3000);
if (status != M2MB_OK )
{
return NULL;
}
// Send end of chunk data
isFirstPkt = M2MB_FRAGMENT_FIRST;
status = m2mb_httpc_send( pHttpcHandle, isFirstPkt,NULL, 0, 0);
if (status != M2MB_OK )
{
return NULL;
}
}

◆ m2mb_httpc_set_cfg()

M2MB_STATUS_T m2mb_httpc_set_cfg ( HANDLE  handle,
M2MB_HTTPC_CONF_ID_E  conf_id,
M2MB_HTTPC_CONF_PARAMS_T conf_params 
)

update the http client session configurations

This function is used to set various HTTP Client options. Based on the conf id the configuration value is used. This api must be used before m2mb_httpc_connect().

Parameters
[in]handleHTTP client session handle.
[in]conf_idHTTP Configuration ID. The following configuration ID's are supported : M2MB_HTTPC_CONF_SET_HEADER : set header M2MB_HTTPC_CONF_SET_CGI_PARAMS : set cgi params M2MB_HTTPC_CONF_ENABLE_RESP_HEADER : enable response header M2MB_HTTPC_CONF_CLEAR_ALL_HEADER : clear all headers
[in]conf_paramsstruct of type M2MB_HTTPC_CONF_PARAMS_T
            Mapping of CONF_ID to CONF_PARAMS Structure:

    M2MB_HTTPC_CONF_SET_HEADER                      :   confParams.param.header_name 
                                          confParams.param.header_val
    M2MB_HTTPC_CONF_ENABLE_RESP_HEADER      :   confParams.param.enable_rsp_header
    M2MB_HTTPC_CONF_CLEAR_ALL_HEADER        :   NULL
Returns
Returns M2MB_OK on success, a different value on error. Example
void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
M2MB_STATUS_T httpc_status;
...
...
// To clear all the headers
M2MB_HTTPC_CONF_PARAMS_T conf_params = {0};
conf_params.param.header_name = NULL;
conf_params.param.header_val = NULL;
httpc_status = m2mb_httpc_set_cfg( pHTTPcHandle, M2MB_HTTPC_CONF_CLEAR_ALL_HEADER, &conf_params );
if( httpc_status != M2MB_OK )
{
return NULL;
}
// To set the http header parameters
memset(&confParams, 0 , sizeof(M2MB_HTTPC_CONF_PARAMS_T));
confParams.param.header_name = "Connection";
confParams.param.header_val = "Keep-alive";
httpc_status = m2mb_httpc_set_cfg(pHTTPcHandle, M2MB_HTTPC_CONF_SET_HEADER, &confParams);
if (httpc_status != M2MB_OK )
{
return NULL;
}
// to enabled response headers in the callback
memset(&confParams, 0 , sizeof(M2MB_HTTPC_CONF_PARAMS_T));
confParams.enable_rsp_header = 1; // 1: enable, 0: disable
httpc_status = m2mb_httpc_set_cfg(pHTTPcHandle, M2MB_HTTPC_CONF_CLEAR_ALL_HEADER, &confParams);
if (httpc_status != M2MB_OK )
{
return NULL;
}
...
}

◆ m2mb_httpc_set_request()

M2MB_STATUS_T m2mb_httpc_set_request ( VOID handle,
M2MB_HTTPC_METHODS_E  method,
UINT8 url 
)

Set the http request.

Parameters
[in]handleHTTP client session handle.
[in]method
[in]url
Returns
Returns M2MB_OK on success, a different value on error.

Example

void m2m_s2w_httpc_cb(VOID* arg, signed int state, M2MB_HTTPC_RESPONSE_T* value);
{
...
}
int main()
{
M2MB_STATUS_T status;
M2MB_HTTPC_OPEN_PARAMS_T httpcOpenParams = {0};
void * pM2mbSslCtxHandle = NULL;
if(HTTP_WITH_SSL)
{
//role: M2MB_SSL_SSL_ROLE_E
M2MB_SSL_OPEN_PARAMS_T sslOpenParams;
M2MB_SSL_CTXT_HANDLE pM2mbSslCtxHandle;
status = m2mb_ssl_open(&pM2mbSslCtxHandle, &sslOpenParams);
if(status != M2MB_OK)
return M2MB_ERROR;
}
httpcOpenParams.timeout = 100;
httpcOpenParams.httpc_max_body_len = 1400;
httpcOpenParams.httpc_max_Header_len = 1400;
httpcOpenParams.httpc_rx_buf_len = 1400;
httpcOpenParams.m2mbSslCtx = pM2mbSslCtxHandle;
httpcOpenParams.cb = m2m_s2w_httpc_cb;
httpcOpenParams.cb_arg = NULL;
HANDLE pHTTPcHandle = NULL;
status = m2mb_httpc_open( &pHTTPcHandle, &httpcOpenParams );
if(status != M2MB_OK)
return M2MB_ERROR;
if(HTTP with SSL)
{
// Do all ssl configurations before http connect
// Load the CA Certificate
M2MB_SSL_CONF_PARAMS_T ssl_conf_params;
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cacert = (char*)"ca_list.bin";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CACERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
// Load the client certificate
memset(&ssl_conf_params, 0, sizeof(M2MB_SSL_CONF_PARAMS_T));
ssl_conf_params.cert = (char *)"clientCert";
status = m2mb_ssl_set_cfg( pM2mbSslCtxHandle, M2MB_SSL_CONF_CERT, ssl_conf_params );
if(status != M2MB_OK )
{
return M2MB_ERROR;
}
}
// Connect to HTTP Server
char *SrvAddr = "192.168.2.100";// or "hostname";
UINT16 SrvPort = 80;
status = m2mb_httpc_connect( pHTTPcHandle, SrvAddr, (UINT16)SrvPort);
if (status != M2MB_OK )
{
return NULL;
}
// For SET request from Uri, METHOD TYPE M2MB_HTTPC_METHODS_E
status = m2mb_httpc_set_request( pHTTPcHandle, M2MB_HTTPC_METHOD_POST_E, "FILE_PATH");
if (status != M2MB_OK )
{
return NULL;
}
...
...
}