WiFi Access Point and TCP port for data output

pull/30/head
Pawel Jalocha 2020-10-29 02:08:34 +00:00
rodzic 07d3ac7e92
commit a00b7df555
10 zmienionych plików z 245 dodań i 24 usunięć

86
main/ap.cpp 100644
Wyświetl plik

@ -0,0 +1,86 @@
#include <ctype.h>
#include "hal.h"
#include "tcpip_adapter.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "format.h"
#include "fifo.h"
#include "socket.h"
#include "proc.h"
#include "wifi.h"
// #define DEBUG_PRINT
#ifdef WITH_AP
DataServer PortServer;
static FIFO<char, 1024> AP_TxFIFO;
static FIFO<uint8_t, 256> AP_RxFIFO;
void AP_Write(char Byte) { AP_TxFIFO.Write(Byte); }
static int AP_TxPush(size_t MaxLen=256) // transmit part of the TxFIFO to the TCP clients
{ char *Data; size_t Len=AP_TxFIFO.getReadBlock(Data); // see how much data is there in the queue for transmission
if(Len==0) return 0; // if block is empty then give up
if(Len>MaxLen) Len=MaxLen; // limit the block size
int Ret=PortServer.Send(Data, Len); // write the block to the Stratux socket
if(Ret<0) return -1; // if an error then give up
AP_TxFIFO.flushReadBlock(Len); // remove the transmitted block from the FIFO
return Len; } // return number of transmitted bytes
extern "C"
void vTaskAP(void* pvParameters)
{ esp_err_t Err;
AP_TxFIFO.Clear();
AP_RxFIFO.Clear();
vTaskDelay(1000);
WIFI_State.Flags=0;
Err=WIFI_Init();
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "WIFI_Init() => ");
if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE;
Format_SignDec(CONS_UART_Write, Err);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
Err=WIFI_StartAP(Parameters.APname, Parameters.APpass);
WIFI_setTxPower(Parameters.APtxPwr);
WIFI_setPowerSave(1);
Err=PortServer.Listen(Parameters.APport);
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "PortServer.Listen() => ");
Format_SignDec(CONS_UART_Write, Err);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
vTaskDelay(1000);
for( ; ; ) // main (endless) loop
{ Err=AP_TxPush();
if(Err>0) { vTaskDelay(1); continue; }
vTaskDelay(50);
Err = PortServer.Accept();
#ifdef DEBUG_PRINT
xSemaphoreTake(CONS_Mutex, portMAX_DELAY);
Format_String(CONS_UART_Write, "PortServer.Accept() => ");
Format_SignDec(CONS_UART_Write, Err);
Format_String(CONS_UART_Write, "\n");
xSemaphoreGive(CONS_Mutex);
#endif
}
}
#endif // WITH_AP

8
main/ap.h 100644
Wyświetl plik

@ -0,0 +1,8 @@
#include "hal.h"
void AP_Write(char Byte);
#ifdef __cplusplus
extern "C"
#endif
void vTaskAP(void* pvParameters);

Wyświetl plik

@ -49,6 +49,9 @@
#include "fifo.h"
#endif
#ifdef WITH_AP
#include "ap.h"
#endif
#ifdef WITH_STRATUX
#include "stratux.h"
#endif
@ -608,7 +611,7 @@ static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_AUTHENTICATE;
static const esp_spp_role_t role_slave = ESP_SPP_ROLE_SLAVE;
static FIFO<char, 2048> BT_SPP_TxFIFO; // buffer for console output to be sent over BT
static FIFO<char, 1024> BT_SPP_TxFIFO; // buffer for console output to be sent over BT
static FIFO<uint8_t, 256> BT_SPP_RxFIFO; // buffer for BT data to be send to the console
static uint32_t BT_SPP_Conn = 0; // BT incoming connection handle
static uint32_t BT_SPP_TxCong = 0; // congestion control
@ -846,6 +849,9 @@ void CONS_UART_Write (char Byte)
#ifdef WITH_BT_SPP
BT_SPP_Write(Byte);
#endif
#ifdef WITH_AP
AP_Write(Byte);
#endif
#ifdef WITH_STRATUX
Stratux_Write(Byte);
#endif

Wyświetl plik

