/* Try to post the "msg" to the mailbox. Returns ERR_MEM if this one is full, else, ERR_OK if the "msg" is posted.  */
int k_mbox_trypost(k_mbox_t *mb, void *msg)
/* Try to post the "msg" to the mailbox. Returns ERR_MEM if this one is full, else, ERR_OK if the "msg" is posted.  */
int k_mbox_trypost(k_mbox_t *mb, void *msg)
{
    u8_t first;
    k_mbox_t *mbox;
    if (NULL == mb) {
        BT_ERR("invaild mbox");
        return ERR_MEM;
    }
    mbox = mb;
    k_sem_take(&mbox->mutex, K_FOREVER);
    BT_DBG("k_mbox_trypost: mbox %p msg %p\n", (void *)mbox, (void *)msg);
    if ((mbox->last + 1) >= (mbox->first + K_MBOX_SIZE)) {
        k_sem_give(&mbox->mutex);
        return ERR_MEM;
    }
    mbox->msgs[mbox->last % K_MBOX_SIZE] = msg;
    if (mbox->last == mbox->first) {
        first = 1;
    } else {
        first = 0;
    }
    mbox->last++;
    if (first) {
        k_sem_give(&mbox->not_empty);
    }
    k_sem_give(&mbox->mutex);
    return ERR_OK;
}