Report
2 months, 1 week ago.

Does mail .full() and .empty() work?

I'd like to do the following

mbed empty

 if (mail_box.empty() ) { //do something }

However, it states that rtos::mail doesn't have member empty.

I tried to use full as well, same error.

I still don't know how to check if something is programmed for particular board, so could it be there is no rtos empty or free for LPC1768?

mbed full

 #include "mbed.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Serials~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
RawSerial pc(USBTX,USBRX);          //pcLog 

/* Mail */
typedef struct {
  float    voltage;                     /* AD result of measured voltage */
  float    current;                     /* AD result of measured current */
  uint32_t counter;                     /* A counter value               */
} mail_t;

Mail<mail_t, 16> mail_box;              //Create a mail queue: Mail<data type of a single mail message element, maximum number of mail messages in queue> mail name;
Thread thread;

void send_thread (void) {
    uint32_t i = 0;
    while (true) {
        printf("Fake data update\n");
        i++; // fake data update
        mail_t *mail = mail_box.alloc();        // .alloc() returns pointer to memory block that you can fill with mail or NULL in case error.
        mail->voltage = (i * 0.1) * 33;         //dereferences pointer mail to fetch the variable voltage from it and sets a random value to it.
        mail->current = (i * 0.1) * 11;
        mail->counter = i;
        mail_box.put(mail);                     //.put(mail) puts a mail in the mail queue named mail_box.
        wait(10);
        if (mail_box.full() ) { 
            printf("DEBUG: Mail is full");
            wait(2); 
        }
    }
}

int main (void) {
    thread.start(callback(send_thread));
    
    while (true) {
        osEvent evt = mail_box.get(0);                                           //Gets a mail from a queue. Returns osEventMessage if message is received.
        if (evt.status == osEventMail) {                                        //
            mail_t *mail = (mail_t*)evt.value.p;
            printf("\nVoltage: %.2f V\n\r"   , mail->voltage);
            printf("Current: %.2f A\n\r"     , mail->current);
            printf("Number of cycles: %u\n\r", mail->counter);
            
            mail_box.free(mail);        //frees the mail just received, where parameter passed is pointer to the memory block that was obtaines with mail::get
        }
    }
}

Gives error: Class "rtos::Mail" has no member "full" "if (mail_box.full() ) {"

Comment on this question

2 Answers

2 months, 1 week ago.

Some thoughts.

In your example if alloc fails it returns null, and then the code will try to access members for the null pointer (this might be undefined behavior? I don't know.) and then it puts null pointer in the mailbox. It seems like you should check for full before doing the alloc(), maybe more like this:

if (!mail_box.full() ) {
       mail_t *mail = mail_box.alloc();   
     // etc.
}

I notice the Mail class didn't always have methods full() and empty(). Any chance you're using an older version of mbed 5?

This was how to check for mailbox full prior to the full() method:

mail_t *mail = mail_box.alloc();

if (mail == NULL) {
    printf("DEBUG: Mail is full");
}
else {
    //...
    mail_box.put(mail);
}

I started with mbed start of this year, so it would be mbed os 5 from that sort of time. I find it very difficult to update mbed, nothing is ever simple with this one..

Edit: It was to do with my mbed os 5 version. Thank you for your help. Although it is most unfortunate that I get compiler errors with this new mbed version, I guess I might have to find version where full() and empty() work without giving me other errors. I think it is due to compiler 6, not sure..

posted by L B 13 May 2019
2 months, 1 week ago.

Mail object has empty method, it should work. What are you include paths? Using online compiler or offline ?

RTOS API is target agnostic, it should be available for your target. This indicates the problem is somewhere else (enviroment, code itself).

I'm using an online compiler and the only thing I did was to slightly modify the mail example. I include mbed.h only and I'm using mbed-os so OS5.

posted by L B 10 May 2019

To post an answer, please log in.