Usage example

Updated at:
Copy as MD

This topic uses the ./demos/remote_access_basic_demo.c demo file from the C Link software development kit (SDK) to demonstrate how to call Link SDK APIs and enable the secure tunnel feature for a device.

Background information

  • For more information about the secure tunnel feature, see Overview.

  • The secure tunnel feature uses an MQTT connection. For more information about the MQTT connection code, see MQTT connection.

Step 1: Initialization

  1. Add the header file.
    ……
    ……
    
    #include "aiot_ra_api.h"

  2. Configure underlying dependencies and log output.

        aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile);
        aiot_state_set_logcb(demo_state_logcb);
  3. Call aiot_ra_init to create an RA
        void *ra_handle = aiot_ra_init();
        if (ra_handle == NULL) {
            printf( "aiot_ra_init failed\n");
            return -1;
        }

Step 2: Configure features

Call aiot_ra_setopt to configure the following features.

  1. Configure feature parameters.
    Important Before you configure parameters for the secure tunnel feature, make sure that device credentials and other related parameters are configured. For more information, see Configure MQTT connection parameters.
    •     aiot_ra_setopt(ra_handle, AIOT_RAOPT_MQTT_HANDLE, mqtt_handle);
          aiot_ra_setopt(ra_handle, AIOT_RAOPT_NETWORK_CRED, (void *)&cred);
          aiot_ra_setopt(ra_handle, AIOT_RAOPT_EVENT_HANDLER, (void *)ra_event_cb);
          for(int i = 0; i < sizeof(services) / sizeof(aiot_ra_service_t); i++) {
              aiot_ra_setopt(ra_handle, AIOT_RAOPT_ADD_SERVICE, (void *)&services[i]);
          }
    • AIOT_RAOPT_MQTT_HANDLE
      mqtt_handle
      The secure tunnel feature sends requests over an MQTT connection. This item associates the MQTT connection handle.
      AIOT_RAOPT_NETWORK_CRED
      cred
      The security credentials for the network when the secure tunnel establishes a connection.
      AIOT_RAOPT_EVENT_HANDLER
      ra_event_cb
      The callback function that is triggered when the WebSocket status changes.
      AIOT_RAOPT_ADD_SERVICE
      services[i]
      The local services supported on the device when a secure tunnel connection is established.


  2. Important When you write the processing logic for the status monitoring callback function, do not call time-consuming or blocking functions.
    void ra_event_cb(void *handle, const aiot_ra_event_t *event, void *userdata)
    {
        switch(event->type)
        {
        case AIOT_RA_EVT_CONNECT:
            printf( "ra_event_cb AIOT_RA_EVT_CONNECT %s \r\n", event->tunnel_id);
            /* TODO: Notify that the WebSocket connection is established. Do not call time-consuming blocking functions here. */
            break;
        case AIOT_RA_EVT_DISCONNECT:
            printf( "ra_event_cb AIOT_RA_EVT_DISCONNECT %s \r\n", event->tunnel_id);
            /* TODO: Notify that the WebSocket connection is dropped. Do not call time-consuming blocking functions here. */
            break;
        case AIOT_RA_EVT_OPEN_WEBSOCKET:
            printf( "ra_event_cb AIOT_RA_EVT_OPEN_WEBSOCKET %s \r\n", event->tunnel_id);
            /* TODO: Notify that RA received the command to open the WebSocket connection. Do not call time-consuming blocking functions here. */
            break;
        case AIOT_RA_EVT_CLOSE_WEBSOCKET:
            printf( "ra_event_cb AIOT_RA_EVT_CLOSE_WEBSOCKET %s \r\n", event->tunnel_id);
            /* TODO: Notify that RA received the command to close the WebSocket connection. Do not call time-consuming blocking functions here. */
            break;
        }
    }

  3. Define the types of locally supported remote services.
    Note
    • The _SSH service is enabled by default. This service is required to use the remote logon feature. For more information, see Usage example of the remote logon feature.
    • If the device provides a specific service for access, add the service information to the local service list of the device.
    aiot_ra_service_t services[] = {
        {
            .type = "_SSH",
            .ip = "127.0.0.1",
            .port = 22,
        },
        {
            .type = "Echo-service",
            .ip = "127.0.0.1",
            .port = 7,
        },
    };

Step 3: Enable the secure tunnel feature

Call aiot_ra_start to notify IoT Platform that the device supports the secure tunnel feature.

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    if (0 != pthread_create(&g_ra_process_thread, &attr, aiot_ra_start, (void*) ra_handle))
    {
        printf( "create remote_proxy_thread error!");
        return -1;
    }

Step 4: Create a secure tunnel

  1. Log on to the IoT Platform console.
  2. Create a secure tunnel. Obtain the Device-side URL and Access-side Token. These are required to establish a WebSocket connection between the access client and IoT Platform.

After you create the secure tunnel, develop an access client application to remotely access and manage the device.

Step 5: Close the secure tunnel

Call aiot_ra_stop to close the secure tunnel.

    int i = 60000;
    while(1)
    {
        sleep(1);
        /* TODO: Business logic */
        i--;
        /*Set conditions based on your business logic. Call aiot_ra_stop to exit the thread.*/
        if(i == 0)
        {
            /*Exit the thread and close the RA service.*/
            aiot_ra_stop(ra_handle);
            break;
        }
    }

Step 6: Exit the program

Call aiot_ra_deinit to destroy the RA instance.

    aiot_ra_deinit(&ra_handle);

What to do next