applied platformio structure

This commit is contained in:
2026-03-13 17:03:22 +00:00
parent c5233cf15c
commit db7d90e736
3510 changed files with 691878 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#pragma once
#include <map> // for map
#include <memory> // for unique_ptr
#include <string> // for string
namespace bell {
class MDNSService {
public:
virtual ~MDNSService() {}
static std::unique_ptr<MDNSService> registerService(
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData);
virtual void unregisterService() = 0;
};
} // namespace bell

View File

@@ -0,0 +1,38 @@
#pragma once
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#elif __APPLE__
#include <dispatch/dispatch.h> // for dispatch_semaphore_t
#elif _WIN32
#include <winsock2.h>
#else
#include <semaphore.h>
#include <time.h>
#endif
namespace bell {
class WrappedSemaphore {
private:
#ifdef ESP_PLATFORM
SemaphoreHandle_t semaphoreHandle;
#elif __APPLE__
dispatch_semaphore_t semaphoreHandle;
#elif _WIN32
HANDLE semaphoreHandle;
#else
sem_t semaphoreHandle;
#endif
public:
WrappedSemaphore(int maxVal = 200);
~WrappedSemaphore();
int wait();
int twait(long milliseconds = 10);
void give();
};
} // namespace bell

View File

@@ -0,0 +1,51 @@
#include "MDNSService.h"
#include <stddef.h> // for NULL
#include <utility> // for pair
#include "dns_sd.h" // for DNSServiceRef, DNSServiceRefDeallocate, DNS...
#include "i386/endian.h" // for htons
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
**/
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData) {
DNSServiceRef* ref = new DNSServiceRef();
TXTRecordRef txtRecord;
TXTRecordCreate(&txtRecord, 0, NULL);
for (auto& data : txtData) {
TXTRecordSetValue(&txtRecord, data.first.c_str(), data.second.size(),
data.second.c_str());
}
DNSServiceRegister(ref, /* sdRef */
0, /* flags */
0, /* interfaceIndex */
serviceName.c_str(), /* name */
(serviceType + "." + serviceProto)
.c_str(), /* regType (_spotify-connect._tcp) */
NULL, /* domain */
NULL, /* host */
htons(servicePort), /* port */
TXTRecordGetLength(&txtRecord), /* txtLen */
TXTRecordGetBytesPtr(&txtRecord), /* txtRecord */
NULL, /* callBack */
NULL /* context */
);
TXTRecordDeallocate(&txtRecord);
return std::make_unique<implMDNSService>(ref);
}

View File

@@ -0,0 +1,27 @@
#include "WrappedSemaphore.h"
using namespace bell;
WrappedSemaphore::WrappedSemaphore(int count) {
semaphoreHandle = dispatch_semaphore_create(0);
}
WrappedSemaphore::~WrappedSemaphore() {
dispatch_release(semaphoreHandle);
}
int WrappedSemaphore::wait() {
return dispatch_semaphore_wait(semaphoreHandle, DISPATCH_TIME_FOREVER);
}
int WrappedSemaphore::twait(long milliseconds) {
dispatch_time_t timeout =
dispatch_time(DISPATCH_TIME_NOW, (NSEC_PER_SEC / 1000) * milliseconds);
return dispatch_semaphore_wait(semaphoreHandle, timeout);
}
void WrappedSemaphore::give() {
dispatch_semaphore_signal(semaphoreHandle);
}

View File

@@ -0,0 +1,46 @@
#include "MDNSService.h"
#include <arpa/inet.h>
#include <vector>
#include "mdns.h"
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
**/
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData) {
std::vector<mdns_txt_item_t> txtItems;
txtItems.reserve(txtData.size());
for (auto& data : txtData) {
mdns_txt_item_t item;
item.key = data.first.c_str();
item.value = data.second.c_str();
txtItems.push_back(item);
}
mdns_service_add(serviceName.c_str(), /* instance_name */
serviceType.c_str(), /* service_type */
serviceProto.c_str(), /* proto */
servicePort, /* port */
txtItems.data(), /* txt */
txtItems.size() /* num_items */
);
return std::make_unique<implMDNSService>(serviceType, serviceProto);
}

View File

@@ -0,0 +1,37 @@
#include "WrappedSemaphore.h"
/**
* Platform semaphopre implementation for the esp-idf.
*/
using namespace bell;
WrappedSemaphore::WrappedSemaphore(int count) {
semaphoreHandle = xSemaphoreCreateCounting(count, 0);
}
WrappedSemaphore::~WrappedSemaphore() {
vSemaphoreDelete(semaphoreHandle);
}
int WrappedSemaphore::wait() {
if (xSemaphoreTake(semaphoreHandle, portMAX_DELAY) == pdTRUE) {
return 0;
}
return 1;
}
int WrappedSemaphore::twait(long milliseconds) {
if (xSemaphoreTake(semaphoreHandle, milliseconds / portTICK_PERIOD_MS) ==
pdTRUE) {
return 0;
}
return 1;
}
void WrappedSemaphore::give() {
xSemaphoreGive(semaphoreHandle);
}

