This topic describes how to perform driver encoding.

Driver and device configurations

Before you perform driver encoding, we recommend that you have a basic understanding of driver and device configurations.

Driver configurations

You can configure the driver settings on the Alibaba Cloud IoT Platform console. When you deploy an edge instance, driver configurations are deployed to the gateway that is associated with the edge instance. The driver configurations are stored in Link IoT Edge in the JSON format. You can call the leda_get_driver_info API operation to retrieve the driver configurations.

You can configure driver settings by using one of the following three methods:
  • Key-value pairs
    {
       "kv":[
            {
                "key":"ip",
                "value":"127.0.0.1",
                "note":"IP address"
            },
            {
                "key":"port",
                "value":"54321",
                "note":"Port number"
            }
        ]
    }
    The following table describes the related parameters.
    Parameter Description
    kv The configuration method: key-value pairs
    key The name of the key
    value The value of the key
    note The description of the key-value pairs
  • JSON format
    {
        "json":{
            "ip":"127.0.0.1",
            "port":54321
        }
    }
    The following table describes the related parameters.
    Parameter Description
    json The configuration method: JSON format. You can specify the JSON-formatted data based on your business requirements.
  • Configuration file
    {
        "fileList":[
            {
                "path":"device_config.json"
            }
        ]
    }
    The following table describes the related parameters.
    Parameter Description
    fileList The configuration method: list of configuration files.
    path The path to the configuration files. The configuration file is stored in the directory of the driver configurations.
Device configurations

You can configure the device settings on the Alibaba Cloud IoT Platform console. When you deploy an edge instance, device configurations are deployed to the gateway that is associated with the edge instance. The device configurations are stored in the JSON format. You can call the leda_get_device_info API operation to retrieve the device configurations.