@ -28,6 +28,10 @@
#include "stratux.h"
#endif
#ifdef WITH_AP
#include "ap.h"
#endif
#ifdef WITH_APRS
#include "aprs.h" // APRS task
#endif
@ -89,43 +93,46 @@ void app_main(void)
#ifdef WITH_SDLOG
Log_Mutex = xSemaphoreCreateMutex();
xTaskCreate(vTaskSDLOG, "SDLOG", 4096, 0, tskIDLE_PRIORITY+1, 0);
xTaskCreate(vTaskSDLOG, "SDLOG", 4000, 0, tskIDLE_PRIORITY+1, 0);
#endif
#ifdef WITH_LOG
xTaskCreate(vTaskLOG , "LOG", 4096, 0, tskIDLE_PRIORITY+1, 0);
xTaskCreate(vTaskLOG , "LOG", 5000, 0, tskIDLE_PRIORITY+1, 0);
#endif
xTaskCreate(vTaskRF, "RF", 2048, 0, tskIDLE_PRIORITY+5, 0);
xTaskCreate(vTaskPROC, "PROC", 2048, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskRF, "RF", 2000, 0, tskIDLE_PRIORITY+5, 0);
xTaskCreate(vTaskPROC, "PROC", 2000, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskGPS, "GPS", 2048, 0, tskIDLE_PRIORITY+4, 0);
xTaskCreate(vTaskGPS, "GPS", 2000, 0, tskIDLE_PRIORITY+4, 0);
#if defined(WITH_BMP180) || defined(WITH_BMP280) || defined(WITH_BME280) || defined(WITH_MS5607) || defined(WITH_MS5611)
xTaskCreate(vTaskSENS, "SENS", 2048, 0, tskIDLE_PRIORITY+4, 0);
xTaskCreate(vTaskSENS, "SENS", 2000, 0, tskIDLE_PRIORITY+4, 0);
#endif
#ifdef WITH_BMX055
xTaskCreate(vTaskIMU, "IMU", 2048, 0, tskIDLE_PRIORITY+4, 0);
xTaskCreate(vTaskIMU, "IMU", 2000, 0, tskIDLE_PRIORITY+4, 0);
#endif
#ifdef WITH_KNOB
xTaskCreate(vTaskKNOB, "KNOB", 2048, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskKNOB, "KNOB", 2000, 0, tskIDLE_PRIORITY+3, 0);
#endif
#ifdef WITH_AERO
xTaskCreate(vTaskAERO, "AERO", 2048, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskAERO, "AERO", 2000, 0, tskIDLE_PRIORITY+3, 0);
#endif
#ifdef WITH_APRS
xTaskCreate(vTaskAPRS, "APRS", 4096, 0, tskIDLE_PRIORITY+2, 0);
xTaskCreate(vTaskAPRS, "APRS", 4000, 0, tskIDLE_PRIORITY+2, 0);
#endif
#ifdef WITH_STRATUX
xTaskCreate(vTaskSTX, "STX", 4096, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskSTX, "STX", 4000, 0, tskIDLE_PRIORITY+3, 0);
#endif
#ifdef WITH_AP
xTaskCreate(vTaskAP, "AP", 4000, 0, tskIDLE_PRIORITY+3, 0);
#endif
#if defined(WITH_OLED) || defined(WITH_U8G2_OLED) || defined(WITH_ST7789) || defined(WITH_ILI9341)
xTaskCreate(vTaskDISP, "DISP", 2048, 0, tskIDLE_PRIORITY+2, 0);
xTaskCreate(vTaskDISP, "DISP", 3000, 0, tskIDLE_PRIORITY+2, 0);
#endif
#ifdef WITH_SOUND
xTaskCreate(vTaskSOUND, "SOUND", 2048, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskSOUND, "SOUND", 2000, 0, tskIDLE_PRIORITY+3, 0);
#endif
xTaskCreate(vTaskTICK , "TICK", 1024, 0, tskIDLE_PRIORITY+3, 0);
xTaskCreate(vTaskTICK , "TICK", 1000, 0, tskIDLE_PRIORITY+3, 0);
// xTaskCreate(vTaskCTRL, "CTRL", 1536, 0, tskIDLE_PRIORITY+2, 0);
vTaskCTRL(0); // run directly the CTRL task, instead of creating a separate one.

Wyświetl plik

