![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
MbedAgent.cpp@93:61d44636f020, 2015-04-20 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 20 15:04:23 2015 +0000
- Revision:
- 93:61d44636f020
- Parent:
- 92:0acd11870c6a
- Child:
- 94:010b0f7a0a1a
Integration of new device push code base.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "MbedAgent.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "rtos.h" |
xinlei | 72:c5709ae7b193 | 3 | #include "logging.h" |
xinlei | 76:b07effe83fb8 | 4 | #include "watchdog.h" |
Cumulocity | 41:804f6a0bda26 | 5 | |
xinlei | 92:0acd11870c6a | 6 | MbedAgent::MbedAgent(GPSI2C& gps, MDMSerial& mdm, LCDDisplay& lcdDisplay, |
xinlei | 92:0acd11870c6a | 7 | DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : |
xinlei | 93:61d44636f020 | 8 | _deviceId(0), |
Cumulocity | 41:804f6a0bda26 | 9 | _mdm(mdm), |
Cumulocity | 41:804f6a0bda26 | 10 | _deviceInfo(deviceInfo), |
vwochnik | 67:c360a2b2c948 | 11 | _deviceMemory(deviceMemory), |
xinlei | 93:61d44636f020 | 12 | _lcdDisplay(lcdDisplay), |
vwochnik | 68:0dc778a16d0d | 13 | _configurationProperties(_deviceConfiguration), |
xinlei | 92:0acd11870c6a | 14 | _client(mdm), |
xinlei | 92:0acd11870c6a | 15 | _bootstrap(_client, lcdDisplay, _deviceInfo, _deviceMemory), |
Cumulocity | 41:804f6a0bda26 | 16 | _integration(_client, _tpl, _deviceId, _deviceInfo), |
vwochnik | 68:0dc778a16d0d | 17 | _configurationSynchronization(_client, _tpl, _deviceId, _deviceMemory, _deviceConfiguration, _configurationProperties), |
xinlei | 92:0acd11870c6a | 18 | _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo, _lcdDisplay), |
xinlei | 92:0acd11870c6a | 19 | _temperatureMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay), |
xinlei | 92:0acd11870c6a | 20 | _accelerationMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay), |
xinlei | 92:0acd11870c6a | 21 | _analogMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay), |
xinlei | 92:0acd11870c6a | 22 | _locationUpdate(_client, _tpl, _deviceId, gps, deviceInfo, _lcdDisplay), |
xinlei | 93:61d44636f020 | 23 | pool(), |
xinlei | 93:61d44636f020 | 24 | _operationSupport(_client, _tpl, _deviceId, pool) |
Cumulocity | 41:804f6a0bda26 | 25 | { |
Cumulocity | 41:804f6a0bda26 | 26 | } |
Cumulocity | 41:804f6a0bda26 | 27 | |
Cumulocity | 41:804f6a0bda26 | 28 | bool MbedAgent::init() |
Cumulocity | 41:804f6a0bda26 | 29 | { |
xinlei | 71:063c45e99578 | 30 | bool flag = true; |
xinlei | 71:063c45e99578 | 31 | if (!_integration.init()) { |
xinlei | 92:0acd11870c6a | 32 | _lcdDisplay.setLines("Integration init failure"); |
xinlei | 71:063c45e99578 | 33 | flag = false; |
xinlei | 71:063c45e99578 | 34 | } |
xinlei | 71:063c45e99578 | 35 | if (!_configurationSynchronization.init()) { |
xinlei | 92:0acd11870c6a | 36 | _lcdDisplay.setLines("Config Sync init failure"); |
xinlei | 71:063c45e99578 | 37 | flag = false; |
xinlei | 71:063c45e99578 | 38 | } |
xinlei | 71:063c45e99578 | 39 | if (!_signalQualityMeasurement.init()) { |
xinlei | 92:0acd11870c6a | 40 | _lcdDisplay.setLines("Signal init failure"); |
xinlei | 71:063c45e99578 | 41 | flag = false; |
xinlei | 71:063c45e99578 | 42 | } |
xinlei | 71:063c45e99578 | 43 | if (!_temperatureMeasurement.init()) { |
xinlei | 92:0acd11870c6a | 44 | _lcdDisplay.setLines("Temperature init failure"); |
xinlei | 71:063c45e99578 | 45 | flag = false; |
Cumulocity | 41:804f6a0bda26 | 46 | } |
xinlei | 71:063c45e99578 | 47 | if (!_accelerationMeasurement.init()) { |
xinlei | 92:0acd11870c6a | 48 | _lcdDisplay.setLines("Acceleration init failure"); |
xinlei | 71:063c45e99578 | 49 | flag = false; |
xinlei | 71:063c45e99578 | 50 | } |
xinlei | 71:063c45e99578 | 51 | if (!_analogMeasurement.init()) { |
xinlei | 92:0acd11870c6a | 52 | _lcdDisplay.setLines("Analog init failure"); |
xinlei | 71:063c45e99578 | 53 | flag = false; |
xinlei | 71:063c45e99578 | 54 | } |
xinlei | 71:063c45e99578 | 55 | if (!_locationUpdate.init()) { |
xinlei | 92:0acd11870c6a | 56 | _lcdDisplay.setLines("Location init failure"); |
xinlei | 71:063c45e99578 | 57 | flag = false; |
xinlei | 71:063c45e99578 | 58 | } |
xinlei | 71:063c45e99578 | 59 | if (!_operationSupport.init()) { |
xinlei | 92:0acd11870c6a | 60 | _lcdDisplay.setLines("Operation init failure"); |
xinlei | 71:063c45e99578 | 61 | flag = false; |
xinlei | 71:063c45e99578 | 62 | } |
xinlei | 71:063c45e99578 | 63 | return flag; |
Cumulocity | 41:804f6a0bda26 | 64 | } |
Cumulocity | 41:804f6a0bda26 | 65 | |
xinlei | 92:0acd11870c6a | 66 | int MbedAgent::run() |
Cumulocity | 41:804f6a0bda26 | 67 | { |
Cumulocity | 41:804f6a0bda26 | 68 | // device bootstrapping process |
vwochnik | 52:8f1370084268 | 69 | if (!_bootstrap.setUpCredentials()) |
xinlei | 92:0acd11870c6a | 70 | return -1; |
xinlei | 93:61d44636f020 | 71 | setAuth(_bootstrap.username(), _bootstrap.password()); |
xinlei | 93:61d44636f020 | 72 | aInfo("Set auth: %s:%s (%s)\n", getUsername(), getPassword(), getAuthStr()); |
vwochnik | 52:8f1370084268 | 73 | |
Cumulocity | 46:f6976fd64387 | 74 | Thread::wait(5000); |
Cumulocity | 41:804f6a0bda26 | 75 | |
xinlei | 92:0acd11870c6a | 76 | _lcdDisplay.setLines("Connect to Cloud", getHost()); |
Cumulocity | 41:804f6a0bda26 | 77 | if (!_integration.integrate()) { |
xinlei | 92:0acd11870c6a | 78 | return -2; |
Cumulocity | 41:804f6a0bda26 | 79 | } |
xinlei | 93:61d44636f020 | 80 | setIdentifier(_client.getIdentifier()); |
xinlei | 93:61d44636f020 | 81 | aInfo("Set id: %s\n", getIdentifier()); |
xinlei | 93:61d44636f020 | 82 | |
vwochnik | 68:0dc778a16d0d | 83 | if (!_configurationSynchronization.integrate()) { |
xinlei | 92:0acd11870c6a | 84 | return -3; |
vwochnik | 68:0dc778a16d0d | 85 | } |
vwochnik | 68:0dc778a16d0d | 86 | |
xinlei | 92:0acd11870c6a | 87 | char status[27]; |
Cumulocity | 41:804f6a0bda26 | 88 | snprintf(status, sizeof(status), "ID: %ld", _deviceId); |
xinlei | 92:0acd11870c6a | 89 | _lcdDisplay.setLines("Connected", status); |
Cumulocity | 41:804f6a0bda26 | 90 | |
xinlei | 92:0acd11870c6a | 91 | const char* user= getUsername(); |
xinlei | 92:0acd11870c6a | 92 | int len = strchr(user, '/')-user+sizeof("Tenant: "); |
xinlei | 92:0acd11870c6a | 93 | len = len <= 27 ? len : 27; |
xinlei | 92:0acd11870c6a | 94 | snprintf(status, len, "Tenant: %s", user); |
xinlei | 92:0acd11870c6a | 95 | _lcdDisplay.setFirstLine(status); |
xinlei | 92:0acd11870c6a | 96 | return 0; |
Cumulocity | 41:804f6a0bda26 | 97 | } |
Cumulocity | 41:804f6a0bda26 | 98 | |
Cumulocity | 41:804f6a0bda26 | 99 | void MbedAgent::loop() |
Cumulocity | 41:804f6a0bda26 | 100 | { |
xinlei | 93:61d44636f020 | 101 | ReportThread reportThread(pool, _mdm); |
xinlei | 93:61d44636f020 | 102 | _operationSupport.run(); |
xinlei | 93:61d44636f020 | 103 | PollingThread pollingThread(pool, _mdm, _lcdDisplay, _configurationSynchronization); |
xinlei | 93:61d44636f020 | 104 | pollingThread.setChannel(_deviceId); |
xinlei | 93:61d44636f020 | 105 | |
xinlei | 76:b07effe83fb8 | 106 | Watchdog wdt; |
xinlei | 76:b07effe83fb8 | 107 | wdt.kick(60.0); // set a 60.0 seconds timeout on watchdog hardware timer |
Cumulocity | 41:804f6a0bda26 | 108 | while (true) { |
xinlei | 93:61d44636f020 | 109 | // _configurationSynchronization.run(); |
xinlei | 93:61d44636f020 | 110 | // _analogMeasurement.run(); |
xinlei | 93:61d44636f020 | 111 | // _signalQualityMeasurement.run(); |
xinlei | 93:61d44636f020 | 112 | // _temperatureMeasurement.run(); |
xinlei | 93:61d44636f020 | 113 | // _accelerationMeasurement.run(); |
xinlei | 93:61d44636f020 | 114 | // _locationUpdate.run(); |
xinlei | 93:61d44636f020 | 115 | Thread::wait(30000); |
xinlei | 76:b07effe83fb8 | 116 | // if ((interval = _configurationProperties.readInterval()) < 0) |
xinlei | 76:b07effe83fb8 | 117 | // break; |
xinlei | 76:b07effe83fb8 | 118 | // while (timer.read() < interval) { |
xinlei | 76:b07effe83fb8 | 119 | // Thread::yield(); |
xinlei | 76:b07effe83fb8 | 120 | // } |
xinlei | 76:b07effe83fb8 | 121 | wdt.kick(); // reset watchdog timer |
Cumulocity | 41:804f6a0bda26 | 122 | } |
Cumulocity | 41:804f6a0bda26 | 123 | } |