2
0
Эх сурвалжийг харах

idf overriding method to bring back SPDIF and fix SPI + new CSPOT (which crashes)

Philippe G 3 жил өмнө
parent
commit
06b637c55b
43 өөрчлөгдсөн 2955 нэмэгдсэн , 402 устгасан
  1. 6 0
      components/_override/CMakeLists.txt
  2. 1206 0
      components/_override/i2s.c
  3. 275 0
      components/_override/i2s_hal.c
  4. 998 0
      components/_override/spi_master.c
  5. 4 4
      components/esp_http_server/CMakeLists.txt
  6. 0 2
      components/services/CMakeLists.txt
  7. 1 1
      components/services/gpio_exp.c
  8. 5 13
      components/spotify/CMakeLists.txt
  9. 2 2
      components/spotify/Shim.cpp
  10. 30 56
      components/spotify/cspot/CMakeLists.txt
  11. 61 41
      components/spotify/cspot/bell/CMakeLists.txt
  12. 7 0
      components/spotify/cspot/bell/include/BaseHTTPServer.h
  13. 4 2
      components/spotify/cspot/bell/include/BellSocket.h
  14. 19 27
      components/spotify/cspot/bell/include/TCPSocket.h
  15. 2 1
      components/spotify/cspot/bell/include/platform/TLSSocket.h
  16. 16 17
      components/spotify/cspot/bell/src/HTTPServer.cpp
  17. 6 17
      components/spotify/cspot/bell/src/platform/esp/TLSSocket.cpp
  18. 7 16
      components/spotify/cspot/bell/src/platform/linux/TLSSocket.cpp
  19. 15 5
      components/spotify/cspot/cpp-reflection/README.md
  20. 1 1
      components/spotify/cspot/include/ConfigJSON.h
  21. 1 1
      components/spotify/cspot/include/LoginBlob.h
  22. 4 2
      components/spotify/cspot/include/MercuryManager.h
  23. 3 1
      components/spotify/cspot/include/MercuryResponse.h
  24. 6 3
      components/spotify/cspot/include/PlayerState.h
  25. 4 1
      components/spotify/cspot/include/Session.h
  26. 0 1
      components/spotify/cspot/include/SpircController.h
  27. 3 2
      components/spotify/cspot/include/SpotifyTrack.h
  28. 2 1
      components/spotify/cspot/include/TrackReference.h
  29. 1 2
      components/spotify/cspot/protocol/authentication.proto
  30. 2 2
      components/spotify/cspot/protocol/keyexchange.proto
  31. 8 8
      components/spotify/cspot/src/ConfigJSON.cpp
  32. 1 1
      components/spotify/cspot/src/LoginBlob.cpp
  33. 12 6
      components/spotify/cspot/src/MercuryManager.cpp
  34. 6 1
      components/spotify/cspot/src/MercuryResponse.cpp
  35. 113 71
      components/spotify/cspot/src/PlayerState.cpp
  36. 51 28
      components/spotify/cspot/src/Session.cpp
  37. 3 1
      components/spotify/cspot/src/Shannon.cpp
  38. 28 29
      components/spotify/cspot/src/SpircController.cpp
  39. 32 27
      components/spotify/cspot/src/SpotifyTrack.cpp
  40. 5 4
      components/spotify/cspot/src/TrackReference.cpp
  41. 1 1
      components/squeezelite/output_i2s.c
  42. 1 1
      main/CMakeLists.txt
  43. 3 3
      sdkconfig_compare.js

+ 6 - 0
components/_override/CMakeLists.txt

@@ -0,0 +1,6 @@
+set(srcs i2s.c i2s_hal.c spi_master.c)
+idf_component_register( SRCS ${srcs}
+						INCLUDE_DIRS ${IDF_PATH}/components/driver
+)
+
+message("overriding ${srcs} !! THIS MUST BE REQUIRED BY MAIN !!")

+ 1206 - 0
components/_override/i2s.c

@@ -0,0 +1,1206 @@
+// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
+//
+// 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.
+
+#include <string.h>
+#include <stdbool.h>
+#include <math.h>
+#include <esp_types.h>
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/queue.h"
+#include "freertos/semphr.h"
+
+#include "soc/lldesc.h"
+#include "driver/gpio.h"
+#include "driver/i2s.h"
+#include "hal/gpio_hal.h"
+#if SOC_I2S_SUPPORTS_ADC_DAC
+#include "driver/dac.h"
+#include "hal/i2s_hal.h"
+#include "adc1_private.h"
+#endif
+
+#include "soc/rtc.h"
+
+#include "esp_intr_alloc.h"
+#include "esp_err.h"
+#include "esp_attr.h"
+#include "esp_log.h"
+#include "esp_pm.h"
+#include "esp_efuse.h"
+#include "esp_rom_gpio.h"
+
+#include "sdkconfig.h"
+
+static const char* I2S_TAG = "I2S";
+
+#define I2S_CHECK(a, str, ret) if (!(a)) {                                              \
+        ESP_LOGE(I2S_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str);                    \
+        return (ret);                                                                   \
+        }
+
+#define I2S_ENTER_CRITICAL_ISR()          portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
+#define I2S_EXIT_CRITICAL_ISR()           portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
+#define I2S_ENTER_CRITICAL()              portENTER_CRITICAL(&i2s_spinlock[i2s_num])
+#define I2S_EXIT_CRITICAL()               portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
+#define I2S_FULL_DUPLEX_SLAVE_MODE_MASK   (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
+#define I2S_FULL_DUPLEX_MASTER_MODE_MASK  (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
+
+//TODO: Refactor to put this logic into LL
+#define I2S_AD_BCK_FACTOR                 (2)
+#define I2S_PDM_BCK_FACTOR                (64)
+#define I2S_BASE_CLK                      (2*APB_CLK_FREQ)
+#define APLL_I2S_MIN_RATE                 (10675) //in Hz, I2S Clock rate limited by hardware
+
+/**
+ * @brief DMA buffer object
+ *
+ */
+typedef struct {
+    char **buf;
+    int buf_size;
+    int rw_pos;
+    void *curr_ptr;
+    SemaphoreHandle_t mux;
+    xQueueHandle queue;
+    lldesc_t **desc;
+} i2s_dma_t;
+
+/**
+ * @brief I2S object instance
+ *
+ */
+typedef struct {
+    i2s_port_t i2s_num;         /*!< I2S port number*/
+    int queue_size;             /*!< I2S event queue size*/
+    QueueHandle_t i2s_queue;    /*!< I2S queue handler*/
+    int dma_buf_count;          /*!< DMA buffer count, number of buffer*/
+    int dma_buf_len;            /*!< DMA buffer length, length of each buffer*/
+    i2s_dma_t *rx;              /*!< DMA Tx buffer*/
+    i2s_dma_t *tx;              /*!< DMA Rx buffer*/
+    i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
+    int channel_num;            /*!< Number of channels*/
+    int bytes_per_sample;        /*!< Bytes per sample*/
+    int bits_per_sample;        /*!< Bits per sample*/
+    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 */
+    double real_rate;
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_handle_t pm_lock;
+#endif
+    i2s_hal_context_t hal;        /*!< I2S hal context*/
+} i2s_obj_t;
+
+static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
+
+static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
+#if SOC_I2S_SUPPORTS_ADC_DAC
+static int _i2s_adc_unit = -1;
+static int _i2s_adc_channel = -1;
+#endif
+
+static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
+static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
+
+static inline void gpio_matrix_out_check(int gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
+{
+    //if pin = -1, do not need to configure
+    if (gpio != -1) {
+        gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
+        gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
+        esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
+    }
+}
+
+static inline void gpio_matrix_in_check(int gpio, uint32_t signal_idx, bool inv)
+{
+    if (gpio != -1) {
+        gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
+        //Set direction, for some GPIOs, the input function are not enabled as default.
+        gpio_set_direction(gpio, GPIO_MODE_INPUT);
+        esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
+    }
+}
+
+esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
+    return ESP_OK;
+}
+
+esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
+{
+
+    I2S_ENTER_CRITICAL();
+    i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
+{
+    I2S_ENTER_CRITICAL();
+    i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
+{
+    I2S_ENTER_CRITICAL();
+    i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
+{
+    I2S_ENTER_CRITICAL();
+    i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+float i2s_get_clk(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    return p_i2s_obj[i2s_num]->real_rate;
+}
+
+static esp_err_t i2s_isr_register(i2s_port_t i2s_num, int intr_alloc_flags, void (*fn)(void*), void * arg, i2s_isr_handle_t *handle)
+{
+    return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
+}
+
+static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
+{
+    int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
+
+#if CONFIG_IDF_TARGET_ESP32
+    /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
+    if (esp_efuse_get_chip_ver() == 0) {
+        sdm0 = 0;
+        sdm1 = 0;
+    }
+#endif
+    float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
+    if (fout < SOC_I2S_APLL_MIN_FREQ || fout > SOC_I2S_APLL_MAX_FREQ) {
+        return SOC_I2S_APLL_MAX_FREQ;
+    }
+    float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
+    return fpll/2;
+}
+
+/**
+ * @brief     APLL calculate function, was described by following:
+ *            APLL Output frequency is given by the formula:
+ *
+ *            apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
+ *            apll_freq = fout / ((o_div + 2) * 2)
+ *
+ *            The dividend in this expression should be in the range of 240 - 600 MHz.
+ *            In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
+ *            * sdm0  frequency adjustment parameter, 0..255
+ *            * sdm1  frequency adjustment parameter, 0..255
+ *            * sdm2  frequency adjustment parameter, 0..63
+ *            * o_div  frequency divider, 0..31
+ *
+ *            The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
+ *            then apply the above formula, finding the closest frequency to the desired one.
+ *            But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
+ *            1. We will choose the parameters with the highest level of change,
+ *               With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
+ *               Take average frequency close to the desired frequency, and select sdm2
+ *            2. Next, we look for sequences of less influential and more detailed parameters,
+ *               also by taking the average of the largest and smallest frequencies closer to the desired frequency.
+ *            3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
+ *
+ * @param[in]  rate                  The I2S Frequency (MCLK)
+ * @param[in]  bits_per_sample       The bits per sample
+ * @param[out]      sdm0             The sdm 0
+ * @param[out]      sdm1             The sdm 1
+ * @param[out]      sdm2             The sdm 2
+ * @param[out]      odir             The odir
+ *
+ * @return     ESP_ERR_INVALID_ARG or ESP_OK
+ */
+
+static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
+ {
+ 	int _odir, _sdm0, _sdm1, _sdm2;
+	float r = rtc_clk_xtal_freq_get() * 1000000. / (rate * 2 * 2);
+	int _sdm2_max;
+	uint32_t prec = -1;
+	int o, s1, s0;
+
+	if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
+        return ESP_ERR_INVALID_ARG;
+    }
+
+	*sdm0 = 0;
+	*sdm1 = 0;
+	*sdm2 = 0;
+	*odir = 0;
+
+	_sdm2 = 1/r * 2 - 4;
+	if (_sdm2 < 4) _sdm2 = 4;
+	_sdm2_max = ceil(1/r * (31 + 2) - (255/256 + 255/65536 + 4));
+	if (_sdm2_max > 8) _sdm2_max = 8;
+
+	// explore up to 5 sdm2 values
+	for (; _sdm2 < _sdm2_max; _sdm2++) {
+		_odir = r * (_sdm2 + 4) - 2;
+		if (_odir < 0) _odir = 0;
+		else if (_odir > 31) _odir = 31;
+
+		for (o = 0; o < 2 && _odir + o < 32; o++) {
+			_sdm1 = 256*((_odir + o + 2) / r - (_sdm2 + 4));
+			if (_sdm1 < 0) _sdm1 = 0;
+			else if (_sdm1 > 255) _sdm1 = 255;
+
+			for (s1 = 0; s1 < 2 && _sdm1 + s1 < 256; s1++) {
+				_sdm0 = 65536*((_odir + o + 2) / r - (_sdm2 + (float) (_sdm1 + s1)/256 + 4));
+				if (_sdm0 < 0) _sdm1 = 0;
+				else if (_sdm0 > 255) _sdm0 = 255;
+
+				for (s0 = 0; s0 < 2 && _sdm2 + s0 < 256; s0++) {
+					int _fi2s = i2s_apll_get_fi2s(bits_per_sample, _sdm0 + s0, _sdm1 + s1, _sdm2, _odir + o);
+					if (abs(_fi2s - rate) < prec) {
+						prec = abs(_fi2s - rate);
+						*sdm0 = _sdm0 + s0;
+						*sdm1 = _sdm1 + s1;
+						*sdm2 = _sdm2;
+						*odir = _odir + o;
+                    }
+				}
+			}
+		}
+	}
+
+	if (*sdm2 + *sdm0 + *sdm0 + *odir) return ESP_OK;
+	else return ESP_ERR_INVALID_ARG;
+}
+
+esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
+{
+    int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
+    int clkmInteger, clkmDecimals, bck = 0;
+    double denom = (double)1 / 64;
+    int channel = 2;
+    i2s_dma_t *save_tx = NULL, *save_rx = NULL;
+ESP_LOGW(I2S_TAG, "THIS IS US IN I2S !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+
+    if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
+        ESP_LOGE(I2S_TAG, "Invalid bits per sample");
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    if (p_i2s_obj[i2s_num] == NULL) {
+        ESP_LOGE(I2S_TAG, "Not initialized yet");
+        return ESP_ERR_INVALID_ARG;
+    }
+    p_i2s_obj[i2s_num]->sample_rate = rate;
+
+    /**
+     * Due to hardware issue, bck division on ESP32/ESP32-S2 should be greater than 8 in slave mode
+     * So the factor need to be an appropriate value
+     */
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) && !p_i2s_obj[i2s_num]->use_apll) {
+        factor = 64 * bits;
+    }
+    double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
+
+    if (clkmdiv > 256) {
+        ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    // wait all on-going writing finish
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
+        xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
+    }
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
+        xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
+    }
+
+    i2s_stop(i2s_num);
+#if SOC_I2S_SUPPORTS_ADC_DAC
+    /* I2S-ADC only support single channel format. */
+    if (!(p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN)) {
+        i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
+    }
+#else
+    i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
+#endif
+    i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
+
+    if (p_i2s_obj[i2s_num]->channel_num != (int)ch) {
+        p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
+    }
+
+    if ((int)bits != p_i2s_obj[i2s_num]->bits_per_sample) {
+        p_i2s_obj[i2s_num]->bits_per_sample = bits;
+
+        // Round bytes_per_sample up to next multiple of 16 bits
+        int halfwords_per_sample = (bits + 15) / 16;
+        p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
+
+        // Because limited of DMA buffer is 4092 bytes
+        if (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num > 4092) {
+            p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
+        }
+        // Re-create TX DMA buffer
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+
+            save_tx = p_i2s_obj[i2s_num]->tx;
+
+            p_i2s_obj[i2s_num]->tx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
+            if (p_i2s_obj[i2s_num]->tx == NULL) {
+                ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
+                i2s_driver_uninstall(i2s_num);
+                return ESP_ERR_NO_MEM;
+            }
+            i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
+
+            //destroy old tx dma if exist
+            if (save_tx) {
+                i2s_destroy_dma_queue(i2s_num, save_tx);
+            }
+        }
+        // Re-create RX DMA buffer
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+
+            save_rx = p_i2s_obj[i2s_num]->rx;
+
+            p_i2s_obj[i2s_num]->rx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
+            if (p_i2s_obj[i2s_num]->rx == NULL){
+                ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
+                i2s_driver_uninstall(i2s_num);
+                return ESP_ERR_NO_MEM;
+            }
+            i2s_hal_set_in_link(&(p_i2s_obj[i2s_num]->hal), p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample, (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0]);
+            //destroy old rx dma if exist
+            if (save_rx) {
+                i2s_destroy_dma_queue(i2s_num, save_rx);
+            }
+        }
+
+    }
+
+    double mclk;
+    //int sdm0, sdm1, sdm2, odir, m_scale = 8;
+	int sdm0, sdm1, sdm2, odir, m_scale = (rate > 96000 && bits > 16) ? 4 : 8;
+    int fi2s_clk = rate*channel*bits*m_scale;
+#if SOC_I2S_SUPPORTS_ADC_DAC
+    if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
+
+        //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
+        //Rate as given to this function is the intended sample rate;
+        //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
+        uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
+        fi2s_clk /= I2S_AD_BCK_FACTOR;
+        int factor2 = 60;
+        mclk = b_clk * factor2;
+        clkmdiv = ((double) I2S_BASE_CLK) / mclk;
+        clkmInteger = clkmdiv;
+        clkmDecimals = (clkmdiv - clkmInteger) / denom;
+        bck = mclk / b_clk;
+#endif
+#if SOC_I2S_SUPPORTS_PDM
+    } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
+        uint32_t b_clk = 0;
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+            uint32_t fp, fs;
+            i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
+            // Recommended set `fp = 960, fs = sample_rate / 100`
+            fs = rate / 100;
+            i2s_hal_tx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), fp, fs);
+            b_clk = rate * I2S_PDM_BCK_FACTOR * fp / fs;
+
+        } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+            uint32_t dsr;
+            i2s_hal_get_rx_pdm(&(p_i2s_obj[i2s_num]->hal), &dsr);
+            b_clk = rate * I2S_PDM_BCK_FACTOR * (dsr ? 2 : 1);
+        }
+        fi2s_clk = b_clk * m_scale;
+        int factor2 = 5 ;
+        mclk = b_clk * factor2;
+        clkmdiv = ((double) I2S_BASE_CLK) / mclk;
+        clkmInteger = clkmdiv;
+        clkmDecimals = (clkmdiv - clkmInteger) / denom;
+        bck = mclk / b_clk;
+    } else
+#endif
+    {
+        clkmInteger = clkmdiv;
+        clkmDecimals = (clkmdiv - clkmInteger) / denom;
+        mclk = clkmInteger + denom * clkmDecimals;
+        bck = factor/(bits * channel);
+    }
+
+    if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
+        fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
+        m_scale = fi2s_clk/bits/rate/channel;
+    }
+    if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
+        ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
+        rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
+        i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
+        i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
+        double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
+        p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
+        ESP_LOGI(I2S_TAG, "APLL: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK_M: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
+            rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
+    } else {
+        i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
+        i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
+        double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
+        p_i2s_obj[i2s_num]->real_rate = real_rate;
+        ESP_LOGI(I2S_TAG, "PLL_D2: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
+            rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
+    }
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+        p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
+        p_i2s_obj[i2s_num]->tx->rw_pos = 0;
+    }
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+        p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
+        p_i2s_obj[i2s_num]->rx->rw_pos = 0;
+    }
+
+    i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
+    i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
+
+    // wait all writing on-going finish
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
+        xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
+    }
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
+        xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
+    }
+    i2s_start(i2s_num);
+    return ESP_OK;
+}
+
+static void IRAM_ATTR i2s_intr_handler_default(void *arg)
+{
+    i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
+    uint32_t status;
+    i2s_hal_get_intr_status(&(p_i2s->hal), &status);
+    if(status == 0) {
+        //Avoid spurious interrupt
+        return;
+    }
+
+    i2s_event_t i2s_event;
+    int dummy;
+
+    portBASE_TYPE high_priority_task_awoken = 0;
+
+    lldesc_t *finish_desc = NULL;
+
+    if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
+        ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
+        if (p_i2s->i2s_queue) {
+            i2s_event.type = I2S_EVENT_DMA_ERROR;
+            if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
+                xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
+            }
+            xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
+        }
+    }
+
+    if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
+        i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
+        // 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) {
+            i2s_event.type = I2S_EVENT_TX_DONE;
+            if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
+                xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
+            }
+            xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
+        }
+    }
+
+    if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
+        // All buffers are full. This means we have an overflow.
+        i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
+        if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
+            xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
+        }
+        xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
+        if (p_i2s->i2s_queue) {
+            i2s_event.type = I2S_EVENT_RX_DONE;
+            if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
+                xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
+            }
+            xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
+        }
+    }
+    i2s_hal_clear_intr_status(&(p_i2s->hal), status);
+
+    if (high_priority_task_awoken == pdTRUE) {
+        portYIELD_FROM_ISR();
+    }
+}
+
+static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
+{
+    int bux_idx;
+    if (p_i2s_obj[i2s_num] == NULL) {
+        ESP_LOGE(I2S_TAG, "Not initialized yet");
+        return ESP_ERR_INVALID_ARG;
+    }
+    if (dma == NULL) {
+        ESP_LOGE(I2S_TAG, "dma is NULL");
+        return ESP_ERR_INVALID_ARG;
+    }
+    for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
+        if (dma->desc && dma->desc[bux_idx]) {
+            free(dma->desc[bux_idx]);
+        }
+        if (dma->buf && dma->buf[bux_idx]) {
+            free(dma->buf[bux_idx]);
+        }
+    }
+    if (dma->buf) {
+        free(dma->buf);
+    }
+    if (dma->desc) {
+        free(dma->desc);
+    }
+    vQueueDelete(dma->queue);
+    vSemaphoreDelete(dma->mux);
+    free(dma);
+    return ESP_OK;
+}
+
+static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
+{
+    int bux_idx;
+    int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
+    i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
+    if (dma == NULL) {
+        ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
+        return NULL;
+    }
+    memset(dma, 0, sizeof(i2s_dma_t));
+
+    dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
+    if (dma->buf == NULL) {
+        ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
+        free(dma);
+        return NULL;
+    }
+    memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
+
+    for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
+        dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
+        if (dma->buf[bux_idx] == NULL) {
+            ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
+            i2s_destroy_dma_queue(i2s_num, dma);
+            return NULL;
+        }
+        ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
+    }
+
+    dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
+    if (dma->desc == NULL) {
+        ESP_LOGE(I2S_TAG, "Error malloc dma description");
+        i2s_destroy_dma_queue(i2s_num, dma);
+        return NULL;
+    }
+    for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
+        dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
+        if (dma->desc[bux_idx] == NULL) {
+            ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
+            i2s_destroy_dma_queue(i2s_num, dma);
+            return NULL;
+        }
+    }
+
+    for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
+        dma->desc[bux_idx]->owner = 1;
+        dma->desc[bux_idx]->eof = 1;
+        dma->desc[bux_idx]->sosf = 0;
+        dma->desc[bux_idx]->length = dma_buf_len * sample_size;
+        dma->desc[bux_idx]->size = dma_buf_len * sample_size;
+        dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
+        dma->desc[bux_idx]->offset = 0;
+        dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
+    }
+    dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
+    dma->mux = xSemaphoreCreateMutex();
+    dma->buf_size = dma_buf_len * sample_size;
+    ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
+    return dma;
+}
+
+esp_err_t i2s_start(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    //start DMA link
+    I2S_ENTER_CRITICAL();
+    i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
+
+    esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
+    i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+        i2s_enable_tx_intr(i2s_num);
+        i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
+    }
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+        i2s_enable_rx_intr(i2s_num);
+        i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
+    }
+    esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+esp_err_t i2s_stop(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_ENTER_CRITICAL();
+    esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+        i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
+        i2s_disable_tx_intr(i2s_num);
+    }
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+        i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
+        i2s_disable_rx_intr(i2s_num);
+    }
+    uint32_t mask;
+    i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
+    i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
+    I2S_EXIT_CRITICAL();
+    return ESP_OK;
+}
+
+#if SOC_I2S_SUPPORTS_ADC_DAC
+esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
+{
+    I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
+    if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
+        dac_output_disable(DAC_CHANNEL_1);
+        dac_output_disable(DAC_CHANNEL_2);
+        dac_i2s_disable();
+    } else {
+        dac_i2s_enable();
+    }
+
+    if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
+        //DAC1, right channel
+        dac_output_enable(DAC_CHANNEL_1);
+    }
+    if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
+        //DAC2, left channel
+        dac_output_enable(DAC_CHANNEL_2);
+    }
+    return ESP_OK;
+}
+
+static esp_err_t _i2s_adc_mode_recover(void)
+{
+    I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
+    return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
+}
+
+esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
+{
+    I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
+    // For now, we only support SAR ADC1.
+    _i2s_adc_unit = adc_unit;
+    _i2s_adc_channel = adc_channel;
+    return adc_i2s_mode_init(adc_unit, adc_channel);
+}
+#endif
+
+esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    if (pin == NULL) {
+#if SOC_I2S_SUPPORTS_ADC_DAC
+        return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
+#else
+        return ESP_ERR_INVALID_ARG;
+#endif
+    }
+
+    if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
+        ESP_LOGE(I2S_TAG, "bck_io_num error");
+        return ESP_FAIL;
+    }
+    if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
+        ESP_LOGE(I2S_TAG, "ws_io_num error");
+        return ESP_FAIL;
+    }
+    if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
+        ESP_LOGE(I2S_TAG, "data_out_num error");
+        return ESP_FAIL;
+    }
+    if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
+        ESP_LOGE(I2S_TAG, "data_in_num error");
+        return ESP_FAIL;
+    }
+
+    int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
+    //Each IIS hw module has a RX and TX unit.
+    //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
+    //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
+            bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
+            ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
+            data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
+        } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
+            bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
+            ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
+            data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
+        }
+    }
+    //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
+    //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
+            bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
+            ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
+            data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
+        } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
+            bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
+            ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
+            data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
+        }
+    }
+    //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
+    //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
+    if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
+        bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
+        ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
+    } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
+        bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
+        ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
+    }
+    gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
+    gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
+    if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
+        gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
+        gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
+    } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
+        gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
+        gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
+    }
+    ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
+
+    return ESP_OK;
+}
+
+esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
+    return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
+}
+
+#if SOC_I2S_SUPPORTS_PDM
+esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    i2s_hal_rx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), dsr);
+    return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
+}
+#endif
+
+static esp_err_t i2s_check_cfg_static(i2s_port_t i2s_num, const i2s_config_t *cfg)
+{
+#if SOC_I2S_SUPPORTS_ADC_DAC
+    //We only check if the I2S number is invalid when set to build in ADC and DAC mode.
+    I2S_CHECK(!((cfg->mode & I2S_MODE_ADC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S ADC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
+    I2S_CHECK(!((cfg->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
+    return ESP_OK;
+#endif
+#if SOC_I2S_SUPPORTS_PDM
+    //We only check if the I2S number is invalid when set to PDM mode.
+    I2S_CHECK(!((cfg->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
+    return ESP_OK;
+#endif
+
+    I2S_CHECK(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), "invalid communication formats", ESP_ERR_INVALID_ARG);
+    I2S_CHECK(!((cfg->communication_format & I2S_COMM_FORMAT_STAND_MSB) && (cfg->communication_format & I2S_COMM_FORMAT_STAND_PCM_LONG)), "multiple communication formats specified", ESP_ERR_INVALID_ARG);
+    return ESP_OK;
+}
+
+static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((i2s_check_cfg_static(i2s_num, i2s_config) == ESP_OK), "param check error", ESP_ERR_INVALID_ARG);
+
+#if SOC_I2S_SUPPORTS_ADC_DAC
+    if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
+        //in ADC built-in mode, we need to call i2s_set_adc_mode to
+        //initialize the specific ADC channel.
+        //in the current stage, we only support ADC1 and single channel mode.
+        //In default data mode, the ADC data is in 12-bit resolution mode.
+        adc_power_acquire();
+    }
+#endif
+    // configure I2S data port interface.
+    i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
+    if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) &&  (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
+        i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
+        if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
+            i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
+        } else {
+            i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
+        }
+    }
+
+    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;
+}
+
+esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
+        for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
+            memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
+        }
+    }
+    if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
+        int bytes_left = 0;
+        bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
+        if (bytes_left) {
+            size_t zero_bytes = 0, bytes_written;
+            i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
+        }
+        for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
+            memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
+        }
+    }
+    return ESP_OK;
+}
+
+esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
+{
+    esp_err_t err;
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((i2s_config->dma_buf_count >= 2 && i2s_config->dma_buf_count <= 128), "I2S buffer count less than 128 and more than 2", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((i2s_config->dma_buf_len >= 8 && i2s_config->dma_buf_len <= 1024), "I2S buffer length at most 1024 and more than 8", ESP_ERR_INVALID_ARG);
+    if (p_i2s_obj[i2s_num] == NULL) {
+        p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
+        if (p_i2s_obj[i2s_num] == NULL) {
+            ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
+            return ESP_ERR_NO_MEM;
+        }
+        memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
+
+        portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
+        for (int x = 0; x < I2S_NUM_MAX; x++) {
+            i2s_spinlock[x] = i2s_spinlock_unlocked[0];
+        }
+        //To make sure hardware is enabled before any hardware register operations.
+        periph_module_enable(i2s_periph_signal[i2s_num].module);
+        i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
+
+        p_i2s_obj[i2s_num]->i2s_num = i2s_num;
+        p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
+        p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
+        p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
+        p_i2s_obj[i2s_num]->mode = i2s_config->mode;
+
+        p_i2s_obj[i2s_num]->bits_per_sample = 0;
+        p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
+        p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
+
+#ifdef CONFIG_PM_ENABLE
+    if (i2s_config->use_apll) {
+        err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
+    } else {
+        err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
+    }
+    if (err != ESP_OK) {
+        free(p_i2s_obj[i2s_num]);
+        p_i2s_obj[i2s_num] = NULL;
+        ESP_LOGE(I2S_TAG, "I2S pm lock error");
+        return err;
+    }
+#endif //CONFIG_PM_ENABLE
+
+        //initial interrupt
+        err = i2s_isr_register(i2s_num, i2s_config->intr_alloc_flags, i2s_intr_handler_default, p_i2s_obj[i2s_num], &p_i2s_obj[i2s_num]->i2s_isr_handle);
+        if (err != ESP_OK) {
+#ifdef CONFIG_PM_ENABLE
+            if (p_i2s_obj[i2s_num]->pm_lock) {
+                esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
+            }
+#endif
+            free(p_i2s_obj[i2s_num]);
+            p_i2s_obj[i2s_num] = NULL;
+            ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
+            return err;
+        }
+        i2s_stop(i2s_num);
+        err = i2s_param_config(i2s_num, i2s_config);
+        if (err != ESP_OK) {
+            i2s_driver_uninstall(i2s_num);
+            ESP_LOGE(I2S_TAG, "I2S param configure error");
+            return err;
+        }
+
+        if (i2s_queue) {
+            p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
+            *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
+            ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
+        } else {
+            p_i2s_obj[i2s_num]->i2s_queue = NULL;
+        }
+        //set clock and start
+        return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
+    }
+
+    ESP_LOGW(I2S_TAG, "I2S driver already installed");
+    return ESP_OK;
+}
+
+esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    if (p_i2s_obj[i2s_num] == NULL) {
+        ESP_LOGI(I2S_TAG, "already uninstalled");
+        return ESP_OK;
+    }
+    i2s_stop(i2s_num);
+    esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
+
+    if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
+        i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
+        p_i2s_obj[i2s_num]->tx = NULL;
+    }
+    if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
+        i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
+        p_i2s_obj[i2s_num]->rx = NULL;
+    }
+
+    if (p_i2s_obj[i2s_num]->i2s_queue) {
+        vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
+        p_i2s_obj[i2s_num]->i2s_queue = NULL;
+    }
+
+    if(p_i2s_obj[i2s_num]->use_apll) {
+        // switch back to PLL clock source
+        i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
+        rtc_clk_apll_enable(0, 0, 0, 0, 0);
+    }
+#ifdef CONFIG_PM_ENABLE
+    if (p_i2s_obj[i2s_num]->pm_lock) {
+        esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
+    }
+#endif
+
+    free(p_i2s_obj[i2s_num]);
+    p_i2s_obj[i2s_num] = NULL;
+    periph_module_disable(i2s_periph_signal[i2s_num].module);
+
+    return ESP_OK;
+}
+
+esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait)
+{
+    char *data_ptr, *src_byte;
+    size_t bytes_can_write;
+    *bytes_written = 0;
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
+    xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
+#endif
+    src_byte = (char *)src;
+    while (size > 0) {
+        if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
+            if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
+                break;
+            }
+            p_i2s_obj[i2s_num]->tx->rw_pos = 0;
+        }
+        ESP_LOGD(I2S_TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
+        data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
+        data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
+        bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
+        if (bytes_can_write > size) {
+            bytes_can_write = size;
+        }
+        memcpy(data_ptr, src_byte, bytes_can_write);
+        size -= bytes_can_write;
+        src_byte += bytes_can_write;
+        p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
+        (*bytes_written) += bytes_can_write;
+    }
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
+#endif
+
+    xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
+    return ESP_OK;
+}
+
+#if SOC_I2S_SUPPORTS_ADC_DAC
+esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
+    I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
+
+    adc1_dma_mode_acquire();
+    _i2s_adc_mode_recover();
+    i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
+    i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
+    return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
+}
+
+esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
+{
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
+    I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
+
+    i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
+    adc1_lock_release();
+    return ESP_OK;
+}
+#endif
+
+esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait)
+{
+    char *data_ptr;
+    int bytes_can_write, tail;
+    int src_bytes, aim_bytes, zero_bytes;
+    *bytes_written = 0;
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((aim_bits * size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
+    if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
+        ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
+        return ESP_ERR_INVALID_ARG;
+    }
+    if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
+        ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
+        return ESP_ERR_INVALID_ARG;
+    }
+    if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
+        ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
+        return ESP_ERR_INVALID_ARG;
+    }
+    if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
+        ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
+        return ESP_ERR_INVALID_ARG;
+    }
+
+    src_bytes = src_bits / 8;
+    aim_bytes = aim_bits / 8;
+    zero_bytes = aim_bytes - src_bytes;
+    xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
+    size = size * aim_bytes / src_bytes;
+    ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
+    while (size > 0) {
+        if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
+            if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
+                break;
+            }
+            p_i2s_obj[i2s_num]->tx->rw_pos = 0;
+        }
+        data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
+        data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
+        bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
+        if (bytes_can_write > (int)size) {
+            bytes_can_write = size;
+        }
+        tail = bytes_can_write % aim_bytes;
+        bytes_can_write = bytes_can_write - tail;
+
+        memset(data_ptr, 0, bytes_can_write);
+        for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
+            j += zero_bytes;
+            memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
+            (*bytes_written) += (aim_bytes - zero_bytes);
+        }
+        size -= bytes_can_write;
+        p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
+    }
+    xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
+    return ESP_OK;
+}
+
+esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
+{
+    char *data_ptr, *dest_byte;
+    int bytes_can_read;
+    *bytes_read = 0;
+    dest_byte = (char *)dest;
+    I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
+    I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
+    xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
+#endif
+    while (size > 0) {
+        if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
+            if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
+                break;
+            }
+            p_i2s_obj[i2s_num]->rx->rw_pos = 0;
+        }
+        data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
+        data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
+        bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
+        if (bytes_can_read > (int)size) {
+            bytes_can_read = size;
+        }
+        memcpy(dest_byte, data_ptr, bytes_can_read);
+        size -= bytes_can_read;
+        dest_byte += bytes_can_read;
+        p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
+        (*bytes_read) += bytes_can_read;
+    }
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
+#endif
+    xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
+    return ESP_OK;
+}

