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