Normal Mode Example

This example show how to open a WebSocket with Data message Reception hooks configured in Normal Mode.

#include <Client/Source/http-c.h> 
/*
*********************************************************************************************************
*                                            LOCAL DEFINES
*********************************************************************************************************
*/

#define  HTTPc_EXAMPLE_URI              "server.example.com"
#define  HTTPc_EXAMPLE_RESSOURCE        "/echo"
#define  HTTPc_EXAMPLE_CONN_BUF_LEN     1024
#define  HTTPc_EXAMPLE_MSG_MAX_LEN      512
 
/*
*********************************************************************************************************
*                                       LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/

static  HTTPc_CONN_OBJ         HTTPcExample_Conn;
static  CPU_CHAR               HTTPcExample_ConnBuf[HTTPc_EXAMPLE_CONN_BUF_LEN];
static  HTTPc_REQ_OBJ          HTTPcExample_Req;
static  HTTPc_RESP_OBJ         HTTPcExample_Resp;
static  HTTPc_WEBSOCK_OBJ      HTTPcExample_WebSock;

static  CPU_CHAR               HTTPcExample_RxBuf[HTTPc_EXAMPLE_MSG_MAX_LEN];
static  CPU_INT32U             HTTPcExample_RxMsgLenToGet ;
 
/*
*********************************************************************************************************
*                                      LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static void        HTTPcExample_ConnOnClose            (HTTPc_CONN_OBJ            *p_conn,
                                                        HTTPc_CONN_CLOSE_STATUS    close_status,
                                                        HTTPc_ERR                  err);

static void        HTTPcExample_WebSockOnMsgRxInit     (HTTPc_CONN_OBJ            *p_conn,
                                                        HTTPc_WEBSOCK_MSG_TYPE     msg_type,
                                                        CPU_INT32U                 msg_len,
                                                        void                     **p_data);

static CPU_INT32U  HTTPcExample_WebSockOnMsgRxData     (HTTPc_CONN_OBJ            *p_conn,
                                                        HTTPc_WEBSOCK_MSG_TYPE     msg_type,
                                                        void                      *p_data,
                                                        CPU_INT16U                 data_len);

static void        HTTPcExample_WebSockOnMsgRxComplete (HTTPc_CONN_OBJ            *p_conn);
 
/*
*********************************************************************************************************
*                                               EXAMPLE
*********************************************************************************************************
*/

