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

This commit is contained in:
Philippe G
2022-01-04 00:15:33 -08:00
parent cf1315e6a4
commit 06b637c55b
43 changed files with 2955 additions and 402 deletions

View File

@@ -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
components/_override/i2s.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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);
}

View File

@@ -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;
}

View File

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

View File

@@ -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;
}

View File

@@ -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 ()

View File

@@ -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) {

View File

@@ -1,66 +1,40 @@
cmake_minimum_required(VERSION 2.8.12)
project(cspot)
cmake_minimum_required(VERSION 2.8.9)
# 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()
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)
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})
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})

View File

@@ -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)
# 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")
# Add platform specific sources
if(${ESP_PLATFORM})
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)
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")
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(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()
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
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})
if(BELL_EXTERNAL_CJSON)
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
else()
set(EXTRA_INC ${EXTRA_INC} "cJSON")
set(SOURCES ${SOURCES} "cJSON/cJSON.c")
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})

View File

@@ -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;
};

View File

@@ -9,7 +9,9 @@ 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;

View File

@@ -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);

View File

@@ -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();
};

View File

@@ -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};

View File

@@ -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);

View File

@@ -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);

View File

@@ -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`

View File

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

View File

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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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.
@@ -42,6 +43,8 @@ public:
*/
PlayerState(std::shared_ptr<TimeProvider> timeProvider);
~PlayerState();
/**
* @brief Updates state according to current playback state.
*

View File

@@ -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);

View File

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

View File

@@ -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;

View File

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

View File

@@ -63,4 +63,3 @@ message ClientResponseEncrypted {
required SystemInfo system_info = 0x32;
optional string version_string = 0x46;
}

View File

@@ -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;
}

View File

@@ -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:

View File

@@ -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)

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -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.state.has_position_ms = true;
innerFrame.state.position_ms = 0;
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.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;
if (innerFrame.state->playing_track_index >= innerFrame.state->track.size())
innerFrame.state.playing_track_index++;
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;
}
capability.stringValue = stringValue;
innerFrame.device_state->capabilities.push_back(capability);
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';
}
this->innerFrame.device_state.capabilities[capabilityIndex].stringValue_count = stringValue.size();
this->capabilityIndex += 1;
}

View File

@@ -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;
auto resultPacket = encodePb(clientResPlaintext);
std::copy(digest.begin(),
digest.end(),
clientResPlaintext.login_crypto_response.diffie_hellman.hmac);
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);
}

View File

@@ -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.

View File

@@ -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);
});
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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;

View File

@@ -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"
)

View File

@@ -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;