make it fit in allocated space

This commit is contained in:
philippe44
2023-03-27 17:09:27 -07:00
parent 7dfdd7b9e5
commit fc78b36c1f
24 changed files with 421 additions and 230 deletions

View File

@@ -17,9 +17,17 @@ option(BELL_SINK_ALSA "Enable ALSA audio sink" OFF)
option(BELL_SINK_PORTAUDIO "Enable PortAudio sink" OFF)
# cJSON wrapper
option(BELL_DISABLE_CJSON "Disable cJSON and JSONObject completely" OFF)
option(BELL_ONLY_CJSON "Use only cJSON, not Nlohmann")
set(BELL_EXTERNAL_CJSON "" CACHE STRING "External cJSON library target name, optional")
# vorbis
set(BELL_EXTERNAL_VORBIS "" CACHE STRING "External Vorbis library target name, optional")
option(BELL_VORBIS_FLOAT "Use floating point Vorbis API" OFF)
# fmt & regex
option(BELL_DISABLE_FMT "Don't use std::fmt (saves space)" OFF)
option(BELL_DISABLE_REGEX "Don't use std::regex (saves space)" OFF)
# disable json tests
set(JSON_BuildTests OFF CACHE INTERNAL "")
@@ -46,13 +54,16 @@ if(NOT BELL_DISABLE_CODECS)
endif()
message(STATUS " Disable built-in audio sinks: ${BELL_DISABLE_SINKS}")
message(STATUS " Use Vorbis float version: ${BELL_VORBIS_FLOAT}")
if(NOT BELL_DISABLE_SINKS)
message(STATUS " - ALSA sink: ${BELL_SINK_ALSA}")
message(STATUS " - PortAudio sink: ${BELL_SINK_PORTAUDIO}")
endif()
message(STATUS " Disable cJSON and JSONObject: ${BELL_DISABLE_CJSON}")
message(STATUS " Use cJSON only: ${BELL_ONLY_CJSON}")
message(STATUS " Disable Fmt: ${BELL_DISABLE_FMT}")
message(STATUS " Disable Regex: ${BELL_DISABLE_REGEX}")
# Include nanoPB library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/external/nanopb/extra")
@@ -212,7 +223,7 @@ else()
list(REMOVE_ITEM SOURCES "${IO_DIR}/EncodedAudioStream.cpp")
endif()
if(BELL_EXTERNAL_VORBIS)
if(NOT BELL_EXTERNAL_VORBIS STREQUAL "")
message(STATUS "Using external Vorbis codec ${BELL_EXTERNAL_VORBIS}")
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_VORBIS})
else()
@@ -254,21 +265,19 @@ if(NOT BELL_DISABLE_SINKS)
list(APPEND SOURCES ${SINK_SOURCES})
endif()
if(BELL_DISABLE_CJSON)
list(REMOVE_ITEM SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main/io/JSONObject.cpp")
else()
if(NOT BELL_ONLY_CJSON)
add_subdirectory(external/nlohmann_json)
list(APPEND EXTRA_LIBS nlohmann_json::nlohmann_json)
if(BELL_EXTERNAL_CJSON)
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
else()
list(APPEND SOURCES "external/cJSON/cJSON.c")
list(APPEND EXTRA_INCLUDES "external/cJSON")
endif()
endif()
endif()
if (BELL_DISABLE_FMT)
else()
if(BELL_EXTERNAL_CJSON)
list(APPEND EXTRA_LIBS ${BELL_EXTERNAL_CJSON})
else()
list(APPEND SOURCES "external/cJSON/cJSON.c")
list(APPEND EXTRA_INCLUDES "external/cJSON")
endif()
if (NOT BELL_DISABLE_FMT)
list(APPEND EXTRA_INCLUDES "external/fmt/include")
endif()
@@ -306,6 +315,18 @@ if(BELL_VORBIS_FLOAT)
target_compile_definitions(bell PUBLIC BELL_VORBIS_FLOAT)
endif()
if(BELL_DISABLE_FMT)
target_compile_definitions(bell PUBLIC BELL_DISABLE_FMT)
endif()
if(BELL_DISABLE_REGEX)
target_compile_definitions(bell PUBLIC BELL_DISABLE_REGEX)
endif()
if(BELL_ONLY_CJSON)
target_compile_definitions(bell PUBLIC BELL_ONLY_CJSON)
endif()
if(WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "SunOS")
target_compile_definitions(bell PUBLIC PB_NO_STATIC_ASSERT)
endif()

View File

@@ -1 +1,38 @@
#include "URLParser.h"
#include "URLParser.h"
namespace bell {
#ifdef BELL_DISABLE_REGEX
void URLParser::parse(const char* url, std::vector<std::string>& match) {
match[0] = url;
char scratch[512];
// get schema [http(s)]://
if (sscanf(url, "%[^:]:/", scratch) > 0) match[1] = scratch;
// get host http(s)://[host]
if (sscanf(url, "htt%*[^:]://%512[^/#]", scratch) > 0) match[2] = scratch;
// get the path
url = strstr(url, match[2].c_str());
if (!url || *url == '\0') return;
url += match[2].size();
if (sscanf(url, "/%512[^?]", scratch) > 0) match[3] = scratch;
// get the query
if (match[3].size()) url += match[3].size() + 1;
if (sscanf(url, "?%512[^#]", scratch) > 0) match[4] = scratch;
// get the hash
if (match[4].size()) url += match[4].size() + 1;
if (sscanf(url, "#%512s", scratch) > 0) match[5] = scratch;
// fix the acquired items
match[3] = "/" + match[3];
if (match[4].size()) match[4] = "?" + match[4];
}
#else
const std::regex URLParser::urlParseRegex = std::regex(
"^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
#endif
}

View File

@@ -15,7 +15,9 @@
#include "ByteStream.h"
#include "SocketStream.h"
#include "URLParser.h"
#ifndef BELL_DISABLE_FMT
#include "fmt/core.h"
#endif
#include "picohttpparser.h"
namespace bell {
@@ -29,11 +31,19 @@ class HTTPClient {
// Helper over ValueHeader, formatting a HTTP bytes range
struct RangeHeader {
static ValueHeader range(int32_t from, int32_t to) {
#ifndef BELL_DISABLE_FMT
return ValueHeader{"Range", fmt::format("bytes={}-{}", from, to)};
#else
return ValueHeader{"Range", "bytes=" + std::to_string(from) + "-" + std::to_string(to) };
#endif
}
static ValueHeader last(int32_t nbytes) {
#ifndef BELL_DISABLE_FMT
return ValueHeader{"Range", fmt::format("bytes=-{}", nbytes)};
#else
return ValueHeader{"Range", "bytes=-" + std::to_string(nbytes)};
#endif
}
};

View File

@@ -58,16 +58,23 @@ class URLParser {
std::string schema = "http";
std::string path;
std::regex urlParseRegex = std::regex(
"^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
#ifdef BELL_DISABLE_REGEX
void parse(const char* url, std::vector<std::string>& match);
#else
static const std::regex urlParseRegex;
#endif
static URLParser parse(const std::string& url) {
URLParser parser;
// apply parser.urlParseRegex to url
#ifdef BELL_DISABLE_REGEX
std::vector<std::string> match(6);
parser.parse(url.c_str(), match);
#else
std::cmatch match;
std::regex_match(url.c_str(), match, parser.urlParseRegex);
#endif
if (match.size() < 3) {
throw std::invalid_argument("Invalid URL");

View File

@@ -2,12 +2,14 @@
#include <map>
#include <string>
#include <memory>
namespace bell {
class MDNSService {
public:
static void* registerService(
public:
virtual ~MDNSService() { }
static std::unique_ptr<MDNSService> registerService(
const std::string &serviceName,
const std::string &serviceType,
const std::string &serviceProto,
@@ -15,7 +17,7 @@ class MDNSService {
int servicePort,
const std::map<std::string, std::string> txtData
);
static void unregisterService(void* service);
virtual void unregisterService() = 0;
};
} // namespace bell

View File

@@ -4,11 +4,20 @@
using namespace bell;
class implMDNSService : public MDNSService {
private:
DNSServiceRef* service;
public:
implMDNSService(DNSServiceRef* service) : service(service) { }
void unregisterService() { DNSServiceRefDeallocate(*service); }
};
/**
* MacOS implementation of MDNSService.
* @see https://developer.apple.com/documentation/dnssd/1804733-dnsserviceregister
**/
void* MDNSService::registerService(
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName,
const std::string& serviceType,
const std::string& serviceProto,
@@ -37,9 +46,5 @@ void* MDNSService::registerService(
NULL /* context */
);
TXTRecordDeallocate(&txtRecord);
return ref;
}
void MDNSService::unregisterService(void* ref) {
DNSServiceRefDeallocate((DNSServiceRef)ref);
return std::make_unique<implMDNSService>(ref);
}

View File

@@ -5,11 +5,22 @@
using namespace bell;
class implMDNSService : public MDNSService {
private:
const std::string type;
const std::string proto;
void unregisterService() { mdns_service_remove(type.c_str(), proto.c_str()); }
public:
implMDNSService(std::string type, std::string proto) : type(type), proto(proto) { };
};
/**
* ESP32 implementation of MDNSService
* @see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mdns.html
**/
void* MDNSService::registerService(
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName,
const std::string& serviceType,
const std::string& serviceProto,
@@ -35,5 +46,5 @@ void* MDNSService::registerService(
txtItems.size() /* num_items */
);
return NULL;
return std::make_unique<implMDNSService>(serviceType, serviceProto);
}

View File

@@ -11,7 +11,7 @@
#include <avahi-common/alternative.h>
#include <avahi-common/simple-watch.h>
#elif !defined(BELL_DISABLE_AVAHI)
#define BELL_DISABLE_AVAHI
#define BELL_DISABLE_AVAHI
#endif
#include "mdnssvc.h"
@@ -21,19 +21,55 @@
using namespace bell;
#ifndef BELL_DISABLE_AVAHI
static AvahiClient *avahiClient = NULL;
static AvahiSimplePoll *avahiPoll = NULL;
static void groupHandler(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { }
#endif
static in_addr_t host = INADDR_ANY;
static struct mdnsd *mdnsServer;
class implMDNSService : public MDNSService {
private:
#ifndef BELL_DISABLE_AVAHI
AvahiEntryGroup *avahiGroup;
#endif
struct mdns_service* service;
public:
#ifndef BELL_DISABLE_AVAHI
static AvahiClient *avahiClient;
static AvahiSimplePoll *avahiPoll;
#endif
static struct mdnsd* mdnsServer;
static in_addr_t host;
implMDNSService(struct mdns_service* service) : service(service) { };
#ifndef BELL_DISABLE_AVAHI
implMDNSService(AvahiEntryGroup *avahiGroup) : avahiGroup(avahiGroup) { };
#endif
void unregisterService();
};
struct mdnsd* implMDNSService::mdnsServer = NULL;
in_addr_t implMDNSService::host = INADDR_ANY;
#ifndef BELL_DISABLE_AVAHI
AvahiClient* implMDNSService::avahiClient = NULL;
AvahiSimplePoll* implMDNSService::avahiPoll = NULL;
#endif
/**
* Linux implementation of MDNSService using avahi.
* @see https://www.avahi.org/doxygen/html/
**/
void* MDNSService::registerService(
void implMDNSService::unregisterService() {
#ifndef BELL_DISABLE_AVAHI
if (avahiGroup) {
avahi_entry_group_free(avahiGroup);
} else
#endif
{
mdns_service_remove(implMDNSService::mdnsServer, service);
}
}
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName,
const std::string& serviceType,
const std::string& serviceProto,
@@ -43,22 +79,22 @@ void* MDNSService::registerService(
) {
#ifndef BELL_DISABLE_AVAHI
// try avahi first if available
if (!avahiPoll) {
avahiPoll = avahi_simple_poll_new();
if (!implMDNSService::avahiPoll) {
implMDNSService::avahiPoll = avahi_simple_poll_new();
}
if (avahiPoll && !avahiClient) {
avahiClient = avahi_client_new(avahi_simple_poll_get(avahiPoll),
if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
implMDNSService::avahiClient = avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
AvahiClientFlags(0), NULL, NULL, NULL);
}
AvahiEntryGroup *avahiGroup;
if (avahiClient &&
(avahiGroup = avahi_entry_group_new(avahiClient, groupHandler, NULL)) == NULL) {
if (implMDNSService::avahiClient &&
(avahiGroup = avahi_entry_group_new(implMDNSService::avahiClient, groupHandler, NULL)) == NULL) {
BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
}
if (avahiGroup) {
AvahiStringList* avahiTxt = NULL;
@@ -80,7 +116,7 @@ void* MDNSService::registerService(
avahi_entry_group_free(avahiGroup);
} else {
BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
return avahiGroup;
return std::make_unique<implMDNSService>(avahiGroup);
}
}
#endif
@@ -92,39 +128,39 @@ void* MDNSService::registerService(
if (serviceHost.size()) {
struct hostent *h = gethostbyname(serviceHost.c_str());
if (h) {
memcpy(&host, h->h_addr_list[0], 4);
memcpy(&implMDNSService::host, h->h_addr_list[0], 4);
}
}
// try go guess ifaddr if we have nothing as listening to INADDR_ANY usually does not work
if (host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
if (implMDNSService::host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET ||
!(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_MULTICAST) ||
(ifa->ifa_flags & IFF_LOOPBACK)) continue;
host = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
implMDNSService::host = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
}
if (!mdnsServer) {
if (!implMDNSService::mdnsServer) {
char hostname[256];
struct in_addr addr;
// it's the same, but who knows..
addr.s_addr = host;
addr.s_addr = implMDNSService::host;
gethostname(hostname, sizeof(hostname));
mdnsServer = mdnsd_start(addr, false);
implMDNSService::mdnsServer = mdnsd_start(addr, false);
if (mdnsServer) {
mdnsd_set_hostname(mdnsServer, hostname, addr);
if (implMDNSService::mdnsServer) {
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
}
}
if (mdnsServer) {
if (implMDNSService::mdnsServer) {
std::vector<const char*> txt;
std::vector<std::unique_ptr<std::string>> txtStr;
@@ -137,25 +173,17 @@ void* MDNSService::registerService(
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
BELL_LOG(info, "MDNS", "using build-in mDNS for %s", serviceName.c_str());
struct mdns_service* mdnsService = mdnsd_register_svc(mdnsServer, serviceName.c_str(),
BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
struct mdns_service* mdnsService = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
if (mdnsService) {
return mdnsService;
auto service = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
return std::make_unique<implMDNSService>(service);
}
}
BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s", serviceName.c_str());
return NULL;
}
void MDNSService::unregisterService(void* service) {
#ifndef BELL_DISABLE_AVAHI
if (avahiClient) {
avahi_entry_group_free((AvahiEntryGroup*)service);
} else
#endif
{
mdns_service_remove(mdnsServer, (mdns_service*)service);
}
}

View File

@@ -17,13 +17,23 @@
using namespace bell;
static struct mdnsd *mdnsService;
class implMDNSService : public MDNSService {
private:
struct mdns_service* service;
void unregisterService(void) { mdns_service_remove(implMDNSService::mdnsServer, service); };
public:
static struct mdnsd* mdnsServer;
implMDNSService(struct mdns_service* service) : service(service) { };
};
/**
* Win32 implementation of MDNSService
**/
void* MDNSService::registerService(
struct mdnsd* implMDNSService::mdnsServer = NULL;
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName,
const std::string& serviceType,
const std::string& serviceProto,
@@ -31,7 +41,7 @@ void* MDNSService::registerService(
int servicePort,
const std::map<std::string, std::string> txtData
) {
if (!mdnsService) {
if (!implMDNSService::mdnsServer) {
char hostname[128];
gethostname(hostname, sizeof(hostname));
@@ -49,14 +59,14 @@ void* MDNSService::registerService(
if (adapter->FirstGatewayAddress && unicast->Address.lpSockaddr->sa_family == AF_INET) {
host = (struct sockaddr_in*)unicast->Address.lpSockaddr;
BELL_LOG(info, "mdns", "mDNS on interface %s", inet_ntoa(host->sin_addr));
mdnsService = mdnsd_start(host->sin_addr, false);
implMDNSService::mdnsServer = mdnsd_start(host->sin_addr, false);
break;
}
}
}
assert(mdnsService);
mdnsd_set_hostname(mdnsService, hostname, host->sin_addr);
assert(implMDNSService::mdnsServer);
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, host->sin_addr);
free(adapters);
}
@@ -71,9 +81,9 @@ void* MDNSService::registerService(
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
return mdnsd_register_svc(mdnsService, serviceName.c_str(), type.c_str(), servicePort, NULL, txt.data());
}
void MDNSService::unregisterService(void* service) {
mdns_service_remove(mdnsService, (mdns_service*)service);
auto service = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
return std::make_unique<implMDNSService>(service);
}

View File

@@ -11,7 +11,6 @@
#include <vector>
#ifdef ESP_PLATFORM
//#include "esp_mac.h"
#include "esp_system.h"
#endif

View File

@@ -22,7 +22,7 @@ extern "C" {
#define DH_KEY_SIZE 96
static unsigned char DHPrime[] = {
const static unsigned char DHPrime[] = {
/* Well-known Group 1, 768-bit prime */
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,