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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port of libmosquitto for genode #12

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions repos/libports/lib/import/import-libmosquitto.mk
@@ -0,0 +1,2 @@
LIBMOSQUITTO_PORT_DIR := $(call select_from_ports,libmosquitto)
INC_DIR += $(LIBMOSQUITTO_PORT_DIR)/src/lib/libmosquitto/lib/ $(LIBMOSQUITTO_PORT_DIR)/src/lib/libmosquitto/lib/cpp
31 changes: 31 additions & 0 deletions repos/libports/lib/mk/libmosquitto.mk
@@ -0,0 +1,31 @@
LIBMOSQUITTO_DIR := $(call select_from_ports,libmosquitto)/src/lib/libmosquitto

SRC_LIBMOSQUITTO := logging_mosq.c \
memory_mosq.c \
messages_mosq.c \
mosquitto.c \
net_mosq.c \
read_handle.c \
read_handle_client.c \
read_handle_shared.c \
send_client_mosq.c \
send_mosq.c \
socks_mosq.c \
srv_mosq.c \
thread_mosq.c \
time_mosq.c \
tls_mosq.c \
util_mosq.c \
will_mosq.c

INC_DIR += $(LIBMOSQUITTO_DIR) $(LIBMOSQUITTO_DIR)/lib $(LIBMOSQUITTO_DIR)/lib/cpp/

SRC_CC = $(addprefix $(LIBMOSQUITTO_DIR)/lib/, $(SRC_LIBMOSQUITTO)) $(LIBMOSQUITTO_DIR)/lib/cpp/mosquittopp.cpp

LIBS += libc libc_lwip lwip pthread stdcxx libssl

CC_DEF += -DWITH_TLS -DWITH_TLS_PSK -DWITH_EC -DWITH_SOCKS -DWITH_THREADING

SHARED_LIB = yes

CC_OPT += -O2
1 change: 1 addition & 0 deletions repos/libports/ports/libmosquitto.hash
@@ -0,0 +1 @@
c12001a88ffc6f51cc5f8e23e50078eb4e2ce5cc
9 changes: 9 additions & 0 deletions repos/libports/ports/libmosquitto.port
@@ -0,0 +1,9 @@
VERSION := 1.4.12
DOWNLOADS := mosquitto.archive
LICENSE := other

URL(mosquitto) := https://mosquitto.org/files/source/mosquitto-$(VERSION).tar.gz
SHA(mosquitto) := 1451547e56bf4d33ea156cbc21f1d12acb58318b
DIR(mosquitto) := src/lib/libmosquitto

PATCHES := src/lib/libmosquitto/net_mosq.patch
35 changes: 35 additions & 0 deletions repos/libports/src/lib/libmosquitto/net_mosq.patch
@@ -0,0 +1,35 @@
+++ src/lib/libmosquitto/lib/net_mosq.c
@@ -1172,7 +1172,6 @@ int _mosquitto_socket_nonblock(mosq_sock_t sock)
#ifndef WITH_BROKER
int _mosquitto_socketpair(mosq_sock_t *pairR, mosq_sock_t *pairW)
{
-#ifdef WIN32
int family[2] = {AF_INET, AF_INET6};
int i;
struct sockaddr_storage ss;
@@ -1278,25 +1277,5 @@ int _mosquitto_socketpair(mosq_sock_t *pairR, mosq_sock_t *pairW)
return MOSQ_ERR_SUCCESS;
}
return MOSQ_ERR_UNKNOWN;
-#else
- int sv[2];
-
- if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1){
- return MOSQ_ERR_ERRNO;
- }
- if(_mosquitto_socket_nonblock(sv[0])){
- COMPAT_CLOSE(sv[0]);
- COMPAT_CLOSE(sv[1]);
- return MOSQ_ERR_ERRNO;
- }
- if(_mosquitto_socket_nonblock(sv[1])){
- COMPAT_CLOSE(sv[0]);
- COMPAT_CLOSE(sv[1]);
- return MOSQ_ERR_ERRNO;
- }
- *pairR = sv[0];
- *pairW = sv[1];
- return MOSQ_ERR_SUCCESS;
-#endif
}
#endif
5 changes: 5 additions & 0 deletions repos/libports/src/test/libports/libmosquitto/target.mk
@@ -0,0 +1,5 @@
TARGET = test-libmosquitto
LIBS = libmosquitto
SRC_CC = main.cc

vpath main.cc $(PRG_DIR)/..
103 changes: 103 additions & 0 deletions repos/mpct/run/mpct.run
@@ -0,0 +1,103 @@
set use_usb_driver [expr [have_spec omap4] || [have_spec arndale] || [have_spec rpi]]
set use_nic_driver [expr !$use_usb_driver && ![have_spec imx53] && ![have_spec hw_odroid_xu] && ![have_spec linux] && ![have_spec hw_wand_quad]]

if {[expr !$use_usb_driver && !$use_nic_driver]} {
puts "\n Run script is not supported on this platform. \n"; exit 0 }

set build_components { core init mpct drivers/timer drivers/nic }

lappend_if $use_usb_driver build_components drivers/usb
lappend_if [have_spec gpio] build_components drivers/gpio

source ${genode_dir}/repos/base/run/platform_drv.inc
append_platform_drv_build_components

build $build_components

create_boot_directory

#
# Generate config
#

