int mc_usb_write(void *buf, size_t n) { unsigned long t, d; uint8_t *p; int ret = -1; if (n > USB_BUF_SIZE) goto bail; if (!USBActive()) goto bail; t = os_ticks_get(); d = t < usb_last_tick ? ~0UL - usb_last_tick + t : t - usb_last_tick; if (d >= USB_TIMEOUT_TICKS) { usb_buf_ptr = 0; usb_last_tick = os_ticks_get(); } if (usb_buf_ptr + n > USB_BUF_SIZE) { usb_buf_ptr = 0; if (d < USB_TIMEOUT_TICKS) os_thread_sleep(USB_TIMEOUT_TICKS); usb_last_tick = os_ticks_get(); } p = &usb_buf[usb_buf_ptr]; memcpy(p, buf, n); if (usb_drv_write(p, n) != (int)n) goto bail; usb_buf_ptr += n; ret = 0; bail: return ret; }
/** * All application specific initializations are performed here. */ int main(void) { /* Random seed. Since both master and slave use os_ticks_get() * at the same time, both will use same seed and hence * will generate same random numbers in exact same sequence */ srand(os_ticks_get()); /* Initialize wmstdio console */ wmstdio_init(UART0_ID, 0); wmprintf("ssp_master_demo application Started\r\n"); /* Initialize SSP Driver */ ssp_drv_init(SSP_ID); /* Set SPI clock frequency (Optional) */ ssp_drv_set_clk(SSP_ID, 1000000); /* Configure SSP as SPI device. SSP0 is configured as master. */ ssp = ssp_drv_open(SSP_ID, SSP_FRAME_SPI, SSP_MASTER, DMA_DISABLE, -1, 0); /* Create the application thread */ return os_thread_create(&app_thread, /* thread handle */ "spi_demo", /* thread name */ ssp_master_write, /* entry function */ 0, /* argument */ &app_stack, /* stack */ OS_PRIO_2); /* priority - medium low */ }
static inline int i2c_timeout(wm_i2c *i2c, uint32_t tout) { uint32_t t = os_ticks_get(); uint32_t tt = os_msec_to_ticks(tout); return tout != 0 && (t >= i2c->sticks ? (t - i2c->sticks) : ((UINT_MAX - i2c->sticks) + t)) >= tt; }
/** * @brief Generate 16 Bytes Nonce * * @param buf A pointer to store nonce * @return None */ void wps_generate_nonce_16B(u8 *buf) { int i, randNum; unsigned int seed; ENTER(); seed = os_ticks_get(); for (i = 0; i < 4; i++) { srand(seed); randNum = rand(); memcpy(buf + i * 4, &randNum, sizeof(int)); seed = seed * 1103515245 + 12345 * i; } LEAVE(); }
/* WPS main processing thread * */ static void wps_main(os_thread_arg_t data) { #ifdef CONFIG_P2P struct timeval tv, now; WPS_DATA *wps_s = (WPS_DATA *) &wps_global; WFD_DATA *pwfd_data = &wps_global.wfd_data; bss_config_t bss_config; #endif int ret; #ifdef CONFIG_WPA2_ENTP struct wpa2_command *wpa2_cmd = NULL; WPS_DATA *wps_s = (WPS_DATA *) &wps_global; #else struct wps_command *wps_cmd = NULL; #endif #ifdef CONFIG_P2P memset(&bss_config, 0, sizeof(bss_config_t)); /* * Download Wfd configuration if supplied with -p command line. */ if (local_wcc->role == WFD_ROLE) { if (wfd_wlan_update_bss_mode(BSS_MODE_WIFIDIRECT_GO) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to initialize " "BSS Mode!\n"); goto fail; } /* Set WFD uAP configuration */ wifidirectapcmd_sys_config(); if (wfd_wlan_update_bss_mode(BSS_MODE_WIFIDIRECT_CLIENT) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to initialize " "BSS Mode!\n"); goto fail; } /* Set WFD configuration */ wifidirectcmd_config(); if (wfd_set_mode(WFD_MODE_START) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to " "initialize WFD!\n"); goto fail; } if (auto_go) { if (wfd_wlan_update_bss_mode(BSS_MODE_WIFIDIRECT_GO) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to " "initialize BSS Mode!\n"); goto fail; } if (wfd_set_mode(WFD_MODE_START_GROUP_OWNER) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to " "initialize WFD!\n"); goto fail; } if (!wps_s->current_ssid.ssid_len) { apcmd_get_bss_config(&bss_config); load_cred_info(wps_s, gpwps_info, &bss_config); /* * For Wi-Fi Direct, we need to * convert the passphrase to PSK. * * Hence update the default * passphrase */ wlan_generate_psk(gpwps_info); } if (wps.cb(P2P_AUTO_GO_STARTED, &WFD_devicename, strlen(WFD_devicename)) == -WM_FAIL) P2P_LOG("WFD Callback failed for " "event: %d\r\n", P2P_AUTO_GO_STARTED); if (wlan_add_network(&p2p_uap_network) != WLAN_ERROR_NONE) { wps_printf(MSG_ERROR, "failed to add " "wfd network\r\n"); goto fail; } if (wlan_start_network(p2p_uap_network.name) != WLAN_ERROR_NONE) { wps_printf(MSG_ERROR, "failed to start " "wfd network\r\n"); goto fail; } } else { mlanconfig_bgscan(1); if (wfd_set_mode(WFD_MODE_START_FIND_PHASE) != WPS_STATUS_SUCCESS) { wps_printf(MSG_ERROR, "ERROR - Fail to " "initialize WFD!\n"); goto fail; } wps_set_wfd_cfg(); if (wps.cb(P2P_DEVICE_STARTED, &WFD_devicename, strlen(WFD_devicename)) == -WM_FAIL) P2P_LOG("WFD Callback failed for " "event: %d\r\n", P2P_DEVICE_STARTED); wfd_start_peer_ageout_timer(pwfd_data); p2p_scan_on = 1; } } #endif #ifndef CONFIG_P2P if (wps.cb(WPS_STARTED, NULL, 0) == -WM_FAIL) WPS_LOG("WPS Callback failed for event: %d\r\n", WPS_STARTED); #endif #ifndef CONFIG_WPA2_ENTP wps_register_rx_callback(WPSEAPoLRxDataHandler); #endif while (1) { #ifdef CONFIG_WPA2_ENTP ret = os_queue_recv(&wps.cmd_queue, &wpa2_cmd, RX_WAIT); if (ret != WM_SUCCESS || wpa2_cmd == NULL) continue; #else #ifdef CONFIG_P2P if (!auto_go && p2p_scan_on) { if (wps_loop.timeout) { now.tv_sec = os_ticks_to_msec(os_ticks_get()) / 1000; now.tv_usec = (os_ticks_to_msec (os_ticks_get()) % 1000) * 1000; if (timer_cmp(&now, &wps_loop.timeout->time)) timersub(&wps_loop.timeout->time, &now, &tv); else tv.tv_sec = tv.tv_usec = 0; } /* check if some registered timeouts have occurred */ if (wps_loop.timeout) { struct wps_timeout_s *tmp; now.tv_sec = os_ticks_to_msec(os_ticks_get()) / 1000; now.tv_usec = (os_ticks_to_msec (os_ticks_get()) % 1000) * 1000; if (!timer_cmp(&now, &wps_loop.timeout->time)) { tmp = wps_loop.timeout; wps_loop.timeout = wps_loop.timeout->next; tmp->handler(tmp->callback_data); wps_mem_free(tmp); } } } if (go_request) { p2p_scan_on = 0; g_method = CMD_WPS_PBC; wps_init(); go_request = 0; } ret = os_queue_recv(&wps.cmd_queue, &wps_cmd, os_msec_to_ticks(1000)); #else ret = os_queue_recv(&wps.cmd_queue, &wps_cmd, RX_WAIT); #endif if (ret != WM_SUCCESS || wps_cmd == NULL) continue; #endif #ifdef CONFIG_WPA2_ENTP memcpy(&wps_s->wpa2_network, &wpa2_cmd->wpa2_network, sizeof(struct wlan_network)); wps_mem_free(wpa2_cmd); g_method = CMD_WPS_PBC; ret = wps_private_info_allocate(&gpwps_info); ret = wps_init(); continue; #else g_method = wps_cmd->command; g_channel = wps_cmd->res.channel; #ifdef CONFIG_P2P g_bssid = (u8 *) wps_cmd->res.bssid; g_ssid = (u8 *) wps_cmd->res.ssid; #else memcpy(g_ssid, (u8 *) wps_cmd->res.ssid, MAX_SSID_LEN + 1); memcpy(g_bssid, (u8 *) wps_cmd->res.bssid, ETH_ALEN); #endif wps_mem_free(wps_cmd); switch (g_method) { case CMD_WPS_PIN: g_gen_pin = wps_cmd->wps_pin; wps_init(); break; case CMD_WPS_PBC: wps_init(); break; } #endif } #ifdef CONFIG_P2P fail: if (wps.cb(P2P_FAILED, NULL, 0) == -WM_FAIL) P2P_LOG("WPS Callback failed for event: %d\r\n", P2P_FAILED); wps_stop(); #endif }
static inline void i2c_timer_start(wm_i2c *i2c) { i2c->sticks = os_ticks_get(); }
/**************************************** * 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; }