XMPPClient

/media/uploads/markfs/xmppmbed.png

Background

After publishes of MQTT client for mbed, i wen to study on another messaging protocol, Extensible Messaging and Presence Protocol, or XMPP. Compared with MQTT protocol, the XMPP protocol has more security and other features, and more advanced. The XMPP is open sourced and their open source communities have contributed rich standards and applications. The XMPP is good for real time applications, like instant messaging and real time gaming. What a nice protocol, my new goal is to see if a XMPP client is feasible for mbed platform.

Journey

Like what i did on developing MQTT client for mbed, my first goal is to write a simple program be able to send “hello” to XMPP clients running on PC. First, i reviewed few PC based XMPP client written on C\C++, like the libstrope and gloox. Both of them support XMPP core protocol, but they are too complex and contain thousands lines due to high complexity of the XMPP. So any attempts to modify them to fit them on mbed will be time consuming and hard works. Thus, i decided to write a simplest XMPP client for mbed which only works on specified condition. The key function is to send and receive messages, other complex features and error handling are not required at this stage.

XMPP client for mbed

This is a simple XMPP client for mbed.

v1.0 Features:

  1. Be able to build TCP connection with server via port 5222.
  2. Support PLAIN authorization.
  3. Support one to one chart.

Limitations:

  1. Unable to handle any error streams and stanzas.
  2. Total size of sending and receiving message stanzas can not excess 1024 bytes.
  3. IQ stanzas are not supported as well as complex presence stanzas.

Requirements:

  1. Ethernet socket or Ethernet shield.
  2. EthernetNetIf library @: http://mbed.org/users/donatien/programs/EthernetNetIf/5z422/docs/
  3. ejabberd server or other jabber server.
  4. Two jabber account, and they subscribe to each other.

Example: An echo client.

Here is small program, an echo client, based on XMPPClient library. The program starts with building a TCP connection with jabber server, and then selects PLAIN authorization and sends user name and password of a pre-registed jabber account. Once he authorization is granted, it opens a session with server and sends presence and few notice messages to a specified jabber client. Then it starts to listen on incoming messages and sends back once it detected them.

I run ejabberd on mac as XMPP server. Two accounts are pre registed on the serer, namely, mbed@ceit.org and mirror@ceit.org. They have subscribed to each other. The mbed@ceit.org is logged on by mbed, and mirror@ceit.org is used to run on mac. The server domain is “ceit.org”, and the IP address of server is 10.1.1.5.

Test platform:

A mbed NXP 1768 + Sparkfun expansion board + Dlink router + mac 460

Close look of mbed+expansion board:

345

Codes:

main.cpp

#include "mbed.h"
#include "EthernetNetIf.h"
#include "XMPPClient.h"

EthernetNetIf ethernet;    

char userName[] = "mbed";
char password[] = "mirror";
char serverDomain[] = "ceit.org";    
IpAddr serverIpAddr(10,1,1,5);

void callback_msg(char* msg);

XMPPClient xmpp(serverIpAddr, serverDomain, callback_msg);

void callback_msg(char* msg){
    printf("Message: %s\r\n\r\n", msg);
    xmpp.send_message("mirror@ceit.org", msg);
}

int main() {
  
    printf("\r\n############### XMPP TCP client #######\r\n\r\n");
    
    EthernetErr ethErr = ethernet.setup();
    if(ethErr){
        printf("Ethernet Error %d\r\n", ethErr);  
    } else {
        printf("mbed is online...\r\n");
    }
    
    if(xmpp.connect(userName, password)){
        printf("\r\nConnect to server sucessed ..\r\n");
    } else {
        printf("\r\nConnect to server failed ..\r\n");
        return -1;
    }
    
    xmpp.send_message("mirror@ceit.org", "Im in Brisbane...");
    xmpp.send_message("mirror@ceit.org", "Now mbed is echoing what u say...");

    printf("\r\nEchoing ..\r\n");
    
    int flag = 0;
    /*Echoing for 300s or 5mins*/
    while(flag < 300){
        Net::poll();
        wait(1);
        flag++;
    }
    
    xmpp.close();
    
    printf("#### End of the test.. ####\r\n\r\n");
}

Results:

The picture below shows a jabber client PSI main window when the mbed has established a session with server and sends out the presence stanza. The bright yellow star on name of mbed@ceit.org shows the mbed is online.

/media/uploads/markfs/psi.png

The screenshot below is a chat window of PSI between mbed@ceit.org and mirror@ceit.org. Echoing messages can be seen clearly. /media/uploads/markfs/echo.png

API

XMPPClient(IpAddr server, char* serverDomain, void (*callback)(char*));
Description: Class constructor, create a XMPP client instance.
Parameters:

  1. server: IP address of server.
  2. serverDomain: Ejabberd server domain.
  3. callback: Call back function to be called when a message stanza arrives.

int connect(char* user, char* pass);
Description: Establish a TCP connection with server and open a new session for given user name.
Parameters:

  1. user: User name of JID.
  2. pass: Password of this JID.

Returns:
1: Session creation successes.

int send_message(char* to, char* msg);
Description: Send a message stanza to other clients.
Parameters:

  1. to: JID of target clients, either bare of full JID are fine.
  2. msg: Message to be send out. (Currently total message stanza size is limited smaller than 1024).

Returns:
1: If message send correctly.

void close();
Description: Client send ending stream to server and disconnect TCP connection with it.

More resources

This library is first published at my CEIT blog. How i developed this library and more information about XMPP can be found at my other posts.


All wikipages