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

@ -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
*/