Configure the device settings as follows:
{
    "deviceList": [{
        "custom": {
             "ip":"127.0.0.1", 
             "port":22322
        }, // The custom device configurations.
        "productKey": "xxxxxxxxxxx", // The product key, which is generated after the product is created.        "deviceName": "demo_led",    // The device name, which is specified when the device is created.    }]
}
The following table describes the parameters for device configurations.
Parameter Description
deviceList The list of devices that are assigned to the current driver. Note that the configurations of these drivers have been specified.
custom The custom device configurations.
productKey The product key. The product key is the unique identifier of the device within the product.
deviceName The name of the device.

Procedure

  1. Initialize the driver. To do this, you can call the leda_init API operation.
    int main(int argc, char** argv)
    {
        ...
    
        /* init driver */
        if (LE_SUCCESS ! = (ret = leda_init(WORKER_THREAD_NUMS)))
        {
            log_e(TAG_NAME_LED_DRIVER, "leda_init failed\n");
            return ret;
        }
    
        ...
    
      return LE_SUCCESS;
    }
  2. Parse the driver configurations and publish the device to IoT Platform. To do this, you can call the leda_get_device_info_size and leda_get_device_info API operations to retrieve the driver and device configurations and parse the configurations based on the driver configuration method. You can connect the device to the driver based on the parsed configurations. Then, you can call the leda_register_and_online_by_device_name API operation to register and publish the device to IoT Platform. For more information, see IoT Platform.
    For more information about the driver configuration methods, see Driver and device configurations in this topic.
    static int online_devices()
    {
      ...
    
        /* Retrieve the driver and device configurations. */    size = leda_get_device_info_size();
        if (size >0)
        {
            device_config = (char*)malloc(size);
            if (NULL == device_config)
            {
                log_e(TAG_DEMO_LED, "allocate memory failed\n");
                return LE_ERROR_INVAILD_PARAM;
            }
    
            if (LE_SUCCESS ! = (ret = leda_get_device_info(device_config, size)))
            {
                log_e(TAG_DEMO_LED, "get device config failed\n");
                return ret;
            }
        }
    
        /* Parse the driver and device configurations. */
        devices = cJSON_Parse(device_config);
        if (NULL == devices)
        {
            log_e(TAG_DEMO_LED, "device config parser failed\n");
            return LE_ERROR_INVAILD_PARAM;
        }
    
        cJSON_ArrayForEach(item, devices)
        {
            if (cJSON_Object == item->type)
            {
                /* Parse the configurations. */            result      = cJSON_GetObjectItem(item, "productKey");
                productKey  = result->valuestring;
    
                result      = cJSON_GetObjectItem(item, "deviceName");
                deviceName  = result->valuestring;
    
                result      = cJSON_GetObjectItem(item, "custom");
                if (NULL ! = result)
                {
                    log_i(TAG_DEMO_LED, "custom content: %s\n", cJSON_Print(result));
                }
    
    /* Register and publish the device. */
                device_cb.get_properties_cb            = get_properties_callback_cb;
                device_cb.set_properties_cb            = set_properties_callback_cb;
                device_cb.call_service_cb              = call_service_callback_cb;
           device_cb.service_output_max_count     = 5;
    
                dev_handle = leda_register_and_online_by_device_name(productKey, deviceName, &device_cb, NULL);
                if (dev_handle < 0)
                {
                    log_e(TAG_DEMO_LED, "product:%s device:%s register failed\n", productKey, deviceName);
                    continue;
                }
    
                g_dev_handle = dev_handle;
                log_i(TAG_DEMO_LED, "product:%s device:%s register success\n", productKey, deviceName);
            }
        }
    
      ...
    
        return LE_SUCCESS;
    }
  3. Convert the received device data to the data that meets the format requirements of Alibaba Cloud Thing Specification Language (TSL) models, and report the converted data to IoT Platform. For more information about the TSL models, see Overview. You can call the leda_report_properties API operation to report the property data, and call the leda_report_event API operation to report the events.
    Note In this example, virtual devices are used to generate and report data that meets the TSL specifications.
    /* Report the data. */
    while (1)
    {
    /* Report the property data. */
        leda_device_data_t dev_proper_data[1] = 
        {
            {
                .type  = LEDA_TYPE_INT,
                .key   = {"temperature"},
                .value = {0}
            }
    };    sprintf(dev_proper_data[0].value, "%d", g_dev_temperature);
        leda_report_properties(g_dev_handle, dev_proper_data, 1);
    
        /* Report the events. */
        if (g_dev_temperature > 50)
        {
            leda_device_data_t dev_event_data[1] = 
            {
                {
                    .type  = LEDA_TYPE_INT,
                    .key   = {"temperature"},
                    .value = {0}
                }
            };
            sprintf(dev_event_data[0].value, "%d", g_dev_temperature);
            leda_report_event(g_dev_handle, "high_temperature", dev_event_data, 1);
        }
    
        sleep(5);
    }
  4. Process the requests that are sent from the cloud. The following three types of callback functions are used to process the requests:
    • Get API operations: process the requests of retrieving device property data.
    • Set API operations: process the requests of configuring device properties.
    • Service API operations: process the requests of calling custom methods.
    Note This example uses the get_properties_callback_cb, set_properties_callback_cb, and call_service_callback_cb callback functions to process the requests.
    static int get_properties_callback_cb(device_handle_t device_handle, 
                                   leda_device_data_t properties[], 
                                   int properties_count, 
                                   void *usr_data)
    {
        ...
    
        return ret;
    }
    
    static int set_properties_callback_cb(device_handle_t device_handle, 
                                   const leda_device_data_t properties[], 
                                   int properties_count, 
                                   void *usr_data)
    {
        ...
    
        return ret;
    }
    
    static int call_service_callback_cb(device_handle_t device_handle, 
                                   const char *service_name, 
                                   const leda_device_data_t data[], 
                                   int data_count, 
                                   leda_device_data_t output_data[], 
                                   void *usr_data)
    {
        ...
    
        return ret;
    }
Note To obtain the complete source code of this example, see LED lamp driver from the GitHub source code library.

Now you have completed driver encoding.