Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Revision 72:c5709ae7b193, committed 2015-02-16
- Comitter:
- xinlei
- Date:
- Mon Feb 16 13:15:52 2015 +0000
- Parent:
- 71:063c45e99578
- Child:
- 73:313975bfec96
- Commit message:
- logging: incorporation of dedicated logging module.
Changed in this revision
--- a/DeviceBootstrap.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/DeviceBootstrap.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -7,6 +7,7 @@ #include "CharValue.h" #include "IntegerValue.h" #include "ParsedRecord.h" +#include "logging.h" DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, DeviceIO& io, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : _client(client), @@ -24,7 +25,7 @@ if (!obtainFromPlatform()) return false; if (!writeToStorage()) - puts("Warning: Could not write credentials to file!"); + aWarning("Could not write credentials to file!"); } if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS) @@ -82,7 +83,7 @@ _client.stop(); for (size_t q = 0; q < recvdRecord.values(); q++) - puts(recvdRecord.rawValue(q)); + aDebug(recvdRecord.rawValue(q)); if ((recvdRecord.values() < 1) || (recvdRecord.value(0).integerValue() == 50)) {
--- a/DeviceIntegration.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/DeviceIntegration.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -3,6 +3,7 @@ #include "ComposedRecord.h" #include "CharValue.h" #include "IntegerValue.h" +#include "logging.h" DeviceIntegration::DeviceIntegration(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceInfo& deviceInfo) : _client(client), @@ -60,7 +61,7 @@ // template bootstrapping process if (_client.bootstrap(_tpl) != SMARTREST_SUCCESS) { - puts("Template bootstrap failed."); + aError("Template bootstrap failed.\r\n"); return false; } @@ -84,20 +85,20 @@ return false; if (_client.send(record) != SMARTREST_SUCCESS) { - puts("Send failed."); + aWarning("Send failed."); _client.stop(); return false; } if (_client.receive(received) != SMARTREST_SUCCESS) { - puts("No device found."); + aError("No device found."); _client.stop(); return false; } _client.stop(); if (received.values() == 0) { - puts("Received no values."); + aWarning("Received no values."); return false; }
--- a/MbedAgent.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/MbedAgent.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -1,5 +1,6 @@ #include "MbedAgent.h" #include "rtos.h" +#include "logging.h" MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : _io(io), @@ -25,41 +26,37 @@ { bool flag = true; if (!_integration.init()) { - puts("Init Device Integration failed."); + aWarning("Init Device Integration failed.\r\n"); flag = false; } if (!_configurationSynchronization.init()) { - puts("Init Configuration Synchronization failed."); + aWarning("Init Configuration Synchronization failed.\r\n"); flag = false; } if (!_signalQualityMeasurement.init()) { - puts("Init Signal Quality Measurement failed."); + aWarning("Init Signal Quality Measurement failed.\r\n"); flag = false; } if (!_temperatureMeasurement.init()) { - puts("Init Temperature Measurement failed."); + aWarning("Init Temperature Measurement failed.\r\n"); flag = false; } if (!_accelerationMeasurement.init()) { - puts("Init Acceleration Measurement failed."); + aWarning("Init Acceleration Measurement failed.\r\n"); flag = false; } if (!_analogMeasurement.init()) { - puts("Init Analog Measurement failed."); + aWarning("Init Analog Measurement failed.\r\n"); flag = false; } if (!_locationUpdate.init()) { - puts("Init Location Measurement failed."); + aWarning("Init Location Measurement failed.\r\n"); flag = false; } if (!_operationSupport.init()) { - puts("Init Operation Support failed."); + aWarning("Init Operation Support failed.\r\n"); flag = false; } - if (flag) - puts("T"); - else - puts("F"); return flag; }
--- a/io/DeviceIO.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/io/DeviceIO.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -1,4 +1,5 @@ #include "DeviceIO.h" +#include "logging.h" #define DEF "\033[39m" #define GRE "\033[32m" @@ -53,20 +54,20 @@ void DeviceIO::lcdPrint(const char *line1, const char *line2, const char *line3) { - printf(GRE "io::lcdPrint" DEF "\r\n"); + aInfo(GRE "io::lcdPrint" DEF "\r\n"); _lcd.cls(); _lcd.locate(0, 0); _lcd.printf("%s\n", line1); - printf(GRE "> " CYA "%s\r\n" DEF, line1); + aInfo(GRE "> " CYA "%s\r\n" DEF, line1); if (line2 != NULL) { _lcd.printf("%s\n", line2); - printf(GRE "> " CYA "%s\r\n" DEF, line2); + aInfo(GRE "> " CYA "%s\r\n" DEF, line2); if (line3 != NULL) { _lcd.printf("%s\n", line3); - printf(GRE "> " CYA "%s\r\n" DEF, line3); + aInfo(GRE "> " CYA "%s\r\n" DEF, line3); } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/logging/logging.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -0,0 +1,81 @@ +#include <cstdio> +#include <cstdarg> +#include "logging.h" +using namespace std; + + +aLogLevel _level = A_INFO; // Default log level is INFO. + +int _log(aLogLevel lvl, const char *fmt, va_list& vlist) +{ + if (_level <= lvl) { + int n = 0; + switch (lvl) + { + case A_DEBUG: n += printf("[DEBUG] "); break; + case A_WARNING: n += printf("[WARN] "); break; + case A_INFO: n += printf("[INFO] "); break; + case A_ERROR: n += printf("[ERROR] "); break; + case A_CRITICAL: n += printf("[CRITICAL] "); break; + default: n += printf("[UNKNOWN] "); break; + } + return n+vprintf(fmt, vlist); + } else { + return 0; + } +} + +int aDebug(const char *fmt, ...) +{ + va_list vlist; + va_start(vlist, fmt); + int n = _log(A_DEBUG, fmt, vlist); + va_end(vlist); + return n; +} + +int aCritical(const char *fmt, ...) +{ + va_list vlist; + va_start(vlist, fmt); + int n = _log(A_CRITICAL, fmt, vlist); + va_end(vlist); + return n; +} + +int aError(const char *fmt, ...) +{ + va_list vlist; + va_start(vlist, fmt); + int n = _log(A_ERROR, fmt, vlist); + va_end(vlist); + return n; +} + +int aInfo(const char *fmt, ...) +{ + va_list vlist; + va_start(vlist, fmt); + int n = _log(A_INFO, fmt, vlist); + va_end(vlist); + return n; +} + +int aWarning(const char *fmt, ...) +{ + va_list vlist; + va_start(vlist, fmt); + int n = _log(A_WARNING, fmt, vlist); + va_end(vlist); + return n; +} + +aLogLevel getLevel() +{ + return _level; +} + +void setLevel(aLogLevel lvl) +{ + _level = lvl; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/logging/logging.h Mon Feb 16 13:15:52 2015 +0000 @@ -0,0 +1,23 @@ +#ifndef LOGGING_H +#define LOGGING_H + +/* Enumeration of logging levels. + Logging levels below have higher severity than the above levels. + */ +enum aLogLevel{ + A_DEBUG=10, + A_WARNING=20, + A_INFO=30, + A_ERROR=40, + A_CRITICAL=50 +}; + +int aDebug(const char *fmt, ...); +int aCritical(const char *fmt, ...); +int aError(const char *fmt, ...); +int aInfo(const char *fmt, ...); +int aWarning(const char *fmt, ...); + +aLogLevel getLevel(); +void setLevel(aLogLevel lvl); +#endif /* LOGGING_H */
--- a/main.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/main.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -1,3 +1,5 @@ +#include <stdio.h> + #include "mbed.h" #include "rtos.h" #include "MDM.h" @@ -6,10 +8,8 @@ #include "DeviceMemory.h" #include "MbedAgent.h" #include "GPSTracker.h" - #include "DeviceConfiguration.h" - -#include <stdio.h> +#include "logging.h" /** * SIM PIN. Null for no pin. @@ -25,6 +25,7 @@ int main() { + setLevel(A_DEBUG); MDMParser::DevStatus devStatus; int res; uint8_t status = 0;
--- a/measurement/AccelerationMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/measurement/AccelerationMeasurement.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -4,6 +4,7 @@ #include "IntegerValue.h" #include "FloatValue.h" #include "MbedClient.h" +#include "logging.h" #define THRESHOLD_PERCENT_ACCE 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. #define TIME_LIMIT_ACCE 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar. (in seconds) @@ -49,10 +50,10 @@ abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ACCE && abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_ACCE) { if (sendingTimer.read() < TIME_LIMIT_ACCE) { - printf("Similar acceleration readings found, no sending!\r\n"); + aInfo("Similar acceleration readings found, no sending!\r\n"); return true; } else { - printf("Sending timer of acceleration sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + aInfo("Sending timer of acceleration sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); } } @@ -66,7 +67,7 @@ return false; if (_client.send(record) != SMARTREST_SUCCESS) { - puts("Signal measurement failed."); + aError("Signal measurement failed."); _client.stop(); return false; } @@ -75,6 +76,6 @@ oldValues[1] = data[1]; oldValues[2] = data[2]; sendingTimer.reset(); - printf("Acceleration readings sent.\r\n"); + aInfo("Acceleration readings sent.\r\n"); return true; }
--- a/measurement/AnalogMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/measurement/AnalogMeasurement.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -3,6 +3,7 @@ #include "CharValue.h" #include "IntegerValue.h" #include "FloatValue.h" +#include "logging.h" #define THRESHOLD_PERCENT_ANA 0.05 // Percentage cut-off for avoiding sending similar analog sensor data. #define TIME_LIMIT_ANA 900 // Time interval for forcing a sending even if analog sensor readings are constantly similar. @@ -42,10 +43,10 @@ if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA && abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) { if (sendingTimer.read() < TIME_LIMIT_ANA) { - printf("Similar analog readings found, no sending!\r\n"); + aInfo("Similar analog readings found, no sending!\r\n"); return true; } else { - printf("Sending timer of analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + aInfo("Sending timer of analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); } } @@ -58,7 +59,7 @@ return false; if (_client.send(record) != SMARTREST_SUCCESS) { - puts("Signal measurement failed."); + aError("Signal measurement failed."); _client.stop(); return false; } @@ -66,6 +67,6 @@ oldValues[0] = data[0]; oldValues[1] = data[1]; sendingTimer.reset(); - printf("Analog readings sent.\r\n"); + aInfo("Analog readings sent.\r\n"); return true; }
--- a/measurement/LocationUpdate.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/measurement/LocationUpdate.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -4,6 +4,7 @@ #include "CharValue.h" #include "IntegerValue.h" #include "FloatValue.h" +#include "logging.h" #define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. #define TIME_LIMIT_LOC 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar. @@ -45,7 +46,7 @@ GPSTracker::Position position; if (!_gpsTracker.position(&position)) { - puts("No GPS data available."); + aError("No GPS data available.\r\n"); return true; } float data[3] = { 0.0, 0.0, 0.0 }; @@ -56,14 +57,13 @@ abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC && abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) { if (sendingTimer.read() < TIME_LIMIT_LOC) { - printf("Similar location readings found, no sending!\r\n"); + aInfo("Similar location readings found, no sending!\r\n"); return true; } else { - printf("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + aInfo("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); } } - puts("Starting measurement sending."); Aggregator aggregator; ComposedRecord record1, record2; IntegerValue msgId1(108); @@ -79,9 +79,8 @@ if ((!aggregator.add(record1)) || (!aggregator.add(record2))) return false; - puts("Sending GPS measurement."); if (_client.send(aggregator) != SMARTREST_SUCCESS) { - puts("Signal measurement failed."); + aError("Signal measurement failed.\r\n"); _client.stop(); return false; } @@ -91,6 +90,6 @@ oldValues[1] = data[1]; oldValues[2] = data[2]; sendingTimer.reset(); - printf("Location readings sent.\r\n"); + aInfo("Location readings sent.\r\n"); return true; }
--- a/measurement/SignalQualityMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/measurement/SignalQualityMeasurement.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -3,6 +3,7 @@ #include "CharValue.h" #include "IntegerValue.h" #include "FloatValue.h" +#include "logging.h" #define THRESHOLD_PERCENT_SIG 0.05 // Percentage cut-off for avoiding sending similar signal sensor data. #define TIME_LIMIT_SIG 900 // Time interval for forcing a sending even if signal sensor readings are constantly similar. @@ -46,10 +47,10 @@ if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_SIG && abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_SIG) { if (sendingTimer.read() < TIME_LIMIT_SIG) { - printf("Similar signal readings found, no sending!\r\n"); + aInfo("Similar signal readings found, no sending!\r\n"); return true; } else { - printf("Sending timer of signal sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + aInfo("Sending timer of signal sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); } } @@ -62,7 +63,7 @@ return false; if (_client.send(record) != SMARTREST_SUCCESS) { - puts("Signal measurement failed."); + aError("Signal measurement failed.\r\n"); _client.stop(); return false; } @@ -71,6 +72,6 @@ oldValues[0] = data[0]; oldValues[1] = data[1]; sendingTimer.reset(); - printf("Signal quality measurement sent.\r\n"); + aInfo("Signal quality measurement sent.\r\n"); return true; }
--- a/measurement/TemperatureMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 +++ b/measurement/TemperatureMeasurement.cpp Mon Feb 16 13:15:52 2015 +0000 @@ -3,6 +3,7 @@ #include "CharValue.h" #include "IntegerValue.h" #include "FloatValue.h" +#include "logging.h" #define THRESHOLD_PERCENT_TEMP 0.05 // Percentage cut-off for avoiding sending similar temperature sensor data. #define TIME_LIMIT_TEMP 900 // Time interval for forcing a sending even if analog sensor readings are constantly similar. @@ -42,10 +43,10 @@ data = _sensor.temp(); if (abs(oldValue-data) <= abs(oldValue)*THRESHOLD_PERCENT_TEMP) { if (sendingTimer.read() < TIME_LIMIT_TEMP) { - printf("Similar temperature readings found, no sending!\r\n"); + aInfo("Similar temperature readings found, no sending!\r\n"); return true; } else { - printf("Sending timer of temperature sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + aInfo("Sending timer of temperature sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); } } @@ -57,13 +58,13 @@ return false; if (_client.send(record) != SMARTREST_SUCCESS) { - puts("Signal measurement failed."); + aError("Signal measurement failed.\r\n"); _client.stop(); return false; } _client.stop(); oldValue = data; sendingTimer.reset(); - printf("Temperature readings sent.\r\n"); + aInfo("Temperature readings sent.\r\n"); return true; }