1. 程式人生 > >AliOS-Things--EMW3060--linkkitapp

AliOS-Things--EMW3060--linkkitapp

windows10 環境下,編譯完成之後下載到模組裡面去:

一、編譯資訊

編譯資訊如下:

> Executing task: aos make [email protected] <

aos-cube version: 0.2.60
Check if required tools for mk3060 exist
Build AOS Now
TOOLCHAIN_PATH=D:/Output/AliOS/AliOS-Things/build/compiler/gcc-arm-none-eabi/Win32/bin/
Components: linkkitapp board_mk3060 moc108 vcall kernel_init auto_component linkkit_sdk iotx-hal netmgr framework libywss uOTA cjson cli armv5 newlib_stub rhino yloop kv alicrypto digest_algorithm net framework_runtime app_runtime libprov activation chip_code mbedtls hal log vfs irot vfs_device libkm libplat_gen
Skipping building bootloader due to \"total\" is not set
Making out/[email protected]/ld/mx108.ld Compiling auto_component Making out/[email protected]/libraries/auto_component.a Making [email protected] Making [email protected] Making [email protected] AOS MEMORY MAP |==============================================
===================| | MODULE | ROM | RAM | |=================================================================| | newlib_stub | 296 | 0 | | alicrypto | 40100 | 8784 | | kv |
2929 | 36 | | libc_nano | 29062 | 464 | | vcall | 6013 | 4 | | kernel_init | 1147 | 48 | | rhino | 14225 | 7721 | | libplat_gen | 513 | 0 | | framework | 240 | 12 | | uOTA | 13345 | 858 | | vfs | 1875 | 1209 | | digest_algorithm | 224 | 0 | | libgcc | 6168 | 0 | | linkkitapp | 19898 | 22 | | activation | 2743 | 8776 | | chip_code | 298 | 240 | | libprov | 624 | 0 | | libywss | 40256 | 1442 | | yloop | 1421 | 24 | | board_mk3060 | 598 | 24 | | iotx-hal | 7583 | 224 | | moc108 | 346 | 32 | | armv5 | 388 | 0 | | netmgr | 1823 | 246 | | libmoc108 | 165113 | 52761 | | mbedtls | 27953 | 76 | | cjson | 1958 | 20 | | libiot_sdk | 373371 | 3172 | | log | 402 | 20 | | net | 46165 | 3676 | | cli | 5945 | 549 | | hal | 478 | 12 | | *fill* | 555 | 192 | |=================================================================| | TOTAL (bytes) | 814055 | 90644 | |=================================================================| 870468 55a091ce758cbd91d263de6360df4b85 Build complete Making .gdbinit

記憶體

ROM:0.81MB
RAM:90.644KB

模組 RAM Flash
EMW3060 256KB 2MB

上電覆位之後

enter low level!
mac fc:ee:e6: 8:6c:b7
leave low level!
app_init finished
start-----------hal
trace config close!!!
[000004]<V> aos framework init.
[000004]<V> IOTX_AWSS_START
awss version: smarthome-bd18d6d<DEBUG> [zconfig_init#2115] : awss lib smarthome-bd18d6d

Soft_AP_start
chan 1
[1] ssid:CMCC-WEB, mac:5866ba6f6760, chn:1, auth:open, none, none, rssi:-236, adha:0
[2] ssid:CMCC, mac:5866ba6f6761, chn:1, auth:wpa2-8021x, aes, aes, rssi:-236, adha:0

 由上可知,linkkitapp例程開始之後就會進入配網模式,這在程式碼中怎麼體現的呢?

二、程式碼分析

1、哪裡是入口主函式?

int application_start(int argc, char **argv)
{
    aos_set_log_level(AOS_LL_DEBUG);
    
    printf("Hello liefyuan\r\n");
    
    netmgr_init();
    aos_register_event_filter(EV_KEY, linkkit_key_process, NULL);
    aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
    aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);

    set_iotx_info();
    aos_task_new("netmgr", start_netmgr, NULL, 4096);

    aos_loop_run();

    return 0;
}

