Embedded MQTT-SN C/C++ Client

This library is intended to have these characteristics:

  • use very limited resources - pick and choose the components needed
  • not reliant on any particular libraries for networking, threading or memory management
  • ANSI standard C for maximum portability, at the lowest level
  • optional higher layer(s) in C and/or C++.

The library can be used on desktop operating systems, but is primarily aimed for environments such as mbed and FreeRTOS.

Source

http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/

Download

There are no pre-built downloads available. This code is intended to be used in the smallest pieces needed for the particular embedded system.

Building from source

Gcc

Samples and tests can be built with "build" shell scripts in their respective directories

Documentation

Will be added when ready

Getting Started

Here is the core of a simple publishing program:

int sendPacketBuffer(int asocket, char* host, int port, unsigned char* buf, int buflen)
{
	struct sockaddr_in cliaddr;
	int rc = 0;

	memset(&cliaddr, 0, sizeof(cliaddr));
	cliaddr.sin_family = AF_INET;
	cliaddr.sin_addr.s_addr = inet_addr(host);
	cliaddr.sin_port = htons(port);

	if ((rc = sendto(asocket, buf, buflen, 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr))) == SOCKET_ERROR)
		Socket_error("sendto", asocket);
	else
		rc = 0;
	return rc;
}

int main(int argc, char** argv)
{
	int rc = 0;
	unsigned char buf[200];
	int buflen = sizeof(buf);
	int mysock = 0;
	MQTTSN_topicid topic;
	unsigned char* payload = (unsigned char*)"mypayload";
	int payloadlen = strlen((char*)payload);
	int len = 0, dup = 0, qos = 0, retained = 0, packetid = 0;
	char *host = "127.0.0.1";
	char *topicname = "a long topic name";
	int port = 1883;
	MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;

	printf("Sending to hostname %s port %d\n", host, port);

	mysock = socket(AF_INET, SOCK_DGRAM, 0);
	if (mysock == INVALID_SOCKET)
		rc = Socket_error("socket", mysock);

	options.clientID.cstring = "myclientid";
	len = MQTTSNSerialize_connect(buf, buflen, &options);
	rc = sendPacketBuffer(mysock, host, port, buf, len);

	topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
	topic.data.qos3.longname = topicname;
	topic.data.qos3.longlen = strlen(topicname);
	len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid,
			topic, payload, payloadlen);
	rc = sendPacketBuffer(mysock, host, port, buf, len);

	rc = shutdown(mysock, SHUT_WR);
	rc = close(mysock);

	return 0;
}