add wifi_api

This commit is contained in:
pvvx 2017-03-09 11:23:37 +03:00
parent 1e61910487
commit affec14b99
27 changed files with 3763 additions and 3052 deletions

View file

@ -2112,7 +2112,7 @@ void fATPF(void *arg)
goto exit;
}
dhcps_set_addr_pool(1,&start_ip,&end_ip);
dhcps_set_addr_pool(&start_ip,&end_ip);
if(argv[3] != NULL){
ip_addr = inet_addr(argv[3]);
@ -2141,7 +2141,7 @@ exit:
if(error_no==0)
at_printf("\r\n[ATPF] OK");
else
at_printf("\r\n[ATPF] ERROR:%d",error_no);
at_printf("\r\n[ATPF] ERROR:%d", error_no);
return;
}

View file

@ -131,25 +131,25 @@ void LwIP_Init(void)
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
//printf("NET_IF_NUM:%d\n\r",NET_IF_NUM);
for(idx=NET_IF_NUM - 1;idx>=0;idx--){
if(idx==0){
for(idx = NET_IF_NUM - 1; idx >= 0 ; idx--) {
if(idx == 0){
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
}
else{
#if CONFIG_ETHERNET
else if(idx == NET_IF_NUM - 1)
{
IP4_ADDR(&ipaddr, ETH_IP_ADDR0, ETH_IP_ADDR1, ETH_IP_ADDR2, ETH_IP_ADDR3);
IP4_ADDR(&netmask, ETH_NETMASK_ADDR0, ETH_NETMASK_ADDR1 , ETH_NETMASK_ADDR2, ETH_NETMASK_ADDR3);
IP4_ADDR(&gw, ETH_GW_ADDR0, ETH_GW_ADDR1, ETH_GW_ADDR2, ETH_GW_ADDR3);
}
#endif
else {
IP4_ADDR(&ipaddr, AP_IP_ADDR0, AP_IP_ADDR1, AP_IP_ADDR2, AP_IP_ADDR3);
IP4_ADDR(&netmask, AP_NETMASK_ADDR0, AP_NETMASK_ADDR1 , AP_NETMASK_ADDR2, AP_NETMASK_ADDR3);
IP4_ADDR(&gw, AP_GW_ADDR0, AP_GW_ADDR1, AP_GW_ADDR2, AP_GW_ADDR3);
}
#if CONFIG_ETHERNET
if(idx == NET_IF_NUM - 1)
{
IP4_ADDR(&ipaddr, ETH_IP_ADDR0, ETH_IP_ADDR1, ETH_IP_ADDR2, ETH_IP_ADDR3);
IP4_ADDR(&netmask, ETH_NETMASK_ADDR0, ETH_NETMASK_ADDR1 , ETH_NETMASK_ADDR2, ETH_NETMASK_ADDR3);
IP4_ADDR(&gw, ETH_GW_ADDR0, ETH_GW_ADDR1, ETH_GW_ADDR2, ETH_GW_ADDR3);
}
#endif
xnetif[idx].name[0] = 'r';
xnetif[idx].name[1] = '0'+idx;
@ -159,17 +159,16 @@ void LwIP_Init(void)
else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#else
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
netif_add(&xnetif[idx], &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
#endif
printf("interface %d is initialized\n", idx);
info_printf("interface %d is initialized\n", idx);
}
/* Registers the default network interface. */
netif_set_default(&xnetif[0]);
/* When the netif is fully configured this function must be called.*/
for(idx = 0;idx < NET_IF_NUM;idx++)
for(idx = 0; idx < NET_IF_NUM; idx++)
netif_set_up(&xnetif[idx]);
lwip_init_done = 1;

View file

@ -999,7 +999,7 @@ int wifi_on(rtw_mode_t mode) {
int wifi_off(void) {
// int ret = 0;
//pvvx int timeout = 20; // 20 sec ?!
int timeout = wifi_test_timeout_ms / 10;
uint32 timeout = xTaskGetTickCount();
if ((rltk_wlan_running(WLAN0_IDX) == 0)
&& (rltk_wlan_running(WLAN1_IDX) == 0)) {
@ -1020,18 +1020,15 @@ int wifi_off(void) {
while (1) {
if ((rltk_wlan_running(WLAN0_IDX) == 0)
&& (rltk_wlan_running(WLAN1_IDX) == 0)) {
info_printf("WIFI deinitialized\n");
// info_printf("WIFI deinitialized\n");
info_printf("WIFI deinitialized (%d ms)\n", xTaskGetTickCount() - timeout);
break;
}
if (timeout == 0) {
info_printf("ERROR: Deinit WIFI timeout!\n");
if(xTaskGetTickCount() - timeout > wifi_test_timeout_ms/portTICK_RATE_MS) {
error_printf("WIFI deinitialized timeout!\n");
break;
}
// vTaskDelay(1 * configTICK_RATE_HZ);
vTaskDelay(10 / portTICK_RATE_MS);
timeout--;
}
wifi_mode = RTW_MODE_NONE;
@ -1544,10 +1541,12 @@ int wifi_get_setting(const char *ifname, rtw_wifi_setting_t *pSetting) {
return ret;
}
//----------------------------------------------------------------------------//
extern char str_rom_57ch3Dch0A[]; // "=========================================================\n" 57 шт
int wifi_show_setting(const char *ifname, rtw_wifi_setting_t *pSetting) {
int ret = 0;
printf("\nWIFI %s Setting:\n==============================\n", ifname);
printf("\nWIFI '%s' Setting:\n", ifname);
printf(&str_rom_57ch3Dch0A[25]); // "================================\n"
switch (pSetting->mode) {
case RTW_MODE_AP:

View file

@ -94,7 +94,7 @@ static rtw_result_t rtw_indicate_event_handle(int event_cmd, char *buf, int buf_
extern gpio_t gpio_led;
#endif
void wifi_indication(WIFI_EVENT_INDICATE event, char *buf, int buf_len,
void wifi_indication(WIFI_EVENT_INDICATE event, unsigned char *buf, int buf_len,
int flags) {
//
// If upper layer application triggers additional operations on receiving of wext_wlan_indicate,
@ -105,7 +105,7 @@ void wifi_indication(WIFI_EVENT_INDICATE event, char *buf, int buf_len,
switch (event) {
case WIFI_EVENT_DISCONNECT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: Disconnection indication received\n", __func__);
info_printf("%s(%d): Disconnection indication received\n", __func__, event);
#endif
break;
case WIFI_EVENT_CONNECT:
@ -117,25 +117,25 @@ void wifi_indication(WIFI_EVENT_INDICATE event, char *buf, int buf_len,
// Sample: return mac address
if (buf != NULL && buf_len == 6) {
info_printf(
"%s: Connect indication received: %02x:%02x:%02x:%02x:%02x:%02x\n",
__func__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
"%s(%d): Connect indication received: %02x:%02x:%02x:%02x:%02x:%02x\n",
__func__, event, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
}
#endif
break;
case WIFI_EVENT_FOURWAY_HANDSHAKE_DONE:
#if(WIFI_INDICATE_MSG>0)
if (buf != NULL)
info_printf("%s: %s\n", __func__, buf);
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_SCAN_RESULT_REPORT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_SCAN_RESULT_REPORT\n", __func__);
info_printf("%s(%d): WIFI_EVENT_SCAN_RESULT_REPORT\n", __func__, event);
#endif
break;
case WIFI_EVENT_SCAN_DONE:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_SCAN_DONE\n", __func__);
info_printf("%s(%d): WIFI_EVENT_SCAN_DONE\n", __func__, event);
//#if CONFIG_DEBUG_LOG > 3
// debug_printf("Time at start %d ms.\n", xTaskGetTickCount());
//#endif
@ -143,56 +143,56 @@ void wifi_indication(WIFI_EVENT_INDICATE event, char *buf, int buf_len,
break;
case WIFI_EVENT_RECONNECTION_FAIL:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: %s\n", __func__, buf);
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_NO_NETWORK:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: %s\n", __func__, buf);
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
#if CONFIG_ENABLE_P2P
case WIFI_EVENT_SEND_ACTION_DONE:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: %s\n", __func__);
info_printf("%s(%d): %s\n", __func__, event, buf);
#endif
break;
case WIFI_EVENT_RX_MGNT:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_RX_MGNT\n", __func__);
info_printf("%s(%d): WIFI_EVENT_RX_MGNT\n", __func__, event);
#endif
break;
#endif //CONFIG_ENABLE_P2P
case WIFI_EVENT_STA_ASSOC:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: %s\n", __func__);
info_printf("%s(%d): WIFI_EVENT_STA_ASSOC\n", __func__, event);
#endif
break;
case WIFI_EVENT_STA_DISASSOC:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: %s\n", __func__);
info_printf("%s(%d): WIFI_EVENT_STA_DISASSOC\n", __func__, event);
#endif
break;
#ifdef CONFIG_WPS
case WIFI_EVENT_STA_WPS_START:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_STA_WPS_START\n", __func__);
info_printf("%s(%d): WIFI_EVENT_STA_WPS_START\n", __func__, event);
#endif
break;
case WIFI_EVENT_WPS_FINISH:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_WPS_FINISH\n", __func__);
info_printf("%s(%d): WIFI_EVENT_WPS_FINISH\n", __func__, event);
#endif
break;
case WIFI_EVENT_EAPOL_RECVD:
#if(WIFI_INDICATE_MSG>0)
info_printf("%s: WIFI_EVENT_EAPOL_RECVD\n", __func__);
info_printf("%s(%d): WIFI_EVENT_EAPOL_RECVD\n", __func__, event);
#endif
break;
#endif
case WIFI_EVENT_BEACON_AFTER_DHCP:
#if(WIFI_INDICATE_MSG>1)
info_printf("%s: WIFI_EVENT_BEACON_AFTER_DHCP\n", __func__);
info_printf("%s(%d): WIFI_EVENT_BEACON_AFTER_DHCP\n", __func__, event);
#endif
#if 0 // test beacon
gpio_write(&gpio_led, 1);

View file

@ -12,7 +12,7 @@ typedef struct
} event_list_elem_t;
void init_event_callback_list(void);
extern void wifi_indication( WIFI_EVENT_INDICATE event, char *buf, int buf_len, int flags);
extern void wifi_indication( WIFI_EVENT_INDICATE event, unsigned char *buf, int buf_len, int flags);
/** Register the event listener
*
* @param[in] event_cmds : The event command number indicated

View file

@ -0,0 +1,709 @@
/*
* wifi_api.c
*
* Created on: 01/11/2017
* Author: pvvx
*/
#include "FreeRTOS.h"
#include <autoconf.h>
#include "main.h"
#include <platform_opts.h>
#if CONFIG_EXAMPLE_WLAN_FAST_CONNECT
#error "Udnef CONFIG_EXAMPLE_WLAN_FAST_CONNECT!"
#endif
#ifndef USE_FLASH_EEP
#error "Define USE_FLASH_EEP!"
#endif
#include "task.h"
#include <platform/platform_stdlib.h>
#include <wifi/wifi_conf.h>
#include "flash_api.h"
#include <lwip_netconf.h>
#include "dhcp/dhcps.h"
#include "ethernetif.h"
#if CONFIG_ETHERNET
#include "ethernet_mii/ethernet_mii.h"
#endif
#include "flash_eep.h"
#include "feep_config.h"
#include "wifi_api.h"
#include "wifi_user_set.h"
//=========================================
//--- Wlan Config Init-------------------
WIFI_CONFIG wifi_cfg = {
.mode = DEF_WIFI_MODE, // rtw_mode_t
.sleep = DEF_WIFI_ST_SLEEP,
.country_code = DEF_WIFI_COUNTRY,// rtw_country_code_t
.tx_pwr = DEF_WIFI_TX_PWR, // rtw_tx_pwr_percentage_t
.bgn = DEF_WIFI_BGN, // rtw_network_mode_t
.load_flg = DEF_LOAD_CFG,
.save_flg = DEF_SAVE_CFG
};
//---- Interface 0 - wlan0 - AP - init ---
SOFTAP_CONFIG wifi_ap_cfg = {
.ssid = DEF_AP_SSID,
.password = DEF_AP_PASSWORD,
.security_type = DEF_AP_SECURITY,
.beacon_interval = DEF_AP_BEACON,
.channel = DEF_AP_CHANNEL,
.ssid_hidden = 0,
.max_sta = DEF_WIFI_AP_STATIONS // Max number of STAs, should be 1..3, default is 3
};
DHCP_CONFIG wifi_ap_dhcp = {
.ip = DEF_AP_IP,
.mask = DEF_AP_MSK,
.gw = DEF_AP_GW,
.mode = 2
};
//---- Interface 1 - wlan1 - ST - init ---
STATION_CONFIG wifi_st_cfg = {
.ssid = DEF_ST_SSID,
.password = DEF_ST_PASSWORD,
.bssid = DEF_ST_BSSID,
.security_type = DEF_ST_SECURITY,
.autoreconnect = DEF_ST_AUTORECONNECT,
.reconnect_pause = DEF_ST_RECONNECT_PAUSE
};
DHCP_CONFIG wifi_st_dhcp = {
.ip = DEF_ST_IP,
.mask = DEF_ST_MSK,
.gw = DEF_ST_GW,
.mode = 1
};
rtw_mode_t wifi_run_mode = RTW_MODE_NONE;
typedef struct _feep_element {
uint16 id;
uint16 size;
void * obj;
} FEEP_ELEMENT, *PFEEP_ELEMENT;
FEEP_ELEMENT feep_tab[] = {
{ FEEP_ID_WIFI_AP_CFG, sizeof(wifi_ap_cfg), &wifi_ap_cfg }, // Bit0
{ FEEP_ID_WIFI_ST_CFG, sizeof(wifi_st_cfg), &wifi_st_cfg }, // Bit1
{ FEEP_ID_AP_DHCP_CFG, sizeof(wifi_ap_dhcp), &wifi_ap_dhcp }, // Bit2
{ FEEP_ID_ST_DHCP_CFG, sizeof(wifi_st_dhcp), &wifi_st_dhcp }, // Bit3
{ FEEP_ID_WIFI_CFG, sizeof(wifi_cfg), &wifi_cfg }, // Bit4
// { FEEP_ID_LWIP_CFG, sizeof(lwip_conn_info), &lwip_conn_info }, // Bit5
{ 0, 0, NULL }
};
uint32 read_wifi_cfg(uint32 flg)
{
uint32 ret = 0;
PFEEP_ELEMENT p = feep_tab;
for(int m = 1; m && p->id != 0; m <<= 1, p++) {
if((flg & m)
&& flash_read_cfg(p->obj, p->id, p->size) < p->size) {
ret |= m;
};
};
return ret;
}
uint32 write_wifi_cfg(uint32 flg)
{
uint32 ret = 0;
PFEEP_ELEMENT p = feep_tab;
for(int m = 1; m && p->id != 0; m <<= 1, p++) {
if((flg & m)
&& flash_write_cfg(p->obj, p->id, p->size) < p->size) {
ret |= m;
};
};
return ret;
}
typedef int (*wlan_init_done_ptr)(void);
typedef int (*write_reconnect_ptr)(uint8_t *data, uint32_t len);
//Function
#if CONFIG_AUTO_RECONNECT
extern void (*p_wlan_autoreconnect_hdl)(rtw_security_t, char*, int, char*, int,
int);
#endif
extern wlan_init_done_ptr p_wlan_init_done_callback;
extern write_reconnect_ptr p_write_reconnect_ptr;
extern struct netif xnetif[NET_IF_NUM];
_WEAK void connect_start(void)
{
info_printf("\%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
}
_WEAK void connect_close(void)
{
info_printf("\%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
}
_WEAK void user_start(void)
{
info_printf("\%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
}
int wlan_init_done_callback(void) {
info_printf("WiFi Init after %d ms\n", xTaskGetTickCount());
return 0;
}
//char wlan_st_name[] = WLAN0_NAME;
char wlan_st_name[] = WLAN0_NAME;
char wlan_ap_name[] = WLAN1_NAME;
char wlan_st_netifn = 0;
char wlan_ap_netifn = 1;
extern rtw_mode_t wifi_mode; // новый режим работы
uint8 chk_ap_netif_num(void)
{
if (wifi_mode == RTW_MODE_AP) {
wlan_st_name[4] = '1';
wlan_ap_name[4] = '0';
wlan_st_netifn = 1;
wlan_ap_netifn = 0;
}
else { // if (wifi_mode == RTW_MODE_STA) {
wlan_st_name[4] = '0';
wlan_ap_name[4] = '1';
wlan_st_netifn = 0;
wlan_ap_netifn = 1;
}
return wlan_ap_netifn;
}
rtw_result_t wifi_run_ap(void) {
chk_ap_netif_num();
rtw_result_t ret = RTW_NOTAP;
if( (wifi_mode == RTW_MODE_AP) || (wifi_mode == RTW_MODE_STA_AP) ){
info_printf("Starting AP (%s, netif%d)...\n", wlan_ap_name, wlan_ap_netifn);
netif_set_addr(&xnetif[WLAN_AP_NETIF_NUM], &wifi_ap_dhcp.ip,
&wifi_ap_dhcp.mask, &wifi_ap_dhcp.gw);
if(wext_set_sta_num(wifi_ap_cfg.max_sta) != 0) { // Max number of STAs, should be 1..3, default is 3
error_printf("AP not set max connections %d!\n", wifi_ap_cfg.max_sta);
};
ret = wifi_start_ap(wifi_ap_cfg.ssid, //char *ssid,
wifi_ap_cfg.security_type, //rtw_security_t ecurity_type,
wifi_ap_cfg.password, //char *password,
strlen(wifi_ap_cfg.ssid), //int ssid_len,
strlen(wifi_ap_cfg.password), //int password_len,
wifi_ap_cfg.channel); //int channel
wifi_run_mode |= RTW_MODE_AP;
if (ret != RTW_SUCCESS) {
error_printf("Error(%d): Start AP failed!\n\n", ret);;
} else {
int timeout = 10000 / 200;
while (1) {
char essid[33];
if (wext_get_ssid(wlan_ap_name, (unsigned char *) essid) > 0) {
if (strcmp((const char * ) essid,
(const char * )wifi_ap_cfg.ssid)
== 0) {
#ifdef CONFIG_DONT_CARE_TP
pnetiff->flags |= NETIF_FLAG_IPSWITCH;
#endif
dhcps_ip4addr_pool_start = DEF_AP_DHCP_START;
dhcps_ip4addr_pool_end = DEF_AP_DHCP_STOP;
dhcps_init(&xnetif[WLAN_AP_NETIF_NUM]);
info_printf("AP '%s' started after %d ms\n",
wifi_ap_cfg.ssid, xTaskGetTickCount());
show_wifi_ap_ip();
if(wifi_cfg.save_flg & (BID_WIFI_AP_CFG | BID_AP_DHCP_CFG))
write_wifi_cfg(wifi_cfg.save_flg & (BID_WIFI_AP_CFG | BID_AP_DHCP_CFG));
ret = RTW_SUCCESS;
break;
}
}
if (timeout == 0) {
error_printf("Start AP timeout!\n");
ret = RTW_TIMEOUT;
break;
}
vTaskDelay(200 / portTICK_RATE_MS);
timeout--;
}
}
}
return ret;
}
rtw_result_t StartStDHCPClient(void)
{
debug_printf("Start DHCPClient...\n");
int ret = RTW_SUCCESS;
struct netif * pnetif = &xnetif[WLAN_ST_NETIF_NUM];
DHCP_CONFIG *p = (dhcp_cfg *)&wifi_st_dhcp;
unsigned char mode = p->mode;
if((mode == 3)
&& (p->ip == IP4ADDR(255,255,255,255)
|| p->ip == IP4ADDR(0,0,0,0))) {
mode = 2;
}
else mode = 1;
if(mode == 2) {
netif_set_addr(pnetif, (ip_addr_t *)&p->ip, (ip_addr_t *)&p->mask, (ip_addr_t *)&p->gw);
// dhcps_init(pnetif);
}
else if(mode) {
UBaseType_t savePriority = uxTaskPriorityGet(NULL);
/* If not rise priority, LwIP DHCP may timeout */
vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 3);
/* Start DHCP Client */
ret = LwIP_DHCP(WLAN_ST_NETIF_NUM, DHCP_START);
vTaskPrioritySet(NULL, savePriority);
if (ret == DHCP_ADDRESS_ASSIGNED) {
p->ip = pnetif->ip_addr.addr;
p->gw = pnetif->gw.addr;
p->mask = pnetif->netmask.addr;
if(p->mode == 3) {
p->mode = 2;
write_wifi_cfg(BID_ST_DHCP_CFG);
}
ret = RTW_SUCCESS;
}
else {
debug_printf("LwIP_DHCP ret=%d\n", ret);
ret = RTW_ERROR;
}
}
return ret;
}
rtw_result_t wifi_run_st(void) {
rtw_result_t ret = RTW_SUCCESS;
chk_ap_netif_num();
if((wifi_mode == RTW_MODE_STA) || (wifi_mode == RTW_MODE_STA_AP)) {
#if CONFIG_AUTO_RECONNECT
// p_wlan_autoreconnect_hdl = NULL;
if (wifi_st_cfg.autoreconnect) {
ret = wifi_config_autoreconnect(1, wifi_st_cfg.autoreconnect,
wifi_st_cfg.reconnect_pause);
if (ret != RTW_SUCCESS)
warning_printf("ERROR: Operation failed! Error=%d\n", ret);
}
#endif
info_printf("Connected to AP (%s, netif%d)...\n", wlan_st_name, wlan_st_netifn);
ret = wifi_connect(wifi_st_cfg.ssid, wifi_st_cfg.security_type,
wifi_st_cfg.password, strlen(wifi_st_cfg.ssid),
strlen(wifi_st_cfg.password), -1, NULL);
wifi_run_mode |= RTW_MODE_STA;
if (ret != RTW_SUCCESS) {
error_printf("%s: Operation failed! Error(%d)\n", __func__, ret);
} else {
if(wifi_cfg.save_flg & BID_WIFI_ST_CFG)
write_wifi_cfg(BID_WIFI_ST_CFG);
// Start DHCPClient
ret = StartStDHCPClient();
if(ret == RTW_SUCCESS) {
show_wifi_st_ip();
#if CONFIG_WLAN_CONNECT_CB
// extern void connect_start(void);
connect_start();
#endif
}
}
};
return ret;
}
int _wifi_on(rtw_mode_t mode) {
int ret = 0;
uint32 timeout = xTaskGetTickCount();
uint8 devnum;
if (rltk_wlan_running(WLAN0_IDX)) {
warning_printf("WIFI is already running\n");
return 0;
}
static int event_init = 0;
if (event_init == 0) {
init_event_callback_list();
event_init = 1;
}
wifi_mode = mode;
// set wifi mib
// adaptivity
wext_set_adaptivity(RTW_ADAPTIVITY_DISABLE);
info_printf("Initializing WIFI...\n");
devnum = (mode == RTW_MODE_STA_AP);
ret = rltk_wlan_init(0, mode);
if (ret < 0) return ret;
if(devnum) {
ret = rltk_wlan_init(1, mode);
if (ret < 0) return ret;
}
rltk_wlan_start(0);
if(devnum) rltk_wlan_start(1);
while (1) {
if (rltk_wlan_running(0) &&
rltk_wlan_running(devnum) ) {
#if CONFIG_DEBUG_LOG > 2
printf("WIFI initialized (%d ms)\n", xTaskGetTickCount() - timeout);
#endif
break;
}
if(xTaskGetTickCount() - timeout > wifi_test_timeout_ms/portTICK_RATE_MS) {
error_printf("WIFI init timeout!\n");
break;
}
vTaskDelay(wifi_test_timeout_step_ms / portTICK_RATE_MS);
}
return ret;
}
extern int lwip_init_done;
void _LwIP_Init(void)
{
int idx;
debug_printf("LwIP Init (%d)\n", wifi_mode);
/* Create tcp_ip stack thread */
tcpip_init( NULL, NULL );
chk_ap_netif_num(); // Исполняется после _wifi_on()
for(int idx = 0; idx < NET_IF_NUM; idx++) {
xnetif[idx].name[0] = 'r';
xnetif[idx].name[1] = '0' + idx;
}
netif_add(&xnetif[WLAN_ST_NETIF_NUM], (struct netif *)&wifi_st_dhcp.ip, (struct netif *)&wifi_st_dhcp.mask, (struct netif *)&wifi_st_dhcp.gw, NULL, &ethernetif_init, &tcpip_input);
netif_add(&xnetif[WLAN_AP_NETIF_NUM], (struct netif *)&wifi_ap_dhcp.ip, (struct netif *)&wifi_ap_dhcp.mask, (struct netif *)&wifi_ap_dhcp.gw, NULL, &ethernetif_init, &tcpip_input);
#if CONFIG_ETHERNET // && NET_IF_NUM > 2
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
ipaddr.addr = DEF_EH_IP;
netmask.addr = DEF_EH_MSK;
gw.addr = DEF_EH_GW;
netif_add(&xnetif[2], &ipaddr, &netmask, &gw, NULL, &ethernetif_mii_init, &tcpip_input);
}
#endif
/* Registers the default network interface. */
netif_set_default(&xnetif[0]);
/* When the netif is fully configured this function must be called.*/
for(idx = 0; idx < NET_IF_NUM; idx++) {
netif_set_up(&xnetif[idx]);
}
info_printf("interface %d is initialized\n", idx);
wifi_mode = 0;
lwip_init_done = 1;
}
int wifi_run(rtw_mode_t mode) {
int ret = 0;
#if CONFIG_DEBUG_LOG > 4
debug_printf("\n%s(%d), %d\n", __func__, mode, wifi_run_mode);
#endif
if(wifi_run_mode & RTW_MODE_AP) {
info_printf("Deinit old AP...\n");
LwIP_DHCP(WLAN_AP_NETIF_NUM, DHCP_STOP);
#if CONFIG_DEBUG_LOG > 4
debug_printf("dhcps_deinit()\n");
#endif
dhcps_deinit();
wifi_run_mode &= ~RTW_MODE_AP;
}
if(wifi_run_mode & RTW_MODE_STA) {
info_printf("Deinit old ST...\n");
LwIP_DHCP(WLAN_ST_NETIF_NUM, DHCP_STOP);
wifi_run_mode &= ~RTW_MODE_STA;
}
// if(mode != wifi_mode)
// wifi_mode = mode;
// chk_ap_netif_num();
if (mode != RTW_MODE_NONE) {
if(mode != (volatile)wifi_mode) {
info_printf("Deinitializing WIFI ...\n");
wifi_off();
vTaskDelay(30);
debug_printf("WiFi_on(%d)\n", mode);
if (_wifi_on(mode) < 0) {
error_printf("Wifi On failed!\n");
goto error_end;
}
wifi_mode = mode;
};
debug_printf("mode == wifi_mode? (%d == %d?)\n", mode, wifi_mode);
// if(mode == wifi_mode) {
// wifi_set_country(wifi_cfg.country_code);
if((wifi_mode == RTW_MODE_AP) || (wifi_mode == RTW_MODE_STA_AP)) {
wifi_run_ap();
}
if((wifi_mode == RTW_MODE_STA) || (wifi_mode == RTW_MODE_STA_AP)) {
wifi_run_st();
}
// wifi_config_autoreconnect(1,1,1);
#if CONFIG_INTERACTIVE_MODE
/* Initial uart rx swmaphore*/
vSemaphoreCreateBinary(uart_rx_interrupt_sema);
xSemaphoreTake(uart_rx_interrupt_sema, 1/portTICK_RATE_MS);
start_interactive_mode();
#endif
// if(wifi_run_mode == wifi_cfg.mode)
ret = 1;
// }
} else {
ret = 1;
error_end:
#if CONFIG_WLAN_CONNECT_CB
connect_close();
#endif
wifi_off();
}
chk_ap_netif_num();
return ret;
}
void wifi_init_thrd(void) {
debug_printf("\nLoad Config\n");
read_wifi_cfg(DEF_LOAD_CFG);
// Call back from wlan driver after wlan init done
p_wlan_init_done_callback = wlan_init_done_callback;
// Call back from application layer after wifi_connection success
// p_write_reconnect_ptr = wlan_write_reconnect_data_to_flash;
p_wlan_autoreconnect_hdl = NULL;
if (wifi_cfg.mode != RTW_MODE_NONE) {
wifi_mode = wifi_cfg.mode;
if(!lwip_init_done) {
/* Initilaize the LwIP stack */
_LwIP_Init();
};
user_start();
#if CONFIG_WIFI_IND_USE_THREAD
wifi_manager_init();
#endif
wifi_run(wifi_cfg.mode);
// if(wifi_run())
}
/* Initilaize the console stack */
console_init();
/* Kill init thread after all init tasks done */
vTaskDelete(NULL);
}
rtw_security_t translate_rtw_security(u8 security_type)
{
rtw_security_t security_mode = RTW_SECURITY_UNKNOWN;
switch (security_type) {
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:
//security_mode = RTW_SECURITY_OPEN;
}
return security_mode;
}
void show_wifi_ap_ip(void) {
printf("SoftAP ip: " IPSTR "\n", IP2STR(&xnetif[WLAN_AP_NETIF_NUM].ip_addr));
}
void show_wifi_st_ip(void) {
printf("Station ip: " IPSTR "\n", IP2STR(&xnetif[WLAN_ST_NETIF_NUM].ip_addr));
}
void show_wifi_st_cfg(void) {
printf("\tSSID: '%s'\n", wifi_st_cfg.ssid);
printf("\tPassword: '%s'\n", wifi_st_cfg.password);
printf("\tSecurity type: %p\n", wifi_st_cfg.security_type);
printf("\tAuto-reconnect: %d\n", wifi_st_cfg.autoreconnect);
printf("\tReconnect pause: %d\n", wifi_st_cfg.reconnect_pause);
}
void show_wifi_ap_cfg(void) {
printf("\tSSID: '%s'\n", wifi_ap_cfg.ssid);
printf("\tSSID hidden: %d\n", wifi_ap_cfg.ssid_hidden);
printf("\tPassword: '%s'\n", wifi_ap_cfg.password);
printf("\tSecurity type: %p\n", wifi_ap_cfg.security_type);
printf("\tChannel: %d\n", wifi_ap_cfg.channel);
printf("\tBeacon interval: %d ms\n", wifi_ap_cfg.beacon_interval);
printf("\tMax connections: %d\n", wifi_ap_cfg.max_sta);
}
void show_wifi_cfg(void) {
printf("\tStart mode: %p\n", wifi_cfg.mode);
printf("\tCountry code: %d\n", wifi_cfg.country_code);
printf("\tSleep mode: %p\n", wifi_cfg.sleep);
printf("\tNetwork mode: %d\n", wifi_cfg.bgn);
printf("\tTx power: %d\n", wifi_cfg.tx_pwr);
printf("\tLoad flags: %p\n", wifi_cfg.load_flg);
printf("\tSave flags: %p\n", wifi_cfg.save_flg);
}
//==========================================================
//--- CONSOLE --------------------------
#include "section_config.h"
#include "hal_diag.h"
// ATPN=<SSID>[,password[,encryption[,auto reconnect[,reconnect pause]]]: WIFI Connect to AP
void fATPN(int argc, char *argv[]){
if(argc > 1) {
if(argv[1][0] == '?') {
show_wifi_st_cfg();
}
else {
strncpy(wifi_st_cfg.ssid, argv[1], NDIS_802_11_LENGTH_SSID);
if(argc > 2) {
strncpy(wifi_st_cfg.password, argv[2], NDIS_802_11_LENGTH_SSID);
int i = strlen(wifi_st_cfg.password);
if(i > 7) {
wifi_st_cfg.security_type = RTW_SECURITY_WPA2_AES_PSK;
}
else if(!i) {
wifi_st_cfg.security_type = RTW_SECURITY_OPEN;
}
else {
printf("password len < 8!\n");
wifi_st_cfg.security_type = RTW_SECURITY_OPEN;
}
}
else {
wifi_st_cfg.password[0] = 0;
wifi_st_cfg.security_type = RTW_SECURITY_OPEN;
}
if(argc > 3) {
wifi_st_cfg.security_type = translate_rtw_security(atoi(argv[3]));
}
if(argc > 4) {
wifi_st_cfg.autoreconnect = atoi(argv[3]);
}
else wifi_st_cfg.autoreconnect = 0;
if(argc > 5) {
wifi_st_cfg.reconnect_pause = atoi(argv[3]);
}
else wifi_st_cfg.reconnect_pause = 5;
show_wifi_st_cfg();
wifi_run(wifi_run_mode | RTW_MODE_STA);
}
}
}
// ATPA=<SSID>[,password[,encryption[,channel[,hidden[,max connections]]]]]: Start WIFI AP
void fATPA(int argc, char *argv[]){
if(argc > 1) {
if(argv[1][0] == '?') {
show_wifi_ap_cfg();
}
else {
strncpy(wifi_ap_cfg.ssid, argv[1], NDIS_802_11_LENGTH_SSID);
if(argc > 2) {
strncpy(wifi_ap_cfg.password, argv[2], NDIS_802_11_LENGTH_SSID);
int i = strlen(wifi_ap_cfg.password);
if(i > 7) {
wifi_ap_cfg.security_type = RTW_SECURITY_WPA2_AES_PSK;
}
else if(!i) {
wifi_ap_cfg.security_type = RTW_SECURITY_OPEN;
}
else {
printf("password len < 8!\n");
wifi_ap_cfg.security_type = RTW_SECURITY_OPEN;
}
}
else {
wifi_ap_cfg.password[0] = 0;
wifi_ap_cfg.security_type = RTW_SECURITY_OPEN;
}
if(argc > 3) {
wifi_ap_cfg.security_type = translate_rtw_security(atoi(argv[3]));
}
if(argc > 4) {
wifi_ap_cfg.channel = atoi(argv[4]);
}
// else wifi_ap_cfg.channel = 1;
if(argc > 5) {
wifi_ap_cfg.ssid_hidden = atoi(argv[5]);
}
else wifi_ap_cfg.ssid_hidden = 0;
if(argc > 6) {
wifi_ap_cfg.max_sta = atoi(argv[6]);
}
else wifi_ap_cfg.max_sta = 3;
show_wifi_ap_cfg();
wifi_run(wifi_run_mode | RTW_MODE_AP);
}
}
}
// WIFI Connect, Disconnect
void fATWR(int argc, char *argv[]){
rtw_mode_t mode = RTW_MODE_NONE;
if(argc) mode = atoi(argv[1]);
wifi_run(mode);
}
// Close connections
void fATOF(int argc, char *argv[]){
connect_close();
}
// Open connections
void fATON(int argc, char *argv[]){
connect_start();
}
void fATWI(int argc, char *argv[]) {
rtw_wifi_setting_t Setting;
if((wifi_run_mode & RTW_MODE_AP)
&& wifi_get_setting(wlan_ap_name, &Setting) == 0) {
wifi_show_setting(wlan_ap_name, &Setting);
// show_wifi_ap_ip();
printf("\tIP: " IPSTR "\n", IP2STR(&xnetif[WLAN_AP_NETIF_NUM].ip_addr));
}
if((wifi_run_mode & RTW_MODE_STA)
&& wifi_get_setting(wlan_st_name, &Setting) == 0) {
wifi_show_setting(wlan_st_name, &Setting);
// show_wifi_st_ip();
printf("\tIP: " IPSTR "\n", IP2STR(&xnetif[WLAN_ST_NETIF_NUM].ip_addr));
}
printf("\nWIFI config:\n");
printf(&str_rom_57ch3Dch0A[25]); // "================================\n"
show_wifi_cfg();
printf("\nWIFI AP config:\n");
printf(&str_rom_57ch3Dch0A[25]); // "================================\n"
show_wifi_ap_cfg();
printf("\nWIFI ST config:\n");
printf(&str_rom_57ch3Dch0A[25]); // "================================\n"
show_wifi_st_cfg();
printf("\n");
if(argc > 2
&& (argv[1][0] == 's'
|| argv[1][0] == 'S')) {
int i = atoi(argv[2]);
printf("Save configs(%d)..\n", i);
write_wifi_cfg(atoi(argv[2]));
}
}
MON_RAM_TAB_SECTION COMMAND_TABLE console_cmd_wifi_api[] = {
{"ATPN", 1, fATPN, "=<SSID>[,password[,encryption[,auto-reconnect[,reconnect pause]]]: WIFI Connect to AP"},
{"ATPA", 1, fATPA, "=<SSID>[,password[,encryption[,channel[,hidden[,max connections]]]]]: Start WIFI AP"},
{"ATWR", 0, fATWR, ": WIFI Connect, Disconnect"},
{"ATON", 0, fATON, ": Open connections"},
{"ATOF", 0, fATOF, ": Close connections"},
{"ATWI", 0, fATWI, ": WiFi Info"}
};

View file

@ -0,0 +1,110 @@
/*
* wifi_user_set.h
*
* Created on: 01/04/2017
* Author: pvvx
*/
#ifndef _WIFI_API_H_
#define _WIFI_API_H_
#define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0])
#define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1])
#define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2])
#define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3])
#define IPSTR "%d.%d.%d.%d"
#define IP2STR(ipaddr) \
ip4_addr1(ipaddr), \
ip4_addr2(ipaddr), \
ip4_addr3(ipaddr), \
ip4_addr4(ipaddr)
extern char str_rom_57ch3Dch0A[]; // "=========================================================\n" 57 <20><>
#define BID_WIFI_AP_CFG (1 << 0)
#define BID_WIFI_ST_CFG (1 << 1)
#define BID_AP_DHCP_CFG (1 << 2)
#define BID_ST_DHCP_CFG (1 << 3)
#define BID_WIFI_CFG (1 << 4)
#define WLAN_ST_NETIF_NUM wlan_st_netifn
#define WLAN_AP_NETIF_NUM wlan_ap_netifn
//==== FEEP_ID ===========================
#define FEEP_ID_WIFI_CFG 0x4347 // id:'GC'
#define FEEP_ID_WIFI_AP_CFG 0x5041 // id:'AP'
#define FEEP_ID_WIFI_ST_CFG 0x5453 // id:'ST'
#define FEEP_ID_AP_DHCP_CFG 0x4144 // id:'DA'
#define FEEP_ID_ST_DHCP_CFG 0x5344 // id:'DS'
#define FEEP_ID_UART_CFG 0x5530 // id:'0U', type: UART_LOG_CONF
#define FEEP_ID_LWIP_CFG 0x4C30 // id:'0L', type: struct atcmd_lwip_conf
#define FEEP_ID_DHCP_CFG 0x4430 // id:'0D', type: struct
//=========================================
#define IW_PASSPHRASE_MAX_SIZE 64
#define NDIS_802_11_LENGTH_SSID 32
#define IP4ADDR(a,b,c,d) (((unsigned int)((d) & 0xff) << 24) | \
((unsigned int)((c) & 0xff) << 16) | \
((unsigned int)((b) & 0xff) << 8) | \
(unsigned int)((a) & 0xff))
//=========================================
//--- Wlan Config struct-------------------
typedef struct _wifi_config {
unsigned char mode; // rtw_mode_t
unsigned char sleep;
unsigned char country_code; // rtw_country_code_t
unsigned char tx_pwr; // rtw_tx_pwr_percentage_t
unsigned char bgn; // 802.11 rtw_network_mode_t
unsigned char load_flg;
unsigned char save_flg;
} WIFI_CONFIG, *PWIFI_CONFIG;
//---- Interface 0 - wlan0 - AP - struct --
typedef struct _softap_config {
unsigned char ssid[NDIS_802_11_LENGTH_SSID];
unsigned char password[IW_PASSPHRASE_MAX_SIZE];
rtw_security_t security_type;
uint16 beacon_interval; // Note: support 100 ~ 60000 ms, default 100
unsigned char channel; // 1..14
unsigned char ssid_hidden; // Note: default 0
unsigned char max_sta; // 1..3
} SOFTAP_CONFIG, *PSOFTAP_CONFIG;
//---- Interface 1 - wlan1 - ST - struct -
typedef struct _station_config {
unsigned char ssid[NDIS_802_11_LENGTH_SSID];
unsigned char password[IW_PASSPHRASE_MAX_SIZE];
rtw_security_t security_type;
unsigned char bssid[6]; // Note: If bssid set is not ff.ff.ff.ff.ff.ff,
// station will connect to the router with both ssid[] and bssid[] matched.
unsigned char autoreconnect; // 0 - none, 1..254 - count, 255 - all
unsigned char reconnect_pause; // in sec
// rtw_adaptivity_mode_t
} STATION_CONFIG, *PSTATION_CONFIG;
//--- LwIP Config -------------------------
struct lwip_conn_info {
int32_t role; //client, server or seed
unsigned int protocol; //tcp or udp
unsigned int remote_addr; //remote ip
unsigned int remote_port; //remote port
unsigned int local_addr; //locale ip, not used yet
unsigned int local_port; //locale port, not used yet
unsigned int reserved; //reserve for further use
};
//--- DHCP Config -------------------------
typedef struct _dhcp_config {
unsigned int ip;
unsigned int mask;
unsigned int gw;
unsigned char mode; // =0 dhcp off, =1 - dhcp on, =2 Static ip, =3 - auto
} DHCP_CONFIG, *PDHCP_CONFIG;
void show_wifi_ap_ip(void);
void show_wifi_st_ip(void);
void show_wifi_cfg(void);
void show_wifi_st_cfg(void);
void show_wifi_ap_cfg(void);
uint32 read_wifi_cfg(uint32 flg);
uint32 write_wifi_cfg(uint32 flg);
int wifi_run(rtw_mode_t mode);
void wifi_init_thrd(void);
#endif // _WIFI_API_H_

View file

@ -19,9 +19,7 @@ static struct ip_addr dhcps_local_gateway;
static struct ip_addr dhcps_network_id;
static struct ip_addr dhcps_subnet_broadcast;
static struct ip_addr dhcps_allocated_client_address;
static int dhcps_addr_pool_set = 0;
static struct ip_addr dhcps_addr_pool_start;
static struct ip_addr dhcps_addr_pool_end;
uint8_t dhcps_ip4addr_pool_start, dhcps_ip4addr_pool_end;
#if 0
static struct ip_addr dhcps_owned_first_ip;
static struct ip_addr dhcps_owned_last_ip;
@ -32,6 +30,8 @@ static int dhcp_message_total_options_lenth;
/* allocated IP range */
static struct table ip_table;
#define check_bit_ip_in_table(a) (ip_table.ip_range[(uint8_t)a >> 5] & (1 << ((uint8_t)a & ((1 << 5) - 1))))
#define set_bit_ip_in_table(a) (ip_table.ip_range[(uint8_t)a >> 5] |= (1 << ((uint8_t)a & ((1 << 5) - 1))))
static struct ip_addr client_request_ip;
static uint8_t dhcp_client_ethernet_address[16];
@ -48,89 +48,24 @@ static struct netif * dhcps_netif = NULL;
#if (!IS_USE_FIXED_IP)
static void mark_ip_in_table(uint8_t d)
{
#if (debug_dhcps)
printf(" mark ip %d",d);
#endif
xSemaphoreTake(dhcps_ip_table_semaphore, portMAX_DELAY);
if (0 < d && d <= 32) {
ip_table.ip_range[0] = MARK_RANGE1_IP_BIT(ip_table, d);
#if (debug_dhcps)
printf(" ip_table.ip_range[0] = 0x%x",ip_table.ip_range[0]);
#endif
} else if (32 < d && d <= 64) {
ip_table.ip_range[1] = MARK_RANGE2_IP_BIT(ip_table, (d - 32));
#if (debug_dhcps)
printf(" ip_table.ip_range[1] = 0x%x",ip_table.ip_range[1]);
#endif
} else if (64 < d && d <= 96) {
ip_table.ip_range[2] = MARK_RANGE3_IP_BIT(ip_table, (d - 64));
#if (debug_dhcps)
printf(" ip_table.ip_range[2] = 0x%x",ip_table.ip_range[2]);
#endif
} else if (96 < d && d <= 128) {
ip_table.ip_range[3] = MARK_RANGE4_IP_BIT(ip_table, (d - 96));
#if (debug_dhcps)
printf(" ip_table.ip_range[3] = 0x%x",ip_table.ip_range[3]);
#endif
} else {
#if (DHCPS_IP_RANGE_FROM_1_to_255)
if (128 < d && d <= 160) {
ip_table.ip_range[4] = MARK_RANGE5_IP_BIT(ip_table, d);
#if (debug_dhcps)
printf(" ip_table.ip_range[4] = 0x%x",ip_table.ip_range[4]);
#endif
} else if (160 < d && d <= 192) {
ip_table.ip_range[5] = MARK_RANGE6_IP_BIT(ip_table, (d - 160));
#if (debug_dhcps)
printf(" ip_table.ip_range[5] = 0x%x",ip_table.ip_range[5]);
#endif
} else if (192 < d && d <= 224) {
ip_table.ip_range[6] = MARK_RANGE7_IP_BIT(ip_table, (d - 192));
#if (debug_dhcps)
printf(" ip_table.ip_range[6] = 0x%x",ip_table.ip_range[6]);
#endif
} else if (224 < d) {
ip_table.ip_range[7] = MARK_RANGE8_IP_BIT(ip_table, (d - 224));
#if (debug_dhcps)
printf(" ip_table.ip_range[7] = 0x%x",ip_table.ip_range[7]);
#endif
}
#else
printf(" Request ip over the range(1-128) ");
#endif
}
set_bit_ip_in_table(d);
xSemaphoreGive(dhcps_ip_table_semaphore);
}
#endif
/**
* @brief get one usable ip from the ip table of dhcp server.
* @param: None
* @retval the usable index which represent the ip4_addr(ip) of allocated ip addr.
*/
#if (!IS_USE_FIXED_IP)
static uint8_t search_next_ip(void)
{
uint8_t range_count, offset_count;
uint8_t start, end;
uint8_t max_count;
if(dhcps_addr_pool_set){
start = (uint8_t)ip4_addr4(&dhcps_addr_pool_start);
end = (uint8_t)ip4_addr4(&dhcps_addr_pool_end);
}else{
start = 0;
end = 255;
}
uint8_t count;
xSemaphoreTake(dhcps_ip_table_semaphore, portMAX_DELAY);
for (range_count = 0; range_count < (max_count = (DHCPS_IP_RANGE_FROM_1_to_255 > 0 ? 8 : 4)); range_count++) {
for (offset_count = 0;offset_count < 32; offset_count++) {
if ((((ip_table.ip_range[range_count] >> offset_count) & 0x01) == 0)
&&(((range_count * 32) + (offset_count + 1)) >= start)
&&(((range_count * 32) + (offset_count + 1)) <= end)) {
xSemaphoreGive(dhcps_ip_table_semaphore);
return ((range_count * 32) + (offset_count + 1));
}
for(count = dhcps_ip4addr_pool_start; count <= dhcps_ip4addr_pool_end; count++) {
if(check_bit_ip_in_table(count) == 0) {
xSemaphoreGive(dhcps_ip_table_semaphore);
return count;
}
}
xSemaphoreGive(dhcps_ip_table_semaphore);
@ -255,11 +190,11 @@ static void dhcps_initialize_message(struct dhcp_msg *dhcp_message_repository, s
dhcp_message_repository->secs = 0;
dhcp_message_repository->flags = htons(BOOTP_BROADCAST);
memcpy((char *)dhcp_message_repository->yiaddr,
memcpy((char *)dhcp_message_repository->yiaddr,
(char *)&yiaddr,
sizeof(dhcp_message_repository->yiaddr));
memset((char *)dhcp_message_repository->ciaddr, 0,
memset((char *)dhcp_message_repository->ciaddr, 0,
sizeof(dhcp_message_repository->ciaddr));
memset((char *)dhcp_message_repository->siaddr, 0,
sizeof(dhcp_message_repository->siaddr));
@ -295,8 +230,8 @@ static void dhcps_send_offer(struct pbuf *packet_buffer)
(ip4_addr2(&client_request_ip) == ip4_addr2(&dhcps_network_id)) &&
(ip4_addr3(&client_request_ip) == ip4_addr3(&dhcps_network_id))) {
uint8_t request_ip4 = (uint8_t) ip4_addr4(&client_request_ip);
if ((request_ip4 != 0) && (((request_ip4 - 1) / 32) >= 0) && (((request_ip4 - 1) / 32) <= 3) &&
(((ip_table.ip_range[(request_ip4 - 1) / 32] >> ((request_ip4 - 1) % 32)) & 0x01) == 0)) {
if ((request_ip4 != 0)
&& check_bit_ip_in_table(request_ip4) == 0) {
temp_ip = request_ip4;
}
}
@ -318,11 +253,11 @@ static void dhcps_send_offer(struct pbuf *packet_buffer)
IP4_ADDR(&dhcps_allocated_client_address, (ip4_addr1(&dhcps_network_id)),
ip4_addr2(&dhcps_network_id), ip4_addr3(&dhcps_network_id), temp_ip);
memcpy(bound_client_ethernet_address, dhcp_client_ethernet_address, sizeof(bound_client_ethernet_address));
#endif
dhcps_initialize_message(dhcp_message_repository, dhcps_allocated_client_address);
add_offer_options(add_msg_type(&dhcp_message_repository->options[4],
#endif
dhcps_initialize_message(dhcp_message_repository, dhcps_allocated_client_address);
add_offer_options(add_msg_type(&dhcp_message_repository->options[4],
DHCP_MESSAGE_TYPE_OFFER));
udp_sendto_if(dhcps_pcb, packet_buffer,
udp_sendto_if(dhcps_pcb, packet_buffer,
&dhcps_send_broadcast_address, DHCP_CLIENT_PORT, dhcps_netif);
}
@ -396,8 +331,8 @@ uint8_t dhcps_handle_state_machine_change(uint8_t option_message_type)
(ip4_addr2(&client_request_ip) == ip4_addr2(&dhcps_network_id)) &&
(ip4_addr3(&client_request_ip) == ip4_addr3(&dhcps_network_id))) {
uint8_t request_ip4 = (uint8_t) ip4_addr4(&client_request_ip);
if ((request_ip4 != 0) && (((request_ip4 - 1) / 32) >= 0) && (((request_ip4 - 1) / 32) <= 3) &&
(((ip_table.ip_range[(request_ip4 - 1) / 32] >> ((request_ip4 - 1) % 32)) & 0x01) == 0)) {
if ((request_ip4 != 0)
&& check_bit_ip_in_table(request_ip4) == 0) {
IP4_ADDR(&dhcps_allocated_client_address, (ip4_addr1(&dhcps_network_id)),
ip4_addr2(&dhcps_network_id), ip4_addr3(&dhcps_network_id), request_ip4);
memcpy(bound_client_ethernet_address, dhcp_client_ethernet_address, sizeof(bound_client_ethernet_address));
@ -534,7 +469,7 @@ struct pbuf *udp_packet_buffer, struct ip_addr *sender_addr, uint16_t sender_por
case DHCP_OPTION_CODE_END:
break;
}
}
}
/* free the UDP connection, so we can accept new clients */
udp_disconnect(udp_pcb);
@ -546,20 +481,6 @@ struct pbuf *udp_packet_buffer, struct ip_addr *sender_addr, uint16_t sender_por
pbuf_free(udp_packet_buffer);
}
void dhcps_set_addr_pool(int addr_pool_set, struct ip_addr * addr_pool_start, struct ip_addr *addr_pool_end)
{
if(addr_pool_set){
dhcps_addr_pool_set = 1;
memcpy(&dhcps_addr_pool_start, addr_pool_start,
sizeof(struct ip_addr));
memcpy(&dhcps_addr_pool_end, addr_pool_end,
sizeof(struct ip_addr));
}else{
dhcps_addr_pool_set = 0;
}
}
/**
* @brief Initialize dhcp server.
* @param None.
@ -570,30 +491,29 @@ void dhcps_set_addr_pool(int addr_pool_set, struct ip_addr * addr_pool_start, st
void dhcps_init(struct netif * pnetif)
{
// printf("dhcps_init,wlan:%c\n\r",pnetif->name[1]);
dhcps_netif = pnetif;
if (dhcps_pcb != NULL) {
udp_remove(dhcps_pcb);
dhcps_pcb = NULL;
}
dhcps_pcb = udp_new();
dhcps_deinit();
dhcps_pcb = udp_new();
if (dhcps_pcb == NULL) {
printf("Error! upd_new error\n");
return;
}
IP4_ADDR(&dhcps_send_broadcast_address, 255, 255, 255, 255);
/* get net info from net interface */
IP4_ADDR(&dhcps_send_broadcast_address, 255, 255, 255, 255);
memset(&ip_table, 0, sizeof(struct table));
if((dhcps_ip4addr_pool_end | dhcps_ip4addr_pool_start) == 0) {
dhcps_ip4addr_pool_start = 1;
dhcps_ip4addr_pool_end = 255;
}
/* get net info from net interface */
memcpy(&dhcps_local_address, &pnetif->ip_addr,
sizeof(struct ip_addr));
memcpy(&dhcps_local_address, &pnetif->ip_addr,
sizeof(struct ip_addr));
memcpy(&dhcps_local_mask, &pnetif->netmask,
sizeof(struct ip_addr));
memcpy(&dhcps_local_gateway, &pnetif->gw,
sizeof(struct ip_addr));
/* calculate the usable network ip range */
dhcps_network_id.addr = ((pnetif->ip_addr.addr) &
(pnetif->netmask.addr));
@ -612,14 +532,8 @@ void dhcps_init(struct netif * pnetif)
, ip4_addr2(&dhcps_local_address), ip4_addr3(&dhcps_local_address),
(ip4_addr4(&dhcps_local_address)) + 1 );
#else
if (dhcps_ip_table_semaphore != NULL) {
vSemaphoreDelete(dhcps_ip_table_semaphore);
dhcps_ip_table_semaphore = NULL;
}
dhcps_ip_table_semaphore = xSemaphoreCreateMutex();
//dhcps_ip_table = (struct ip_table *)(pvPortMalloc(sizeof(struct ip_table)));
memset(&ip_table, 0, sizeof(struct table));
memset(&dhcps_allocated_client_address, 0, sizeof(struct ip_addr));
memset(bound_client_ethernet_address, 0, sizeof(bound_client_ethernet_address));
mark_ip_in_table((uint8_t)ip4_addr4(&dhcps_local_address));
@ -630,8 +544,8 @@ void dhcps_init(struct netif * pnetif)
}
#endif
#endif
udp_bind(dhcps_pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
udp_recv(dhcps_pcb, dhcps_receive_udp_packet_handler, NULL);
udp_bind(dhcps_pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
udp_recv(dhcps_pcb, dhcps_receive_udp_packet_handler, NULL);
}
void dhcps_deinit(void)

View file

@ -13,7 +13,6 @@
#define IS_USE_FIXED_IP 0
#define debug_dhcps 0
#define DHCPS_IP_RANGE_FROM_1_to_255 1
/* dhcp server states */
#define DHCP_SERVER_STATE_OFFER (1)
@ -97,11 +96,7 @@ static const uint8_t dhcp_option_lease_time_one_day[] = {0x00, 0x01, 0x51, 0x80}
static const uint8_t dhcp_option_interface_mtu_576[] = {0x02, 0x40};
struct table {
#if DHCPS_IP_RANGE_FROM_1_to_255
uint32_t ip_range[8];
#else
uint32_t ip_range[4];
#endif
};
struct address_pool{
@ -109,27 +104,9 @@ struct address_pool{
uint32_t end;
};
/* 01~32 */
#define MARK_RANGE1_IP_BIT(table, ip) ((table.ip_range[0]) | (1 << ((ip) - 1)))
/* 33~64 */
#define MARK_RANGE2_IP_BIT(table, ip) ((table.ip_range[1]) | (1 << ((ip) - 1)))
/* 65~96 */
#define MARK_RANGE3_IP_BIT(table, ip) ((table.ip_range[2]) | (1 << ((ip) - 1)))
/* 97~128 */
#define MARK_RANGE4_IP_BIT(table, ip) ((table.ip_range[3]) | (1 << ((ip) - 1)))
#if DHCPS_IP_RANGE_FROM_1_to_255
/* 129~160 */
#define MARK_RANGE5_IP_BIT(table, ip) ((table.ip_range[4]) | (1 << ((ip) - 1)))
/* 161~192 */
#define MARK_RANGE6_IP_BIT(table, ip) ((table.ip_range[5]) | (1 << ((ip) - 1)))
/* 193~224 */
#define MARK_RANGE7_IP_BIT(table, ip) ((table.ip_range[6]) | (1 << ((ip) - 1)))
/* 225~255 */
#define MARK_RANGE8_IP_BIT(table, ip) ((table.ip_range[7]) | (1 << ((ip) - 1)))
#endif
/* expose API */
void dhcps_set_addr_pool(int addr_pool_set, struct ip_addr * addr_pool_start, struct ip_addr *addr_pool_end);
extern uint8_t dhcps_ip4addr_pool_start, dhcps_ip4addr_pool_end;
void dhcps_init(struct netif * pnetif);
void dhcps_deinit(void);

View file

@ -75,7 +75,8 @@
LWIP_PLATFORM_ASSERT(message); handler;}} while(0)
#endif /* LWIP_ERROR */
#ifdef LWIP_DEBUG
#if LWIP_DEBUG
//#ifdef LWIP_DEBUG
/** print debug message only if debug message type is enabled...
* AND is of correct type AND is at least LWIP_DBG_LEVEL
*/

View file

@ -548,7 +548,9 @@ const HeapRegion_t *pxHeapRegion;
// DBG_8195A("ChipID: %p !\n", chip_id);
}
else {
#if CONFIG_DEBUG_LOG > 2
DBG_8195A("Init Heap Region: %p[%d]\n", pxHeapRegion->pucStartAddress, pxHeapRegion->xSizeInBytes);
#endif
#if CONFIG_DEBUG_LOG > 4
rtl_memset(pxHeapRegion->pucStartAddress, 0, pxHeapRegion->xSizeInBytes);
#endif

View file

@ -47,7 +47,7 @@ START_RAM_FUN_SECTION RAM_FUNCTION_START_TABLE __ram_start_table_start__ = {
/* Set Debug Flags */
LOCAL void BOOT_RAM_TEXT_SECTION SetDebugFlgs() {
#if CONFIG_DEBUG_LOG > 2
#if CONFIG_DEBUG_LOG > 3
CfgSysDebugWarn = -1;
CfgSysDebugInfo = -1;
CfgSysDebugErr = -1;

View file

@ -624,6 +624,7 @@ SECTIONS
rom_wps_rcons = 0x35d88;
rom_wps_Td4s = 0x35d94;
rom_wps_Td0 = 0x35e94;
str_rom_57ch3Dch0A = 0x3ed05;
str_rom_0123456789ABCDEF = 0x3ec24; /* "0123456789ABCDEF" */
str_rom_hex_addr = 0x442D6; /* "[Addr] .0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .A .B .C .D .E .F\r\n" */
str_rom_0123456789abcdef = 0x44660; /* "0123456789abcdef" */

View file

@ -310,9 +310,10 @@ MON_RAM_TEXT_SECTION void console_init(void) {
//<Return >: none
//<Notes >:
//======================================================
extern char str_rom_57ch3Dch0A[]; // "=========================================================\n" 57 шт
_WEAK void console_help(int argc, char *argv[]) { // Help
DiagPrintf("CONSOLE COMMAND SET:\n");
DiagPrintf("==============================\n");
DiagPrintf(&str_rom_57ch3Dch0A[25]); // DiagPrintf("==============================\n");
PCOMMAND_TABLE pcmdtab = UartLogRamCmdTable;
while(pcmdtab->cmd) {
#ifdef USE_ROM_CONSOLE
@ -322,7 +323,7 @@ _WEAK void console_help(int argc, char *argv[]) { // Help
#endif
pcmdtab++;
}
DiagPrintf("==============================\n");
DiagPrintf(&str_rom_57ch3Dch0A[25]); // DiagPrintf("==============================\n");
}
// (!) размещается в специальном сегменте '.mon.tab*' (см. *.ld файл)
MON_RAM_TAB_SECTION COMMAND_TABLE console_commands[] = {