Ejemplo n.º 1
0
/* This is the main event handler for this project. The application framework
 * calls this function in response to the various events in the system.
 */
int common_event_handler(int event, void *data)
{
    int ret;
    static bool is_cloud_started;
    switch (event) {
    case AF_EVT_WLAN_INIT_DONE:
        ret = psm_cli_init();
        if (ret != WM_SUCCESS)
            wmprintf("Error: psm_cli_init failed\r\n");
        int i = (int) data;

        if (i == APP_NETWORK_NOT_PROVISIONED) {
            wmprintf("\r\nPlease provision the device "
                     "and then reboot it:\r\n\r\n");
            wmprintf("psm-set network ssid <ssid>\r\n");
            wmprintf("psm-set network security <security_type>"
                     "\r\n");
            wmprintf("    where: security_type: 0 if open,"
                     " 3 if wpa, 4 if wpa2\r\n");
            wmprintf("psm-set network passphrase <passphrase>"
                     " [valid only for WPA and WPA2 security]\r\n");
            wmprintf("psm-set network configured 1\r\n");
            wmprintf("pm-reboot\r\n\r\n");
        } else
            app_sta_start();

        break;
    case AF_EVT_NORMAL_CONNECTED:
        set_device_time();
        if (!is_cloud_started) {
            configure_gpios();
            os_thread_sleep(2000);
            ret = os_thread_create(
                      /* thread handle */
                      &app_thread,
                      /* thread name */
                      "evrythng_demo_thread",
                      /* entry function */
                      evrythng_task,
                      /* argument */
                      0,
                      /* stack */
                      &app_stack,
                      /* priority - medium low */
                      OS_PRIO_3);
            is_cloud_started = true;
        }
        break;
    default:
        break;
    }

    return 0;
}
Ejemplo n.º 2
0
void event_wlan_init_done(void *data)
{
	int ret;
	char temp[20];
    
    //init cfg_struct from otp
    config_cfg_struct();
    
    //print did & mac
    uint32_t digital_did = get_device_did_digital();
    
    //for factory check did and mac
    wmprintf("digital_did is %d \r\n", digital_did);
    snprintf_safe(temp,sizeof(temp),"%.2x%.2x%.2x%.2x%.2x%.2x",
                    cfg_struct.mac_address[0],
                    cfg_struct.mac_address[1],
                    cfg_struct.mac_address[2],
                    cfg_struct.mac_address[3],
                    cfg_struct.mac_address[4],
                    cfg_struct.mac_address[5]
                    );
    wmprintf("mac address is %s\r\n",temp);//for factory checking
    wmprintf("last four byte of key is %.2x%.2x%.2x%.2x\r\n",cfg_struct.key[OT_PROTO_DEVICE_KEY_SIZE -4],
                                                               cfg_struct.key[OT_PROTO_DEVICE_KEY_SIZE -3],
                                                               cfg_struct.key[OT_PROTO_DEVICE_KEY_SIZE -2],
                                                               cfg_struct.key[OT_PROTO_DEVICE_KEY_SIZE -1]
                                                               );

    //print chip boot status info
    if (is_factory_mode_enable()) {
        wmprintf(FACTORY_STARTUP_PASS);
    } else {
        wmprintf(FACTORY_NORMAL_BOOT);
    }

    //gen token
    deal_with_token(false); 

    //set wifi work channel 
    wlan_set_country(COUNTRY_CN);

#ifndef RELEASE
    LOG_DEBUG("dump token value\r\n");
    xiaomi_dump_hex(token,16);
#endif

    ret = netif_add_udp_broadcast_filter(fixed_cfg_data.lport);
    if (ret != WM_SUCCESS) {
        LOG_ERROR("Failed to add ot local port to boardcast filter \r\n");
    }

    //ot start as idle
    start_otd();


#ifdef MIIO_COMMANDS
    OTN_ONLINE(mum_online_notifier);
    OTN_OFFLINE(mum_offline_notifier);
#endif
    OTN_ONLINE(ot_online_notifier);
    OTN_ONLINE(ot_offline_notifier);

    //generate dhcp name
    strcpy(dhcp_name_strval,cfg_struct.model);
    char *c = dhcp_name_strval;
    while(*c) {
        if('.' == *c) {
            *c = '-';
        }
        c++;
    }

    strcat(dhcp_name_strval,"_miio");
    snprintf_safe(temp,20,"%lu",digital_did);
    strcat(dhcp_name_strval,temp);

    /*
     * Start mDNS and advertize our hostname using mDNS
     */
    init_mdns_service_struct(dhcp_name_strval);


    ret = is_factory_mode_enable();

    //factory mode,connect to miio_default
    if(ret) {
        wmprintf("in factory mode, will auto connect to miio_default\r\n");

        struct wlan_network factory_net;

        memset(&factory_net,0,sizeof(struct wlan_network));
        strcpy(factory_net.name,FACTORY_NET_SSID);
        strcpy(factory_net.ssid,FACTORY_NET_SSID); 

        factory_net.security.type = WLAN_SECURITY_WPA2;
        
        factory_net.security.psk_len = strlen(FACTORY_NET_PASSPHASE);
        memcpy(factory_net.security.psk,FACTORY_NET_PASSPHASE,strlen(FACTORY_NET_PASSPHASE));

        factory_net.ip.ipv4.addr_type = 1; //use dhcp
        
        //will connect to miio_default
        app_sta_start_by_network(&factory_net);

	    goto safe_exit;

    }

    g_provisioned.state = (int) data;
    //already provisioned
    if(APP_NETWORK_PROVISIONED == g_provisioned.state) {
        LOG_DEBUG("already provisioned \r\n");
        set_device_network_state(SMT_CONNECTING);
        app_sta_start(); //just start sta mode , & it will auto connect to network
	    goto safe_exit;
     }

    //not provisioned & should start uap
    //Note: this interface only should be used for testing
    if(ENABLE_UAP == get_trigger_uap_info()) {
        wmprintf("should start UAP \r\n");
        
        app_uap_start_on_channel_with_dhcp(construct_ssid_for_smt_cfg(), NULL,DEFAULT_AP_CHANNEL);
        goto safe_exit;
    }


#if defined(ENABLE_AIRKISS) && (ENABLE_AIRKISS == 1)
    //not yet provisioned,start kuailian
    set_device_network_state(WAIT_SMT_TRIGGER);

    //trigger prov
	app_ctrl_notify_event(AF_EVT_INTERNAL_PROV_REQUESTED, (void *)NULL);

    //trigger scan
    void * msg;
    msg = (void *)SCAN_ROUTER;
    os_queue_send(&main_thread_sched_queue, &msg, OS_NO_WAIT);

#else
    LOG_DEBUG("start uap mode for smart config \r\n");
    app_uap_start_on_channel_with_dhcp(construct_ssid_for_smt_cfg(), NULL,DEFAULT_AP_CHANNEL);
    
    //start monitor timer 
    if(smt_cfg_monitor_init() ==WM_SUCCESS) {
        smt_cfg_monitor_start();
    }
#endif

safe_exit:
    return;

}