Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support support using the RSMB code as an ESP-IDF component #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB srcs rsmb/src/*.c)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D _BOOL=1")

idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "rsmb/src"
)
18 changes: 16 additions & 2 deletions rsmb/src/Broker.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ void HUPHandler(int sig)


#if !defined(WIN32)
#ifdef ESP_PLATFORM
void linux_segv(int signo, siginfo_t* info, void* context)
{
const char *signal_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
static char symptoms[1024];

sprintf(symptoms, "SEGV information\n"
"info.si_signo = %d\n"
"info.si_code = %d (%s)\n",
info->si_signo, info->si_code, signal_codes[info->si_code]);
#else
void linux_segv(int signo, siginfo_t* info, void* context)
{
const char *signal_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
Expand All @@ -170,6 +181,7 @@ void linux_segv(int signo, siginfo_t* info, void* context)
"info.si_code = %d (%s)\n"
"info.si_addr = %p\n",
info->si_signo, info->si_errno, info->si_code, signal_codes[info->si_code], info->si_addr);
#endif
#else
/**
* Normal segv handling function
Expand All @@ -187,6 +199,7 @@ void segv(int sig)
exit(-1);
}

#ifndef ESP_PLATFORM

int set_sigsegv()
{
Expand All @@ -196,7 +209,6 @@ int set_sigsegv()
#else
struct sigaction action;
void linux_segv(int signo, siginfo_t* info, void* context);

action.sa_flags = SA_SIGINFO; /* indicates the sa_sigaction field is to be used */
action.sa_sigaction = linux_segv;
sigemptyset(&action.sa_mask);
Expand All @@ -205,7 +217,7 @@ int set_sigsegv()
#endif
}


#endif

