move to new cspot

This commit is contained in:
philippe44
2023-03-25 16:48:41 -07:00
parent c712b78931
commit 008c36facf
2983 changed files with 465270 additions and 13569 deletions

View File

@@ -0,0 +1,21 @@
#pragma once
#include <map>
#include <string>
namespace bell {
class MDNSService {
public:
static void* 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
);
static void unregisterService(void* service);
};
} // 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>
#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,45 @@
#include "MDNSService.h"
#include "dns_sd.h"
#include <arpa/inet.h>
using namespace bell;
/**
* MacOS implementation of MDNSService.
* @see https://developer.apple.com/documentation/dnssd/1804733-dnsserviceregister
**/
void* 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 ref;
}
void MDNSService::unregisterService(void* ref) {
DNSServiceRefDeallocate((DNSServiceRef)ref);
}

View File

@@ -0,0 +1,31 @@
#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,39 @@
#include "MDNSService.h"
#include <arpa/inet.h>
#include <vector>
#include "mdns.h"
using namespace bell;
/**
* ESP32 implementation of MDNSService
* @see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mdns.html
**/
void* 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 NULL;
}

View File

@@ -0,0 +1,41 @@
#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,161 @@
#include <vector>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
#include <ifaddrs.h>
#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 "mdnssvc.h"
#include "BellLogger.h"
#include "MDNSService.h"
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;
/**
* Linux implementation of MDNSService using avahi.
* @see https://www.avahi.org/doxygen/html/
**/
void* 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
) {
#ifndef BELL_DISABLE_AVAHI
// try avahi first if available
if (!avahiPoll) {
avahiPoll = avahi_simple_poll_new();
}
if (avahiPoll && !avahiClient) {
avahiClient = avahi_client_new(avahi_simple_poll_get(avahiPoll),
AvahiClientFlags(0), NULL, NULL, NULL);
}
AvahiEntryGroup *avahiGroup;
if (avahiClient &&
(avahiGroup = avahi_entry_group_new(avahiClient, groupHandler, NULL)) == NULL) {
BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
}
if (avahiGroup) {
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 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(&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) {
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;
break;
}
freeifaddrs(ifaddr);
}
if (!mdnsServer) {
char hostname[256];
struct in_addr addr;
// it's the same, but who knows..
addr.s_addr = host;
gethostname(hostname, sizeof(hostname));
mdnsServer = mdnsd_start(addr, false);
if (mdnsServer) {
mdnsd_set_hostname(mdnsServer, hostname, addr);
}
}
if (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 build-in mDNS for %s", serviceName.c_str());
struct mdns_service* mdnsService = mdnsd_register_svc(mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
if (mdnsService) {
return mdnsService;
}
}
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

@@ -0,0 +1,33 @@
#include "WrappedSemaphore.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;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += (milliseconds % 1000) * 1000000;
return sem_timedwait(&this->semaphoreHandle, &ts);
}
void WrappedSemaphore::give()
{
sem_post(&this->semaphoreHandle);
}

View File

@@ -0,0 +1,79 @@
#include <vector>
#include <cassert>
#include "MDNSService.h"
#include "BellLogger.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"
#include <arpa/inet.h>
#endif
using namespace bell;
static struct mdnsd *mdnsService;
/**
* Win32 implementation of MDNSService
**/
void* 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
) {
if (!mdnsService) {
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));
mdnsService = mdnsd_start(host->sin_addr, false);
break;
}
}
}
assert(mdnsService);
mdnsd_set_hostname(mdnsService, 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");
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);
}

View File

@@ -0,0 +1,29 @@
#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,14 @@
#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); }