Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ConfigurationHandler/ConfigurationHandler.cpp@0:65cfa4873284, 2016-09-01 (annotated)
- Committer:
- jmarkel44
- Date:
- Thu Sep 01 18:57:04 2016 +0000
- Revision:
- 0:65cfa4873284
- Child:
- 1:057d8fc6cb2f
Base version (threading + messaging based on mbed 5.0);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:65cfa4873284 | 1 | /****************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 2 | * |
jmarkel44 | 0:65cfa4873284 | 3 | * File: ConfigurationHandler.cpp |
jmarkel44 | 0:65cfa4873284 | 4 | * Desciption: source for the ICE Configuration Handler |
jmarkel44 | 0:65cfa4873284 | 5 | * |
jmarkel44 | 0:65cfa4873284 | 6 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 7 | #include <stdio.h> |
jmarkel44 | 0:65cfa4873284 | 8 | #include "global.h" |
jmarkel44 | 0:65cfa4873284 | 9 | #include "rtos.h" |
jmarkel44 | 0:65cfa4873284 | 10 | #include "ConfigurationHandler.h" |
jmarkel44 | 0:65cfa4873284 | 11 | #include <map> |
jmarkel44 | 0:65cfa4873284 | 12 | #include <string> |
jmarkel44 | 0:65cfa4873284 | 13 | |
jmarkel44 | 0:65cfa4873284 | 14 | using namespace std; |
jmarkel44 | 0:65cfa4873284 | 15 | |
jmarkel44 | 0:65cfa4873284 | 16 | extern osThreadId mainThreadId; |
jmarkel44 | 0:65cfa4873284 | 17 | |
jmarkel44 | 0:65cfa4873284 | 18 | // local functions |
jmarkel44 | 0:65cfa4873284 | 19 | static void loadPersistentControls( void ); |
jmarkel44 | 0:65cfa4873284 | 20 | static void createHandler ( const Message_t * ); |
jmarkel44 | 0:65cfa4873284 | 21 | static void modifyHandler ( const Message_t * ); |
jmarkel44 | 0:65cfa4873284 | 22 | static void destroyHandler( const Message_t * ); |
jmarkel44 | 0:65cfa4873284 | 23 | static void displayThreadTable( void ); |
jmarkel44 | 0:65cfa4873284 | 24 | |
jmarkel44 | 0:65cfa4873284 | 25 | // control spawners |
jmarkel44 | 0:65cfa4873284 | 26 | static Thread* spawnNewTimerControl( const char * ); |
jmarkel44 | 0:65cfa4873284 | 27 | static Thread* spawnNewPIDControl( const char * ); |
jmarkel44 | 0:65cfa4873284 | 28 | static Thread* spawnNewSetpointControl( const char * ); |
jmarkel44 | 0:65cfa4873284 | 29 | static Thread* spawnNewCompositeControl(const char *); |
jmarkel44 | 0:65cfa4873284 | 30 | static Thread* spawnNewManualControl(const char *); |
jmarkel44 | 0:65cfa4873284 | 31 | |
jmarkel44 | 0:65cfa4873284 | 32 | // control thread table |
jmarkel44 | 0:65cfa4873284 | 33 | typedef map<string, Thread *> StringThreadMap; |
jmarkel44 | 0:65cfa4873284 | 34 | static StringThreadMap threadTable; |
jmarkel44 | 0:65cfa4873284 | 35 | |
jmarkel44 | 0:65cfa4873284 | 36 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 37 | * Function: ConfigurationHandler |
jmarkel44 | 0:65cfa4873284 | 38 | * Description: entry point for the configuration handler |
jmarkel44 | 0:65cfa4873284 | 39 | * |
jmarkel44 | 0:65cfa4873284 | 40 | * @param (IN) args (user-defined arguments) |
jmarkel44 | 0:65cfa4873284 | 41 | * @return none |
jmarkel44 | 0:65cfa4873284 | 42 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 43 | void ConfigurationHandler(void const *args) |
jmarkel44 | 0:65cfa4873284 | 44 | { |
jmarkel44 | 0:65cfa4873284 | 45 | int rc; |
jmarkel44 | 0:65cfa4873284 | 46 | printf("\r%s is started...\n", __func__); |
jmarkel44 | 0:65cfa4873284 | 47 | |
jmarkel44 | 0:65cfa4873284 | 48 | //loadPersistentControls(); |
jmarkel44 | 0:65cfa4873284 | 49 | |
jmarkel44 | 0:65cfa4873284 | 50 | osSignalSet(mainThreadId, sig_continue); |
jmarkel44 | 0:65cfa4873284 | 51 | |
jmarkel44 | 0:65cfa4873284 | 52 | while (true) { |
jmarkel44 | 0:65cfa4873284 | 53 | // wait for a message from a data handler |
jmarkel44 | 0:65cfa4873284 | 54 | osEvent evt = MailBox.get(); |
jmarkel44 | 0:65cfa4873284 | 55 | if (evt.status == osEventMail) { |
jmarkel44 | 0:65cfa4873284 | 56 | Message_t *msg = (Message_t*)evt.value.p; |
jmarkel44 | 0:65cfa4873284 | 57 | switch (msg->action) { |
jmarkel44 | 0:65cfa4873284 | 58 | case ACTION_CREATE: |
jmarkel44 | 0:65cfa4873284 | 59 | createHandler(msg); |
jmarkel44 | 0:65cfa4873284 | 60 | break; |
jmarkel44 | 0:65cfa4873284 | 61 | case ACTION_MODIFY: |
jmarkel44 | 0:65cfa4873284 | 62 | modifyHandler(msg); |
jmarkel44 | 0:65cfa4873284 | 63 | break; |
jmarkel44 | 0:65cfa4873284 | 64 | case ACTION_DESTROY: |
jmarkel44 | 0:65cfa4873284 | 65 | destroyHandler(msg); |
jmarkel44 | 0:65cfa4873284 | 66 | break; |
jmarkel44 | 0:65cfa4873284 | 67 | default: |
jmarkel44 | 0:65cfa4873284 | 68 | printf("\r\n%s: ERROR\n", __FUNCTION__); |
jmarkel44 | 0:65cfa4873284 | 69 | rc = -1; |
jmarkel44 | 0:65cfa4873284 | 70 | break; |
jmarkel44 | 0:65cfa4873284 | 71 | } |
jmarkel44 | 0:65cfa4873284 | 72 | |
jmarkel44 | 0:65cfa4873284 | 73 | if ( rc != 0 ) { |
jmarkel44 | 0:65cfa4873284 | 74 | // do something |
jmarkel44 | 0:65cfa4873284 | 75 | } |
jmarkel44 | 0:65cfa4873284 | 76 | |
jmarkel44 | 0:65cfa4873284 | 77 | // free the allocated memory for the message |
jmarkel44 | 0:65cfa4873284 | 78 | MailBox.free(msg); |
jmarkel44 | 0:65cfa4873284 | 79 | } |
jmarkel44 | 0:65cfa4873284 | 80 | } |
jmarkel44 | 0:65cfa4873284 | 81 | } |
jmarkel44 | 0:65cfa4873284 | 82 | |
jmarkel44 | 0:65cfa4873284 | 83 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 84 | * Function: ConfigurationHandler_DisplayThreads |
jmarkel44 | 0:65cfa4873284 | 85 | * Description: Display list of active threads |
jmarkel44 | 0:65cfa4873284 | 86 | * |
jmarkel44 | 0:65cfa4873284 | 87 | * @param none |
jmarkel44 | 0:65cfa4873284 | 88 | * @return none |
jmarkel44 | 0:65cfa4873284 | 89 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 90 | void ConfigurationHandler_DisplayThreads(void) |
jmarkel44 | 0:65cfa4873284 | 91 | { |
jmarkel44 | 0:65cfa4873284 | 92 | displayThreadTable(); |
jmarkel44 | 0:65cfa4873284 | 93 | } |
jmarkel44 | 0:65cfa4873284 | 94 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 95 | * Function: loadPersistentControls |
jmarkel44 | 0:65cfa4873284 | 96 | * Description: Create a new control thread |
jmarkel44 | 0:65cfa4873284 | 97 | * |
jmarkel44 | 0:65cfa4873284 | 98 | * @param (IN) msg (parsed message data from the cloud) |
jmarkel44 | 0:65cfa4873284 | 99 | * @return none |
jmarkel44 | 0:65cfa4873284 | 100 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 101 | static void loadPersistentControls(void) |
jmarkel44 | 0:65cfa4873284 | 102 | { |
jmarkel44 | 0:65cfa4873284 | 103 | printf("\r%s no-op\n", __func__); |
jmarkel44 | 0:65cfa4873284 | 104 | } |
jmarkel44 | 0:65cfa4873284 | 105 | |
jmarkel44 | 0:65cfa4873284 | 106 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 107 | * Function: createHandler |
jmarkel44 | 0:65cfa4873284 | 108 | * Description: Create a new control thread |
jmarkel44 | 0:65cfa4873284 | 109 | * |
jmarkel44 | 0:65cfa4873284 | 110 | * @param (IN) msg (parsed message data from the cloud) |
jmarkel44 | 0:65cfa4873284 | 111 | * @return none |
jmarkel44 | 0:65cfa4873284 | 112 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 113 | static void createHandler(const Message_t *msg) |
jmarkel44 | 0:65cfa4873284 | 114 | { |
jmarkel44 | 0:65cfa4873284 | 115 | printf("\r\n%s: invoked with %s [%u]\n", |
jmarkel44 | 0:65cfa4873284 | 116 | __func__, msg->controlFile, msg->control); |
jmarkel44 | 0:65cfa4873284 | 117 | |
jmarkel44 | 0:65cfa4873284 | 118 | switch (msg->control) { |
jmarkel44 | 0:65cfa4873284 | 119 | case CONTROL_TIMER: { |
jmarkel44 | 0:65cfa4873284 | 120 | Thread *t = spawnNewTimerControl(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 121 | threadTable[msg->controlFile] = t; |
jmarkel44 | 0:65cfa4873284 | 122 | break; |
jmarkel44 | 0:65cfa4873284 | 123 | } |
jmarkel44 | 0:65cfa4873284 | 124 | case CONTROL_PID: { |
jmarkel44 | 0:65cfa4873284 | 125 | Thread *t = spawnNewPIDControl(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 126 | threadTable[msg->controlFile] = t; |
jmarkel44 | 0:65cfa4873284 | 127 | break; |
jmarkel44 | 0:65cfa4873284 | 128 | } |
jmarkel44 | 0:65cfa4873284 | 129 | case CONTROL_SETPOINT: { |
jmarkel44 | 0:65cfa4873284 | 130 | Thread *t = spawnNewSetpointControl(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 131 | threadTable[msg->controlFile] = t; |
jmarkel44 | 0:65cfa4873284 | 132 | break; |
jmarkel44 | 0:65cfa4873284 | 133 | } |
jmarkel44 | 0:65cfa4873284 | 134 | case CONTROL_COMPOSITE: { |
jmarkel44 | 0:65cfa4873284 | 135 | Thread *t = spawnNewCompositeControl(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 136 | threadTable[msg->controlFile] = t; |
jmarkel44 | 0:65cfa4873284 | 137 | break; |
jmarkel44 | 0:65cfa4873284 | 138 | } |
jmarkel44 | 0:65cfa4873284 | 139 | case CONTROL_MANUAL: { |
jmarkel44 | 0:65cfa4873284 | 140 | Thread *t = spawnNewManualControl(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 141 | threadTable[msg->controlFile] = t; |
jmarkel44 | 0:65cfa4873284 | 142 | break; |
jmarkel44 | 0:65cfa4873284 | 143 | } |
jmarkel44 | 0:65cfa4873284 | 144 | default: |
jmarkel44 | 0:65cfa4873284 | 145 | printf("%s: unkown control type (%u)", __func__, msg->control); |
jmarkel44 | 0:65cfa4873284 | 146 | break; |
jmarkel44 | 0:65cfa4873284 | 147 | } |
jmarkel44 | 0:65cfa4873284 | 148 | } |
jmarkel44 | 0:65cfa4873284 | 149 | |
jmarkel44 | 0:65cfa4873284 | 150 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 151 | * Function: modifyHandler |
jmarkel44 | 0:65cfa4873284 | 152 | * Description: modify an existing control thread |
jmarkel44 | 0:65cfa4873284 | 153 | * |
jmarkel44 | 0:65cfa4873284 | 154 | * @param (IN) args (user-defined arguments) |
jmarkel44 | 0:65cfa4873284 | 155 | * @return none |
jmarkel44 | 0:65cfa4873284 | 156 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 157 | static void modifyHandler(const Message_t *msg) |
jmarkel44 | 0:65cfa4873284 | 158 | { |
jmarkel44 | 0:65cfa4873284 | 159 | // STUBBED |
jmarkel44 | 0:65cfa4873284 | 160 | printf("\r\n%s: invoked with %s\n", __FUNCTION__, msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 161 | } |
jmarkel44 | 0:65cfa4873284 | 162 | |
jmarkel44 | 0:65cfa4873284 | 163 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 164 | * Function: destroyHandler |
jmarkel44 | 0:65cfa4873284 | 165 | * Description: detroy an existing control thread |
jmarkel44 | 0:65cfa4873284 | 166 | * |
jmarkel44 | 0:65cfa4873284 | 167 | * @param (IN) args (user-defined arguments) |
jmarkel44 | 0:65cfa4873284 | 168 | * @return none |
jmarkel44 | 0:65cfa4873284 | 169 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 170 | static void destroyHandler(const Message_t *msg) |
jmarkel44 | 0:65cfa4873284 | 171 | { |
jmarkel44 | 0:65cfa4873284 | 172 | printf("\r\n%s: invoked with %s\n", __FUNCTION__, msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 173 | |
jmarkel44 | 0:65cfa4873284 | 174 | StringThreadMap::iterator pos; |
jmarkel44 | 0:65cfa4873284 | 175 | |
jmarkel44 | 0:65cfa4873284 | 176 | // find the control thread, terminate it, then delete the entry |
jmarkel44 | 0:65cfa4873284 | 177 | // from the thread table. |
jmarkel44 | 0:65cfa4873284 | 178 | pos = threadTable.find(msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 179 | if ( pos != threadTable.end() ) { |
jmarkel44 | 0:65cfa4873284 | 180 | Thread *t = pos->second; |
jmarkel44 | 0:65cfa4873284 | 181 | t->terminate(); |
jmarkel44 | 0:65cfa4873284 | 182 | // perform cleanup |
jmarkel44 | 0:65cfa4873284 | 183 | delete t; |
jmarkel44 | 0:65cfa4873284 | 184 | // remove the entry from the map |
jmarkel44 | 0:65cfa4873284 | 185 | threadTable.erase(pos); |
jmarkel44 | 0:65cfa4873284 | 186 | } |
jmarkel44 | 0:65cfa4873284 | 187 | } |
jmarkel44 | 0:65cfa4873284 | 188 | |
jmarkel44 | 0:65cfa4873284 | 189 | static Thread* spawnNewTimerControl( const char *controlFile ) |
jmarkel44 | 0:65cfa4873284 | 190 | { |
jmarkel44 | 0:65cfa4873284 | 191 | Thread *t = new Thread(TimerControl, (void *)controlFile); |
jmarkel44 | 0:65cfa4873284 | 192 | return t; |
jmarkel44 | 0:65cfa4873284 | 193 | } |
jmarkel44 | 0:65cfa4873284 | 194 | static Thread* spawnNewPIDControl( const char *controlFile ) |
jmarkel44 | 0:65cfa4873284 | 195 | { |
jmarkel44 | 0:65cfa4873284 | 196 | Thread *t = new Thread(PIDControl, (void *)controlFile); |
jmarkel44 | 0:65cfa4873284 | 197 | return t; |
jmarkel44 | 0:65cfa4873284 | 198 | } |
jmarkel44 | 0:65cfa4873284 | 199 | |
jmarkel44 | 0:65cfa4873284 | 200 | static Thread* spawnNewSetpointControl( const char *controlFile ) |
jmarkel44 | 0:65cfa4873284 | 201 | { |
jmarkel44 | 0:65cfa4873284 | 202 | Thread *t = new Thread(SetpointControl, (void *)controlFile); |
jmarkel44 | 0:65cfa4873284 | 203 | return t; |
jmarkel44 | 0:65cfa4873284 | 204 | } |
jmarkel44 | 0:65cfa4873284 | 205 | |
jmarkel44 | 0:65cfa4873284 | 206 | static Thread* spawnNewCompositeControl(const char *controlFile) |
jmarkel44 | 0:65cfa4873284 | 207 | { |
jmarkel44 | 0:65cfa4873284 | 208 | Thread *t = new Thread(CompositeControl, (void *)controlFile); |
jmarkel44 | 0:65cfa4873284 | 209 | return t; |
jmarkel44 | 0:65cfa4873284 | 210 | } |
jmarkel44 | 0:65cfa4873284 | 211 | static Thread* spawnNewManualControl(const char *controlFile) |
jmarkel44 | 0:65cfa4873284 | 212 | { |
jmarkel44 | 0:65cfa4873284 | 213 | Thread *t = new Thread(ManualControl, (void *)controlFile); |
jmarkel44 | 0:65cfa4873284 | 214 | return t; |
jmarkel44 | 0:65cfa4873284 | 215 | } |
jmarkel44 | 0:65cfa4873284 | 216 | |
jmarkel44 | 0:65cfa4873284 | 217 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 218 | * Function: displayThreadTable |
jmarkel44 | 0:65cfa4873284 | 219 | * Description: display the elements in the thread table |
jmarkel44 | 0:65cfa4873284 | 220 | * |
jmarkel44 | 0:65cfa4873284 | 221 | * @param (IN) controlFile: file which to control fome |
jmarkel44 | 0:65cfa4873284 | 222 | * @return osThreadId |
jmarkel44 | 0:65cfa4873284 | 223 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 224 | static void displayThreadTable(void) |
jmarkel44 | 0:65cfa4873284 | 225 | { |
jmarkel44 | 0:65cfa4873284 | 226 | StringThreadMap::iterator pos; |
jmarkel44 | 0:65cfa4873284 | 227 | |
jmarkel44 | 0:65cfa4873284 | 228 | // this must exactly match the OS thread states in Thread.h |
jmarkel44 | 0:65cfa4873284 | 229 | const char *stateMapper[] = { "Inactive", |
jmarkel44 | 0:65cfa4873284 | 230 | "Ready", |
jmarkel44 | 0:65cfa4873284 | 231 | "Running", |
jmarkel44 | 0:65cfa4873284 | 232 | "WaitingDelay", |
jmarkel44 | 0:65cfa4873284 | 233 | "WaitingInterval", |
jmarkel44 | 0:65cfa4873284 | 234 | "WaitingOr", |
jmarkel44 | 0:65cfa4873284 | 235 | "WaitingAnd", |
jmarkel44 | 0:65cfa4873284 | 236 | "WaitingSemaphore", |
jmarkel44 | 0:65cfa4873284 | 237 | "WaitingMailbox", |
jmarkel44 | 0:65cfa4873284 | 238 | "WaitingMutex", |
jmarkel44 | 0:65cfa4873284 | 239 | "Deleted" |
jmarkel44 | 0:65cfa4873284 | 240 | }; |
jmarkel44 | 0:65cfa4873284 | 241 | |
jmarkel44 | 0:65cfa4873284 | 242 | if ( threadTable.size() == 0 ) { |
jmarkel44 | 0:65cfa4873284 | 243 | printf("\r\nThere are currently no active control threads\r\n"); |
jmarkel44 | 0:65cfa4873284 | 244 | return; |
jmarkel44 | 0:65cfa4873284 | 245 | } |
jmarkel44 | 0:65cfa4873284 | 246 | |
jmarkel44 | 0:65cfa4873284 | 247 | printf("\rControl Thread Table\n"); |
jmarkel44 | 0:65cfa4873284 | 248 | for ( pos = threadTable.begin(); pos != threadTable.end(); ++pos ) { |
jmarkel44 | 0:65cfa4873284 | 249 | Thread *t = (Thread *)pos->second; |
jmarkel44 | 0:65cfa4873284 | 250 | printf("\r[%32s]->\tpri=%u\tstate=%s\tstack_size=%u\tfree=%u\tused=%u\tmax=%u\r\n", |
jmarkel44 | 0:65cfa4873284 | 251 | pos->first.c_str(), |
jmarkel44 | 0:65cfa4873284 | 252 | t->get_priority(), |
jmarkel44 | 0:65cfa4873284 | 253 | stateMapper[t->get_state()], |
jmarkel44 | 0:65cfa4873284 | 254 | t->stack_size(), |
jmarkel44 | 0:65cfa4873284 | 255 | t->free_stack(), |
jmarkel44 | 0:65cfa4873284 | 256 | t->used_stack(), |
jmarkel44 | 0:65cfa4873284 | 257 | t->max_stack()); |
jmarkel44 | 0:65cfa4873284 | 258 | } |
jmarkel44 | 0:65cfa4873284 | 259 | } |
jmarkel44 | 0:65cfa4873284 | 260 |