Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Mon Sep 19 21:03:14 2016 +0000
Revision:
77:43e0a3d9e536
Parent:
75:96512ccc0443
Child:
80:b12b0adfcdc2
output task priority vectors;

Who changed what in which revision?

UserRevisionLine numberNew 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 77:43e0a3d9e536 11 #include <string>
jmarkel44 77:43e0a3d9e536 12 #include <algorithm>
jmarkel44 48:1c7861d80d16 13
jmarkel44 70:7427f4959201 14 // locals
jmarkel44 67:49f266601d83 15 static int createOutput(const char *controlFile);
jmarkel44 66:db1425574b58 16 static void loadPersistentOutputs(void);
jmarkel44 63:0ded43237b22 17
jmarkel44 74:03ccf04998b5 18 // local helpers
jmarkel44 77:43e0a3d9e536 19 static int enableOutputReq (const char* id, unsigned int pri, const char* output);
jmarkel44 77:43e0a3d9e536 20 static int disableOutputReq (const char *id, unsigned int pri, const char *output);
jmarkel44 72:3754b352f156 21 static int unregisterControl(const char *id, unsigned int pri, const char *output);
jmarkel44 71:34856d21f2bf 22
jmarkel44 71:34856d21f2bf 23 typedef std::map<string, vector<Control> > StringOutputVector_t;
jmarkel44 77:43e0a3d9e536 24 StringOutputVector_t outputMap;
jmarkel44 66:db1425574b58 25
jmarkel44 77:43e0a3d9e536 26 bool operator<(const Control &control1, const Control &control2) {
jmarkel44 77:43e0a3d9e536 27 return control1.getPriority() < control2.getPriority();
jmarkel44 77:43e0a3d9e536 28 }
jmarkel44 63:0ded43237b22 29
jmarkel44 70:7427f4959201 30 /*****************************************************************************
jmarkel44 70:7427f4959201 31 * Function: OutputTask
jmarkel44 70:7427f4959201 32 * Description: Main entry point for the Output Task
jmarkel44 70:7427f4959201 33 *
jmarkel44 70:7427f4959201 34 * @param args -> not used
jmarkel44 70:7427f4959201 35 * @return none
jmarkel44 70:7427f4959201 36 *****************************************************************************/
jmarkel44 48:1c7861d80d16 37 void OutputTask(void const *args)
jmarkel44 48:1c7861d80d16 38 {
jmarkel44 51:66b820f203a5 39 UNUSED(args);
jmarkel44 70:7427f4959201 40
jmarkel44 67:49f266601d83 41 printf("\r%s has started...\n", __func__);
jmarkel44 63:0ded43237b22 42
jmarkel44 66:db1425574b58 43 loadPersistentOutputs();
jmarkel44 75:96512ccc0443 44 osSignalSet(mainThreadId, sig_output_continue);
jmarkel44 75:96512ccc0443 45
jmarkel44 66:db1425574b58 46
jmarkel44 48:1c7861d80d16 47 while (true) {
jmarkel44 51:66b820f203a5 48 // wait for an event
jmarkel44 56:225786c56315 49 osEvent evt = OutputMasterMailBox.get();
jmarkel44 51:66b820f203a5 50 if (evt.status == osEventMail) {
jmarkel44 63:0ded43237b22 51 OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p;
jmarkel44 74:03ccf04998b5 52 #if 0
jmarkel44 56:225786c56315 53 printf("\r%s received message from someone...\n", __func__);
jmarkel44 71:34856d21f2bf 54 printf("\rmsg->relay = %s\n", msg->output);
jmarkel44 56:225786c56315 55 printf("\rmsg->state = %s\n", msg->state == ON ? "ON" : "OFF");
jmarkel44 56:225786c56315 56 printf("\rmsg->priority = %u\n", msg->priority);
jmarkel44 74:03ccf04998b5 57 #endif
jmarkel44 63:0ded43237b22 58
jmarkel44 63:0ded43237b22 59 switch ( msg->action ) {
jmarkel44 63:0ded43237b22 60 case ACTION_NEW:
jmarkel44 63:0ded43237b22 61 // read the file and and create an output entry
jmarkel44 66:db1425574b58 62 (void) createOutput(msg->controlFile);
jmarkel44 63:0ded43237b22 63 break;
jmarkel44 71:34856d21f2bf 64 case ACTION_CONTROL_ON:
jmarkel44 77:43e0a3d9e536 65 logInfo("%s is requesting ON control of %s", msg->id, msg->output);
jmarkel44 71:34856d21f2bf 66 (void) enableOutputReq(msg->id, msg->priority, msg->output);
jmarkel44 71:34856d21f2bf 67 break;
jmarkel44 71:34856d21f2bf 68 case ACTION_CONTROL_OFF:
jmarkel44 77:43e0a3d9e536 69 logInfo("%s is requesting OFF control of %s", msg->id, msg->output);
jmarkel44 71:34856d21f2bf 70 (void) disableOutputReq(msg->id, msg->priority, msg->output);
jmarkel44 71:34856d21f2bf 71 break;
jmarkel44 72:3754b352f156 72 case ACTION_CONTROL_UNREGISTER:
jmarkel44 77:43e0a3d9e536 73 logInfo("%s is requesting its deletion from %s", msg->id, msg->output);
jmarkel44 72:3754b352f156 74 (void) unregisterControl(msg->id, msg->priority, msg->output);
jmarkel44 72:3754b352f156 75 break;
jmarkel44 63:0ded43237b22 76 default:
jmarkel44 63:0ded43237b22 77 break;
jmarkel44 63:0ded43237b22 78 }
jmarkel44 63:0ded43237b22 79
jmarkel44 56:225786c56315 80 // free the message
jmarkel44 56:225786c56315 81 OutputMasterMailBox.free(msg);
jmarkel44 51:66b820f203a5 82 }
jmarkel44 48:1c7861d80d16 83 }
jmarkel44 66:db1425574b58 84 }
jmarkel44 66:db1425574b58 85
jmarkel44 70:7427f4959201 86 /*****************************************************************************
jmarkel44 70:7427f4959201 87 * Function: DisplayOutputs
jmarkel44 70:7427f4959201 88 * Description: Display a list of outputs and its controls
jmarkel44 70:7427f4959201 89 *
jmarkel44 70:7427f4959201 90 * @param args -> not used
jmarkel44 70:7427f4959201 91 * @return none
jmarkel44 70:7427f4959201 92 *****************************************************************************/
jmarkel44 66:db1425574b58 93 void DisplayOutputs(void)
jmarkel44 66:db1425574b58 94 {
jmarkel44 66:db1425574b58 95 StringOutputVector_t::iterator pos;
jmarkel44 66:db1425574b58 96
jmarkel44 66:db1425574b58 97 for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
jmarkel44 71:34856d21f2bf 98 if ( pos->second.empty() ) {
jmarkel44 71:34856d21f2bf 99 printf("\r [%s]->[no controls]\n", pos->first.c_str());
jmarkel44 71:34856d21f2bf 100 } else {
jmarkel44 71:34856d21f2bf 101 printf("\r [%s]->", pos->first.c_str());
jmarkel44 71:34856d21f2bf 102 vector<Control>::iterator i;
jmarkel44 71:34856d21f2bf 103 for ( i = pos->second.begin(); i != pos->second.end(); ++i ) {
jmarkel44 71:34856d21f2bf 104 i->display();
jmarkel44 71:34856d21f2bf 105 }
jmarkel44 71:34856d21f2bf 106 printf("\n");
jmarkel44 71:34856d21f2bf 107 }
jmarkel44 66:db1425574b58 108 }
jmarkel44 66:db1425574b58 109 printf("\r\n");
jmarkel44 66:db1425574b58 110 }
jmarkel44 66:db1425574b58 111
jmarkel44 71:34856d21f2bf 112
jmarkel44 71:34856d21f2bf 113 /*****************************************************************************
jmarkel44 72:3754b352f156 114 * Function: createOutput
jmarkel44 72:3754b352f156 115 * Description:
jmarkel44 71:34856d21f2bf 116 *
jmarkel44 77:43e0a3d9e536 117 * @param controlFile -> name of output file
jmarkel44 71:34856d21f2bf 118 * @return none
jmarkel44 71:34856d21f2bf 119 *****************************************************************************/
jmarkel44 77:43e0a3d9e536 120 static int createOutput(const char *outputFile)
jmarkel44 66:db1425574b58 121 {
jmarkel44 66:db1425574b58 122 char dataBuf[1024];
jmarkel44 77:43e0a3d9e536 123 int status = GLOBAL_mdot->readUserFile(outputFile, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 67:49f266601d83 124 if ( status != true ) {
jmarkel44 77:43e0a3d9e536 125 logError("%s failed to read %s", __func__, outputFile);
jmarkel44 66:db1425574b58 126 return -1;
jmarkel44 66:db1425574b58 127 }
jmarkel44 66:db1425574b58 128
jmarkel44 66:db1425574b58 129 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 130 parse(json_value, dataBuf);
jmarkel44 66:db1425574b58 131
jmarkel44 66:db1425574b58 132 // extract the relay information
jmarkel44 66:db1425574b58 133 string id = json_value["id"].get<string>();
jmarkel44 66:db1425574b58 134
jmarkel44 77:43e0a3d9e536 135 // maps don't allow duplicates, and the vector is empty for now
jmarkel44 71:34856d21f2bf 136 vector<Control> v;
jmarkel44 71:34856d21f2bf 137 outputMap[id] = v;
jmarkel44 71:34856d21f2bf 138
jmarkel44 71:34856d21f2bf 139 return 0;
jmarkel44 71:34856d21f2bf 140 }
jmarkel44 71:34856d21f2bf 141
jmarkel44 71:34856d21f2bf 142
jmarkel44 71:34856d21f2bf 143 /*****************************************************************************
jmarkel44 72:3754b352f156 144 * Function: enableOutputReq
jmarkel44 71:34856d21f2bf 145 * Description: Display a list of outputs and its controls
jmarkel44 71:34856d21f2bf 146 *
jmarkel44 71:34856d21f2bf 147 * @param args -> not used
jmarkel44 71:34856d21f2bf 148 * @return none
jmarkel44 71:34856d21f2bf 149 *****************************************************************************/
jmarkel44 71:34856d21f2bf 150 static int enableOutputReq(const char *id, unsigned int priority, const char *output)
jmarkel44 71:34856d21f2bf 151 {
jmarkel44 71:34856d21f2bf 152 // attempt to find the output in the map
jmarkel44 71:34856d21f2bf 153 StringOutputVector_t::iterator pos;
jmarkel44 71:34856d21f2bf 154
jmarkel44 71:34856d21f2bf 155 pos = outputMap.find(output);
jmarkel44 71:34856d21f2bf 156 if ( pos == outputMap.end() ) {
jmarkel44 71:34856d21f2bf 157 printf("%s: failed to find the designated output %s\n",
jmarkel44 71:34856d21f2bf 158 __func__, output);
jmarkel44 71:34856d21f2bf 159 return -1;
jmarkel44 71:34856d21f2bf 160 }
jmarkel44 71:34856d21f2bf 161
jmarkel44 71:34856d21f2bf 162 if ( pos->second.empty() ) {
jmarkel44 71:34856d21f2bf 163 // this is a new request
jmarkel44 71:34856d21f2bf 164 string cid(id);
jmarkel44 71:34856d21f2bf 165 Control c(cid, priority, CONTROL_ON);
jmarkel44 71:34856d21f2bf 166 pos->second.push_back(c);
jmarkel44 71:34856d21f2bf 167 } else {
jmarkel44 71:34856d21f2bf 168 // find this control in the list
jmarkel44 71:34856d21f2bf 169 vector<Control>::iterator v;
jmarkel44 71:34856d21f2bf 170 for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
jmarkel44 71:34856d21f2bf 171 if ( strcmp(v->getId().c_str(), id) == 0 ) {
jmarkel44 71:34856d21f2bf 172 v->setState(CONTROL_ON);
jmarkel44 77:43e0a3d9e536 173 break;
jmarkel44 71:34856d21f2bf 174 }
jmarkel44 71:34856d21f2bf 175 }
jmarkel44 77:43e0a3d9e536 176 if ( v == pos->second.end() ) {
jmarkel44 77:43e0a3d9e536 177 // this is a new request, so add it and sort the vector
jmarkel44 77:43e0a3d9e536 178 string cid(id);
jmarkel44 77:43e0a3d9e536 179 Control c(cid, priority, CONTROL_ON);
jmarkel44 77:43e0a3d9e536 180 pos->second.push_back(c);
jmarkel44 77:43e0a3d9e536 181 std::sort(pos->second.begin(), pos->second.end());
jmarkel44 77:43e0a3d9e536 182 }
jmarkel44 71:34856d21f2bf 183 }
jmarkel44 71:34856d21f2bf 184
jmarkel44 71:34856d21f2bf 185 return 0;
jmarkel44 71:34856d21f2bf 186 }
jmarkel44 71:34856d21f2bf 187
jmarkel44 71:34856d21f2bf 188 /*****************************************************************************
jmarkel44 72:3754b352f156 189 * Function: disableOutputReq
jmarkel44 72:3754b352f156 190 * Description:
jmarkel44 71:34856d21f2bf 191 *
jmarkel44 71:34856d21f2bf 192 * @param args -> not used
jmarkel44 71:34856d21f2bf 193 * @return none
jmarkel44 71:34856d21f2bf 194 *****************************************************************************/
jmarkel44 71:34856d21f2bf 195 static int disableOutputReq(const char *id, unsigned int priority, const char *output)
jmarkel44 71:34856d21f2bf 196 {
jmarkel44 71:34856d21f2bf 197 // attempt to find the output in the map
jmarkel44 71:34856d21f2bf 198 StringOutputVector_t::iterator pos;
jmarkel44 71:34856d21f2bf 199
jmarkel44 71:34856d21f2bf 200 pos = outputMap.find(output);
jmarkel44 71:34856d21f2bf 201 if ( pos == outputMap.end() ) {
jmarkel44 71:34856d21f2bf 202 printf("%s: failed to find the designated output %s\n",
jmarkel44 71:34856d21f2bf 203 __func__, output);
jmarkel44 71:34856d21f2bf 204 return -1;
jmarkel44 71:34856d21f2bf 205 }
jmarkel44 71:34856d21f2bf 206
jmarkel44 71:34856d21f2bf 207 // is the same id requesting?
jmarkel44 71:34856d21f2bf 208 if ( pos->second.empty() ) {
jmarkel44 71:34856d21f2bf 209 string cid(id);
jmarkel44 71:34856d21f2bf 210 Control c(cid, priority, CONTROL_OFF);
jmarkel44 71:34856d21f2bf 211 pos->second.push_back(c);
jmarkel44 71:34856d21f2bf 212 } else {
jmarkel44 71:34856d21f2bf 213 // find this control in the list
jmarkel44 71:34856d21f2bf 214 vector<Control>::iterator v;
jmarkel44 71:34856d21f2bf 215 for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
jmarkel44 71:34856d21f2bf 216 if ( strcmp(v->getId().c_str(), id) == 0 ) {
jmarkel44 71:34856d21f2bf 217 v->setState(CONTROL_OFF);
jmarkel44 77:43e0a3d9e536 218 break;
jmarkel44 71:34856d21f2bf 219 }
jmarkel44 71:34856d21f2bf 220 }
jmarkel44 71:34856d21f2bf 221 }
jmarkel44 66:db1425574b58 222
jmarkel44 66:db1425574b58 223 return 0;
jmarkel44 66:db1425574b58 224 }
jmarkel44 66:db1425574b58 225
jmarkel44 74:03ccf04998b5 226 /*****************************************************************************
jmarkel44 74:03ccf04998b5 227 * Function: unregisterControl
jmarkel44 74:03ccf04998b5 228 * Description:
jmarkel44 74:03ccf04998b5 229 *
jmarkel44 74:03ccf04998b5 230 * @param id -> control identifier
jmarkel44 74:03ccf04998b5 231 * @param pri -> priority
jmarkel44 74:03ccf04998b5 232 * @param output -> output (e.g. "o_rly5)
jmarkel44 74:03ccf04998b5 233 *
jmarkel44 74:03ccf04998b5 234 * @return 0 on success; -1 on error
jmarkel44 74:03ccf04998b5 235 *****************************************************************************/
jmarkel44 72:3754b352f156 236 static int unregisterControl(const char *id, unsigned int pri, const char *output)
jmarkel44 72:3754b352f156 237 {
jmarkel44 72:3754b352f156 238 // attempt to find the output in the map
jmarkel44 72:3754b352f156 239 StringOutputVector_t::iterator pos;
jmarkel44 72:3754b352f156 240
jmarkel44 72:3754b352f156 241 pos = outputMap.find(output);
jmarkel44 72:3754b352f156 242 if ( pos == outputMap.end() ) {
jmarkel44 72:3754b352f156 243 printf("%s: failed to find the designated output %s\n",
jmarkel44 72:3754b352f156 244 __func__, output);
jmarkel44 72:3754b352f156 245 return -1;
jmarkel44 72:3754b352f156 246 }
jmarkel44 72:3754b352f156 247
jmarkel44 72:3754b352f156 248 // find the control in the list
jmarkel44 72:3754b352f156 249 vector<Control>::iterator v;
jmarkel44 72:3754b352f156 250 for ( v = pos->second.begin(); v != pos->second.end(); ++v) {
jmarkel44 72:3754b352f156 251 if ( strcmp(v->getId().c_str(), id) == 0 ) {
jmarkel44 72:3754b352f156 252 // delete this entry
jmarkel44 72:3754b352f156 253 pos->second.erase(v);
jmarkel44 72:3754b352f156 254 break;
jmarkel44 72:3754b352f156 255 }
jmarkel44 74:03ccf04998b5 256 }
jmarkel44 72:3754b352f156 257 return 0;
jmarkel44 72:3754b352f156 258 }
jmarkel44 72:3754b352f156 259
jmarkel44 72:3754b352f156 260 /*****************************************************************************
jmarkel44 72:3754b352f156 261 * Function: loadPersistentOutputs
jmarkel44 72:3754b352f156 262 * Description: pump up the output map based on persistent files
jmarkel44 72:3754b352f156 263 *
jmarkel44 72:3754b352f156 264 * @param args -> not used
jmarkel44 72:3754b352f156 265 * @return none
jmarkel44 72:3754b352f156 266 *****************************************************************************/
jmarkel44 66:db1425574b58 267 static void loadPersistentOutputs(void)
jmarkel44 66:db1425574b58 268 {
jmarkel44 66:db1425574b58 269 bool status;
jmarkel44 66:db1425574b58 270 MbedJSONValue json_value;
jmarkel44 71:34856d21f2bf 271
jmarkel44 77:43e0a3d9e536 272 printf("\rLoading persistent outputs:\n");
jmarkel44 66:db1425574b58 273
jmarkel44 66:db1425574b58 274 std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
jmarkel44 70:7427f4959201 275
jmarkel44 66:db1425574b58 276 for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
jmarkel44 67:49f266601d83 277 if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) {
jmarkel44 66:db1425574b58 278
jmarkel44 66:db1425574b58 279 logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name);
jmarkel44 66:db1425574b58 280
jmarkel44 66:db1425574b58 281 char scratchBuf[1024];
jmarkel44 66:db1425574b58 282
jmarkel44 66:db1425574b58 283 status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024);
jmarkel44 66:db1425574b58 284 if( status != true ) {
jmarkel44 66:db1425574b58 285 logInfo("(%d)read file failed, status=%d", __LINE__, status);
jmarkel44 66:db1425574b58 286 } else {
jmarkel44 66:db1425574b58 287 logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf );
jmarkel44 66:db1425574b58 288 }
jmarkel44 66:db1425574b58 289
jmarkel44 66:db1425574b58 290 parse( json_value, scratchBuf );
jmarkel44 66:db1425574b58 291
jmarkel44 66:db1425574b58 292 string id = json_value["id"].get<string>();
jmarkel44 77:43e0a3d9e536 293 printf("\r output %s loaded\n", i->name);
jmarkel44 74:03ccf04998b5 294
jmarkel44 74:03ccf04998b5 295 // emplace the empty control vector into the outputm map
jmarkel44 71:34856d21f2bf 296 vector<Control> v;
jmarkel44 71:34856d21f2bf 297 outputMap[id] = v;
jmarkel44 66:db1425574b58 298 }
jmarkel44 66:db1425574b58 299 }
jmarkel44 70:7427f4959201 300 }