将Azure物联网sdk-c移植到yocto系统



我们正在考虑将Azure Hub的sdk-c移植到运行yocto的设备上,我们已经使用目标端工具链对其进行了编译,现在我们正在测试目标的结果。我们的意图是将它与mbedtls上的MQTT一起使用,并且我们目前收到了socketio(socketio_berkley.c(和tlsio_mbedtls.c的错误

azure-c共享实用程序/adapters/ssocketio_berkeley.c函数:initiate_socket_connection行:353打开到端点的连接失败azure-c共享实用程序/adapters/ssocketio_berkeley.c函数:socketio_open行:829 lookup_address_and_connect_socket失败azure-umqtt-c/src/mqt_client.c函数:onOpenComplete行:454错误:打开到端点的连接失败

设备客户端已断开^M

azure-c共享实用程序/adapters/tlsio_mbedtls.c函数:tlsio_mbedtls_open行:649底层IO打开失败azure-umqtt-c/src/mqt_client.c函数:mqtt_client_connect行:1093错误:io_open失败^Mazure-iot-sdk-c/iohub_client/src/iohubtransport_mqt_common.c函数:SendMqttConnectMsg行:2530连接到地址MYHUBNAME.azure-evice.net 失败

This is the code I am using:
/* Legato Framework */
#include "legato.h"
#include "interfaces.h"
#include "le_basics.h"
#include "json.h"

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style
// Checking of return codes and error values shall be omitted for brevity.  Please practice sound engineering practices
// when writing production code.
#include <stdio.h>
#include <stdlib.h>
#include "iothub.h"
#include "iothub_device_client_ll.h"
#include "iothub_client_options.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/threadapi.h" //need to find this
#include "azure_c_shared_utility/crt_abstractions.h"  //need to find this
#include "azure_c_shared_utility/shared_util_options.h"  //need to find this
#include "internal/iothub_internal_consts.h"
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
#include "certs.h"
#endif // SET_TRUSTED_CERT_IN_SAMPLES
/* This sample uses the _LL APIs of iothub_client for example purposes.
Simply changing the using the convenience layer (functions not having _LL)
and removing calls to _DoWork will yield the same results. */
// The protocol you wish to use should be uncommented
//
#define SAMPLE_MQTT
//#define SAMPLE_MQTT_OVER_WEBSOCKETS
//#define SAMPLE_AMQP
//#define SAMPLE_AMQP_OVER_WEBSOCKETS
//#define SAMPLE_HTTP
#define USE_MBEDTLS
#ifdef SAMPLE_MQTT
#include "iothubtransportmqtt.h"
#endif // SAMPLE_MQTT
/*
#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
#include "iothubtransportmqtt_websockets.h"
#endif // SAMPLE_MQTT_OVER_WEBSOCKETS
#ifdef SAMPLE_AMQP
#include "iothubtransportamqp.h"
#endif // SAMPLE_AMQP
#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
#include "iothubtransportamqp_websockets.h"
#endif // SAMPLE_AMQP_OVER_WEBSOCKETS
#ifdef SAMPLE_HTTP
#include "iothubtransporthttp.h"
#endif // SAMPLE_HTTP
*/
/* Paste in the your iothub connection string  */
static const char* connectionString = "****MY-CONNECTION-STRING****";
#define MESSAGE_COUNT        5
static bool g_continueRunning = true;
static size_t g_message_count_send_confirmations = 0;
static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
(void)userContextCallback;
// When a message is sent this callback will get envoked
g_message_count_send_confirmations++;
(void)printf("Confirmation callback received for message %lu with result %srn", (unsigned long)g_message_count_send_confirmations, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}
static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
{
(void)reason;
(void)user_context;
// This sample DOES NOT take into consideration network outages.
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
{
(void)printf("The device client is connected to iothubrn");
}
else
{
(void)printf("The device client has been disconnectedrn");
}
}

main
{
IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = MQTT_Protocol;
IOTHUB_MESSAGE_HANDLE message_handle;
size_t messages_sent = 0;
const char* telemetry_msg = "test_message";
// Used to initialize IoTHub SDK subsystem
(void)IoTHub_Init();
IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
(void)printf("Creating IoTHub Device handlern");
// Create the iothub handle here
device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
if (device_ll_handle == NULL)
{
(void)printf("Failure creating IotHub device. Hint: Check your connection string.rn");
}
else
{
// Set any option that are neccessary.
// For available options please see the iothub_sdk_options.md documentation
/*
#ifndef SAMPLE_HTTP
// Can not set this options in HTTP
bool traceOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
#endif
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
// Setting the Trusted Certificate. This is only necessary on systems without
// built in certificate stores.
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);
#endif // SET_TRUSTED_CERT_IN_SAMPLES
*/
#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_OVER_WEBSOCKETS
//Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
//you are URL Encoding inputs yourself.
//ONLY valid for use with MQTT
bool urlEncodeOn = true;
(void)IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
#endif

// Setting connection status callback to get indication of connection to iothub
(void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL);
do
{
if (messages_sent < MESSAGE_COUNT)
{
// Construct the iothub message from a string or a byte array
message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
//message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
// Set Message property

(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
(void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
(void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
(void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
(void)IoTHubMessage_SetMessageCreationTimeUtcSystemProperty(message_handle, "2020-07-01T01:00:00.346Z");


// Add custom properties to message
(void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value");
(void)printf("Sending message %d to IoTHubrn", (int)(messages_sent + 1));
IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
// The message is copied to the sdk so the we can destroy it
IoTHubMessage_Destroy(message_handle);
messages_sent++;
}
else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
{
// After all messages are all received stop running
g_continueRunning = false;
}
IoTHubDeviceClient_LL_DoWork(device_ll_handle);
ThreadAPI_Sleep(1);
} while (g_continueRunning);
// Clean up the iothub sdk handle
IoTHubDeviceClient_LL_Destroy(device_ll_handle);
}
// Free all the sdk subsystem
IoTHub_Deinit();
printf("Press any key to continue, this is a test.");
(void)getchar();
return;
}

我刚刚选择了MQTT_Protocol,但是连接字符串是否足以连接到后端,或者我还需要有证书?

设备需要证书才能通过TLS握手。如果您已经覆盖了证书,则证书是连接的一个选项,但您可以使用基于连接字符串生成的SAS令牌进行连接。

对于Yocto的特定问题,您尝试过这个入门示例吗?

如果在审查后仍然不起作用,请打开GitHub问题,并添加日志进行调查。

最新更新