全部產品
Search
文件中心

:驅動編碼

更新時間:Jul 13, 2024

在驅動開發過程中進行驅動編碼時,需遵循物聯網邊緣計算的驅動編碼規範和步驟。

驅動與裝置資訊配置

進行驅動編碼前,需要瞭解Link IoT Edge的裝置資訊配置和驅動資訊配置相關內容。

驅動資訊配置

驅動資訊配置在阿里雲物聯網平台進行配置。部署邊緣執行個體時,驅動配置資訊會被部署到邊緣網關,其內容以JSON格式儲存在Link IoT Edge配置中心,可以通過leda_get_driver_info介面擷取。

驅動資訊配置有如下三種配置格式:
  • 索引值對配置
    {
       "kv":[
            {
                "key":"ip",
                "value":"127.0.0.1",
                "note":"ip地址"
            },
            {
                "key":"port",
                "value":"54321",
                "note":"port連接埠"
            }
        ]
    }
    格式參數說明如下:
    參數名稱說明
    kv驅動配置的格式為索引值對配置。
    key鍵名稱。
    value值內容。
    note索引值注釋。
  • JSON格式
    {
        "json":{
            "ip":"127.0.0.1",
            "port":54321
        }
    }
    格式參數說明如下:
    參數名稱說明
    json驅動配置的格式為JSON格式配置。配置內容為自訂內容。
  • 設定檔
    {
        "fileList":[
            {
                "path":"device_config.json"
            }
        ]
    }
    格式參數說明如下:
    參數名稱說明
    fileList驅動配置格式為設定檔列表。
    path設定檔路徑。設定檔在驅動目前的目錄中。
裝置資訊配置

裝置資訊配置在阿里雲物聯網平台控制台配置。部署邊緣執行個體時,裝置資訊配置會被部署到邊緣網關,其內容以JSON格式儲存,可以通過leda_get_device_info介面擷取。

裝置資訊配置格式定義:
{
    "deviceList": [{
        "custom": {
             "ip":"127.0.0.1", 
             "port":22322
        }, // 裝置自訂配置
        "productKey": "xxxxxxxxxxx", // 產品ProductKey,在建立產品時產生
        "deviceName": "demo_led",    // 裝置DeviceName,在建立裝置時設定
    }]
}
裝置資訊配置參數說明:
配置名稱配置解釋
deviceList當前驅動下所有已進行裝置配置的裝置列表。
custom裝置自訂配置。
productKey裝置所在產品唯一識別碼。
deviceName裝置名稱。

操作步驟

  1. 初始化驅動資源。調用leda_init介面完成資源初始化。
    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. 解析驅動配置,完成裝置上線。通過調用leda_get_driver_info介面擷取驅動配置,解析裝置的串連資訊,並根據解析結果串連裝置。裝置串連成功後,調用leda_get_device_info介面擷取裝置配置並解析,根據解析結果驗證裝置功能。功能驗證通過後,調用leda_register_and_online_by_device_name介面完成裝置註冊並上線到阿里雲物聯網平台
    驅動資訊配置格式定義請參見本文上方驅動與裝置資訊配置內容。
    static int online_devices()
    {
      ...
    
        /* 擷取驅動和裝置配置 */
        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;
            }
        }
    
        /* 解析驅動和裝置配置 */
        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)
            {
                /* 解析配置內容 */
                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));
                }
    
                /* 註冊並上線裝置 */
                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. 將收到的裝置資料轉換為阿里雲IoT物模型格式並上報到物聯網平台,調用leda_report_properties介面上報裝置屬性資料,調用leda_report_event介面上報裝置事件。
    說明 此處使用虛擬設備,直接構造物模型格式的資料進行上報。
    /* 上報資料 */
    while (1)
    {
        /* 上報屬性 */
        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);
    
        /* 上報事件 */
        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. 處理雲端服務要求。實現服務要求的三個回呼函數如下所示。
    • get介面: 處理擷取裝置屬性的請求。
    • set介面: 處理設定裝置屬性的請求。
    • service介面:處理調用裝置自訂方法的請求。
    說明 本樣本使用get_properties_callback_cbset_properties_callback_cbcall_service_callback_cb三個回呼函數實現服務要求。
    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;
    }
說明 本樣本的完整工程源碼請參見Github源碼庫LED裝置驅動

至此,您已完成驅動編碼過程。