CPU_BOOLEAN  HTTPcExample_OpenWebSocket (void)
{
    CPU_BOOLEAN  result;
    HTTPc_ERR    err;
                                                                /* ----------- INIT NEW CONNECTION & REQUEST ---------- */
                                                                /* Always Clear the object.                             */
    HTTPc_ConnClr(&HTTPcExample_Conn, &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
    HTTPc_ReqClr(&HTTPcExample_Req, &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
    HTTPc_WebSockClr(&HTTPcExample_WebSock, &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ------------- SET ON CONNECTION CLOSE -------------- */
                                                                /* This callback is always required.                    */
    HTTPc_ConnSetParam(        &HTTPcExample_Conn,
                                HTTPc_PARAM_TYPE_CONN_CLOSE_CALLBACK,
                       (void *) HTTPcExample_ConnOnClose,
                               &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ----------------- OPEN CONNECTION ------------------ */
    result = HTTPc_ConnOpen(&HTTPcExample_Conn,
                             HTTPcExample_ConnBuf,
                             HTTPc_EXAMPLE_CONN_BUF_LEN,
                             HTTPc_EXAMPLE_URI,
                             sizeof(HTTPc_EXAMPLE_URI),
                             HTTPc_FLAG_NONE,
                            &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ----------- SET WEBSOCK HOOKS/CALLBACKS ------------ */
                                                                /* Set OnMsgRxInit hooks.                               */
    HTTPc_WebSockSetParam(        &HTTPcExample_WebSock,
                                   HTTPc_PARAM_TYPE_WEBSOCK_ON_MSG_RX_INIT,
                          (void *) HTTPcExample_WebSockOnMsgRxInit,
                                  &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* Set OnMsgRxData hooks.                               */
    HTTPc_WebSockSetParam(        &HTTPcExample_WebSock,
                                   HTTPc_PARAM_TYPE_WEBSOCK_ON_MSG_RX_DATA,
                          (void *) HTTPcExample_WebSockOnMsgRxData,
                                  &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* Set OnMsgRxComplete hooks.                           */
    HTTPc_WebSockSetParam(        &HTTPcExample_WebSock,
                                   HTTPc_PARAM_TYPE_WEBSOCK_ON_MSG_RX_COMPLETE,
                          (void *) HTTPcExample_WebSockOnMsgRxComplete,
                                  &err);
    if (err != HTTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ------- UPGRADE HTTP CONNECTION TO WEBSOCKET ------- */
    result = HTTPc_WebSockUpgrade(&HTTPcExample_Conn,
                                  &HTTPcExample_Req,
                                  &HTTPcExample_Resp,
                                  &HTTPcExample_WebSock,
                                   HTTPc_EXAMPLE_RESSOURCE,
                                   sizeof(HTTPc_EXAMPLE_RESSOURCE),
                                   HTTPc_FLAG_NONE,
                                  &err);
    if (result != DEF_OK) {
        return (DEF_FAIL);
    }
    return (DEF_OK);
}

/*
*********************************************************************************************************
*                                      CONNECTION ON CLOSE CALLBACK
*********************************************************************************************************
*/

static void  HTTPcExample_ConnOnClose (HTTPc_CONN_OBJ           *p_conn,
                                       HTTPc_CONN_CLOSE_STATUS   close_status,
                                       HTTPc_ERR                 err)
{
                                                                /* Called  when a connection close.                     */
}

/*
*********************************************************************************************************
*                                    WEBSOCK ON MSG RX INIT HOOK
*********************************************************************************************************
*/
static void  HTTPcExample_WebSockOnMsgRxInit (HTTPc_CONN_OBJ            *p_conn,
                                              HTTPc_WEBSOCK_MSG_TYPE     msg_type,
                                              CPU_INT32U                 msg_len,
                                              void                     **p_data)
{
                                                                /* Validate that we have enough space in the buffer...  */
                                                                /* ...to get all the message.                           */
    if (msg_len <= HTTPc_EXAMPLE_MSG_MAX_LEN) {
        HTTPcExample_RxMsgLenToGet = msg_len;
    } else {
        HTTPcExample_RxMsgLenToGet = 0;
    }
}
/*
*********************************************************************************************************
*                                    WEBSOCK ON MSG RX DATA HOOK
*********************************************************************************************************
*/

static CPU_INT32U  HTTPcExample_WebSockOnMsgRxData (HTTPc_CONN_OBJ            *p_conn,
                                                    HTTPc_WEBSOCK_MSG_TYPE     msg_type,
                                                    void                      *p_data,
                                                    CPU_INT16U                 data_len)
{
    CPU_INT16U  data_len_used;
 
    data_len_used = 0u;

    if (HTTPcExample_RxMsgLenToGet != 0u) {
        if (HTTPcExample_RxMsgLenToGet == data_len) {
                                                                /* In this example, wait to have all the message...     */
                                                                /* ...before copying it in the reception buffer.        */
            Mem_Copy(HTTPcExample_RxBuf, p_data, data_len);
            HTTPcExample_RxMsgLenToGet = 0u;
            data_len_used              = data_len;
        }
    }
    return data_len_used;                                       /* Return that number of bytes used.                    */
}
 
/*
*********************************************************************************************************
*                                   WEBSOCK ON MSG _RX COMLETE HOOK
*********************************************************************************************************
*/

static void  HTTPcExample_WebSockOnMsgRxComplete (HTTPc_CONN_OBJ *p_conn)
{
                                                                /* Once the message is completely received...           */
                                                                /* ...Do Something.                                     */

}