diff --git a/CMakeLists.txt b/CMakeLists.txt index 68ba6b3..b8445e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ idf_component_register( "src/WiFiTransport.c" "src/GSMTransport.c" "src/ETHTransport.c" - "src/LoRaWANTransport.c" + "src/LoRaWAN.c" "src/SNTP.c" "src/MQTT.c" "src/MQTTSysHandler.c" diff --git a/include/LoRaWAN.h b/include/LoRaWAN.h new file mode 100644 index 0000000..1eb2f79 --- /dev/null +++ b/include/LoRaWAN.h @@ -0,0 +1,41 @@ + /*! Copyright 2022 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * \file LoRaWAN.h + * \version 1.0 + * \date 2022-12-19 + * \author Bogdan Pilyugin + * \brief + * \details + * \copyright Apache License, Version 2.0 + */ + +#ifndef COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_ +#define COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_ + + +typedef struct +{ + char *raw_data_ptr; + int data_length; +}LORA_DATA_SEND_STRUCT; + +void LoRaWANInitJoinTask(void *pvParameter); +void LoRaWANStop(void); +void LoRaWANStart(void); +esp_err_t LORASendData(LORA_DATA_SEND_STRUCT *pdss); +void regLoRaUserReceiveHandler( + void (*user_handler)(const char *message, int length, int port)); + +#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_LORAWAN_H_ */ diff --git a/include/MQTT.h b/include/MQTT.h index ee15f54..d285511 100644 --- a/include/MQTT.h +++ b/include/MQTT.h @@ -52,7 +52,7 @@ typedef struct char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH]; char *raw_data_ptr; int data_length; -}DATA_SEND_STRUCT; +}MQTT_DATA_SEND_STRUCT; /** * @brief wrapper around esp_mqtt_client_handle_t with additional info diff --git a/include/NetTransport.h b/include/NetTransport.h index ebfe45b..d00370a 100644 --- a/include/NetTransport.h +++ b/include/NetTransport.h @@ -48,21 +48,8 @@ typedef struct QueueHandle_t MQTT1MessagesQueueHandle; QueueHandle_t MQTT2MessagesQueueHandle; - EventGroupHandle_t transport_event_group; -typedef enum -{ - MQTT = 0, -} transport_data_type; - -typedef struct -{ - transport_data_type dType; - char *raw_data_ptr; - int data_length; -} net_transport_data_t; - void StartTimeGet(void); void WiFiAPStart(void); @@ -70,10 +57,7 @@ void WiFiSTAStart(void); void EthStart(void); void WiFiTransportTask(void *prm); -void LoRaWANInitJoinTask(void *pvParameter); -void LoRaWANStop(void); -void LoRaWANStart(void); void PPPModemColdStart(void); void PPPModemSoftRestart(void); @@ -122,7 +106,6 @@ uint32_t GetUpTime(void); void RegEthReset(void (*eth_rst)(uint8_t level)); void RegGSMReset(void (*gsm_rst)(uint8_t level)); -void regLoRaUserReceiveHandler( - void (*user_handler)(const char *message, int length, int port)); + #endif /* MAIN_INCLUDE_NETTRANSPORT_H_ */ diff --git a/include/webguiapp.h b/include/webguiapp.h index 2accad7..ab37254 100644 --- a/include/webguiapp.h +++ b/include/webguiapp.h @@ -1,41 +1,42 @@ - /*! Copyright 2022 Bogdan Pilyugin - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * \file webguiapp.h - * \version 1.0 - * \date 2022-08-21 - * \author Bogdan Pilyugin - * \brief - * \details - * \copyright Apache License, Version 2.0 - */ - -#ifndef COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ -#define COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ - -#include "HTTPServer.h" -#include "MQTT.h" - -#include "esp_log.h" -#include "esp_event.h" -#include "esp_netif.h" -#include "esp_system.h" -#include "driver/spi_master.h" - -esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, spi_transaction_t *trans_desc); - -bool GetUserAppNeedReset(void); -void SetUserAppNeedReset(bool res); - -#endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */ + /*! Copyright 2022 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * \file webguiapp.h + * \version 1.0 + * \date 2022-08-21 + * \author Bogdan Pilyugin + * \brief + * \details + * \copyright Apache License, Version 2.0 + */ + +#ifndef COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ +#define COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ + +#include "HTTPServer.h" +#include "MQTT.h" +#include "LoRaWAN.h" + +#include "esp_log.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_system.h" +#include "driver/spi_master.h" + +esp_err_t spi_device_polling_transmit_synchronized(spi_device_handle_t handle, spi_transaction_t *trans_desc); + +bool GetUserAppNeedReset(void); +void SetUserAppNeedReset(bool res); + +#endif /* COMPONENTS_WEBGUIAPPCOMPONENT_INCLUDE_WEBGUIAPP_H_ */ diff --git a/src/HTTPPostSystem.c b/src/HTTPPostSystem.c index fd7ad85..39750b4 100644 --- a/src/HTTPPostSystem.c +++ b/src/HTTPPostSystem.c @@ -22,6 +22,7 @@ */ #include "HTTPServer.h" +#include "LoRaWAN.h" static const char *TAG = "HTTPServerPost"; @@ -104,26 +105,26 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData) if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK) { if (!strcmp((const char*) tmp, (const char*) "1")) - TempIsETHEnabled = true; + TempIsETHEnabled = true; } if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK) { if (!strcmp((const char*) tmp, (const char*) "1")) - TempIsETHDHCPEnabled = true; + TempIsETHDHCPEnabled = true; } if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr); if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask); if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway); if (httpd_query_key_value(PostData, "dns1", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1); if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2); if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3); #endif @@ -445,6 +446,58 @@ static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData) } + if (httpd_query_key_value(PostData, "cmd", tmp, 4) == ESP_OK) + { + if (!strcmp(tmp, (const char*) "1")) + { + ESP_LOGI(TAG, "Got command F1 send test lora"); + const char test[] = {"LoRaWAN test message"}; + LORA_DATA_SEND_STRUCT dss; + dss.raw_data_ptr = test; + dss.data_length = sizeof(test)+1; + LORASendData(&dss); + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "2")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "3")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "4")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "5")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "6")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "7")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "8")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "9")) + { + return HTTP_IO_DONE_NOREFRESH; + } + else if (!strcmp(tmp, (const char*) "10")) + { + return HTTP_IO_DONE_NOREFRESH; + } + + + } + return HTTP_IO_DONE; } diff --git a/src/LoRaWANTransport.c b/src/LoRaWAN.c similarity index 93% rename from src/LoRaWANTransport.c rename to src/LoRaWAN.c index 21cd26d..2f50c7d 100644 --- a/src/LoRaWANTransport.c +++ b/src/LoRaWAN.c @@ -28,6 +28,7 @@ #include "SystemConfiguration.h" #include "NetTransport.h" #include "webguiapp.h" +#include "LoRaWAN.h" // Pins and other resources /*Defined in global configuration*/ @@ -52,7 +53,7 @@ static const int LORA_WAIT_DELIVERY_BIT = BIT2; QueueHandle_t LORAMessagesQueueHandle; static StaticQueue_t xStaticLoRaMessagesQueue; uint8_t LoRaMessagesQueueStorageArea[LORAWAN_MESSAGE_BUFER_LENTH - * sizeof(DATA_SEND_STRUCT)]; + * sizeof(LORA_DATA_SEND_STRUCT)]; void (*LoRaUserReceiveHandler)(const char *message, int length, int port); void regLoRaUserReceiveHandler( @@ -62,13 +63,13 @@ void regLoRaUserReceiveHandler( } -esp_err_t LORASendData(DATA_SEND_STRUCT *pdss) +esp_err_t LORASendData(LORA_DATA_SEND_STRUCT *pdss) { char *ptr = (char*) malloc(MESSAGE_LENGTH); if (ptr) { memcpy(ptr, pdss->raw_data_ptr, MESSAGE_LENGTH); - DATA_SEND_STRUCT DSS; + LORA_DATA_SEND_STRUCT DSS; DSS.raw_data_ptr = ptr; DSS.data_length = MESSAGE_LENGTH; @@ -96,13 +97,14 @@ void messageReceived(const uint8_t *message, size_t length, ttn_port_t port) ESP_LOGI(TAG, "Received=%s", P); } #endif - //TODO Here registered from application handler must be called + if(LoRaUserReceiveHandler != NULL) + LoRaUserReceiveHandler((char*)message, length, (int)port); } void LoRaWANTransportTask(void *pvParameter) { - DATA_SEND_STRUCT DSS; + LORA_DATA_SEND_STRUCT DSS; while (!LORAMessagesQueueHandle) vTaskDelay(pdMS_TO_TICKS(300)); //wait for LORA queue ready while (1) @@ -146,7 +148,7 @@ void LoRaWANInitJoinTask(void *pvParameter) LORAMessagesQueueHandle = NULL; if (GetSysConf()->lorawanSettings.Flags1.bIsLoRaWANEnabled) LORAMessagesQueueHandle = xQueueCreateStatic(LORAWAN_MESSAGE_BUFER_LENTH, - sizeof(DATA_SEND_STRUCT), + sizeof(LORA_DATA_SEND_STRUCT), LoRaMessagesQueueStorageArea, &xStaticLoRaMessagesQueue); diff --git a/src/MQTT.c b/src/MQTT.c index 49f1c64..d5227b5 100644 --- a/src/MQTT.c +++ b/src/MQTT.c @@ -25,7 +25,7 @@ #include "NetTransport.h" #include "MQTT.h" -#define CH_MESSAGE_BUFER_LENTH 32 //size of mqtt queue +#define MQTT_MESSAGE_BUFER_LENTH 32 //size of mqtt queue #define MQTT_RECONNECT_CHANGE_ADAPTER 3 #if CONFIG_WEBGUIAPP_MQTT_ENABLE @@ -34,8 +34,8 @@ static SemaphoreHandle_t xSemaphoreMQTTHandle = NULL; static StaticSemaphore_t xSemaphoreMQTTBuf; static StaticQueue_t xStaticMQTT1MessagesQueue; static StaticQueue_t xStaticMQTT2MessagesQueue; -uint8_t MQTT1MessagesQueueStorageArea[CH_MESSAGE_BUFER_LENTH * sizeof(DATA_SEND_STRUCT)]; -uint8_t MQTT2MessagesQueueStorageArea[CH_MESSAGE_BUFER_LENTH * sizeof(DATA_SEND_STRUCT)]; +uint8_t MQTT1MessagesQueueStorageArea[MQTT_MESSAGE_BUFER_LENTH * sizeof(MQTT_DATA_SEND_STRUCT)]; +uint8_t MQTT2MessagesQueueStorageArea[MQTT_MESSAGE_BUFER_LENTH * sizeof(MQTT_DATA_SEND_STRUCT)]; mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 }; @@ -207,7 +207,7 @@ void MQTTReconnect(void) void MQTTTaskTransmit(void *pvParameter) { - DATA_SEND_STRUCT DSS; + MQTT_DATA_SEND_STRUCT DSS; int idx = *(int*) pvParameter; while (!mqtt[idx].mqtt_queue) vTaskDelay(pdMS_TO_TICKS(300)); //wait for MQTT queue ready @@ -283,16 +283,16 @@ void MQTTRun(void) MQTT1MessagesQueueHandle = NULL; MQTT2MessagesQueueHandle = NULL; if (GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled) - MQTT1MessagesQueueHandle = xQueueCreateStatic(CH_MESSAGE_BUFER_LENTH, - sizeof(DATA_SEND_STRUCT), + MQTT1MessagesQueueHandle = xQueueCreateStatic(MQTT_MESSAGE_BUFER_LENTH, + sizeof(MQTT_DATA_SEND_STRUCT), MQTT1MessagesQueueStorageArea, &xStaticMQTT1MessagesQueue); mqtt[0].mqtt_queue = MQTT1MessagesQueueHandle; #if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2 if (GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled) - MQTT2MessagesQueueHandle = xQueueCreateStatic(CH_MESSAGE_BUFER_LENTH, - sizeof(DATA_SEND_STRUCT), + MQTT2MessagesQueueHandle = xQueueCreateStatic(MQTT_MESSAGE_BUFER_LENTH, + sizeof(MQTT_DATA_SEND_STRUCT), MQTT2MessagesQueueStorageArea, &xStaticMQTT2MessagesQueue); mqtt[1].mqtt_queue = MQTT2MessagesQueueHandle; diff --git a/src/MQTTSysHandler.c b/src/MQTTSysHandler.c index 4512b0b..92a20a9 100644 --- a/src/MQTTSysHandler.c +++ b/src/MQTTSysHandler.c @@ -130,7 +130,7 @@ static mqtt_app_err_t ResponceWithError(int idx, if (buf) { memcpy(buf, JSONErrorMess, strlen(JSONErrorMess)); - DATA_SEND_STRUCT DSS; + MQTT_DATA_SEND_STRUCT DSS; ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK"); DSS.raw_data_ptr = buf; DSS.data_length = strlen(JSONErrorMess); @@ -217,7 +217,7 @@ static mqtt_app_err_t ResponceWithFile(int idx, espfs_file_t *file, const char tail[] = "}"; strcat((fdata + readBytes), tail); free(filebuf); - DATA_SEND_STRUCT DSS; + MQTT_DATA_SEND_STRUCT DSS; ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK"); DSS.raw_data_ptr = outbuf; DSS.data_length = (fdata - outbuf) + readBytes + strlen(tail);