1.2.1.2 Using the BLE Software

Application shall be able to get the events from BLE Stack if the callback event is registered. And it is strongly recommend application shall not handle the event directly. Because timing is critical for BLE Stack, application shall not occupy the execution time before task switch. Application shall generate a new message to handle the event within application task execution time.

Example of handling event from BLE Stack:

    //Create a new message and handler the event later
    void APP_BleStackCb(STACK_Event_T *p_stack)
    {
        STACK_Event_T stackEvent;
        APP_Msg_T   appMsg;
        APP_Msg_T   *p_appMsg;
    
        memcpy((uint8_t *)&stackEvent, (uint8_t *)p_stack, sizeof(STACK_Event_T));
        stackEvent.p_event=OSAL_Malloc(p_stack->evtLen);
        if(stackEvent.p_event==NULL)
        {
            return;
        }
        memcpy(stackEvent.p_event, p_stack->p_event, p_stack->evtLen);
        stackEvent.p_event=stackEvent.p_event;
        ...
    
        appMsg.msgId=APP_MSG_BLE_STACK_EVT;
    
        ((STACK_Event_T *)appMsg.msgData)->groupId=p_stack->groupId;
        ((STACK_Event_T *)appMsg.msgData)->evtLen=p_stack->evtLen;
        ((STACK_Event_T *)appMsg.msgData)->p_event=stackEvent.p_event;
    
        p_appMsg = &appMsg;
        OSAL_QUEUE_Send(&appData.appQueue, p_appMsg, 0);
    }
    
    //Handle the BLE Stack events
    void APP_Tasks ( void )
    {
        APP_Msg_T    appMsg[1];
        APP_Msg_T   *p_appMsg;
        p_appMsg=appMsg;
    
        /* Check the application's current state. */
        switch ( appData.state )
        {
            ...
            case APP_STATE_SERVICE_TASKS:
            {
                if (OSAL_QUEUE_Receive(&appData.appQueue, &appMsg, OSAL_WAIT_FOREVER))
                {
                    if(p_appMsg->msgId==APP_MSG_BLE_STACK_EVT)
                    {
                        APP_BleStackEvtHandler((STACK_Event_T *)p_appMsg->msgData);
                    }
                }
                break;
            }
        }
    }
}

All the group events of BLE Stack comes in one entry point. Hence, application shall check the groupId to identify the group first when receiving the BLE Stack event. And then check the eventId in each group.

Example of handling the BLE Stack events:

void APP_BleStackEvtHandler(STACK_Event_T *p_stackEvt)
{
    switch (p_stackEvt->groupId)
    {
        case STACK_GRP_BLE_GAP:
        {
            /* TODO: implement your application code.*/
        }
        break;
        
        case STACK_GRP_BLE_L2CAP:
        {
            /* TODO: implement your application code.*/
        }
        break;

        case STACK_GRP_BLE_SMP:
        {
            /* TODO: implement your application code.*/
        }
        break;

        case STACK_GRP_GATT:
        {
            /* TODO: implement your application code.*/
        }
        break;

    } 
    OSAL_Free(p_stackEvt->p_event);
}