int rthw_wifi_connect(char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type) { int mode; rtw_wifi_setting_t setting; mode = rthw_wifi_mode_get(); if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP)) { return -1; } if(wext_get_mode(WLAN0_NAME, &mode) < 0) { rt_kprintf("L:%d wifi get mode err\n", __LINE__); return -1; } if(wifi_connect(ssid, security_type, password, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS) { rt_kprintf("wifi connect fail\n"); return -1; } rt_kprintf("wifi connect success\n"); rt_kprintf("Show Wi-Fi information\n"); wifi_get_setting(WLAN0_NAME,&setting); wifi_show_setting(WLAN0_NAME,&setting); return 0; }
static int wps_connect_to_AP_by_open_system(char *target_ssid) { int retry_count = 3, ret; if (target_ssid != NULL) { rtw_msleep_os(500); //wait scan complete. while (1) { ret = wifi_connect(target_ssid, RTW_SECURITY_OPEN, NULL, strlen(target_ssid), 0, 0, NULL); if (ret == RTW_SUCCESS) { //wps_check_and_show_connection_info(); break; } if (retry_count == 0) { printf("\r\n[WPS]Join bss failed\n"); return -1; } retry_count --; } // } else { printf("\r\n[WPS]Target SSID is NULL\n"); } return 0; }
/* Example of network connection in HomeKit * Wi-Fi connection with the stored WAC configuration * Set LWIP hostname to accessory name before DHCP * Setup IPv4 and IPv6 address. Suggest to set IPv6 address before IPv4 when mDNS is working */ void HomeKitNetworkConnection(void) { static char hostname[32]; WACPersistentConfig_t config; WACPlatformReadConfig(&config); if((config.name_len > 0) && (config.name_len < 32)) { memset(hostname, 0, sizeof(hostname)); memcpy(hostname, config.name, config.name_len); } else { uint8_t *mac = LwIP_GetMAC(&xnetif[0]); sprintf(hostname, "RealtekAmeba%02x%02x%02x", mac[3], mac[4], mac[5]); } netif_set_hostname(&xnetif[0], hostname); if((config.ssid_len > 0) && (config.ssid_len < 32) && (config.password_len >= 0) && (config.password_len < 32)) { if(wifi_connect((unsigned char *) config.ssid, (config.password_len) ? RTW_SECURITY_WPA2_AES_PSK : RTW_SECURITY_OPEN, (unsigned char *) config.password, config.ssid_len, config.password_len, 0, NULL) == RTW_SUCCESS) { #if LWIP_IPV6 LwIP_AUTOIP_IPv6(&xnetif[0]); #endif printf("\nIPv4 DHCP ..."); LwIP_DHCP(0, DHCP_START); #if LWIP_AUTOIP uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == 0) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 0)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } #endif } } }
static void wps_connect_to_AP_by_certificate(rtw_network_info_t *wifi) { #define RETRY_COUNT 3 int retry_count = RETRY_COUNT, ret; printf("\r\n=============== wifi_certificate_info ===============\n"); printf("\r\nwps_wifi.ssid = %s\n", wifi->ssid.val); printf("\r\nsecurity_type = %d\n", wifi->security_type); printf("\r\nwps_wifi.password = %s\n", wifi->password); printf("\r\nssid_len = %d\n", wifi->ssid.len); printf("\r\npassword_len = %d\n", wifi->password_len); while (1) { ret = wifi_connect((char*)wifi->ssid.val, wifi->security_type, (char*)wifi->password, wifi->ssid.len, wifi->password_len, wifi->key_id, NULL); if (ret == RTW_SUCCESS) { if(retry_count == RETRY_COUNT) rtw_msleep_os(1000); //When start wps with OPEN AP, AP will send a disassociate frame after STA connected, need reconnect here. if(RTW_SUCCESS == wifi_is_ready_to_transceive(RTW_STA_INTERFACE)){ //printf("\r\n[WPS]Ready to tranceive!!\n"); wps_check_and_show_connection_info(); break; } } if (retry_count == 0) { printf("\r\n[WPS]Join bss failed\n"); break; } retry_count --; } }
static int wifi_cli(int argc, char **argv) { struct wifi_if *wi; int i; if (argc < 2) { goto usage; } wi = wifi_if_lookup(0); if (!strcmp(argv[1], "start")) { wifi_start(wi); } else if (!strcmp(argv[1], "stop")) { wifi_stop(wi); } else if (!strcmp(argv[1], "scan")) { wifi_scan_start(wi); } else if (!strcmp(argv[1], "aps")) { int i; struct wifi_ap *ap; console_printf(" %32s %4s %4s %s\n", "SSID", "RSSI", "chan", "sec"); for (i = 0; i < wi->wi_scan_cnt; i++) { ap = (struct wifi_ap *)&wi->wi_scan[i]; console_printf("%2d:%32s %4d %4d %s\n", i, ap->wa_ssid, ap->wa_rssi, ap->wa_channel, ap->wa_key_type ? "X" : ""); } } else if (!strcmp(argv[1], "connect")) { if (argc < 2) { goto conn_usage; } i = strlen(argv[2]); if (i >= sizeof(wi->wi_ssid)) { goto conn_usage; } if (argc > 2) { i = strlen(argv[2]); if (i >= sizeof(wi->wi_key)) { goto conn_usage; } strcpy(wi->wi_key, argv[3]); } strcpy(wi->wi_ssid, argv[2]); if (wifi_connect(wi)) { conn_usage: console_printf("%s %s <ssid> [<key>]\n", argv[0], argv[1]); } } else { usage: console_printf("start|stop|scan|aps|connect <ssid> [<key>]\n"); } return 0; }
int main(int argc, char *argv[]) { int rc = inetcfg_init(); if (rc != EOK) { printf("%s: Failed connecting to inetcfg service (%d).\n", NAME, rc); return 1; } rc = dhcp_init(); if (rc != EOK) { printf("%s: Failed connecting to dhcp service (%d).\n", NAME, rc); return 1; } if (argc == 2) { if (!str_cmp(argv[1], "list")) return wifi_list(); } else if (argc > 2) { uint32_t index; rc = str_uint32_t(argv[2], NULL, 10, false, &index); if (rc != EOK) { printf("%s: Invalid argument.\n", NAME); print_syntax(); return EINVAL; } if (!str_cmp(argv[1], "scan")) { bool now = false; if (argc > 3) if (!str_cmp(argv[3], "-n")) now = true; return wifi_scan(index, now); } else if (!str_cmp(argv[1], "connect")) { char *pass = NULL; if (argc > 3) { if (argc > 4) pass = argv[4]; return wifi_connect(index, argv[3], pass); } } else if (!str_cmp(argv[1], "disconnect")) return wifi_disconnect(index); } print_syntax(); return EOK; }
static void ICACHE_FLASH_ATTR scan_done_slave(void *arg, STATUS status) { uint8 ssid[33]; char temp[128]; os_printf("scan done init\r\n"); bool found = false; os_timer_disarm(&scanTimer); if (status == OK) { struct bss_info *bss_link = (struct bss_info *)arg; bss_link = bss_link->next.stqe_next;//ignore first while (bss_link != 0) { os_memset(ssid, 0, 33); if (os_strlen(bss_link->ssid) <= 32) { os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid)); } else { os_memcpy(ssid, bss_link->ssid, 32); } //os_printf("%s \r\n", bss_link->ssid); if(os_strcmp(bss_link->ssid, MASTER_AP) == 0) { scancount = 0; found = true; if(ip_connected == 0) wifi_connect(bss_link->ssid, MASTER_PASSWORD, bss_link->bssid); } os_sprintf(temp,"+CWLAP:(%d,\"%s\",%d,\""MACSTR"\",%d)\r\n", bss_link->authmode, ssid, bss_link->rssi, MAC2STR(bss_link->bssid),bss_link->channel); os_printf(temp); bss_link = bss_link->next.stqe_next; } } scancount++; if(scancount > 3 && found == 0) { config_mode = MODE_MASTER_S; connect_attempts = 0; reset_slave(); return; } os_timer_arm(&scanTimer, 2000, 0); os_printf("scan done\r\n"); }
static void wifi_autoreconnect_thread(void *param) { int ret = RTW_ERROR; struct wifi_autoreconnect_param *reconnect_param = (struct wifi_autoreconnect_param *) param; printf("\n\rauto reconnect ...\n"); ret = wifi_connect(reconnect_param->ssid, reconnect_param->security_type, reconnect_param->password, reconnect_param->ssid_len, reconnect_param->password_len, reconnect_param->key_id, NULL); #if CONFIG_LWIP_LAYER if(ret == RTW_SUCCESS) { LwIP_DHCP(0, DHCP_START); #if LWIP_AUTOIP uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == 0) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 0)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } #endif } #endif vTaskDelete(NULL); }
int uartadapter_connect_wifi(rtw_network_info_t *p_wifi, uint32_t channel, uint8_t pscan_config) { int retry = 3; rtw_wifi_setting_t setting; int ret; while (1) { if(wifi_set_pscan_chan((uint8_t *)&channel, &pscan_config, 1) < 0){ printf("\n\rERROR: wifi set partial scan channel fail"); ret = SC_TARGET_CHANNEL_SCAN_FAIL; return ret; } ret = wifi_connect((char*)p_wifi->ssid.val, p_wifi->security_type, (char*)p_wifi->password, p_wifi->ssid.len, p_wifi->password_len, p_wifi->key_id, NULL); if (ret == RTW_SUCCESS) { ret = LwIP_DHCP(0, DHCP_START); wifi_get_setting(WLAN0_NAME, &setting); wifi_show_setting(WLAN0_NAME, &setting); if (ret == DHCP_ADDRESS_ASSIGNED) return SC_SUCCESS; else return SC_DHCP_FAIL; } if (retry == 0) { ret = SC_JOIN_BSS_FAIL; break; } retry --; } return ret; }
static void disp_task(void *params) { struct mg_connection *conn = NULL; struct conn_ctx ctx; printf("Starting network test...\n"); memset(data, 'A', sizeof(data)); memset(&ctx, 0, sizeof(ctx)); wifi_connect(); int i = 0; while (1) { mongoose_poll(2); if (!wifi_connected) continue; switch (ctx.state) { case DISCONNECTED: memset(&ctx, 0, sizeof(ctx)); conn = mg_connect(&sj_mgr, ADDR_TO_CONNECT, conn_handler); if (conn == NULL) { vTaskDelay(100); printf("reconnecting\n"); continue; } printf("conn = %p\n", conn); conn->user_data = &ctx; ctx.state = CONNECTING; break; case CONNECTING: break; case CONNECTED: i++; if (i % 10000 == 0) { printf("sent %u, recv %u heap free: %u uptime %d\n", ctx.num_sent, ctx.num_received, system_get_free_heap_size(), system_get_time() / 1000000); } break; } } }
bool __wifi_found_ap_cb(wifi_ap_h ap, void *user_data) { OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received "); int error_code = 0; char *ap_name = NULL; wifi_connection_state_e state; error_code = wifi_ap_get_essid(ap, &ap_name); if (error_code != WIFI_ERROR_NONE) { OIC_LOG(ERROR,LOG_TAG,"#### Fail to get AP name."); return false; } error_code = wifi_ap_get_connection_state(ap, &state); if (error_code != WIFI_ERROR_NONE) { OIC_LOG(ERROR,LOG_TAG,"#### Fail to get state."); free(ap_name); return false; } OIC_LOG_V(INFO,LOG_TAG,"#### AP name : %s, state : %s", ap_name, print_state(state)); if (strcmp(ap_name, gSsid) == 0) { OIC_LOG(INFO,LOG_TAG,"#### network found"); wifi_ap_set_passphrase(ap, gPass); connectedWifi = ap; error_code = wifi_connect(ap, __wifi_connected_cb, NULL); OIC_LOG_V(INFO,LOG_TAG,"Code=%d", error_code); } OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received "); free(ap_name); return true; }
void setup() { Serial.begin(115200); Notify(PSTR("initializing WiFi\n")); wifi_init(); Notify(PSTR("waiting for connection\n")); wifi_connect(); Notify(PSTR("setting up USB\n")); usb_setup(); hokuyo_data = (char*) malloc(HOKUYO_DATA_LENGTH); if (hokuyo_data) { Notify(PSTR("hokuyo_data allocated\n")); } hokuyo_on(); Notify(PSTR("hokuyo on\n")); Wire.begin(I2C_MASTER_ADDRESS); Wire.onReceive(receive); }
int main() { wifi_start(31, 30, 115200, WX_ALL_COM); wifi_setBuffer(str, sizeof(str)); int tcpHandle = wifi_connect("api.openweathermap.org", 80); print("tcpHandle = %d\r", tcpHandle); pause(2000); // IMPORTANT: Replace YourKeyYourKey... with the API key you // obtain from openweathermap.com when you create a free // account. char request[] = "GET /data/2.5/weather?zip=95677,us"\ "&appid=YourKeyYourKeyYourKeyYourKeyYour"\ " HTTP/1.1\r\n"\ "Host: api.openweathermap.org\r\n"\ "Connection: keep-alive\r\n"\ "Accept: *" "/" "*\r\n\r\n"; int size = strlen(request); print("GET req size: %d\r", size); pause(2000); wifi_print(TCP, tcpHandle, "%s", request); event = wifi_event; pause(2000); size = strlen(str); print("size = %d", size); pause(2000); wifi_scan(TCP, tcpHandle, "%s", str); for(int n = 0; n < sizeof(str); n++) { if(str[n] <= 'z' && str[n] >= ' ') { print("%c", str[n]); } else if(str[n] == 0) { print("[%d]", str[n]); break; } else if(str[n] == '\n') { print("\r", str[n]); } else { print("[%d]", str[n]); } } char *loc = strstr(str, "temp"); print("\rloc = %d\r", loc); float temp = 0; sscan(loc+5, "%f", &temp); float degC = temp -273.15; print("temp = %6.2f deg C\r", degC); float degF = degC * 9.0 / 5.0 + 32.0; print("temp = %6.2f deg C\r", degF); }
static void ICACHE_FLASH_ATTR timer_fn(void *timer_data) { uint32_t delay = MAX_LOOP_TIMER_MS; os_timer_disarm(&timer); PRINTF("state: %d\n\r", state); switch(state) { case STATE_LOAD_PERSIST: if(persist_load()) { PRINTF("persist data sensor value: %d\r\n", persist_data_ptr()->sensor_value); PRINTF("persist data sensor poll count: %d\r\n", persist_data_ptr()->poll_counter); state = STATE_HF_READ; persist_data_ptr()->poll_counter += 1; if(persist_data_ptr()->poll_counter >= 10) { // read low-frequency sensor (temp etc) persist_data_ptr()->poll_counter = 0; } persist_dirty(); } else { PRINTF("err loading persist\r\n"); // todo: goto error state (blink led?) state = STATE_IDLE; } delay = MIN_LOOP_TIMER_MS; break; case STATE_INIT_PERSIST: persist_init(); state = STATE_HF_READ; delay = MIN_LOOP_TIMER_MS; break; case STATE_HF_READ: state = STATE_LF_READ; if(GPIO_INPUT_GET(GPIO_SWITCH)) { if(!(persist_data_ptr()->sensor_value)) { persist_data_ptr()->sensor_value = true; persist_dirty(); state = STATE_HF_WIFI_CONNECT; } } else { if(persist_data_ptr()->sensor_value) { persist_data_ptr()->sensor_value = false; persist_dirty(); state = STATE_HF_WIFI_CONNECT; } } break; case STATE_HF_WIFI_CONNECT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_HF_SEND; } else { wifi_connect(); state = STATE_HF_WIFI_WAIT; } break; case STATE_HF_WIFI_WAIT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_HF_SEND; } break; case STATE_HF_SEND: state = STATE_HF_SEND_WAIT; break; case STATE_HF_SEND_WAIT: state = STATE_LF_READ; break; case STATE_LF_READ: state = STATE_LF_WIFI_CONNECT; break; case STATE_LF_WIFI_CONNECT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_LF_SEND; } else { wifi_connect(); state = STATE_LF_WIFI_WAIT; } break; case STATE_LF_WIFI_WAIT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_LF_SEND; } break; case STATE_LF_SEND: state = STATE_LF_SEND_WAIT; break; case STATE_LF_SEND_WAIT: state = STATE_IDLE; break; case STATE_IDLE: // go to deep sleep persist_save(); system_deep_sleep(SLEEP_TIME_US); return; break; default: // uhoh PRINTF("invalid state, reset\n\r"); state = STATE_IDLE; break; } os_timer_setfn(&timer, (os_timer_func_t *)timer_fn, NULL); os_timer_arm(&timer, delay, false); /* case STATE_IDLE: http_get("http://10.1.3.161/text", "", my_http_callback); state = STATE_HTTP_PENDING; timeout = 30; break; case STATE_HTTP_PENDING: timeout--; if(timeout==0) { PRINTF("timeout!\n\r"); state=STATE_IDLE; } else { delay_ms(1000); } break; default: PRINTF("invalid state, reset\n\r"); state=STATE_IDLE; break; } //system_os_post(user_procTaskPrio, 0, 0 ); */ }
int SC_connect_to_AP(void) { int ret = SC_ERROR; u8 scan_channel; u8 pscan_config; int retry = 3; rtw_security_t security_mode; rtw_network_info_t wifi = {0}; if(!(fixed_channel_num == 0)){ scan_channel = fixed_channel_num; } pscan_config = PSCAN_ENABLE; switch(g_security_mode){ case RTW_ENCRYPTION_OPEN: security_mode = RTW_SECURITY_OPEN; break; case RTW_ENCRYPTION_WEP40: case RTW_ENCRYPTION_WEP104: security_mode = RTW_SECURITY_WEP_PSK; break; case RTW_ENCRYPTION_WPA_TKIP: case RTW_ENCRYPTION_WPA_AES: case RTW_ENCRYPTION_WPA2_TKIP: case RTW_ENCRYPTION_WPA2_AES: case RTW_ENCRYPTION_WPA2_MIXED: security_mode = RTW_SECURITY_WPA2_AES_PSK; break; case RTW_ENCRYPTION_UNKNOWN: case RTW_ENCRYPTION_UNDEF: default: printf("\r\n unknow security mode,connect fail!"); return ret; } g_security_mode = 0xff;//clear it if (-1 == get_connection_info_from_profile(security_mode, &wifi)) { ret = SC_CONTROLLER_INFO_PARSE_FAIL; return ret; } while (1) { if(wifi_set_pscan_chan(&scan_channel, &pscan_config, 1) < 0){ printf("\n\rERROR: wifi set partial scan channel fail"); ret = SC_TARGET_CHANNEL_SCAN_FAIL; return ret; } #if 0 ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); #else ret = wifi_connect_bssid(g_bssid, (char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, 6, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); #endif if (ret == RTW_SUCCESS) { ret = SC_check_and_show_connection_info(); break; } if (retry == 0) { ret = SC_JOIN_BSS_FAIL; break; } retry --; } return ret; }
static void ICACHE_FLASH_ATTR send_heartbeat() { wifi_connect(); }
int main(void) { int server; video_init(); wifi_init(); while (!wifi_connect()); server = wifi_listen(); while (1) { static Connection conn; int i; uint32 data_goal; connection_accept(&conn, server); conn.card_type = cardmeGetType(); iprintf("Card type %d\n", conn.card_type); if (conn.card_type <= 0) { close(conn.fd); continue; } conn.card_size = cardmeSize(conn.card_type); iprintf("%d byte EEPROM\n", conn.card_size); for (conn.address = 0; conn.address < conn.card_size; conn.address += sizeof conn.buffer) { int sent = 0; cardmeReadEeprom(conn.address, conn.buffer, sizeof conn.buffer, conn.card_type); data_goal = Wifi_GetStats(WSTAT_TXDATABYTES) + sizeof conn.buffer; do { sent += send(conn.fd, conn.buffer + sent, sizeof conn.buffer - sent, 0); } while (sent < sizeof conn.buffer); /* * dswifi's TCP stack is buggy... or sometihing. It's retransmitting every packet * several times. This seems to help a little. Also, this should help work around * the fact that dswifi doesn't flush any buffers on close(). */ while (Wifi_GetStats(WSTAT_TXDATABYTES) < data_goal) { swiWaitForVBlank(); } iprintf("(%d bytes)\x1b[60D", conn.address); } iprintf("Complete. \n"); /* * Wait for the other end to close the connection. * Closing it immediately seems to just lose data in * dswifi's buffers... */ while (recv(conn.fd, conn.buffer, sizeof conn.buffer, 0) > 0); close(conn.fd); } return 0; }
void init_sequence(void *param) { int wac_unconfigured = 0; WACPersistentConfig_t config; // Clear WLAN init done callback to prevent re-enter init sequence at each wlan init p_wlan_init_done_callback = NULL; // Get existed WAC configuration WACPlatformReadConfig(&config); memset(dev_name, 0, sizeof(dev_name)); if((config.name_len > 0) && (config.name_len < 32)) { memcpy(dev_name, config.name, config.name_len); } else { uint8_t *mac = LwIP_GetMAC(&xnetif[0]); sprintf(dev_name, "RealtekAmeba%02x%02x%02x", mac[3], mac[4], mac[5]); wac_unconfigured = 1; } // Setup LWIP hostname before connecting netowrk netif_set_hostname(&xnetif[0], dev_name); if(wac_unconfigured) { // Start WAC server WACDevice_t dev = {0}; dev.name = dev_name; dev.manufacturer = "Realtek"; dev.model = "Ameba"; WACSetupDebug(1); WACStart(&dev); } else { if((config.ssid_len > 0) && (config.ssid_len < 32) && (config.password_len >= 0) && (config.password_len < 32)) { if(wifi_connect((unsigned char *) config.ssid, (config.password_len) ? RTW_SECURITY_WPA2_AES_PSK : RTW_SECURITY_OPEN, (unsigned char *) config.password, config.ssid_len, config.password_len, 0, NULL) == RTW_SUCCESS) { #if LWIP_IPV6 LwIP_AUTOIP_IPv6(&xnetif[0]); #endif printf("\nIPv4 DHCP ..."); LwIP_DHCP(0, DHCP_START); uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == IP_ADDR0) && (ip[1] == IP_ADDR1) && (ip[2] == IP_ADDR2) && (ip[3] == IP_ADDR3)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } char setup_code[11]; FlashSetupcodeRead(setup_code); if(strlen(setup_code) != 10) { random_setupcode(setup_code); printf("\nNo setupcode in flash, use random setupcode: %s\n", setup_code); } printf("\nSETUP CODE for Test: %s\n", setup_code); // Start HAP server HAPParameter_t hap_param; hap_param.name = dev_name; hap_param.model = "Ameba"; hap_param.setupcode = setup_code; hap_param.config_number = 1; hap_param.use_MFi = 1; WACSetupDebug(1); HAPSetupDebug(1); HAPStart(&hap_param); } } else { printf("\nInvalid network config\n"); } } vTaskDelete(NULL); }
void fATWC(void *arg){ int mode, ret; unsigned long tick1 = xTaskGetTickCount(); unsigned long tick2, tick3; char empty_bssid[6] = {0}, assoc_by_bssid = 0; printf("[ATWC]: _AT_WLAN_JOIN_NET_\n\r"); if(memcmp (wifi.bssid.octet, empty_bssid, 6)) assoc_by_bssid = 1; else if(wifi.ssid.val[0] == 0){ printf("[ATWC]Error: SSID can't be empty\n\r"); goto EXIT; } if(wifi.password != NULL){ if((wifi.key_id >= 0)&&(wifi.key_id <= 3)) { wifi.security_type = RTW_SECURITY_WEP_PSK; } else{ wifi.security_type = RTW_SECURITY_WPA2_AES_PSK; } } else{ wifi.security_type = RTW_SECURITY_OPEN; } //Check if in AP mode wext_get_mode(WLAN0_NAME, &mode); if(mode == IW_MODE_MASTER) { #if CONFIG_LWIP_LAYER dhcps_deinit(); #endif wifi_off(); vTaskDelay(20); if (wifi_on(RTW_MODE_STA) < 0){ printf("\n\rERROR: Wifi on failed!"); goto EXIT; } } if(assoc_by_bssid){ printf("\n\rJoining BSS by BSSID "MAC_FMT" ...\n\r", MAC_ARG(wifi.bssid.octet)); ret = wifi_connect_bssid(wifi.bssid.octet, (char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, ETH_ALEN, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); } else { printf("\n\rJoining BSS by SSID %s...\n\r", (char*)wifi.ssid.val); ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); } if(ret!= RTW_SUCCESS){ printf("\n\rERROR: Operation failed!"); goto EXIT; } tick2 = xTaskGetTickCount(); printf("\r\nConnected after %dms.\n", (tick2-tick1)); #if CONFIG_LWIP_LAYER /* Start DHCPClient */ LwIP_DHCP(0, DHCP_START); tick3 = xTaskGetTickCount(); printf("\r\n\nGot IP after %dms.\n", (tick3-tick1)); #endif printf("\n\r"); EXIT: init_wifi_struct( ); }
int wifi_restart_ap( unsigned char *ssid, rtw_security_t security_type, unsigned char *password, int ssid_len, int password_len, int channel) { unsigned char idx = 0; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; struct netif * pnetif = &xnetif[0]; #ifdef CONFIG_CONCURRENT_MODE rtw_wifi_setting_t setting; int sta_linked = 0; #endif if(rltk_wlan_running(WLAN1_IDX)){ idx = 1; } // stop dhcp server dhcps_deinit(); #ifdef CONFIG_CONCURRENT_MODE if(idx > 0){ sta_linked = wifi_get_setting(WLAN0_NAME, &setting); wifi_off(); vTaskDelay(20); wifi_on(RTW_MODE_STA_AP); } else #endif { IP4_ADDR(&ipaddr, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(pnetif, &ipaddr, &netmask,&gw); wifi_off(); vTaskDelay(20); wifi_on(RTW_MODE_AP); } // start ap if(wifi_start_ap((char*)ssid, security_type, (char*)password, ssid_len, password_len, channel) < 0) { printf("\n\rERROR: Operation failed!"); return -1; } #if (INCLUDE_uxTaskGetStackHighWaterMark == 1) printf("\r\nWebServer Thread: High Water Mark is %ld\n", uxTaskGetStackHighWaterMark(NULL)); #endif #ifdef CONFIG_CONCURRENT_MODE // connect to ap if wlan0 was linked with ap if(idx > 0 && sta_linked == 0){ int ret; printf("\r\nAP: ssid=%s", (char*)setting.ssid); printf("\r\nAP: security_type=%d", setting.security_type); printf("\r\nAP: password=%s", (char*)setting.password); printf("\r\nAP: key_idx =%d\n", setting.key_idx); ret = wifi_connect((char*)setting.ssid, setting.security_type, (char*)setting.password, strlen((char*)setting.ssid), strlen((char*)setting.password), setting.key_idx, NULL); #if CONFIG_DHCP_CLIENT if(ret == RTW_SUCCESS) { /* Start DHCPClient */ LwIP_DHCP(0, DHCP_START); } #endif } #endif #if (INCLUDE_uxTaskGetStackHighWaterMark == 1) printf("\r\nWebServer Thread: High Water Mark is %ld\n", uxTaskGetStackHighWaterMark(NULL)); #endif // start dhcp server dhcps_init(&xnetif[idx]); return 0; }