Ajoutez le Link SDK à un projet de développement existant et compilez-les ensemble. L'exemple suivant détaille cette procédure.
Description de l'exemple
Cet exemple comprend un programme de test nommé hello.c qui affiche Hello World! ainsi qu'un makefile pour le compiler. Vous pouvez également télécharger l'extrait de code hello.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("Hello World!\n\r");
return(0);
}
Le makefile contient le contenu suivant :
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)
Porter le SDK
-
Obtenez le SDK.
Récupérez le SDK à partir du chemin indiqué dans Obtenir un SDK ou . Configurez le SDK avec les paramètres suivants :
Définissez Device OS sur Linux.
Définissez Protocol to connect to IoT Platform sur MQTT.
Définissez Data Encryption sur TLS-CA.
Définissez Device Authentication Method sur Device Secret.
Ne sélectionnez aucune Advanced Features. Cliquez sur Generate pour télécharger le SDK.
-
Procédure de portage du SDK
Après avoir téléchargé et décompressé le SDK, copiez le dossier LinkSDK dans le répertoire contenant
hello.c, comme illustré ci-dessous :$ 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 -
Copiez et modifiez l'exemple MQTT.
Copiez
LinkSDK/demos/mqtt_basic_demo.cdans le répertoire contenanthello.c. Dansmqtt_basic_demo.c, renommez la fonctionmainensdk_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. */ ... } -
Modifiez le makefile pour inclure le SDK.
Modifiez le makefile afin de compiler le code source du SDK et le fichier
mqtt_basic_demo.cque vous avez copié. Vous pouvez également télécharger l'extrait de code 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)Prenez note des modifications suivantes :
SDK_DIR doit contenir les dossiers correspondant aux fonctionnalités utilisées par votre appareil. Si vous avez sélectionné des fonctionnalités avancées, les dossiers correspondants apparaissent dans le répertoire
LinkSDK/components. Vous devez ajouter ces dossiers à SDK_DIR.Cet exemple utilise le protocole TLS (Transport Layer Security) pour chiffrer les données. Par conséquent, la bibliothèque mbedtls incluse avec le SDK est également compilée. La variable
SDK_INCspécifie l'inclusion du répertoireexternal/mbedtls/include.Le programme compilé s'exécute sous Linux et utilise des bibliothèques liées aux threads. Ainsi, la variable SDK_LIBS spécifie la liaison avec la bibliothèque pthread.
-
Modifiez hello.c pour appeler le SDK.
Modifiez
hello.cafin d'appeler la fonctionsdk_testdepuismqtt_basic_demo.c. Cela permet d'initialiser le SDK et de connecter l'appareil à 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); } -
Pour compiler le code, exécutez
make. Pour exécuter le programme, lancez./hello.Exécutez
makedans le répertoire contenanthello.cpour compiler le programme. Exécutez ensuite./hellopour le lancer. En cas de succès, la sortie suivante s'affiche :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