pass msg back to main

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

pass msg back to main

Post by Tabulous »

how can one pass a msg back to the main thread.

i.e. how do i get the pointer to main thread so i can post this to the thread doing the work and then wait for the response ?

Code: Select all


int main(void)
{

    dongmsg.key = &key;
    dongmsg.tp = *******calling thread pointer********;
    dongmsg.id = DONG_KEY_REQUEST;
    dongmsg.imei = md->manu.imei;

    chMBPost( mbox,
              ( msg_t )&dongmsg,
              TIME_INFINITE );

    p = chMsgWait();

    if( p != NULL )
    {
    }
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: pass msg back to main

Post by Giovanni »

Hi,

You are mixing mailboxes and synchronous messages.

Code: Select all

static THD_FUNCTION(ServerThread, arg) {
 
  while (true) {
    /* Waiting for a queued message then retrieving it.*/
    thread_t *tp = chMsgWait();
    msg_t msg = chMsgGet(tp);
 
    /* Processing the message.*/
    ...;
 
    /* Sending back an acknowledge.*/
    chMsgRelease(tp, MSG_OK);
  }
}


You send messages to the server using chMsgSend(), it returns the answer, MSG_OK in the example.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: pass msg back to main

Post by Tabulous »

forget about my code it is misleading, all i want is how do i get a pointer to the main thread structure ?

normally when you create a thread you can assign it like this

pThread = chThdCreateStatic( ETICKETWA, sizeof(ETICKETWA),
NORMALPRIO, ThreadEticket,
NULL );

but what about int main(void) ? or does this not have a thread structure ?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: pass msg back to main

Post by Giovanni »

You can get the pointer to the current thread from the main (chThdSelf() in 2.6, chThdGetSelfX() in 3.0) and then store it somewhere for other threads to use.

Giovanni
Post Reply