MQTT Mosquitto library on FXT009

Hi,

I want to use the MQTT Mosquitto library, which is written in C, on my FXT009.

The library has been linked and added as it should, as you can see in the output. (Ofc I’m not 100% sure it is correct as it isn’t working)

I have the lates DS on my Win7 64bit

As of now, this is my code

#include "adl_global.h"
#include "wip_net.h"
#include "wip_bearer.h"
#include <stdio.h>
#include <mosquitto.h>
#include <string.h>


/***************************************************************************/
/*  prototypes                                                             */
/***************************************************************************/
void MainTask ( void );

/************************************************************************************/
/* Task table definition of tasks entry point, stack size, task name, task priority */
/************************************************************************************/
const adl_InitTasks_t adl_InitTasks [ ] = {
      { MainTask, 4096, "WipSoft", 1 },
      { 0, 0, 0, 0 }
};

wip_bearer_t myBearer = NULL;
s8 ret = 0;

void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
	TRACE((1,"GOT SOMETHING"));

	TRACE((1, message->payload));
}

void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
	if(!result){
		/* Subscribe to broker information topics on successful connect. */
		mosquitto_subscribe(mosq, NULL, "/broker/mferno/test", 0);
	}else{
		TRACE((1,"Connect failed\n"));
	}
}

void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
	int i;

	TRACE((1,"Subscribed (mid: %d): %d", mid, granted_qos[0]));
	for(i=1; i<qos_count; i++){
		TRACE((1,", %d", granted_qos[i]));
	}
}

void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
	/* Pring all log messages regardless of level. */
	TRACE((1, str));
}


void connectToBroker(){

	char id[30];
	char *host = "test.mosquitto.org";
	int port = 1883;
	int keepalive = 60;
	bool clean_session = true;
	struct mosquitto *mosq = NULL;

	mosquitto_lib_init();

	mosq = mosquitto_new(id, clean_session, NULL);
	if(!mosq){
		TRACE((1, "OUT OF MEMORY"));
	}

	mosquitto_log_callback_set(mosq, my_log_callback);
	mosquitto_connect_callback_set(mosq, my_connect_callback);
	mosquitto_message_callback_set(mosq, my_message_callback);
	mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);

	if(mosquitto_connect(mosq, host, port, keepalive)){
		TRACE((1,"Unable to connect"));
	}

	while(!mosquitto_loop(mosq, -1, 1)){
	}

	mosquitto_destroy(mosq);
	mosquitto_lib_cleanup();

}

void bearerHandler(wip_bearer_t bearer, s8 event, void *context){

	switch(event){

		case WIP_BEV_IP_CONNECTED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_IP_CONNECTED>"));

			//Connect to broker
			connectToBroker();

			break;
		}

		case WIP_BEV_CONN_FAILED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_CONN_FAILED>"));
			break;
		}

		case WIP_BEV_STOPPED: {
			TRACE (( 1, "bearerHandler: <WIP_BEV_STOPPED>"));
			break;
		}

		default: {
			TRACE (( 1, "bearerHandler: <DEFAULT>"));
			break;
		}
	}
}

void simHandler (u8 event){

	switch(event){

		case ADL_SIM_EVENT_FULL_INIT:{

			TRACE((1, "ADL_SIM_EVENT_FULL_INIT"));

			ret = wip_bearerOpen(&myBearer, "GPRS", bearerHandler, NULL);
			TRACE((1,"OPEN BEARER: %d",ret));

			ret = wip_bearerSetOpts(myBearer,
					WIP_BOPT_GPRS_APN, "machinetohuman.ts.m2m",
					WIP_BOPT_LOGIN, "",
					WIP_BOPT_PASSWORD, "",
					WIP_BOPT_END);

			TRACE((1,"SET OPTS: %d",ret));

			ret = wip_bearerStart(myBearer);
			TRACE((1, "START: %d", ret));

			break;
		}

		default: {

			TRACE((1, "IN DEFAULT"));

			break;
		}

	}

}


void MainTask ( void ) {

	TRACE((1,"IN MAIN"));

	adl_simSubscribe((adl_simHdlr_f) simHandler, NULL);

	wip_netInit();

}

And the output to the console is this