View File

@@ -0,0 +1,206 @@
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <unistd.h>
#include <cstring>
#include <vector>
#include <mutex>
#include <atomic>
#if __has_include("avahi-client/client.h")
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/simple-watch.h>
#elif !defined(BELL_DISABLE_AVAHI)
#define BELL_DISABLE_AVAHI
#endif
#include "BellLogger.h"
#include "MDNSService.h"
#include "mdnssvc.h"
using namespace bell;
#ifndef BELL_DISABLE_AVAHI
static void groupHandler(AvahiEntryGroup* g, AvahiEntryGroupState state,
AVAHI_GCC_UNUSED void* userdata) {}
#endif
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;
static std::atomic<size_t> instances;
implMDNSService(struct mdns_service* service) : service(service){ instances++; };
#ifndef BELL_DISABLE_AVAHI
implMDNSService(AvahiEntryGroup* avahiGroup) : avahiGroup(avahiGroup){};
#endif
void unregisterService();
};
struct mdnsd* implMDNSService::mdnsServer = NULL;
in_addr_t implMDNSService::host = INADDR_ANY;
std::atomic<size_t> implMDNSService::instances = 0;
static std::mutex registerMutex;
#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 implMDNSService::unregisterService() {
#ifndef BELL_DISABLE_AVAHI
if (avahiGroup) {
avahi_entry_group_free(avahiGroup);
if (!--instances && implMDNSService::avahiClient) {
avahi_client_free(implMDNSService::avahiClient);
avahi_simple_poll_free(implMDNSService::avahiPoll);
implMDNSService::avahiClient = nullptr;
implMDNSService::avahiPoll = nullptr;
}
} else
#endif
{
mdns_service_remove(implMDNSService::mdnsServer, service);
if (!--instances && implMDNSService::mdnsServer) {
mdnsd_stop(implMDNSService::mdnsServer);
implMDNSService::mdnsServer = nullptr;
}
}
}
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData) {
std::lock_guard lock(registerMutex);
#ifndef BELL_DISABLE_AVAHI
// try avahi first if available
if (!implMDNSService::avahiPoll) {
implMDNSService::avahiPoll = avahi_simple_poll_new();
}
if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
implMDNSService::avahiClient =
avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
AvahiClientFlags(0), NULL, NULL, NULL);
}
AvahiEntryGroup* avahiGroup = 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 != NULL) {
AvahiStringList* avahiTxt = NULL;
for (auto& [key, value] : txtData) {
avahiTxt =
avahi_string_list_add_pair(avahiTxt, key.c_str(), value.c_str());
}
std::string type(serviceType + "." + serviceProto);
int ret = avahi_entry_group_add_service_strlst(
avahiGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0,
serviceName.c_str(), type.c_str(), NULL, NULL, servicePort, avahiTxt);
avahi_string_list_free(avahiTxt);
if (ret >= 0) {
ret = avahi_entry_group_commit(avahiGroup);
}
if (ret < 0) {
BELL_LOG(error, "MDNS", "cannot run service %s", serviceName.c_str());
avahi_entry_group_free(avahiGroup);
} else {
BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
return std::make_unique<implMDNSService>(avahiGroup);
}
}
#endif
// avahi failed, use build-in server
struct ifaddrs* ifaddr;
// get the host address first
if (serviceHost.size()) {
struct hostent* h = gethostbyname(serviceHost.c_str());
if (h) {
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 (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;
implMDNSService::host =
((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
}
if (!implMDNSService::mdnsServer) {
char hostname[256];
struct in_addr addr;
// it's the same, but who knows..
addr.s_addr = implMDNSService::host;
gethostname(hostname, sizeof(hostname));
implMDNSService::mdnsServer = mdnsd_start(addr, false);
if (implMDNSService::mdnsServer) {
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
}
}
if (implMDNSService::mdnsServer) {
std::vector<const char*> txt;
std::vector<std::unique_ptr<std::string>> txtStr;
for (auto& [key, value] : txtData) {
auto str = make_unique<std::string>(key + "=" + value);
txtStr.push_back(std::move(str));
txt.push_back(txtStr.back()->c_str());
}
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
auto service =
mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
if (service) return std::make_unique<implMDNSService>(service);
}
BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s",
serviceName.c_str());
return nullptr;
}

View File

@@ -0,0 +1,33 @@
#include "WrappedSemaphore.h"
#include <sys/time.h>
using namespace bell;
WrappedSemaphore::WrappedSemaphore(int count) {
sem_init(&this->semaphoreHandle, 0, 0); // eek pointer
}
WrappedSemaphore::~WrappedSemaphore() {
sem_destroy(&this->semaphoreHandle);
}
int WrappedSemaphore::wait() {
sem_wait(&this->semaphoreHandle);
return 0;
}
int WrappedSemaphore::twait(long milliseconds) {
// wait on semaphore with timeout
struct timespec ts;
struct timeval tv;
gettimeofday(&tv, 0);
ts.tv_sec = tv.tv_sec + milliseconds / 1000;
ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
return sem_timedwait(&this->semaphoreHandle, &ts);
}
void WrappedSemaphore::give() {
sem_post(&this->semaphoreHandle);
}

View File

@@ -0,0 +1,108 @@
#include <cassert>
#include <vector>
#include <mutex>
#include "BellLogger.h"
#include "MDNSService.h"
#ifdef _WIN32
#include <WinSock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
#include "mdnssvc.h"
#else
#include <arpa/inet.h>
#include "mdns.h"
#endif
using namespace bell;
class implMDNSService : public MDNSService {
private:
struct mdns_service* service;
void unregisterService(void);
public:
static struct mdnsd* mdnsServer;
static std::atomic<size_t> instances;
implMDNSService(struct mdns_service* service) : service(service) { instances++; };
};
/**
* Win32 implementation of MDNSService
**/
struct mdnsd* implMDNSService::mdnsServer = NULL;
std::atomic<size_t> implMDNSService::instances = 0;
static std::mutex registerMutex;
void implMDNSService::unregisterService() {
mdns_service_remove(implMDNSService::mdnsServer, service);
if (!--instances && implMDNSService::mdnsServer) {
mdnsd_stop(implMDNSService::mdnsServer);
implMDNSService::mdnsServer = nullptr;
}
}
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData) {
std::lock_guard lock(registerMutex);
if (!implMDNSService::mdnsServer) {
char hostname[128];
gethostname(hostname, sizeof(hostname));
struct sockaddr_in* host = NULL;
ULONG size = sizeof(IP_ADAPTER_ADDRESSES) * 64;
IP_ADAPTER_ADDRESSES* adapters = (IP_ADAPTER_ADDRESSES*)malloc(size);
int ret = GetAdaptersAddresses(AF_UNSPEC,
GAA_FLAG_INCLUDE_GATEWAYS |
GAA_FLAG_SKIP_MULTICAST |
GAA_FLAG_SKIP_ANYCAST,
0, adapters, &size);
for (PIP_ADAPTER_ADDRESSES adapter = adapters; adapter && !host;
adapter = adapter->Next) {
if (adapter->TunnelType == TUNNEL_TYPE_TEREDO)
continue;
if (adapter->OperStatus != IfOperStatusUp)
continue;
for (IP_ADAPTER_UNICAST_ADDRESS* unicast = adapter->FirstUnicastAddress;
unicast; unicast = unicast->Next) {
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));
implMDNSService::mdnsServer = mdnsd_start(host->sin_addr, false);
break;
}
}
}
assert(implMDNSService::mdnsServer);
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, host->sin_addr);
free(adapters);
}
std::vector<const char*> txt;
std::vector<std::unique_ptr<std::string>> txtStr;
for (auto& [key, value] : txtData) {
auto str = make_unique<std::string>(key + "=" + value);
txtStr.push_back(std::move(str));
txt.push_back(txtStr.back()->c_str());
}
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
auto service =
mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
return service ? std::make_unique<implMDNSService>(service) : nullptr;
}

View File

@@ -0,0 +1,25 @@
#include "WrappedSemaphore.h"
using namespace bell;
WrappedSemaphore::WrappedSemaphore(int count) {
this->semaphoreHandle = CreateSemaphore(NULL, 0, count, NULL);
}
WrappedSemaphore::~WrappedSemaphore() {
CloseHandle(this->semaphoreHandle);
}
int WrappedSemaphore::wait() {
WaitForSingleObject(this->semaphoreHandle, INFINITE);
return 0;
}
int WrappedSemaphore::twait(long milliseconds) {
return WaitForSingleObject(this->semaphoreHandle, milliseconds) !=
WAIT_OBJECT_0;
}
void WrappedSemaphore::give() {
ReleaseSemaphore(this->semaphoreHandle, 1, NULL);
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include <winsock2.h>
#define SHUT_RDWR SD_BOTH
#define ssize_t SSIZE_T
#define strcasecmp stricmp
#define strncasecmp _strnicmp
#define bzero(p, n) memset(p, 0, n)
#define usleep(x) Sleep((x) / 1000)
inline size_t read(int sock, char* buf, size_t n) {
return recv(sock, buf, n, 0);
}
inline int write(int sock, const char* buf, size_t n) {
return send(sock, buf, n, 0);
}