diff --git a/source/shoh/.cproject b/source/shoh/.cproject index 7436f16..9e9d1b7 100644 --- a/source/shoh/.cproject +++ b/source/shoh/.cproject @@ -43,20 +43,22 @@ @@ -212,6 +226,13 @@ + + + + + + + @@ -243,6 +264,13 @@ + + + + + + + diff --git a/source/shoh/freertos/FreeRTOSConfig.h b/source/shoh/freertos/FreeRTOSConfig.h index fc7a4d3..5d808d5 100644 --- a/source/shoh/freertos/FreeRTOSConfig.h +++ b/source/shoh/freertos/FreeRTOSConfig.h @@ -94,6 +94,7 @@ #define configGENERATE_RUN_TIME_STATS 1 #define configRECORD_STACK_HIGH_ADDRESS 1 #define configUSE_TICKLESS_IDLE 1 +#define configUSE_TASK_NOTIFICATIONS 1 #define configRUN_TIME_COUNTER_TYPE uint64_t diff --git a/source/shoh/src/peripherals/networking/esp8266_socket.c b/source/shoh/src/peripherals/networking/esp8266_socket.c index 138d723..4c75d7a 100644 --- a/source/shoh/src/peripherals/networking/esp8266_socket.c +++ b/source/shoh/src/peripherals/networking/esp8266_socket.c @@ -86,7 +86,6 @@ static void stPassthrough(smi *ctx, const event *e); static void stPassthroughOK(smi *ctx, const event *e); static void stAT(smi *ctx, const event *e); static void stCommandMode(smi *ctx, const event *e); -static int cur_state(); static void EspSocketRun(smi *ctx); @@ -118,7 +117,10 @@ void smi_init(smi *ctx) ctx->state(ctx, &evEnter); // enter initial state } - +//Needs a mechanism to return error code after the timeout. +//Timeout is done by the ctx->timer in states. +//Tick event should be dispatched every ms, which seems not to be the case. (It's more rare.) +//Look at the stInit for reference int esp_socket(const char *ssid, const char *password) { smi_init(&EspSocketInstance); @@ -129,7 +131,6 @@ int esp_socket(const char *ssid, const char *password) while(EspSocketInstance.state != stReady) { - //printf("[socket]: Current state %d\r\n", cur_state()); // run esp task and run ticks EspSocketRun(&EspSocketInstance); } @@ -137,38 +138,7 @@ int esp_socket(const char *ssid, const char *password) return 0; } -int cur_state() -{ - int st = -1; - if (EspSocketInstance.state == stInit ) - st = 0; - else if (EspSocketInstance.state == stEchoOff ) - st = 1; - else if (EspSocketInstance.state == stStationModeCheck ) - st = 2; - else if (EspSocketInstance.state == stStationModeSet ) - st = 3; - else if (EspSocketInstance.state == stConnectAP ) - st = 4; - else if (EspSocketInstance.state == stReady ) - st = 5; - else if (EspSocketInstance.state == stConnectTCP ) - st = 6; - else if (EspSocketInstance.state == stConnected ) - st = 7; - else if (EspSocketInstance.state == stCloseTCP ) - st = 8; - else if (EspSocketInstance.state == stPassthrough ) - st = 9; - else if (EspSocketInstance.state == stPassthroughOK ) - st = 10; - else if (EspSocketInstance.state == stAT ) - st = 11; - else if (EspSocketInstance.state == stCommandMode ) - st = 12; - return st; -} - +//Needs a mechanism to return error code after the timeout. int esp_connect(int sockfd, const char *addr, int port) { I_DONT_USE(sockfd); @@ -380,14 +350,19 @@ static void stInit(smi *ctx, const event *e) case eExit: break; case eTick: - ++ctx->timer; + ++ctx->timer; //increases timer value with every tick + //if(ctx->timer == 2) DEBUGP("[%s]\r\n", ctx->buffer); + + //When it hits five - tries to reach the esp, incresing count of attempts. if(ctx->timer >= 5) { ctx->timer = 0; ++ctx->count; + if(ctx->count < 2) { serial_write_str(ctx, "AT\r\n"); } + //If done more attempts than expected - give up. else { DEBUGP("Error: Module not responding\r\n"); TRAN(stAT); @@ -605,6 +580,10 @@ static void stConnectTCP(smi *ctx, const event *e) case eExit: break; case eTick: + ++ctx->timer; + if(ctx->timer >= 70) { + ctx->timer = 0; + } break; case eReceive: rc = sm_read_result(ctx); diff --git a/source/shoh/src/threads/master/Master.cpp b/source/shoh/src/threads/master/Master.cpp index e8c19ac..8206980 100644 --- a/source/shoh/src/threads/master/Master.cpp +++ b/source/shoh/src/threads/master/Master.cpp @@ -14,8 +14,8 @@ #include "Logging.h" #include "UserInterface.h" #include "Temperature.h" +#include "Network.h" #include "queue.h" -#include "esp8266_socket.h" static const char* rotary_direction[] = { @@ -76,8 +76,6 @@ void Master::HandleEventType(Event* e) void Master::taskFunction() { Event data(Event::Null, 0); - //int soc = esp_socket("SSID", "PASSWORD"); - //int stat = esp_connect(soc, "IP", 5000); for (;;) { if(!_qm->receive(ThreadCommon::QueueManager::master_event_all, &data, 10000)) @@ -88,9 +86,6 @@ void Master::taskFunction() { HandleEventType(&data); global_clock->updateClock(); - //LOG_WARNING("ESP socket status: %d", stat); - //if(stat == 0) - // stat = esp_connect(soc, "IP", 5000); } } @@ -142,6 +137,9 @@ void thread_master(void* pvParams) { manager->tm->createTask(thread_temperature, "temperature", configMINIMAL_STACK_SIZE * 8,tskIDLE_PRIORITY + 1UL, static_cast(manager)); + manager->tm->createTask(thread_network, "network", + configMINIMAL_STACK_SIZE * 8,tskIDLE_PRIORITY + 1UL, + static_cast(manager)); LOG_INFO("Master created tasks"); m.taskFunction(); } diff --git a/source/shoh/src/threads/networking/Network.cpp b/source/shoh/src/threads/networking/Network.cpp new file mode 100644 index 0000000..486c6e5 --- /dev/null +++ b/source/shoh/src/threads/networking/Network.cpp @@ -0,0 +1,54 @@ +/* + * Network.cpp + * + * Created on: 31 May 2023 + */ + +#include "Network.h" +#include "esp8266_socket.h" +#include "Log.h" + +#define NETWORK_DEMO 0 + +Network::Network(ThreadCommon::QueueManager* qm) : _qm(qm) +{ + this->ssid = "SSID"; + this->password = "PASSWORD"; + this->ip = "127.0.0.1"; +} + +Network::~Network() {} + +void Network::taskFunction() +{ + #if NETWORK_DEMO + + int soc = esp_socket(this->ssid.c_str(), this->password.c_str()); + LOG_INFO("Connected to network"); + int stat = esp_connect(soc, this->ip.c_str(), 5000); + + #endif + + for (;;) + { + #if NETWORK_DEMO + + LOG_DEBUG("ESP socket status: %d", stat); + if(stat == 0) + stat = esp_connect(soc, this->ip.c_str(), 5000); + + #else + + vTaskDelay(portMAX_DELAY); + + #endif + } + +} + +void thread_network(void* pvParams) +{ + ThreadCommon::CommonManagers * manager = static_cast(pvParams); + Network n(manager->qm); + n.taskFunction(); +} \ No newline at end of file diff --git a/source/shoh/src/threads/networking/Network.h b/source/shoh/src/threads/networking/Network.h new file mode 100644 index 0000000..47aed19 --- /dev/null +++ b/source/shoh/src/threads/networking/Network.h @@ -0,0 +1,29 @@ +/* + * Network.h + * + * Created on: 31 May 2023 + */ + +#ifndef THREADS_NETWORKING_NETWORK_H_ +#define THREADS_NETWORKING_NETWORK_H_ + +#include + +#include "Event.h" +#include "ThreadCommon.h" + +class Network { +public: + Network(ThreadCommon::QueueManager* qm); + virtual ~Network(); + void taskFunction(); +private: + ThreadCommon::QueueManager* _qm; + std::string ssid; + std::string password; + std::string ip; +}; + +void thread_network(void* pvParams); + +#endif /* THREADS_NETWORKING_NETWORK_H_ */