/* The example of esp-free-rtos
*
* This sample code is in the public domain.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp8266.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#define SERVER_PORT 9876
SemaphoreHandle_t xSemaphore;
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));
}
}
// Socket Server 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 connect to */
struct sockaddr_in srcAddr;
struct sockaddr_in dstAddr;
memset(&srcAddr, 0, sizeof(srcAddr));
//srcAddr.sin_len = sizeof(srcAddr);
srcAddr.sin_family = AF_INET;
//srcAddr.sin_port = PP_HTONS(SERVER_PORT);
srcAddr.sin_port = htons(SERVER_PORT);
srcAddr.sin_addr.s_addr = INADDR_ANY;
/* create the socket */
int srcSocket;
int dstSocket;
socklen_t dstAddrSize;
int ret;
int numrcv;
srcSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
LWIP_ASSERT("srcSocket >= 0", srcSocket >=
0);
/* bind socket */
ret = lwip_bind(srcSocket, (struct sockaddr
*)&srcAddr, sizeof(srcAddr));
/* should succeed */
LWIP_ASSERT("ret == 0", ret == 0);
/* listen socket */
ret = lwip_listen(srcSocket, 5);
/* should succeed */
LWIP_ASSERT("ret == 0", ret == 0);
char buf[1024];
while(1) {
// 接続の受付け
printf("[%s:%d] 接続を待っています
クライアントプログラムを動かして下さい\n",pcTaskGetName(0),nowTick);
dstAddrSize = sizeof(dstAddr);
dstSocket = lwip_accept(srcSocket,
(struct sockaddr *)&dstAddr, &dstAddrSize);
nowTick = xTaskGetTickCount();
printf("[%s:%d] %s
から接続を受けました\n",pcTaskGetName(0),nowTick,inet_ntoa(dstAddr.sin_addr));
while(1) { //
クライアントがSocketをクローズしてからこちらもクローズする
/* read something */
memset(buf,0,sizeof(buf));
numrcv =
lwip_read(dstSocket, buf, 1024);
nowTick =
xTaskGetTickCount();
printf("[%s:%d]
numrcv=%d\n",pcTaskGetName(0),nowTick,numrcv);
if(numrcv ==0 || numrcv
==-1 ){ // client close socket
lwip_close(dstSocket); break;
}
printf("Recv=[%s]",buf);
for (int i=0; i< numrcv;
i++){ // bufの中の小文字を大文字に変換
if(isalpha((int)buf[i])) {
if(islower((int)buf[i])) {
buf[i] = toupper((int)buf[i]);
}
else {
buf[i] = tolower((int)buf[i]);
}
}
}
/* write something */
ret = lwip_write(dstSocket,
buf, numrcv);
LWIP_ASSERT("ret ==
numrcv", ret == numrcv);
printf("->Send=[%s]\n",buf);
} // end while
} // end for
/* close (never come here) */
ret = lwip_close(srcSocket);
LWIP_ASSERT("ret == 0", ret == 0);
vTaskDelete( NULL );
}
// 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);
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));
/* Create Semaphore */
xSemaphore = xSemaphoreCreateBinary();
/* Check everything was created. */
configASSERT( xSemaphore );
xTaskCreate(task1, "WiFi", 256, NULL, 2, NULL);
xTaskCreate(task2, "Server", 512, NULL, 2, NULL);
}
|