![](/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
DeviceIntegration.cpp@116:5de54f09f754, 2015-05-20 (annotated)
- Committer:
- xinlei
- Date:
- Wed May 20 09:55:49 2015 +0000
- Revision:
- 116:5de54f09f754
- Parent:
- 109:b7a403dbceb6
- Child:
- 118:1b6c7d06d933
device bootstrap revamp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 92:0acd11870c6a | 1 | #include <stdio.h> |
Cumulocity | 41:804f6a0bda26 | 2 | #include "DeviceIntegration.h" |
Cumulocity | 41:804f6a0bda26 | 3 | #include "ComposedRecord.h" |
Cumulocity | 41:804f6a0bda26 | 4 | #include "CharValue.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "IntegerValue.h" |
xinlei | 100:dbcd3bc51758 | 6 | #include "SmartRestConf.h" |
xinlei | 72:c5709ae7b193 | 7 | #include "logging.h" |
Cumulocity | 41:804f6a0bda26 | 8 | |
Cumulocity | 41:804f6a0bda26 | 9 | bool DeviceIntegration::init() |
Cumulocity | 41:804f6a0bda26 | 10 | { |
Cumulocity | 41:804f6a0bda26 | 11 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 12 | return false; |
Cumulocity | 41:804f6a0bda26 | 13 | |
Cumulocity | 41:804f6a0bda26 | 14 | // get device by identity |
Cumulocity | 41:804f6a0bda26 | 15 | // Usage: 100,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 16 | if (!_tpl.add("10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n")) |
Cumulocity | 41:804f6a0bda26 | 17 | return false; |
Cumulocity | 41:804f6a0bda26 | 18 | |
Cumulocity | 41:804f6a0bda26 | 19 | // get device id from identity |
Cumulocity | 41:804f6a0bda26 | 20 | // Response: 200,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 21 | if (!_tpl.add("11,200,\"$.managedObject\",,\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 22 | return false; |
Cumulocity | 41:804f6a0bda26 | 23 | |
Cumulocity | 41:804f6a0bda26 | 24 | // Create device |
Cumulocity | 41:804f6a0bda26 | 25 | // Usage: 101,<SERIAL/NR> |
vwochnik | 69:237c01eb36c2 | 26 | if (!_tpl.add("10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedOperations\"\":[\"\"c8y_Relay\"\",\"\"c8y_Configuration\"\",\"\"c8y_Message\"\"],\"\"c8y_IsDevice\"\":{},\"\"com_cumulocity_model_Agent\"\":{}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 27 | return false; |
Cumulocity | 41:804f6a0bda26 | 28 | |
Cumulocity | 41:804f6a0bda26 | 29 | // Get device id |
Cumulocity | 41:804f6a0bda26 | 30 | // Response: 201,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 31 | if (!_tpl.add("11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 32 | return false; |
Cumulocity | 41:804f6a0bda26 | 33 | |
Cumulocity | 41:804f6a0bda26 | 34 | // Insert global ID |
Cumulocity | 41:804f6a0bda26 | 35 | // Usage: 102,<DEVICE/ID>,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 36 | if (!_tpl.add("10,102,POST,/identity/globalIds/%%/externalIds,application/vnd.com.nsn.cumulocity.externalId+json,application/vnd.com.nsn.cumulocity.externalId+json,%%,UNSIGNED STRING,\"{\"\"type\"\":\"\"c8y_Serial\"\",\"\"externalId\"\":\"\"%%\"\"}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 37 | return false; |
Cumulocity | 41:804f6a0bda26 | 38 | |
Cumulocity | 41:804f6a0bda26 | 39 | // Update IMEI, CellId and iccid |
Cumulocity | 41:804f6a0bda26 | 40 | // Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID> |
Cumulocity | 41:804f6a0bda26 | 41 | if (!_tpl.add("10,103,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING STRING STRING,\"{\"\"c8y_Mobile\"\":{\"\"imei\"\":\"\"%%\"\",\"\"cellId\"\":\"\"%%\"\",\"\"iccid\"\":\"\"%%\"\"}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 42 | return false; |
Cumulocity | 41:804f6a0bda26 | 43 | |
Cumulocity | 41:804f6a0bda26 | 44 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 45 | return true; |
Cumulocity | 41:804f6a0bda26 | 46 | } |
Cumulocity | 41:804f6a0bda26 | 47 | |
Cumulocity | 41:804f6a0bda26 | 48 | bool DeviceIntegration::integrate() |
Cumulocity | 41:804f6a0bda26 | 49 | { |
xinlei | 99:47ea098f8a47 | 50 | if (deviceID != 0) |
Cumulocity | 41:804f6a0bda26 | 51 | return false; |
Cumulocity | 41:804f6a0bda26 | 52 | |
Cumulocity | 41:804f6a0bda26 | 53 | // template bootstrapping process |
Cumulocity | 41:804f6a0bda26 | 54 | if (_client.bootstrap(_tpl) != SMARTREST_SUCCESS) { |
xinlei | 72:c5709ae7b193 | 55 | aError("Template bootstrap failed.\r\n"); |
Cumulocity | 49:ac0ba9d54ebc | 56 | return false; |
Cumulocity | 41:804f6a0bda26 | 57 | } |
Cumulocity | 41:804f6a0bda26 | 58 | |
vwochnik | 52:8f1370084268 | 59 | if ((!deviceExisting()) && ((!createDevice()) || (!addGlobalIdentifier()))) |
vwochnik | 52:8f1370084268 | 60 | return false; |
Cumulocity | 41:804f6a0bda26 | 61 | |
vwochnik | 52:8f1370084268 | 62 | if (!updateDevice()) |
Cumulocity | 41:804f6a0bda26 | 63 | return false; |
Cumulocity | 41:804f6a0bda26 | 64 | |
Cumulocity | 41:804f6a0bda26 | 65 | return true; |
Cumulocity | 41:804f6a0bda26 | 66 | } |
Cumulocity | 41:804f6a0bda26 | 67 | |
Cumulocity | 41:804f6a0bda26 | 68 | bool DeviceIntegration::deviceExisting() |
Cumulocity | 41:804f6a0bda26 | 69 | { |
Cumulocity | 41:804f6a0bda26 | 70 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 71 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 72 | |
Cumulocity | 41:804f6a0bda26 | 73 | IntegerValue msgId(100); |
Cumulocity | 41:804f6a0bda26 | 74 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 75 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 76 | return false; |
Cumulocity | 41:804f6a0bda26 | 77 | |
Cumulocity | 41:804f6a0bda26 | 78 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 79 | aWarning("Send failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 80 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 81 | return false; |
Cumulocity | 41:804f6a0bda26 | 82 | } |
Cumulocity | 41:804f6a0bda26 | 83 | |
Cumulocity | 41:804f6a0bda26 | 84 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 85 | aError("No device found.\n"); |
Cumulocity | 41:804f6a0bda26 | 86 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 87 | return false; |
Cumulocity | 41:804f6a0bda26 | 88 | } |
Cumulocity | 41:804f6a0bda26 | 89 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 90 | |
Cumulocity | 41:804f6a0bda26 | 91 | if (received.values() == 0) { |
Cumulocity | 41:804f6a0bda26 | 92 | return false; |
Cumulocity | 41:804f6a0bda26 | 93 | } |
Cumulocity | 41:804f6a0bda26 | 94 | |
Cumulocity | 41:804f6a0bda26 | 95 | if (received.value(0).integerValue() == 50) { |
Cumulocity | 41:804f6a0bda26 | 96 | return false; |
Cumulocity | 41:804f6a0bda26 | 97 | } |
Cumulocity | 41:804f6a0bda26 | 98 | |
Cumulocity | 41:804f6a0bda26 | 99 | if (received.value(0).integerValue() != 200) { |
xinlei | 92:0acd11870c6a | 100 | aError("Unknown message ID: %d.\n", received.value(0).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 101 | return false; |
Cumulocity | 41:804f6a0bda26 | 102 | } |
xinlei | 99:47ea098f8a47 | 103 | setDeviceID(received.value(2).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 104 | |
Cumulocity | 41:804f6a0bda26 | 105 | return true; |
Cumulocity | 41:804f6a0bda26 | 106 | } |
Cumulocity | 41:804f6a0bda26 | 107 | |
Cumulocity | 41:804f6a0bda26 | 108 | bool DeviceIntegration::createDevice() |
Cumulocity | 41:804f6a0bda26 | 109 | { |
Cumulocity | 41:804f6a0bda26 | 110 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 111 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 112 | |
xinlei | 92:0acd11870c6a | 113 | aInfo("Create device...\n"); |
Cumulocity | 41:804f6a0bda26 | 114 | |
Cumulocity | 41:804f6a0bda26 | 115 | IntegerValue msgId(101); |
Cumulocity | 41:804f6a0bda26 | 116 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 117 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 118 | return false; |
Cumulocity | 41:804f6a0bda26 | 119 | |
Cumulocity | 41:804f6a0bda26 | 120 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 121 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 122 | return 0; |
Cumulocity | 41:804f6a0bda26 | 123 | } |
Cumulocity | 41:804f6a0bda26 | 124 | |
Cumulocity | 41:804f6a0bda26 | 125 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 126 | aError("No device found.\n"); |
Cumulocity | 41:804f6a0bda26 | 127 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 128 | return false; |
Cumulocity | 41:804f6a0bda26 | 129 | } |
Cumulocity | 41:804f6a0bda26 | 130 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 131 | |
Cumulocity | 41:804f6a0bda26 | 132 | if (received.values() != 3) { |
xinlei | 92:0acd11870c6a | 133 | aError("createDevice: Incomplete response.\n"); |
Cumulocity | 41:804f6a0bda26 | 134 | return false; |
Cumulocity | 41:804f6a0bda26 | 135 | } |
Cumulocity | 41:804f6a0bda26 | 136 | |
Cumulocity | 41:804f6a0bda26 | 137 | if (received.value(0).integerValue() != 201) { |
xinlei | 92:0acd11870c6a | 138 | aError("createDevice: Unknown message ID: %d.\n", received.value(0).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 139 | return false; |
Cumulocity | 41:804f6a0bda26 | 140 | } |
Cumulocity | 41:804f6a0bda26 | 141 | |
xinlei | 99:47ea098f8a47 | 142 | setDeviceID(received.value(2).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 143 | return true; |
Cumulocity | 41:804f6a0bda26 | 144 | } |
Cumulocity | 41:804f6a0bda26 | 145 | |
Cumulocity | 41:804f6a0bda26 | 146 | bool DeviceIntegration::addGlobalIdentifier() |
Cumulocity | 41:804f6a0bda26 | 147 | { |
Cumulocity | 41:804f6a0bda26 | 148 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 149 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 150 | |
xinlei | 92:0acd11870c6a | 151 | aInfo("Add global identifier.\n"); |
Cumulocity | 41:804f6a0bda26 | 152 | |
Cumulocity | 41:804f6a0bda26 | 153 | IntegerValue msgId(102); |
xinlei | 99:47ea098f8a47 | 154 | IntegerValue devId(deviceID); |
Cumulocity | 41:804f6a0bda26 | 155 | CharValue imei(_deviceInfo.imei()); |
xinlei | 99:47ea098f8a47 | 156 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 157 | return false; |
Cumulocity | 41:804f6a0bda26 | 158 | |
Cumulocity | 41:804f6a0bda26 | 159 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 160 | aWarning("Adding global identifier failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 161 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 162 | return false; |
Cumulocity | 41:804f6a0bda26 | 163 | } |
Cumulocity | 41:804f6a0bda26 | 164 | |
Cumulocity | 41:804f6a0bda26 | 165 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 166 | aError("Receive identifier failed.\n"); |
Cumulocity | 41:804f6a0bda26 | 167 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 168 | return false; |
Cumulocity | 41:804f6a0bda26 | 169 | } |
Cumulocity | 41:804f6a0bda26 | 170 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 171 | |
Cumulocity | 41:804f6a0bda26 | 172 | if (received.values() != 3) { |
xinlei | 92:0acd11870c6a | 173 | aError("%d (3) values received.\n", received.values()); |
Cumulocity | 41:804f6a0bda26 | 174 | return false; |
Cumulocity | 41:804f6a0bda26 | 175 | } |
Cumulocity | 41:804f6a0bda26 | 176 | |
Cumulocity | 41:804f6a0bda26 | 177 | if (received.value(0).integerValue() != 200) { |
xinlei | 92:0acd11870c6a | 178 | aError("Unknown message ID: %d\n", received.value(0).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 179 | return false; |
Cumulocity | 41:804f6a0bda26 | 180 | } |
Cumulocity | 41:804f6a0bda26 | 181 | |
Cumulocity | 41:804f6a0bda26 | 182 | return true; |
Cumulocity | 41:804f6a0bda26 | 183 | } |
Cumulocity | 41:804f6a0bda26 | 184 | |
Cumulocity | 41:804f6a0bda26 | 185 | bool DeviceIntegration::updateDevice() |
Cumulocity | 41:804f6a0bda26 | 186 | { |
Cumulocity | 41:804f6a0bda26 | 187 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 188 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 189 | |
Cumulocity | 41:804f6a0bda26 | 190 | IntegerValue msgId(103); |
xinlei | 99:47ea098f8a47 | 191 | IntegerValue devId(deviceID); |
Cumulocity | 41:804f6a0bda26 | 192 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 193 | CharValue cellId(_deviceInfo.cellId()); |
Cumulocity | 41:804f6a0bda26 | 194 | CharValue iccid(_deviceInfo.iccid()); |
xinlei | 93:61d44636f020 | 195 | aInfo("Update Device.\n"); |
xinlei | 99:47ea098f8a47 | 196 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(imei)) || (!record.add(cellId)) || (!record.add(iccid))) |
Cumulocity | 41:804f6a0bda26 | 197 | return false; |
Cumulocity | 41:804f6a0bda26 | 198 | |
Cumulocity | 41:804f6a0bda26 | 199 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 200 | aError("Send.\n"); |
Cumulocity | 41:804f6a0bda26 | 201 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 202 | return false; |
Cumulocity | 41:804f6a0bda26 | 203 | } |
Cumulocity | 41:804f6a0bda26 | 204 | |
Cumulocity | 41:804f6a0bda26 | 205 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 206 | aError("Receive.\n"); |
Cumulocity | 41:804f6a0bda26 | 207 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 208 | return false; |
Cumulocity | 41:804f6a0bda26 | 209 | } |
Cumulocity | 41:804f6a0bda26 | 210 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 211 | |
Cumulocity | 41:804f6a0bda26 | 212 | if (received.values() != 3) { |
xinlei | 92:0acd11870c6a | 213 | aError("%d (3) values received.\n", received.values()); |
Cumulocity | 41:804f6a0bda26 | 214 | return false; |
Cumulocity | 41:804f6a0bda26 | 215 | } |
Cumulocity | 41:804f6a0bda26 | 216 | |
Cumulocity | 41:804f6a0bda26 | 217 | if (received.value(0).integerValue() != 201) { |
xinlei | 92:0acd11870c6a | 218 | aError("Unknown message ID: %d\n", received.value(0).integerValue()); |
Cumulocity | 41:804f6a0bda26 | 219 | return false; |
Cumulocity | 41:804f6a0bda26 | 220 | } |
Cumulocity | 41:804f6a0bda26 | 221 | |
Cumulocity | 41:804f6a0bda26 | 222 | return true; |
Cumulocity | 41:804f6a0bda26 | 223 | } |