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

@@ -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)
if(BELL_DISABLE_CODECS)
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/DecoderGlobals.cpp)
else()
set(EXTRA_INC ${EXTRA_INC} "libhelix-aac" "libhelix-mp3")
set(SOURCES ${SOURCES} "libhelix-aac/aacdec.c" "libhelix-aac/aactabs.c" "libhelix-aac/bitstream.c" "libhelix-aac/buffers.c" "libhelix-aac/dct4.c" "libhelix-aac/decelmnt.c" "libhelix-aac/dequant.c" "libhelix-aac/fft.c" "libhelix-aac/filefmt.c" "libhelix-aac/huffman.c" "libhelix-aac/hufftabs.c" "libhelix-aac/imdct.c" "libhelix-aac/noiseless.c" "libhelix-aac/pns.c" "libhelix-aac/sbr.c" "libhelix-aac/sbrfft.c" "libhelix-aac/sbrfreq.c" "libhelix-aac/sbrhfadj.c" "libhelix-aac/sbrhfgen.c" "libhelix-aac/sbrhuff.c" "libhelix-aac/sbrimdct.c" "libhelix-aac/sbrmath.c" "libhelix-aac/sbrqmf.c" "libhelix-aac/sbrside.c" "libhelix-aac/sbrtabs.c" "libhelix-aac/stproc.c" "libhelix-aac/tns.c" "libhelix-aac/trigtabs.c")
set(SOURCES ${SOURCES} "libhelix-mp3/bitstream.c" "libhelix-mp3/buffers.c" "libhelix-mp3/dct32.c" "libhelix-mp3/dequant.c" "libhelix-mp3/dqchan.c" "libhelix-mp3/huffman.c" "libhelix-mp3/hufftabs.c" "libhelix-mp3/imdct.c" "libhelix-mp3/mp3dec.c" "libhelix-mp3/mp3tabs.c" "libhelix-mp3/polyphase.c" "libhelix-mp3/scalfact.c" "libhelix-mp3/stproc.c" "libhelix-mp3/subband.c" "libhelix-mp3/trigtabs.c")
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})
else()
set(EXTRA_INC ${EXTRA_INC} "cJSON")
set(SOURCES ${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_CJSON)
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
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")
list(APPEND EXTRA_INCLUDES "cJSON")
list(APPEND SOURCES "cJSON/cJSON.c")
endif()
if(BELL_EXTERNAL_TREMOR)
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_TREMOR})
else()
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,11 +9,13 @@ namespace bell {
Socket() {};
virtual ~Socket() = default;
virtual void open(std::string url) = 0;
void open(const std::string &url);
virtual void open(std::string host, uint16_t port) = 0;
virtual size_t poll() = 0;
virtual size_t write(uint8_t* buf, size_t len) = 0;
virtual size_t read(uint8_t* buf, size_t len) = 0;
virtual void close() = 0;
};
}
#endif
#endif

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);
@@ -107,4 +99,4 @@ namespace bell
}
#endif
#endif

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