Exemple #1
0
void
log_buffer(struct ratbag *ratbag,
	enum ratbag_log_priority priority,
	const char *header,
	uint8_t *buf, size_t len)
{
	_cleanup_free_ char *output_buf = NULL;
	char *sep = "";
	unsigned int i, n;
	unsigned int buf_len;

	if (ratbag->log_handler &&
	    ratbag->log_priority > priority)
		return;

	buf_len = header ? strlen(header) : 0;
	buf_len += len * 3;
	buf_len += 1; /* terminating '\0' */

	output_buf = zalloc(buf_len);
	n = 0;
	if (header)
		n += snprintf_safe(output_buf, buf_len - n, "%s", header);

	for (i = 0; i < len; ++i) {
		n += snprintf_safe(&output_buf[n], buf_len - n, "%s%02x", sep, buf[i] & 0xFF);
		sep = " ";
	}

	log_msg(ratbag, priority, "%s\n", output_buf);
}
Exemple #2
0
static uint32_t construct_conn_json(char *buf,size_t buf_capacity)
{
    int n = 0;
    char * state = "unknown";

    if(conn_state.state == CONN_STATE_NOTCONNECTED) {
       state = "NOTCONNECTED"; 
    } else if(conn_state.state == CONN_STATE_CONNECTING) {
       state = "CONNECTING";
    } else if(conn_state.state == CONN_STATE_CONNECTED) {
        state = "CONNECTED";

        if(otn_is_online()) {
            state = "ONLINE";
        }
    }

    n += snprintf_safe(buf, buf_capacity, "{\"result\":{\"state\":\"%s\",\"auth_fail_count\":%d,\"conn_succes_count\":%d,\"conn_fail_count\":%d,\"dhcp_fail_count\":%d",
            state, conn_state.auth_failed,
            conn_state.conn_success,
            conn_state.conn_failed,
            conn_state.dhcp_failed);

    n += snprintf_safe(buf + n,buf_capacity - n,"}}");

    return n;
}
Exemple #3
0
void test_strncat(){
	int8_t tx[5];
	int8_t rx[32];
	int len;
	printf("-----test strncat-----\n");
	snprintf_safe(tx, 5, "%s", "bi");
	snprintf_safe(rx, 32, "%s", "ngo 12345");
	len = strlen_safe(tx);
	printf("tx='%s' len=%d\n", tx, len);
	strncat_safe((char*)tx, (char*)rx, 5);
	len = strlen_safe(tx);
	printf("tx='%s' len=%d %s\n", tx, len, (len == 4) ? "DONE" : "FAILED");
}
Exemple #4
0
void test_snprintf(){
	int8_t buffer[1024];
	printf("-----test snprintf-----\n");
	snprintf_safe(buffer, 1024, "char=%c int=%d float=%f string='%s' l=%lld\n",
			'A', 10, 11.23, "abcd", 4334556405L);
	printf("%s", buffer);
}
Exemple #5
0
void test_popen(){
	int8_t buffer[1024];
	int ret;
	FILE* f;
	printf("-----test popen-----\n");
	snprintf_safe(buffer, 1024, "ls -la;echo risk > risk_done");
//	printf("exec '%s' ", buffer);
	f = popen_safe(buffer, "r");
	if(f){
		memset(buffer, 0, 1024);
		ret = fread(buffer, 1, 1023, f);
		if(ret > 0) printf("%s\n", buffer);
		pclose(f);
	}else{
		printf("popen failed\n");
	}
}
Exemple #6
0
int report_ap_info_change_event()
{
#define JS_BUF_LEN (128)

   char *js_buf = malloc(JS_BUF_LEN);
   if(!js_buf) {
       LOG_ERROR("no enough mem to report event\r\n");
       
       //clear flags
       clear_ap_info_change_flag();
       return -1;
   }

   int n = snprintf_safe(js_buf,JS_BUF_LEN,"{\"method\":\"event.router_changed\",\"params\":");
   
   if(ap_info_change == ANY_ONE) {
       n += snprintf_safe(js_buf+n,JS_BUF_LEN - n,"\"ssid or passwd changed\"}");
   } else {
       n += snprintf_safe(js_buf+n,JS_BUF_LEN-n,"\""); 

       if(is_ssid_changed()) {
           n += snprintf_safe(js_buf+n,JS_BUF_LEN -n,"ssid changed ");
       }

       if(is_passwd_changed()) {
           n += snprintf_safe(js_buf+n,JS_BUF_LEN-n,"passwd changed");
       }
   
       n += snprintf_safe(js_buf+n,JS_BUF_LEN-n,"\"}");
   }

   LOG_DEBUG("report :len is %d %s \r\n",n,js_buf);

   //clear flags
   clear_ap_info_change_flag();
   
   report_success = false;
   int retry_count = 3;

   while(!report_success && retry_count > 0) {
       ot_api_method(js_buf,n,report_ack, NULL);
       retry_count --; 

       //it's diffcult to query ot status, so just wait
       os_thread_sleep(3000);
   }

   free(js_buf);

   return 0;
}
static void report_reset_event()
{
#define JS_BUF_LEN (64)

   char *js_buf = malloc(JS_BUF_LEN);
   if(!js_buf) {
       return;
   }

   int n = snprintf_safe(js_buf,JS_BUF_LEN,"{\"method\":\"event.dev_is_reset\",\"params\":\"\"}");

   LOG_DEBUG("report :len is %d %s \r\n",n,js_buf);

   ot_api_method(js_buf,n, NULL, NULL);

   //make sure the report is send out before reboot, but do not assure reach the server
   os_thread_sleep(100);

   free(js_buf);
}
Exemple #8
0
/*
 * Opens a JUNOScript session on the localhost using jade for authentication
 */
