Develop on the device

Updated at:

Use the Link SDK for C to connect a communication module to IoT Platform.

Configure the device-side SDK

  1. Download Link SDK for C V3.0.1.
  2. Extract the SDK code from the downloaded package. This topic uses Linux operations as an example.
    1. Run make menuconfig.
    2. Select ATM Configurations and click Select.
    3. Select AT HAL Configurations and click Select.
      AT HAL Configurations
    4. Configure the following items.
      FEATURE_PLATFORM_HAS_STDINT=y
      FEATURE_PLATFORM_HAS_OS=y
      FEATURE_INFRA_STRING=y
      FEATURE_INFRA_NET=y
      FEATURE_INFRA_LIST=y
      FEATURE_INFRA_LOG=y
      FEATURE_INFRA_LOG_ALL_MUTED=y
      FEATURE_INFRA_LOG_MUTE_FLW=y
      FEATURE_INFRA_LOG_MUTE_DBG=y
      FEATURE_INFRA_LOG_MUTE_INF=y
      FEATURE_INFRA_LOG_MUTE_WRN=y
      FEATURE_INFRA_LOG_MUTE_ERR=y
      FEATURE_INFRA_LOG_MUTE_CRT=y
      FEATURE_INFRA_TIMER=y
      FEATURE_INFRA_SHA256=y
      FEATURE_INFRA_REPORT=y
      FEATURE_INFRA_COMPAT=y
      FEATURE_DEV_SIGN=y
      FEATURE_MQTT_COMM_ENABLED=y
      FEATURE_MQTT_DEFAULT_IMPL=y
      FEATURE_MQTT_DIRECT=y
      FEATURE_DEVICE_MODEL_CLASSIC=y
      FEATURE_ATM_ENABLED=y
      FEATURE_AT_TCP_ENABLED=y
      FEATURE_AT_PARSER_ENABLED=y
      FEATURE_AT_TCP_HAL_SIM800=y
    5. After the configuration is complete, run ./extract.sh to extract the code.
      extract.sh

      The extracted code is in the output/eng folder.

      output/eng

      The following table describes the code in each subdirectory.

      Folder Code Content
      atm AT command sending and receiving module
      dev_sign Device identity authentication module
      infra Internal implementation module
      mqtt MQTT protocol module
      wrappers HAL integration module
    6. In the wrappers folder, create a file named wrappers.c. This file must implement the following HAL functions.
      int32_t HAL_AT_Uart_Deinit(uart_dev_t *uart)
      int32_t HAL_AT_Uart_Init(uart_dev_t *uart)
      int32_t HAL_AT_Uart_Recv(uart_dev_t *uart, void *data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
      int32_t HAL_AT_Uart_Send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout)
      
      int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN])
      int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN])
      int HAL_GetFirmwareVersion(char *version)
      int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN])
      
      void *HAL_Malloc(uint32_t size)
      void HAL_Free(void *ptr)
      
      void *HAL_MutexCreate(void)
      void HAL_MutexDestroy(void *mutex)
      void HAL_MutexLock(void *mutex)
      void HAL_MutexUnlock(void *mutex)
      
      void *HAL_SemaphoreCreate(void)
      void HAL_SemaphoreDestroy(void *sem)
      void HAL_SemaphorePost(void *sem)
      int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
      int HAL_ThreadCreate(void **thread_handle,
                           void *(*work_routine)(void *),
                           void *arg,
                           hal_os_thread_param_t *hal_os_thread_param,
                           int *stack_used)
      
      void HAL_SleepMs(uint32_t ms)
      void HAL_Printf(const char *fmt, ...)
      int HAL_Snprintf(char *str, const int len, const char *fmt, ...)
      uint64_t HAL_UptimeMs(void)

      Download the sample code for the wrappers.c file.

      In the sample code, replace the device certificate information with your own.

      设备证书
      Note If you do not use the NUCLEO-F103RB communication module development board, set FEATURE_AT_TCP_HAL_SIM800=n during configuration. In addition, the code in the wrappers.c file must implement the following HAL functions.
      int HAL_AT_CONN_Close(int fd, int32_t remote_port)
      int HAL_AT_CONN_Deinit(void)
      int HAL_AT_CONN_DomainToIp(char *domain, char ip[16])
      int HAL_AT_CONN_Init(void)
      int HAL_AT_CONN_Send(int fd, uint8_t *data, uint32_t len, char remote_ip[16], int32_t remote_port, int32_t timeout)
      int HAL_AT_CONN_Start(at_conn_t *conn)
      
      int32_t HAL_AT_Uart_Deinit(uart_dev_t *uart)
      int32_t HAL_AT_Uart_Init(uart_dev_t *uart)
      int32_t HAL_AT_Uart_Recv(uart_dev_t *uart, void *data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
      int32_t HAL_AT_Uart_Send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout)
      
      int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN])
      int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN])
      int HAL_GetFirmwareVersion(char *version)
      int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN])
      
      void *HAL_Malloc(uint32_t size)
      void HAL_Free(void *ptr)
      
      void *HAL_MutexCreate(void)
      void HAL_MutexDestroy(void *mutex)
      void HAL_MutexLock(void *mutex)
      void HAL_MutexUnlock(void *mutex)
      
      void *HAL_SemaphoreCreate(void)
      void HAL_SemaphoreDestroy(void *sem)
      void HAL_SemaphorePost(void *sem)
      int HAL_SemaphoreWait(void *sem, uint32_t timeout_ms)
      int HAL_ThreadCreate(void **thread_handle,
                           void *(*work_routine)(void *),
                           void *arg,
                           hal_os_thread_param_t *hal_os_thread_param,
                           int *stack_used)
      
      void HAL_SleepMs(uint32_t ms)
      void HAL_Printf(const char *fmt, ...)
      int HAL_Snprintf(char *str, const int len, const char *fmt, ...)
      uint64_t HAL_UptimeMs(void)
                                          
  3. Integrate the SDK into the IAR project.
    Refer to the following figure. AR
  4. Run the SDK to perform a test.
    Upon successful execution, the following figure shows the local logs on the device. 运行SDK

    Log on to the IoT Platform console. You can also view logs of data reported by devices to the cloud for the corresponding instance in the Monitoring and O&M > Log Service section.