/* The example of esp-free-rtos
*
* This sample code is in the public domain.
*/
#include <stdlib.h>
#include <string.h>
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <FreeRTOS.h>
#include <task.h>
#include <timers.h>
#include <ssid_config.h>
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#define UDP_PORT 9876
SemaphoreHandle_t xSemaphore;
static void showNetworkInfo() {
uint8_t hwaddr[6];
if (sdk_wifi_get_macaddr(STATION_IF, hwaddr)) {
printf("[%s] MAC
address="MACSTR"\n",pcTaskGetName(0),MAC2STR(hwaddr));
}
struct ip_info info;
if (sdk_wifi_get_ip_info(STATION_IF, &info)) {
printf("[%s] IP
address="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.ip));
printf("[%s]
Netmask="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.netmask));
printf("[%s]
Gateway="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.gw));
}
}
// WiFi Task
void task1(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
/* Wait until we have joined AP and are assigned an
IP */
int wifiStatus;
while (1) {
wifiStatus =
sdk_wifi_station_get_connect_status();
nowTick = xTaskGetTickCount();
printf("[%s:%d]
wifiStatus=%d\n",pcTaskGetName(0),nowTick,wifiStatus);
//if
(sdk_wifi_station_get_connect_status() == STATION_GOT_IP)
break;
if (wifiStatus == STATION_GOT_IP)
break;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
nowTick = xTaskGetTickCount();
printf("[%s:%d] Connected to
AP\n",pcTaskGetName(0),nowTick);
showNetworkInfo();
/* Now start task2 */
xSemaphoreGive(xSemaphore);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Give to
task2\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
// Bradcast Receive Task
void task2(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
/* wait for Semaphore */
xSemaphoreTake(xSemaphore, portMAX_DELAY);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Take\n",pcTaskGetName(0),nowTick);
/* set up address to recvfrom */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(UDP_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*
senderInfo message from ANY */
/* create the socket */
int fd;
int ret;
fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP
); // Create a UDP socket.
LWIP_ASSERT("fd >= 0", fd >= 0);
#if 0
/* set option */
int broadcast=1;
ret = lwip_setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast);
LWIP_ASSERT("ret >= 0", ret >= 0);
#endif
/* bind socket */
ret = lwip_bind(fd, (struct sockaddr *)&addr,
sizeof(addr));
LWIP_ASSERT("ret >= 0", ret >= 0);
/* senderInfo data */
char buffer[64];
struct sockaddr_in senderInfo;
socklen_t senderInfoLen = sizeof(senderInfo);
char senderstr[16];
while(1) {
socklen_t senderInfoLen =
sizeof(senderInfo);
memset(buffer, 0, sizeof(buffer));
ret = lwip_recvfrom(fd, buffer,
sizeof(buffer), 0, (struct sockaddr*)&senderInfo,
&senderInfoLen);
LWIP_ASSERT("ret > 0", ret > 0);
nowTick = xTaskGetTickCount();
printf("[%s:%d] lwip_recv
ret=%d\n",pcTaskGetName(0),nowTick,ret);
if (ret > 0) {
buffer[ret] = 0;
printf("[%s:%d] lwip_recv
buffer=%s\n",pcTaskGetName(0),nowTick,buffer);
inet_ntop(AF_INET,
&senderInfo.sin_addr, senderstr, sizeof(senderstr));
printf("recvfrom : %s,
port=%d\n", senderstr, ntohs(senderInfo.sin_port));
}
}
/* close socket. Don't reach here. */
ret = lwip_close(fd);
LWIP_ASSERT("ret == 0", ret == 0);
vTaskDelete( NULL );
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n",
sdk_system_get_sdk_version());
printf("pcTaskGetName=%s\n",pcTaskGetName(0));
struct sdk_station_config st_config = {
.ssid =WIFI_SSID,
.password =WIFI_PASS,
};
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&st_config);
/* Create Semaphore */
xSemaphore = xSemaphoreCreateBinary();
/* Check everything was created. */
configASSERT( xSemaphore );
// Create Task
xTaskCreate(task1, "WiFi", 256, NULL, 2, NULL);
xTaskCreate(task2, "Client", 1024, NULL, 2, NULL);
}
|