11:31:14 **** Incremental Build of configuration [Target]_ARM_EABI_GCC_Debug for project MosquittoClient ****
Info: Internal Builder is used for build
"C:\\Program Files (x86)\\Sierra Wireless\\Developer Studio\\tools\\armeabigcc\\4.4.1\\bin\\arm-none-eabi-g++.exe" -Wl,--script,gcc.lkopt "src\\generated.o" "src\\appli.o" -lethernet_driver -lmosquitto -Wl,-( -nostartfiles "-LC:\\workspace\\ethernet_driver\\[Target]_ARM_EABI_GCC_Debug" "-LC:\\workspace\\MosquittoClient\\mosquitto-1.1.3\\lib" -Os -O2 -fshort-enums -fdollars-in-identifiers -ggdb3 -u _siwiStubsImplementation -fomit-frame-pointer -mthumb-interwork -mthumb -march=armv5te -msoft-float -mfpu=fpa -mapcs -mno-apcs-stack-check -Wl,-nmagic -Wl,-Map,MosquittoClient.map "C:\\Program Files (x86)\\Sierra Wireless\\Embedded Software\\com.wavecom.openat.ide.spm.lib.os.model.6.51.0.201206010944_6.51.0.201206010944\\resources\\ADL\\RVDS_ADL.lib" "C:\\Program Files (x86)\\Sierra Wireless\\Embedded Software\\com.wavecom.openat.ide.spm.lib.plugin.model.WIP.5.54.0.201206011257_5.54.0.201206011257\\resources\\WIP\\RVDS_WIP.lib" "C:\\Program Files (x86)\\Sierra Wireless\\Embedded Software\\com.wavecom.openat.ide.spm.lib.plugin.model.WIP.5.54.0.201206011257_5.54.0.201206011257\\resources\\WIP\\RVDS_WIPSoftLib.lib" -Wl,-) -oMosquittoClient.axf 
src\appli.o: In function `connectToBroker':
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:72: undefined reference to `mosquitto_lib_init'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:74: undefined reference to `mosquitto_new'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:79: undefined reference to `mosquitto_log_callback_set'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:80: undefined reference to `mosquitto_connect_callback_set'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:81: undefined reference to `mosquitto_message_callback_set'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:82: undefined reference to `mosquitto_subscribe_callback_set'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:84: undefined reference to `mosquitto_connect'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:88: undefined reference to `mosquitto_loop'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:91: undefined reference to `mosquitto_destroy'
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:92: undefined reference to `mosquitto_lib_cleanup'
src\appli.o: In function `my_connect_callback':
C:\workspace\MosquittoClient\[Target]_ARM_EABI_GCC_Debug/..\src/appli.c:40: undefined reference to `mosquitto_subscribe'
collect2: ld returned 1 exit status

11:31:14 Build Finished (took 284ms)

I have no idea how to go from here and am in desperate need of help.

Best regards

Duplicate of http://forum.sierrawireless.com/viewtopic.php?f=116&t=6935&hilit=MQTT+Mosquitto+library+on+FXT009

I removed that thread now.

Should not make duplicate threads, sorry.

I realized this was a better forum section for the question, hence I made a new one.

Br

Something just hit me:

I am not too experienced with toolchains etc. but might my problem be that I’m trying to use a lib that is built with regular gcc with an application built with gcc arm eabi ??

I am now trying to build the Mosquitto lib with the arm eabi compiler.

Will post the result when done.

BR

I tried to build the mosquitto library with the arm-none-eabi but I get this error:

make CC=arm-none-eabi-gcc LD=arm-none-eabi-ld CXX=arm-none-eabi-g++ CFLAGS=-I/Users/Mathias/arm-cs-tools/arm-none-eabi/include/ LDFLAGS=-l/Users/Mathias/arm-cs-tools/arm-none-eabi/lib/
set -e; for d in lib client src; do make -C ${d}; done
arm-none-eabi-gcc -I/Users/Mathias/arm-cs-tools/arm-none-eabi/include/  -I. -I.. -I../lib -fPIC -c mosquitto.c -o mosquitto.o
mosquitto.c:36:24: fatal error: sys/select.h: No such file or directory
compilation terminated.
make[1]: *** [mosquitto.o] Error 1
make: *** [mosquitto] Error 2

And if I remove the inclusion of sys/select.h, other includes are not found etc.

Anyone have a clue?

Hi,

One thing is…MQTT protocol is not part of the open AT offerings.

Thanks.

Hi!

Yes I know, that is why I am trying to make Mosquitto work…

BR

Does a file named select.h exist in a folder named sys relative to an include path specified by your project :question:

Not sure if I understand your question, but in mosquitto.c (one of the library files) it includes sys/select.h and that library does not seem to be recognized with the ARM EABI or something similar. When building the library with the regular gcc, no problem at all.

Best regards

That’s a relative path - isn’t it :question:

So you have to correctly cofigure your project so that the compiler knows what it’s relative to :exclamation:

This is nothing specifically to do with Open-AT - it applies to ‘C’ programming in general.

select.h is just a header file - it is not a library.

What do you mean by that?

#include <sys/select.h> - Yes

To be clear, I am now trying to build Mosquitto with a arm-none-eabi-gcc compiler on my OSX. If/when that is done, Im going to put that on my windows distribution where my DS lies etc.

I mean when I build the library on my computer with the standard gcc i686-apple-darwin11 that comes with the OSX.

Unfortunately, Mosquitto is written with a POSIX-like OS in mind: it’s expected to run in its own blocking thread, using Unix’ network-multiplexing features such as select().

Converting such a code base into the callback-driven style mandated by Open AT represents a significant amount of work, probably measured in men-month. It also requires some proficiency with both Unix and Open AT development.

I did read this thread about almost a year ago. In Vietnam, we did try to archive the same target, but on a Maestro hardware (OpenAT OS). At first, lib mosquitto seem quite difficult for building with OpenAT OS tool chain. After turning around so many great MQTT library (eclipse M&M taho, some MQTT library for arduino) out there (at 2013,2014 time). IMHO, lib-mosquitto is the most compatible MQTT library for porting to OpenAT OS environment. Mainly because of its clear coding style and It was written to support multiple platform (POSIX, Win32). Thanks to lib mosquitto author.