This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Send OSAL message to multiple tasks

Hello,
I am having difficulty sending the same message to multiple tasks while using TI BLE Stack version 1.4.   I have the following code to accomplish sending a message to multiple tasks.  The problem I am running into is that after the first osal_msg_send call, I get the following error message (printed from my code):

[OpMode.c:911] ERROR: Failed to send 'Change Mode' Message to Task 0xb (Status (0x5) 'INVALID_MSG_POINTER')

Which makes some sense, because the receiving tasks call osal_msg_deallocate after processing the OSAL message. I could forsee getting around this issue by allocating in the loop, but I believe OSAL should be able to handle this. So, long story short, is there a way to send a OSAL message to multiple tasks (w/o allocating memory for each message sent)?

/// Op-Mode Connection Change Request Message
typedef struct
{
    osal_event_hdr_t hdr;    ///< OSAL Message Header
    bool connect;            ///< True to connect, False to disconnect
    const char* pFile;       ///< The calling file name (could use __FILE__)
    uint16 lineNo;           ///< The calling line number (could use __LINE__)
} OpModeConnectMsg_t;

static OpModeResult_t SendConnectionChange( bool connect,
                                            const char* pFile,
                                            const uint16 lineNo )
{
    OpModeResult_t result = OP_MODE_RESULT_ERROR;
    OpModeConnectMsg_t* pMsg;

    TRACE_FLOW( THIS_FILE, __LINE__, TRACE_ENABLE,
                "Send Connection Change (0x%x) Request from %s:%d",
                connect,
                pFile,
                lineNo );

    pMsg = (OpModeConnectMsg_t*) osal_msg_allocate( sizeof( OpModeConnectMsg_t ) );

    if ( pMsg != NULL )
    {
        pMsg->hdr.event = OP_MODE_CONNECTION_CHANGE_REQUEST;
        pMsg->connect   = connect;
        pMsg->pFile     = pFile;
        pMsg->lineNo    = lineNo;

        result = OP_MODE_RESULT_OK;

        SendMsgToRegistered( (uint8*) pMsg,
                             "Connection Change",
                             THIS_FILE,
                             __LINE__ );
    }

    return ( result );
}

static void SendMsgToRegistered( uint8* pMsg,
                                 const char* pDescription,
                                 const char* pFile,
                                 const uint16 lineNo )
{
    uint8 status;
    uint8 i;
    for ( i = 0; i < numRegisteredTasks; i++ )
    {
        if ( registeredTasks[i] == INVALID_TASK_ID )
        {
            continue;
        }

        status = osal_msg_send( registeredTasks[i],
                                pMsg );

        if ( status != SUCCESS )
        {
            TRACE_ERROR( pFile, lineNo, TRACE_ENABLE,
                         "Failed to send '%s' Message to Task 0x%x " \
                         "(Status (0x%x) '%s')",
                         pDescription,
                         registeredTasks[i],
                         status,
                         StatusToString( status ) );
        }
        else
        {
            TRACE_FLOW( pFile, lineNo, TRACE_ENABLE,
                        "Sent '%s' Message to Task 0x%x",
                        pDescription,
                        registeredTasks[i] );
        }
    }
}

  • Hi,

    looking into the current OSAL implementation, i guess your idea to use one API call to send messages to multiple tasks is not possible. The osal_msg_enqueue_push() which is basically the implementation of osal_msg_send will only check the task ID to be valid (not greater than taskCnt) and it will only send an event to the specific single task at the end of the function.

    Another thing which will be confusing is that since messages need to be deallocated after received and processed, how will the receiver tasks know that it is the last one to read the task and need to execute de-allocate API.

    So although this will require copying of identical messages and sending each message to the multiple receiver task, i believe this is the cleaner and safer option to do.