+ 275 - 0
components/_override/i2s_hal.c

@@ -0,0 +1,275 @@
+// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
+//
+// 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.
+
+// The HAL layer for I2S (common part)
+
+#include "soc/soc.h"
+#include "soc/soc_caps.h"
+#include "hal/i2s_hal.h"
+
+#define I2S_TX_PDM_FP_DEF  960   // Set to the recommended value(960) in TRM
+#define I2S_RX_PDM_DSR_DEF 0
+
+void i2s_hal_set_tx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits)
+{
+    if (bits <= I2S_BITS_PER_SAMPLE_16BIT) {
+        i2s_ll_set_tx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+    } else {
+        i2s_ll_set_tx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 2 : 3);
+    }
+    i2s_ll_set_tx_chan_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+#if SOC_I2S_SUPPORTS_DMA_EQUAL
+    i2s_ll_set_tx_dma_equal(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+#endif
+}
+
+void i2s_hal_set_rx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits)
+{
+    if (bits <= I2S_BITS_PER_SAMPLE_16BIT) {
+        i2s_ll_set_rx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+    } else {
+        i2s_ll_set_rx_fifo_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 2 : 3);
+    }
+    i2s_ll_set_rx_chan_mod(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+#if SOC_I2S_SUPPORTS_DMA_EQUAL
+    i2s_ll_set_rx_dma_equal(hal->dev, (ch == I2S_CHANNEL_STEREO) ? 0 : 1);
+#endif
+}
+
+void i2s_hal_set_in_link(i2s_hal_context_t *hal, uint32_t bytes_num, uint32_t addr)
+{
+    i2s_ll_set_in_link_addr(hal->dev, addr);
+    i2s_ll_set_rx_eof_num(hal->dev, bytes_num);
+}
+
+#if SOC_I2S_SUPPORTS_PDM
+void i2s_hal_tx_pdm_cfg(i2s_hal_context_t *hal, uint32_t fp, uint32_t fs)
+{
+    i2s_ll_tx_pdm_cfg(hal->dev, fp, fs);
+}
+
+void i2s_hal_get_tx_pdm(i2s_hal_context_t *hal, uint32_t *fp, uint32_t *fs)
+{
+    i2s_ll_get_tx_pdm(hal->dev, fp, fs);
+}
+
+void i2s_hal_rx_pdm_cfg(i2s_hal_context_t *hal, uint32_t dsr)
+{
+    i2s_ll_rx_pdm_cfg(hal->dev, dsr);
+}
+
+void i2s_hal_get_rx_pdm(i2s_hal_context_t *hal, uint32_t *dsr)
+{
+    i2s_ll_get_rx_pdm(hal->dev, dsr);
+}
+#endif
+
+void i2s_hal_set_clk_div(i2s_hal_context_t *hal, int div_num, int div_a, int div_b, int tx_bck_div, int rx_bck_div)
+{
+    i2s_ll_set_clkm_div_num(hal->dev, div_num);
+    i2s_ll_set_clkm_div_a(hal->dev, div_a);
+    i2s_ll_set_clkm_div_b(hal->dev, div_b);
+    i2s_ll_set_tx_bck_div_num(hal->dev, tx_bck_div);
+    i2s_ll_set_rx_bck_div_num(hal->dev, rx_bck_div);
+}
+
+void i2s_hal_set_tx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits)
+{
+    i2s_ll_set_tx_bits_mod(hal->dev, bits);
+}
+
+void i2s_hal_set_rx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits)
+{
+    i2s_ll_set_rx_bits_mod(hal->dev, bits);
+}
+
+void i2s_hal_reset(i2s_hal_context_t *hal)
+{
+    // Reset I2S TX/RX module first, and then, reset DMA and FIFO.
+    i2s_ll_reset_tx(hal->dev);
+    i2s_ll_reset_rx(hal->dev);
+    i2s_ll_reset_dma_in(hal->dev);
+    i2s_ll_reset_dma_out(hal->dev);
+    i2s_ll_reset_rx_fifo(hal->dev);
+    i2s_ll_reset_tx_fifo(hal->dev);
+}
+
+void i2s_hal_start_tx(i2s_hal_context_t *hal)
+{
+    i2s_ll_start_out_link(hal->dev);
+    i2s_ll_start_tx(hal->dev);
+}
+
+void i2s_hal_start_rx(i2s_hal_context_t *hal)
+{
+    i2s_ll_start_in_link(hal->dev);
+    i2s_ll_start_rx(hal->dev);
+}
+
+void i2s_hal_stop_tx(i2s_hal_context_t *hal)
+{
+    i2s_ll_stop_out_link(hal->dev);
+    i2s_ll_stop_tx(hal->dev);
+}
+
+void i2s_hal_stop_rx(i2s_hal_context_t *hal)
+{
+    i2s_ll_stop_in_link(hal->dev);
+    i2s_ll_stop_rx(hal->dev);
+}
+
+void i2s_hal_format_config(i2s_hal_context_t *hal, const i2s_config_t *i2s_config)
+{
+    switch (i2s_config->communication_format) {
+        case I2S_COMM_FORMAT_STAND_MSB:
+            if (i2s_config->mode & I2S_MODE_TX) {
+                i2s_ll_set_tx_format_msb_align(hal->dev);
+            }
+            if (i2s_config->mode & I2S_MODE_RX) {
+                i2s_ll_set_rx_format_msb_align(hal->dev);
+            }
+            break;
+        case I2S_COMM_FORMAT_STAND_PCM_SHORT:
+            if (i2s_config->mode & I2S_MODE_TX) {
+                i2s_ll_set_tx_pcm_long(hal->dev);
+            }
+            if (i2s_config->mode & I2S_MODE_RX) {
+                i2s_ll_set_rx_pcm_long(hal->dev);
+            }
+            break;
+        case I2S_COMM_FORMAT_STAND_PCM_LONG:
+            if (i2s_config->mode & I2S_MODE_TX) {
+                i2s_ll_set_tx_pcm_short(hal->dev);
+            }
+            if (i2s_config->mode & I2S_MODE_RX) {
+                i2s_ll_set_rx_pcm_short(hal->dev);
+            }
+            break;
+        default: //I2S_COMM_FORMAT_STAND_I2S
+            if (i2s_config->mode & I2S_MODE_TX) {
+                i2s_ll_set_tx_format_philip(hal->dev);
+            }
+            if (i2s_config->mode & I2S_MODE_RX) {
+                i2s_ll_set_rx_format_philip(hal->dev);
+            }
+            break;
+    }
+}
+#include "stdio.h"
+void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_config_t *i2s_config)
+{
+    //reset i2s
+    i2s_ll_reset_tx(hal->dev);
+    i2s_ll_reset_rx(hal->dev);
+
+    //reset dma
+    i2s_ll_reset_dma_in(hal->dev);
+    i2s_ll_reset_dma_out(hal->dev);
+
+    i2s_ll_enable_dma(hal->dev);
+
+    i2s_ll_set_lcd_en(hal->dev, 0);
+    i2s_ll_set_camera_en(hal->dev, 0);
+
+    i2s_ll_set_dscr_en(hal->dev, 0);
+
+    i2s_ll_set_tx_chan_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1)); // 0-two channel;1-right;2-left;3-righ;4-left
+    i2s_ll_set_tx_fifo_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1); // 0-right&left channel;1-one channel
+    i2s_ll_set_tx_mono(hal->dev, 0);
+
+    i2s_ll_set_rx_chan_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? i2s_config->channel_format : (i2s_config->channel_format >> 1)); // 0-two channel;1-right;2-left;3-righ;4-left
+    i2s_ll_set_rx_fifo_mod(hal->dev, i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 0 : 1); // 0-right&left channel;1-one channel
+    i2s_ll_set_rx_mono(hal->dev, 0);
+
+    i2s_ll_set_dscr_en(hal->dev, 1); //connect dma to fifo
+
+    i2s_ll_stop_tx(hal->dev);
+    i2s_ll_stop_rx(hal->dev);
+
+    if (i2s_config->mode & I2S_MODE_TX) {
+		int order = i2s_config->bits_per_sample == 32 ? 0 : 1;
+        i2s_ll_set_tx_msb_right(hal->dev, order);
+        i2s_ll_set_tx_right_first(hal->dev, ~order);
+
+        i2s_ll_set_tx_slave_mod(hal->dev, 0); // Master
+        i2s_ll_set_tx_fifo_mod_force_en(hal->dev, 1);
+
+        if (i2s_config->mode & I2S_MODE_SLAVE) {
+            i2s_ll_set_tx_slave_mod(hal->dev, 1); //TX Slave
+        }
+    }
+
+    if (i2s_config->mode & I2S_MODE_RX) {
+        i2s_ll_set_rx_msb_right(hal->dev, 0);
+        i2s_ll_set_rx_right_first(hal->dev, 0);
+        i2s_ll_set_rx_slave_mod(hal->dev, 0); // Master
+        i2s_ll_set_rx_fifo_mod_force_en(hal->dev, 1);
+
+        if (i2s_config->mode & I2S_MODE_SLAVE) {
+            i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
+        }
+    }
+
+#if SOC_I2S_SUPPORTS_PDM
+    if (!(i2s_config->mode & I2S_MODE_PDM)) {
+        i2s_ll_set_rx_pdm_en(hal->dev, 0);
+        i2s_ll_set_tx_pdm_en(hal->dev, 0);
+    } else {
+        if (i2s_config->mode & I2S_MODE_TX) {
+            i2s_ll_tx_pdm_cfg(hal->dev, I2S_TX_PDM_FP_DEF, i2s_config->sample_rate/100);
+        }
+        if(i2s_config->mode & I2S_MODE_RX) {
+            i2s_ll_rx_pdm_cfg(hal->dev, I2S_RX_PDM_DSR_DEF);
+        }
+        // PDM mode have nothing to do with communication format configuration.
+        return;
+    }
+#endif
+
+#if SOC_I2S_SUPPORTS_ADC_DAC
+    if (i2s_config->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
+        if (i2s_config->mode & I2S_MODE_DAC_BUILT_IN) {
+            i2s_ll_build_in_dac_ena(hal->dev);
+        }
+        if (i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
+            i2s_ll_build_in_adc_ena(hal->dev);
+            i2s_ll_set_rx_chan_mod(hal->dev, 1);
+            i2s_ll_set_rx_fifo_mod(hal->dev, 1);
+            i2s_ll_set_rx_mono(hal->dev, 0);
+        }
+        // Buildin ADC and DAC have nothing to do with communication format configuration.
+        return;
+    }
+#endif
+
+    i2s_hal_format_config(hal, i2s_config);
+}
+
+void i2s_hal_enable_master_mode(i2s_hal_context_t *hal)
+{
+    i2s_ll_set_tx_slave_mod(hal->dev, 0); //MASTER Slave
+    i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
+}
+
+void i2s_hal_enable_slave_mode(i2s_hal_context_t *hal)
+{
+    i2s_ll_set_tx_slave_mod(hal->dev, 1); //TX Slave
+    i2s_ll_set_rx_slave_mod(hal->dev, 1); //RX Slave
+}
+
+void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num)
+{
+    //Get hardware instance.
+    hal->dev = I2S_LL_GET_HW(i2s_num);
+}

