
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Revision 106:fc5f25f0e0d5, committed 2015-05-13
- Comitter:
- xinlei
- Date:
- Wed May 13 12:12:55 2015 +0000
- Parent:
- 105:c61f0d62b625
- Child:
- 107:f1ee3e1eb126
- Commit message:
- OperationSupport revamped
Changed in this revision
--- a/MbedAgent.cpp Mon May 11 18:10:44 2015 +0000 +++ b/MbedAgent.cpp Wed May 13 12:12:55 2015 +0000 @@ -6,14 +6,14 @@ _client(), tpl(), _bootstrap(_client, deviceInfo), _integration(_client, tpl, deviceInfo), lcdThirdLineBlank(true), signal(deviceInfo), temp(), poti(), gps(), acc(), conf(), sock(), - pool(), _operationSupport(_client, tpl, pool) + pool(), _operationSupport(tpl, pool, conf) { - reporters[0] = &signal; + reporters[0] = &conf; reporters[1] = &temp; - reporters[2] = &poti; + reporters[2] = &signal; reporters[3] = &gps; reporters[4] = &acc; - reporters[5] = &conf; + reporters[5] = &poti; } bool MbedAgent::init() @@ -97,30 +97,33 @@ void MbedAgent::loop() { ReportThread reportThread(pool); - _operationSupport.run(); - PollThread pollThread(pool, conf); - pollThread.setChannel(deviceID); + _operationSupport.executePendingOperations(); +// PollThread pollThread(pool, conf); +// pollThread.setChannel(deviceID); Watchdog wdt; wdt.kick(60.0); // set a 60.0 seconds watchdog while (true) { + int l = 0; for (size_t i = 0; i < N; ++i) { -// if (reporters[i] == &acc) { - size_t l = reporters[i]->read(buf2, sizeof(buf2), status, DISPLAY_LEN); - bool b = l; - if (b) { // Refresh LCD display needed + if (reporters[i] == NULL) { + int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN); + if (l2) { // Refresh LCD display needed LCDDisplay::inst().setThirdLine(status); - } else if (!lcdThirdLineBlank && !b) { // Clear LCD display needed + Thread::wait(400); + } else if (!lcdThirdLineBlank && !l2) { // Clear LCD display needed LCDDisplay::inst().setThirdLine(""); + Thread::wait(100); } - lcdThirdLineBlank = !b; - if (b) { - int l2 = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); - l2 = sock.sendOnly(buf, l2); - if (l2 < 0) - aWarning("%s\n", status); + lcdThirdLineBlank = !l2; + l += l2; } -// } + } + if (l) { + l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); + l = sock.sendOnly(buf, l); + if (l < 0) + aWarning("%s\n", status); } wdt.kick(); // reset watchdog timer }
--- a/io/GPSTracker.cpp Mon May 11 18:10:44 2015 +0000 +++ b/io/GPSTracker.cpp Wed May 13 12:12:55 2015 +0000 @@ -33,7 +33,6 @@ char buf[256], chr; // needs to be that big otherwise mdm isn't working int ret, len, n; double altitude, latitude, longitude; - aInfo("GPS thread: %p\r\n", Thread::gettid()); while (true) { ret = _gps.getMessage(buf, sizeof(buf)); if (ret <= 0) {
--- a/main.cpp Mon May 11 18:10:44 2015 +0000 +++ b/main.cpp Wed May 13 12:12:55 2015 +0000 @@ -121,7 +121,6 @@ shutdown(); return 0; } - aInfo("Main Thread: %p\r\n", Thread::gettid()); LCDDisplay::inst().setLines("Register Network...", "IMEI", devStatus.imei); if (!mdm.registerNet()) { LCDDisplay::inst().setLines("No Network Coverage");
--- a/operation/ControlParser.cpp Mon May 11 18:10:44 2015 +0000 +++ b/operation/ControlParser.cpp Wed May 13 12:12:55 2015 +0000 @@ -22,16 +22,21 @@ { if (tok.type == Token::INT && strncmp("211", tok.p, tok.len)==0) { opType = 211; - ptrPF = &ControlParser::parseDeviceId; + ptrPF = &ControlParser::parseRowNumber; } else { parseError(tok); } } -void ControlParser::parseDeviceId(Token& tok) +void ControlParser::parseRowNumber(Token& tok) { if (tok.type == Token::INT) { - ptrPF = &ControlParser::parseOpId; + if (opType == 86) + ptrPF = &ControlParser::parseAdviceTimeout; + else + ptrPF = &ControlParser::parseOpId; + } else if (tok.type == Token::NONE) { + ptrPF = &ControlParser::parseAdviceTimeout; } else { parseError(tok); } @@ -77,7 +82,7 @@ if (tok.type == Token::INT) { sscanf(tok.p, "%hu", &opType); if (opType >= 220 && opType <= 222) { - ptrPF = &ControlParser::parseDeviceId; + ptrPF = &ControlParser::parseRowNumber; } else { parseError(tok); } @@ -138,21 +143,13 @@ void ControlParser::parseBayeuxAdvice(Token& tok) { if (strncmp("86", tok.p, tok.len) == 0) { + opType = 86; ptrPF = &ControlParser::parseRowNumber; } else { parseError(tok); } } -void ControlParser::parseRowNumber(Token& tok) -{ - if (tok.type == Token::INT || tok.type == Token::NONE) { - ptrPF = &ControlParser::parseAdviceTimeout; - } else { - parseError(tok); - } -} - void ControlParser::parseAdviceTimeout(Token& tok) { if (tok.type == Token::NONE) {
--- a/operation/ControlParser.h Mon May 11 18:10:44 2015 +0000 +++ b/operation/ControlParser.h Wed May 13 12:12:55 2015 +0000 @@ -24,7 +24,6 @@ void parseAdvicePolicy(Token& tok); void parseAdviceTimeout(Token& tok); void parseBayeuxAdvice(Token& tok); - void parseDeviceId(Token& tok); void parseError(Token& tok); void parseGetOp(Token& tok); void parseGetOpOrBayeuxAdvice(Token& tok);
--- a/operation/OperationSupport.cpp Mon May 11 18:10:44 2015 +0000 +++ b/operation/OperationSupport.cpp Wed May 13 12:12:55 2015 +0000 @@ -1,9 +1,10 @@ #include <string.h> #include "OperationSupport.h" -#include "ComposedRecord.h" -#include "ParsedRecord.h" -#include "IntegerValue.h" -#include "SmartRestConf.h" +//#include "ComposedRecord.h" +//#include "ParsedRecord.h" +//#include "IntegerValue.h" +#include "SmartRestSocket.h" +#include "ControlParser.h" #include "logging.h" bool OperationSupport::init() @@ -55,101 +56,44 @@ return true; } -bool OperationSupport::run() -{ - if (_firstRun) { - _firstRun = false; - bool b = requestPendingOperations(); - return b; - } else { - return true; - } -} - -bool OperationSupport::executePendingOperation(Operation& op) -{ - ComposedRecord r; - if (!r.add(IntegerValue(112)) || !r.add(IntegerValue(op.identifier))) { - return false; - } else if (_client.send(r) != SMARTREST_SUCCESS) { - _client.stop(); - return false; - } - ParsedRecord p; - bool b = true; - while (_client.receive(p) == SMARTREST_SUCCESS) { - if (p.values() >= 3 && - p.value(0).integerValue() >= 220 && - p.value(0).integerValue() <= 222 && - p.value(2).valueType() == VALUE_INTEGER) { - Operation* op1 = opool.alloc(); - op1->identifier = p.value(2).integerValue(); -// bool ret = _executor.executeOperation(p); - bool ret = true; - op1->state = ret ? OPERATION_SUCCESSFUL : OPERATION_FAILED; - opool.put(op1); - } - } - _client.stop(); - return b; -} - -bool OperationSupport::requestPendingOperations() +bool OperationSupport::executePendingOperations() { - IntegerValue msgId(110); - ComposedRecord record; - - if (!record.add(msgId) || !record.add(IntegerValue(deviceID))) - return false; - else if (_client.send(record) != SMARTREST_SUCCESS) { - _client.stop(); - return false; - } - - uint8_t ret; - ParsedRecord received; - Operation opl[10]; - size_t c = 0; - while ((ret=_client.receive(received)) == SMARTREST_SUCCESS) { - if (c < 10 && operationFromRecord(received, opl[c])) { - ++c; - } else { - aWarning("Ignored pending operation after 10.\n"); - break; + char buf[SMARTREST_SIZE]; + char buf2[SMARTREST_BODY_SIZE]; + SmartRestSocket sock; + int l = snprintf(buf2, sizeof(buf2), "110,%ld\r\n", deviceID); + l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); + l = sock.sendAndReceive(buf, l, sizeof(buf)); + if (l < 0) { + aError("Retrieve pending operations\n"); + return false; } - } - _client.stop(); - - for (size_t i = 0; i < c; ++i) { - Operation* op = opool.alloc(); - op->identifier = opl[i].identifier; - op->state = OPERATION_EXECUTING; - opool.put(op); - executePendingOperation(opl[i]); - } - return (ret == SMARTREST_END_OF_RESPONSE || ret == SMARTREST_CONNECTION_CLOSED); -} - -bool OperationSupport::operationFromRecord(ParsedRecord& received, Operation& op) -{ - if ((received.values() < 4) || - (received.value(0).valueType() != VALUE_INTEGER) || -// (received.value(0).integerValue() != 211) || - (received.value(2).valueType() != VALUE_INTEGER) || - (received.value(3).valueType() != VALUE_CHARACTER)) - return false; - - op.identifier = received.value(2).integerValue(); - const char *tmp = received.value(3).characterValue(); - if (strcmp(tmp, strExecuting) == 0) - op.state = OPERATION_EXECUTING; - else if (strcmp(tmp, strSuccessful) == 0) - op.state = OPERATION_SUCCESSFUL; - else if (strcmp(tmp, strFailed) == 0) - op.state = OPERATION_FAILED; - else if (strcmp(tmp, strPending) == 0) - op.state = OPERATION_PENDING; - else - return false; - return true; + const size_t N = 10; + long opl[N]; + size_t i = 0; + for (const char* p = skipHTTPHeader(buf); i<N && *p;) { + long id = 0; + int c = 0; + int n = sscanf(p, "210,1,%ld,PENDING\r\n%n", &id, &c); + if (n == 1) { + opl[i++] = id; + p += c; + } else + break; + } + if (i >= N) + aWarning("Over %u pending operations.\n", N); + ControlParser cp(opool, conf); + bool flag = true; + l = 0; + for (size_t j = 0; j < i; ++j) { + l += snprintf(buf2+l, sizeof(buf2)-l, "112,%ld\r\n", opl[j]); + } + l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); + l = sock.sendAndReceive(buf, l, sizeof(buf)); + if (l >= 0) + cp.parse(buf); + else + flag = false; + return flag; } \ No newline at end of file
--- a/operation/OperationSupport.h Mon May 11 18:10:44 2015 +0000 +++ b/operation/OperationSupport.h Wed May 13 12:12:55 2015 +0000 @@ -2,29 +2,24 @@ #define OPERATIONSUPPORT_H #include "Operation.h" -#include "SmartRest.h" +#include "SmartRestConf.h" #include "SmartRestTemplate.h" +#include "ConfigSync.h" class OperationSupport { public: - OperationSupport(SmartRest& client, SmartRestTemplate& tpl, OperationPool& pool): - _init(false), _firstRun(true), _tpl(tpl), - _client(client), opool(pool) {} + OperationSupport(SmartRestTemplate& tpl, OperationPool& pool, + ConfigSync& _conf): _init(false), _tpl(tpl), opool(pool), conf(_conf) {} + + bool executePendingOperations(); bool init(); - bool run(); - -protected: - bool requestPendingOperations(); - bool executePendingOperation(Operation&); - bool operationFromRecord(ParsedRecord& record, Operation& op); private: bool _init; - bool _firstRun; SmartRestTemplate& _tpl; - SmartRest& _client; OperationPool& opool; + ConfigSync &conf; }; #endif \ No newline at end of file
--- a/operation/PollThread.cpp Mon May 11 18:10:44 2015 +0000 +++ b/operation/PollThread.cpp Wed May 13 12:12:55 2015 +0000 @@ -41,7 +41,6 @@ void PollThread::threadFunc() { unsigned short state = 1; - aInfo("Poll thread: %p\n", Thread::gettid()); while (true) { switch (state) { case 1: if (!handshake()) {
--- a/util/SmartRestSocket.cpp Mon May 11 18:10:44 2015 +0000 +++ b/util/SmartRestSocket.cpp Wed May 13 12:12:55 2015 +0000 @@ -10,7 +10,7 @@ int n = -1; ipLock.lock(); for (size_t i = 0; i < 3; ++i) { - if (cachedIP[0] == '\0') { + if (cachedIP[0] == 0) { MDMParser::IP ip = pMdm->gethostbyname(srHost); if (ip == NOIP) continue; @@ -25,7 +25,7 @@ Socket::set_blocking(false, timeout); break; } else { - cachedIP[0] = '\0'; + cachedIP[0] = 0; } } ipLock.unlock();