amiro-os / core / src / aos_shell.c @ 9ff01927
History | View | Annotate | Download (46.2 KB)
| 1 | e545e620 | Thomas Schöpping | /*
|
|---|---|---|---|
| 2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
| 3 | Copyright (C) 2016..2018 Thomas Schöpping et al.
|
||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify
|
||
| 6 | it under the terms of the GNU General Public License as published by
|
||
| 7 | the Free Software Foundation, either version 3 of the License, or
|
||
| 8 | (at your option) any later version.
|
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful,
|
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 13 | GNU General Public License for more details.
|
||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License
|
||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 17 | */
|
||
| 18 | |||
| 19 | 53710ca3 | Marc Rothmann | /**
|
| 20 | * @file aos_shell.c
|
||
| 21 | * @brief Shell code.
|
||
| 22 | * @details Shell code as well as shell related channels and streams.
|
||
| 23 | *
|
||
| 24 | * @addtogroup aos_shell
|
||
| 25 | * @{
|
||
| 26 | */
|
||
| 27 | |||
| 28 | e545e620 | Thomas Schöpping | #include <aos_shell.h> |
| 29 | |||
| 30 | ba516b61 | Thomas Schöpping | #if (AMIROOS_CFG_SHELL_ENABLE == true) |
| 31 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
| 32 | #include <aos_time.h> |
||
| 33 | #include <aos_system.h> |
||
| 34 | #include <string.h> |
||
| 35 | ba516b61 | Thomas Schöpping | |
| 36 | /**
|
||
| 37 | * @brief Event mask to be set on OS related events.
|
||
| 38 | */
|
||
| 39 | #define AOS_SHELL_EVENTMASK_OS EVENT_MASK(0) |
||
| 40 | |||
| 41 | /**
|
||
| 42 | * @brief Event mask to be set on a input event.
|
||
| 43 | */
|
||
| 44 | #define AOS_SHELL_EVENTMASK_INPUT EVENT_MASK(1) |
||
| 45 | |||
| 46 | /**
|
||
| 47 | * @brief Implementation of the BaseAsynchronous write() method (inherited from BaseSequentialStream).
|
||
| 48 | */
|
||
| 49 | static size_t _channelwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 50 | {
|
||
| 51 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 52 | dd8738ea | Thomas Schöpping | return streamWrite(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
| 53 | ba516b61 | Thomas Schöpping | } else {
|
| 54 | return 0; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | /**
|
||
| 59 | * @brief Implementation of the BaseAsynchronous read() method (inherited from BaseSequentialStream).
|
||
| 60 | */
|
||
| 61 | static size_t _channelread(void *instance, uint8_t *bp, size_t n) |
||
| 62 | {
|
||
| 63 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 64 | return streamRead(((AosShellChannel*)instance)->asyncchannel, bp, n);
|
||
| 65 | } else {
|
||
| 66 | return 0; |
||
| 67 | } |
||
| 68 | ba516b61 | Thomas Schöpping | } |
| 69 | |||
| 70 | /**
|
||
| 71 | * @brief Implementation of the BaseAsynchronous put() method (inherited from BaseSequentialStream).
|
||
| 72 | */
|
||
| 73 | static msg_t _channelput(void *instance, uint8_t b) |
||
| 74 | {
|
||
| 75 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 76 | dd8738ea | Thomas Schöpping | return streamPut(((AosShellChannel*)instance)->asyncchannel, b);
|
| 77 | ba516b61 | Thomas Schöpping | } else {
|
| 78 | return MSG_RESET;
|
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /**
|
||
| 83 | * @brief Implementation of the BaseAsynchronous get() method (inherited from BaseSequentialStream).
|
||
| 84 | */
|
||
| 85 | static msg_t _channelget(void *instance) |
||
| 86 | {
|
||
| 87 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 88 | return streamGet(((AosShellChannel*)instance)->asyncchannel);
|
||
| 89 | } else {
|
||
| 90 | return MSG_RESET;
|
||
| 91 | } |
||
| 92 | ba516b61 | Thomas Schöpping | } |
| 93 | |||
| 94 | /**
|
||
| 95 | * @brief Implementation of the BaseAsynchronous putt() method.
|
||
| 96 | */
|
||
| 97 | 2c99037f | Marc Rothmann | static msg_t _channelputt(void *instance, uint8_t b, sysinterval_t time) |
| 98 | ba516b61 | Thomas Schöpping | {
|
| 99 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 100 | dd8738ea | Thomas Schöpping | return chnPutTimeout(((AosShellChannel*)instance)->asyncchannel, b, time);
|
| 101 | ba516b61 | Thomas Schöpping | } else {
|
| 102 | return MSG_RESET;
|
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /**
|
||
| 107 | * @brief Implementation of the BaseAsynchronous gett() method.
|
||
| 108 | */
|
||
| 109 | 2c99037f | Marc Rothmann | static msg_t _channelgett(void *instance, sysinterval_t time) |
| 110 | ba516b61 | Thomas Schöpping | {
|
| 111 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 112 | return chnGetTimeout(((AosShellChannel*)instance)->asyncchannel, time);
|
||
| 113 | } else {
|
||
| 114 | return MSG_RESET;
|
||
| 115 | } |
||
| 116 | ba516b61 | Thomas Schöpping | } |
| 117 | |||
| 118 | /**
|
||
| 119 | * @brief Implementation of the BaseAsynchronous writet() method.
|
||
| 120 | */
|
||
| 121 | 2c99037f | Marc Rothmann | static size_t _channelwritet(void *instance, const uint8_t *bp, size_t n, sysinterval_t time) |
| 122 | ba516b61 | Thomas Schöpping | {
|
| 123 | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_OUTPUT_ENABLED) {
|
||
| 124 | dd8738ea | Thomas Schöpping | return chnWriteTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
| 125 | ba516b61 | Thomas Schöpping | } else {
|
| 126 | return 0; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /**
|
||
| 131 | * @brief Implementation of the BaseAsynchronous readt() method.
|
||
| 132 | */
|
||
| 133 | 2c99037f | Marc Rothmann | static size_t _channelreadt(void *instance, uint8_t *bp, size_t n, sysinterval_t time) |
| 134 | ba516b61 | Thomas Schöpping | {
|
| 135 | dd8738ea | Thomas Schöpping | if (((AosShellChannel*)instance)->flags & AOS_SHELLCHANNEL_INPUT_ENABLED) {
|
| 136 | return chnReadTimeout(((AosShellChannel*)instance)->asyncchannel, bp, n, time);
|
||
| 137 | } else {
|
||
| 138 | return 0; |
||
| 139 | } |
||
| 140 | ba516b61 | Thomas Schöpping | } |
| 141 | |||
| 142 | 2c99037f | Marc Rothmann | /**
|
| 143 | * @brief Implementation of the BaseAsynchronousChannel ctl() method.
|
||
| 144 | */
|
||
| 145 | static msg_t _channelctl(void *instance, unsigned int operation, void *arg) { |
||
| 146 | (void) instance;
|
||
| 147 | |||
| 148 | switch (operation) {
|
||
| 149 | case CHN_CTL_NOP:
|
||
| 150 | osalDbgCheck(arg == NULL);
|
||
| 151 | break;
|
||
| 152 | case CHN_CTL_INVALID:
|
||
| 153 | osalDbgAssert(false, "invalid CTL operation"); |
||
| 154 | break;
|
||
| 155 | default:
|
||
| 156 | break;
|
||
| 157 | } |
||
| 158 | return MSG_OK;
|
||
| 159 | } |
||
| 160 | |||
| 161 | ba516b61 | Thomas Schöpping | static const struct AosShellChannelVMT _channelvmt = { |
| 162 | 0128be0f | Marc Rothmann | (size_t) 0,
|
| 163 | ba516b61 | Thomas Schöpping | _channelwrite, |
| 164 | _channelread, |
||
| 165 | _channelput, |
||
| 166 | _channelget, |
||
| 167 | _channelputt, |
||
| 168 | _channelgett, |
||
| 169 | _channelwritet, |
||
| 170 | _channelreadt, |
||
| 171 | 2c99037f | Marc Rothmann | _channelctl, |
| 172 | ba516b61 | Thomas Schöpping | }; |
| 173 | |||
| 174 | static size_t _streamwrite(void *instance, const uint8_t *bp, size_t n) |
||
| 175 | {
|
||
| 176 | aosDbgCheck(instance != NULL);
|
||
| 177 | |||
| 178 | // local variables
|
||
| 179 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 180 | size_t bytes; |
||
| 181 | size_t maxbytes = 0;
|
||
| 182 | |||
| 183 | // iterate through the list of channels
|
||
| 184 | while (channel != NULL) { |
||
| 185 | bytes = streamWrite(channel, bp, n); |
||
| 186 | maxbytes = (bytes > maxbytes) ? bytes : maxbytes; |
||
| 187 | channel = channel->next; |
||
| 188 | } |
||
| 189 | |||
| 190 | return maxbytes;
|
||
| 191 | } |
||
| 192 | |||
| 193 | static size_t _stremread(void *instance, uint8_t *bp, size_t n) |
||
| 194 | {
|
||
| 195 | (void)instance;
|
||
| 196 | (void)bp;
|
||
| 197 | (void)n;
|
||
| 198 | |||
| 199 | return 0; |
||
| 200 | } |
||
| 201 | |||
| 202 | static msg_t _streamput(void *instance, uint8_t b) |
||
| 203 | {
|
||
| 204 | aosDbgCheck(instance != NULL);
|
||
| 205 | |||
| 206 | // local variables
|
||
| 207 | AosShellChannel* channel = ((AosShellStream*)instance)->channel; |
||
| 208 | dd8738ea | Thomas Schöpping | msg_t ret = MSG_OK; |
| 209 | ba516b61 | Thomas Schöpping | |
| 210 | // iterate through the list of channels
|
||
| 211 | while (channel != NULL) { |
||
| 212 | dd8738ea | Thomas Schöpping | msg_t ret_ = streamPut(channel, b); |
| 213 | ret = (ret_ < ret) ? ret_ : ret; |
||
| 214 | ba516b61 | Thomas Schöpping | channel = channel->next; |
| 215 | } |
||
| 216 | |||
| 217 | dd8738ea | Thomas Schöpping | return ret;
|
| 218 | ba516b61 | Thomas Schöpping | } |
| 219 | |||
| 220 | static msg_t _streamget(void *instance) |
||
| 221 | {
|
||
| 222 | (void)instance;
|
||
| 223 | |||
| 224 | return 0; |
||
| 225 | } |
||
| 226 | |||
| 227 | static const struct AosShellStreamVMT _streamvmt = { |
||
| 228 | 0128be0f | Marc Rothmann | (size_t) 0,
|
| 229 | ba516b61 | Thomas Schöpping | _streamwrite, |
| 230 | _stremread, |
||
| 231 | _streamput, |
||
| 232 | _streamget, |
||
| 233 | }; |
||
| 234 | |||
| 235 | e545e620 |