int StoreApInfo() { rtw_wifi_config_t wifi_config; uint32_t address; #ifdef STM32F10X_XL address = 0x08080000; //bank2 domain #else uint16_t sector_nb = FLASH_Sector_11; address = flash_SectorAddress(sector_nb); #endif wifi_config.boot_mode = 0x77665502; memcpy(wifi_config.ssid, wifi_setting.ssid, strlen((char*)wifi_setting.ssid)); wifi_config.ssid_len = strlen((char*)wifi_setting.ssid); wifi_config.security_type = wifi_setting.security_type; memcpy(wifi_config.password, wifi_setting.password, strlen((char*)wifi_setting.password)); wifi_config.password_len= strlen((char*)wifi_setting.password); wifi_config.channel = wifi_setting.channel; printf("\n\rWritting boot mode 0x77665502 and Wi-Fi setting to flash ..."); #ifdef STM32F10X_XL FLASH_ErasePage(address); #else flash_EraseSector(sector_nb); #endif flash_Wrtie(address, (char *)&wifi_config, sizeof(rtw_wifi_config_t)); return 0; }
void LoadWifiConfig() { rtw_wifi_config_t local_config; uint32_t address; #ifdef STM32F10X_XL address = 0x08080000; //bank2 domain #else uint16_t sector_nb = FLASH_Sector_11; address = flash_SectorAddress(sector_nb); #endif printf("\r\nLoadWifiConfig(): Read from FLASH!\n"); flash_Read(address, (char *)&local_config, sizeof(local_config)); printf("\r\nLoadWifiConfig(): local_config.boot_mode=0x%x\n", local_config.boot_mode); printf("\r\nLoadWifiConfig(): local_config.ssid=%s\n", local_config.ssid); printf("\r\nLoadWifiConfig(): local_config.channel=%d\n", local_config.channel); printf("\r\nLoadWifiConfig(): local_config.security_type=%d\n", local_config.security_type); printf("\r\nLoadWifiConfig(): local_config.password=%s\n", local_config.password); if(local_config.boot_mode == 0x77665502) { wifi_setting.mode = RTW_MODE_AP; if(local_config.ssid_len > 32) local_config.ssid_len = 32; memcpy(wifi_setting.ssid, local_config.ssid, local_config.ssid_len); wifi_setting.ssid[local_config.ssid_len] = '\0'; wifi_setting.channel = local_config.channel; wifi_setting.security_type = local_config.security_type; if(local_config.password_len > 32) local_config.password_len = 32; memcpy(wifi_setting.password, local_config.password, local_config.password_len); wifi_setting.password[local_config.password_len] = '\0'; } else { LoadWifiSetting(); } }
static void update_ota_local_task(void *param) { int server_socket = 0; struct sockaddr_in server_addr; char *buf; int read_bytes, size = 0, i; update_cfg_local_t *cfg = (update_cfg_local_t*)param; uint32_t address, checksum = 0; #if CONFIG_WRITE_MAC_TO_FLASH char mac[ETH_ALEN]; #endif printf("\n\r[%s] Update task start", __FUNCTION__); buf = update_malloc(BUF_SIZE); if(!buf){ printf("\n\r[%s] Alloc buffer failed", __FUNCTION__); goto update_ota_exit; } // Connect socket server_socket = socket(AF_INET, SOCK_STREAM, 0); if(server_socket < 0){ printf("\n\r[%s] Create socket failed", __FUNCTION__); goto update_ota_exit; } server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = cfg->ip_addr; server_addr.sin_port = cfg->port; if(connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1){ printf("\n\r[%s] socket connect failed", __FUNCTION__); goto update_ota_exit; } // Erase config sectors if(flash_EraseSector(CONFIG_SECTOR) < 0){ printf("\n\r[%s] Erase sector failed", __FUNCTION__); goto update_ota_exit; } #if CONFIG_WRITE_MAC_TO_FLASH // Read MAC address if(flash_Read(FLASH_ADD_STORE_MAC, mac, ETH_ALEN) < 0){ printf("\n\r[%s] Read MAC error", __FUNCTION__); goto update_ota_exit; } #endif // Erase update sectors for(i = UPDATE_SECTOR; i <= FLASH_Sector_11; i += 8){ if(flash_EraseSector(i) < 0){ printf("\n\r[%s] Erase sector failed", __FUNCTION__); goto update_ota_exit; } } // Write update sectors address = flash_SectorAddress(UPDATE_SECTOR); printf("\n\r"); while(1){ read_bytes = read(server_socket, buf, BUF_SIZE); if(read_bytes == 0) break; // Read end if(read_bytes < 0){ printf("\n\r[%s] Read socket failed", __FUNCTION__); goto update_ota_exit; } if(flash_Wrtie(address + size, buf, read_bytes) < 0){ printf("\n\r[%s] Write sector failed", __FUNCTION__); goto update_ota_exit; } size += read_bytes; for(i = 0; i < read_bytes; i ++) checksum += buf[i]; printf("\rUpdate file size = %d ", size); } #if CONFIG_WRITE_MAC_TO_FLASH //Write MAC address if(!(mac[0]==0xff&&mac[1]==0xff&&mac[2]==0xff&&mac[3]==0xff&&mac[4]==0xff&&mac[5]==0xff)){ if(flash_Wrtie(FLASH_ADD_STORE_MAC, mac, ETH_ALEN) < 0){ printf("\n\r[%s] Write MAC failed", __FUNCTION__); goto update_ota_exit; } } #endif // Write config sectors address = flash_SectorAddress(CONFIG_SECTOR); if( (flash_Wrtie(address, (char*)&size, 4) < 0) || (flash_Wrtie(address+4, (char*)&checksum, 4) < 0) ){ printf("\n\r[%s] Write sector failed", __FUNCTION__); goto update_ota_exit; } printf("\n\r[%s] Update OTA success!", __FUNCTION__); update_ota_exit: if(buf) update_free(buf); if(server_socket >= 0) close(server_socket); if(param) update_free(param); TaskOTA = NULL; printf("\n\r[%s] Update task exit", __FUNCTION__); vTaskDelete(NULL); return; }
static void update_ota_cloud_task(void *param) { struct updater_ctx ctx; char *buf; int read_bytes, size = 0, i; uint32_t address, checksum = 0; update_cfg_cloud_t *cfg = (update_cfg_cloud_t*)param; #if CONFIG_WRITE_MAC_TO_FLASH char mac[ETH_ALEN]; #endif printf("\n\r[%s] Update task start", __FUNCTION__); buf = update_malloc(BUF_SIZE); if(!buf){ printf("\n\r[%s] Alloc buffer failed", __FUNCTION__); goto update_ota_exit_1; } // Init ctx if(updater_init_ctx(&ctx, (char*)cfg->repository, (char*)cfg->file_path) != 0) { printf("\n\r[%s] Cloud ctx init failed", __FUNCTION__); goto update_ota_exit_1; } printf("\n\r[%s] Firmware link: %s, size = %d bytes, checksum = 0x%08x, version = %s\n", __FUNCTION__, ctx.link, ctx.size, ctx.checksum, ctx.version); // Erase config sectors if(flash_EraseSector(CONFIG_SECTOR) < 0){ printf("\n\r[%s] Erase sector failed", __FUNCTION__); goto update_ota_exit; } #if CONFIG_WRITE_MAC_TO_FLASH // Read MAC address if(flash_Read(FLASH_ADD_STORE_MAC, mac, ETH_ALEN) < 0){ printf("\n\r[%s] Read MAC error", __FUNCTION__); goto update_ota_exit; } #endif // Erase update sectors for(i = UPDATE_SECTOR; i <= FLASH_Sector_11; i += 8){ if(flash_EraseSector(i) < 0){ printf("\n\r[%s] Erase sector failed", __FUNCTION__); goto update_ota_exit; } } // Write update sectors address = flash_SectorAddress(UPDATE_SECTOR); printf("\n\r"); while(ctx.bytes < ctx.size){ read_bytes = updater_read_bytes(&ctx, (unsigned char*)buf, BUF_SIZE); if(read_bytes == 0) break; // Read end if(read_bytes < 0){ printf("\n\r[%s] Read socket failed", __FUNCTION__); goto update_ota_exit; } if(flash_Wrtie(address + size, buf, read_bytes) < 0){ printf("\n\r[%s] Write sector failed", __FUNCTION__); goto update_ota_exit; } size += read_bytes; for(i = 0; i < read_bytes; i ++) checksum += buf[i]; printf("\rUpdate file size = %d/%d bytes ", ctx.bytes, ctx.size); } printf("\n\r[%s] ctx checksum = %08x, computed checksum = %08x\n", __FUNCTION__, ctx.checksum, checksum); if(checksum != ctx.checksum){ printf("\n\r[%s] Checksum error", __FUNCTION__); goto update_ota_exit; } #if CONFIG_WRITE_MAC_TO_FLASH //Write MAC address if(!(mac[0]==0xff&&mac[1]==0xff&&mac[2]==0xff&&mac[3]==0xff&&mac[4]==0xff&&mac[5]==0xff)){ if(flash_Wrtie(FLASH_ADD_STORE_MAC, mac, ETH_ALEN) < 0){ printf("\n\r[%s] Write MAC failed", __FUNCTION__); goto update_ota_exit; } } #endif // Write config sectors address = flash_SectorAddress(CONFIG_SECTOR); if( (flash_Wrtie(address, (char*)&size, 4) < 0) || (flash_Wrtie(address+4, (char*)&checksum, 4) < 0) ){ printf("\n\r[%s] Write sector failed", __FUNCTION__); goto update_ota_exit; } printf("\n\r[%s] Update OTA success!", __FUNCTION__); update_ota_exit: updater_free_ctx(&ctx); update_ota_exit_1: if(buf) update_free(buf); if(param) update_free(param); TaskOTA = NULL; printf("\n\r[%s] Update task exit", __FUNCTION__); vTaskDelete(NULL); return; }