big merge

This commit is contained in:
Philippe G
2021-12-18 21:04:23 -08:00
parent 955692f8ad
commit 898998efb0
583 changed files with 84472 additions and 1965 deletions

Binary file not shown.

View File

@@ -0,0 +1,29 @@
#include "platform/WrappedSemaphore.h"
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,92 @@
#include "platform/TLSSocket.h"
/**
* Platform TLSSocket implementation for the mbedtls
*/
bell::TLSSocket::TLSSocket()
{
mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
const char *pers = "euphonium";
int ret;
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *)pers,
strlen(pers))) != 0)
{
BELL_LOG(error, "http_tls",
"failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
}
}
void bell::TLSSocket::open(std::string url)
{
// 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",
MBEDTLS_NET_PROTO_TCP)) != 0)
{
BELL_LOG(error, "http_tls", "failed! connect returned %d\n", ret);
}
if ((ret = mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
{
BELL_LOG(error, "http_tls", "failed! config returned %d\n", ret);
}
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
mbedtls_ssl_setup(&ssl, &conf);
if ((ret = mbedtls_ssl_set_hostname(&ssl, "Mbed TLS Server 1")) != 0)
{
BELL_LOG(info, "oh", "kocz");
}
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv,
NULL);
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0)
{
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
BELL_LOG(error, "http_tls", "failed! config returned %d\n", ret);
}
}
}
size_t bell::TLSSocket::read(uint8_t *buf, size_t len)
{
return mbedtls_ssl_read(&ssl, buf, len);
}
size_t bell::TLSSocket::write(uint8_t *buf, size_t len)
{
return mbedtls_ssl_write(&ssl, buf, len);
}
void bell::TLSSocket::close()
{
mbedtls_net_free(&server_fd);
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
}

View File

@@ -0,0 +1,39 @@
#include "platform/WrappedSemaphore.h"
/**
* Platform semaphopre implementation for the esp-idf.
*/
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,72 @@
#include "platform/TLSSocket.h"
/**
* Platform TLSSocket implementation for the openssl
*/
bell::TLSSocket::TLSSocket() {
ERR_load_crypto_strings();
ERR_load_SSL_strings();
OpenSSL_add_all_algorithms();
ctx = SSL_CTX_new(SSLv23_client_method());
}
void bell::TLSSocket::open(std::string url) {
/* We'd normally set some stuff like the verify paths and
* mode here because as things stand this will connect to
* any server whose certificate is signed by any CA.
*/
sbio = BIO_new_ssl_connect(ctx);
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());
out = BIO_new_fp(stdout, BIO_NOCLOSE);
if (BIO_do_connect(sbio) <= 0) {
BELL_LOG(error, "http_tls", "Error connecting with server");
/* whatever ... */
}
if (BIO_do_handshake(sbio) <= 0) {
BELL_LOG(error, "http_tls", "Error TLS connection");
/* whatever ... */
} // remove https or http from url
// split by first "/" in url
}
size_t bell::TLSSocket::read(uint8_t *buf, size_t len) {
return BIO_read(sbio, buf, len);
}
size_t bell::TLSSocket::write(uint8_t *buf, size_t len) {
return BIO_write(sbio, buf, len);
}
void bell::TLSSocket::close() {
if (!isClosed) {
BIO_free_all(sbio);
BIO_free(out);
isClosed = true;
}
}

View File

@@ -0,0 +1,31 @@
#include "platform/WrappedSemaphore.h"
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);
}