set config {
<config>
<parent-provides>
<service name="LOG"/>
<service name="RM"/>
<service name="ROM"/>
<service name="RAM"/>
<service name="IO_MEM"/>
<service name="IO_PORT"/>
<service name="IRQ"/>
<service name="CAP"/>
<service name="SIGNAL"/>
</parent-provides>
<default-route>
<any-service> <parent/> <any-child/> </any-service>
</default-route>
<start name="timer">
<resource name="RAM" quantum="1M"/>
<provides><service name="Timer"/></provides>
</start>
<start name="mpct">
<resource name="RAM" quantum="4M"/>
<config>
<network dhcp="no" ip-address="192.168.100.42" subnet-mask="255.255.255.0" default-gateway="192.168.100.254" />
<mosquitto ip-address="192.168.100.254" port="1883" />
</config>
</start>}

append_if [have_spec gpio] config {
<start name="gpio_drv">
<resource name="RAM" quantum="4M"/>
<provides><service name="Gpio"/></provides>
<config/>
</start>}

append_if $use_usb_driver config {
<start name="usb_drv">
<resource name="RAM" quantum="64M"/>
<provides>
<service name="Nic"/>
</provides>
<config uhci="yes" ehci="yes" xhci="yes">
<nic mac="02:00:00:00:01:01"/>
</config>
</start>}

append_platform_drv_config

append_if $use_nic_driver config {
<start name="nic_drv">
<resource name="RAM" quantum="4M"/>
<provides><service name="Nic"/></provides>
</start>}

append config {
</config>
}

install_config $config

#
# Boot image
#

append qemu_args "-net vde,sock=/tmp/switch1 "
append qemu_args "-net nic,model=lan9118,macaddr=02:00:00:00:01:01 "

set boot_modules { core init mpct ld.lib.so libc.lib.so stdcxx.lib.so libm.lib.so pthread.lib.so timer lwip.lib.so libmosquitto.lib.so libssl.lib.so libcrypto.lib.so }

# platform-specific modules
lappend_if $use_usb_driver boot_modules usb_drv
lappend_if $use_nic_driver boot_modules nic_drv
lappend_if [have_spec gpio] boot_modules gpio_drv

append_platform_drv_boot_modules

build_boot_image $boot_modules

append qemu_args " -nographic "

run_genode_until forever
128 changes: 128 additions & 0 deletions repos/mpct/src/mpct/main.cpp
@@ -0,0 +1,128 @@
/* mosquitto includes */
#include <mosquittopp.h>

/* genode includes */
#include <base/env.h>
#include <base/printf.h>
#include <util/xml_node.h>
#include <os/config.h>

/* lwip includes */
extern "C" {
#include <lwip/sockets.h>
#include <lwip/api.h>
}
#include <lwip/genode.h>
#include <nic/packet_allocator.h>

/* etc */
#include <cstdio>
#include <cstring>

class MPCT : public mosqpp::mosquittopp {
public:
MPCT(const char* id, const char* host, int port) : mosquittopp(id) {
/* init the library */
mosqpp::lib_init();

int keepalive = 60;
MPCT::connect(host, port, keepalive);
}

/* connect callback */
void on_connect(int ret) {
PDBG("Connected with code %d!", ret);

MPCT::my_publish();
}

/* publish callback */
void on_publish(int ret) {
PDBG("Published with code %d!", ret);

MPCT::my_publish();
}

/* disconnect callback */
void on_disconnect(int ret) {
PDBG("Disconnected with code %d!", ret);
}

/* error callback */
void on_error() {
PDBG("Error!");
}


private:
char buffer[1024] = { 0 };
int i = 0, ret = -1;

void my_publish() {
sprintf(buffer, "Hello World: %d", i);
ret = MPCT::publish(NULL, "mpct", strlen(buffer), buffer);
PDBG("Publish '%s' successful: %d", buffer, MOSQ_ERR_SUCCESS == ret);
i++;
};
};

int main(int argc, char* argv[]) {
//lwip_tcpip_init(); /* causes freeze, code works fine without it */

enum { BUF_SIZE = Nic::Packet_allocator::DEFAULT_PACKET_SIZE * 128 };

Genode::Xml_node network = Genode::config()->xml_node().sub_node("network");

if (network.attribute_value<bool>("dhcp", true)) {
PDBG("DHCP network...");
if (lwip_nic_init(0,
0,
0,
BUF_SIZE,
BUF_SIZE)) {
PERR("lwip init failed!");
return 1;
}
PDBG("done");
} else {
PDBG("manual network...");
char ip_addr[16] = {0};
char subnet[16] = {0};
char gateway[16] = {0};

network.attribute("ip-address").value(ip_addr, sizeof(ip_addr));
network.attribute("subnet-mask").value(subnet, sizeof(subnet));
network.attribute("default-gateway").value(gateway, sizeof(gateway));

if (lwip_nic_init(inet_addr(ip_addr),
inet_addr(subnet),
inet_addr(gateway),
BUF_SIZE,
BUF_SIZE)) {
PERR("lwip init failed!");
return 1;
}
PDBG("done");
}

/* get config */
Genode::Xml_node mosquitto = Genode::config()->xml_node().sub_node("mosquitto");

char ip_addr[16] = {0};
char port[5] = {0};

mosquitto.attribute("ip-address").value(ip_addr, sizeof(ip_addr));
mosquitto.attribute("port").value(port, sizeof(port));

/* create new mosquitto peer */
PDBG("mosquitto init");
class MPCT *mpct = new MPCT("mpct", ip_addr, atoi(port));
PDBG("done");

/* endless loop with auto reconnect */
mpct->loop_forever();

/* cleanup */
mosqpp::lib_cleanup();
return 0;
}
4 changes: 4 additions & 0 deletions repos/mpct/src/mpct/target.mk
@@ -0,0 +1,4 @@
TARGET = mpct
SRC_CC = main.cpp
INC_DIR += $(REP_DIR)/../libports/include/lwip
LIBS = base libmosquitto stdcxx lwip pthread