Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Thu Sep 15 22:26:28 2016 +0000
Revision:
66:db1425574b58
Parent:
63:0ded43237b22
Child:
67:49f266601d83
output thread framework;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 48:1c7861d80d16 1 #include "global.h"
jmarkel44 66:db1425574b58 2 #include "MbedJSONValue.h"
jmarkel44 48:1c7861d80d16 3
jmarkel44 66:db1425574b58 4 static int createOutput(const char *controlFile);
jmarkel44 66:db1425574b58 5 static void loadPersistentOutputs(void);
jmarkel44 63:0ded43237b22 6
jmarkel44 66:db1425574b58 7 typedef std::map<string, string> StringOutputVector_t;
jmarkel44 66:db1425574b58 8
jmarkel44 66:db1425574b58 9 StringOutputVector_t outputMap;
jmarkel44 63:0ded43237b22 10
jmarkel44 48:1c7861d80d16 11 void OutputTask(void const *args)
jmarkel44 48:1c7861d80d16 12 {
jmarkel44 51:66b820f203a5 13 UNUSED(args);
jmarkel44 63:0ded43237b22 14
jmarkel44 66:db1425574b58 15 loadPersistentOutputs();
jmarkel44 66:db1425574b58 16
jmarkel44 48:1c7861d80d16 17 while (true) {
jmarkel44 51:66b820f203a5 18 // wait for an event
jmarkel44 56:225786c56315 19 osEvent evt = OutputMasterMailBox.get();
jmarkel44 51:66b820f203a5 20 if (evt.status == osEventMail) {
jmarkel44 63:0ded43237b22 21 OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p;
jmarkel44 56:225786c56315 22 printf("\r%s received message from someone...\n", __func__);
jmarkel44 63:0ded43237b22 23 printf("\rmsg->relay = %s\n", msg->output.c_str());
jmarkel44 56:225786c56315 24 printf("\rmsg->state = %s\n", msg->state == ON ? "ON" : "OFF");
jmarkel44 56:225786c56315 25 printf("\rmsg->priority = %u\n", msg->priority);
jmarkel44 63:0ded43237b22 26
jmarkel44 63:0ded43237b22 27 switch ( msg->action ) {
jmarkel44 63:0ded43237b22 28 case ACTION_NEW:
jmarkel44 63:0ded43237b22 29 // read the file and and create an output entry
jmarkel44 66:db1425574b58 30 (void) createOutput(msg->controlFile);
jmarkel44 63:0ded43237b22 31 break;
jmarkel44 66:db1425574b58 32 case ACTION_CONTROL_REQ:
jmarkel44 63:0ded43237b22 33 default:
jmarkel44 63:0ded43237b22 34 break;
jmarkel44 63:0ded43237b22 35 }
jmarkel44 63:0ded43237b22 36
jmarkel44 56:225786c56315 37 // free the message
jmarkel44 56:225786c56315 38 OutputMasterMailBox.free(msg);
jmarkel44 51:66b820f203a5 39 }
jmarkel44 48:1c7861d80d16 40 }
jmarkel44 66:db1425574b58 41 }
jmarkel44 66:db1425574b58 42
jmarkel44 66:db1425574b58 43 void DisplayOutputs(void)
jmarkel44 66:db1425574b58 44 {
jmarkel44 66:db1425574b58 45 StringOutputVector_t::iterator pos;
jmarkel44 66:db1425574b58 46
jmarkel44 66:db1425574b58 47 for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
jmarkel44 66:db1425574b58 48 printf("\r [%s] [ %s]\n", pos->first.c_str(), pos->second.c_str());
jmarkel44 66:db1425574b58 49 }
jmarkel44 66:db1425574b58 50 printf("\r\n");
jmarkel44 66:db1425574b58 51 }
jmarkel44 66:db1425574b58 52
jmarkel44 66:db1425574b58 53 static int createOutput(const char *controlFile)
jmarkel44 66:db1425574b58 54 {
jmarkel44 66:db1425574b58 55 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile, mDot::FM_RDONLY);
jmarkel44 66:db1425574b58 56 if ( file.fd < 0 ) {
jmarkel44 66:db1425574b58 57 logError("%s: failed to open file %s", __func__, controlFile);
jmarkel44 66:db1425574b58 58 return -1;
jmarkel44 66:db1425574b58 59 }
jmarkel44 66:db1425574b58 60
jmarkel44 66:db1425574b58 61 char dataBuf[1024];
jmarkel44 66:db1425574b58 62 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 66:db1425574b58 63 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 66:db1425574b58 64 logError("%s failed to read %s", __func__, controlFile);
jmarkel44 66:db1425574b58 65 return -1;
jmarkel44 66:db1425574b58 66 }
jmarkel44 66:db1425574b58 67
jmarkel44 66:db1425574b58 68 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 69 parse(json_value, dataBuf);
jmarkel44 66:db1425574b58 70
jmarkel44 66:db1425574b58 71 // extract the relay information
jmarkel44 66:db1425574b58 72 string id = json_value["id"].get<string>();
jmarkel44 66:db1425574b58 73
jmarkel44 66:db1425574b58 74 // maps shouldnt' allow duplicates
jmarkel44 66:db1425574b58 75 outputMap[id] = "Null for now";
jmarkel44 66:db1425574b58 76
jmarkel44 66:db1425574b58 77 return 0;
jmarkel44 66:db1425574b58 78
jmarkel44 66:db1425574b58 79 }
jmarkel44 66:db1425574b58 80
jmarkel44 66:db1425574b58 81 static void loadPersistentOutputs(void)
jmarkel44 66:db1425574b58 82 {
jmarkel44 66:db1425574b58 83 bool status;
jmarkel44 66:db1425574b58 84 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 85
jmarkel44 66:db1425574b58 86 std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
jmarkel44 66:db1425574b58 87 for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
jmarkel44 66:db1425574b58 88 if( strncmp( i->name, "output", (strlen("input")-1)) == 0 ) {
jmarkel44 66:db1425574b58 89
jmarkel44 66:db1425574b58 90 logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name);
jmarkel44 66:db1425574b58 91
jmarkel44 66:db1425574b58 92 char scratchBuf[1024];
jmarkel44 66:db1425574b58 93
jmarkel44 66:db1425574b58 94 status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024);
jmarkel44 66:db1425574b58 95 if( status != true ) {
jmarkel44 66:db1425574b58 96 logInfo("(%d)read file failed, status=%d", __LINE__, status);
jmarkel44 66:db1425574b58 97 } else {
jmarkel44 66:db1425574b58 98 logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf );
jmarkel44 66:db1425574b58 99 }
jmarkel44 66:db1425574b58 100
jmarkel44 66:db1425574b58 101 parse( json_value, scratchBuf );
jmarkel44 66:db1425574b58 102
jmarkel44 66:db1425574b58 103 string id = json_value["id"].get<string>();
jmarkel44 66:db1425574b58 104 outputMap[id] = "Null for now";
jmarkel44 66:db1425574b58 105
jmarkel44 66:db1425574b58 106 }
jmarkel44 66:db1425574b58 107 }
jmarkel44 66:db1425574b58 108 }