@ -108,11 +108,19 @@ class FlashParameters
// char BTpin[16];
#endif
#ifdef WITH_AP
char APname[32];
char APpass[32];
uint16_t APport;
int8_t APminSig;
int8_t APtxPwr;
#endif
#ifdef WITH_STRATUX
char StratuxWIFI[32];
char StratuxPass[32];
char StratuxHost[32];
uint16_t StratuxPort;
uint16_t StratuxPort;
int8_t StratuxMinSig;
int8_t StratuxTxPwr;
#endif
@ -239,6 +247,13 @@ class FlashParameters
// #endif
// strcpy(BTpin, "1234");
#endif
#ifdef WITH_AP
getAprsCall(APname);
APpass[0]=0;
APport = 30000;
APminSig = -70; // [dBm]
APtxPwr = 40; // [0.25dBm]
#endif
#ifdef WITH_STRATUX
strcpy(StratuxWIFI, "stratux");
StratuxPass[0] = 0;
@ -277,8 +292,9 @@ class FlashParameters
if(Err!=ESP_OK) return Err;
size_t Size=0;
Err = nvs_get_blob(Handle, Name, 0, &Size); // get the Size of the blob in the Flash
if( (Err==ESP_OK) && (Size<=sizeof(FlashParameters)) )
Err = nvs_get_blob(Handle, Name, this, &Size); // read the Blob from the Flash
if( (Err==ESP_OK) && (Size==sizeof(FlashParameters)) )
{ Err = nvs_get_blob(Handle, Name, this, &Size); } // read the Blob from the Flash
else Err=ESP_ERR_NVS_NOT_FOUND;
nvs_close(Handle);
return Err; }
#endif // WITH_ESP32
@ -571,6 +587,19 @@ class FlashParameters
#ifdef WITH_BT_SPP
if(strcmp(Name, "BTname")==0) return Read_String(BTname, Value, 16)<=0;
#endif
#ifdef WITH_AP
if(strcmp(Name, "APname")==0) return Read_String(APname, Value, 16)<=0;
if(strcmp(Name, "APpass")==0) return Read_String(APpass, Value, 16)<=0;
if(strcmp(Name, "APport")==0)
{ int32_t Port; if(Read_Int(Port, Value)<=0) return 0;
if(Port<=0 || Port>0xFFFF) Port=30011; APport=Port; return 1; }
if(strcmp(Name, "APtxPwr")==0)
{ int32_t TxPwr; if(Read_Float1(TxPwr, Value)<=0) return 0;
TxPwr=(TxPwr*4+5)/10;
if(TxPwr<=0) TxPwr=0;
if(TxPwr>=80) TxPwr=80;
APtxPwr=TxPwr; return 1; }
#endif
#ifdef WITH_STRATUX
if(strcmp(Name, "StratuxWIFI")==0) return Read_String(StratuxWIFI, Value, 32)<=0;
if(strcmp(Name, "StratuxPass")==0) return Read_String(StratuxPass, Value, 32)<=0;
@ -701,6 +730,12 @@ class FlashParameters
#ifdef WITH_BT_SPP
strcpy(Line, "BTname = "); strcat(Line, BTname); strcat(Line, "; # [char]\n"); if(fputs(Line, File)==EOF) return EOF;
#endif
#ifdef WITH_AP
strcpy(Line, "APname = "); strcat(Line, APname); strcat(Line, "; # [char]\n"); if(fputs(Line, File)==EOF) return EOF;
strcpy(Line, "APpass = "); strcat(Line, APpass); strcat(Line, "; # [char]\n"); if(fputs(Line, File)==EOF) return EOF;
Write_UnsDec (Line, "APport" , (uint32_t)APport ); strcat(Line, " # [port]\n"); if(fputs(Line, File)==EOF) return EOF;
Write_Float1(Line, "APtxPwr" , (int32_t)10*APtxPwr/4); strcat(Line, " # [ dBm]\n"); if(fputs(Line, File)==EOF) return EOF;
#endif
#ifdef WITH_STRATUX
strcpy(Line, "StratuxWIFI = "); strcat(Line, StratuxWIFI); strcat(Line, "; # [char]\n"); if(fputs(Line, File)==EOF) return EOF;
strcpy(Line, "StratuxPass = "); strcat(Line, StratuxPass); strcat(Line, "; # [char]\n"); if(fputs(Line, File)==EOF) return EOF;
@ -757,6 +792,12 @@ class FlashParameters
#endif
#ifdef WITH_BT_SPP
strcpy(Line, "BTname = "); strcat(Line, BTname); strcat(Line, "; # [char]\n"); Format_String(Output, Line);
#endif
#ifdef WITH_AP
strcpy(Line, "APname = "); strcat(Line, APname); strcat(Line, "; # [char]\n"); Format_String(Output, Line);
strcpy(Line, "APpass = "); strcat(Line, APpass); strcat(Line, "; # [char]\n"); Format_String(Output, Line);
Write_UnsDec (Line, "APport", (uint32_t)APport ); strcat(Line, " # [port]\n"); Format_String(Output, Line);
Write_Float1 (Line, "APtxPwr", (int32_t)10*APtxPwr/4); strcat(Line, " # [ dBm]\n"); Format_String(Output, Line);
#endif
for(uint8_t Idx=0; Idx<InfoParmNum; Idx++)
{ Write_String (Line, OGN_Packet::InfoParmName(Idx), InfoParmValue(Idx)); strcat(Line, "; # [char]\n"); Format_String(Output, Line); }

Wyświetl plik