js_session_t *
js_session_open_localhost (js_session_opts_t *jsop, int flags, 
                           const char *auth_socket)
{
    js_session_t *jsp;
    js_session_opts_t jso; 
    int conn_addr_len, conn_sock;
    char auth_rpc[4 * BUFSIZ];
    char *auth_resp;
    struct sockaddr_un conn_addr;

    if (jsop == NULL) {
        bzero(&jso, sizeof(jso));
        jsop = &jso;
    }    

    if (auth_socket == NULL) {
	jsio_trace("Missing mandatory auth socket");
        return NULL;
    }

    js_initialize();

    if (jsop->jso_stype == ST_DEFAULT)
        jsop->jso_stype = js_default_stype;

    if (jsop->jso_server == NULL || *jsop->jso_server == '\0') {
        jsop->jso_server = js_default_server;
        if (jsop->jso_server == NULL || *jsop->jso_server == '\0')
            return NULL;
    }

    /*
     * Check whether the junoscript session already exists for the given 
     * hostname, if so then return that.
     */
    jsp = js_session_find(jsop->jso_server, jsop->jso_stype);
    if (jsp)
        return jsp;

    conn_sock = socket(PF_LOCAL, SOCK_STREAM, 0);

    if (conn_sock < 0) {
        jsio_trace("Failed to create socket");
        return NULL;
    }

    /*
     * Set up connection for authentication
     */
#ifdef HAVE_SUN_LEN
    conn_addr.sun_len = sizeof(struct sockaddr_un);
#endif /* HAVE_SUN_LEN */
    conn_addr.sun_family = AF_UNIX;
    strcpy(conn_addr.sun_path, auth_socket);

#ifdef HAVE_SUN_LEN
    conn_addr_len = sizeof(conn_addr.sun_len) + sizeof( conn_addr.sun_family)
#else
    conn_addr_len = sizeof( conn_addr.sun_family)
#endif /* HAVE_SUN_LEN */
        + strlen(conn_addr.sun_path);

    /*
     * Connect on the socket to exec jade that handles authentication and
     * provides us with a Junoscript session
     */
    if (connect(conn_sock, (struct sockaddr *)&conn_addr, conn_addr_len) < 0) {
        jsio_trace("Failed to connect for authentication");
        return NULL;
    } else {
	jsp = js_session_create_internal(jsop->jso_server, -1, conn_sock, 
					 conn_sock, -1, jsop->jso_stype, 
					 flags);

	if (jsp == NULL)
	    return NULL;

        if (js_session_init(jsp)) {
            js_session_terminate(jsp);
            return NULL;
        }

        jsio_trace("Session initialized");
        snprintf_safe(auth_rpc, sizeof(auth_rpc),
                     "<rpc><request-login><username>%s</username>"
                     "<challenge-response>%s</challenge-respone>"
                     "</request-login></rpc>\n", jsop->jso_username,
                     jsop->jso_passphrase);

        /*
         * Write authetication RPC into the socket
         */
        if (write(conn_sock, auth_rpc, strlen(auth_rpc)) < 0) {
            jsio_trace("Failed to send authentication rpc");
            bzero(auth_rpc, strlen(auth_rpc));
            bzero(jsop->jso_passphrase, strlen(jsop->jso_passphrase));
            return NULL;
        }

        bzero(auth_rpc, strlen(auth_rpc));
        bzero(jsop->jso_passphrase, strlen(jsop->jso_passphrase));

        if (!fbuf_has_buffered(jsp->js_fbuf)) {
            if (js_initial_read(jsp, JS_READ_TIMEOUT, 0)) {
                return NULL;
            }
        }

        /*
         * Read the response and look for status in authentication reply
         */
	for (;;) {
	    auth_resp = js_gets_timed(jsp, 0, JS_READ_QUICK);

	    if (auth_resp == NULL)
		break;

            if (strstr(auth_resp, "<status>fail</status>")) {
                jsio_trace("Authentication failed");
                js_session_terminate(jsp);
                return NULL;
            } else if (strstr(auth_resp, "<status>success</status>")) {
                jsio_trace("Authentication successful");
                /*
                 * Add the session details to patricia tree
                 */
                if (!js_session_add(jsp)) {
                    js_session_terminate(jsp);
                    return NULL;
                }
                return jsp;
            }
	}
    }

    js_session_terminate(jsp);
    return NULL;
}
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;

}
/****************************************
* generate new token to global token array
****************************************/
static int deal_with_token(bool should_re_gen)
{
    int ret;

    #define STR_LEN 35
    char strval[STR_LEN] = {0};
    psm_handle_t handle;

    ret = psm_open(&handle,"ot_config");
    if(ret) {
        LOG_ERROR("open psm area error \r\n");
        return -WM_FAIL;
    }
    
    ret = psm_get(&handle,OT_TOKEN,strval,STR_LEN);
    if(!ret && !should_re_gen) { //token already exist
        //read token_string to token array
        arch_axtobuf_detail(strval, sizeof(strval), token, sizeof(token), NULL);
        goto exit;
    }

    //should regenerator token
    if(ret) { //which mean first time generate token
        memset(token,0,sizeof(token));
    }

    //max length is 10 + 16 + 16 +6 = 48 
    #define GEN_BUF_LEN (52)
    uint8_t  token_generate_buffer[GEN_BUF_LEN];
    int n = 0;

    n = snprintf_safe((char *)token_generate_buffer,GEN_BUF_LEN,"%u",os_ticks_get());
   
    memcpy(token_generate_buffer + n,cfg_struct.key,OT_PROTO_DEVICE_KEY_SIZE);
    n+= OT_PROTO_DEVICE_KEY_SIZE;
    
    memcpy(token_generate_buffer + n,token,sizeof(token));
    n += sizeof(token);

    memcpy(token_generate_buffer + n,cfg_struct.mac_address,6);
    n += 6;
    
    //generate token
    md5(token_generate_buffer,n,token);
   
    snprintf_safe(strval,STR_LEN,"%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x",
                token[0],
                token[1],
                token[2],
                token[3],
                token[4],
                token[5],
                token[6],
                token[7],
                token[8],
                token[9],
                token[10],
                token[11],
                token[12],
                token[13],
                token[14],
                token[15]
                );
    ret = psm_set(&handle,OT_TOKEN,strval);
    if(ret){
        LOG_ERROR("write token fail");
        goto exit;
    }

    LOG_DEBUG("generate token success \r\n");

exit:
    psm_close(&handle);
    return ret;
}