static bool InitializeAzureIoTHub()

in roscpp_azure_iothub/src/ros_azure_iothub_cpp_node.cpp [600:655]


static bool InitializeAzureIoTHub(ROS_Azure_IoT_Hub* iotHub)
{
    IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = MQTT_Protocol;

    (void)IoTHub_Init();
    ROS_INFO("Creating IoTHub Device handle");

    ros::NodeHandle nh("~");
    std::string connectionString;
    nh.getParam("connection_string", connectionString);
    ROS_INFO("connection_string: %s", connectionString.c_str());

    std::string authenticationType;
    nh.getParam("authentication_type", authenticationType);
    ROS_INFO("authentication_type: %s", authenticationType.c_str());

    // Create the iothub handle here
    iotHub->deviceHandle = IoTHubDeviceClient_CreateFromConnectionString(connectionString.c_str(), protocol);
    if (iotHub->deviceHandle == NULL)
    {
        ROS_ERROR("Failure createing Iothub device.  Hint: Check you connection string.");
        return false;
    }

    // Check if a x509 certificate should be used for authentication. If not,
    // a sharedAccessKey is expected in the connection string.
    if (authenticationType == g_authentication_SAS || authenticationType == "")
    {
        ROS_INFO("Using Shared Access Signatures authentication.");
    }
    else if (authenticationType == g_authentication_x509)
    {
        ROS_INFO("Using x.509 Certificate authentication.");
        if(!InitializeX509Certificate(iotHub->deviceHandle, nh))
        {
            ROS_ERROR("Failed to initialize x509 certificate.");
            return false;
        }
    }
    else
    {
        ROS_ERROR("Invalid authentication type: %s", authenticationType.c_str());
        return false;
    }

    // Setting message callback to get C2D messages
    (void)IoTHubDeviceClient_SetMessageCallback(iotHub->deviceHandle, receive_msg_callback, NULL);
    // Setting method callback to handle a SetTelemetryInterval method to control
    //   how often telemetry messages are sent from the simulated device.
    (void)IoTHubDeviceClient_SetDeviceMethodCallback(iotHub->deviceHandle, device_method_callback, NULL);
    // Setting connection status callback to get indication of connection to iothub
    (void)IoTHubDeviceClient_SetConnectionStatusCallback(iotHub->deviceHandle, connection_status_callback, NULL);
    (void)IoTHubDeviceClient_SetDeviceTwinCallback(iotHub->deviceHandle, deviceTwinCallback, iotHub);  //Device Twin callback requires context to send message and device Handle

    return true;
}