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.

关于BLE协议栈 结构体指针强制转换的问题

请大家指点一下:

在OSAL.h中有如下两个结构体:

typedef struct {  

 void   *next;  

uint16 len;  

uint8  dest_id;

} osal_msg_hdr_t;

 

typedef struct {

  uint8  event;  

uint8  status;

 } osal_event_hdr_t;

在peripheral.c中,有如下语句:

uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events ) {  

    VOID task_id;                                                                           // OSAL required parameter that isn't used in this function

  if ( events & SYS_EVENT_MSG )  

 {     uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )   

     {    

       gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

        。。。。。

 

}

static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )

{  

switch ( pMsg->event )  

{ case GAP_MSG_EVENT:      

 gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );

。。。

}

osal_msg_receive函数return (uint8*) foundHdr ,而 foundHdr 是 osal_msg_hdr_t * 型的。

所以在pMsg = osal_msg_receive( gapRole_TaskID )后,pMsg是一个uint8 *型的指向有那么一个结构

void   *next;  

uint16 len;  

uint8  dest_id;

}

的指针,该结构的长度为sizeof(void *)+2+1个字节;

gapRole_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );语句后,pMsg变成了指向osal_event_hdr_t结构体的指针,该结构为

{

  uint8  event;  

uint8  status;

}共两个字节;

请问:

pMsg的值并没有改变,它指向的还是同一个存储块,但是

{

void   *next; 

uint16 len; 

uint8  dest_id;

}却变成了

{

  uint8  event; 

uint8  status;

}完全是不一样的数据结构

那在 gapRole_ProcessOSALMsg函数中的 pMsg->event ,这event是怎么变来的,我想不明白,请大家给我指点一二,感激不尽

 

  • 仔细看一下如下这个函数,注意看红色的。

    uint8 OnBoard_SendKeys( uint8 keys, uint8 state )
    {
      keyChange_t *msgPtr;

      if ( registeredKeysTaskID != NO_TASK_ID )
      {
        // Send the address to the task
        msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );
        if ( msgPtr )
        {
          msgPtr->hdr.event = KEY_CHANGE;
          msgPtr->state = state;
          msgPtr->keys = keys;

          osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );
        }
        return ( SUCCESS );
      }
      else
        return ( FAILURE );
    }

    所以一个消息是由三部分组成,如下 先是osal_msg_hdr_t,接后面是osal_event_hdr_t,最后才是用户数据:

    0.osal_msg_hdr_t

    1.osal_event_hdr_t hdr;

    2.用户数据

    osal_msg_allocate分配的是 osal_mem_alloc( (short)(len + sizeof( osal_msg_hdr_t )) ); 它的输入参数是keyChange_t ,

    而keyChange_t为

    typedef struct
    {
      osal_event_hdr_t hdr;
      uint8             state; // shift
      uint8             keys;  // keys
    } keyChange_t;

    其他情况类似,如gaprole消息等等,不一一列举

    typedef struct
    {
      osal_event_hdr_t      hdr;
      gapCentralRoleRssi_t  *pRssi;
    } gapCentralRoleRssiEvent_t;

  • hi,TY

      非常感谢您的解答,已经让我明白了,我看代码看得不仔细,把osal_msg_allocateosal_mem_alloc看混了,所以一直没明白。关于gaprole消息,我没有找到是在哪儿发送的,这些消息是不是协议栈自动去发送的,具体代码没有暴露给用户