rh li / Mbed OS f767zi_mqtt

Dependencies:   azure_c_shared_utility azure_umqtt_c iothub_client iothub_mqtt_transport serializer wolfSSL

Fork of simplesample_mqtt by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers simplesample_mqtt.c Source File

simplesample_mqtt.c

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include <stdlib.h>
00005 
00006 #include <stdio.h>
00007 #include <stdint.h>
00008 
00009 /* This sample uses the _LL APIs of iothub_client for example purposes.
00010 That does not mean that MQTT only works with the _LL APIs.
00011 Simply changing the using the convenience layer (functions not having _LL)
00012 and removing calls to _DoWork will yield the same results. */
00013 
00014 #ifdef ARDUINO
00015 #include "AzureIoTHub.h"
00016 #else
00017 #include "azure_c_shared_utility/threadapi.h"
00018 #include "azure_c_shared_utility/platform.h"
00019 #include "serializer.h"
00020 #include "iothub_client_ll.h"
00021 #include "iothubtransportmqtt.h"
00022 #endif
00023 
00024 #ifdef MBED_BUILD_TIMESTAMP
00025 #include "certs.h"
00026 #endif // MBED_BUILD_TIMESTAMP
00027 
00028 
00029 /*String containing Hostname, Device Id & Device Key in the format:             */
00030 /*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"    */
00031 static const char* connectionString = "[device connection string]";
00032 
00033 // Define the Model
00034 BEGIN_NAMESPACE(WeatherStation);
00035 
00036 DECLARE_MODEL(ContosoAnemometer,
00037 WITH_DATA(ascii_char_ptr, DeviceId),
00038 WITH_DATA(int, WindSpeed),
00039 WITH_ACTION(TurnFanOn),
00040 WITH_ACTION(TurnFanOff),
00041 WITH_ACTION(SetAirResistance, int, Position)
00042 );
00043 
00044 END_NAMESPACE(WeatherStation);
00045 
00046 
00047 EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
00048 {
00049     (void)device;
00050     (void)printf("Turning fan on.\r\n");
00051     return EXECUTE_COMMAND_SUCCESS;
00052 }
00053 
00054 EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
00055 {
00056     (void)device;
00057     (void)printf("Turning fan off.\r\n");
00058     return EXECUTE_COMMAND_SUCCESS;
00059 }
00060 
00061 EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
00062 {
00063     (void)device;
00064     (void)printf("Setting Air Resistance Position to %d.\r\n", Position);
00065     return EXECUTE_COMMAND_SUCCESS;
00066 }
00067 
00068 void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
00069 {
00070     unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback;
00071 
00072     (void)printf("Message Id: %u Received.\r\n", messageTrackingId);
00073 
00074     (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
00075 }
00076 
00077 static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
00078 {
00079     static unsigned int messageTrackingId;
00080     IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
00081     if (messageHandle == NULL)
00082     {
00083         printf("unable to create a new IoTHubMessage\r\n");
00084     }
00085     else
00086     {
00087         if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
00088         {
00089             printf("failed to hand over the message to IoTHubClient");
00090         }
00091         else
00092         {
00093             printf("IoTHubClient accepted the message for delivery\r\n");
00094         }
00095         IoTHubMessage_Destroy(messageHandle);
00096     }
00097     free((void*)buffer);
00098     messageTrackingId++;
00099 }
00100 
00101 /*this function "links" IoTHub to the serialization library*/
00102 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
00103 {
00104     IOTHUBMESSAGE_DISPOSITION_RESULT result;
00105     const unsigned char* buffer;
00106     size_t size;
00107     if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
00108     {
00109         printf("unable to IoTHubMessage_GetByteArray\r\n");
00110         result = IOTHUBMESSAGE_ABANDONED;
00111     }
00112     else
00113     {
00114         /*buffer is not zero terminated*/
00115         char* temp = malloc(size + 1);
00116         if (temp == NULL)
00117         {
00118             printf("failed to malloc\r\n");
00119             result = IOTHUBMESSAGE_ABANDONED;
00120         }
00121         else
00122         {
00123             (void)memcpy(temp, buffer, size);
00124             temp[size] = '\0';
00125             EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
00126             result =
00127                 (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
00128                 (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
00129                 IOTHUBMESSAGE_REJECTED;
00130             free(temp);
00131         }
00132     }
00133     return result;
00134 }
00135 
00136 void simplesample_mqtt_run(void)
00137 {
00138     if (platform_init() != 0)
00139     {
00140         (void)printf("Failed to initialize platform.\r\n");
00141     }
00142     else
00143     {
00144         if (serializer_init(NULL) != SERIALIZER_OK)
00145         {
00146             (void)printf("Failed on serializer_init\r\n");
00147         }
00148         else
00149         {
00150             IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
00151             srand((unsigned int)time(NULL));
00152             int avgWindSpeed = 10;
00153 
00154             if (iotHubClientHandle == NULL)
00155             {
00156                 (void)printf("Failed on IoTHubClient_LL_Create\r\n");
00157             }
00158             else
00159             {
00160 #ifdef MBED_BUILD_TIMESTAMP
00161                 // For mbed add the certificate information
00162                 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
00163                 {
00164                     (void)printf("failure to set option \"TrustedCerts\"\r\n");
00165                 }
00166 #endif // MBED_BUILD_TIMESTAMP
00167                 
00168 
00169                 ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
00170                 if (myWeather == NULL)
00171                 {
00172                     (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
00173                 }
00174                 else
00175                 {
00176                     if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
00177                     {
00178                         printf("unable to IoTHubClient_SetMessageCallback\r\n");
00179                     }
00180                     else
00181                     {
00182                         myWeather->DeviceId = "myFirstDevice";
00183                         myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
00184                         {
00185                             unsigned char* destination;
00186                             size_t destinationSize;
00187                             if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != CODEFIRST_OK)
00188                             {
00189                                 (void)printf("Failed to serialize\r\n");
00190                             }
00191                             else
00192                             {
00193                                 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
00194                                 if (messageHandle == NULL)
00195                                 {
00196                                     printf("unable to create a new IoTHubMessage\r\n");
00197                                 }
00198                                 else
00199                                 {
00200                                     if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
00201                                     {
00202                                         printf("failed to hand over the message to IoTHubClient");
00203                                     }
00204                                     else
00205                                     {
00206                                         printf("IoTHubClient accepted the message for delivery\r\n");
00207                                     }
00208 
00209                                     IoTHubMessage_Destroy(messageHandle);
00210                                 }
00211                                 free(destination);
00212                             }
00213                         }
00214 
00215                         /* wait for commands */
00216                         while (1)
00217                         {
00218                             IoTHubClient_LL_DoWork(iotHubClientHandle);
00219                             ThreadAPI_Sleep(100);
00220                         }
00221                     }
00222 
00223                     DESTROY_MODEL_INSTANCE(myWeather);
00224                 }
00225                 IoTHubClient_LL_Destroy(iotHubClientHandle);
00226             }
00227             serializer_deinit();
00228         }
00229         platform_deinit();
00230     }
00231 }