demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
Diff: IothubRobotArm.cpp
- Revision:
- 21:051751f9ca9e
- Parent:
- 18:224289104fc0
- Child:
- 23:381b6c77a6a0
--- a/IothubRobotArm.cpp Tue Jan 26 17:34:51 2016 +0000 +++ b/IothubRobotArm.cpp Tue Jan 26 20:19:43 2016 +0000 @@ -9,7 +9,7 @@ #include "mbed/mbedtime.h" #include <NTPClient.h> -#include "iothub_client.h" +#include "iothub_mod_client.h" #include "iothub_message.h" #include "threadapi.h" #include "crt_abstractions.h" @@ -107,7 +107,7 @@ // a larger MESSAGE_COUNT results in being able to send as data is available // but requires more heap space to hold buffers -#define MESSAGE_COUNT 2 +#define MESSAGE_COUNT 4 EVENT_INSTANCE messages[MESSAGE_COUNT]; // sending thread timeout @@ -125,6 +125,9 @@ #define SEND_CONFIRM_TO 30000 RtosTimer* confirmTimer; +// object for IoTHub interface +IothubRobotArm iotRobot; + // pass received commands to device extern void ControlArmCommands(const char* cmd); @@ -171,6 +174,12 @@ IoTHubMessage_Destroy(eventInstance->messageHandle); } + +static void SendCallback(IOTHUB_CLIENT_HANDLE iotHubClientHandle, void* userContextCallback) +{ + iotRobot.SendMessage(iotHubClientHandle, userContextCallback); +} + // communication timeout void CommunicationTO(void const * tid) { @@ -178,59 +187,26 @@ ShowLedColor(2); } - -// IoT Hub thread -static THREAD_HANDLE IotThread; -static bool IotThreadClose; - -// entry point for ITHub sending thread -int IothubThread(void *args) -{ - (void)printf("Iothub thread start\r\n"); - IotThreadClose = false; - IothubRobotArm iotRobot; - - confirmTimer = new RtosTimer(CommunicationTO, osTimerOnce, (void *)osThreadGetId()); - - iotRobot.Init(); - // wait for connection establishment for SSL - ThreadAPI_Sleep(15000); - - while (1) - { - if (IotThreadClose) - { - (void)printf("Iothub thread close\r\n"); - iotRobot.Terminate(); - break; - } - else - { - iotRobot.SendMessage(); - } - ThreadAPI_Sleep(SEND_POLL_MS); - } - - return 0; -} - - +// entry point to start IoTHub connection bool StartIothubThread() { InitEthernet(); - ThreadAPI_Create(&IotThread, IothubThread, NULL); + confirmTimer = new RtosTimer(CommunicationTO, osTimerOnce, (void *)osThreadGetId()); + + iotRobot.Init(); return true; } - +// stop IoTHub connection void EndIothubThread() { - IotThreadClose = true; + iotRobot.Terminate(); } - +// IoTHub connection for RobotArm +// sends and receives messages between RobotArm and IoTHub IothubRobotArm::IothubRobotArm() { iotHubClientHandle = NULL; @@ -244,7 +220,7 @@ (void)printf("Starting the IoTHub RobotArm sample AMQP...\r\n"); - if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL) + if ((iotHubClientHandle = IoTHubClient_Mod_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL) { (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); return false; @@ -254,16 +230,15 @@ #ifdef MBED_BUILD_TIMESTAMP (void)printf("INFO: IoTHubClient_SetOption\r\n"); // For mbed add the certificate information - if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + if (IoTHubClient_Mod_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) { printf("failure to set option \"TrustedCerts\"\r\n"); return false; } #endif // MBED_BUILD_TIMESTAMP - (void)printf("INFO: IoTHubClient_SetMessageCallback\r\n"); /* Setting Message call back, so we can receive Commands. */ - if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) + if (IoTHubClient_Mod_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) { (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n"); return false; @@ -272,6 +247,17 @@ { (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n"); } + + /* Setting Send call back, so we can send from worker thread. */ + if (IoTHubClient_Mod_SetSendCallback(iotHubClientHandle, SendCallback, &receiveContext) != IOTHUB_CLIENT_OK) + { + (void)printf("ERROR: IoTHubClient_SetSendCallback..........FAILED!\r\n"); + return false; + } + else + { + (void)printf("IoTHubClient_SetSendCallback...successful.\r\n"); + } } return true; } @@ -280,13 +266,13 @@ { if (iotHubClientHandle != NULL) { - IoTHubClient_Destroy(iotHubClientHandle); + IoTHubClient_Mod_Destroy(iotHubClientHandle); iotHubClientHandle = NULL; } } - -void IothubRobotArm::SendMessage(void) +// Invoked from callback +void IothubRobotArm::SendMessage(IOTHUB_CLIENT_HANDLE iotHubClient, void* userContextCallback) { // send until circular buf empty or no sending buffers avail // may drop message if confirmations are slow @@ -313,9 +299,9 @@ else { messages[i].messageTrackingId = msgNumber; - + confirmTimer->stop(); - if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) + if (IoTHubClient_Mod_SendEventAsync(iotHubClient, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) { (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n"); }