Issue with mailboxes and pointers

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
fractal
Posts: 16
Joined: Mon Oct 28, 2013 12:13 pm
Has thanked: 2 times
Been thanked: 5 times

Issue with mailboxes and pointers

Post by fractal »

Hallo everyone,

I have a pretty stupid question, but unfortunately I now have a big knot of pointers in my head and can not figure the answer out. Maybe you can help me before I spend even more time on this stupid problem.

So the situation is this: I want to use a mailbox to communicate between a console command and a worker thread, where the user via console creates a number of tests and the thread asynchronously executes one by one. The tests are defined as structs containing a few floats and the messages in the mailbox are then pointers to these structs. Thats the idea, but I found the whole thing does not work (the received pointer contains nonsense), so I must have messed up some pointing somewhere. I think the issue might be with the init, what kind of buffer structure does that command need?

The code looks like this:

Definitions:

Code: Select all

typedef struct
{
  float test_voltage;
  float test_current;
  float first_pulse_length;
  uint8_t charging_voltage;
}test_data_t;

#define MAX_SEQUENTIAL_TESTS 1024
mailbox_t test_mailbox;
test_data_t test_data[MAX_SEQUENTIAL_TESTS];


Init after system init:

Code: Select all

chMBObjectInit(&test_mailbox, (msg_t*)&test_data, MAX_SEQUENTIAL_TESTS);


Console command posting a message:

Code: Select all

MB_status = chMBPost(&test_mailbox, (msg_t)&new_test_data, TIME_IMMEDIATE);      // put new test data into the mailbox


Worker thread getting a message and looking at its content:

Code: Select all

chMBFetch(&test_mailbox, &struct_pointer, TIME_INFINITE);        // try to get a message containing a new test config, waits until one is available
running_test = *(test_data_t*)struct_pointer;


Thanks in advance,
Thomas
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Issue with mailboxes and pointers

Post by Giovanni »

Hi,

The buffer of the mailbox must always be of type msg_t which can be cast from/to pointer. Your structures must be passed by pointer, you can handle their allocation using a memory pool.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Issue with mailboxes and pointers

Post by Giovanni »

A while ago I wrote this small module, an handler of "mail" objects to be exchanged using mailboxes. You may give it a try but I never tested it, others used it. It is not very complex anyway.

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    mail.h
 * @brief   Threads mail macros and structures.
 *
 * @addtogroup mail
 * @{
 */

#ifndef _MAIL_H_
#define _MAIL_H_

/*
 * Module dependencies check.
 */
#if !CH_USE_SEMAPHORES || !CH_USE_MEMPOOLS
#error "Mail Pools require CH_USE_SEMAPHORES and CH_USE_MEMPOOLS"
#endif

/**
 * @brief   Mail Pool descriptor.
 */
typedef struct {
  MemoryPool            pool;           /**< @brief Available mail objects. */
  Semaphore             sem;            /**< @brief Semaphore guard.        */
} MailPool;

#ifdef __cplusplus
extern "C" {
#endif
  void mailInit(MailPool *mlp, size_t size, void *p, size_t n);
  void *mailCreate(MailPool *mlp, systime_t time);
  void mailDelete(MailPool *mlp, void *mailp);
#ifdef __cplusplus
}
#endif

#endif /* _MAIL_H_ */

/** @} */


Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    mail.c
 * @brief   Threads mail code.
 *
 * @addtogroup mail
 * @{
 */

#include "ch.h"
#include "mail.h"

/**
 * @brief   Initializes a Mail Pool.
 * @note    The number of the mail objects in the mail pool should be at
 *          least <b>2+size(mailbox)</b>, this considering one writer and
 *          one reader, add one element for each extra reader or writer in
 *          order to avoid waiting on the mail pool. A smaller number of
 *          elements can be specified if waiting on the pool is acceptable.
 *
 * @param[out] mlp      pointer to a @p MailPool structure
 * @param[in] size      the size of the mail objects to be placed in the pool
 * @param[in] p         pointer to the mail objects array first element
 * @param[in] n         number of elements in the mail objects array
 *
 * @init
 */
void mailInit(MailPool *mlp, size_t size, void *p, size_t n) {

  chPoolInit(&mlp->pool, size, NULL);
  chPoolLoadArray(&mlp->pool, p, n);
  chSemInit(&mlp->sem, (cnt_t)n);
}

/**
 * @brief   Allocates a mail object from a mail pool.
 * @pre     The mail pool must be already been initialized.
 *
 * @param[in] mlp       pointer to a @p MailPool structure
 * @param[in] time      the number of ticks before the operation timeouts,
 *                      the following special values are allowed:
 *                      - @a TIME_IMMEDIATE immediate timeout.
 *                      - @a TIME_INFINITE no timeout.
 *                      .
 * @return              The mail object.
 * @retval NULL         timeout expired.
 *
 * @api
 */
void *mailCreate(MailPool *mlp, systime_t time) {
  msg_t msg;
  void *mailp;

  msg = chSemWaitTimeout(&mlp->sem, time);
  if (msg != RDY_OK)
    return NULL;
  mailp = chPoolAlloc(&mlp->pool);
  chDbgAssert(mailp != NULL, "mailCreate(), #1", "empty pool");
  return mailp;
}

/**
 * @brief   Releases a mail object into a mail pool.
 * @pre     The mail pool must be already been initialized.
 *
 * @param[in] mlp       pointer to a @p MailPool structure
 * @param[in] mailp     the pointer to the mail object to be released
 *
 * @api
 */
void mailDelete(MailPool *mlp, void *mailp) {

  chPoolFree(&mlp->pool, mailp);
  chSemSignal(&mlp->sem);
}

/** @} */


This is not the first time something like this has been requested, probably I will reintroduce it.

mail.zip
(2.29 KiB) Downloaded 424 times


Giovanni
Post Reply