From e65a5b08cda0ef8358b4a851feb74860cdc74625 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Mon, 2 Oct 2017 12:19:24 +0000 Subject: Added mailboxes to the factory. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10743 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/common/oslib/include/chfactory.h | 50 +++++++++++++++++++-- os/common/oslib/src/chfactory.c | 87 ++++++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 5 deletions(-) (limited to 'os') diff --git a/os/common/oslib/include/chfactory.h b/os/common/oslib/include/chfactory.h index 9dbcb7b57..ae975d3bd 100644 --- a/os/common/oslib/include/chfactory.h +++ b/os/common/oslib/include/chfactory.h @@ -68,6 +68,13 @@ #define CH_CFG_FACTORY_SEMAPHORES TRUE #endif +/** + * @brief Enables factory for mailboxes. + */ +#if !defined(CH_CFG_FACTORY_MAILBOXES) || defined(__DOXYGEN__) +#define CH_CFG_FACTORY_MAILBOXES TRUE +#endif + /*===========================================================================*/ /* Derived constants and error checks. */ /*===========================================================================*/ @@ -76,7 +83,8 @@ ((CH_CFG_FACTORY_SEMAPHORES == TRUE)) #define CH_FACTORY_REQUIRES_HEAP \ - ((CH_CFG_FACTORY_GENERIC_BUFFERS == TRUE)) + ((CH_CFG_FACTORY_GENERIC_BUFFERS == TRUE) || \ + (CH_CFG_FACTORY_MAILBOXES == TRUE)) #if (CH_CFG_FACTORY_MAX_NAMES_LENGHT < 0) || \ (CH_CFG_FACTORY_MAX_NAMES_LENGHT > 32) @@ -99,6 +107,10 @@ #error "CH_CFG_FACTORY_SEMAPHORES requires CH_CFG_USE_SEMAPHORES" #endif +#if (CH_CFG_FACTORY_MAILBOXES == TRUE) && (CH_CFG_USE_MAILBOXES == FALSE) +#error "CH_CFG_FACTORY_MAILBOXES requires CH_CFG_USE_MAILBOXES" +#endif + /*===========================================================================*/ /* Module data structures and types. */ /*===========================================================================*/ @@ -156,7 +168,7 @@ typedef struct ch_dyn_object { */ dyn_element_t element; /** - * @brief Physical buffer objects. + * @brief The buffer. * @note This requires C99. */ uint8_t buffer[]; @@ -173,12 +185,33 @@ typedef struct ch_dyn_semaphore { */ dyn_element_t element; /** - * @brief Physical semaphore. + * @brief The semaphore. */ semaphore_t sem; } dyn_semaphore_t; #endif +#if (CH_CFG_FACTORY_MAILBOXES == TRUE) || defined(__DOXIGEN__) +/** + * @brief Type of a dynamic buffer object. + */ +typedef struct ch_dyn_mailbox { + /** + * @brief List element of the dynamic buffer object. + */ + dyn_element_t element; + /** + * @brief The mailbox. + */ + mailbox_t mbx; + /** + * @brief Mailbox buffer. + * @note This requires C99. + */ + msg_t buffer[]; +} dyn_mailbox_t; +#endif + /** * @brief Type of the factory main object. */ @@ -207,6 +240,12 @@ typedef struct ch_objects_factory { */ memory_pool_t sem_pool; #endif /* CH_CFG_FACTORY_SEMAPHORES = TRUE */ +#if (CH_CFG_FACTORY_MAILBOXES == TRUE) || defined(__DOXIGEN__) + /** + * @brief List of the allocated buffer objects. + */ + dyn_list_t mbx_list; +#endif /* CH_CFG_FACTORY_MAILBOXES = TRUE */ } objects_factory_t; /*===========================================================================*/ @@ -241,6 +280,11 @@ extern "C" { dyn_semaphore_t *chFactoryFindSemaphore(const char *name); void chFactoryReleaseSemaphore(dyn_semaphore_t *dsp); #endif +#if (CH_CFG_FACTORY_MAILBOXES == TRUE) || defined(__DOXIGEN__) + dyn_mailbox_t *chFactoryCreateMailbox(const char *name, cnt_t n); + dyn_mailbox_t *chFactoryFindMailbox(const char *name); + void chFactoryReleaseMailbox(dyn_mailbox_t *dmp); +#endif #ifdef __cplusplus } #endif diff --git a/os/common/oslib/src/chfactory.c b/os/common/oslib/src/chfactory.c index 501f7e59d..e556489b1 100644 --- a/os/common/oslib/src/chfactory.c +++ b/os/common/oslib/src/chfactory.c @@ -246,6 +246,9 @@ void _factory_init(void) { sizeof (dyn_semaphore_t), chCoreAllocAlignedI); #endif +#if CH_CFG_FACTORY_MAILBOXES == TRUE + dyn_list_init(&ch_factory.mbx_list); +#endif } #if (CH_CFG_FACTORY_OBJECTS_REGISTRY == TRUE) || defined(__DOXIGEN__) @@ -334,7 +337,7 @@ void chFactoryReleaseObject(registered_object_t *rop){ * reference counter is initialized to one. * @post The dynamic buffer object is filled with zeros. * - * @param[in] name name to be assigned to the new dynamic buffer object + * @param[in] name name to be assigned to the new dynamic buffer object * @param[in] size payload size of the dynamic buffer object to be created * * @return The reference to the created dynamic buffer object. @@ -387,7 +390,7 @@ dyn_buffer_t *chFactoryFindBuffer(const char *name) { } /** - * @brief Releases a generic dynamic buffer object. + * @brief Releases a dynamic buffer object. * @details The reference counter of the dynamic buffer object is decreased * by one, if reaches zero then the dynamic buffer object memory * is freed. @@ -487,6 +490,86 @@ void chFactoryReleaseSemaphore(dyn_semaphore_t *dsp) { } #endif /* CH_CFG_FACTORY_SEMAPHORES = TRUE */ +#if (CH_CFG_FACTORY_MAILBOXES == TRUE) || defined(__DOXIGEN__) +/** + * @brief Creates a dynamic mailbox object. + * @post A reference to the dynamic mailbox object is returned and the + * reference counter is initialized to one. + * @post The dynamic buffer object is filled with zeros. + * + * @param[in] name name to be assigned to the new dynamic mailbox object + * @param[in] n mailbox buffer size as number of messages + * + * @return The reference to the created dynamic mailbox object. + * @retval NULL if the dynamic mailbox object cannot be allocated or + * a dynamic mailbox object with the same name exists. + * + * @api + */ +dyn_mailbox_t *chFactoryCreateMailbox(const char *name, cnt_t n) { + dyn_mailbox_t *dmp; + + chSysLock(); + + dmp = (dyn_mailbox_t *)dyn_create_object_heap(name, + &ch_factory.mbx_list, + sizeof (dyn_mailbox_t) + + ((size_t)n * sizeof (msg_t))); + if (dmp != NULL) { + /* Initializing mailbox object data.*/ + chMBObjectInit(&dmp->mbx, dmp->buffer, n); + } + + chSysUnlock(); + + return dmp; +} + +/** + * @brief Retrieves a dynamic mailbox object. + * @post A reference to the dynamic mailbox object is returned with the + * reference counter increased by one. + * + * @param[in] name name of the dynamic mailbox object + * + * @return The reference to the found dynamic mailbox object. + * @retval NULL if a dynamic mailbox object with the specified name + * does not exist. + * + * @api + */ +dyn_mailbox_t *chFactoryFindMailbox(const char *name) { + dyn_mailbox_t *dmp; + + chSysLock(); + + dmp = (dyn_mailbox_t *)dyn_find_object(name, &ch_factory.mbx_list); + + chSysUnlock(); + + return dmp; +} + +/** + * @brief Releases a dynamic mailbox object. + * @details The reference counter of the dynamic mailbox object is decreased + * by one, if reaches zero then the dynamic mailbox object memory + * is freed. + * + * @param[in] dbp dynamic mailbox object reference + * + * @api + */ +void chFactoryReleaseMailbox(dyn_mailbox_t *dmp) { + + chSysLock(); + + dyn_release_object_heap(&dmp->element, &ch_factory.mbx_list); + + chSysUnlock(); +} +#endif /* CH_CFG_FACTORY_MAILBOXES = TRUE */ + #endif /* CH_CFG_USE_FACTORY == TRUE */ /** @} */ -- cgit v1.2.3