@ -37,9 +37,6 @@ class Socket
if(Err!=0) { Disconnect(); return -3; }
return Link; }
// int Connect()
// { Link = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); }
void Disconnect(void)
{ if(Link>=0) { close(Link); Link=(-1); }
if(Host) { freeaddrinfo(Host); Host=0; }
@ -74,3 +71,66 @@ class Socket
} ;
class DataServer
{ public:
static const int MaxConn = 2;
int Link;
int Client[MaxConn];
public:
DataServer()
{ Link=(-1);
for(int Idx=0; Idx<MaxConn; Idx++)
Client[Idx]=(-1);
}
int Listen(int Port)
{ struct sockaddr_in dest_addr;
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(Port);
Link = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if(Link<0) return -1;
int Err = bind(Link, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if(Err!=0) { close(Link); Link=(-1); return -1; }
Err = listen(Link, MaxConn);
if(Err!=0) { close(Link); Link=(-1); return -1; }
setBlocking(Link, 0);
return Link; }
static int setBlocking(int Link, int Block=1)
{ int Flags = fcntl(Link, F_GETFL, 0);
if(Block) Flags &= ~O_NONBLOCK;
else Flags |= O_NONBLOCK;
return fcntl(Link, F_SETFL, Flags); }
int Accept(void) // check for new clients
{ struct sockaddr_in6 source_addr;
uint addr_len = sizeof(source_addr);
int New=accept(Link, (struct sockaddr *)&source_addr, &addr_len); // attempt to accept a new client
if(New<0) return -1; // if nobody waiting then give up
int Idx;
for(Idx=0; Idx<MaxConn; Idx++) // go through the list
{ if(Client[Idx]<0) break; } // stop on the first free slot
if(Idx>=MaxConn) { close(New); return -1; } // if no free slots then close the new client and give up
Client[Idx]=New; return New; }
// static int Send(int Link, void *Buff, int Len)
// { if(Link<0) return -1;
// return send(Link, Buff, Len, 0); }
int Send(const char *Buff)
{ return Send(Buff, strlen(Buff)); }
int Send(const void *Buff, int Len)
{ if(Link<0) return -1;
int Count=0;
for(int Idx=0; Idx<MaxConn; Idx++)
{ if(Client[Idx]<0) continue;
int Sent=send(Client[Idx], Buff, Len, 0);
if(Sent!=Len) { close(Client[Idx]); Client[Idx]=(-1); continue; }
Count++; }
return Count; }
} ;

Wyświetl plik

@ -10,14 +10,13 @@
#include "fifo.h"
#include "socket.h"
#include "proc.h"
#include "wifi.h"
#include "stratux.h"
#define DEBUG_PRINT
#ifdef WITH_STRATUX
#include "wifi.h"
// ==============================================================================================
static char Stratux_Host[32] = { 0 };

Wyświetl plik

@ -65,6 +65,19 @@ esp_err_t WIFI_Start(void)
Err = esp_wifi_start();
return Err; }
esp_err_t WIFI_StartAP(const char *SSID, const char *Pass, int MaxConnections)
{ esp_err_t Err;
WIFI_Config.ap.ssid_len = strlen(SSID);
strcpy((char *)WIFI_Config.ap.ssid, SSID);
if(Pass) strcpy((char *)WIFI_Config.ap.password, Pass);
else WIFI_Config.ap.password[0]=0;
WIFI_Config.ap.max_connection = MaxConnections;
WIFI_Config.ap.authmode = (Pass && Pass[0]) ? WIFI_AUTH_WPA_WPA2_PSK:WIFI_AUTH_OPEN;
Err = esp_wifi_set_config(ESP_IF_WIFI_AP, &WIFI_Config); if(Err!=ESP_OK) return Err;
Err = esp_wifi_set_mode(WIFI_MODE_AP); if(Err!=ESP_OK) return Err;
Err = esp_wifi_start();
return Err; }
esp_err_t WIFI_Stop(void)
{ return esp_wifi_stop(); }

Wyświetl plik

@ -22,6 +22,7 @@ bool WIFI_isConnected(void);
esp_err_t WIFI_Init(void);
esp_err_t WIFI_setPowerSave(bool ON);
esp_err_t WIFI_Start(void);
esp_err_t WIFI_StartAP(const char *SSID, const char *Pass=0, int MaxConnecions=8);
esp_err_t WIFI_Stop(void);
esp_err_t WIFI_setTxPower(int8_t TxPwr=40);
esp_err_t WIFI_Connect(wifi_ap_record_t *AP, const char *Pass, int8_t MinSig=(-90));

Wyświetl plik

@ -1,5 +1,5 @@
read_log: read_log.cc
g++ -Wall -Wno-misleading-indentation -g -O2 -o read_log read_log.cc format.cpp
g++ -Wall -Wno-misleading-indentation -O2 -o read_log read_log.cc format.cpp
aprs2igc: aprs2igc.cc
g++ -Wall -Wno-misleading-indentation -O2 -o aprs2igc aprs2igc.cc format.cpp