列印了資訊,說明確實是以這個函式作為入口的。

enter low level!
mac fc:ee:e6: 8:6c:b7
leave low level!
app_init finished
start-----------hal
trace config close!!!
[000004]<V> aos framework init.
Hello liefyuan
[000004]<V> IOTX_AWSS_START

2、哪裡是進入配網模式的?

死辦法,如下:

int application_start(int argc, char **argv)
{
    aos_set_log_level(AOS_LL_DEBUG);

    printf("Hello 1\r\n");

    netmgr_init();
    printf("Hello 2\r\n");
    aos_register_event_filter(EV_KEY, linkkit_key_process, NULL);
    printf("Hello 3\r\n");
    aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
    printf("Hello 4\r\n");
    aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
    printf("Hello 5\r\n");

    set_iotx_info();
    printf("Hello 6\r\n");
    aos_task_new("netmgr", start_netmgr, NULL, 4096);
    printf("Hello 7\r\n");

    aos_loop_run();
    printf("Hello 8\r\n");

    return 0;
}

列印資訊

trace config close!!!
[000004]<V> aos framework init.
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
[000006]<V> IOTX_AWSS_START
awss version: smarthome-bd18d6d<DEBUG> [zconfig_init#2115] : awss lib smarthome-bd18d6d

Soft_AP_start

可以推測出來,以下就是進入配網模式的函式。

 aos_task_new("netmgr", start_netmgr, NULL, 4096);

3、配網模式函式:aos_task_new(“netmgr”, start_netmgr, NULL, 4096);

看看它的的定義:
 可以看出這是個任務建立函式,有點FreeRTOS的任務建立風格。路徑為:AliOS-Things\include\aos\kernel.h

/**
 * Create a task.
 *
 * @param[in]  name       task name.
 * @param[in]  fn         function to run.
 * @param[in]  arg        argument of the function.
 * @param[in]  stacksize  stack-size in bytes.
 *
 * @return  0: success.
 */
int aos_task_new(const char *name, void (*fn)(void *), void *arg, int stack_size);

於是知道了start_netmgr是個函式名字,看看這個函式是咋樣的:

static void start_netmgr(void *p)
{
    iotx_event_regist_cb(linkkit_event_monitor);
    netmgr_start(true);
    aos_task_exit(0);
}

從函式名上可以看到,這個函式裡面註冊了一個事件回撥函式:

/**
 * @brief event list used for iotx_regist_event_monitor_cb
 */
enum iotx_event_t {
    IOTX_AWSS_START = 0x1000,       // AWSS start without enbale, just supports device discover
    IOTX_AWSS_ENABLE,               // AWSS enable
    IOTX_AWSS_LOCK_CHAN,            // AWSS lock channel(Got AWSS sync packet)
    IOTX_AWSS_CS_ERR,               // AWSS AWSS checksum is error
    IOTX_AWSS_PASSWD_ERR,           // AWSS decrypt passwd error
    IOTX_AWSS_GOT_SSID_PASSWD,      // AWSS parse ssid and passwd successfully
    IOTX_AWSS_CONNECT_ADHA,         // AWSS try to connnect adha (device discover, router solution)
    IOTX_AWSS_CONNECT_ADHA_FAIL,    // AWSS fails to connect adha
    IOTX_AWSS_CONNECT_AHA,          // AWSS try to connect aha (AP solution)
    IOTX_AWSS_CONNECT_AHA_FAIL,     // AWSS fails to connect aha
    IOTX_AWSS_SETUP_NOTIFY,         // AWSS sends out device setup information (AP and router solution)
    IOTX_AWSS_CONNECT_ROUTER,       // AWSS try to connect destination router
    IOTX_AWSS_CONNECT_ROUTER_FAIL,  // AWSS fails to connect destination router.
    IOTX_AWSS_GOT_IP,               // AWSS connects destination successfully and got ip address
    IOTX_AWSS_SUC_NOTIFY,           // AWSS sends out success notify (AWSS sucess)
    IOTX_AWSS_BIND_NOTIFY,          // AWSS sends out bind notify information to support bind between user and device
    IOTX_CONN_CLOUD = 0x2000,       // Device try to connect cloud
    IOTX_CONN_CLOUD_FAIL,           // Device fails to connect cloud, refer to net_sockets.h for error code
    IOTX_CONN_CLOUD_SUC,            // Device connects cloud successfully
    IOTX_RESET = 0x3000,            // Linkkit reset success (just got reset response from cloud without any other operation)
};

/**
 * @brief register callback to monitor all event from system.
 *             註冊監控回撥函式來監控所有來自系統的事件
 * @param callback, when some event occurs, the system will trigger callback to user.
 *                  refer to enum iotx_event_t for event list supported.
 *          當有某些時間發生時,系統將觸發使用者的回撥函式,事件列表上列舉的事件型別都支援
 * @return 0 when success, -1 when fail.
 * @note: user should make sure that callback is not block and runs to complete fast.
 */
extern int iotx_event_regist_cb(void (*monitor_cb)(int event));

看看事件監視器函式linkkit_event_monitor()是咋樣的:

  • linkkit_event_monit函式不能阻塞執行,應該要執行一些能夠快速執行的操作
  • 如果使用者想要做一些費時的複雜操作,使用者應該建立一個任務來做,
  • 而不是在linkkit_event_monit函式中去做複雜操作。
/*
 * Note:
 * the linkkit_event_monitor must not block and should run to complete fast
 * if user wants to do complex operation with much time,
 * user should post one task to do this, not implement complex operation in
 * linkkit_event_monitor
 */

static void linkkit_event_monitor(int event)
{
    switch (event) {
        case IOTX_AWSS_START: // AWSS start without enbale, just supports device
                              // discover
            // operate led to indicate user
            LOG("IOTX_AWSS_START");
            break;
        case IOTX_AWSS_ENABLE: // AWSS enable
            LOG("IOTX_AWSS_ENABLE");
            // operate led to indicate user
            break;
        case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
            LOG("IOTX_AWSS_LOCK_CHAN");
            // operate led to indicate user
            break;
        case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
            LOG("IOTX_AWSS_PASSWD_ERR");
            // operate led to indicate user
            break;
        case IOTX_AWSS_GOT_SSID_PASSWD:
            LOG("IOTX_AWSS_GOT_SSID_PASSWD");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
                                     // discover, router solution)
            LOG("IOTX_AWSS_CONNECT_ADHA");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
            LOG("IOTX_AWSS_CONNECT_ADHA_FAIL");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
            LOG("IOTX_AWSS_CONNECT_AHA");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
            LOG("IOTX_AWSS_CONNECT_AHA_FAIL");
            // operate led to indicate user
            break;
        case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
                                     // (AP and router solution)
            LOG("IOTX_AWSS_SETUP_NOTIFY");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
            LOG("IOTX_AWSS_CONNECT_ROUTER");
            // operate led to indicate user
            break;
        case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
                                            // router.
            LOG("IOTX_AWSS_CONNECT_ROUTER_FAIL");
            // operate led to indicate user
            break;
        case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
                               // ip address
            LOG("IOTX_AWSS_GOT_IP");
            // operate led to indicate user
            break;
        case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
                                   // sucess)
            LOG("IOTX_AWSS_SUC_NOTIFY");
            // operate led to indicate user
            break;
        case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
                                    // support bind between user and device
            LOG("IOTX_AWSS_BIND_NOTIFY");
            // operate led to indicate user
            break;
        case IOTX_CONN_CLOUD: // Device try to connect cloud
            LOG("IOTX_CONN_CLOUD");
            // operate led to indicate user
            break;
        case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
                                   // net_sockets.h for error code
            LOG("IOTX_CONN_CLOUD_FAIL");
            // operate led to indicate user
            break;
        case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
            LOG("IOTX_CONN_CLOUD_SUC");
            // operate led to indicate user
            break;
        case IOTX_RESET: // Linkkit reset success (just got reset response from
                         // cloud without any other operation)
            LOG("IOTX_RESET");
            // operate led to indicate user
            break;
        default:
            break;
    }
}