Auto Mode Example

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

/*
*********************************************************************************************************
*                                            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];

/*
*********************************************************************************************************
*                                      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 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 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) {
        *p_data = HTTPcExample_RxBuf;                           /* Set the pointer where the message data must be set.  */
    }
}

/*
*********************************************************************************************************
*                                   WEBSOCK ON MSG RX COMPLETE HOOK
*********************************************************************************************************
*/
static void  HTTPcExample_WebSockOnMsgRxComplete (HTTPc_CONN_OBJ *p_conn)
{
                                                                /* Once the message is completely received...           */
                                                                /* ...Do Something.                                     */
}