Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
MbedAgent.cpp@46:f6976fd64387, 2014-07-24 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Jul 24 16:02:47 2014 +0000
- Revision:
- 46:f6976fd64387
- Parent:
- 42:104746744af8
- Child:
- 47:89ae46d5c466
implement device credential reset; show username/password on display
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" |
Cumulocity | 41:804f6a0bda26 | 3 | |
Cumulocity | 42:104746744af8 | 4 | MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo) : |
Cumulocity | 42:104746744af8 | 5 | _io(io), |
Cumulocity | 41:804f6a0bda26 | 6 | _mdm(mdm), |
Cumulocity | 41:804f6a0bda26 | 7 | _deviceInfo(deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 8 | _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER), |
Cumulocity | 41:804f6a0bda26 | 9 | _bootstrap(_client, _mdm, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 10 | _integration(_client, _tpl, _deviceId, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 11 | _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 12 | _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()), |
Cumulocity | 41:804f6a0bda26 | 13 | _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer()), |
Cumulocity | 41:804f6a0bda26 | 14 | _deviceId(0) |
Cumulocity | 41:804f6a0bda26 | 15 | { |
Cumulocity | 41:804f6a0bda26 | 16 | } |
Cumulocity | 41:804f6a0bda26 | 17 | |
Cumulocity | 41:804f6a0bda26 | 18 | bool MbedAgent::init() |
Cumulocity | 41:804f6a0bda26 | 19 | { |
Cumulocity | 41:804f6a0bda26 | 20 | if ((!_integration.init()) || |
Cumulocity | 41:804f6a0bda26 | 21 | (!_signalQualityMeasurement.init()) || |
Cumulocity | 41:804f6a0bda26 | 22 | (!_temperatureMeasurement.init()) || |
Cumulocity | 41:804f6a0bda26 | 23 | (!_accelerationMeasurement.init())) { |
Cumulocity | 41:804f6a0bda26 | 24 | puts("Initialization failed."); |
Cumulocity | 41:804f6a0bda26 | 25 | return false; |
Cumulocity | 41:804f6a0bda26 | 26 | } |
Cumulocity | 41:804f6a0bda26 | 27 | return true; |
Cumulocity | 41:804f6a0bda26 | 28 | } |
Cumulocity | 41:804f6a0bda26 | 29 | |
Cumulocity | 41:804f6a0bda26 | 30 | bool MbedAgent::run() |
Cumulocity | 41:804f6a0bda26 | 31 | { |
Cumulocity | 41:804f6a0bda26 | 32 | // device bootstrapping process |
Cumulocity | 41:804f6a0bda26 | 33 | _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 34 | if (!_bootstrap.setUpCredentials()) { |
Cumulocity | 46:f6976fd64387 | 35 | _io.lcdPrint("BOOTSTRAP FAILURE"); |
Cumulocity | 41:804f6a0bda26 | 36 | puts("Could not obtain credentials."); |
Cumulocity | 41:804f6a0bda26 | 37 | return false; |
Cumulocity | 41:804f6a0bda26 | 38 | } |
Cumulocity | 46:f6976fd64387 | 39 | _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _bootstrap.username(), _bootstrap.password()); |
Cumulocity | 46:f6976fd64387 | 40 | Thread::wait(5000); |
Cumulocity | 41:804f6a0bda26 | 41 | |
Cumulocity | 41:804f6a0bda26 | 42 | _io.lcdPrint("INTEGRATION"); |
Cumulocity | 41:804f6a0bda26 | 43 | if (!_integration.integrate()) { |
Cumulocity | 41:804f6a0bda26 | 44 | puts("Device integration process failed."); |
Cumulocity | 41:804f6a0bda26 | 45 | return false; |
Cumulocity | 41:804f6a0bda26 | 46 | } |
Cumulocity | 41:804f6a0bda26 | 47 | |
Cumulocity | 41:804f6a0bda26 | 48 | char status[60]; |
Cumulocity | 41:804f6a0bda26 | 49 | snprintf(status, sizeof(status), "ID: %ld", _deviceId); |
Cumulocity | 41:804f6a0bda26 | 50 | _io.lcdPrint("INTEGRATED", status); |
Cumulocity | 41:804f6a0bda26 | 51 | puts(status); |
Cumulocity | 41:804f6a0bda26 | 52 | |
Cumulocity | 41:804f6a0bda26 | 53 | loop(); |
Cumulocity | 41:804f6a0bda26 | 54 | return true; |
Cumulocity | 41:804f6a0bda26 | 55 | } |
Cumulocity | 41:804f6a0bda26 | 56 | |
Cumulocity | 41:804f6a0bda26 | 57 | void MbedAgent::loop() |
Cumulocity | 41:804f6a0bda26 | 58 | { |
Cumulocity | 41:804f6a0bda26 | 59 | Timer timer; |
Cumulocity | 41:804f6a0bda26 | 60 | |
Cumulocity | 41:804f6a0bda26 | 61 | timer.start(); |
Cumulocity | 41:804f6a0bda26 | 62 | while (true) { |
Cumulocity | 41:804f6a0bda26 | 63 | timer.reset(); |
Cumulocity | 41:804f6a0bda26 | 64 | |
Cumulocity | 41:804f6a0bda26 | 65 | _signalQualityMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 66 | _temperatureMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 67 | _accelerationMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 68 | |
Cumulocity | 41:804f6a0bda26 | 69 | while (timer.read() < MBED_AGENT_INTERVAL) { |
Cumulocity | 41:804f6a0bda26 | 70 | Thread::yield(); |
Cumulocity | 41:804f6a0bda26 | 71 | } |
Cumulocity | 41:804f6a0bda26 | 72 | } |
Cumulocity | 41:804f6a0bda26 | 73 | } |