Port to C027 (using AppShield and Ethernet)
Dependencies: C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
mbed-rtos/rtos/Mail.h@6:37b6d0d56190, 2014-08-20 (annotated)
- Committer:
- samdanbury
- Date:
- Wed Aug 20 12:45:14 2014 +0000
- Revision:
- 6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /* mbed Microcontroller Library |
samdanbury | 6:37b6d0d56190 | 2 | * Copyright (c) 2006-2012 ARM Limited |
samdanbury | 6:37b6d0d56190 | 3 | * |
samdanbury | 6:37b6d0d56190 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
samdanbury | 6:37b6d0d56190 | 5 | * of this software and associated documentation files (the "Software"), to deal |
samdanbury | 6:37b6d0d56190 | 6 | * in the Software without restriction, including without limitation the rights |
samdanbury | 6:37b6d0d56190 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
samdanbury | 6:37b6d0d56190 | 8 | * copies of the Software, and to permit persons to whom the Software is |
samdanbury | 6:37b6d0d56190 | 9 | * furnished to do so, subject to the following conditions: |
samdanbury | 6:37b6d0d56190 | 10 | * |
samdanbury | 6:37b6d0d56190 | 11 | * The above copyright notice and this permission notice shall be included in |
samdanbury | 6:37b6d0d56190 | 12 | * all copies or substantial portions of the Software. |
samdanbury | 6:37b6d0d56190 | 13 | * |
samdanbury | 6:37b6d0d56190 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
samdanbury | 6:37b6d0d56190 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
samdanbury | 6:37b6d0d56190 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
samdanbury | 6:37b6d0d56190 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
samdanbury | 6:37b6d0d56190 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samdanbury | 6:37b6d0d56190 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
samdanbury | 6:37b6d0d56190 | 20 | * SOFTWARE. |
samdanbury | 6:37b6d0d56190 | 21 | */ |
samdanbury | 6:37b6d0d56190 | 22 | #ifndef MAIL_H |
samdanbury | 6:37b6d0d56190 | 23 | #define MAIL_H |
samdanbury | 6:37b6d0d56190 | 24 | |
samdanbury | 6:37b6d0d56190 | 25 | #include <stdint.h> |
samdanbury | 6:37b6d0d56190 | 26 | #include <string.h> |
samdanbury | 6:37b6d0d56190 | 27 | |
samdanbury | 6:37b6d0d56190 | 28 | #include "cmsis_os.h" |
samdanbury | 6:37b6d0d56190 | 29 | |
samdanbury | 6:37b6d0d56190 | 30 | namespace rtos { |
samdanbury | 6:37b6d0d56190 | 31 | |
samdanbury | 6:37b6d0d56190 | 32 | /** The Mail class allow to control, send, receive, or wait for mail. |
samdanbury | 6:37b6d0d56190 | 33 | A mail is a memory block that is send to a thread or interrupt service routine. |
samdanbury | 6:37b6d0d56190 | 34 | @tparam T data type of a single message element. |
samdanbury | 6:37b6d0d56190 | 35 | @tparam queue_sz maximum number of messages in queue. |
samdanbury | 6:37b6d0d56190 | 36 | */ |
samdanbury | 6:37b6d0d56190 | 37 | template<typename T, uint32_t queue_sz> |
samdanbury | 6:37b6d0d56190 | 38 | class Mail { |
samdanbury | 6:37b6d0d56190 | 39 | public: |
samdanbury | 6:37b6d0d56190 | 40 | /** Create and Initialise Mail queue. */ |
samdanbury | 6:37b6d0d56190 | 41 | Mail() { |
samdanbury | 6:37b6d0d56190 | 42 | #ifdef CMSIS_OS_RTX |
samdanbury | 6:37b6d0d56190 | 43 | memset(_mail_q, 0, sizeof(_mail_q)); |
samdanbury | 6:37b6d0d56190 | 44 | _mail_p[0] = _mail_q; |
samdanbury | 6:37b6d0d56190 | 45 | |
samdanbury | 6:37b6d0d56190 | 46 | memset(_mail_m, 0, sizeof(_mail_m)); |
samdanbury | 6:37b6d0d56190 | 47 | _mail_p[1] = _mail_m; |
samdanbury | 6:37b6d0d56190 | 48 | |
samdanbury | 6:37b6d0d56190 | 49 | _mail_def.pool = _mail_p; |
samdanbury | 6:37b6d0d56190 | 50 | _mail_def.queue_sz = queue_sz; |
samdanbury | 6:37b6d0d56190 | 51 | _mail_def.item_sz = sizeof(T); |
samdanbury | 6:37b6d0d56190 | 52 | #endif |
samdanbury | 6:37b6d0d56190 | 53 | _mail_id = osMailCreate(&_mail_def, NULL); |
samdanbury | 6:37b6d0d56190 | 54 | } |
samdanbury | 6:37b6d0d56190 | 55 | |
samdanbury | 6:37b6d0d56190 | 56 | /** Allocate a memory block of type T |
samdanbury | 6:37b6d0d56190 | 57 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
samdanbury | 6:37b6d0d56190 | 58 | @return pointer to memory block that can be filled with mail or NULL in case error. |
samdanbury | 6:37b6d0d56190 | 59 | */ |
samdanbury | 6:37b6d0d56190 | 60 | T* alloc(uint32_t millisec=0) { |
samdanbury | 6:37b6d0d56190 | 61 | return (T*)osMailAlloc(_mail_id, millisec); |
samdanbury | 6:37b6d0d56190 | 62 | } |
samdanbury | 6:37b6d0d56190 | 63 | |
samdanbury | 6:37b6d0d56190 | 64 | /** Allocate a memory block of type T and set memory block to zero. |
samdanbury | 6:37b6d0d56190 | 65 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
samdanbury | 6:37b6d0d56190 | 66 | @return pointer to memory block that can be filled with mail or NULL in case error. |
samdanbury | 6:37b6d0d56190 | 67 | */ |
samdanbury | 6:37b6d0d56190 | 68 | T* calloc(uint32_t millisec=0) { |
samdanbury | 6:37b6d0d56190 | 69 | return (T*)osMailCAlloc(_mail_id, millisec); |
samdanbury | 6:37b6d0d56190 | 70 | } |
samdanbury | 6:37b6d0d56190 | 71 | |
samdanbury | 6:37b6d0d56190 | 72 | /** Put a mail in the queue. |
samdanbury | 6:37b6d0d56190 | 73 | @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. |
samdanbury | 6:37b6d0d56190 | 74 | @return status code that indicates the execution status of the function. |
samdanbury | 6:37b6d0d56190 | 75 | */ |
samdanbury | 6:37b6d0d56190 | 76 | osStatus put(T *mptr) { |
samdanbury | 6:37b6d0d56190 | 77 | return osMailPut(_mail_id, (void*)mptr); |
samdanbury | 6:37b6d0d56190 | 78 | } |
samdanbury | 6:37b6d0d56190 | 79 | |
samdanbury | 6:37b6d0d56190 | 80 | /** Get a mail from a queue. |
samdanbury | 6:37b6d0d56190 | 81 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
samdanbury | 6:37b6d0d56190 | 82 | @return event that contains mail information or error code. |
samdanbury | 6:37b6d0d56190 | 83 | */ |
samdanbury | 6:37b6d0d56190 | 84 | osEvent get(uint32_t millisec=osWaitForever) { |
samdanbury | 6:37b6d0d56190 | 85 | return osMailGet(_mail_id, millisec); |
samdanbury | 6:37b6d0d56190 | 86 | } |
samdanbury | 6:37b6d0d56190 | 87 | |
samdanbury | 6:37b6d0d56190 | 88 | /** Free a memory block from a mail. |
samdanbury | 6:37b6d0d56190 | 89 | @param mptr pointer to the memory block that was obtained with Mail::get. |
samdanbury | 6:37b6d0d56190 | 90 | @return status code that indicates the execution status of the function. |
samdanbury | 6:37b6d0d56190 | 91 | */ |
samdanbury | 6:37b6d0d56190 | 92 | osStatus free(T *mptr) { |
samdanbury | 6:37b6d0d56190 | 93 | return osMailFree(_mail_id, (void*)mptr); |
samdanbury | 6:37b6d0d56190 | 94 | } |
samdanbury | 6:37b6d0d56190 | 95 | |
samdanbury | 6:37b6d0d56190 | 96 | private: |
samdanbury | 6:37b6d0d56190 | 97 | osMailQId _mail_id; |
samdanbury | 6:37b6d0d56190 | 98 | osMailQDef_t _mail_def; |
samdanbury | 6:37b6d0d56190 | 99 | #ifdef CMSIS_OS_RTX |
samdanbury | 6:37b6d0d56190 | 100 | uint32_t _mail_q[4+(queue_sz)]; |
samdanbury | 6:37b6d0d56190 | 101 | uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)]; |
samdanbury | 6:37b6d0d56190 | 102 | void *_mail_p[2]; |
samdanbury | 6:37b6d0d56190 | 103 | #endif |
samdanbury | 6:37b6d0d56190 | 104 | }; |
samdanbury | 6:37b6d0d56190 | 105 | |
samdanbury | 6:37b6d0d56190 | 106 | } |
samdanbury | 6:37b6d0d56190 | 107 | |
samdanbury | 6:37b6d0d56190 | 108 | #endif |
samdanbury | 6:37b6d0d56190 | 109 |