/* 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_IP "192.168.10.10"
#define SEND_PORT 9876
#define RECEIVE_PORT 9877
int TaskIsAlive = 1;
SemaphoreHandle_t xSemaphore1;
SemaphoreHandle_t xSemaphore2;
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(xSemaphore1, 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(RECEIVE_PORT);
srcAddr.sin_port = htons(RECEIVE_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] %d で接続を待っています
クライアントプログラムを動かして下さい\n",pcTaskGetName(0),nowTick,RECEIVE_PORT);
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(xSemaphore1);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Give to
task2\n",pcTaskGetName(0),nowTick);
/* Now start task3 */
xSemaphoreGive(xSemaphore2);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Give to
task3\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
// Socket Client Task
void task3(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
/* wait for Semaphore */
xSemaphoreTake(xSemaphore2, portMAX_DELAY);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Take\n",pcTaskGetName(0),nowTick);
/* set up address to connect to */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
//addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(SEND_PORT);
addr.sin_addr.s_addr = inet_addr(SERVER_IP);
/* create the socket */
int fd;
int ret;
fd = lwip_socket(AF_INET, SOCK_STREAM, 0);
LWIP_ASSERT("fd >= 0", fd >= 0);
/* connect */
ret = lwip_connect(fd, (struct sockaddr*)&addr,
sizeof(addr));
/* should succeed */
LWIP_ASSERT("ret == 0", ret == 0);
char buf[256];
for(int i=0;i<10;i++) {
/* write something */
sprintf(buf,"test");
ret = lwip_write(fd, buf, strlen(buf));
_printf("lwip_write ret=%d\n",ret);
LWIP_ASSERT("ret == strlen(buf)", ret
== strlen(buf));
/* read something */
memset(buf,0,sizeof(buf));
ret = lwip_read(fd, buf, sizeof(buf));
LWIP_ASSERT("ret > 0", ret > 0);
nowTick = xTaskGetTickCount();
printf("[%s:%d] lwip_read
ret=%d\n",pcTaskGetName(0),nowTick,ret);
if (ret > 0) {
nowTick =
xTaskGetTickCount();
printf("[%s:%d] ret=%d
buf=%s\n",pcTaskGetName(0),nowTick,ret,buf);
}
vTaskDelay(10000 / portTICK_PERIOD_MS);
} // end while
/* close */
ret = lwip_close(fd);
LWIP_ASSERT("ret == 0", ret == 0);
TaskIsAlive = 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));
/* Create Semaphore */
xSemaphore1 = xSemaphoreCreateBinary();
xSemaphore2 = xSemaphoreCreateBinary();
/* Check everything was created. */
configASSERT( xSemaphore1 );
configASSERT( xSemaphore2 );
xTaskCreate(task1, "WiFi", 256, NULL, 2, NULL);
xTaskCreate(task2, "Server", 1024, NULL, 2, NULL);
xTaskCreate(task3, "Client", 512, NULL, 2, NULL);
}
|