Puede agregar el Link SDK a un proyecto de desarrollo existente y compilarlos juntos. A continuación se muestra un ejemplo que detalla este proceso.
Descripción del ejemplo
Este ejemplo incluye un programa de prueba llamado hello.c que imprime Hello World! y un makefile para compilarlo. También puede descargar el fragmento de código hello.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("Hello World!\n\r");
return(0);
}
El makefile contiene el siguiente contenido:
PROG_FILE := hello.c mqtt_basic_demo.c
PROG_OBJS := $(patsubst %.c,%.o,$(PROG_FILE))
PROG = hello
SDK_ROOT = $(shell pwd)/LinkSDK
SDK_INC = -I$(SDK_ROOT)/output/include/
SDK_LIB = $(SDK_ROOT)/output/lib/libaiot.a -lpthread
all:prepare $(PROG_OBJS)
$(CC) $(CFLAGS) -o $(PROG) $(PROG_OBJS) $(SDK_INC) $(SDK_LIB)
prepare:
make -C $(SDK_ROOT)
./%.o: %.c
$(CC) -o $@ -c $< $(CFLAGS) $(SDK_INC)
clean:
make -C LinkSDK clean
rm -f *.o $(PROG) $(SDK_OBJS)
Portar el SDK
-
Obtenga el SDK.
Obtenga el SDK desde la ruta proporcionada en Obtain an SDK o . Configure el SDK con los siguientes ajustes:
Establezca Device OS en Linux.
Establezca Protocol to connect to IoT Platform en MQTT.
Establezca Data Encryption en TLS-CA.
Establezca Device Authentication Method en Device Secret.
No seleccione ninguna opción en Advanced Features. Haga clic en Generate para descargar el SDK.
-
Procedimiento de portabilidad del SDK
Después de descargar y descomprimir el SDK, copie la carpeta LinkSDK al directorio que contiene
hello.c, como se muestra a continuación:$ ls -l -rwxrwxrwx 1 root root 183 May 11 19:45 hello.c drwxrwxrwx 1 root root 4096 May 11 17:28 LinkSDK -rwxrwxrwx 1 root root 672 May 11 19:49 makefile -
Copie y modifique el ejemplo de MQTT.
Copie
LinkSDK/demos/mqtt_basic_demo.cal directorio que contienehello.c. Enmqtt_basic_demo.c, cambie el nombre de la funciónmainporsdk_test:/* TODO: Replace with your device credentials. */ char *product_key = "${YourProductKey}"; char *device_name = "${YourDeviceName}"; char *device_secret = "${YourDeviceSecret}"; /* TODO: Replace with the endpoint of your instance. */ char *mqtt_host = "${YourInstanceId}.mqtt.iothub.aliyuncs.com"; int sdk_test(int argc, char *argv[]) { int32_t res = STATE_SUCCESS; void *mqtt_handle = NULL; char *url = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; /* The domain name suffix for Alibaba Cloud IoT Platform in the China (Shanghai) region. */ ... } -
Modifique el makefile para incluir el SDK.
Modifique el makefile para compilar el código fuente del SDK y el archivo
mqtt_basic_demo.cque copió. También puede descargar el fragmento de código sdk_test.PROG_FILE := hello.c mqtt_basic_demo.c PROG_OBJS := $(patsubst %.c,%.o,$(PROG_FILE)) PROG = hello SDK_ROOT = $(shell pwd)/LinkSDK SDK_DIR = $(SDK_ROOT)/core $(SDK_ROOT)/core/sysdep $(SDK_ROOT)/core/utils $(SDK_ROOT)/portfiles/aiot_port $(SDK_ROOT)/external $(SDK_ROOT)/external/mbedtls/library SDK_INC = -I$(SDK_ROOT)/external/mbedtls/include $(foreach dir, $(SDK_DIR), -I$(dir) ) SDK_FILES = $(foreach dir, $(SDK_DIR), $(wildcard $(dir)/*.c)) SDK_OBJS = $(patsubst %.c,%.o,$(SDK_FILES)) SDK_LIBS = -lpthread CFLAGS += $(SDK_INC) main:$(PROG_OBJS) $(SDK_OBJS) $(CC) $(CFLAGS) -o $(PROG) $(PROG_OBJS) $(SDK_OBJS) $(SDK_LIBS) clean: rm -f *.o $(PROG) $(SDK_OBJS)Tome en cuenta los siguientes cambios:
SDK_DIR debe contener las carpetas de las funciones que utiliza su dispositivo. Si seleccionó funciones avanzadas, las carpetas correspondientes aparecen en el directorio
LinkSDK/components. Debe agregar estas carpetas a SDK_DIR.Este ejemplo usa Transport Layer Security (TLS) para cifrar los datos. Por lo tanto, también se compila la biblioteca mbedtls que se incluye con el SDK. La variable
SDK_INCespecifica la inclusión del directorioexternal/mbedtls/include.El programa compilado se ejecuta en Linux y utiliza bibliotecas relacionadas con hilos. Por esta razón, la variable SDK_LIBS especifica el vínculo a la biblioteca pthread.
-
Modifique hello.c para llamar al SDK.
Modifique
hello.cpara llamar a la funciónsdk_testdemqtt_basic_demo.c. Esto inicializa el SDK y conecta el dispositivo a Alibaba Cloud IoT Platform.#include <stdio.h> #include <stdlib.h> /* Declare the sdk_test function. */ extern int sdk_test(int argc, char *argv[]); int main(int argc, char **argv) { printf("Hello World!\n\r"); /* Call the sdk_test function in the SDK demo to initialize the SDK and connect to Alibaba Cloud IoT Platform. */ sdk_test(0,NULL); return(0); } -
Para compilar el código, ejecute
make. Para ejecutar el programa, ejecute./hello.Ejecute
makeen el directorio que contienehello.cpara compilar el programa. Luego, ejecute./hellopara ejecutarlo. Si el proceso es exitoso, se muestra la siguiente salida:Hello World! [1687781092.755][LK-0313] MQTT user calls aiot_mqtt_connect api, connect [1687781092.755][LK-032A] mqtt host: iot-****.mqtt.iothub.aliyuncs.com [1687781092.755][LK-0317] user name: demo****** establish tcp connection with server(host='iot-******.mqtt.iothub.aliyuncs.com', port=[443]) success to establish tcp, fd=3 local port: 52008 [1687781092.811][LK-1000] establish mbedtls connection with server(host='iot-****.mqtt.iothub.aliyuncs.com', port=[443]) [1687781092.844][LK-1000] success to establish mbedtls connection, (cost 45338 bytes in total, max used 48306 bytes) [1687781092.899][LK-0313] MQTT connect success in 137 ms AIOT_MQTTEVT_CONNECT heartbeat response