]> granicus.if.org Git - esp-idf/commitdiff
Merge branch 'bugfix/mdns_udp_send_protected_backport3.2' into 'release/v3.2'
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 8 Jan 2019 04:04:23 +0000 (12:04 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 8 Jan 2019 04:04:23 +0000 (12:04 +0800)
mdns networking udp send protected (Backport 3.2)

See merge request idf/esp-idf!4020

17 files changed:
components/app_update/esp_ota_ops.c
components/bt/Kconfig
components/bt/bt.c
components/driver/i2s.c
components/driver/include/driver/i2s.h
components/esp32/lib
components/freemodbus/Kconfig
components/freemodbus/port/portserial.c
components/lwip/lwip
components/mdns/mdns.c
components/mdns/mdns_networking.c
components/mdns/private_include/mdns_private.h
components/soc/esp32/rtc_clk.c
examples/bluetooth/a2dp_gatts_coex/main/main.c
examples/bluetooth/a2dp_sink/main/main.c
examples/protocols/modbus_slave/main/Kconfig.projbuild [new file with mode: 0644]
examples/protocols/modbus_slave/main/freemodbus.c

index 6416b5d93f82ce464cc30d26ebcc2150cb0e9d54..d47638c72352f2bb5b125ab4493a717083e27cfe 100644 (file)
@@ -145,8 +145,7 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
         if (it->handle == handle) {
             // must erase the partition before writing to it
             assert(it->erased_size > 0 && "must erase the partition before writing to it");
-
-            if(it->wrote_size == 0 && size > 0 && data_bytes[0] != 0xE9) {
+            if (it->wrote_size == 0 && it->partial_bytes == 0 && size > 0 && data_bytes[0] != ESP_IMAGE_HEADER_MAGIC) {
                 ESP_LOGE(TAG, "OTA image has invalid magic byte (expected 0xE9, saw 0x%02x", data_bytes[0]);
                 return ESP_ERR_OTA_VALIDATE_FAILED;
             }
index c583f4f7e54b048dc4b4fedf9885f833dc7bd4a4..f018d6d0360c594cf14ea8048dd60ad904fe717e 100644 (file)
@@ -145,11 +145,13 @@ choice BTDM_MODEM_SLEEP_MODE
 config BTDM_MODEM_SLEEP_MODE_ORIG
     bool "ORIG Mode(sleep with low power clock)"
     help
-        ORIG mode is a deep sleep mode that can be used for dual mode controller. In this mode, bluetooth controller sleeps between BR/EDR frames and BLE events. A low power clock is used to maintain bluetooth reference clock.
+        ORIG mode is a bluetooth sleep mode that can be used for dual mode controller. In this mode, bluetooth controller
+        sleeps between BR/EDR frames and BLE events. A low power clock is used to maintain bluetooth reference clock.
 config BTDM_MODEM_SLEEP_MODE_EVED
-    bool "EVED Mode "
+    bool "EVED Mode(For internal test only)"
     help
-        This mode is for BLE only.
+        EVED mode is for BLE only and is only for internal test. Do not use it for production. this mode is not compatible
+        with DFS nor light sleep
 endchoice
 
 choice BTDM_LOW_POWER_CLOCK
@@ -163,6 +165,7 @@ config BTDM_LPCLK_SEL_MAIN_XTAL
     help
         Main crystal can be used as low power clock for bluetooth modem sleep. If this option is selected, bluetooth
         modem sleep can work under Dynamic Frequency Scaling(DFS) enabled, but cannot work when light sleep is enabled.
+        Main crystal has a relatively better performance than other bluetooth low power clock sources.
 config BTDM_LPCLK_SEL_EXT_32K_XTAL
     bool "External 32kHz crystal"
     depends on ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL
index 8e37f39268bd7694ee9190642dd3970cac36a06f..56629e01df2007b51079de74eba6ab6fd61a6399 100644 (file)
@@ -65,7 +65,7 @@
 /* Sleep mode */
 #define BTDM_MODEM_SLEEP_MODE_NONE          (0)
 #define BTDM_MODEM_SLEEP_MODE_ORIG          (1)
-#define BTDM_MODEM_SLEEP_MODE_EVED          (2)
+#define BTDM_MODEM_SLEEP_MODE_EVED          (2)  // sleep mode for BLE controller, used only for internal test.
 
 /* Low Power Clock Selection */
 #define BTDM_LPCLK_SEL_XTAL      (0)
index 65b83c9e77baa27caf23a721a99f434b77f7435f..480cd98ef1f0143e26325a2da8055aa0d80573cc 100644 (file)
@@ -86,6 +86,7 @@ typedef struct {
     i2s_mode_t mode;            /*!< I2S Working mode*/
     uint32_t sample_rate;              /*!< I2S sample rate */
     bool use_apll;               /*!< I2S use APLL clock */
+    bool tx_desc_auto_clear;    /*!< I2S auto clear tx descriptor on underflow */
     int fixed_mclk;             /*!< I2S fixed MLCK clock */
 } i2s_obj_t;
 
@@ -502,6 +503,12 @@ static void IRAM_ATTR i2s_intr_handler_default(void *arg)
         // All buffers are empty. This means we have an underflow on our hands.
         if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
             xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
+            // See if tx descriptor needs to be auto cleared:
+            // This will avoid any kind of noise that may get introduced due to transmission
+            // of previous data from tx descriptor on I2S line.
+            if (p_i2s->tx_desc_auto_clear == true) {
+                memset((void *) dummy, 0, p_i2s->tx->buf_size);
+            }
         }
         xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
         if (p_i2s->i2s_queue) {
@@ -991,6 +998,7 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co
     }
 
     p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
+    p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
     p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
     return ESP_OK;
 }
index 1cd454796f388c5101172d831fed8dc063ae87dd..1f73e1f5566e075735e77aad8680d3d51cc4f05d 100644 (file)
@@ -139,6 +139,7 @@ typedef struct {
     int                     dma_buf_count;          /*!< I2S DMA Buffer Count */
     int                     dma_buf_len;            /*!< I2S DMA Buffer Length */
     bool                    use_apll;              /*!< I2S using APLL as main I2S clock, enable it to get accurate clock */
+    bool                    tx_desc_auto_clear;     /*!< I2S auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) */
     int                     fixed_mclk;             /*!< I2S using fixed MCLK output. If use_apll = true and fixed_mclk > 0, then the clock output for i2s is fixed and equal to the fixed_mclk value.*/
 } i2s_config_t;
 
index acc7e050c8ab15b04d2ac97085ba49823b8cd911..9db42853fe76e5d7cae9c72e823febf757b38a2e 160000 (submodule)
@@ -1 +1 @@
-Subproject commit acc7e050c8ab15b04d2ac97085ba49823b8cd911
+Subproject commit 9db42853fe76e5d7cae9c72e823febf757b38a2e
index fd4314beca7ab76ec3e81ffe44ef6a40cd16bfce..b6d3d29fb4b514ef902d3bb8a0ebb73c295a2fa9 100644 (file)
@@ -1,29 +1,5 @@
 menu "Modbus configuration"
 
-config MB_UART_RXD
-    int "UART RXD pin number"
-    range 0 34
-    default 22
-    help
-        GPIO number for UART RX pin. See UART documentation for more information
-        about available pin numbers for UART.
-        
-config MB_UART_TXD
-    int "UART TXD pin number"
-    range 0 34
-    default 23
-    help
-        GPIO number for UART TX pin. See UART documentation for more information
-        about available pin numbers for UART.
-
-config MB_UART_RTS
-    int "UART RTS pin number"
-    range 0 34
-    default 18
-    help
-        GPIO number for UART RTS pin. This pin is connected to 
-        ~RE/DE pin of RS485 transceiver to switch direction.
-
 config MB_QUEUE_LENGTH
     int "Modbus serial task queue length"
     range 0 200
index e9103d0b75befded641bc238235f8655ed4a0d77..3543135ee3deaf1009f810c43e3e2af15f2cca8b 100644 (file)
@@ -231,19 +231,11 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate,
     xErr = uart_param_config(ucUartNumber, &xUartConfig);
     MB_PORT_CHECK((xErr == ESP_OK),
             FALSE, "mb config failure, uart_param_config() returned (0x%x).", (uint32_t)xErr);
-    // Set UART pin numbers
-    xErr = uart_set_pin(ucUartNumber, MB_UART_TXD, MB_UART_RXD, MB_UART_RTS, UART_PIN_NO_CHANGE);
-    MB_PORT_CHECK((xErr == ESP_OK), FALSE,
-            "mb set pin failure, uart_set_pin() returned (0x%x).", (uint32_t)xErr);
     // Install UART driver, and get the queue.
     xErr = uart_driver_install(ucUartNumber, MB_SERIAL_BUF_SIZE, MB_SERIAL_BUF_SIZE,
             MB_QUEUE_LENGTH, &xMbUartQueue, ESP_INTR_FLAG_LOWMED);
     MB_PORT_CHECK((xErr == ESP_OK), FALSE,
             "mb serial driver failure, uart_driver_install() returned (0x%x).", (uint32_t)xErr);
-    // Set driver mode to Half Duplex
-    xErr = uart_set_mode(ucUartNumber, UART_MODE_RS485_HALF_DUPLEX);
-    MB_PORT_CHECK((xErr == ESP_OK), FALSE,
-            "mb serial set mode failure, uart_set_mode() returned (0x%x).", (uint32_t)xErr);
 #ifndef MB_TIMER_PORT_ENABLED
     // Set timeout for TOUT interrupt (T3.5 modbus time)
     xErr = uart_set_rx_timeout(ucUartNumber, MB_SERIAL_TOUT);
index 046fadde072b5fca94bea84c16cce5ecbfd6948e..0865edf80ad0a96e0eaf2bbedbad350cab248115 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 046fadde072b5fca94bea84c16cce5ecbfd6948e
+Subproject commit 0865edf80ad0a96e0eaf2bbedbad350cab248115
index f5d337a32ce23df1dda4d337ca47740af8109056..742c47aeee70682d84840daf669352ab23e20f0f 100644 (file)
@@ -15,6 +15,7 @@
 #include "mdns.h"
 #include "mdns_private.h"
 #include "mdns_networking.h"
+#include "esp_log.h"
 #include <string.h>
 
 #ifdef MDNS_ENABLE_DEBUG
@@ -26,6 +27,8 @@ static const char * MDNS_SUB_STR = "_sub";
 
 mdns_server_t * _mdns_server = NULL;
 
+static const char *TAG = "MDNS";
+
 static volatile TaskHandle_t _mdns_service_task_handle = NULL;
 static SemaphoreHandle_t _mdns_service_semaphore = NULL;
 
@@ -63,11 +66,16 @@ static char * _mdns_mangle_name(char* in) {
         //need to add -2 to string
         ret = malloc(strlen(in) + 3);
         if (ret == NULL) {
+            HOOK_MALLOC_FAILED;
             return NULL;
         }
         sprintf(ret, "%s-2", in);
     } else {
         ret = malloc(strlen(in) + 2); //one extra byte in case 9-10 or 99-100 etc
+        if (ret == NULL) {
+            HOOK_MALLOC_FAILED;
+            return NULL;
+        }
         strcpy(ret, in);
         int baseLen = p - in; //length of 'bla' in 'bla-123'
         //overwrite suffix with new suffix
@@ -117,6 +125,7 @@ esp_err_t _mdns_send_rx_action(mdns_rx_packet_t * packet)
 
     action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
 
@@ -557,6 +566,9 @@ static uint16_t _mdns_append_txt_record(uint8_t * packet, uint16_t * index, mdns
                 return 0;
             }
             data_len += l;
+        } else {
+            HOOK_MALLOC_FAILED;
+            // continue
         }
         txt = txt->next;
     }
