All Products
Search
Document Center

IoT Platform:Integrate Link SDK for C into a Linux development environment

Last Updated:Oct 12, 2023

If you have an existing project when you develop applications, you can add Link SDK for C to the project to compile a program. This topic describes how to add Link SDK for C to an existing project for compilation.

Example

In this example, the hello.c test program that can print Hello World! is used and a makefile is used to compile the test program. For more information, see hello. Sample code:

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

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

The makefile contains the following code:

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 the Device OS parameter to Linux.

    • Set the Connection protocol parameter to MQTT.

    • Set the Data encryption parameter to TLS-CA.

    • Set the Device authentication scheme parameter to Device key.

    Do not select options in the Advanced Capabilities section. Click Start generation to download the SDK.

  2. Port the SDK.

    Download and decompress the SDK package, and then copy the Link SDK directory to the directory where hello.c is stored.

    $ 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 sample code for the MQTT-based device connection.

    Copy the LinkSDK/demos/mqtt_basic_demo.c file to the directory where the hello.c file is stored and change the name of the main function to sdk_test in the mqtt_basic_demo.c file. Modify the sample code for the MQTT-based device connection as follows:

    /* Replace the variables with your device certificate. */
    char *product_key       = "${YourProductKey}";
    char *device_name       = "${YourDeviceName}";
    char *device_secret     = "${YourDeviceSecret}";
    
    /*
        Replace the variable 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 suffix of the endpoint of an IoT Platform instance that is deployed in the China (Shanghai) region. */
    
    ...
    }             
  4. Modify the makefile to include the SDK.

    Modify the makefile to compile the source code of the SDK and the copied mqtt_basic_demo.c file (including 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)

    When you modify the preceding code, take note of the following items:

    • The SDK_DIR parameter must contain the directories of features that are required by the devices in the SDK. If you select an option in the Advanced Capabilities section, you can view the corresponding directories in the LinkSDK/components directory, and you must add the directories to SDK_DIR.

    • In this example, Transport Layer Security (TLS) is used to encrypt data. In this case, the mbedtls that is included in the SDK is compiled and the external/mbedtls/include directory is specified in SDK_INC.

    • The compiled program runs on Linux and uses thread-related libraries. In this case, the link to the pthread library is specified in SDK_LIBS.

  5. Modify hello.c to call the SDK.

    Modify hello.c to call the sdk_test function in the mqtt_basic_demo.c file, initialize the SDK, and connect 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 package to initialize the SDK and connect the device to Alibaba Cloud IoT Platform.*/
            sdk_test(0,NULL);
            return(0);
    }
                            
  6. Run the make command to compile a program. Run the ./hello command to execute the program.

    Run the make command in the directory where hello.c is stored to compile the hello program, and then run the program. If no error occurs, 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