void getopts(int argc, char** argv)
{
Expand Down Expand Up @@ -320,12 +332,14 @@ int Broker_startup()
int rc;

FUNC_ENTRY;
#if !defined(ESP_PLATFORM)
signal(SIGINT, finish);
signal(SIGTERM, finish);
signal(SIGHUP, HUPHandler);
#if !defined(_DEBUG)
set_sigsegv();
#endif
#endif

BrokerState.clients = TreeInitialize(clientSocketCompare);
TreeAddIndex(BrokerState.clients, clientIDCompare);
Expand Down
2 changes: 2 additions & 0 deletions rsmb/src/Broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#define true 1
#define false 0

#ifndef assert
#define assert(x) if (!x) printf("Assertion error %s\n", # x);
#endif

#ifdef __GNUC__
#define ATTR_UNUSED __attribute__((unused))
Expand Down
9 changes: 9 additions & 0 deletions rsmb/src/Log.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
#include <stdlib.h>

#if !defined(WIN32)
#ifdef ESP_PLATFORM
#include <esp_log.h>
#else
#include <syslog.h>
#endif
#define GETTIMEOFDAY 1
#else
#define snprintf _snprintf
Expand Down Expand Up @@ -399,8 +403,13 @@ void Log(int log_level, int msgno, char* format, ...)
#if !defined(WIN32)
if (trace_settings.isdaemon)
{
#ifdef ESP_PLATFORM
static char priorities[] = { 5, 5, 5, 5, 4, 4, 3, 3, 2, 2, 1, 0};
esp_log_write(priorities[log_level], "mqtt", "%s", &msg_buf[22]);
#else
static char priorities[] = { 7, 7, 7, 7, 6, 6, 5, 5, 4, 3, 1, 0};
syslog(priorities[log_level], "%s", &msg_buf[22]);
#endif
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions rsmb/src/MQTTPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ include "LinkedList"
include "Clients"
BE*/

#ifndef _BOOL
#define _BOOL
typedef unsigned int bool;
#endif

#define PRIVATE_PROTOCOL_VERSION 131 /**< protocol version used for noLocal subscriptions */
#define BAD_MQTT_PACKET -4 /**< completion code for an ill-formed packet */
Expand Down
8 changes: 7 additions & 1 deletion rsmb/src/Messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ int Messages_findMyLocation(char* buf, int bufsize)
rc = GetModuleFileName(NULL, wbuf, bufsize);
wcstombs(buf, wbuf, bufsize);
#else /* Linux */
#ifdef ESP_PLATFORM
rc = sprintf(buf, "/sdcard/dummy.exe");
#else
rc = (int)readlink("/proc/self/exe", buf, bufsize);
#endif
#endif
if (rc > 0 && rc < bufsize)
{
Expand Down Expand Up @@ -244,13 +248,15 @@ int Messages_initialize(BrokerStates* bstate)
{
char fullfn[256];
sprintf(fullfn, "..%cmessages%c%s", sep, sep, fn);

if ((rfile = fopen(fullfn, "r")) == NULL)
{
if (Messages_findMyLocation(fullfn, sizeof(fullfn)) == 0)
{
int dirlength = strlen(fullfn);

snprintf(&fullfn[dirlength], sizeof(fullfn) - dirlength, "%c%s", sep, fn);

rfile = fopen(fullfn, "r");
if (rfile == NULL)
{
Expand Down
35 changes: 32 additions & 3 deletions rsmb/src/Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ void Socket_outInitialize()
WSAStartup(winsockVer, &wsd);
#else
FUNC_ENTRY;
#if !defined(ESP_PLATFORM)
signal(SIGPIPE, SIG_IGN);
#endif
#endif
SocketBuffer_initialize();

Expand Down Expand Up @@ -323,11 +325,15 @@ int Socket_joinMulticastGroup(int sock, int ipv6, char* mcast)
struct ifreq ifreq;

strncpy(ifreq.ifr_name, mcast_interface, IFNAMSIZ);
#ifdef ESP_PLATFORM
#warning Socket_joinMulticastGroup() broken on ESP-IDF
#else
if (ioctl(sock, SIOCGIFADDR, &ifreq) >= 0)
memcpy(&mreq.imr_interface,
&((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
sizeof(struct in_addr));
else
#endif
Socket_error("ioctl SIOCGIFADDR", sock);
#endif
}
Expand Down Expand Up @@ -1276,6 +1282,10 @@ int Socket_noPendingWrites(int socket)
}
#endif

#ifdef ESP_PLATFORM
#include <lwip/sockets.h>
#define writev(s, iov, iovcnt) lwip_writev(s,iov,iovcnt)
#endif

/**
* Attempts to write a series of iovec buffers to a socket in *one* system call so that
Expand All @@ -1301,6 +1311,7 @@ int Socket_writev(int socket, iobuf* iovecs, int count, unsigned long* bytes)
}
#else
*bytes = 0L;

rc = writev(socket, iovecs, count);
if (rc == SOCKET_ERROR)
{
Expand Down Expand Up @@ -1346,11 +1357,18 @@ int Socket_putdatas(int socket, char* buf0, int buf0len, int count, char** buffe

iovecs[0].iov_base = buf0;
iovecs[0].iov_len = buf0len;
for (i = 0; i < count; i++)

int targetbufs = 0;
for (i = 0; i < count;i++)
{
iovecs[i+1].iov_base = buffers[i];
iovecs[i+1].iov_len = buflens[i];
if(buflens[i] > 0)
{
iovecs[targetbufs+1].iov_base = buffers[i];
iovecs[targetbufs+1].iov_len = buflens[i];
targetbufs++;
}
}
count = targetbufs;

if ((rc = Socket_writev(socket, iovecs, count+1, &bytes)) != SOCKET_ERROR)
{
Expand Down Expand Up @@ -1721,6 +1739,10 @@ int Socket_continueWrites(fd_set* pwset)
}
#endif

#ifdef ESP_PLATFORM
#include "esp_netif.h"
#endif

/**
* Get the hostname of the computer we are running on
* @return the hostname
Expand All @@ -1731,7 +1753,14 @@ char* Socket_gethostname()
#define HOST_NAME_MAX 256 /* 255 is a POSIX limit/256 a Windows max for this call */
#endif
static char buf[HOST_NAME_MAX+1] = "";

#ifdef ESP_PLATFORM
const char *pHostName = buf;
tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &pHostName);
snprintf(buf, HOST_NAME_MAX+1, "%s", pHostName);
#else
gethostname(buf, HOST_NAME_MAX+1);
#endif
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion rsmb/src/tools/be/bersmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class MemList
MemEntry* entry = head;
while (entry != NULL)
{
printf("f0x%lX = m[0x%lX]s[%d]\n", entry->fileaddr, entry->memoryaddr, entry->size);
printf("f0x%lX = m[0x%llX]s[%d]\n", entry->fileaddr, entry->memoryaddr, entry->size);
entry = entry->next;
}
printf("------------\n");
Expand Down