diff --git a/main/ap.cpp b/main/ap.cpp new file mode 100644 index 0000000..d559389 --- /dev/null +++ b/main/ap.cpp @@ -0,0 +1,86 @@ +#include + +#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 AP_TxFIFO; +static FIFO 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 diff --git a/main/ap.h b/main/ap.h new file mode 100644 index 0000000..abb4dc5 --- /dev/null +++ b/main/ap.h @@ -0,0 +1,8 @@ +#include "hal.h" + +void AP_Write(char Byte); + +#ifdef __cplusplus + extern "C" +#endif + void vTaskAP(void* pvParameters); diff --git a/main/hal.cpp b/main/hal.cpp index 5f09f64..36f58d1 100644 --- a/main/hal.cpp +++ b/main/hal.cpp @@ -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 BT_SPP_TxFIFO; // buffer for console output to be sent over BT +static FIFO BT_SPP_TxFIFO; // buffer for console output to be sent over BT static FIFO 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 diff --git a/main/main.cpp b/main/main.cpp index 129585a..753f03a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -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. diff --git a/main/parameters.h b/main/parameters.h index 84889ea..520271b 100644 --- a/main/parameters.h +++ b/main/parameters.h @@ -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=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) { 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