Integrate an SDK in a Linux development environment

更新时间:
复制 MD 格式

You can add the Link SDK to an existing development project and compile them together. The following example walks you through this process.

Example description

This example includes a test program named hello.c that prints Hello World! and a makefile for compiling it. You can also download the hello code snippet.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        printf("Hello World!\n\r");
        return(0);
}

The makefile contains the following content:

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)

Port the SDK

  1. Obtain the SDK.

    • Set Device OS to Linux.

    • Set Protocol to connect to IoT Platform to MQTT.

    • Set Data Encryption to TLS-CA.

    • Set Device Authentication Method to Device Secret.

    Do not select any Advanced Features. Click Generate to download the SDK.

  2. SDK porting procedure

    After you download and decompress the SDK, copy the LinkSDK folder to the directory that contains hello.c, as shown below:

    $ 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
                            
  3. Copy and modify the MQTT example.

    Copy LinkSDK/demos/mqtt_basic_demo.c to the directory that contains hello.c. In mqtt_basic_demo.c, rename the main function to sdk_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. */
    
    ...
    }             
  4. Modify the makefile to include the SDK.

    Modify the makefile to compile the SDK source code and the copied mqtt_basic_demo.c file. You can also download the sdk_test code snippet.

    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)

    Note the following changes:

    • SDK_DIR must contain the folders for the features that your device uses. If you selected advanced features, the corresponding folders appear in the LinkSDK/components directory. You must add these folders to SDK_DIR.

    • This example uses Transport Layer Security (TLS) to encrypt data. Therefore, the mbedtls library that is included with the SDK is also compiled. The SDK_INC variable specifies the inclusion of the external/mbedtls/include directory.

    • The compiled program runs on Linux and uses thread-related libraries. Therefore, the SDK_LIBS variable specifies the link to the pthread library.

  5. Modify hello.c to call the SDK.

    Modify hello.c to call the sdk_test function from mqtt_basic_demo.c. This initializes the SDK and connects the device to 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);
    }
                            
  6. To compile the code, run make. To run the program, run ./hello.

    Run make in the directory that contains hello.c to compile the program. Then run ./hello to execute it. On success, the following output is displayed:

    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