/* similar to sys_arch_mbox_fetch, however if a message is not present in the mailbox, it immediately returns with the code SYS_MBOX_EMPTY.  */
int k_mbox_tryfetch(k_mbox_t *mb, void **msg)
/* similar to sys_arch_mbox_fetch, however if a message is not present in the mailbox, it immediately returns with the code SYS_MBOX_EMPTY.  */
int k_mbox_tryfetch(k_mbox_t *mb, void **msg)
{
    k_mbox_t *mbox;
    if (NULL == mb) {
        BT_ERR("invaild mbox");
        return ERR_MEM;
    }
    mbox = mb;
    k_sem_take(&mbox->mutex, K_FOREVER);
    if (mbox->first == mbox->last) {
        k_sem_give(&mbox->mutex);
        return SYS_MBOX_EMPTY;
    }
    if (msg != NULL) {
        BT_DBG("k_mbox_tryfetch: mbox %p msg %p\n", (void *)mbox, *msg);
        *msg = mbox->msgs[mbox->first % K_MBOX_SIZE];
    } else {
        BT_DBG("k_mbox_tryfetch: mbox %p, null msg\n", (void *)mbox);
    }
    mbox->first++;
    if (mbox->wait_send) {
        k_sem_give(&mbox->not_full);
    }
    k_sem_give(&mbox->mutex);
    return 0;
}