demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
IothubRobotArm.cpp@33:8b9dcbf6d8ec, 2016-02-04 (annotated)
- Committer:
- henryrawas
- Date:
- Thu Feb 04 20:10:33 2016 +0000
- Revision:
- 33:8b9dcbf6d8ec
- Parent:
- 27:4239713d9690
update libs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryrawas | 4:36a4eceb1b7f | 1 | // Copyright (c) Microsoft. All rights reserved. |
henryrawas | 4:36a4eceb1b7f | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
henryrawas | 4:36a4eceb1b7f | 3 | |
henryrawas | 4:36a4eceb1b7f | 4 | #include "mbed.h" |
henryrawas | 4:36a4eceb1b7f | 5 | #include "rtos.h" |
henryrawas | 4:36a4eceb1b7f | 6 | |
henryrawas | 10:9b21566a5ddb | 7 | #include "EthernetInterface.h" |
henryrawas | 10:9b21566a5ddb | 8 | #include "mbed/logging.h" |
henryrawas | 10:9b21566a5ddb | 9 | #include "mbed/mbedtime.h" |
henryrawas | 10:9b21566a5ddb | 10 | #include <NTPClient.h> |
henryrawas | 10:9b21566a5ddb | 11 | |
henryrawas | 21:051751f9ca9e | 12 | #include "iothub_mod_client.h" |
henryrawas | 4:36a4eceb1b7f | 13 | #include "iothub_message.h" |
henryrawas | 13:ffeff9b5e513 | 14 | #include "threadapi.h" |
henryrawas | 4:36a4eceb1b7f | 15 | #include "crt_abstractions.h" |
henryrawas | 4:36a4eceb1b7f | 16 | #include "iothubtransportamqp.h" |
henryrawas | 18:224289104fc0 | 17 | |
henryrawas | 4:36a4eceb1b7f | 18 | #include "MeasureBuf.h" |
henryrawas | 4:36a4eceb1b7f | 19 | #include "IothubRobotArm.h" |
henryrawas | 4:36a4eceb1b7f | 20 | #include "IothubSerial.h" |
henryrawas | 4:36a4eceb1b7f | 21 | |
henryrawas | 4:36a4eceb1b7f | 22 | #ifdef MBED_BUILD_TIMESTAMP |
henryrawas | 4:36a4eceb1b7f | 23 | #include "certs.h" |
henryrawas | 4:36a4eceb1b7f | 24 | #endif // MBED_BUILD_TIMESTAMP |
henryrawas | 4:36a4eceb1b7f | 25 | |
henryrawas | 27:4239713d9690 | 26 | #include "RobotArmCfg.h" |
henryrawas | 5:36916b1c5a06 | 27 | |
henryrawas | 10:9b21566a5ddb | 28 | extern void ShowLedColor(int col); |
henryrawas | 10:9b21566a5ddb | 29 | |
henryrawas | 15:4bd10f531cdc | 30 | // method to initialize platform - Time of day |
henryrawas | 10:9b21566a5ddb | 31 | int setupRealTime(void) |
henryrawas | 10:9b21566a5ddb | 32 | { |
henryrawas | 10:9b21566a5ddb | 33 | int result; |
henryrawas | 10:9b21566a5ddb | 34 | |
henryrawas | 10:9b21566a5ddb | 35 | (void)printf("setupRealTime begin\r\n"); |
henryrawas | 10:9b21566a5ddb | 36 | if (EthernetInterface::connect()) |
henryrawas | 10:9b21566a5ddb | 37 | { |
henryrawas | 10:9b21566a5ddb | 38 | (void)printf("Error initializing EthernetInterface.\r\n"); |
henryrawas | 10:9b21566a5ddb | 39 | result = __LINE__; |
henryrawas | 10:9b21566a5ddb | 40 | } |
henryrawas | 10:9b21566a5ddb | 41 | else |
henryrawas | 10:9b21566a5ddb | 42 | { |
henryrawas | 10:9b21566a5ddb | 43 | (void)printf("setupRealTime NTP begin\r\n"); |
henryrawas | 10:9b21566a5ddb | 44 | NTPClient ntp; |
henryrawas | 10:9b21566a5ddb | 45 | if (ntp.setTime("0.pool.ntp.org") != 0) |
henryrawas | 10:9b21566a5ddb | 46 | { |
henryrawas | 10:9b21566a5ddb | 47 | (void)printf("Failed setting time.\r\n"); |
henryrawas | 10:9b21566a5ddb | 48 | result = __LINE__; |
henryrawas | 10:9b21566a5ddb | 49 | } |
henryrawas | 10:9b21566a5ddb | 50 | else |
henryrawas | 10:9b21566a5ddb | 51 | { |
henryrawas | 10:9b21566a5ddb | 52 | (void)printf("set time correctly!\r\n"); |
henryrawas | 10:9b21566a5ddb | 53 | result = 0; |
henryrawas | 10:9b21566a5ddb | 54 | } |
henryrawas | 10:9b21566a5ddb | 55 | (void)printf("setupRealTime NTP end\r\n"); |
henryrawas | 10:9b21566a5ddb | 56 | EthernetInterface::disconnect(); |
henryrawas | 10:9b21566a5ddb | 57 | } |
henryrawas | 10:9b21566a5ddb | 58 | (void)printf("setupRealTime end\r\n"); |
henryrawas | 10:9b21566a5ddb | 59 | |
henryrawas | 10:9b21566a5ddb | 60 | return result; |
henryrawas | 10:9b21566a5ddb | 61 | } |
henryrawas | 10:9b21566a5ddb | 62 | |
henryrawas | 15:4bd10f531cdc | 63 | // method to initialize platform - Ethernet |
henryrawas | 10:9b21566a5ddb | 64 | int InitEthernet() |
henryrawas | 10:9b21566a5ddb | 65 | { |
henryrawas | 10:9b21566a5ddb | 66 | (void)printf("Initializing ethernet..\r\n"); |
henryrawas | 10:9b21566a5ddb | 67 | |
henryrawas | 10:9b21566a5ddb | 68 | /* These are needed in order to initialize the time provider for Proton-C */ |
henryrawas | 10:9b21566a5ddb | 69 | mbedtime_init(); |
henryrawas | 10:9b21566a5ddb | 70 | |
henryrawas | 10:9b21566a5ddb | 71 | if (EthernetInterface::init()) |
henryrawas | 10:9b21566a5ddb | 72 | { |
henryrawas | 10:9b21566a5ddb | 73 | (void)printf("Error initializing EthernetInterface.\r\n"); |
henryrawas | 10:9b21566a5ddb | 74 | return -1; |
henryrawas | 10:9b21566a5ddb | 75 | } |
henryrawas | 10:9b21566a5ddb | 76 | |
henryrawas | 10:9b21566a5ddb | 77 | if (setupRealTime() != 0) |
henryrawas | 10:9b21566a5ddb | 78 | { |
henryrawas | 10:9b21566a5ddb | 79 | (void)printf("Failed setting up real time clock\r\n"); |
henryrawas | 10:9b21566a5ddb | 80 | return -1; |
henryrawas | 10:9b21566a5ddb | 81 | } |
henryrawas | 10:9b21566a5ddb | 82 | |
henryrawas | 10:9b21566a5ddb | 83 | if (EthernetInterface::connect()) |
henryrawas | 10:9b21566a5ddb | 84 | { |
henryrawas | 10:9b21566a5ddb | 85 | (void)printf("Error connecting EthernetInterface.\r\n"); |
henryrawas | 10:9b21566a5ddb | 86 | return -1; |
henryrawas | 10:9b21566a5ddb | 87 | } |
henryrawas | 10:9b21566a5ddb | 88 | |
henryrawas | 10:9b21566a5ddb | 89 | return 0; |
henryrawas | 10:9b21566a5ddb | 90 | } |
henryrawas | 10:9b21566a5ddb | 91 | |
henryrawas | 15:4bd10f531cdc | 92 | // IoTHub SDK stuff |
henryrawas | 4:36a4eceb1b7f | 93 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); |
henryrawas | 4:36a4eceb1b7f | 94 | |
henryrawas | 4:36a4eceb1b7f | 95 | typedef struct EVENT_INSTANCE_TAG |
henryrawas | 4:36a4eceb1b7f | 96 | { |
henryrawas | 4:36a4eceb1b7f | 97 | IOTHUB_MESSAGE_HANDLE messageHandle; |
henryrawas | 4:36a4eceb1b7f | 98 | int messageTrackingId; // For tracking the messages within the user callback. |
henryrawas | 4:36a4eceb1b7f | 99 | } EVENT_INSTANCE; |
henryrawas | 4:36a4eceb1b7f | 100 | |
henryrawas | 13:ffeff9b5e513 | 101 | |
henryrawas | 5:36916b1c5a06 | 102 | // message buffers to use |
henryrawas | 13:ffeff9b5e513 | 103 | #define MESSAGE_LEN 1024 |
henryrawas | 5:36916b1c5a06 | 104 | static char msgText[MESSAGE_LEN]; |
henryrawas | 13:ffeff9b5e513 | 105 | |
henryrawas | 15:4bd10f531cdc | 106 | // a larger MESSAGE_COUNT results in being able to send as data is available |
henryrawas | 15:4bd10f531cdc | 107 | // but requires more heap space to hold buffers |
henryrawas | 21:051751f9ca9e | 108 | #define MESSAGE_COUNT 4 |
henryrawas | 13:ffeff9b5e513 | 109 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
henryrawas | 7:6723f6887d00 | 110 | |
henryrawas | 5:36916b1c5a06 | 111 | |
henryrawas | 5:36916b1c5a06 | 112 | // context for send & receive |
henryrawas | 5:36916b1c5a06 | 113 | static int receiveContext; |
henryrawas | 5:36916b1c5a06 | 114 | static int callbackCounter; |
henryrawas | 5:36916b1c5a06 | 115 | static int msgNumber; |
henryrawas | 5:36916b1c5a06 | 116 | |
henryrawas | 5:36916b1c5a06 | 117 | static IOTHUB_CLIENT_HANDLE iotHubClientHandle; |
henryrawas | 5:36916b1c5a06 | 118 | |
henryrawas | 10:9b21566a5ddb | 119 | // used to detect send confirmation timeout |
henryrawas | 10:9b21566a5ddb | 120 | #define SEND_CONFIRM_TO 30000 |
henryrawas | 10:9b21566a5ddb | 121 | RtosTimer* confirmTimer; |
henryrawas | 10:9b21566a5ddb | 122 | |
henryrawas | 21:051751f9ca9e | 123 | // object for IoTHub interface |
henryrawas | 21:051751f9ca9e | 124 | IothubRobotArm iotRobot; |
henryrawas | 21:051751f9ca9e | 125 | |
henryrawas | 10:9b21566a5ddb | 126 | |
henryrawas | 15:4bd10f531cdc | 127 | // pass received commands to device |
henryrawas | 14:570c8071f577 | 128 | extern void ControlArmCommands(const char* cmd); |
henryrawas | 5:36916b1c5a06 | 129 | |
henryrawas | 15:4bd10f531cdc | 130 | // callback for incoming messages |
henryrawas | 4:36a4eceb1b7f | 131 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
henryrawas | 4:36a4eceb1b7f | 132 | { |
henryrawas | 4:36a4eceb1b7f | 133 | int* counter = (int*)userContextCallback; |
henryrawas | 4:36a4eceb1b7f | 134 | const char* buffer; |
henryrawas | 4:36a4eceb1b7f | 135 | size_t size; |
henryrawas | 5:36916b1c5a06 | 136 | char cmdbuf[20]; |
henryrawas | 5:36916b1c5a06 | 137 | |
henryrawas | 4:36a4eceb1b7f | 138 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK) |
henryrawas | 4:36a4eceb1b7f | 139 | { |
henryrawas | 5:36916b1c5a06 | 140 | int slen = size; |
henryrawas | 13:ffeff9b5e513 | 141 | |
henryrawas | 5:36916b1c5a06 | 142 | if (size >= 20) |
henryrawas | 5:36916b1c5a06 | 143 | slen = 19; |
henryrawas | 5:36916b1c5a06 | 144 | strncpy(cmdbuf, buffer, slen); |
henryrawas | 5:36916b1c5a06 | 145 | cmdbuf[slen] = 0; |
henryrawas | 13:ffeff9b5e513 | 146 | (void)printf(cmdbuf); |
henryrawas | 13:ffeff9b5e513 | 147 | (void)printf("\r\n"); |
henryrawas | 14:570c8071f577 | 148 | ControlArmCommands((const char*)cmdbuf); |
henryrawas | 4:36a4eceb1b7f | 149 | } |
henryrawas | 4:36a4eceb1b7f | 150 | |
henryrawas | 4:36a4eceb1b7f | 151 | (*counter)++; |
henryrawas | 4:36a4eceb1b7f | 152 | return IOTHUBMESSAGE_ACCEPTED; |
henryrawas | 4:36a4eceb1b7f | 153 | } |
henryrawas | 4:36a4eceb1b7f | 154 | |
henryrawas | 15:4bd10f531cdc | 155 | // callback for completion of sent messages |
henryrawas | 4:36a4eceb1b7f | 156 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
henryrawas | 4:36a4eceb1b7f | 157 | { |
henryrawas | 4:36a4eceb1b7f | 158 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
henryrawas | 11:3a2e6eb9fbb8 | 159 | if (eventInstance == NULL) |
henryrawas | 11:3a2e6eb9fbb8 | 160 | { |
henryrawas | 11:3a2e6eb9fbb8 | 161 | printf("SendConfirmation with NULL context\r\n"); |
henryrawas | 11:3a2e6eb9fbb8 | 162 | return; |
henryrawas | 11:3a2e6eb9fbb8 | 163 | } |
henryrawas | 13:ffeff9b5e513 | 164 | |
henryrawas | 13:ffeff9b5e513 | 165 | (void)printf("Confirmation received\r\n"); |
henryrawas | 7:6723f6887d00 | 166 | |
henryrawas | 10:9b21566a5ddb | 167 | confirmTimer->stop(); |
henryrawas | 4:36a4eceb1b7f | 168 | callbackCounter++; |
henryrawas | 4:36a4eceb1b7f | 169 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
henryrawas | 4:36a4eceb1b7f | 170 | } |
henryrawas | 4:36a4eceb1b7f | 171 | |
henryrawas | 21:051751f9ca9e | 172 | |
henryrawas | 21:051751f9ca9e | 173 | static void SendCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, void* userContextCallback) |
henryrawas | 21:051751f9ca9e | 174 | { |
henryrawas | 21:051751f9ca9e | 175 | iotRobot.SendMessage(iotHubClientHandle, userContextCallback); |
henryrawas | 21:051751f9ca9e | 176 | } |
henryrawas | 21:051751f9ca9e | 177 | |
henryrawas | 10:9b21566a5ddb | 178 | // communication timeout |
henryrawas | 10:9b21566a5ddb | 179 | void CommunicationTO(void const * tid) |
henryrawas | 10:9b21566a5ddb | 180 | { |
henryrawas | 10:9b21566a5ddb | 181 | (void)printf("*** Iothub thread communication Timeout\r\n"); |
henryrawas | 10:9b21566a5ddb | 182 | ShowLedColor(2); |
henryrawas | 10:9b21566a5ddb | 183 | } |
henryrawas | 10:9b21566a5ddb | 184 | |
henryrawas | 21:051751f9ca9e | 185 | // entry point to start IoTHub connection |
henryrawas | 4:36a4eceb1b7f | 186 | bool StartIothubThread() |
henryrawas | 4:36a4eceb1b7f | 187 | { |
henryrawas | 13:ffeff9b5e513 | 188 | InitEthernet(); |
henryrawas | 13:ffeff9b5e513 | 189 | |
henryrawas | 21:051751f9ca9e | 190 | confirmTimer = new RtosTimer(CommunicationTO, osTimerOnce, (void *)osThreadGetId()); |
henryrawas | 21:051751f9ca9e | 191 | |
henryrawas | 21:051751f9ca9e | 192 | iotRobot.Init(); |
henryrawas | 13:ffeff9b5e513 | 193 | |
henryrawas | 4:36a4eceb1b7f | 194 | return true; |
henryrawas | 4:36a4eceb1b7f | 195 | } |
henryrawas | 4:36a4eceb1b7f | 196 | |
henryrawas | 21:051751f9ca9e | 197 | // stop IoTHub connection |
henryrawas | 4:36a4eceb1b7f | 198 | void EndIothubThread() |
henryrawas | 4:36a4eceb1b7f | 199 | { |
henryrawas | 21:051751f9ca9e | 200 | iotRobot.Terminate(); |
henryrawas | 4:36a4eceb1b7f | 201 | } |
henryrawas | 4:36a4eceb1b7f | 202 | |
henryrawas | 21:051751f9ca9e | 203 | // IoTHub connection for RobotArm |
henryrawas | 21:051751f9ca9e | 204 | // sends and receives messages between RobotArm and IoTHub |
henryrawas | 4:36a4eceb1b7f | 205 | IothubRobotArm::IothubRobotArm() |
henryrawas | 4:36a4eceb1b7f | 206 | { |
henryrawas | 4:36a4eceb1b7f | 207 | iotHubClientHandle = NULL; |
henryrawas | 4:36a4eceb1b7f | 208 | } |
henryrawas | 4:36a4eceb1b7f | 209 | |
henryrawas | 4:36a4eceb1b7f | 210 | bool IothubRobotArm::Init() |
henryrawas | 4:36a4eceb1b7f | 211 | { |
henryrawas | 5:36916b1c5a06 | 212 | receiveContext = 0; |
henryrawas | 4:36a4eceb1b7f | 213 | callbackCounter = 0; |
henryrawas | 4:36a4eceb1b7f | 214 | msgNumber = 0; |
henryrawas | 4:36a4eceb1b7f | 215 | |
henryrawas | 24:f6aa0e64c5e1 | 216 | (void)printf("Starting the IoTHub RobotArm connection\r\n"); |
henryrawas | 24:f6aa0e64c5e1 | 217 | |
henryrawas | 24:f6aa0e64c5e1 | 218 | if (connectionString[0] == '[') |
henryrawas | 24:f6aa0e64c5e1 | 219 | { |
henryrawas | 24:f6aa0e64c5e1 | 220 | // forgot to replace placeholder |
henryrawas | 24:f6aa0e64c5e1 | 221 | (void)printf("ERROR: connectionString is not set to a device connection!\r\n"); |
henryrawas | 24:f6aa0e64c5e1 | 222 | return false; |
henryrawas | 24:f6aa0e64c5e1 | 223 | } |
henryrawas | 4:36a4eceb1b7f | 224 | |
henryrawas | 21:051751f9ca9e | 225 | if ((iotHubClientHandle = IoTHubClient_Mod_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL) |
henryrawas | 4:36a4eceb1b7f | 226 | { |
henryrawas | 4:36a4eceb1b7f | 227 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
henryrawas | 4:36a4eceb1b7f | 228 | return false; |
henryrawas | 4:36a4eceb1b7f | 229 | } |
henryrawas | 4:36a4eceb1b7f | 230 | else |
henryrawas | 4:36a4eceb1b7f | 231 | { |
henryrawas | 4:36a4eceb1b7f | 232 | #ifdef MBED_BUILD_TIMESTAMP |
henryrawas | 4:36a4eceb1b7f | 233 | // For mbed add the certificate information |
henryrawas | 21:051751f9ca9e | 234 | if (IoTHubClient_Mod_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
henryrawas | 4:36a4eceb1b7f | 235 | { |
henryrawas | 11:3a2e6eb9fbb8 | 236 | printf("failure to set option \"TrustedCerts\"\r\n"); |
henryrawas | 4:36a4eceb1b7f | 237 | return false; |
henryrawas | 4:36a4eceb1b7f | 238 | } |
henryrawas | 4:36a4eceb1b7f | 239 | #endif // MBED_BUILD_TIMESTAMP |
henryrawas | 4:36a4eceb1b7f | 240 | |
henryrawas | 4:36a4eceb1b7f | 241 | /* Setting Message call back, so we can receive Commands. */ |
henryrawas | 21:051751f9ca9e | 242 | if (IoTHubClient_Mod_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
henryrawas | 4:36a4eceb1b7f | 243 | { |
henryrawas | 4:36a4eceb1b7f | 244 | (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n"); |
henryrawas | 4:36a4eceb1b7f | 245 | return false; |
henryrawas | 4:36a4eceb1b7f | 246 | } |
henryrawas | 4:36a4eceb1b7f | 247 | else |
henryrawas | 4:36a4eceb1b7f | 248 | { |
henryrawas | 4:36a4eceb1b7f | 249 | (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n"); |
henryrawas | 4:36a4eceb1b7f | 250 | } |
henryrawas | 21:051751f9ca9e | 251 | |
henryrawas | 21:051751f9ca9e | 252 | /* Setting Send call back, so we can send from worker thread. */ |
henryrawas | 21:051751f9ca9e | 253 | if (IoTHubClient_Mod_SetSendCallback(iotHubClientHandle, SendCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
henryrawas | 21:051751f9ca9e | 254 | { |
henryrawas | 21:051751f9ca9e | 255 | (void)printf("ERROR: IoTHubClient_SetSendCallback..........FAILED!\r\n"); |
henryrawas | 21:051751f9ca9e | 256 | return false; |
henryrawas | 21:051751f9ca9e | 257 | } |
henryrawas | 21:051751f9ca9e | 258 | else |
henryrawas | 21:051751f9ca9e | 259 | { |
henryrawas | 21:051751f9ca9e | 260 | (void)printf("IoTHubClient_SetSendCallback...successful.\r\n"); |
henryrawas | 21:051751f9ca9e | 261 | } |
henryrawas | 4:36a4eceb1b7f | 262 | } |
henryrawas | 4:36a4eceb1b7f | 263 | return true; |
henryrawas | 4:36a4eceb1b7f | 264 | } |
henryrawas | 4:36a4eceb1b7f | 265 | |
henryrawas | 4:36a4eceb1b7f | 266 | void IothubRobotArm::Terminate() |
henryrawas | 4:36a4eceb1b7f | 267 | { |
henryrawas | 4:36a4eceb1b7f | 268 | if (iotHubClientHandle != NULL) |
henryrawas | 4:36a4eceb1b7f | 269 | { |
henryrawas | 21:051751f9ca9e | 270 | IoTHubClient_Mod_Destroy(iotHubClientHandle); |
henryrawas | 4:36a4eceb1b7f | 271 | iotHubClientHandle = NULL; |
henryrawas | 4:36a4eceb1b7f | 272 | } |
henryrawas | 4:36a4eceb1b7f | 273 | } |
henryrawas | 4:36a4eceb1b7f | 274 | |
henryrawas | 21:051751f9ca9e | 275 | // Invoked from callback |
henryrawas | 21:051751f9ca9e | 276 | void IothubRobotArm::SendMessage(IOTHUB_CLIENT_HANDLE iotHubClient, void* userContextCallback) |
henryrawas | 4:36a4eceb1b7f | 277 | { |
henryrawas | 5:36916b1c5a06 | 278 | // send until circular buf empty or no sending buffers avail |
henryrawas | 5:36916b1c5a06 | 279 | // may drop message if confirmations are slow |
henryrawas | 5:36916b1c5a06 | 280 | while (msgNumber - callbackCounter < MESSAGE_COUNT) |
henryrawas | 5:36916b1c5a06 | 281 | { |
henryrawas | 5:36916b1c5a06 | 282 | // get buffer from array |
henryrawas | 5:36916b1c5a06 | 283 | int i = msgNumber % MESSAGE_COUNT; |
henryrawas | 4:36a4eceb1b7f | 284 | |
henryrawas | 7:6723f6887d00 | 285 | int msglen = 0; |
henryrawas | 13:ffeff9b5e513 | 286 | |
henryrawas | 7:6723f6887d00 | 287 | // get alert if any, otherwise get measure data |
henryrawas | 18:224289104fc0 | 288 | msglen = _msgSerialize.AlertBufToString(msgText, MESSAGE_LEN); |
henryrawas | 7:6723f6887d00 | 289 | if (msglen == 0) |
henryrawas | 7:6723f6887d00 | 290 | { |
henryrawas | 18:224289104fc0 | 291 | msglen = _msgSerialize.MeasureBufToString(msgText, MESSAGE_LEN); |
henryrawas | 7:6723f6887d00 | 292 | } |
henryrawas | 7:6723f6887d00 | 293 | |
henryrawas | 4:36a4eceb1b7f | 294 | if (msglen > 0) |
henryrawas | 4:36a4eceb1b7f | 295 | { |
henryrawas | 4:36a4eceb1b7f | 296 | if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, msglen)) == NULL) |
henryrawas | 4:36a4eceb1b7f | 297 | { |
henryrawas | 4:36a4eceb1b7f | 298 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
henryrawas | 4:36a4eceb1b7f | 299 | } |
henryrawas | 4:36a4eceb1b7f | 300 | else |
henryrawas | 4:36a4eceb1b7f | 301 | { |
henryrawas | 4:36a4eceb1b7f | 302 | messages[i].messageTrackingId = msgNumber; |
henryrawas | 21:051751f9ca9e | 303 | |
henryrawas | 10:9b21566a5ddb | 304 | confirmTimer->stop(); |
henryrawas | 21:051751f9ca9e | 305 | if (IoTHubClient_Mod_SendEventAsync(iotHubClient, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) |
henryrawas | 4:36a4eceb1b7f | 306 | { |
henryrawas | 4:36a4eceb1b7f | 307 | (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n"); |
henryrawas | 4:36a4eceb1b7f | 308 | } |
henryrawas | 4:36a4eceb1b7f | 309 | else |
henryrawas | 4:36a4eceb1b7f | 310 | { |
henryrawas | 10:9b21566a5ddb | 311 | confirmTimer->start(SEND_CONFIRM_TO); |
henryrawas | 13:ffeff9b5e513 | 312 | (void)printf("Send async completed\r\n"); |
henryrawas | 4:36a4eceb1b7f | 313 | } |
henryrawas | 4:36a4eceb1b7f | 314 | msgNumber++; |
henryrawas | 4:36a4eceb1b7f | 315 | } |
henryrawas | 4:36a4eceb1b7f | 316 | } |
henryrawas | 4:36a4eceb1b7f | 317 | else if (msglen == 0) |
henryrawas | 4:36a4eceb1b7f | 318 | { |
henryrawas | 13:ffeff9b5e513 | 319 | break; |
henryrawas | 4:36a4eceb1b7f | 320 | } |
henryrawas | 4:36a4eceb1b7f | 321 | else if (msglen < 0) |
henryrawas | 4:36a4eceb1b7f | 322 | { |
henryrawas | 4:36a4eceb1b7f | 323 | (void)printf("ERROR: Serialized message too big for buffer\r\n"); |
henryrawas | 4:36a4eceb1b7f | 324 | break; |
henryrawas | 4:36a4eceb1b7f | 325 | } |
henryrawas | 4:36a4eceb1b7f | 326 | } // while |
henryrawas | 4:36a4eceb1b7f | 327 | |
henryrawas | 4:36a4eceb1b7f | 328 | } |
henryrawas | 7:6723f6887d00 | 329 |