Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Fri Sep 16 21:42:11 2016 +0000
Revision:
72:3754b352f156
Parent:
71:34856d21f2bf
Child:
74:03ccf04998b5
output task operations;

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