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 14:42:19 2016 +0000
Revision:
75:96512ccc0443
Parent:
74:03ccf04998b5
Child:
77:43e0a3d9e536
cleanup

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