+ 998 - 0
components/_override/spi_master.c

@@ -0,0 +1,998 @@
+// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
+//
+// 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.
+
+/*
+Architecture:
+
+We can initialize a SPI driver, but we don't talk to the SPI driver itself, we address a device. A device essentially
+is a combination of SPI port and CS pin, plus some information about the specifics of communication to the device
+(timing, command/address length etc). The arbitration between tasks is also in conception of devices.
+
+A device can work in interrupt mode and polling mode, and a third but
+complicated mode which combines the two modes above:
+
+1. Work in the ISR with a set of queues; one per device.
+
+   The idea is that to send something to a SPI device, you allocate a
+   transaction descriptor. It contains some information about the transfer
+   like the lenghth, address, command etc, plus pointers to transmit and
+   receive buffer. The address of this block gets pushed into the transmit
+   queue. The SPI driver does its magic, and sends and retrieves the data
+   eventually. The data gets written to the receive buffers, if needed the
+   transaction descriptor is modified to indicate returned parameters and
+   the entire thing goes into the return queue, where whatever software
+   initiated the transaction can retrieve it.
+
+   The entire thing is run from the SPI interrupt handler. If SPI is done
+   transmitting/receiving but nothing is in the queue, it will not clear the
+   SPI interrupt but just disable it by esp_intr_disable. This way, when a
+   new thing is sent, pushing the packet into the send queue and re-enabling
+   the interrupt (by esp_intr_enable) will trigger the interrupt again, which
+   can then take care of the sending.
+
+2. Work in the polling mode in the task.
+
+   In this mode we get rid of the ISR, FreeRTOS queue and task switching, the
+   task is no longer blocked during a transaction. This increase the cpu
+   load, but decrease the interval of SPI transactions. Each time only one
+   device (in one task) can send polling transactions, transactions to
+   other devices are blocked until the polling transaction of current device
+   is done.
+
+   In the polling mode, the queue is not used, all the operations are done
+   in the task. The task calls ``spi_device_polling_start`` to setup and start
+   a new transaction, then call ``spi_device_polling_end`` to handle the
+   return value of the transaction.
+
+   To handle the arbitration among devices, the device "temporarily" acquire
+   a bus by the ``device_acquire_bus_internal`` function, which writes
+   dev_request by CAS operation. Other devices which wants to send polling
+   transactions but don't own the bus will block and wait until given the
+   semaphore which indicates the ownership of bus.
+
+   In case of the ISR is still sending transactions to other devices, the ISR
+   should maintain an ``random_idle`` flag indicating that it's not doing
+   transactions. When the bus is locked, the ISR can only send new
+   transactions to the acquiring device. The ISR will automatically disable
+   itself and send semaphore to the device if the ISR is free. If the device
+   sees the random_idle flag, it can directly start its polling transaction.
+   Otherwise it should block and wait for the semaphore from the ISR.
+
+   After the polling transaction, the driver will release the bus. During the
+   release of the bus, the driver search all other devices to see whether
+   there is any device waiting to acquire the bus, if so, acquire for it and
+   send it a semaphore if the device queue is empty, or invoke the ISR for
+   it. If all other devices don't need to acquire the bus, but there are
+   still transactions in the queues, the ISR will also be invoked.
+
+   To get better polling efficiency, user can call ``spi_device_acquire_bus``
+   function, which also calls the ``spi_bus_lock_acquire_core`` function,
+   before a series of polling transactions to a device. The bus acquiring and
+   task switching before and after the polling transaction will be escaped.
+
+3. Mixed mode
+
+   The driver is written under the assumption that polling and interrupt
+   transactions are not happening simultaneously. When sending polling
+   transactions, it will check whether the ISR is active, which includes the
+   case the ISR is sending the interrupt transactions of the acquiring
+   device. If the ISR is still working, the routine sending a polling
+   transaction will get blocked and wait until the semaphore from the ISR
+   which indicates the ISR is free now.
+
+   A fatal case is, a polling transaction is in flight, but the ISR received
+   an interrupt transaction. The behavior of the driver is unpredictable,
+   which should be strictly forbidden.
+
+We have two bits to control the interrupt:
+
+1. The slave->trans_done bit, which is automatically asserted when a transaction is done.
+
+   This bit is cleared during an interrupt transaction, so that the interrupt
+   will be triggered when the transaction is done, or the SW can check the
+   bit to see if the transaction is done for polling transactions.
+
+   When no transaction is in-flight, the bit is kept active, so that the SW
+   can easily invoke the ISR by enable the interrupt.
+
+2. The system interrupt enable/disable, controlled by esp_intr_enable and esp_intr_disable.
+
+   The interrupt is disabled (by the ISR itself) when no interrupt transaction
+   is queued. When the bus is not occupied, any task, which queues a
+   transaction into the queue, will enable the interrupt to invoke the ISR.
+   When the bus is occupied by a device, other device will put off the
+   invoking of ISR to the moment when the bus is released. The device
+   acquiring the bus can still send interrupt transactions by enable the
+   interrupt.
+
+*/
+
+#include <string.h>
+#include "driver/spi_common_internal.h"
+#include "driver/spi_master.h"
+
+#include "esp_log.h"
+#include "freertos/task.h"
+#include "freertos/queue.h"
+#include "soc/soc_memory_layout.h"
+#include "driver/gpio.h"
+#include "hal/spi_hal.h"
+#include "esp_heap_caps.h"
+
+
+typedef struct spi_device_t spi_device_t;
+
+/// struct to hold private transaction data (like tx and rx buffer for DMA).
+typedef struct {
+    spi_transaction_t   *trans;
+    const uint32_t *buffer_to_send;   //equals to tx_data, if SPI_TRANS_USE_RXDATA is applied; otherwise if original buffer wasn't in DMA-capable memory, this gets the address of a temporary buffer that is;
+                                //otherwise sets to the original buffer or NULL if no buffer is assigned.
+    uint32_t *buffer_to_rcv;    // similar to buffer_to_send
+} spi_trans_priv_t;
+
+typedef struct {
+    int id;
+    spi_device_t* device[DEV_NUM_MAX];
+    intr_handle_t intr;
+    spi_hal_context_t hal;
+    spi_trans_priv_t cur_trans_buf;
+    int cur_cs;     //current device doing transaction
+    const spi_bus_attr_t* bus_attr;
+
+    /**
+     * the bus is permanently controlled by a device until `spi_bus_release_bus`` is called. Otherwise
+     * the acquiring of SPI bus will be freed when `spi_device_polling_end` is called.
+     */
+    spi_device_t* device_acquiring_lock;
+
+//debug information
+    bool polling;   //in process of a polling, avoid of queue new transactions into ISR
+} spi_host_t;
+
+struct spi_device_t {
+    int id;
+    QueueHandle_t trans_queue;
+    QueueHandle_t ret_queue;
+    spi_device_interface_config_t cfg;
+    spi_hal_dev_config_t hal_dev;
+    spi_host_t *host;
+    spi_bus_lock_dev_handle_t dev_lock;
+};
+
+static spi_host_t* bus_driver_ctx[SOC_SPI_PERIPH_NUM] = {};
+
+static const char *SPI_TAG = "spi_master";
+#define SPI_CHECK(a, str, ret_val, ...) \
+    if (unlikely(!(a))) { \
+        ESP_LOGE(SPI_TAG,"%s(%d): "str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
+        return (ret_val); \
+    }
+
+
+static void spi_intr(void *arg);
+static void spi_bus_intr_enable(void *host);
+static void spi_bus_intr_disable(void *host);
+
+static esp_err_t spi_master_deinit_driver(void* arg);
+
+static inline bool is_valid_host(spi_host_device_t host)
+{
+//SPI1 can be used as GPSPI only on ESP32
+#if CONFIG_IDF_TARGET_ESP32
+    return host >= SPI1_HOST && host <= SPI3_HOST;
+#elif (SOC_SPI_PERIPH_NUM == 2)
+    return host == SPI2_HOST;
+#elif (SOC_SPI_PERIPH_NUM == 3)
+    return host >= SPI2_HOST && host <= SPI3_HOST;
+#endif
+}
+
+// Should be called before any devices are actually registered or used.
+// Currently automatically called after `spi_bus_initialize()` and when first device is registered.
+static esp_err_t spi_master_init_driver(spi_host_device_t host_id)
+{
+    esp_err_t err = ESP_OK;
+
+    const spi_bus_attr_t* bus_attr = spi_bus_get_attr(host_id);
+    SPI_CHECK(bus_attr != NULL, "host_id not initialized", ESP_ERR_INVALID_STATE);
+    SPI_CHECK(bus_attr->lock != NULL, "SPI Master cannot attach to bus. (Check CONFIG_SPI_FLASH_SHARE_SPI1_BUS)", ESP_ERR_INVALID_ARG);
+    // spihost contains atomic variables, which should not be put in PSRAM
+    spi_host_t* host = heap_caps_malloc(sizeof(spi_host_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
+    if (host == NULL) {
+        err = ESP_ERR_NO_MEM;
+        goto cleanup;
+    }
+
+    *host = (spi_host_t) {
+        .id = host_id,
+        .cur_cs = DEV_NUM_MAX,
+        .polling = false,
+        .device_acquiring_lock = NULL,
+        .bus_attr = bus_attr,
+    };
+
+    if (host_id != SPI1_HOST) {
+        // interrupts are not allowed on SPI1 bus
+        err = esp_intr_alloc(spicommon_irqsource_for_host(host_id),
+                            bus_attr->bus_cfg.intr_flags | ESP_INTR_FLAG_INTRDISABLED,
+                            spi_intr, host, &host->intr);
+        if (err != ESP_OK) {
+            goto cleanup;
+        }
+    }
+
+    //assign the SPI, RX DMA and TX DMA peripheral registers beginning address
+    spi_hal_config_t hal_config = {
+        //On ESP32-S2 and earlier chips, DMA registers are part of SPI registers. Pass the registers of SPI peripheral to control it.
+        .dma_in = SPI_LL_GET_HW(host_id),
+        .dma_out = SPI_LL_GET_HW(host_id),
+        .dma_enabled = bus_attr->dma_enabled,
+        .dmadesc_tx = bus_attr->dmadesc_tx,
+        .dmadesc_rx = bus_attr->dmadesc_rx,
+        .tx_dma_chan = bus_attr->tx_dma_chan,
+        .rx_dma_chan = bus_attr->rx_dma_chan,
+        .dmadesc_n = bus_attr->dma_desc_num,
+    };
+    spi_hal_init(&host->hal, host_id, &hal_config);
+
+    if (host_id != SPI1_HOST) {
+        //SPI1 attributes are already initialized at start up.
+        spi_bus_lock_handle_t lock = spi_bus_lock_get_by_id(host_id);
+        spi_bus_lock_set_bg_control(lock, spi_bus_intr_enable, spi_bus_intr_disable, host);
+        spi_bus_register_destroy_func(host_id, spi_master_deinit_driver, host);
+    }
+
+    bus_driver_ctx[host_id] = host;
+    return ESP_OK;
+
+cleanup:
+    if (host) {
+        spi_hal_deinit(&host->hal);
+        if (host->intr) {
+            esp_intr_free(host->intr);
+        }
+    }
+    free(host);
+    return err;
+}
+
+static esp_err_t spi_master_deinit_driver(void* arg)
+{
+    spi_host_t *host = (spi_host_t*)arg;
+    SPI_CHECK(host != NULL, "host_id not in use", ESP_ERR_INVALID_STATE);
+
+    int host_id = host->id;
+    SPI_CHECK(is_valid_host(host_id), "invalid host_id", ESP_ERR_INVALID_ARG);
+
+    int x;
+    for (x=0; x<DEV_NUM_MAX; x++) {
+        SPI_CHECK(host->device[x] == NULL, "not all CSses freed", ESP_ERR_INVALID_STATE);
+    }
+
+    spi_hal_deinit(&host->hal);
+
+    if (host->intr) {
+        esp_intr_free(host->intr);
+    }
+    free(host);
+    bus_driver_ctx[host_id] = NULL;
+    return ESP_OK;
+}
+
+void spi_get_timing(bool gpio_is_used, int input_delay_ns, int eff_clk, int* dummy_o, int* cycles_remain_o)
+{
+    int timing_dummy;
+    int timing_miso_delay;
+
+    spi_hal_cal_timing(eff_clk, gpio_is_used, input_delay_ns, &timing_dummy, &timing_miso_delay);
+    if (dummy_o) *dummy_o = timing_dummy;
+    if (cycles_remain_o) *cycles_remain_o = timing_miso_delay;
+}
+
+int spi_get_freq_limit(bool gpio_is_used, int input_delay_ns)
+{
+    return spi_hal_get_freq_limit(gpio_is_used, input_delay_ns);
+}
+
+/*
+ Add a device. This allocates a CS line for the device, allocates memory for the device structure and hooks
+ up the CS pin to whatever is specified.
+*/
+esp_err_t spi_bus_add_device(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle)
+{
+    spi_device_t *dev = NULL;
+    esp_err_t err = ESP_OK;
+
+    SPI_CHECK(is_valid_host(host_id), "invalid host", ESP_ERR_INVALID_ARG);
+    if (bus_driver_ctx[host_id] == NULL) {
+        //lazy initialization the driver, get deinitialized by the bus is freed
+        err = spi_master_init_driver(host_id);
+        if (err != ESP_OK) {
+            return err;
+        }
+    }
+
+    spi_host_t *host = bus_driver_ctx[host_id];
+    const spi_bus_attr_t* bus_attr = host->bus_attr;
+    SPI_CHECK(dev_config->spics_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(dev_config->spics_io_num), "spics pin invalid", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(dev_config->clock_speed_hz > 0, "invalid sclk speed", ESP_ERR_INVALID_ARG);
+#ifdef CONFIG_IDF_TARGET_ESP32
+    //The hardware looks like it would support this, but actually setting cs_ena_pretrans when transferring in full
+    //duplex mode does absolutely nothing on the ESP32.
+    SPI_CHECK(dev_config->cs_ena_pretrans <= 1 || (dev_config->address_bits == 0 && dev_config->command_bits == 0) ||
+        (dev_config->flags & SPI_DEVICE_HALFDUPLEX), "In full-duplex mode, only support cs pretrans delay = 1 and without address_bits and command_bits", ESP_ERR_INVALID_ARG);
+#endif
+    uint32_t lock_flag = ((dev_config->spics_io_num != -1)? SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED: 0);
+
+    spi_bus_lock_dev_config_t lock_config = {
+        .flags = lock_flag,
+    };
+    spi_bus_lock_dev_handle_t dev_handle;
+    err = spi_bus_lock_register_dev(bus_attr->lock, &lock_config, &dev_handle);
+    if (err != ESP_OK) {
+        goto nomem;
+    }
+
+    int freecs = spi_bus_lock_get_dev_id(dev_handle);
+    SPI_CHECK(freecs != -1, "no free cs pins for the host", ESP_ERR_NOT_FOUND);
+
+    //input parameters to calculate timing configuration
+    int half_duplex = dev_config->flags & SPI_DEVICE_HALFDUPLEX ? 1 : 0;
+    int no_compensate = dev_config->flags & SPI_DEVICE_NO_DUMMY ? 1 : 0;
+    int duty_cycle = (dev_config->duty_cycle_pos==0) ? 128 : dev_config->duty_cycle_pos;
+    int use_gpio = !(bus_attr->flags & SPICOMMON_BUSFLAG_IOMUX_PINS);
+    spi_hal_timing_param_t timing_param = {
+        .half_duplex = half_duplex,
+        .no_compensate = no_compensate,
+        .clock_speed_hz = dev_config->clock_speed_hz,
+        .duty_cycle = duty_cycle,
+        .input_delay_ns = dev_config->input_delay_ns,
+        .use_gpio = use_gpio
+    };
+
+    //output values of timing configuration
+    spi_hal_timing_conf_t temp_timing_conf;
+    int freq;
+    esp_err_t ret = spi_hal_cal_clock_conf(&timing_param, &freq, &temp_timing_conf);
+    SPI_CHECK(ret==ESP_OK, "assigned clock speed not supported", ret);
+
+    //Allocate memory for device
+    dev = malloc(sizeof(spi_device_t));
+    if (dev == NULL) goto nomem;
+    memset(dev, 0, sizeof(spi_device_t));
+
+    dev->id = freecs;
+    dev->dev_lock = dev_handle;
+
+    //Allocate queues, set defaults
+    dev->trans_queue = xQueueCreate(dev_config->queue_size, sizeof(spi_trans_priv_t));
+    dev->ret_queue = xQueueCreate(dev_config->queue_size, sizeof(spi_trans_priv_t));
+    if (!dev->trans_queue || !dev->ret_queue) {
+        goto nomem;
+    }
+
+    //We want to save a copy of the dev config in the dev struct.
+    memcpy(&dev->cfg, dev_config, sizeof(spi_device_interface_config_t));
+    dev->cfg.duty_cycle_pos = duty_cycle;
+    // TODO: if we have to change the apb clock among transactions, re-calculate this each time the apb clock lock is locked.
+
+    //Set CS pin, CS options
+    if (dev_config->spics_io_num >= 0) {
+        spicommon_cs_initialize(host_id, dev_config->spics_io_num, freecs, use_gpio);
+    }
+
+    //save a pointer to device in spi_host_t
+    host->device[freecs] = dev;
+    //save a pointer to host in spi_device_t
+    dev->host= host;
+
+    //initialise the device specific configuration
+    spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
+    hal_dev->mode = dev_config->mode;
+    hal_dev->cs_setup = dev_config->cs_ena_pretrans;
+    hal_dev->cs_hold = dev_config->cs_ena_posttrans;
+    //set hold_time to 0 will not actually append delay to CS
+    //set it to 1 since we do need at least one clock of hold time in most cases
+    if (hal_dev->cs_hold == 0) {
+        hal_dev->cs_hold = 1;
+    }
+    hal_dev->cs_pin_id = dev->id;
+    hal_dev->timing_conf = temp_timing_conf;
+    hal_dev->sio = (dev_config->flags) & SPI_DEVICE_3WIRE ? 1 : 0;
+    hal_dev->half_duplex = dev_config->flags & SPI_DEVICE_HALFDUPLEX ? 1 : 0;
+    hal_dev->tx_lsbfirst = dev_config->flags & SPI_DEVICE_TXBIT_LSBFIRST ? 1 : 0;
+    hal_dev->rx_lsbfirst = dev_config->flags & SPI_DEVICE_RXBIT_LSBFIRST ? 1 : 0;
+    hal_dev->no_compensate = dev_config->flags & SPI_DEVICE_NO_DUMMY ? 1 : 0;
+#if SOC_SPI_SUPPORT_AS_CS
+    hal_dev->as_cs = dev_config->flags& SPI_DEVICE_CLK_AS_CS ? 1 : 0;
+#endif
+    hal_dev->positive_cs = dev_config->flags & SPI_DEVICE_POSITIVE_CS ? 1 : 0;
+
+    *handle = dev;
+    ESP_LOGD(SPI_TAG, "SPI%d: New device added to CS%d, effective clock: %dkHz", host_id+1, freecs, freq/1000);
+
+    return ESP_OK;
+
+nomem:
+    if (dev) {
+        if (dev->trans_queue) vQueueDelete(dev->trans_queue);
+        if (dev->ret_queue) vQueueDelete(dev->ret_queue);
+        spi_bus_lock_unregister_dev(dev->dev_lock);
+    }
+    free(dev);
+    return ESP_ERR_NO_MEM;
+}
+
+esp_err_t spi_bus_remove_device(spi_device_handle_t handle)
+{
+    SPI_CHECK(handle!=NULL, "invalid handle", ESP_ERR_INVALID_ARG);
+    //These checks aren't exhaustive; another thread could sneak in a transaction inbetween. These are only here to
+    //catch design errors and aren't meant to be triggered during normal operation.
+    SPI_CHECK(uxQueueMessagesWaiting(handle->trans_queue)==0, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
+    SPI_CHECK(handle->host->cur_cs == DEV_NUM_MAX || handle->host->device[handle->host->cur_cs] != handle, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
+    SPI_CHECK(uxQueueMessagesWaiting(handle->ret_queue)==0, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
+
+    //return
+    int spics_io_num = handle->cfg.spics_io_num;
+    if (spics_io_num >= 0) spicommon_cs_free_io(spics_io_num);
+
+    //Kill queues
+    vQueueDelete(handle->trans_queue);
+    vQueueDelete(handle->ret_queue);
+    spi_bus_lock_unregister_dev(handle->dev_lock);
+
+    assert(handle->host->device[handle->id] == handle);
+    handle->host->device[handle->id] = NULL;
+    free(handle);
+    return ESP_OK;
+}
+
+int spi_cal_clock(int fapb, int hz, int duty_cycle, uint32_t *reg_o)
+{
+    return spi_ll_master_cal_clock(fapb, hz, duty_cycle, reg_o);
+}
+
+int spi_get_actual_clock(int fapb, int hz, int duty_cycle)
+{
+    return spi_hal_master_cal_clock(fapb, hz, duty_cycle);
+}
+
+// Setup the device-specified configuration registers. Called every time a new
+// transaction is to be sent, but only apply new configurations when the device
+// changes.
+static SPI_MASTER_ISR_ATTR void spi_setup_device(spi_device_t *dev)
+{
+    spi_bus_lock_dev_handle_t dev_lock = dev->dev_lock;
+
+    if (!spi_bus_lock_touch(dev_lock)) {
+        //if the configuration is already applied, skip the following.
+        return;
+    }
+    spi_hal_context_t *hal = &dev->host->hal;
+    spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
+    spi_hal_setup_device(hal, hal_dev);
+}
+
+static SPI_MASTER_ISR_ATTR spi_device_t *get_acquiring_dev(spi_host_t *host)
+{
+    spi_bus_lock_dev_handle_t dev_lock = spi_bus_lock_get_acquiring_dev(host->bus_attr->lock);
+    if (!dev_lock) return NULL;
+
+    return host->device[spi_bus_lock_get_dev_id(dev_lock)];
+}
+
+// Debug only
+// NOTE if the acquiring is not fully completed, `spi_bus_lock_get_acquiring_dev`
+// may return a false `NULL` cause the function returning false `false`.
+static inline SPI_MASTER_ISR_ATTR bool spi_bus_device_is_polling(spi_device_t *dev)
+{
+    return get_acquiring_dev(dev->host) == dev && dev->host->polling;
+}
+
+/*-----------------------------------------------------------------------------
+    Working Functions
+-----------------------------------------------------------------------------*/
+
+// The interrupt may get invoked by the bus lock.
+static void SPI_MASTER_ISR_ATTR spi_bus_intr_enable(void *host)
+{
+    esp_intr_enable(((spi_host_t*)host)->intr);
+}
+
+// The interrupt is always disabled by the ISR itself, not exposed
+static void SPI_MASTER_ISR_ATTR spi_bus_intr_disable(void *host)
+{
+    esp_intr_disable(((spi_host_t*)host)->intr);
+}
+
+// The function is called to send a new transaction, in ISR or in the task.
+// Setup the transaction-specified registers and linked-list used by the DMA (or FIFO if DMA is not used)
+static void SPI_MASTER_ISR_ATTR spi_new_trans(spi_device_t *dev, spi_trans_priv_t *trans_buf)
+{
+    spi_transaction_t *trans = NULL;
+    spi_host_t *host = dev->host;
+    spi_hal_context_t *hal = &(host->hal);
+    spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
+
+    trans = trans_buf->trans;
+    host->cur_cs = dev->id;
+
+    //Reconfigure according to device settings, the function only has effect when the dev_id is changed.
+    spi_setup_device(dev);
+
+    //set the transaction specific configuration each time before a transaction setup
+    spi_hal_trans_config_t hal_trans = {};
+    hal_trans.tx_bitlen = trans->length;
+    hal_trans.rx_bitlen = trans->rxlength;
+    hal_trans.rcv_buffer = (uint8_t*)host->cur_trans_buf.buffer_to_rcv;
+    hal_trans.send_buffer = (uint8_t*)host->cur_trans_buf.buffer_to_send;
+    hal_trans.cmd = trans->cmd;
+    hal_trans.addr = trans->addr;
+    //Set up QIO/DIO if needed
+    hal_trans.io_mode = (trans->flags & SPI_TRANS_MODE_DIO ?
+                        (trans->flags & SPI_TRANS_MODE_DIOQIO_ADDR ? SPI_LL_IO_MODE_DIO : SPI_LL_IO_MODE_DUAL) :
+                    (trans->flags & SPI_TRANS_MODE_QIO ?
+                        (trans->flags & SPI_TRANS_MODE_DIOQIO_ADDR ? SPI_LL_IO_MODE_QIO : SPI_LL_IO_MODE_QUAD) :
+                    SPI_LL_IO_MODE_NORMAL
+                    ));
+
+    if (trans->flags & SPI_TRANS_VARIABLE_CMD) {
+        hal_trans.cmd_bits = ((spi_transaction_ext_t *)trans)->command_bits;
+    } else {
+        hal_trans.cmd_bits = dev->cfg.command_bits;
+    }
+    if (trans->flags & SPI_TRANS_VARIABLE_ADDR) {
+        hal_trans.addr_bits = ((spi_transaction_ext_t *)trans)->address_bits;
+    } else {
+        hal_trans.addr_bits = dev->cfg.address_bits;
+    }
+    if (trans->flags & SPI_TRANS_VARIABLE_DUMMY) {
+        hal_trans.dummy_bits = ((spi_transaction_ext_t *)trans)->dummy_bits;
+    } else {
+        hal_trans.dummy_bits = dev->cfg.dummy_bits;
+    }
+
+    spi_hal_setup_trans(hal, hal_dev, &hal_trans);
+    spi_hal_prepare_data(hal, hal_dev, &hal_trans);
+
+    //Call pre-transmission callback, if any
+    if (dev->cfg.pre_cb) dev->cfg.pre_cb(trans);
+    //Kick off transfer
+    spi_hal_user_start(hal);
+}
+
+// The function is called when a transaction is done, in ISR or in the task.
+// Fetch the data from FIFO and call the ``post_cb``.
+static void SPI_MASTER_ISR_ATTR spi_post_trans(spi_host_t *host)
+{
+    spi_transaction_t *cur_trans = host->cur_trans_buf.trans;
+
+    spi_hal_fetch_result(&host->hal);
+    //Call post-transaction callback, if any
+    spi_device_t* dev = host->device[host->cur_cs];
+    if (dev->cfg.post_cb) dev->cfg.post_cb(cur_trans);
+
+    host->cur_cs = DEV_NUM_MAX;
+}
+
+// This is run in interrupt context.
+static void SPI_MASTER_ISR_ATTR spi_intr(void *arg)
+{
+    BaseType_t do_yield = pdFALSE;
+    spi_host_t *host = (spi_host_t *)arg;
+    const spi_bus_attr_t* bus_attr = host->bus_attr;
+
+    assert(spi_hal_usr_is_done(&host->hal));
+
+    /*
+     * Help to skip the handling of in-flight transaction, and disable of the interrupt.
+     * The esp_intr_enable will be called (b) after new BG request is queued (a) in the task;
+     * while esp_intr_disable should be called (c) if we check and found the sending queue is empty (d).
+     * If (c) is called after (d), then there is a risk that things happens in this sequence:
+     * (d) -> (a) -> (b) -> (c), and in this case the interrupt is disabled while there's pending BG request in the queue.
+     * To avoid this, interrupt is disabled here, and re-enabled later if required.
+     */
+    if (!spi_bus_lock_bg_entry(bus_attr->lock)) {
+        /*------------ deal with the in-flight transaction -----------------*/
+        assert(host->cur_cs != DEV_NUM_MAX);
+        //Okay, transaction is done.
+        const int cs = host->cur_cs;
+        //Tell common code DMA workaround that our DMA channel is idle. If needed, the code will do a DMA reset.
+        if (bus_attr->dma_enabled) {
+            //This workaround is only for esp32, where tx_dma_chan and rx_dma_chan are always same
+            spicommon_dmaworkaround_idle(bus_attr->tx_dma_chan);
+        }
+
+        //cur_cs is changed to DEV_NUM_MAX here
+        spi_post_trans(host);
+        // spi_bus_lock_bg_pause(bus_attr->lock);
+        //Return transaction descriptor.
+        xQueueSendFromISR(host->device[cs]->ret_queue, &host->cur_trans_buf, &do_yield);
+#ifdef CONFIG_PM_ENABLE
+        //Release APB frequency lock
+        esp_pm_lock_release(bus_attr->pm_lock);
+#endif
+    }
+
+    /*------------ new transaction starts here ------------------*/
+    assert(host->cur_cs == DEV_NUM_MAX);
+
+    spi_bus_lock_handle_t lock = host->bus_attr->lock;
+    BaseType_t trans_found = pdFALSE;
+
+
+    // There should be remaining requests
+    BUS_LOCK_DEBUG_EXECUTE_CHECK(spi_bus_lock_bg_req_exist(lock));
+
+    do {
+        spi_bus_lock_dev_handle_t acq_dev_lock = spi_bus_lock_get_acquiring_dev(lock);
+        spi_bus_lock_dev_handle_t desired_dev = acq_dev_lock;
+        bool resume_task = false;
+        spi_device_t* device_to_send = NULL;
+
+        if (!acq_dev_lock) {
+            // This function may assign a new acquiring device, otherwise it will suggest a desired device with BG active
+            // We use either of them without further searching in the devices.
+            // If the return value is true, it means either there's no acquiring device, or the acquiring device's BG is active,
+            // We stay in the ISR to deal with those transactions of desired device, otherwise nothing will be done, check whether we need to resume some other tasks, or just quit the ISR
+            resume_task = spi_bus_lock_bg_check_dev_acq(lock, &desired_dev);
+        }
+
+        if (!resume_task) {
+            bool dev_has_req = spi_bus_lock_bg_check_dev_req(desired_dev);
+            if (dev_has_req) {
+                device_to_send = host->device[spi_bus_lock_get_dev_id(desired_dev)];
+                trans_found = xQueueReceiveFromISR(device_to_send->trans_queue, &host->cur_trans_buf, &do_yield);
+                if (!trans_found) {
+                    spi_bus_lock_bg_clear_req(desired_dev);
+                }
+            }
+        }
+
+        if (trans_found) {
+            spi_trans_priv_t *const cur_trans_buf = &host->cur_trans_buf;
+            if (bus_attr->dma_enabled && (cur_trans_buf->buffer_to_rcv || cur_trans_buf->buffer_to_send)) {
+                //mark channel as active, so that the DMA will not be reset by the slave
+                //This workaround is only for esp32, where tx_dma_chan and rx_dma_chan are always same
+                spicommon_dmaworkaround_transfer_active(bus_attr->tx_dma_chan);
+            }
+            spi_new_trans(device_to_send, cur_trans_buf);
+        }
+        // Exit of the ISR, handle interrupt re-enable (if sending transaction), retry (if there's coming BG),
+        // or resume acquiring device task (if quit due to bus acquiring).
+    } while (!spi_bus_lock_bg_exit(lock, trans_found, &do_yield));
+
+    if (do_yield) portYIELD_FROM_ISR();
+}
+
+static SPI_MASTER_ISR_ATTR esp_err_t check_trans_valid(spi_device_handle_t handle, spi_transaction_t *trans_desc)
+{
+    SPI_CHECK(handle!=NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
+    spi_host_t *host = handle->host;
+    const spi_bus_attr_t* bus_attr = host->bus_attr;
+    bool tx_enabled = (trans_desc->flags & SPI_TRANS_USE_TXDATA) || (trans_desc->tx_buffer);
+    bool rx_enabled = (trans_desc->flags & SPI_TRANS_USE_RXDATA) || (trans_desc->rx_buffer);
+    spi_transaction_ext_t *t_ext = (spi_transaction_ext_t *)trans_desc;
+    bool dummy_enabled = (((trans_desc->flags & SPI_TRANS_VARIABLE_DUMMY)? t_ext->dummy_bits: handle->cfg.dummy_bits) != 0);
+    bool extra_dummy_enabled = handle->hal_dev.timing_conf.timing_dummy;
+    bool is_half_duplex = ((handle->cfg.flags & SPI_DEVICE_HALFDUPLEX) != 0);
+
+    //check transmission length
+    SPI_CHECK((trans_desc->flags & SPI_TRANS_USE_RXDATA)==0 || trans_desc->rxlength <= 32, "SPI_TRANS_USE_RXDATA only available for rxdata transfer <= 32 bits", ESP_ERR_INVALID_ARG);
+    SPI_CHECK((trans_desc->flags & SPI_TRANS_USE_TXDATA)==0 || trans_desc->length <= 32, "SPI_TRANS_USE_TXDATA only available for txdata transfer <= 32 bits", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(trans_desc->length <= bus_attr->max_transfer_sz*8, "txdata transfer > host maximum", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(trans_desc->rxlength <= bus_attr->max_transfer_sz*8, "rxdata transfer > host maximum", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(is_half_duplex || trans_desc->rxlength <= trans_desc->length, "rx length > tx length in full duplex mode", ESP_ERR_INVALID_ARG);
+    //check working mode
+    SPI_CHECK(!((trans_desc->flags & (SPI_TRANS_MODE_DIO|SPI_TRANS_MODE_QIO)) && (handle->cfg.flags & SPI_DEVICE_3WIRE)), "incompatible iface params", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(!((trans_desc->flags & (SPI_TRANS_MODE_DIO|SPI_TRANS_MODE_QIO)) && !is_half_duplex), "incompatible iface params", ESP_ERR_INVALID_ARG);
+#ifdef CONFIG_IDF_TARGET_ESP32
+    SPI_CHECK(!is_half_duplex || !bus_attr->dma_enabled || !rx_enabled || !tx_enabled, "SPI half duplex mode does not support using DMA with both MOSI and MISO phases.", ESP_ERR_INVALID_ARG );
+#elif CONFIG_IDF_TARGET_ESP32S3
+    SPI_CHECK(!is_half_duplex || !tx_enabled || !rx_enabled, "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", ESP_ERR_INVALID_ARG);
+#endif
+    //MOSI phase is skipped only when both tx_buffer and SPI_TRANS_USE_TXDATA are not set.
+    SPI_CHECK(trans_desc->length != 0 || !tx_enabled, "trans tx_buffer should be NULL and SPI_TRANS_USE_TXDATA should be cleared to skip MOSI phase.", ESP_ERR_INVALID_ARG);
+    //MISO phase is skipped only when both rx_buffer and SPI_TRANS_USE_RXDATA are not set.
+    //If set rxlength=0 in full_duplex mode, it will be automatically set to length
+    SPI_CHECK(!is_half_duplex || trans_desc->rxlength != 0 || !rx_enabled, "trans rx_buffer should be NULL and SPI_TRANS_USE_RXDATA should be cleared to skip MISO phase.", ESP_ERR_INVALID_ARG);
+    //In Full duplex mode, default rxlength to be the same as length, if not filled in.
+    // set rxlength to length is ok, even when rx buffer=NULL
+    if (trans_desc->rxlength==0 && !is_half_duplex) {
+        trans_desc->rxlength=trans_desc->length;
+    }
+    //Dummy phase is not available when both data out and in are enabled, regardless of FD or HD mode.
+    SPI_CHECK(!tx_enabled || !rx_enabled || !dummy_enabled || !extra_dummy_enabled, "Dummy phase is not available when both data out and in are enabled", ESP_ERR_INVALID_ARG);
+
+    return ESP_OK;
+}
+
+static SPI_MASTER_ISR_ATTR void uninstall_priv_desc(spi_trans_priv_t* trans_buf)
+{
+    spi_transaction_t *trans_desc = trans_buf->trans;
+    if ((void *)trans_buf->buffer_to_send != &trans_desc->tx_data[0] &&
+        trans_buf->buffer_to_send != trans_desc->tx_buffer) {
+        free((void *)trans_buf->buffer_to_send); //force free, ignore const
+    }
+    // copy data from temporary DMA-capable buffer back to IRAM buffer and free the temporary one.
+    if ((void *)trans_buf->buffer_to_rcv != &trans_desc->rx_data[0] &&
+        trans_buf->buffer_to_rcv != trans_desc->rx_buffer) { // NOLINT(clang-analyzer-unix.Malloc)
+        if (trans_desc->flags & SPI_TRANS_USE_RXDATA) {
+            memcpy((uint8_t *) & trans_desc->rx_data[0], trans_buf->buffer_to_rcv, (trans_desc->rxlength + 7) / 8);
+        } else {
+            memcpy(trans_desc->rx_buffer, trans_buf->buffer_to_rcv, (trans_desc->rxlength + 7) / 8);
+        }
+        free(trans_buf->buffer_to_rcv);
+    }
+}
+
+static SPI_MASTER_ISR_ATTR esp_err_t setup_priv_desc(spi_transaction_t *trans_desc, spi_trans_priv_t* new_desc, bool isdma)
+{
+    *new_desc = (spi_trans_priv_t) { .trans = trans_desc, };
+
+    // rx memory assign
+    uint32_t* rcv_ptr;
+    if ( trans_desc->flags & SPI_TRANS_USE_RXDATA ) {
+        rcv_ptr = (uint32_t *)&trans_desc->rx_data[0];
+    } else {
+        //if not use RXDATA neither rx_buffer, buffer_to_rcv assigned to NULL
+        rcv_ptr = trans_desc->rx_buffer;
+    }
+    if (rcv_ptr && isdma && (!esp_ptr_dma_capable(rcv_ptr) || ((int)rcv_ptr % 4 != 0))) {
+        //if rxbuf in the desc not DMA-capable, malloc a new one. The rx buffer need to be length of multiples of 32 bits to avoid heap corruption.
+        ESP_LOGD(SPI_TAG, "Allocate RX buffer for DMA" );
+        rcv_ptr = heap_caps_malloc((trans_desc->rxlength + 31) / 8, MALLOC_CAP_DMA);
+        if (rcv_ptr == NULL) goto clean_up;
+    }
+    new_desc->buffer_to_rcv = rcv_ptr;
+
+    // tx memory assign
+    const uint32_t *send_ptr;
+    if ( trans_desc->flags & SPI_TRANS_USE_TXDATA ) {
+        send_ptr = (uint32_t *)&trans_desc->tx_data[0];
+    } else {
+        //if not use TXDATA neither tx_buffer, tx data assigned to NULL
+        send_ptr = trans_desc->tx_buffer ;
+    }
+    if (send_ptr && isdma && !esp_ptr_dma_capable( send_ptr )) {
+        //if txbuf in the desc not DMA-capable, malloc a new one
+        ESP_LOGD(SPI_TAG, "Allocate TX buffer for DMA" );
+        uint32_t *temp = heap_caps_malloc((trans_desc->length + 7) / 8, MALLOC_CAP_DMA);
+        if (temp == NULL) goto clean_up;
+
+        memcpy( temp, send_ptr, (trans_desc->length + 7) / 8 );
+        send_ptr = temp;
+    }
+    new_desc->buffer_to_send = send_ptr;
+
+    return ESP_OK;
+
+clean_up:
+    uninstall_priv_desc(new_desc);
+    return ESP_ERR_NO_MEM;
+}
+
+esp_err_t SPI_MASTER_ATTR spi_device_queue_trans(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait)
+{
+    esp_err_t ret = check_trans_valid(handle, trans_desc);
+    if (ret != ESP_OK) return ret;
+
+    spi_host_t *host = handle->host;
+
+    SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot queue new transaction while previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE );
+
+    spi_trans_priv_t trans_buf;
+    ret = setup_priv_desc(trans_desc, &trans_buf, (host->bus_attr->dma_enabled));
+    if (ret != ESP_OK) return ret;
+
+#ifdef CONFIG_PM_ENABLE
+    esp_pm_lock_acquire(host->bus_attr->pm_lock);
+#endif
+    //Send to queue and invoke the ISR.
+
+    BaseType_t r = xQueueSend(handle->trans_queue, (void *)&trans_buf, ticks_to_wait);
+    if (!r) {
+        ret = ESP_ERR_TIMEOUT;
+#ifdef CONFIG_PM_ENABLE
+        //Release APB frequency lock
+        esp_pm_lock_release(host->bus_attr->pm_lock);
+#endif
+        goto clean_up;
+    }
+
+    // The ISR will be invoked at correct time by the lock with `spi_bus_intr_enable`.
+    ret = spi_bus_lock_bg_request(handle->dev_lock);
+    if (ret != ESP_OK) {
+        goto clean_up;
+    }
+    return ESP_OK;
+
+clean_up:
+    uninstall_priv_desc(&trans_buf);
+    return ret;
+}
+
+esp_err_t SPI_MASTER_ATTR spi_device_get_trans_result(spi_device_handle_t handle, spi_transaction_t **trans_desc, TickType_t ticks_to_wait)
+{
+    BaseType_t r;
+    spi_trans_priv_t trans_buf;
+    SPI_CHECK(handle!=NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
+
+    //use the interrupt, block until return
+    r=xQueueReceive(handle->ret_queue, (void*)&trans_buf, ticks_to_wait);
+    if (!r) {
+        // The memory occupied by rx and tx DMA buffer destroyed only when receiving from the queue (transaction finished).
+        // If timeout, wait and retry.
+        // Every in-flight transaction request occupies internal memory as DMA buffer if needed.
+        return ESP_ERR_TIMEOUT;
+    }
+    //release temporary buffers
+    uninstall_priv_desc(&trans_buf);
+    (*trans_desc) = trans_buf.trans;
+
+    return ESP_OK;
+}
+
+//Porcelain to do one blocking transmission.
+esp_err_t SPI_MASTER_ATTR spi_device_transmit(spi_device_handle_t handle, spi_transaction_t *trans_desc)
+{
+    esp_err_t ret;
+    spi_transaction_t *ret_trans;
+    //ToDo: check if any spi transfers in flight
+    ret = spi_device_queue_trans(handle, trans_desc, portMAX_DELAY);
+    if (ret != ESP_OK) return ret;
+
+    ret = spi_device_get_trans_result(handle, &ret_trans, portMAX_DELAY);
+    if (ret != ESP_OK) return ret;
+
+    assert(ret_trans == trans_desc);
+    return ESP_OK;
+}
+
+esp_err_t SPI_MASTER_ISR_ATTR spi_device_acquire_bus(spi_device_t *device, TickType_t wait)
+{
+    spi_host_t *const host = device->host;
+    SPI_CHECK(wait==portMAX_DELAY, "acquire finite time not supported now.", ESP_ERR_INVALID_ARG);
+    SPI_CHECK(!spi_bus_device_is_polling(device), "Cannot acquire bus when a polling transaction is in progress.", ESP_ERR_INVALID_STATE );
+
+    esp_err_t ret = spi_bus_lock_acquire_start(device->dev_lock, wait);
+    if (ret != ESP_OK) {
+        return ret;
+    }
+    host->device_acquiring_lock = device;
+
+    ESP_LOGD(SPI_TAG, "device%d locked the bus", device->id);
+
+#ifdef CONFIG_PM_ENABLE
+    // though we don't suggest to block the task before ``release_bus``, still allow doing so.
+    // this keeps the spi clock at 80MHz even if all tasks are blocked
+    esp_pm_lock_acquire(host->bus_attr->pm_lock);
+#endif
+    //configure the device ahead so that we don't need to do it again in the following transactions
+    spi_setup_device(host->device[device->id]);
+    //the DMA is also occupied by the device, all the slave devices that using DMA should wait until bus released.
+    if (host->bus_attr->dma_enabled) {
+        //This workaround is only for esp32, where tx_dma_chan and rx_dma_chan are always same
+        spicommon_dmaworkaround_transfer_active(host->bus_attr->tx_dma_chan);
+    }
+    return ESP_OK;
+}
+
+// This function restore configurations required in the non-polling mode
+void SPI_MASTER_ISR_ATTR spi_device_release_bus(spi_device_t *dev)
+{
+    spi_host_t *host = dev->host;
+
+    if (spi_bus_device_is_polling(dev)){
+        ESP_EARLY_LOGE(SPI_TAG, "Cannot release bus when a polling transaction is in progress.");
+        assert(0);
+    }
+
+    if (host->bus_attr->dma_enabled) {
+        //This workaround is only for esp32, where tx_dma_chan and rx_dma_chan are always same
+        spicommon_dmaworkaround_idle(host->bus_attr->tx_dma_chan);
+    }
+    //Tell common code DMA workaround that our DMA channel is idle. If needed, the code will do a DMA reset.
+
+    //allow clock to be lower than 80MHz when all tasks blocked
+#ifdef CONFIG_PM_ENABLE
+    //Release APB frequency lock
+    esp_pm_lock_release(host->bus_attr->pm_lock);
+#endif
+    ESP_LOGD(SPI_TAG, "device%d release bus", dev->id);
+
+    host->device_acquiring_lock = NULL;
+    esp_err_t ret = spi_bus_lock_acquire_end(dev->dev_lock);
+    assert(ret == ESP_OK);
+}
+
+esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_start(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait)
+{
+    esp_err_t ret;
+    SPI_CHECK(ticks_to_wait == portMAX_DELAY, "currently timeout is not available for polling transactions", ESP_ERR_INVALID_ARG);
+    ret = check_trans_valid(handle, trans_desc);
+    if (ret!=ESP_OK) return ret;
+    SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot send polling transaction while the previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE );
+
+    /* If device_acquiring_lock is set to handle, it means that the user has already
+     * acquired the bus thanks to the function `spi_device_acquire_bus()`.
+     * In that case, we don't need to take the lock again. */
+    spi_host_t *host = handle->host;
+    if (host->device_acquiring_lock != handle) {
+        ret = spi_bus_lock_acquire_start(handle->dev_lock, ticks_to_wait);
+    } else {
+        ret = spi_bus_lock_wait_bg_done(handle->dev_lock, ticks_to_wait);
+    }
+    if (ret != ESP_OK) return ret;
+
+    ret = setup_priv_desc(trans_desc, &host->cur_trans_buf, (host->bus_attr->dma_enabled));
+    if (ret!=ESP_OK) return ret;
+
+    //Polling, no interrupt is used.
+    host->polling = true;
+
+    ESP_LOGV(SPI_TAG, "polling trans");
+    spi_new_trans(handle, &host->cur_trans_buf);
+
+    return ESP_OK;
+}
+
+esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_end(spi_device_handle_t handle, TickType_t ticks_to_wait)
+{
+    SPI_CHECK(handle != NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
+    spi_host_t *host = handle->host;
+
+    assert(host->cur_cs == handle->id);
+    assert(handle == get_acquiring_dev(host));
+
+    TickType_t start = xTaskGetTickCount();
+    while (!spi_hal_usr_is_done(&host->hal)) {
+        TickType_t end = xTaskGetTickCount();
+        if (end - start > ticks_to_wait) {
+            return ESP_ERR_TIMEOUT;
+        }
+    }
+
+    ESP_LOGV(SPI_TAG, "polling trans done");
+    //deal with the in-flight transaction
+    spi_post_trans(host);
+    //release temporary buffers
+    uninstall_priv_desc(&host->cur_trans_buf);
+
+    host->polling = false;
+    if (host->device_acquiring_lock != handle) {
+        assert(host->device_acquiring_lock == NULL);
+        spi_bus_lock_acquire_end(handle->dev_lock);
+    }
+
+    return ESP_OK;
+}
+
+esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_transmit(spi_device_handle_t handle, spi_transaction_t* trans_desc)
+{
+    esp_err_t ret;
+	static SemaphoreHandle_t mutex;
+	if (!mutex) mutex = xSemaphoreCreateMutex();
+	xSemaphoreTake(mutex, portMAX_DELAY);
+
+    ret = spi_device_polling_start(handle, trans_desc, portMAX_DELAY);
+	if (ret != ESP_OK) {
+		xSemaphoreGive(mutex);
+		return ret;	
+	}	
+
+	ret = spi_device_polling_end(handle, portMAX_DELAY);
+	xSemaphoreGive(mutex);
+	return ret;
+}

+ 4 - 4
components/esp_http_server/CMakeLists.txt

@@ -2,9 +2,9 @@ idf_build_get_property(prefix IDF_PATH)
 string(CONCAT prefix "${prefix}" "/components/esp_http_server")
 
 idf_component_register(
-		SRC_DIRS "${prefix}/src"	"${prefix}/src/util"	
+		SRC_DIRS "${prefix}/src" "${prefix}/src/util"	
 		INCLUDE_DIRS "${prefix}/include"
-                PRIV_INCLUDE_DIRS "." "${prefix}/src/port/esp32" "${prefix}/src/util"
-                REQUIRES nghttp # for http_parser.h
-                    PRIV_REQUIRES lwip mbedtls esp_timer
+        PRIV_INCLUDE_DIRS "." "${prefix}/src/port/esp32" "${prefix}/src/util"
+        REQUIRES nghttp # for http_parser.h
+        PRIV_REQUIRES lwip mbedtls esp_timer
 ) 

+ 0 - 2
components/services/CMakeLists.txt

@@ -3,5 +3,3 @@ idf_component_register(SRC_DIRS .
 						REQUIRES json tools platform_config display wifi-manager
 						PRIV_REQUIRES soc esp32
 )
-
-

+ 1 - 1
components/services/gpio_exp.c

@@ -213,7 +213,7 @@ gpio_exp_t* gpio_exp_create(const gpio_exp_config_t *config) {
 		gpio_intr_enable(config->intr);						
 	}
 	
-	ESP_LOGI(TAG, "Create GPIO expander %s at base %u with INT %d at @%x on port/host %d/%d", config->model, config->base, config->intr, config->phy.addr, config->phy.port, config->phy.host);
+	ESP_LOGI(TAG, "Create GPIO expander %s at base %u with intr %d at @%x on port/host %d/%d", config->model, config->base, config->intr, config->phy.addr, config->phy.port, config->phy.host);
 	return expander;
 }
 

+ 5 - 13
components/spotify/CMakeLists.txt

@@ -3,26 +3,18 @@ set(CMAKE_CXX_STANDARD 17)
 
 idf_component_register(
 		SRC_DIRS . 
-		INCLUDE_DIRS . "cspot/include" "cspot/bell/include" "cspot/protos"
-		PRIV_REQUIRES mbedtls mdns nvs_flash platform_config services esp_http_server tools
+		INCLUDE_DIRS . "cspot/include" "cspot/bell/include"
+		PRIV_REQUIRES mbedtls mdns nvs_flash platform_config services esp_http_server tools codecs
 		LDFRAGMENTS "linker.lf"
 )
 
-include_directories("../codecs/inc")
 add_definitions(-DBELL_USE_MBEDTLS)
 add_definitions(-Wno-unused-variable -Wno-unused-const-variable -Wchar-subscripts -Wunused-label -Wmaybe-uninitialized -Wmisleading-indentation)
 
-set(BELL_DISABLE_CODECS 1)
-set(BELL_TREMOR_EXTERNAL "idf::codecs")
-set(BELL_CJSON_EXTERNAL "idf::json")
+set(BELL_DISABLE_CODECS ON)
+set(BELL_EXTERNAL_TREMOR "idf::codecs")
+set(BELL_EXTERNAL_CJSON "idf::json")
 
 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cspot ${CMAKE_CURRENT_BINARY_DIR}/cspot)
 target_link_libraries(${COMPONENT_LIB} PRIVATE cspot ${EXTRA_REQ_LIBS})
 
-#if (!WIN32)
-#	message(${CMAKE_CURRENT_SOURCE_DIR}/cmake/generate_protos.sh)
-#	execute_process(COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/cmake/generate_protos.sh" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
-#endif ()
-
-
-

+ 2 - 2
components/spotify/Shim.cpp

@@ -71,7 +71,7 @@ static void cspotTask(void *pvParameters) {
 		ESP_LOGW(TAG, "Cannot load config, using default");
 		
 		configMan->deviceName = cspot.name;
-		configMan->format = AudioFormat::OGG_VORBIS_160;
+		configMan->format = AudioFormat_OGG_VORBIS_160;
 		configMan->volume = 32767;
 
 		configMan->save();	
@@ -80,7 +80,7 @@ static void cspotTask(void *pvParameters) {
 	// safely load config now
 	configMan->load();
 	if (!configMan->deviceName.length()) configMan->deviceName = cspot.name;
-	ESP_LOGI(TAG, "Started CSpot with %s (bitrate %d)", configMan->deviceName.c_str(), configMan->format == AudioFormat::OGG_VORBIS_320 ? 320 : (configMan->format == AudioFormat::OGG_VORBIS_160 ? 160 : 96));
+	ESP_LOGI(TAG, "Started CSpot with %s (bitrate %d)", configMan->deviceName.c_str(), configMan->format == AudioFormat_OGG_VORBIS_320 ? 320 : (configMan->format == AudioFormat_OGG_VORBIS_160 ? 160 : 96));
 
 	// All we do here is notify the task to start the mercury loop
     auto createPlayerCallback = [](std::shared_ptr<LoginBlob> blob) {

+ 30 - 56
components/spotify/cspot/CMakeLists.txt

@@ -1,66 +1,40 @@
+cmake_minimum_required(VERSION 2.8.12)
+
 project(cspot)
 
-cmake_minimum_required(VERSION 2.8.9)
-set (CMAKE_CXX_STANDARD 17)
+# Configurable options
+set(CSPOT_EXTERNAL_BELL "" CACHE STRING "External bell library target name, optional")
+
+# CMake options
+set(CMAKE_CXX_STANDARD 17)
 
+# Main library sources
 file(GLOB SOURCES "src/*.cpp" "src/*.c")
 
-if (NOT DEFINED ${USE_EXTERNAL_BELL})
-    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/bell ${CMAKE_CURRENT_BINARY_DIR}/bell)
+# Use externally specified bell library or the submodule
+if(CSPOT_EXTERNAL_BELL)
+    list(APPEND EXTRA_LIBS ${CSPOT_EXTERNAL_BELL})
+else()
+    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/bell)
+    list(APPEND EXTRA_LIBS bell)
 endif()
-# Add platform specific sources
-# if(${ESP_PLATFORM})
-#     file(GLOB ESP_PLATFORM_SOURCES "src/platform/esp/*.cpp" "src/platform/esp/*.c")
-#     set(SOURCES ${SOURCES} ${ESP_PLATFORM_SOURCES} )
-# endif()
-
-# if(UNIX)
-#     file(GLOB UNIX_PLATFORM_SOURCES "src/platform/unix/*.cpp" "src/platform/unix/*.c")
-#     set(SOURCES ${SOURCES} ${UNIX_PLATFORM_SOURCES} )
-# endif()
-
-# if(APPLE)
-#     file(GLOB APPLE_PLATFORM_SOURCES "src/platform/apple/*.cpp" "src/platform/apple/*.c")
-#     set(SOURCES ${SOURCES} ${APPLE_PLATFORM_SOURCES} )
-# endif()
-
-# if(UNIX AND NOT APPLE)
-# #     file(GLOB LINUX_PLATFORM_SOURCES "src/platform/linux/*.cpp" "src/platform/linux/*.c")
-# #     set(SOURCES ${SOURCES} ${LINUX_PLATFORM_SOURCES} )
-# # endif()
-
-
-# if(${ESP_PLATFORM})
-#     list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoOpenSSL.cpp) # use MBedTLS 
-#     idf_build_set_property(COMPILE_DEFINITIONS "-DCSPOT_USE_MBEDTLS" APPEND)
-#     set(EXTRA_REQ_LIBS idf::mbedtls idf::pthread idf::mdns)
-#     add_definitions(-Wunused-const-variable -Wchar-subscripts -Wunused-label -Wmaybe-uninitialized -Wmisleading-indentation)
-# else()
-#     list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoMbedTLS.cpp) # use OpenSSL
-#     find_package(OpenSSL REQUIRED)
-#     if(OPENSSL_FOUND)
-#         set(OPENSSL_USE_STATIC_LIBS TRUE)
-#     endif()
-#     set(EXTRA_REQ_LIBS OpenSSL::Crypto Threads::Threads)
-#     set(THREADS_PREFER_PTHREAD_FLAG ON)
-#     find_package(Threads REQUIRED)
-# endif()
 
+# Add Apple Bonjour compatibility library for Linux
 if(UNIX AND NOT APPLE)
-    set(EXTRA_REQ_LIBS ${EXTRA_REQ_LIBS} dns_sd) # add apple bonjur compatibility library for linux
+    list(APPEND EXTRA_LIBS dns_sd)
     # TODO: migrate from this to native linux mDNS
 endif()
-
-include_directories("include")
-include_directories("${CMAKE_CURRENT_BINARY_DIR}")
-include_directories("protos")
-
-message(${CMAKE_CURRENT_SOURCE_DIR}/cmake/generate_protos.sh)
-execute_process(COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/cmake/generate_protos.sh" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
-
-set(SOURCES ${SOURCES} "protos/AnyRefImpl.cpp" "protos/ReflectTypeInfo.cpp")
-
-add_library(cspot STATIC ${SOURCES})
-target_link_libraries(cspot PRIVATE bell ${EXTRA_REQ_LIBS})
-
-target_include_directories(cspot PUBLIC "include" "protos" bell ${EXTRA_REQ_LIBS} ${CMAKE_CURRENT_BINARY_DIR})
+set(NANOPB_OPTIONS "-I${CMAKE_CURRENT_SOURCE_DIR}")
+set(PROTOS protobuf/authentication.proto protobuf/mercury.proto protobuf/keyexchange.proto protobuf/spirc.proto protobuf/metadata.proto)
+message(${PROTOS})
+message("building protobuf")
+message(${CMAKE_CURRENT_SOURCE_DIR})
+nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS RELPATH ${CMAKE_CURRENT_SOURCE_DIR} ${PROTOS})
+add_custom_target(generate_proto_sources DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
+set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS}
+    PROPERTIES GENERATED TRUE)
+
+add_library(cspot STATIC ${SOURCES} ${PROTO_SRCS})
+# PUBLIC to propagate includes from bell to cspot dependents
+target_link_libraries(cspot PUBLIC ${EXTRA_LIBS})
+target_include_directories(cspot PUBLIC "include" ${CMAKE_CURRENT_BINARY_DIR} ${NANOPB_INCLUDE_DIRS})

+ 61 - 41
components/spotify/cspot/bell/CMakeLists.txt

@@ -1,80 +1,100 @@
+cmake_minimum_required(VERSION 2.8.12)
+
 project(bell)
 
-cmake_minimum_required(VERSION 2.8.9)
-set (CMAKE_CXX_STANDARD 17)
+# Configurable options
+option(BELL_DISABLE_CODECS "Disable libhelix AAC and MP3 codecs" OFF)
+#set(BELL_EXTERNAL_CJSON "" CACHE STRING "External cJSON library target name, optional")
+#set(BELL_EXTERNAL_TREMOR "" CACHE STRING "External tremor library target name, optional")
 
-file(GLOB SOURCES "src/*.cpp" "src/*.c")
+add_definitions(-DPB_ENABLE_MALLOC)
 
-# Add platform specific sources
+# Include nanoPB library
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/nanopb/extra)
+find_package(Nanopb REQUIRED)
+include_directories(${NANOPB_INCLUDE_DIRS})
+
+# CMake options
+set(CMAKE_CXX_STANDARD 17)
+add_definitions(-DUSE_DEFAULT_STDLIB=1)
+
+# Main library sources
+file(GLOB SOURCES "src/*.cpp" "src/*.c" "nanopb/*.c")
 
-if(${ESP_PLATFORM})
+# Add platform specific sources
+if(ESP_PLATFORM)
     file(GLOB ESP_PLATFORM_SOURCES "src/platform/esp/*.cpp" "src/platform/esp/*.c" "src/asm/biquad_f32_ae32.S")
-    set(SOURCES ${SOURCES} ${ESP_PLATFORM_SOURCES} )
+    list(APPEND SOURCES ${ESP_PLATFORM_SOURCES})
 endif()
 
 if(UNIX)
     file(GLOB UNIX_PLATFORM_SOURCES "src/platform/unix/*.cpp" "src/platform/linux/TLSSocket.cpp" "src/platform/unix/*.c")
-    set(SOURCES ${SOURCES} ${UNIX_PLATFORM_SOURCES} )
+    list(APPEND SOURCES ${UNIX_PLATFORM_SOURCES})
 endif()
 
 if(APPLE)
     file(GLOB APPLE_PLATFORM_SOURCES "src/platform/apple/*.cpp" "src/platform/linux/TLSSocket.cpp"  "src/platform/apple/*.c")
-    set(SOURCES ${SOURCES} ${APPLE_PLATFORM_SOURCES} )
+    list(APPEND SOURCES ${APPLE_PLATFORM_SOURCES})
 endif()
 
 if(UNIX AND NOT APPLE)
     file(GLOB LINUX_PLATFORM_SOURCES "src/platform/linux/*.cpp" "src/platform/linux/*.c")
-    set(SOURCES ${SOURCES} ${LINUX_PLATFORM_SOURCES} )
+    list(APPEND SOURCES ${LINUX_PLATFORM_SOURCES})
 endif()
 
-if(${ESP_PLATFORM})
-    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoOpenSSL.cpp) # use MBedTLS 
+if(ESP_PLATFORM)
+    # Use MBedTLS on ESP32
+    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoOpenSSL.cpp)
     idf_build_set_property(COMPILE_DEFINITIONS "-DBELL_USE_MBEDTLS" APPEND)
-    set(EXTRA_REQ_LIBS idf::mbedtls idf::pthread idf::mdns)
+    list(APPEND EXTRA_LIBS idf::mbedtls idf::pthread idf::mdns)
     add_definitions(-Wunused-const-variable -Wchar-subscripts -Wunused-label -Wmaybe-uninitialized -Wmisleading-indentation)
 else()
-    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoMbedTLS.cpp) # use OpenSSL
+    # Use OpenSSL elsewhere
+    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/CryptoMbedTLS.cpp)
     find_package(OpenSSL REQUIRED)
+    find_package(Threads REQUIRED)
+    set(THREADS_PREFER_PTHREAD_FLAG ON)
     if(OPENSSL_FOUND)
         set(OPENSSL_USE_STATIC_LIBS TRUE)
     endif()
-    set(EXTRA_REQ_LIBS OpenSSL::Crypto OpenSSL::SSL Threads::Threads)
-    set(THREADS_PREFER_PTHREAD_FLAG ON)
-    find_package(Threads REQUIRED)
+    list(APPEND EXTRA_LIBS OpenSSL::Crypto OpenSSL::SSL Threads::Threads)
 endif()
 
-if (BELL_DISABLE_CODECS) 
-    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/DecoderGlobals.cpp) # use OpenSSL
-	add_definitions(-DBELL_DISABLE_CODECS)
+if(BELL_DISABLE_CODECS)
+    list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/DecoderGlobals.cpp)
 else()
-	set(EXTRA_INC ${EXTRA_INC} "libhelix-aac" "libhelix-mp3")
-	set(SOURCES ${SOURCES} "libhelix-aac/aacdec.c" "libhelix-aac/aactabs.c" "libhelix-aac/bitstream.c" "libhelix-aac/buffers.c" "libhelix-aac/dct4.c" "libhelix-aac/decelmnt.c" "libhelix-aac/dequant.c" "libhelix-aac/fft.c" "libhelix-aac/filefmt.c" "libhelix-aac/huffman.c" "libhelix-aac/hufftabs.c" "libhelix-aac/imdct.c" "libhelix-aac/noiseless.c" "libhelix-aac/pns.c" "libhelix-aac/sbr.c" "libhelix-aac/sbrfft.c" "libhelix-aac/sbrfreq.c" "libhelix-aac/sbrhfadj.c" "libhelix-aac/sbrhfgen.c" "libhelix-aac/sbrhuff.c" "libhelix-aac/sbrimdct.c" "libhelix-aac/sbrmath.c" "libhelix-aac/sbrqmf.c" "libhelix-aac/sbrside.c" "libhelix-aac/sbrtabs.c" "libhelix-aac/stproc.c" "libhelix-aac/tns.c" "libhelix-aac/trigtabs.c")
-	set(SOURCES ${SOURCES} "libhelix-mp3/bitstream.c" "libhelix-mp3/buffers.c" "libhelix-mp3/dct32.c" "libhelix-mp3/dequant.c" "libhelix-mp3/dqchan.c" "libhelix-mp3/huffman.c" "libhelix-mp3/hufftabs.c" "libhelix-mp3/imdct.c" "libhelix-mp3/mp3dec.c" "libhelix-mp3/mp3tabs.c" "libhelix-mp3/polyphase.c" "libhelix-mp3/scalfact.c" "libhelix-mp3/stproc.c" "libhelix-mp3/subband.c" "libhelix-mp3/trigtabs.c")
-endif()
+    file(GLOB LIBHELIX_AAC_SOURCES "libhelix-aac/*.c")
+    file(GLOB LIBHELIX_MP3_SOURCES "libhelix-mp3/*.c")
+    list(APPEND EXTRA_INCLUDES "libhelix-aac" "libhelix-mp3")
+    list(APPEND SOURCES ${LIBHELIX_MP3_SOURCES} ${LIBHELIX_AAC_SOURCES})
 
-if(UNIX AND NOT APPLE)
-    set(EXTRA_REQ_LIBS ${EXTRA_REQ_LIBS} dns_sd) # add apple bonjur compatibility library for linux
-    # TODO: migrate from this to native linux mDNS
+    if(CYGWIN)
+        # Both Cygwin and ESP are Unix-like so this seems to work (or, at least, compile)
+        set_source_files_properties(src/DecoderGlobals.cpp PROPERTIES COMPILE_FLAGS -DESP_PLATFORM)
+        set_source_files_properties(${LIBHELIX_AAC_SOURCES} PROPERTIES COMPILE_FLAGS -DESP_PLATFORM)
+        set_source_files_properties(${LIBHELIX_MP3_SOURCES} PROPERTIES COMPILE_FLAGS -DESP_PLATFORM)
+    endif()
 endif()
-add_definitions( -DUSE_DEFAULT_STDLIB=1)
-
-if (BELL_CJSON_EXTERNAL) 
-	message("Using external cJSON")
-	set(EXTRA_REQ_LIBS ${EXTRA_REQ_LIBS} ${BELL_CJSON_EXTERNAL})
-else() 
-	set(EXTRA_INC ${EXTRA_INC} "cJSON")
-	set(SOURCES ${SOURCES} "cJSON/cJSON.c")
+
+if(BELL_EXTERNAL_CJSON)
+    list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
+else()
+    list(APPEND EXTRA_INCLUDES "cJSON")
+    list(APPEND SOURCES "cJSON/cJSON.c")
 endif()
 
-if (BELL_TREMOR_EXTERNAL) 
-	message("Using external TREMOR")
-	set(EXTRA_REQ_LIBS ${EXTRA_REQ_LIBS} ${BELL_TREMOR_EXTERNAL})
+if(BELL_EXTERNAL_TREMOR)
+    list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_TREMOR})
 else()
-	set(EXTRA_INC ${EXTRA_INC} "tremor")
-	set(SOURCES ${SOURCES} "tremor/mdct.c" "tremor/dsp.c" "tremor/info.c" "tremor/misc.c" "tremor/floor1.c" "tremor/floor0.c" "tremor/vorbisfile.c" "tremor/res012.c" "tremor/mapping0.c" "tremor/codebook.c" "tremor/framing.c" "tremor/bitwise.c" "tremor/floor_lookup.c")
+    file(GLOB TREMOR_SOURCES "tremor/*.c")
+    list(REMOVE_ITEM TREMOR_SOURCES "tremor/ivorbisfile_example.c")
+    list(APPEND EXTRA_INCLUDES "tremor")
+    list(APPEND SOURCES ${TREMOR_SOURCES})
 endif()
 
 add_library(bell STATIC ${SOURCES})
-target_link_libraries(bell PRIVATE ${EXTRA_REQ_LIBS})
-target_include_directories(bell PUBLIC "include" "protos" ${EXTRA_INC} ${EXTRA_REQ_LIBS} ${CMAKE_CURRENT_BINARY_DIR})
 
+message(${NANOPB_INCLUDE_DIRS})
+# PUBLIC to propagate esp-idf includes to bell dependents
+target_link_libraries(bell PUBLIC ${EXTRA_LIBS})
+target_include_directories(bell PUBLIC "include" ${EXTRA_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})

+ 7 - 0
components/spotify/cspot/bell/include/BaseHTTPServer.h

@@ -6,6 +6,7 @@
 #include <map>
 #include <memory>
 #include <functional>
+#include <vector>
 
 namespace bell {
 
@@ -16,6 +17,7 @@ class ResponseReader {
 
     virtual size_t getTotalSize() = 0;
     virtual size_t read(char *buffer, size_t size) = 0;
+    virtual void close() = 0;
 };
 
 class FileResponseReader : public ResponseReader {
@@ -34,6 +36,10 @@ class FileResponseReader : public ResponseReader {
         return fread(buffer, 1, size, file);
     }
 
+    void close() {
+        fclose(file);
+    }
+
     size_t getTotalSize() { return fileSize; }
 };
 
@@ -43,6 +49,7 @@ struct HTTPRequest {
     std::map<std::string, std::string> urlParams;
     std::map<std::string, std::string> queryParams;
     std::string body;
+    std::string url;
     int handlerId;
     int connection;
 };

+ 4 - 2
components/spotify/cspot/bell/include/BellSocket.h

@@ -9,11 +9,13 @@ namespace bell {
             Socket() {};
             virtual ~Socket() = default;
 
-            virtual void open(std::string url) = 0;
+			void open(const std::string &url);
+			virtual void open(std::string host, uint16_t port) = 0;
+			virtual size_t poll() = 0;
             virtual size_t write(uint8_t* buf, size_t len) = 0;
             virtual size_t read(uint8_t* buf, size_t len) = 0;
             virtual void close() = 0;
     };
 }
 
-#endif
+#endif

+ 19 - 27
components/spotify/cspot/bell/include/TCPSocket.h

@@ -17,6 +17,7 @@
 #include <fstream>
 #include <netinet/tcp.h>
 #include <BellLogger.h>
+#include <sys/ioctl.h>
 
 namespace bell
 {
@@ -32,50 +33,35 @@ namespace bell
             close();
         };
 
-        void open(std::string url)
+        void open(std::string host, uint16_t port)
         {
-            // remove https or http from url
-            url.erase(0, url.find("://") + 3);
-
-            // split by first "/" in url
-            std::string hostUrl = url.substr(0, url.find('/'));
-            std::string pathUrl = url.substr(url.find('/'));
-
-            std::string portString = "80";
-
-            // check if hostUrl contains ':'
-            if (hostUrl.find(':') != std::string::npos)
-            {
-                // split by ':'
-                std::string host = hostUrl.substr(0, hostUrl.find(':'));
-                portString = hostUrl.substr(hostUrl.find(':') + 1);
-                hostUrl = host;
-            }
-
+			int err;
             int domain = AF_INET;
             int socketType = SOCK_STREAM;
 
-            addrinfo hints, *addr;
+            struct addrinfo hints{}, *addr;
             //fine-tune hints according to which socket you want to open
             hints.ai_family = domain;
             hints.ai_socktype = socketType;
             hints.ai_protocol = IPPROTO_IP; // no enum : possible value can be read in /etc/protocols
             hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
 
-            BELL_LOG(info, "http", "%s %s", hostUrl.c_str(), portString.c_str());
+            BELL_LOG(info, "http", "%s %d", host.c_str(), port);
 
-            if (getaddrinfo(hostUrl.c_str(), portString.c_str(), &hints, &addr) != 0)
-            {
-                BELL_LOG(error, "webradio", "DNS lookup error");
+            char portStr[6];
+			sprintf(portStr, "%u", port);
+			err = getaddrinfo(host.c_str(), portStr, &hints, &addr);
+            if (err != 0) {
                 throw std::runtime_error("Resolve failed");
             }
 
             sockFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
 
-            if (connect(sockFd, addr->ai_addr, addr->ai_addrlen) < 0)
+			err = connect(sockFd, addr->ai_addr, addr->ai_addrlen);
+            if (err < 0)
             {
                 close();
-                BELL_LOG(error, "http", "Could not connect to %s", url.c_str());
+                BELL_LOG(error, "http", "Could not connect to %s. Error %d", host.c_str(), errno);
                 throw std::runtime_error("Resolve failed");
             }
 
@@ -97,6 +83,12 @@ namespace bell
             return send(sockFd, buf, len, 0);
         }
 
+		size_t poll() {
+			int value;
+			ioctl(sockFd, FIONREAD, &value);
+			return value;
+		}
+
         void close() {
             if (!isClosed) {
                 ::close(sockFd);
@@ -107,4 +99,4 @@ namespace bell
 
 }
 
-#endif
+#endif

+ 2 - 1
components/spotify/cspot/bell/include/platform/TLSSocket.h

@@ -58,10 +58,11 @@ public:
   TLSSocket();
   ~TLSSocket() { close(); };
 
-  void open(std::string host);
+  void open(std::string host, uint16_t port);
 
   size_t read(uint8_t *buf, size_t len);
   size_t write(uint8_t *buf, size_t len);
+  size_t poll();
 
   void close();
 };

+ 16 - 17
components/spotify/cspot/bell/src/HTTPServer.cpp

@@ -65,7 +65,7 @@ void bell::HTTPServer::registerHandler(RequestType requestType,
 }
 
 void bell::HTTPServer::listen() {
-    BELL_LOG(info, "http", "Starting configuration server at port %d",
+    BELL_LOG(info, "http", "Starting server at port %d",
              this->serverPort);
 
     // setup address
@@ -170,8 +170,8 @@ void bell::HTTPServer::readFromClient(int clientFd) {
                 if (line.find("Content-Length: ") != std::string::npos) {
                     conn.contentLength =
                         std::stoi(line.substr(16, line.size() - 1));
-                    BELL_LOG(info, "http", "Content-Length: %d",
-                             conn.contentLength);
+                    //BELL_LOG(info, "http", "Content-Length: %d",
+                    //         conn.contentLength);
                 }
                 if (line.size() == 0) {
                     if (conn.contentLength != 0) {
@@ -201,7 +201,7 @@ void bell::HTTPServer::writeResponseEvents(int connFd) {
 
     std::stringstream stream;
     stream << "HTTP/1.1 200 OK\r\n";
-    stream << "Server: EUPHONIUM\r\n";
+    stream << "Server: bell-http\r\n";
     stream << "Connection: keep-alive\r\n";
     stream << "Content-type: text/event-stream\r\n";
     stream << "Cache-Control: no-cache\r\n";
@@ -229,7 +229,7 @@ void bell::HTTPServer::writeResponse(const HTTPResponse &response) {
 
     std::stringstream stream;
     stream << "HTTP/1.1 " << response.status << " OK\r\n";
-    stream << "Server: EUPHONIUM\r\n";
+    stream << "Server: bell-http\r\n";
     stream << "Connection: close\r\n";
     stream << "Content-type: " << response.contentType << "\r\n";
 
@@ -270,7 +270,6 @@ void bell::HTTPServer::writeResponse(const HTTPResponse &response) {
         } while (read > 0);
     }
 
-    BELL_LOG(info, "HTTP", "Closing connection");
     this->closeConnection(response.connectionFd);
 }
 
@@ -282,7 +281,7 @@ void bell::HTTPServer::redirectTo(const std::string & url, int connectionFd) {
     std::lock_guard lock(this->responseMutex);
     std::stringstream stream;
     stream << "HTTP/1.1 301 Moved Permanently\r\n";
-    stream << "Server: EUPHONIUM\r\n";
+    stream << "Server: bell-http\r\n";
     stream << "Connection: close\r\n";
     stream << "Location: " << url << "\r\n\r\n";
     auto responseStr = stream.str();
@@ -314,11 +313,11 @@ std::map<std::string, std::string>
 bell::HTTPServer::parseQueryString(const std::string &queryString) {
     std::map<std::string, std::string> query;
     auto prefixedString = "&" + queryString;
-    while (prefixedString.find("&") != std::string::npos) {
-        auto keyStart = prefixedString.find("&");
-        auto keyEnd = prefixedString.find("=");
+    while (prefixedString.find('&') != std::string::npos) {
+        auto keyStart = prefixedString.find('&');
+        auto keyEnd = prefixedString.find('=');
         // Find second occurence of "&" in prefixedString
-        auto valueEnd = prefixedString.find("&", keyStart + 1);
+        auto valueEnd = prefixedString.find('&', keyStart + 1);
         if (valueEnd == std::string::npos) {
             valueEnd = prefixedString.size();
         }
@@ -336,12 +335,11 @@ void bell::HTTPServer::findAndHandleRoute(std::string &url, std::string &body,
                                           int connectionFd) {
     std::map<std::string, std::string> pathParams;
     std::map<std::string, std::string> queryParams;
-    BELL_LOG(info, "http", "URL %s", url.c_str());
 
     if (url.find("OPTIONS /") != std::string::npos) {
         std::stringstream stream;
         stream << "HTTP/1.1 200 OK\r\n";
-        stream << "Server: EUPHONIUM\r\n";
+        stream << "Server: bell-http\r\n";
         stream << "Allow: OPTIONS, GET, HEAD, POST\r\n";
         stream << "Connection: close\r\n";
         stream << "Access-Control-Allow-Origin: *\r\n";
@@ -377,9 +375,9 @@ void bell::HTTPServer::findAndHandleRoute(std::string &url, std::string &body,
                 continue;
             }
 
-            path = path.substr(0, path.find(" "));
+            path = path.substr(0, path.find(' '));
 
-            if (path.find("?") != std::string::npos) {
+            if (path.find('?') != std::string::npos) {
                 auto urlEncodedSplit = splitUrl(path, '?');
                 path = urlEncodedSplit[0];
                 queryParams = this->parseQueryString(urlEncodedSplit[1]);
@@ -406,19 +404,20 @@ void bell::HTTPServer::findAndHandleRoute(std::string &url, std::string &body,
                 matches = false;
             }
 
-            if (routeSplit.back().find("*") != std::string::npos &&
+            if (routeSplit.back().find('*') != std::string::npos &&
                 urlSplit[1] == routeSplit[1]) {
                 matches = true;
             }
 
             if (matches) {
-                if (body.find("&") != std::string::npos) {
+                if (body.find('&') != std::string::npos) {
                     queryParams = this->parseQueryString(body);
                 }
 
                 HTTPRequest req = {.urlParams = pathParams,
                                    .queryParams = queryParams,
                                    .body = body,
+                                   .url = path,
                                    .handlerId = 0,
                                    .connection = connectionFd};
 

+ 6 - 17
components/spotify/cspot/bell/src/platform/esp/TLSSocket.cpp

@@ -21,25 +21,10 @@ bell::TLSSocket::TLSSocket()
   }
 }
 
-void bell::TLSSocket::open(std::string url)
+void bell::TLSSocket::open(std::string hostUrl, uint16_t port)
 {
-  // initialize
   int ret;
-  url.erase(0, url.find("://") + 3);
-  std::string hostUrl = url.substr(0, url.find('/'));
-  std::string pathUrl = url.substr(url.find('/'));
-
-  std::string portString = "443";
-  // check if hostUrl contains ':'
-  if (hostUrl.find(':') != std::string::npos)
-  {
-    // split by ':'
-    std::string host = hostUrl.substr(0, hostUrl.find(':'));
-    portString = hostUrl.substr(hostUrl.find(':') + 1);
-    hostUrl = host;
-  }
-
-  if ((ret = mbedtls_net_connect(&server_fd, hostUrl.c_str(), "443",
+  if ((ret = mbedtls_net_connect(&server_fd, hostUrl.c_str(), std::to_string(port).c_str(),
                                  MBEDTLS_NET_PROTO_TCP)) != 0)
   {
     BELL_LOG(error, "http_tls", "failed! connect returned %d\n", ret);
@@ -82,6 +67,10 @@ size_t bell::TLSSocket::write(uint8_t *buf, size_t len)
   return mbedtls_ssl_write(&ssl, buf, len);
 }
 
+size_t bell::TLSSocket::poll() {
+  return mbedtls_ssl_get_bytes_avail(&ssl);
+}
+
 void bell::TLSSocket::close()
 {
   mbedtls_net_free(&server_fd);

+ 7 - 16
components/spotify/cspot/bell/src/platform/linux/TLSSocket.cpp

@@ -11,7 +11,7 @@ bell::TLSSocket::TLSSocket() {
   ctx = SSL_CTX_new(SSLv23_client_method());
 }
 
-void bell::TLSSocket::open(std::string url) {
+void bell::TLSSocket::open(std::string host, uint16_t port) {
 
   /* We'd normally set some stuff like the verify paths and
    * mode here because as things stand this will connect to
@@ -23,21 +23,8 @@ void bell::TLSSocket::open(std::string url) {
   BIO_get_ssl(sbio, &ssl);
   SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
 
-  url.erase(0, url.find("://") + 3);
-  std::string hostUrl = url.substr(0, url.find('/'));
-  std::string pathUrl = url.substr(url.find('/'));
-
-  std::string portString = "443";
-  // check if hostUrl contains ':'
-  if (hostUrl.find(':') != std::string::npos) {
-    // split by ':'
-    std::string host = hostUrl.substr(0, hostUrl.find(':'));
-    portString = hostUrl.substr(hostUrl.find(':') + 1);
-    hostUrl = host;
-  }
-
-  BELL_LOG(info, "http_tls", "Connecting with %s", hostUrl.c_str());
-  BIO_set_conn_hostname(sbio, std::string(hostUrl + ":443").c_str());
+  BELL_LOG(info, "http_tls", "Connecting with %s", host.c_str());
+  BIO_set_conn_hostname(sbio, std::string(host + ":" + std::to_string(port)).c_str());
 
   out = BIO_new_fp(stdout, BIO_NOCLOSE);
   if (BIO_do_connect(sbio) <= 0) {
@@ -63,6 +50,10 @@ size_t bell::TLSSocket::write(uint8_t *buf, size_t len) {
   return BIO_write(sbio, buf, len);
 }
 
+size_t bell::TLSSocket::poll() {
+  return BIO_pending(sbio);
+}
+
 void bell::TLSSocket::close() {
   if (!isClosed) {
     BIO_free_all(sbio);

+ 15 - 5
components/spotify/cspot/cpp-reflection/README.md

@@ -1,7 +1,17 @@
-# Running
+Docker files
+------------
+
+This folder contains docker files that are used in testing nanopb automatically
+on various platforms.
+
+By default they take the newest master branch code from github.
+
+To build tests for a single target, use for example:
+
+    docker build ubuntu1804
+
+To build tests for all targets, use:
+
+    ./build_all.sh
 
-```sh
-$ go build -o protoc-gen-cpprefl . && protoc --plugin=protoc-gen-cpprefl=protoc-gen-cpprefl --cpprefl_out okon protos/*.proto --proto_path protos/
-```
 
-Will get protos from `protos/` and output to `out/protobuf.h`

+ 1 - 1
components/spotify/cspot/include/ConfigJSON.h

@@ -4,7 +4,7 @@
 #include <memory>
 #include <iostream>
 #include "FileHelper.h"
-#include "ProtoHelper.h"
+#include "protobuf/metadata.pb.h"
 
 class ConfigJSON
 {

+ 1 - 1
components/spotify/cspot/include/LoginBlob.h

@@ -5,7 +5,7 @@
 #include <memory>
 #include <iostream>
 #include "Crypto.h"
-#include "ProtoHelper.h"
+#include "protobuf/authentication.pb.h"
 
 class LoginBlob
 {

+ 4 - 2
components/spotify/cspot/include/MercuryManager.h

@@ -10,7 +10,6 @@
 #include "MercuryResponse.h"
 #include "Packet.h"
 #include "Utils.h"
-#include "ProtoHelper.h"
 #include "MercuryManager.h"
 #include "AudioChunk.h"
 #include "AudioChunkManager.h"
@@ -19,7 +18,8 @@
 #include "platform/WrappedSemaphore.h"
 #include "TimeProvider.h"
 #include "Session.h"
-
+#include <NanoPBHelper.h>
+#include "protobuf/mercury.pb.h"
 #include <stdint.h>
 #include <memory>
 
@@ -56,6 +56,7 @@ extern std::map<MercuryType, std::string> MercuryTypeMap;
 class MercuryManager : public bell::Task
 {
 private:
+  Header tempMercuryHeader;
   std::map<uint64_t, mercuryCallback> callbacks;
   std::mutex reconnectionMutex;
   std::mutex runningMutex;
@@ -76,6 +77,7 @@ private:
   void runTask();
 public:
   MercuryManager(std::unique_ptr<Session> session);
+  ~MercuryManager();
   voidCallback reconnectedCallback;
   uint16_t audioChunkSequence;
   std::shared_ptr<TimeProvider> timeProvider;

+ 3 - 1
components/spotify/cspot/include/MercuryResponse.h

@@ -6,7 +6,8 @@
 #include <string>
 #include <functional>
 #include <vector>
-#include "ProtoHelper.h"
+#include <NanoPBHelper.h>
+#include "protobuf/mercury.pb.h"
 #include "Utils.h"
 
 typedef std::vector<std::vector<uint8_t>> mercuryParts;
@@ -18,6 +19,7 @@ private:
     std::vector<uint8_t> data;
 public:
     MercuryResponse(std::vector<uint8_t> &data);
+    ~MercuryResponse();
     void decodeHeader();
     Header mercuryHeader;
     uint8_t flags;

+ 6 - 3
components/spotify/cspot/include/PlayerState.h

@@ -4,13 +4,14 @@
 #include <vector>
 #include <memory>
 #include <string>
-#include "ProtoHelper.h"
 #include "Utils.h"
 #include "TimeProvider.h"
 #include "ConstantParameters.h"
 #include "CspotAssert.h"
 #include "TrackReference.h"
 #include "ConfigJSON.h"
+#include <NanoPBHelper.h>
+#include "protobuf/spirc.pb.h"
 
 enum class PlaybackState {
     Playing,
@@ -30,8 +31,8 @@ private:
 
     void addCapability(CapabilityType typ, int intValue = -1, std::vector<std::string> stringsValue = std::vector<std::string>());
 public:
-    Frame innerFrame = Frame();
-    Frame remoteFrame = Frame();
+    Frame innerFrame;
+    Frame remoteFrame;
 
     /**
      * @brief Player state represents the current state of player.
@@ -41,6 +42,8 @@ public:
      * @param timeProvider synced time provider
      */
     PlayerState(std::shared_ptr<TimeProvider> timeProvider);
+    
+    ~PlayerState();
 
     /**
      * @brief Updates state according to current playback state.

+ 4 - 1
components/spotify/cspot/include/Session.h

@@ -16,7 +16,9 @@
 #include "Packet.h"
 #include "ConstantParameters.h"
 #include "Crypto.h"
-#include "ProtoHelper.h"
+#include "NanoPBHelper.h"
+#include "protobuf/authentication.pb.h"
+#include "protobuf/keyexchange.pb.h"
 
 #define SPOTIFY_VERSION 0x10800000000
 #define LOGIN_REQUEST_COMMAND 0xAB
@@ -38,6 +40,7 @@ private:
 
 public:
     Session();
+    ~Session();
     std::shared_ptr<ShannonConnection> shanConn;
     std::shared_ptr<LoginBlob> authBlob;
     void connect(std::unique_ptr<PlainConnection> connection);

+ 0 - 1
components/spotify/cspot/include/SpircController.h

@@ -6,7 +6,6 @@
 #include <functional>
 #include "Utils.h"
 #include "MercuryManager.h"
-#include "ProtoHelper.h"
 #include "Session.h"
 #include "PlayerState.h"
 #include "SpotifyTrack.h"

+ 3 - 2
components/spotify/cspot/include/SpotifyTrack.h

@@ -5,7 +5,6 @@
 #include <vector>
 #include <iostream>
 #include "MercuryManager.h"
-#include "ProtoHelper.h"
 #include "Utils.h"
 #include "MercuryResponse.h"
 #include <fstream>
@@ -13,6 +12,8 @@
 #include <functional>
 #include "ChunkedAudioStream.h"
 #include "TrackReference.h"
+#include "NanoPBHelper.h"
+#include "protobuf/metadata.pb.h"
 #include <cassert>
 
 struct TrackInfo {
@@ -33,7 +34,7 @@ private:
     void episodeInformationCallback(std::unique_ptr<MercuryResponse> response, uint32_t position_ms, bool isPaused);
     void requestAudioKey(std::vector<uint8_t> fileId, std::vector<uint8_t> trackId, int32_t trackDuration, uint32_t position_ms, bool isPaused);
     bool countryListContains(std::string countryList, std::string country);
-    bool canPlayTrack(std::vector<Restriction> &restrictions);
+    bool canPlayTrack();
     Track trackInfo;
     Episode episodeInfo;
 

+ 2 - 1
components/spotify/cspot/include/TrackReference.h

@@ -3,7 +3,8 @@
 
 #include <vector>
 #include "Utils.h"
-#include "ProtoHelper.h"
+#include "protobuf/spirc.pb.h"
+#include <NanoPBHelper.h>
 #include <iostream>
 #include <string>
 

+ 1 - 2
components/spotify/cspot/protocol/authentication.proto

@@ -62,5 +62,4 @@ message ClientResponseEncrypted {
     required LoginCredentials login_credentials = 0xa; 
     required SystemInfo system_info = 0x32; 
     optional string version_string = 0x46; 
-}
-
+}

+ 2 - 2
components/spotify/cspot/protocol/keyexchange.proto

@@ -24,7 +24,7 @@ message LoginCryptoHelloUnion {
 }
 
 
-enum Platform {
+enum Platform2 {
     PLATFORM_WIN32_X86 = 0x0;
     PLATFORM_OSX_X86 = 0x1;
     PLATFORM_LINUX_X86 = 0x2;
@@ -58,7 +58,7 @@ enum Cryptosuite {
 
 message BuildInfo {
     required Product product = 0xa; 
-    required Platform platform = 0x1e; 
+    required Platform2 platform = 0x1e; 
     required uint64 version = 0x28; 
 }
 

+ 8 - 8
components/spotify/cspot/src/ConfigJSON.cpp

@@ -33,16 +33,16 @@ bool ConfigJSON::load()
           auto bitrateObject = cJSON_GetObjectItemCaseSensitive(root, "bitrate");
           switch((uint16_t)cJSON_GetNumberValue(bitrateObject)){
             case 320:
-              this->format = AudioFormat::OGG_VORBIS_320;
+              this->format = AudioFormat_OGG_VORBIS_320;
               break;
             case 160:
-              this->format = AudioFormat::OGG_VORBIS_160;
+              this->format = AudioFormat_OGG_VORBIS_160;
               break;
             case 96:
-              this->format = AudioFormat::OGG_VORBIS_96;
+              this->format = AudioFormat_OGG_VORBIS_96;
               break;
             default:
-              this->format = AudioFormat::OGG_VORBIS_320;
+              this->format = AudioFormat_OGG_VORBIS_320;
               break;
           }
         }
@@ -59,7 +59,7 @@ bool ConfigJSON::load()
          // Set default values
          this->volume = 32767;
          this->deviceName = defaultDeviceName;
-         this->format = AudioFormat::OGG_VORBIS_160;
+         this->format = AudioFormat_OGG_VORBIS_160;
       }
       return true;
     }
@@ -76,13 +76,13 @@ bool ConfigJSON::save()
     obj["volume"] = this->volume;
     obj["deviceName"] = this->deviceName;
     switch(this->format){
-        case AudioFormat::OGG_VORBIS_320:
+        case AudioFormat_OGG_VORBIS_320:
             obj["bitrate"] = 320;
             break;
-        case AudioFormat::OGG_VORBIS_160:
+        case AudioFormat_OGG_VORBIS_160:
             obj["bitrate"] = 160;
             break;
-        case AudioFormat::OGG_VORBIS_96:
+        case AudioFormat_OGG_VORBIS_96:
             obj["bitrate"] = 96;
             break;
         default:

+ 1 - 1
components/spotify/cspot/src/LoginBlob.cpp

@@ -103,7 +103,7 @@ void LoginBlob::loadUserPass(const std::string &username, const std::string &pas
 {
     this->username = username;
     this->authData = std::vector<uint8_t>(password.begin(), password.end());
-    this->authType = static_cast<uint32_t>(AuthenticationType::AUTHENTICATION_USER_PASS);
+    this->authType = static_cast<uint32_t>(AuthenticationType_AUTHENTICATION_USER_PASS);
 }
 
 void LoginBlob::loadJson(const std::string &json)

+ 12 - 6
components/spotify/cspot/src/MercuryManager.cpp

@@ -11,6 +11,7 @@ std::map<MercuryType, std::string> MercuryTypeMap({
 
 MercuryManager::MercuryManager(std::unique_ptr<Session> session): bell::Task("mercuryManager", 6 * 1024, +1, 1)
 {
+    tempMercuryHeader = Header_init_default;
     this->timeProvider = std::make_shared<TimeProvider>();
     this->callbacks = std::map<uint64_t, mercuryCallback>();
     this->subscriptions = std::map<std::string, mercuryCallback>();
@@ -27,6 +28,11 @@ MercuryManager::MercuryManager(std::unique_ptr<Session> session): bell::Task("me
     };
 }
 
+MercuryManager::~MercuryManager()
+{
+    pbFree(Header_fields, &tempMercuryHeader);
+}
+
 bool MercuryManager::timeoutHandler()
 {
     if (!isRunning) return true;
@@ -257,9 +263,10 @@ void MercuryManager::updateQueue() {
             {
                 auto response = std::make_unique<MercuryResponse>(packet->data);
 
-                if (this->subscriptions.count(response->mercuryHeader.uri.value()) > 0)
+                auto uri = std::string(response->mercuryHeader.uri);
+                if (this->subscriptions.count(uri) > 0)
                 {
-                    this->subscriptions[response->mercuryHeader.uri.value()](std::move(response));
+                    this->subscriptions[uri](std::move(response));
                     //this->subscriptions.erase(std::string(response->mercuryHeader.uri));
                 }
                 break;
@@ -288,9 +295,8 @@ uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCal
     // Construct mercury header
 
     CSPOT_LOG(debug, "executing MercuryType %s", MercuryTypeMap[method].c_str());
-    Header mercuryHeader;
-    mercuryHeader.uri = uri;
-    mercuryHeader.method = MercuryTypeMap[method];
+    tempMercuryHeader.uri = (char *)(uri.c_str());
+    tempMercuryHeader.method = (char *)(MercuryTypeMap[method].c_str());
 
     // GET and SEND are actually the same. Therefore the override
     // The difference between them is only in header's method
@@ -299,7 +305,7 @@ uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCal
         method = MercuryType::SEND;
     }
 
-    auto headerBytes = encodePb(mercuryHeader);
+    auto headerBytes = pbEncode(Header_fields, &tempMercuryHeader);
 
     // Register a subscription when given method is called
     if (method == MercuryType::SUB)

+ 6 - 1
components/spotify/cspot/src/MercuryResponse.cpp

@@ -3,10 +3,15 @@
 MercuryResponse::MercuryResponse(std::vector<uint8_t> &data)
 {
     // this->mercuryHeader = std::make_unique<Header>();
+    this->mercuryHeader = Header_init_default;
     this->parts = mercuryParts(0);
     this->parseResponse(data);
 }
 
+MercuryResponse::~MercuryResponse() {
+    pbFree(Header_fields, &mercuryHeader);
+}
+
 void MercuryResponse::parseResponse(std::vector<uint8_t> &data)
 {
     auto sequenceLength = ntohs(extract<uint16_t>(data, 0));
@@ -29,5 +34,5 @@ void MercuryResponse::parseResponse(std::vector<uint8_t> &data)
         pos += 2 + partSize;
     }
 
-    this->mercuryHeader = decodePb<Header>(headerBytes);
+    pbDecode(this->mercuryHeader, Header_fields, headerBytes);
 }

+ 113 - 71
components/spotify/cspot/src/PlayerState.cpp

@@ -5,35 +5,56 @@
 PlayerState::PlayerState(std::shared_ptr<TimeProvider> timeProvider)
 {
     this->timeProvider = timeProvider;
+    innerFrame = {};
+    remoteFrame = {};
 
     // Prepare default state
-    innerFrame.state.emplace();
-    innerFrame.state->position_ms = 0;
-    innerFrame.state->status = PlayStatus::kPlayStatusStop;
-    innerFrame.state->position_measured_at = 0;
-    innerFrame.state->shuffle = false;
-    innerFrame.state->repeat = false;
-
-    innerFrame.device_state.emplace();
-    innerFrame.device_state->sw_version = swVersion;
-    innerFrame.device_state->is_active = false;
-    innerFrame.device_state->can_play = true;
-    innerFrame.device_state->volume = configMan->volume;
-    innerFrame.device_state->name = configMan->deviceName;
+    innerFrame.state.has_position_ms = true;
+    innerFrame.state.position_ms = 0;
+
+    innerFrame.state.status = PlayStatus_kPlayStatusStop;
+    innerFrame.state.has_status = true;
+
+    innerFrame.state.position_measured_at = 0;
+    innerFrame.state.has_position_measured_at = true;
+
+    innerFrame.state.shuffle = false;
+    innerFrame.state.has_shuffle = true;
+
+    innerFrame.state.repeat = false;
+    innerFrame.state.has_repeat = true;
+
+    innerFrame.device_state.sw_version = (char*) swVersion;
+
+    innerFrame.device_state.is_active = false;
+    innerFrame.device_state.has_is_active = true;
+    
+    innerFrame.device_state.can_play = true;
+    innerFrame.device_state.has_can_play = true;
+
+    innerFrame.device_state.volume = configMan->volume;
+    innerFrame.device_state.has_volume = true;
+
+    innerFrame.device_state.name = (char*) configMan->deviceName.c_str();
 
     // Prepare player's capabilities
-    innerFrame.device_state->capabilities = std::vector<Capability>();
-    addCapability(CapabilityType::kCanBePlayer, 1);
-    addCapability(CapabilityType::kDeviceType, 4);
-    addCapability(CapabilityType::kGaiaEqConnectId, 1);
-    addCapability(CapabilityType::kSupportsLogout, 0);
-    addCapability(CapabilityType::kIsObservable, 1);
-    addCapability(CapabilityType::kVolumeSteps, 64);
-    addCapability(CapabilityType::kSupportedContexts, -1,
+    addCapability(CapabilityType_kCanBePlayer, 1);
+    addCapability(CapabilityType_kDeviceType, 4);
+    addCapability(CapabilityType_kGaiaEqConnectId, 1);
+    addCapability(CapabilityType_kSupportsLogout, 0);
+    addCapability(CapabilityType_kIsObservable, 1);
+    addCapability(CapabilityType_kVolumeSteps, 64);
+    addCapability(CapabilityType_kSupportedContexts, -1,
                   std::vector<std::string>({"album", "playlist", "search", "inbox",
                                             "toplist", "starred", "publishedstarred", "track"}));
-    addCapability(CapabilityType::kSupportedTypes, -1,
+    addCapability(CapabilityType_kSupportedTypes, -1,
                   std::vector<std::string>({"audio/local", "audio/track", "audio/episode", "local", "track"}));
+    innerFrame.device_state.capabilities_count = 8;
+}
+
+PlayerState::~PlayerState() {
+    pbFree(Frame_fields, &innerFrame);
+    pbFree(Frame_fields, &remoteFrame);
 }
 
 void PlayerState::setPlaybackState(const PlaybackState state)
@@ -42,38 +63,40 @@ void PlayerState::setPlaybackState(const PlaybackState state)
     {
     case PlaybackState::Loading:
         // Prepare the playback at position 0
-        innerFrame.state->status = PlayStatus::kPlayStatusPause;
-        innerFrame.state->position_ms = 0;
-        innerFrame.state->position_measured_at = timeProvider->getSyncedTimestamp();
+        innerFrame.state.status = PlayStatus_kPlayStatusPause;
+        innerFrame.state.position_ms = 0;
+        innerFrame.state.position_measured_at = timeProvider->getSyncedTimestamp();
         break;
     case PlaybackState::Playing:
-        innerFrame.state->status = PlayStatus::kPlayStatusPlay;
-        innerFrame.state->position_measured_at = timeProvider->getSyncedTimestamp();
+        innerFrame.state.status = PlayStatus_kPlayStatusPlay;
+        innerFrame.state.position_measured_at = timeProvider->getSyncedTimestamp();
         break;
     case PlaybackState::Stopped:
         break;
     case PlaybackState::Paused:
         // Update state and recalculate current song position
-        innerFrame.state->status = PlayStatus::kPlayStatusPause;
-        uint32_t diff = timeProvider->getSyncedTimestamp() - innerFrame.state->position_measured_at.value();
-        this->updatePositionMs(innerFrame.state->position_ms.value() + diff);
+        innerFrame.state.status = PlayStatus_kPlayStatusPause;
+        uint32_t diff = timeProvider->getSyncedTimestamp() - innerFrame.state.position_measured_at;
+        this->updatePositionMs(innerFrame.state.position_ms + diff);
         break;
     }
 }
 
 bool PlayerState::isActive()
 {
-    return innerFrame.device_state->is_active.value();
+    return innerFrame.device_state.is_active;
 }
 
 bool PlayerState::nextTrack()
 {
-    innerFrame.state->playing_track_index.value()++;
+    if (innerFrame.state.repeat) return true;
+
+    innerFrame.state.playing_track_index++;
 
-    if (innerFrame.state->playing_track_index >= innerFrame.state->track.size())
+    if (innerFrame.state.playing_track_index >= innerFrame.state.track_count)
     {
-        innerFrame.state->playing_track_index = 0;
-        if (!innerFrame.state->repeat)
+        innerFrame.state.playing_track_index = 0;
+        if (!innerFrame.state.repeat)
         {
             setPlaybackState(PlaybackState::Paused);
             return false;
@@ -85,44 +108,46 @@ bool PlayerState::nextTrack()
 
 void PlayerState::prevTrack()
 {
-    if (innerFrame.state->playing_track_index > 0)
+    if (innerFrame.state.playing_track_index > 0)
     {
-        innerFrame.state->playing_track_index.value()--;
+        innerFrame.state.playing_track_index--;
     }
-    else if (innerFrame.state->repeat)
+    else if (innerFrame.state.repeat)
     {
-        innerFrame.state->playing_track_index = innerFrame.state->track.size() - 1;
+        innerFrame.state.playing_track_index = innerFrame.state.track_count - 1;
     }
 }
 
 void PlayerState::setActive(bool isActive)
 {
-    innerFrame.device_state->is_active = isActive;
+    innerFrame.device_state.is_active = isActive;
     if (isActive)
     {
-        innerFrame.device_state->became_active_at = timeProvider->getSyncedTimestamp();
+        innerFrame.device_state.became_active_at = timeProvider->getSyncedTimestamp();
+        innerFrame.device_state.has_became_active_at = true;
     }
 }
 
 void PlayerState::updatePositionMs(uint32_t position)
 {
-    innerFrame.state->position_ms = position;
-    innerFrame.state->position_measured_at = timeProvider->getSyncedTimestamp();
+    innerFrame.state.position_ms = position;
+    innerFrame.state.position_measured_at = timeProvider->getSyncedTimestamp();
 }
 void PlayerState::updateTracks()
 {
-    CSPOT_LOG(info, "---- Track count %d", remoteFrame.state->track.size());
+    CSPOT_LOG(info, "---- Track count %d", remoteFrame.state.track_count);
     // innerFrame.state->context_uri = remoteFrame.state->context_uri == nullptr ? nullptr : strdup(otherFrame->state->context_uri);
+    std::copy(std::begin(remoteFrame.state.track), std::end(remoteFrame.state.track), std::begin(innerFrame.state.track));
+    innerFrame.state.track_count = remoteFrame.state.track_count;
+    innerFrame.state.has_playing_track_index = true;
+    innerFrame.state.playing_track_index = remoteFrame.state.playing_track_index;
 
-    innerFrame.state->track = remoteFrame.state->track;
-    innerFrame.state->playing_track_index = remoteFrame.state->playing_track_index;
-
-    if (remoteFrame.state->repeat.value())
+    if (remoteFrame.state.repeat)
     {
         setRepeat(true);
     }
 
-    if (remoteFrame.state->shuffle.value())
+    if (remoteFrame.state.shuffle)
     {
         setShuffle(true);
     }
@@ -130,70 +155,87 @@ void PlayerState::updateTracks()
 
 void PlayerState::setVolume(uint32_t volume)
 {
-    innerFrame.device_state->volume = volume;
+    innerFrame.device_state.volume = volume;
     configMan->volume = volume;
     configMan->save();
 }
 
 void PlayerState::setShuffle(bool shuffle)
 {
-    innerFrame.state->shuffle = shuffle;
+    innerFrame.state.shuffle = shuffle;
     if (shuffle)
     {
         // Put current song at the begining
-        auto tmp = innerFrame.state->track.at(0);
-        innerFrame.state->track.at(0) = innerFrame.state->track.at(innerFrame.state->playing_track_index.value());
-        innerFrame.state->track.at(innerFrame.state->playing_track_index.value()) = tmp;
+        auto tmp = innerFrame.state.track[0];
+        innerFrame.state.track[0] = innerFrame.state.track[innerFrame.state.playing_track_index];
+        innerFrame.state.track[innerFrame.state.playing_track_index] = tmp;
 
         // Shuffle current tracks
-        for (int x = 1; x < innerFrame.state->track.size() - 1; x++)
+        for (int x = 1; x < innerFrame.state.track_count - 1; x++)
         {
-            auto j = x + (std::rand() % (innerFrame.state->track.size() - x));
-            tmp = innerFrame.state->track.at(j);
-            innerFrame.state->track.at(j) = innerFrame.state->track.at(x);
-            innerFrame.state->track.at(x) = tmp;
+            auto j = x + (std::rand() % (innerFrame.state.track_count - x));
+            tmp = innerFrame.state.track[j];
+            innerFrame.state.track[j] = innerFrame.state.track[x];
+            innerFrame.state.track[x] = tmp;
         }
-        innerFrame.state->playing_track_index = 0;
+        innerFrame.state.playing_track_index = 0;
     }
 }
 
 void PlayerState::setRepeat(bool repeat)
 {
-    innerFrame.state->repeat = repeat;
+    innerFrame.state.repeat = repeat;
 }
 
 std::shared_ptr<TrackReference> PlayerState::getCurrentTrack()
 {
     // Wrap current track in a class
-    return std::make_shared<TrackReference>(&innerFrame.state->track.at(innerFrame.state->playing_track_index.value()));
+    return std::make_shared<TrackReference>(&innerFrame.state.track[innerFrame.state.playing_track_index]);
 }
 
 std::vector<uint8_t> PlayerState::encodeCurrentFrame(MessageType typ)
 {
     // Prepare current frame info
     innerFrame.version = 1;
-    innerFrame.ident = deviceId;
+    innerFrame.ident = (char *) deviceId;
     innerFrame.seq_nr = this->seqNum;
-    innerFrame.protocol_version = protocolVersion;
+    innerFrame.protocol_version = (char*) protocolVersion;
     innerFrame.typ = typ;
     innerFrame.state_update_id = timeProvider->getSyncedTimestamp();
+    innerFrame.has_version = true;
+    innerFrame.has_seq_nr = true;
+    innerFrame.recipient_count = 0;
+    innerFrame.has_state = true;
+    innerFrame.has_device_state = true;
+    innerFrame.has_typ = true;
+    innerFrame.has_state_update_id = true;
 
     this->seqNum += 1;
-    auto fram = encodePb(innerFrame);
-    return fram;
+    return pbEncode(Frame_fields, &innerFrame);
 }
 
 // Wraps messy nanopb setters. @TODO: find a better way to handle this
 void PlayerState::addCapability(CapabilityType typ, int intValue, std::vector<std::string> stringValue)
 {
-    auto capability = Capability();
-    capability.typ = typ;
+    innerFrame.device_state.capabilities[capabilityIndex].has_typ = true;
+    this->innerFrame.device_state.capabilities[capabilityIndex].typ = typ;
 
     if (intValue != -1)
     {
-        capability.intValue = std::vector<int64_t>({intValue});
+        this->innerFrame.device_state.capabilities[capabilityIndex].intValue[0] = intValue;
+        this->innerFrame.device_state.capabilities[capabilityIndex].intValue_count = 1;
+    }
+    else
+    {
+        this->innerFrame.device_state.capabilities[capabilityIndex].intValue_count = 0;
+    }
+
+    for (int x = 0; x < stringValue.size(); x++)
+    {
+        stringValue[x].copy(this->innerFrame.device_state.capabilities[capabilityIndex].stringValue[x], stringValue[x].size());
+        this->innerFrame.device_state.capabilities[capabilityIndex].stringValue[x][stringValue[x].size()] = '\0';
     }
 
-    capability.stringValue = stringValue;
-    innerFrame.device_state->capabilities.push_back(capability);
+    this->innerFrame.device_state.capabilities[capabilityIndex].stringValue_count = stringValue.size();
+    this->capabilityIndex += 1;
 }

+ 51 - 28
components/spotify/cspot/src/Session.cpp

@@ -6,11 +6,24 @@ using random_bytes_engine = std::independent_bits_engine<std::default_random_eng
 
 Session::Session()
 {
+    this->clientHello = ClientHello_init_default;
+    this->apResponse = APResponseMessage_init_default;
+    this->authRequest = ClientResponseEncrypted_init_default;
+    this->clientResPlaintext = ClientResponsePlaintext_init_default;
+
     // Generates the public and priv key
     this->crypto = std::make_unique<Crypto>();
     this->shanConn = std::make_shared<ShannonConnection>();
 }
 
+Session::~Session()
+{
+    pbFree(ClientHello_fields, &clientHello);
+    pbFree(APResponseMessage_fields, &apResponse);
+    pbFree(ClientResponseEncrypted_fields, &authRequest);
+    pbFree(ClientResponsePlaintext_fields, &clientResPlaintext);
+}
+
 void Session::connect(std::unique_ptr<PlainConnection> connection)
 {
     this->conn = std::move(connection);
@@ -37,16 +50,16 @@ std::vector<uint8_t> Session::authenticate(std::shared_ptr<LoginBlob> blob)
     authBlob = blob;
 
     // prepare authentication request proto
-    authRequest.login_credentials.username = blob->username;
-    authRequest.login_credentials.auth_data = blob->authData;
-    authRequest.login_credentials.typ = static_cast<AuthenticationType>(blob->authType);
-    authRequest.system_info.cpu_family = CpuFamily::CPU_UNKNOWN;
-    authRequest.system_info.os = Os::OS_UNKNOWN;
-    authRequest.system_info.system_information_string = std::string(informationString);
-    authRequest.system_info.device_id = std::string(deviceId);
-    authRequest.version_string = std::string(versionString);
+    authRequest.login_credentials.username = (char *)(blob->username.c_str());
+    authRequest.login_credentials.auth_data = vectorToPbArray(blob->authData);
+    authRequest.login_credentials.typ = (AuthenticationType) blob->authType;
+    authRequest.system_info.cpu_family = CpuFamily_CPU_UNKNOWN;
+    authRequest.system_info.os = Os_OS_UNKNOWN;
+    authRequest.system_info.system_information_string = (char *)informationString;
+    authRequest.system_info.device_id = (char *)deviceId;
+    authRequest.version_string = (char *)versionString;
 
-    auto data = encodePb(authRequest);
+    auto data = pbEncode(ClientResponseEncrypted_fields, &authRequest);
 
     // Send login request
     this->shanConn->sendPacket(LOGIN_REQUEST_COMMAND, data);
@@ -82,11 +95,11 @@ void Session::processAPHelloResponse(std::vector<uint8_t> &helloPacket)
     CSPOT_LOG(debug, "Received AP hello response");
     // Decode the response
     auto skipSize = std::vector<uint8_t>(data.begin() + 4, data.end());
-    apResponse = decodePb<APResponseMessage>(skipSize);
+    pbDecode(apResponse, APResponseMessage_fields, skipSize);
 
-    auto kkEy = apResponse.challenge->login_crypto_challenge.diffie_hellman->gs;
+    auto diffieKey = std::vector<uint8_t>(apResponse.challenge.login_crypto_challenge.diffie_hellman.gs, apResponse.challenge.login_crypto_challenge.diffie_hellman.gs + 96);
     // Compute the diffie hellman shared key based on the response
-    auto sharedKey = this->crypto->dhCalculateShared(kkEy);
+    auto sharedKey = this->crypto->dhCalculateShared(diffieKey);
 
     // Init client packet + Init server packets are required for the hmac challenge
     data.insert(data.begin(), helloPacket.begin(), helloPacket.end());
@@ -106,11 +119,14 @@ void Session::processAPHelloResponse(std::vector<uint8_t> &helloPacket)
     auto lastVec = std::vector<uint8_t>(resultData.begin(), resultData.begin() + 0x14);
 
     // Digest generated!
-    clientResPlaintext.login_crypto_response = {};
-    clientResPlaintext.login_crypto_response.diffie_hellman.emplace();
-    clientResPlaintext.login_crypto_response.diffie_hellman->hmac = crypto->sha1HMAC(lastVec, data);
+    auto digest = crypto->sha1HMAC(lastVec, data);
+    clientResPlaintext.login_crypto_response.has_diffie_hellman = true;
+
+    std::copy(digest.begin(),
+              digest.end(),
+              clientResPlaintext.login_crypto_response.diffie_hellman.hmac);
 
-    auto resultPacket = encodePb(clientResPlaintext);
+    auto resultPacket = pbEncode(ClientResponsePlaintext_fields, &clientResPlaintext);
 
     auto emptyPrefix = std::vector<uint8_t>(0);
 
@@ -136,20 +152,27 @@ std::vector<uint8_t> Session::sendClientHelloRequest()
     this->crypto->dhInit();
 
     // Copy the public key into diffiehellman hello packet
-    clientHello.login_crypto_hello.diffie_hellman.emplace();
-    clientHello.feature_set.emplace();
-    clientHello.login_crypto_hello.diffie_hellman->gc = this->crypto->publicKey;
-    clientHello.login_crypto_hello.diffie_hellman->server_keys_known = 1;
-    clientHello.build_info.product = Product::PRODUCT_PARTNER;
-    clientHello.build_info.platform = Platform::PLATFORM_LINUX_X86;
-    clientHello.build_info.version = 112800721;
-    clientHello.feature_set->autoupdate2 = true;
-    clientHello.cryptosuites_supported = std::vector<Cryptosuite>({Cryptosuite::CRYPTO_SUITE_SHANNON});
-    clientHello.padding = std::vector<uint8_t>({0x1E});
+    std::copy(this->crypto->publicKey.begin(),
+              this->crypto->publicKey.end(),
+              clientHello.login_crypto_hello.diffie_hellman.gc);
+
+    clientHello.login_crypto_hello.diffie_hellman.server_keys_known = 1;
+    clientHello.build_info.product = Product_PRODUCT_PARTNER;
+    clientHello.build_info.platform = Platform2_PLATFORM_LINUX_X86;
+    clientHello.build_info.version = SPOTIFY_VERSION;
+    clientHello.feature_set.autoupdate2 = true;
+    clientHello.cryptosuites_supported[0] = Cryptosuite_CRYPTO_SUITE_SHANNON;
+    clientHello.padding[0] = 0x1E;
+
+    clientHello.has_feature_set = true;
+    clientHello.login_crypto_hello.has_diffie_hellman = true;
+    clientHello.has_padding = true;
+    clientHello.has_feature_set = true;
 
     // Generate the random nonce
-    clientHello.client_nonce = crypto->generateVectorWithRandomData(16);
-    auto vecData = encodePb(clientHello);
+    auto nonce = crypto->generateVectorWithRandomData(16);
+    std::copy(nonce.begin(), nonce.end(), clientHello.client_nonce);
+    auto vecData = pbEncode(ClientHello_fields, &clientHello);
     auto prefix = std::vector<uint8_t>({0x00, 0x04});
     return this->conn->sendPrefixPacket(prefix, vecData);
 }

+ 3 - 1
components/spotify/cspot/src/Shannon.cpp

@@ -5,6 +5,8 @@
 // #define NDEBUG
 #include <assert.h>
 
+using std::size_t;
+
 static inline uint32_t rotl(uint32_t n, unsigned int c)
 {
     const unsigned int mask = (CHAR_BIT * sizeof(n) - 1); // assumes width is a power of 2.
@@ -440,4 +442,4 @@ void Shannon::finish(std::vector<uint8_t> &bufVec)
             break;
         }
     }
-}
+}

+ 28 - 29
components/spotify/cspot/src/SpircController.cpp

@@ -25,7 +25,7 @@ SpircController::SpircController(std::shared_ptr<MercuryManager> manager,
 void SpircController::subscribe() {
     mercuryCallback responseLambda = [=](std::unique_ptr<MercuryResponse> res) {
         // this->trackInformationCallback(std::move(res));
-        sendCmd(MessageType::kMessageTypeHello);
+        sendCmd(MessageType_kMessageTypeHello);
         CSPOT_LOG(debug, "Sent kMessageTypeHello!");
     };
     mercuryCallback subLambda = [=](std::unique_ptr<MercuryResponse> res) {
@@ -60,7 +60,7 @@ void SpircController::disconnect(void) {
 }
 
 void SpircController::playToggle() {
-    if (state->innerFrame.state->status.value() == PlayStatus::kPlayStatusPause) {
+    if (state->innerFrame.state.status == PlayStatus_kPlayStatusPause) {
         setPause(false);
     } else {
         setPause(true);
@@ -68,8 +68,8 @@ void SpircController::playToggle() {
 }
 
 void SpircController::adjustVolume(int by) {
-    if (state->innerFrame.device_state->volume.has_value()) {
-        int volume = state->innerFrame.device_state->volume.value() + by;
+    if (state->innerFrame.device_state.has_volume) {
+        int volume = state->innerFrame.device_state.volume + by;
         if (volume < 0) volume = 0;
         else if (volume > MAX_VOLUME) volume = MAX_VOLUME;
         setVolume(volume);
@@ -103,45 +103,45 @@ void SpircController::prevSong() {
 }
 
 void SpircController::handleFrame(std::vector<uint8_t> &data) {
-    state->remoteFrame = decodePb<Frame>(data);
+    pbDecode(state->remoteFrame, Frame_fields, data);
 
-    switch (state->remoteFrame.typ.value()) {
-    case MessageType::kMessageTypeNotify: {
+    switch (state->remoteFrame.typ) {
+    case MessageType_kMessageTypeNotify: {
         CSPOT_LOG(debug, "Notify frame");
         // Pause the playback if another player took control
         if (state->isActive() &&
-            state->remoteFrame.device_state->is_active.value()) {
+            state->remoteFrame.device_state.is_active) {
             disconnect();
         }
         break;
     }
-    case MessageType::kMessageTypeSeek: {
+    case MessageType_kMessageTypeSeek: {
         CSPOT_LOG(debug, "Seek command");
-        sendEvent(CSpotEventType::SEEK, (int) state->remoteFrame.position.value());
-        state->updatePositionMs(state->remoteFrame.position.value());
-        this->player->seekMs(state->remoteFrame.position.value());
+        sendEvent(CSpotEventType::SEEK, (int) state->remoteFrame.position);
+        state->updatePositionMs(state->remoteFrame.position);
+        this->player->seekMs(state->remoteFrame.position);
         notify();
         break;
     }
-    case MessageType::kMessageTypeVolume:
-        sendEvent(CSpotEventType::VOLUME, (int) state->remoteFrame.volume.value());
-        setVolume(state->remoteFrame.volume.value());
+    case MessageType_kMessageTypeVolume:
+        sendEvent(CSpotEventType::VOLUME, (int) state->remoteFrame.volume);
+        setVolume(state->remoteFrame.volume);
         break;
-    case MessageType::kMessageTypePause:
+    case MessageType_kMessageTypePause:
         setPause(true);
         break;
-    case MessageType::kMessageTypePlay:
+    case MessageType_kMessageTypePlay:
         setPause(false);
         break;
-    case MessageType::kMessageTypeNext:
+    case MessageType_kMessageTypeNext:
         sendEvent(CSpotEventType::NEXT);
         nextSong();
         break;
-    case MessageType::kMessageTypePrev:
+    case MessageType_kMessageTypePrev:
         sendEvent(CSpotEventType::PREV);
         prevSong();
         break;
-    case MessageType::kMessageTypeLoad: {
+    case MessageType_kMessageTypeLoad: {
         CSPOT_LOG(debug, "Load frame!");
 
         state->setActive(true);
@@ -154,25 +154,25 @@ void SpircController::handleFrame(std::vector<uint8_t> &data) {
 
         // bool isPaused = (state->remoteFrame.state->status.value() ==
         // PlayStatus::kPlayStatusPlay) ? false : true;
-        loadTrack(state->remoteFrame.state->position_ms.value(), false);
-        state->updatePositionMs(state->remoteFrame.state->position_ms.value());
+        loadTrack(state->remoteFrame.state.position_ms, false);
+        state->updatePositionMs(state->remoteFrame.state.position_ms);
 
         this->notify();
         break;
     }
-    case MessageType::kMessageTypeReplace: {
+    case MessageType_kMessageTypeReplace: {
         CSPOT_LOG(debug, "Got replace frame!");
         break;
     }
-    case MessageType::kMessageTypeShuffle: {
+    case MessageType_kMessageTypeShuffle: {
         CSPOT_LOG(debug, "Got shuffle frame");
-        state->setShuffle(state->remoteFrame.state->shuffle.value());
+        state->setShuffle(state->remoteFrame.state.shuffle);
         this->notify();
         break;
     }
-    case MessageType::kMessageTypeRepeat: {
+    case MessageType_kMessageTypeRepeat: {
         CSPOT_LOG(debug, "Got repeat frame");
-        state->setRepeat(state->remoteFrame.state->repeat.value());
+        state->setRepeat(state->remoteFrame.state.repeat);
         this->notify();
         break;
     }
@@ -194,7 +194,7 @@ void SpircController::loadTrack(uint32_t position_ms, bool isPaused) {
 }
 
 void SpircController::notify() {
-    this->sendCmd(MessageType::kMessageTypeNotify);
+    this->sendCmd(MessageType_kMessageTypeNotify);
 }
 
 void SpircController::sendEvent(CSpotEventType eventType, std::variant<TrackInfo, int, bool> data) {
@@ -218,7 +218,6 @@ void SpircController::setEventHandler(cspotEventHandler callback) {
             info.imageUrl = track.imageUrl;
             info.name = track.name;
             info.duration = track.duration;
-
             this->sendEvent(CSpotEventType::TRACK_INFO, info);
     });
 }

+ 32 - 27
components/spotify/cspot/src/SpotifyTrack.cpp

@@ -10,6 +10,8 @@ SpotifyTrack::SpotifyTrack(std::shared_ptr<MercuryManager> manager, std::shared_
 {
     this->manager = manager;
     this->fileId = std::vector<uint8_t>();
+    episodeInfo = Episode_init_default;
+    trackInfo = Track_init_default;
 
     mercuryCallback trackResponseLambda = [=](std::unique_ptr<MercuryResponse> res) {
         this->trackInformationCallback(std::move(res), position_ms, isPaused);
@@ -33,6 +35,8 @@ SpotifyTrack::~SpotifyTrack()
 {
     this->manager->unregisterMercuryCallback(this->reqSeqNum);
     this->manager->freeAudioKeyCallback();
+    pbFree(Track_fields, &this->trackInfo);
+    pbFree(Episode_fields, &this->episodeInfo);
 }
 
 bool SpotifyTrack::countryListContains(std::string countryList, std::string country)
@@ -47,18 +51,18 @@ bool SpotifyTrack::countryListContains(std::string countryList, std::string coun
     return false;
 }
 
-bool SpotifyTrack::canPlayTrack(std::vector<Restriction>& restrictions)
+bool SpotifyTrack::canPlayTrack()
 {
-    for (int x = 0; x < restrictions.size(); x++)
+    for (int x = 0; x < trackInfo.restriction_count; x++)
     {
-        if (restrictions[x].countries_allowed.has_value())
+        if (trackInfo.restriction[x].countries_allowed != nullptr)
         {
-            return countryListContains(restrictions[x].countries_allowed.value(), manager->countryCode);
+            return countryListContains(std::string(trackInfo.restriction[x].countries_allowed), manager->countryCode);
         }
 
-        if (restrictions[x].countries_forbidden.has_value())
+        if (trackInfo.restriction[x].countries_forbidden != nullptr)
         {
-            return !countryListContains(restrictions[x].countries_forbidden.value(), manager->countryCode);
+            return !countryListContains(std::string(trackInfo.restriction[x].countries_forbidden), manager->countryCode);
         }
     }
 
@@ -71,48 +75,49 @@ void SpotifyTrack::trackInformationCallback(std::unique_ptr<MercuryResponse> res
         return;
     CSPOT_ASSERT(response->parts.size() > 0, "response->parts.size() must be greater than 0");
 
-    trackInfo = decodePb<Track>(response->parts[0]);
+    pbDecode(trackInfo, Track_fields, response->parts[0]);
 
-    CSPOT_LOG(info, "Track name: %s", trackInfo.name.value().c_str());
-        CSPOT_LOG(info, "Track duration: %d", trackInfo.duration.value());
-
-    CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction.size());
+    CSPOT_LOG(info, "Track name: %s", trackInfo.name);
+    CSPOT_LOG(info, "Track duration: %d", trackInfo.duration);
+    CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction_count);
     int altIndex = 0;
-    while (!canPlayTrack(trackInfo.restriction))
+    while (!canPlayTrack())
     {
         trackInfo.restriction = trackInfo.alternative[altIndex].restriction;
+        trackInfo.restriction_count = trackInfo.alternative[altIndex].restriction_count;
         trackInfo.gid = trackInfo.alternative[altIndex].gid;
         trackInfo.file = trackInfo.alternative[altIndex].file;
         altIndex++;
         CSPOT_LOG(info, "Trying alternative %d", altIndex);
     }
-    auto trackId = trackInfo.gid.value();
+    auto trackId = pbArrayToVector(trackInfo.gid);
     this->fileId = std::vector<uint8_t>();
 
-    for (int x = 0; x < trackInfo.file.size(); x++)
+    for (int x = 0; x < trackInfo.file_count; x++)
     {
         if (trackInfo.file[x].format == configMan->format)
         {
-            this->fileId = trackInfo.file[x].file_id.value();
+            this->fileId = pbArrayToVector(trackInfo.file[x].file_id);
             break; // If file found stop searching
         }
     }
 
     if (trackInfoReceived != nullptr)
     {
+        auto imageId = pbArrayToVector(trackInfo.album.cover_group.image[0].file_id);
         TrackInfo simpleTrackInfo = {
-            .name = trackInfo.name.value(),
-            .album = trackInfo.album.value().name.value(),
-            .artist = trackInfo.artist[0].name.value(),
-            .imageUrl = "https://i.scdn.co/image/" + bytesToHexString(trackInfo.album.value().cover_group.value().image[0].file_id.value()),
-            .duration = trackInfo.duration.value(),
+            .name = std::string(trackInfo.name),
+            .album = std::string(trackInfo.album.name),
+            .artist = std::string(trackInfo.artist[0].name),
+            .imageUrl = "https://i.scdn.co/image/" + bytesToHexString(imageId),
+            .duration = trackInfo.duration,
 
         };
 
         trackInfoReceived(simpleTrackInfo);
     }
 
-    this->requestAudioKey(this->fileId, trackId, trackInfo.duration.value(), position_ms, isPaused);
+    this->requestAudioKey(this->fileId, trackId, trackInfo.duration, position_ms, isPaused);
 }
 
 void SpotifyTrack::episodeInformationCallback(std::unique_ptr<MercuryResponse> response, uint32_t position_ms, bool isPaused)
@@ -121,23 +126,23 @@ void SpotifyTrack::episodeInformationCallback(std::unique_ptr<MercuryResponse> r
         return;
     CSPOT_LOG(debug, "Got to episode");
     CSPOT_ASSERT(response->parts.size() > 0, "response->parts.size() must be greater than 0");
-    episodeInfo = decodePb<Episode>(response->parts[0]);
+    pbDecode(episodeInfo, Episode_fields, response->parts[0]);
 
-    CSPOT_LOG(info, "--- Episode name: %s", episodeInfo.name.value().c_str());
+    CSPOT_LOG(info, "--- Episode name: %s", episodeInfo.name);
 
     this->fileId = std::vector<uint8_t>();
 
     // TODO: option to set file quality
-    for (int x = 0; x < episodeInfo.audio.size(); x++)
+    for (int x = 0; x < episodeInfo.audio_count; x++)
     {
-        if (episodeInfo.audio[x].format == AudioFormat::OGG_VORBIS_96)
+        if (episodeInfo.audio[x].format == AudioFormat_OGG_VORBIS_96)
         {
-            this->fileId = episodeInfo.audio[x].file_id.value();
+            this->fileId = pbArrayToVector(episodeInfo.audio[x].file_id);
             break; // If file found stop searching
         }
     }
 
-    this->requestAudioKey(episodeInfo.gid.value(), this->fileId, episodeInfo.duration.value(), position_ms, isPaused);
+    this->requestAudioKey(pbArrayToVector(episodeInfo.gid), this->fileId, episodeInfo.duration, position_ms, isPaused);
 }
 
 void SpotifyTrack::requestAudioKey(std::vector<uint8_t> fileId, std::vector<uint8_t> trackId, int32_t trackDuration, uint32_t position_ms, bool isPaused)

+ 5 - 4
components/spotify/cspot/src/TrackReference.cpp

@@ -3,13 +3,13 @@
 
 TrackReference::TrackReference(TrackRef *ref)
 {
-    if (ref->gid.has_value())
+    if (ref->gid != nullptr)
     {
-        gid = ref->gid.value();
+        gid = pbArrayToVector(ref->gid);
     }
-    else if (ref->uri.has_value())
+    else if (ref->uri != nullptr)
     {
-        auto uri = ref->uri.value();
+        auto uri = std::string(ref->uri);
         auto idString = uri.substr(uri.find_last_of(":") + 1, uri.size());
         CSPOT_LOG(debug, "idString = %s", idString.c_str());
         gid = base62Decode(idString);
@@ -19,6 +19,7 @@ TrackReference::TrackReference(TrackRef *ref)
 
 TrackReference::~TrackReference()
 {
+    //pbFree(TrackRef_fields, &ref);
 }
 
 std::vector<uint8_t> TrackReference::base62Decode(std::string uri)

+ 1 - 1
components/squeezelite/output_i2s.c

@@ -282,7 +282,7 @@ void output_init_i2s(log_level level, char *device, unsigned output_buf_size, ch
 		
 		res = i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
 		res |= i2s_set_pin(CONFIG_I2S_NUM, &i2s_spdif_pin);
-		LOG_INFO("SPDIF using I2S bck:%u, ws:%u, do:%u", i2s_spdif_pin.bck_io_num, i2s_spdif_pin.ws_io_num, i2s_spdif_pin.data_out_num);
+		LOG_INFO("SPDIF using I2S bck:%d, ws:%d, do:%d", i2s_spdif_pin.bck_io_num, i2s_spdif_pin.ws_io_num, i2s_spdif_pin.data_out_num);
 	} else {
 		i2s_config.sample_rate = output.current_sample_rate;
 		i2s_config.bits_per_sample = BYTES_PER_FRAME * 8 / 2;

+ 1 - 1
main/CMakeLists.txt

@@ -1,5 +1,5 @@
 idf_component_register(SRC_DIRS . 
-						PRIV_REQUIRES esp_common wifi-manager pthread squeezelite-ota platform_console telnet display
+						PRIV_REQUIRES _override esp_common wifi-manager pthread squeezelite-ota platform_console telnet display
                     	EMBED_FILES ../server_certs/github.pem
 						LDFRAGMENTS "linker.lf"
                     	)

+ 3 - 3
sdkconfig_compare.js

@@ -92,7 +92,7 @@ if (args.length != 3) {
 	console.log("Usage: node sdkconfig_compare file1 ");
 	process.exit();
 }
-const sdkconfig = ".\\sdkconfig";
+const sdkconfig = "./sdkconfig";
 const comparedFile = args[2];
 
 buildMap(sdkconfig).then((sdkConfigResult ) => {
@@ -101,7 +101,7 @@ buildMap(sdkconfig).then((sdkConfigResult ) => {
 	buildMap(comparedFile).then((comparedResult) => {
 		var comparedFileMap = comparedResult.map;
 		var comparedLines = comparedResult.lines_index;
-		buildMap(".\\sdkconfig.defaults").then((sdkconfigResults) => {
+		buildMap("./sdkconfig.defaults").then((sdkconfigResults) => {
 			var sdkconfig_defaults = sdkconfigResults.map;
 			var sdkconfig_defaults_lines = sdkconfigResults.lines_index;
 
@@ -176,4 +176,4 @@ buildMap(sdkconfig).then((sdkConfigResult ) => {
 	console.log(err);
 	process.exit();
 }
-);
+);