当前位置: 首页 > 工具软件 > Alink > 使用案例 >

ESP8266--esp8266-alink-v1.0学习笔记(一)

燕博文
2023-12-01

下载地址:https://github.com/espressif/esp8266-alink-v1.0

  • 使用非透传
  • -
static xTaskHandle read_handle = NULL;
void start_alink_task(void *pvParameters)
{
    ALINK_LOGD("start demo task, free heap size %d", system_get_free_heap_size());

    /* fill main device info here */
    alink_product_t product_info = {
        .sn             = DEV_SN,
        .name           = DEV_NAME,
        .brand          = DEV_BRAND,
        .type           = DEV_TYPE,
        .category       = DEV_CATEGORY,
        .manufacturer   = DEV_MANUFACTURE,
        .version        = DEV_VERSION,
        .model          = DEV_MODEL,
        .key            = ALINK_KEY,
        .secret         = ALINK_SECRET,
        .key_sandbox    = ALINK_KEY_SANDBOX,
        .secret_sandbox = ALINK_SECRET_SANDBOX,
    };
    alink_key_trigger();  // 按键初始化,2个按键,一个重新配网,一个复位
    esp_info_init();      // esp信息初始化 

    esp_alink_init(&product_info, alink_event_handler); // 初始化这个设备,根据上面的信息,重点在这里

    xTaskCreate(read_task_test, "read_task_test", (1024 + 512) / 4, NULL, tskIDLE_PRIORITY + 5, &read_handle);

    vTaskDelete(NULL);
}
typedef struct device_info {
    /* optional */
    char sn[STR_SN_LEN];
    char name[STR_NAME_LEN];
    char brand[STR_NAME_LEN];
    char type[STR_NAME_LEN];
    char category[STR_NAME_LEN];
    char manufacturer[STR_NAME_LEN];
    char version[STR_NAME_LEN];

    /* mandatory */
    /* must follow the format xx:xx:xx:xx:xx:xx */
    char mac[STR_MAC_LEN];

    /* manufacturer_category_type_name */
    char model[STR_MODEL_LEN];

    /* mandatory for gateway, optional for child dev */
    char cid[STR_CID_LEN];
    char key[STR_KEY_LEN];
    char secret[STR_SEC_LEN];

    alink_callback dev_callback[MAX_CALLBACK_NUM];
    alink_func sys_callback[MAX_SYS_CALLBACK];
    char ip[20];
    char key_sandbox[STR_KEY_LEN];
    char secret_sandbox[STR_SEC_LEN];
} alink_product_t;
alink_err_t esp_alink_init(alink_product_t* info, alink_event_cb_t event_handler_cb)
{
    ALINK_PARAM_CHECK(!info);
    ALINK_PARAM_CHECK(!event_handler_cb);

    int ret = ALINK_OK;

    if (s_need_notify_app == NULL) {
        portENTER_CRITICAL();
        vSemaphoreCreateBinary(s_need_notify_app);
        if (s_need_notify_app) {
            xSemaphoreTake(s_need_notify_app, 0);
        }
        portEXIT_CRITICAL();
    }

    if (!xQueueEvent) {
        xQueueEvent = xQueueCreate(EVENT_QUEUE_NUM, sizeof(alink_event_t));
    }

    alink_set_loglevel(ALINK_SDK_LOG_LEVEL);
    xTaskCreate(alink_event_loop_task, "alink_event_loop_task", 256, event_handler_cb,
        tskIDLE_PRIORITY + 5, &event_handle);

    ret = alink_connect_ap();
    ALINK_ERROR_CHECK(ret != ALINK_OK, ALINK_ERR, "connect ap fail");

    ret = alink_trans_init(info);
    ALINK_ERROR_CHECK(ret != ALINK_OK, ALINK_ERR, "alink trans init fail");

    xTaskCreate(alink_post_data_task, "alink_post_data_task", 512+32, NULL,
        tskIDLE_PRIORITY + 5, &post_handle);

    return ret;
}
alink_err_t alink_trans_init(alink_product_t* info)
{
    ALINK_PARAM_CHECK(!info);

    alink_err_t ret  = ALINK_OK;
    post_data_enable = ALINK_TRUE;
    xQueueDownCmd = xQueueCreate(DOWN_CMD_QUEUE_NUM, sizeof(char *));
    xQueueUpCmd = xQueueCreate(UP_CMD_QUEUE_NUM, sizeof(char *));

    uint8 macaddr[6];
    char mac_str[STR_MAC_LEN] = {0};
    wifi_get_macaddr(0, macaddr);
    snprintf(info->mac, sizeof(info->mac), MACSTR, MAC2STR(macaddr));
    snprintf(info->cid, sizeof(info->cid), "%024d", system_get_chip_id());

    info->sys_callback[ALINK_FUNC_SERVER_STATUS] = alink_handler_systemstates_callback;
    info->dev_callback[ACB_SET_DEVICE_STATUS_ARRAY] = NULL;
    info->dev_callback[ACB_REQUEST_REMOTE_SERVICE] = NULL;
    info->dev_callback[ACB_GET_DEVICE_STATUS_BY_RAWDATA] = NULL;
    info->dev_callback[ACB_SET_DEVICE_STATUS_BY_RAWDATA] = NULL;
    info->dev_callback[ACB_REQUEST_DEVICE_UPGRADE] = NULL;
    info->dev_callback[ACB_REQUEST_DEVICE_UPGRADE_CANCEL] = NULL;
    info->dev_callback[ACB_REQUEST_DEVICE_UNUPGRADE] = NULL;

    //alink_enable_sandbox_mode();       //if in online environment, this function should be closed
    alink_set_callback(ALINK_FUNC_AVAILABLE_MEMORY, print_mem_callback);
    /* ALINK_CONFIG_LEN 2048 */
    alink_register_cb(ALINK_FUNC_READ_CONFIG, (void *) &alink_read_flash_config);
    alink_register_cb(ALINK_FUNC_WRITE_CONFIG, (void *) &alink_write_flash_config);
    alink_register_cb(ALINK_FUNC_GET_STATUS, alink_get_debuginfo);
    alink_register_cb(ALINK_FUNC_OTA_FIRMWARE_SAVE, esp_ota_firmware_save);
    alink_register_cb(ALINK_FUNC_OTA_UPGRADE, esp_ota_upgrade);
    /*start alink-sdk */
    alink_set_thread_stack_size(&g_thread_stacksize);

    alink_set_product_info(info);

#ifdef ALINK_PASSTHROUGH
    ret = alink_start_rawdata(info, rawdata_get_callback, rawdata_set_callback);
#else
    info->dev_callback[ACB_GET_DEVICE_STATUS] = main_dev_get_device_status_callback;
    info->dev_callback[ACB_SET_DEVICE_STATUS] = main_dev_set_device_status_callback;
    ret = alink_start(info);
#endif

    if(ret != ALINK_OK) {
        post_data_enable = ALINK_FALSE;
        alink_end();
        return ret;
    } 

    alink_wait_ap_connected();
    ALINK_LOGD("connected to ap!");
    struct station_config wifi_config;

    if (esp_info_load(NVS_KEY_WIFI_CONFIG, &wifi_config, sizeof(struct station_config)) <= 0) {
        wifi_station_get_config_default(&wifi_config);
        int cfg_len = esp_info_save(NVS_KEY_WIFI_CONFIG, &wifi_config, sizeof(struct station_config));
        ALINK_ERROR_CHECK(cfg_len < 0, ALINK_ERR, "write wifi cfg err");
    }

    if (xSemaphoreTake(s_need_notify_app, 1000 / portTICK_RATE_MS) == pdPASS) {
        aws_notify_app();
    }

    return ret;
}
 类似资料: