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/OutputTask/OutputTask.cpp@71:34856d21f2bf, 2016-09-16 (annotated)
- Committer:
- jmarkel44
- Date:
- Fri Sep 16 20:54:07 2016 +0000
- Revision:
- 71:34856d21f2bf
- Parent:
- 70:7427f4959201
- Child:
- 72:3754b352f156
chunking in the output task;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 70:7427f4959201 | 1 | /****************************************************************************** |
jmarkel44 | 70:7427f4959201 | 2 | * |
jmarkel44 | 70:7427f4959201 | 3 | * File: OutputTask.cpp |
jmarkel44 | 70:7427f4959201 | 4 | * Desciption: source for the ICE Output task |
jmarkel44 | 70:7427f4959201 | 5 | * |
jmarkel44 | 70:7427f4959201 | 6 | *****************************************************************************/ |
jmarkel44 | 67:49f266601d83 | 7 | #include "OutputTask.h" |
jmarkel44 | 48:1c7861d80d16 | 8 | #include "global.h" |
jmarkel44 | 66:db1425574b58 | 9 | #include "MbedJSONValue.h" |
jmarkel44 | 70:7427f4959201 | 10 | #include <vector> |
jmarkel44 | 48:1c7861d80d16 | 11 | |
jmarkel44 | 70:7427f4959201 | 12 | // locals |
jmarkel44 | 67:49f266601d83 | 13 | static int createOutput(const char *controlFile); |
jmarkel44 | 66:db1425574b58 | 14 | static void loadPersistentOutputs(void); |
jmarkel44 | 63:0ded43237b22 | 15 | |
jmarkel44 | 71:34856d21f2bf | 16 | static int enableOutputReq (const char* id, unsigned int pri, const char* output); |
jmarkel44 | 71:34856d21f2bf | 17 | static int disableOutputReq(const char *id, unsigned int pri, const char *output); |
jmarkel44 | 71:34856d21f2bf | 18 | //static int unregisterControl(const char *id, unsigned int pri, const char *output); |
jmarkel44 | 71:34856d21f2bf | 19 | |
jmarkel44 | 71:34856d21f2bf | 20 | typedef enum { |
jmarkel44 | 71:34856d21f2bf | 21 | CONTROL_OFF = 0, |
jmarkel44 | 71:34856d21f2bf | 22 | CONTROL_ON = 1 |
jmarkel44 | 71:34856d21f2bf | 23 | } ControlState; |
jmarkel44 | 71:34856d21f2bf | 24 | |
jmarkel44 | 70:7427f4959201 | 25 | class Control |
jmarkel44 | 70:7427f4959201 | 26 | { |
jmarkel44 | 70:7427f4959201 | 27 | private: |
jmarkel44 | 70:7427f4959201 | 28 | string id; |
jmarkel44 | 70:7427f4959201 | 29 | unsigned int priority; |
jmarkel44 | 71:34856d21f2bf | 30 | ControlState state; |
jmarkel44 | 70:7427f4959201 | 31 | public: |
jmarkel44 | 71:34856d21f2bf | 32 | Control(string id, unsigned int priority, ControlState state) : |
jmarkel44 | 70:7427f4959201 | 33 | id(id), priority(priority), state(state) {} |
jmarkel44 | 71:34856d21f2bf | 34 | void display() { |
jmarkel44 | 71:34856d21f2bf | 35 | printf("[%s | %s | %u]", |
jmarkel44 | 71:34856d21f2bf | 36 | id.c_str(), (state == CONTROL_ON) ? "ON" : "OFF", priority); |
jmarkel44 | 71:34856d21f2bf | 37 | } |
jmarkel44 | 71:34856d21f2bf | 38 | //void setId(string id) id(id) { } |
jmarkel44 | 71:34856d21f2bf | 39 | string getId() { |
jmarkel44 | 71:34856d21f2bf | 40 | return id; |
jmarkel44 | 71:34856d21f2bf | 41 | } |
jmarkel44 | 71:34856d21f2bf | 42 | void setState(ControlState _state) { |
jmarkel44 | 71:34856d21f2bf | 43 | state = _state; |
jmarkel44 | 71:34856d21f2bf | 44 | } |
jmarkel44 | 70:7427f4959201 | 45 | }; |
jmarkel44 | 70:7427f4959201 | 46 | |
jmarkel44 | 70:7427f4959201 | 47 | |
jmarkel44 | 71:34856d21f2bf | 48 | typedef std::map<string, vector<Control> > StringOutputVector_t; |
jmarkel44 | 66:db1425574b58 | 49 | |
jmarkel44 | 66:db1425574b58 | 50 | StringOutputVector_t outputMap; |
jmarkel44 | 63:0ded43237b22 | 51 | |
jmarkel44 | 70:7427f4959201 | 52 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 53 | * Function: OutputTask |
jmarkel44 | 70:7427f4959201 | 54 | * Description: Main entry point for the Output Task |
jmarkel44 | 70:7427f4959201 | 55 | * |
jmarkel44 | 70:7427f4959201 | 56 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 57 | * @return none |
jmarkel44 | 70:7427f4959201 | 58 | *****************************************************************************/ |
jmarkel44 | 48:1c7861d80d16 | 59 | void OutputTask(void const *args) |
jmarkel44 | 48:1c7861d80d16 | 60 | { |
jmarkel44 | 51:66b820f203a5 | 61 | UNUSED(args); |
jmarkel44 | 70:7427f4959201 | 62 | |
jmarkel44 | 67:49f266601d83 | 63 | printf("\r%s has started...\n", __func__); |
jmarkel44 | 63:0ded43237b22 | 64 | |
jmarkel44 | 66:db1425574b58 | 65 | loadPersistentOutputs(); |
jmarkel44 | 66:db1425574b58 | 66 | |
jmarkel44 | 48:1c7861d80d16 | 67 | while (true) { |
jmarkel44 | 51:66b820f203a5 | 68 | // wait for an event |
jmarkel44 | 56:225786c56315 | 69 | osEvent evt = OutputMasterMailBox.get(); |
jmarkel44 | 51:66b820f203a5 | 70 | if (evt.status == osEventMail) { |
jmarkel44 | 63:0ded43237b22 | 71 | OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p; |
jmarkel44 | 56:225786c56315 | 72 | printf("\r%s received message from someone...\n", __func__); |
jmarkel44 | 71:34856d21f2bf | 73 | printf("\rmsg->relay = %s\n", msg->output); |
jmarkel44 | 56:225786c56315 | 74 | printf("\rmsg->state = %s\n", msg->state == ON ? "ON" : "OFF"); |
jmarkel44 | 56:225786c56315 | 75 | printf("\rmsg->priority = %u\n", msg->priority); |
jmarkel44 | 63:0ded43237b22 | 76 | |
jmarkel44 | 63:0ded43237b22 | 77 | switch ( msg->action ) { |
jmarkel44 | 63:0ded43237b22 | 78 | case ACTION_NEW: |
jmarkel44 | 63:0ded43237b22 | 79 | // read the file and and create an output entry |
jmarkel44 | 66:db1425574b58 | 80 | (void) createOutput(msg->controlFile); |
jmarkel44 | 63:0ded43237b22 | 81 | break; |
jmarkel44 | 71:34856d21f2bf | 82 | case ACTION_CONTROL_ON: |
jmarkel44 | 71:34856d21f2bf | 83 | printf("\r%s is requesting ON control of %s\n", |
jmarkel44 | 71:34856d21f2bf | 84 | msg->id, msg->output); |
jmarkel44 | 71:34856d21f2bf | 85 | (void) enableOutputReq(msg->id, msg->priority, msg->output); |
jmarkel44 | 71:34856d21f2bf | 86 | break; |
jmarkel44 | 71:34856d21f2bf | 87 | case ACTION_CONTROL_OFF: |
jmarkel44 | 71:34856d21f2bf | 88 | printf("\r%s is requesting OFF control of %s\n", |
jmarkel44 | 71:34856d21f2bf | 89 | msg->id, msg->output); |
jmarkel44 | 71:34856d21f2bf | 90 | (void) disableOutputReq(msg->id, msg->priority, msg->output); |
jmarkel44 | 71:34856d21f2bf | 91 | break; |
jmarkel44 | 63:0ded43237b22 | 92 | default: |
jmarkel44 | 63:0ded43237b22 | 93 | break; |
jmarkel44 | 63:0ded43237b22 | 94 | } |
jmarkel44 | 63:0ded43237b22 | 95 | |
jmarkel44 | 56:225786c56315 | 96 | // free the message |
jmarkel44 | 56:225786c56315 | 97 | OutputMasterMailBox.free(msg); |
jmarkel44 | 51:66b820f203a5 | 98 | } |
jmarkel44 | 48:1c7861d80d16 | 99 | } |
jmarkel44 | 66:db1425574b58 | 100 | } |
jmarkel44 | 66:db1425574b58 | 101 | |
jmarkel44 | 70:7427f4959201 | 102 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 103 | * Function: DisplayOutputs |
jmarkel44 | 70:7427f4959201 | 104 | * Description: Display a list of outputs and its controls |
jmarkel44 | 70:7427f4959201 | 105 | * |
jmarkel44 | 70:7427f4959201 | 106 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 107 | * @return none |
jmarkel44 | 70:7427f4959201 | 108 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 109 | void DisplayOutputs(void) |
jmarkel44 | 66:db1425574b58 | 110 | { |
jmarkel44 | 66:db1425574b58 | 111 | StringOutputVector_t::iterator pos; |
jmarkel44 | 66:db1425574b58 | 112 | |
jmarkel44 | 66:db1425574b58 | 113 | for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) { |
jmarkel44 | 71:34856d21f2bf | 114 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 115 | printf("\r [%s]->[no controls]\n", pos->first.c_str()); |
jmarkel44 | 71:34856d21f2bf | 116 | } else { |
jmarkel44 | 71:34856d21f2bf | 117 | printf("\r [%s]->", pos->first.c_str()); |
jmarkel44 | 71:34856d21f2bf | 118 | vector<Control>::iterator i; |
jmarkel44 | 71:34856d21f2bf | 119 | for ( i = pos->second.begin(); i != pos->second.end(); ++i ) { |
jmarkel44 | 71:34856d21f2bf | 120 | i->display(); |
jmarkel44 | 71:34856d21f2bf | 121 | } |
jmarkel44 | 71:34856d21f2bf | 122 | printf("\n"); |
jmarkel44 | 71:34856d21f2bf | 123 | } |
jmarkel44 | 66:db1425574b58 | 124 | } |
jmarkel44 | 66:db1425574b58 | 125 | printf("\r\n"); |
jmarkel44 | 66:db1425574b58 | 126 | } |
jmarkel44 | 66:db1425574b58 | 127 | |
jmarkel44 | 71:34856d21f2bf | 128 | |
jmarkel44 | 71:34856d21f2bf | 129 | /***************************************************************************** |
jmarkel44 | 71:34856d21f2bf | 130 | * Function: DisplayOutputs |
jmarkel44 | 71:34856d21f2bf | 131 | * Description: Display a list of outputs and its controls |
jmarkel44 | 71:34856d21f2bf | 132 | * |
jmarkel44 | 71:34856d21f2bf | 133 | * @param args -> not used |
jmarkel44 | 71:34856d21f2bf | 134 | * @return none |
jmarkel44 | 71:34856d21f2bf | 135 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 136 | static int createOutput(const char *controlFile) |
jmarkel44 | 66:db1425574b58 | 137 | { |
jmarkel44 | 66:db1425574b58 | 138 | char dataBuf[1024]; |
jmarkel44 | 67:49f266601d83 | 139 | int status = GLOBAL_mdot->readUserFile(controlFile, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 67:49f266601d83 | 140 | if ( status != true ) { |
jmarkel44 | 66:db1425574b58 | 141 | logError("%s failed to read %s", __func__, controlFile); |
jmarkel44 | 66:db1425574b58 | 142 | return -1; |
jmarkel44 | 66:db1425574b58 | 143 | } |
jmarkel44 | 66:db1425574b58 | 144 | |
jmarkel44 | 66:db1425574b58 | 145 | MbedJSONValue json_value; |
jmarkel44 | 66:db1425574b58 | 146 | parse(json_value, dataBuf); |
jmarkel44 | 66:db1425574b58 | 147 | |
jmarkel44 | 66:db1425574b58 | 148 | // extract the relay information |
jmarkel44 | 66:db1425574b58 | 149 | string id = json_value["id"].get<string>(); |
jmarkel44 | 66:db1425574b58 | 150 | |
jmarkel44 | 71:34856d21f2bf | 151 | // maps doesn't allow duplicates, and the vector is empty for now |
jmarkel44 | 71:34856d21f2bf | 152 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 153 | outputMap[id] = v; |
jmarkel44 | 71:34856d21f2bf | 154 | |
jmarkel44 | 71:34856d21f2bf | 155 | return 0; |
jmarkel44 | 71:34856d21f2bf | 156 | } |
jmarkel44 | 71:34856d21f2bf | 157 | |
jmarkel44 | 71:34856d21f2bf | 158 | |
jmarkel44 | 71:34856d21f2bf | 159 | /***************************************************************************** |
jmarkel44 | 71:34856d21f2bf | 160 | * Function: DisplayOutputs |
jmarkel44 | 71:34856d21f2bf | 161 | * Description: Display a list of outputs and its controls |
jmarkel44 | 71:34856d21f2bf | 162 | * |
jmarkel44 | 71:34856d21f2bf | 163 | * @param args -> not used |
jmarkel44 | 71:34856d21f2bf | 164 | * @return none |
jmarkel44 | 71:34856d21f2bf | 165 | *****************************************************************************/ |
jmarkel44 | 71:34856d21f2bf | 166 | static int enableOutputReq(const char *id, unsigned int priority, const char *output) |
jmarkel44 | 71:34856d21f2bf | 167 | { |
jmarkel44 | 71:34856d21f2bf | 168 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 169 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 170 | |
jmarkel44 | 71:34856d21f2bf | 171 | pos = outputMap.find(output); |
jmarkel44 | 71:34856d21f2bf | 172 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 173 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 71:34856d21f2bf | 174 | __func__, output); |
jmarkel44 | 71:34856d21f2bf | 175 | return -1; |
jmarkel44 | 71:34856d21f2bf | 176 | } |
jmarkel44 | 71:34856d21f2bf | 177 | |
jmarkel44 | 71:34856d21f2bf | 178 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 179 | // this is a new request |
jmarkel44 | 71:34856d21f2bf | 180 | string cid(id); |
jmarkel44 | 71:34856d21f2bf | 181 | Control c(cid, priority, CONTROL_ON); |
jmarkel44 | 71:34856d21f2bf | 182 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 183 | } else { |
jmarkel44 | 71:34856d21f2bf | 184 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 185 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 186 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 71:34856d21f2bf | 187 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 188 | v->setState(CONTROL_ON); |
jmarkel44 | 71:34856d21f2bf | 189 | } |
jmarkel44 | 71:34856d21f2bf | 190 | } |
jmarkel44 | 71:34856d21f2bf | 191 | } |
jmarkel44 | 71:34856d21f2bf | 192 | |
jmarkel44 | 71:34856d21f2bf | 193 | return 0; |
jmarkel44 | 71:34856d21f2bf | 194 | } |
jmarkel44 | 71:34856d21f2bf | 195 | |
jmarkel44 | 71:34856d21f2bf | 196 | |
jmarkel44 | 71:34856d21f2bf | 197 | /***************************************************************************** |
jmarkel44 | 71:34856d21f2bf | 198 | * Function: DisplayOutputs |
jmarkel44 | 71:34856d21f2bf | 199 | * Description: Display a list of outputs and its controls |
jmarkel44 | 71:34856d21f2bf | 200 | * |
jmarkel44 | 71:34856d21f2bf | 201 | * @param args -> not used |
jmarkel44 | 71:34856d21f2bf | 202 | * @return none |
jmarkel44 | 71:34856d21f2bf | 203 | *****************************************************************************/ |
jmarkel44 | 71:34856d21f2bf | 204 | static int disableOutputReq(const char *id, unsigned int priority, const char *output) |
jmarkel44 | 71:34856d21f2bf | 205 | { |
jmarkel44 | 71:34856d21f2bf | 206 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 207 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 208 | |
jmarkel44 | 71:34856d21f2bf | 209 | pos = outputMap.find(output); |
jmarkel44 | 71:34856d21f2bf | 210 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 211 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 71:34856d21f2bf | 212 | __func__, output); |
jmarkel44 | 71:34856d21f2bf | 213 | return -1; |
jmarkel44 | 71:34856d21f2bf | 214 | } |
jmarkel44 | 71:34856d21f2bf | 215 | |
jmarkel44 | 71:34856d21f2bf | 216 | // is the same id requesting? |
jmarkel44 | 71:34856d21f2bf | 217 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 218 | string cid(id); |
jmarkel44 | 71:34856d21f2bf | 219 | Control c(cid, priority, CONTROL_OFF); |
jmarkel44 | 71:34856d21f2bf | 220 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 221 | } else { |
jmarkel44 | 71:34856d21f2bf | 222 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 223 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 224 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 71:34856d21f2bf | 225 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 226 | v->setState(CONTROL_OFF); |
jmarkel44 | 71:34856d21f2bf | 227 | } |
jmarkel44 | 71:34856d21f2bf | 228 | } |
jmarkel44 | 71:34856d21f2bf | 229 | } |
jmarkel44 | 66:db1425574b58 | 230 | |
jmarkel44 | 66:db1425574b58 | 231 | return 0; |
jmarkel44 | 66:db1425574b58 | 232 | } |
jmarkel44 | 66:db1425574b58 | 233 | |
jmarkel44 | 66:db1425574b58 | 234 | static void loadPersistentOutputs(void) |
jmarkel44 | 66:db1425574b58 | 235 | { |
jmarkel44 | 66:db1425574b58 | 236 | bool status; |
jmarkel44 | 66:db1425574b58 | 237 | MbedJSONValue json_value; |
jmarkel44 | 71:34856d21f2bf | 238 | |
jmarkel44 | 70:7427f4959201 | 239 | printf("\rLoading persistent controls\n"); |
jmarkel44 | 66:db1425574b58 | 240 | |
jmarkel44 | 66:db1425574b58 | 241 | std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles(); |
jmarkel44 | 70:7427f4959201 | 242 | |
jmarkel44 | 66:db1425574b58 | 243 | for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) { |
jmarkel44 | 67:49f266601d83 | 244 | if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) { |
jmarkel44 | 66:db1425574b58 | 245 | |
jmarkel44 | 66:db1425574b58 | 246 | logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name); |
jmarkel44 | 66:db1425574b58 | 247 | |
jmarkel44 | 66:db1425574b58 | 248 | char scratchBuf[1024]; |
jmarkel44 | 66:db1425574b58 | 249 | |
jmarkel44 | 66:db1425574b58 | 250 | status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024); |
jmarkel44 | 66:db1425574b58 | 251 | if( status != true ) { |
jmarkel44 | 66:db1425574b58 | 252 | logInfo("(%d)read file failed, status=%d", __LINE__, status); |
jmarkel44 | 66:db1425574b58 | 253 | } else { |
jmarkel44 | 66:db1425574b58 | 254 | logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 255 | } |
jmarkel44 | 66:db1425574b58 | 256 | |
jmarkel44 | 66:db1425574b58 | 257 | parse( json_value, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 258 | |
jmarkel44 | 66:db1425574b58 | 259 | string id = json_value["id"].get<string>(); |
jmarkel44 | 70:7427f4959201 | 260 | printf("\r %s id = %s loaded\n", __func__, id.c_str()); |
jmarkel44 | 71:34856d21f2bf | 261 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 262 | outputMap[id] = v; |
jmarkel44 | 66:db1425574b58 | 263 | } |
jmarkel44 | 66:db1425574b58 | 264 | } |
jmarkel44 | 70:7427f4959201 | 265 | } |