@@ -1130,6 +1142,7 @@ static bool _mdns_alloc_answer(mdns_out_answer_t ** destnation, uint16_t type, m
 
     mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
     if (!a) {
+        HOOK_MALLOC_FAILED;
         return false;
     }
     a->type = type;
@@ -1148,6 +1161,7 @@ static mdns_tx_packet_t * _mdns_alloc_packet_default(tcpip_adapter_if_t tcpip_if
 {
     mdns_tx_packet_t * packet = (mdns_tx_packet_t*)malloc(sizeof(mdns_tx_packet_t));
     if (!packet) {
+        HOOK_MALLOC_FAILED;
         return NULL;
     }
     memset((uint8_t*)packet, 0, sizeof(mdns_tx_packet_t));
@@ -1285,6 +1299,7 @@ static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if,
     for (i=0; i<len; i++) {
         mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
         if (!q) {
+            HOOK_MALLOC_FAILED;
             _mdns_free_tx_packet(packet);
             return NULL;
         }
@@ -1310,6 +1325,7 @@ static mdns_tx_packet_t * _mdns_create_probe_packet(tcpip_adapter_if_t tcpip_if,
     if (include_ip && !_str_null_or_empty(_mdns_server->hostname)) {
         mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
         if (!q) {
+            HOOK_MALLOC_FAILED;
             _mdns_free_tx_packet(packet);
             return NULL;
         }
@@ -1457,6 +1473,7 @@ static void _mdns_init_pcb_probe(tcpip_adapter_if_t tcpip_if, mdns_ip_protocol_t
     if (services_final_len) {
         _services = (mdns_srv_item_t **)malloc(sizeof(mdns_srv_item_t *) * services_final_len);
         if (!_services) {
+            HOOK_MALLOC_FAILED;
             return;
         }
 
@@ -1737,6 +1754,7 @@ static mdns_txt_linked_item_t * _mdns_allocate_txt(size_t num_items, mdns_txt_it
         for (i=0; i<num_items; i++) {
             mdns_txt_linked_item_t * new_item = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
             if (!new_item) {
+                HOOK_MALLOC_FAILED;
                 break;
             }
             new_item->key = strdup(txt[i].key);
@@ -1783,6 +1801,7 @@ static mdns_service_t * _mdns_create_service(const char * service, const char *
 {
     mdns_service_t * s = (mdns_service_t *)malloc(sizeof(mdns_service_t));
     if (!s) {
+        HOOK_MALLOC_FAILED;
         return NULL;
     }
 
@@ -2046,6 +2065,9 @@ static int _mdns_check_txt_collision(mdns_service_t * service, const uint8_t * d
             sprintf(tmp, "%s=%s", txt->key, txt->value);
             _mdns_append_string(ours, &index, tmp);
             free(tmp);
+        } else {
+            HOOK_MALLOC_FAILED;
+            // continue
         }
         txt = txt->next;
     }
@@ -2402,6 +2424,10 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
     }
 
     mdns_txt_item_t * txt = (mdns_txt_item_t *)malloc(sizeof(mdns_txt_item_t) * num_items);
+    if (!txt) {
+        HOOK_MALLOC_FAILED;
+        return;
+    }
     memset(txt, 0, sizeof(mdns_txt_item_t) * num_items);
     size_t txt_num = 0;
 
@@ -2422,6 +2448,7 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
         }
         char * key = (char *)malloc(name_len + 1);
         if (!key) {
+            HOOK_MALLOC_FAILED;
             goto handle_error;//error
         }
 
@@ -2435,6 +2462,10 @@ static void _mdns_result_txt_create(const uint8_t * data, size_t len, mdns_txt_i
         int value_len = partLen - name_len - 1;
         if (value_len > 0) {
             char * value = (char *)malloc(value_len + 1);
+            if (!value) {
+                HOOK_MALLOC_FAILED;
+                goto handle_error;//error
+            }
             memcpy(value, data + i, value_len);
             value[value_len] = 0;
             i += value_len;
@@ -2498,6 +2529,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
 
     mdns_parsed_packet_t * parsed_packet = (mdns_parsed_packet_t *)malloc(sizeof(mdns_parsed_packet_t));
     if (!parsed_packet) {
+        HOOK_MALLOC_FAILED;
         return;
     }
     memset(parsed_packet, 0, sizeof(mdns_parsed_packet_t));
@@ -2560,6 +2592,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
                 while (a) {
                     mdns_parsed_question_t * question = (mdns_parsed_question_t *)malloc(sizeof(mdns_parsed_question_t));
                     if (!question) {
+                        HOOK_MALLOC_FAILED;
                         goto clear_rx_packet;
                     }
                     question->next = parsed_packet->questions;
@@ -2587,6 +2620,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
 
             mdns_parsed_question_t * question = (mdns_parsed_question_t *)malloc(sizeof(mdns_parsed_question_t));
             if (!question) {
+                HOOK_MALLOC_FAILED;
                 goto clear_rx_packet;
             }
             question->next = parsed_packet->questions;
@@ -2769,10 +2803,6 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
                 if (search_result) {
                     mdns_txt_item_t * txt = NULL;
                     size_t txt_count = 0;
-                    _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
-                    if (!txt_count) {
-                        continue;
-                    }
 
                     mdns_result_t * result = NULL;
                     if (search_result->type == MDNS_TYPE_PTR) {
@@ -2792,8 +2822,11 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
                             }
                         }
                         if (!result->txt) {
-                            result->txt = txt;
-                            result->txt_count = txt_count;
+                            _mdns_result_txt_create(data_ptr, data_len, &txt, &txt_count);
+                            if (txt_count) {
+                                result->txt = txt;
+                                result->txt_count = txt_count;
+                            }
                         }
                     } else {
                         _mdns_search_result_add_txt(search_result, txt, txt_count, packet->tcpip_if, packet->ip_protocol);
@@ -3047,6 +3080,7 @@ static mdns_search_once_t * _mdns_search_init(const char * name, const char * se
 {
     mdns_search_once_t * search = (mdns_search_once_t *)malloc(sizeof(mdns_search_once_t));
     if (!search) {
+        HOOK_MALLOC_FAILED;
         return NULL;
     }
     memset(search, 0, sizeof(mdns_search_once_t));
@@ -3138,6 +3172,7 @@ static mdns_ip_addr_t * _mdns_result_addr_create_ip(ip_addr_t * ip)
 {
     mdns_ip_addr_t * a = (mdns_ip_addr_t *)malloc(sizeof(mdns_ip_addr_t));
     if (!a) {
+        HOOK_MALLOC_FAILED;
         return NULL;
     }
     memset(a, 0 , sizeof(mdns_ip_addr_t));
@@ -3197,6 +3232,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
         if (!search->max_results || search->num_results < search->max_results) {
             r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
             if (!r) {
+                HOOK_MALLOC_FAILED;
                 return;
             }
 
@@ -3242,6 +3278,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search,
     if (!search->max_results || search->num_results < search->max_results) {
         r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
         if (!r) {
+            HOOK_MALLOC_FAILED;
             return NULL;
         }
 
@@ -3277,6 +3314,7 @@ static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char
     if (!search->max_results || search->num_results < search->max_results) {
         r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
         if (!r) {
+            HOOK_MALLOC_FAILED;
             return;
         }
 
@@ -3305,7 +3343,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
     while (r) {
         if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
             if (r->txt) {
-                return;
+                goto free_txt;
             }
             r->txt = txt;
             r->txt_count = txt_count;
@@ -3316,12 +3354,8 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
     if (!search->max_results || search->num_results < search->max_results) {
         r = (mdns_result_t *)malloc(sizeof(mdns_result_t));
         if (!r) {
-            for (i=0; i<txt_count; i++) {
-                free((char *)(txt[i].key));
-                free((char *)(txt[i].value));
-            }
-            free(txt);
-            return;
+            HOOK_MALLOC_FAILED;
+            goto free_txt;
         }
 
         memset(r, 0 , sizeof(mdns_result_t));
@@ -3333,6 +3367,14 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
         search->result = r;
         search->num_results++;
     }
+    return;
+
+free_txt:
+    for (i=0; i<txt_count; i++) {
+        free((char *)(txt[i].key));
+        free((char *)(txt[i].value));
+    }
+    free(txt);
 }
 
 /**
@@ -3418,6 +3460,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search
 
     mdns_out_question_t * q = (mdns_out_question_t *)malloc(sizeof(mdns_out_question_t));
     if (!q) {
+        HOOK_MALLOC_FAILED;
         _mdns_free_tx_packet(packet);
         return NULL;
     }
@@ -3440,6 +3483,7 @@ static mdns_tx_packet_t * _mdns_create_search_packet(mdns_search_once_t * search
             }
             mdns_out_answer_t * a = (mdns_out_answer_t *)malloc(sizeof(mdns_out_answer_t));
             if (!a) {
+                HOOK_MALLOC_FAILED;
                 _mdns_free_tx_packet(packet);
                 return NULL;
             }
@@ -3480,6 +3524,22 @@ static void _mdns_search_send_pcb(mdns_search_once_t * search, tcpip_adapter_if_
  */
 static void _mdns_search_send(mdns_search_once_t * search)
 {
+    mdns_search_once_t* queue = _mdns_server->search_once;
+    bool found = false;
+    // looking for this search in active searches
+    while (queue) {
+        if (queue == search) {
+            found = true;
+            break;
+        }
+        queue = queue->next;
+    }
+
+    if (!found) {
+        // no longer active -> skip sending this search
+        return;
+    }
+
     uint8_t i, j;
     for (i=0; i<TCPIP_ADAPTER_IF_MAX; i++) {
         for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
@@ -3669,6 +3729,7 @@ static void _mdns_execute_action(mdns_action_t * action)
         if (!txt) {
             txt = (mdns_txt_linked_item_t *)malloc(sizeof(mdns_txt_linked_item_t));
             if (!txt) {
+                HOOK_MALLOC_FAILED;
                 _mdns_free_action(action);
                 return;
             }
@@ -3777,6 +3838,7 @@ static esp_err_t _mdns_send_search_action(mdns_action_type_t type, mdns_search_o
 
     action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
 
@@ -3812,6 +3874,9 @@ static void _mdns_scheduler_run()
                 free(action);
                 _mdns_server->tx_queue_head = p;
             }
+        } else {
+            HOOK_MALLOC_FAILED;
+            // continue
         }
     }
     MDNS_SERVICE_UNLOCK();
@@ -3822,12 +3887,13 @@ static void _mdns_scheduler_run()
  */
 static void _mdns_search_run()
 {
+    MDNS_SERVICE_LOCK();
     mdns_search_once_t * s = _mdns_server->search_once;
     uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
     if (!s) {
+        MDNS_SERVICE_UNLOCK();
         return;
     }
-    MDNS_SERVICE_LOCK();
     while (s) {
         if (s->state != SEARCH_OFF) {
             if (now > (s->started_at + s->timeout)) {
@@ -3945,6 +4011,7 @@ static esp_err_t _mdns_service_task_stop()
 {
     MDNS_SERVICE_LOCK();
     _mdns_stop_timer();
+    MDNS_SERVICE_UNLOCK();
     if (_mdns_service_task_handle) {
         mdns_action_t action;
         mdns_action_t * a = &action;
@@ -3957,7 +4024,6 @@ static esp_err_t _mdns_service_task_stop()
             vTaskDelay(10 / portTICK_PERIOD_MS);
         }
     }
-    MDNS_SERVICE_UNLOCK();
     return ESP_OK;
 }
 
@@ -3973,6 +4039,7 @@ esp_err_t mdns_handle_system_event(void *ctx, system_event_t *event)
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_OK;
     }
     action->type = ACTION_SYSTEM_EVENT;
@@ -3994,6 +4061,7 @@ esp_err_t mdns_init()
 
     _mdns_server = (mdns_server_t *)malloc(sizeof(mdns_server_t));
     if (!_mdns_server) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
     memset((uint8_t*)_mdns_server, 0, sizeof(mdns_server_t));
@@ -4047,8 +4115,8 @@ void mdns_free()
     if (!_mdns_server) {
         return;
     }
-    _mdns_service_task_stop();
     mdns_service_remove_all(_mdns_server);
+    _mdns_service_task_stop();
     for (i=0; i<TCPIP_ADAPTER_IF_MAX; i++) {
         for (j=0; j<MDNS_IP_PROTOCOL_MAX; j++) {
             _mdns_pcb_deinit(i, j);
@@ -4096,6 +4164,7 @@ esp_err_t mdns_hostname_set(const char * hostname)
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         free(new_hostname);
         return ESP_ERR_NO_MEM;
     }
@@ -4124,6 +4193,7 @@ esp_err_t mdns_instance_name_set(const char * instance)
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         free(new_instance);
         return ESP_ERR_NO_MEM;
     }
@@ -4163,6 +4233,7 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch
 
     item = (mdns_srv_item_t *)malloc(sizeof(mdns_srv_item_t));
     if (!item) {
+        HOOK_MALLOC_FAILED;
         _mdns_free_service(s);
         return ESP_ERR_NO_MEM;
     }
@@ -4172,6 +4243,7 @@ esp_err_t mdns_service_add(const char * instance, const char * service, const ch
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         _mdns_free_service(s);
         free(item);
         return ESP_ERR_NO_MEM;
@@ -4208,6 +4280,7 @@ esp_err_t mdns_service_port_set(const char * service, const char * proto, uint16
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
     action->type = ACTION_SERVICE_PORT_SET;
@@ -4240,6 +4313,7 @@ esp_err_t mdns_service_txt_set(const char * service, const char * proto, mdns_tx
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         _mdns_free_linked_txt(new_txt);
         return ESP_ERR_NO_MEM;
     }
@@ -4267,6 +4341,7 @@ esp_err_t mdns_service_txt_item_set(const char * service, const char * proto, co
     }
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
 
@@ -4303,6 +4378,7 @@ esp_err_t mdns_service_txt_item_remove(const char * service, const char * proto,
     }
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
 
@@ -4340,6 +4416,7 @@ esp_err_t mdns_service_instance_name_set(const char * service, const char * prot
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         free(new_instance);
         return ESP_ERR_NO_MEM;
     }
@@ -4366,6 +4443,7 @@ esp_err_t mdns_service_remove(const char * service, const char * proto)
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
     action->type = ACTION_SERVICE_DEL;
@@ -4388,6 +4466,7 @@ esp_err_t mdns_service_remove_all()
 
     mdns_action_t * action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
     if (!action) {
+        HOOK_MALLOC_FAILED;
         return ESP_ERR_NO_MEM;
     }
     action->type = ACTION_SERVICES_CLEAR;
index c8c70f075b67ac03746f6ef1dc276ee6a2fd6462..981b9d8252662c53da9c2da2d3da33fafb466831 100644 (file)
@@ -5,6 +5,7 @@
  */
 #include <string.h>
 #include "mdns_networking.h"
+#include "esp_log.h"
 
 
 extern mdns_server_t * _mdns_server;
@@ -13,6 +14,7 @@ extern mdns_server_t * _mdns_server;
  * MDNS Server Networking
  *
  */
+static const char *TAG = "MDNS_Networking";
 
 static struct udp_pcb * _pcb_main = NULL;
 
@@ -118,6 +120,7 @@ static void _udp_recv(void *arg, struct udp_pcb *upcb, struct pbuf *pb, const ip
 
         mdns_rx_packet_t * packet = (mdns_rx_packet_t *)malloc(sizeof(mdns_rx_packet_t));
         if (!packet) {
+            HOOK_MALLOC_FAILED;
             //missed packet - no memory
             pbuf_free(this_pb);
             continue;
index 568c81b096e8e008321c595bbeb932d7bf3676d4..934b9427e2bcf1f4edddbc05c412e9119d3f5988 100644 (file)
 #define MDNS_SEARCH_LOCK()      xSemaphoreTake(_mdns_server->search.lock, portMAX_DELAY)
 #define MDNS_SEARCH_UNLOCK()    xSemaphoreGive(_mdns_server->search.lock)
 
+#ifndef HOOK_MALLOC_FAILED
+#define HOOK_MALLOC_FAILED  ESP_LOGE(TAG, "Cannot allocate memory (line: %d, free heap: %d bytes)", __LINE__, esp_get_free_heap_size());
+#endif
 
 typedef enum {
     PCB_OFF, PCB_DUP, PCB_INIT,
index 79f9f23da9e7b931d5eeef5b6c9a74df165fc5c8..8c2948553d76bfb56901b50c3ad41e69ef4fa28e 100644 (file)
 #define RTC_SLOW_CLK_FREQ_8MD256    (RTC_FAST_CLK_FREQ_8M / 256)
 #define RTC_SLOW_CLK_FREQ_32K       32768
 
+/* BBPLL configuration values */
 #define BBPLL_ENDIV5_VAL_320M       0x43
 #define BBPLL_BBADC_DSMP_VAL_320M   0x84
 #define BBPLL_ENDIV5_VAL_480M       0xc3
 #define BBPLL_BBADC_DSMP_VAL_480M   0x74
+#define BBPLL_IR_CAL_DELAY_VAL      0x18
+#define BBPLL_IR_CAL_EXT_CAP_VAL    0x20
+#define BBPLL_OC_ENB_FCAL_VAL       0x9a
+#define BBPLL_OC_ENB_VCON_VAL       0x00
+#define BBPLL_BBADC_CAL_7_0_VAL     0x00
 
 #define APLL_SDM_STOP_VAL_1         0x09
 #define APLL_SDM_STOP_VAL_2_REV0    0x69
@@ -429,6 +435,13 @@ static void rtc_clk_bbpll_enable()
     CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG,
              RTC_CNTL_BIAS_I2C_FORCE_PD | RTC_CNTL_BB_I2C_FORCE_PD |
              RTC_CNTL_BBPLL_FORCE_PD | RTC_CNTL_BBPLL_I2C_FORCE_PD);
+
+    /* reset BBPLL configuration */
+    I2C_WRITEREG_RTC(I2C_BBPLL, I2C_BBPLL_IR_CAL_DELAY, BBPLL_IR_CAL_DELAY_VAL);
+    I2C_WRITEREG_RTC(I2C_BBPLL, I2C_BBPLL_IR_CAL_EXT_CAP, BBPLL_IR_CAL_EXT_CAP_VAL);
+    I2C_WRITEREG_RTC(I2C_BBPLL, I2C_BBPLL_OC_ENB_FCAL, BBPLL_OC_ENB_FCAL_VAL);
+    I2C_WRITEREG_RTC(I2C_BBPLL, I2C_BBPLL_OC_ENB_VCON, BBPLL_OC_ENB_VCON_VAL);
+    I2C_WRITEREG_RTC(I2C_BBPLL, I2C_BBPLL_BBADC_CAL_7_0, BBPLL_BBADC_CAL_7_0_VAL);
 }
 
 /**
index 69e489930566cf6d137437ea32410b52e0b61a45..367602705917c8acf24bf687b72461fe9f1fb6a1 100644 (file)
@@ -653,8 +653,9 @@ void app_main()
         .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
         .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
         .dma_buf_count = 6,
-        .dma_buf_len = 60,                                                      //
-        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1                                //Interrupt level 1
+        .dma_buf_len = 60,
+        .intr_alloc_flags = 0,                                                  //Default interrupt priority
+        .tx_desc_auto_clear = true                                              //Auto clear tx descriptor on underflow
     };
 
 
index 66850d30d391bc69182cc7b000482254a305ae9b..830afe45e4793a431a03fc358efa2a7299b24eda 100644 (file)
@@ -63,8 +63,9 @@ void app_main()
         .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
         .communication_format = I2S_COMM_FORMAT_I2S_MSB,
         .dma_buf_count = 6,
-        .dma_buf_len = 60,                                                      //
-        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1                                //Interrupt level 1
+        .dma_buf_len = 60,
+        .intr_alloc_flags = 0,                                                  //Default interrupt priority
+        .tx_desc_auto_clear = true                                              //Auto clear tx descriptor on underflow
     };
 
 
diff --git a/examples/protocols/modbus_slave/main/Kconfig.projbuild b/examples/protocols/modbus_slave/main/Kconfig.projbuild
new file mode 100644 (file)
index 0000000..c4e1217
--- /dev/null
@@ -0,0 +1,27 @@
+menu "Modbus Slave Example Configuration"
+
+config MB_UART_RXD
+    int "UART RXD pin number"
+    range 0 34
+    default 22
+    help
+        GPIO number for UART RX pin. See UART documentation for more information
+        about available pin numbers for UART.
+        
+config MB_UART_TXD
+    int "UART TXD pin number"
+    range 0 34
+    default 23
+    help
+        GPIO number for UART TX pin. See UART documentation for more information
+        about available pin numbers for UART.
+
+config MB_UART_RTS
+    int "UART RTS pin number"
+    range 0 34
+    default 18
+    help
+        GPIO number for UART RTS pin. This pin is connected to 
+        ~RE/DE pin of RS485 transceiver to switch direction.
+
+endmenu
index 19e18f350c1dd7000855743eb5dd08509326ef27..ff52efcb46d391727d7525f379572428935403c4 100644 (file)
@@ -75,7 +75,7 @@ void app_main()
     comm_info.baudrate = MB_DEV_SPEED;
     comm_info.parity = MB_PARITY_NONE;
     ESP_ERROR_CHECK(mbcontroller_setup(comm_info));
-
+    
     // The code below initializes Modbus register area descriptors
     // for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
     // Initialization should be done for each supported Modbus register area according to register map.
@@ -113,6 +113,14 @@ void app_main()
 
     // Starts of modbus controller and stack
     ESP_ERROR_CHECK(mbcontroller_start());
+    
+    // Set UART driver mode to Half Duplex
+    ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));  
+
+    // Set UART pin numbers
+    ESP_ERROR_CHECK(uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, 
+                                    CONFIG_MB_UART_RXD, CONFIG_MB_UART_RTS, 
+                                    UART_PIN_NO_CHANGE));
 
     // The cycle below will be terminated when parameter holdingRegParams.dataChan0
     // incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.