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 12:54:55 2016 +0000
Revision:
74:03ccf04998b5
Parent:
72:3754b352f156
Child:
75:96512ccc0443
load persistent controls (first spin)

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