Real Time Logic LLC / Mbed 2 deprecated SMQ Client-example Featured

Dependencies:   EthernetInterface mbed-rtos mbed

Introduction

The SMQ Architecture is an Internet of Things (IoT) publish subscribe end-to-end solution that is optimized for embedded systems to provide instantaneous Device Edge Node connectivity, 1 to 1 Communications, and Ease of Transcending Firewalls. The solution is ideal for resource constrained devices that require real-time dynamic control, analytic information, and firmware updates in both LAN and WAN environments.

Architecture Component List

  • SMQ C Client (Non-secure)
  • SharkMQ (Secure) C Client
  • SMQjs (Javascript)
  • SMQ JAVA
  • SMQ Broker

/media/uploads/wini/smq_architecture.png

SMQ Client-example

A (non-secure) C Client implementation of the SMQ protocol demonstrating device control over the on board LEDs via any modern (WebSocket enabled) Browser interface.

  • Code Size: 3kB

See Also

How to setup your own SMQ IoT cloud server

Most IoT cloud server solutions, whether they provide ready-to-use hosted services or not, are based on a standard Virtual Private Server (VPS). Most developers probably think of Amazon or Microsoft Azure's services when considering the server side of their IoT solution. These high-end services are great if you need to scale up to millions of connected devices. However, for most small-scale operations and DIY projects, a low-cost VPS is more than adequate.

SimpleMQ/src/seLwIP.c

Committer:
wini
Date:
2014-12-03
Revision:
0:bd3aeb15634e
Child:
2:bac2873dbd15

File content as of revision 0:bd3aeb15634e:

/**
 *     ____             _________                __                _
 *    / __ \___  ____ _/ /_  __(_)___ ___  ___  / /   ____  ____ _(_)____
 *   / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ /   / __ \/ __ `/ / ___/
 *  / _, _/  __/ /_/ / / / / / / / / / / /  __/ /___/ /_/ / /_/ / / /__
 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
 *                                                       /____/
 *
 *                 SharkSSL Embedded SSL/TLS Stack
 ****************************************************************************
 *   PROGRAM MODULE
 *
 *   $Id: seLwIP.c 3617 2014-12-03 23:38:39Z wini $
 *
 *   COPYRIGHT:  Real Time Logic LLC, 2014
 *
 *   This software is copyrighted by and is the sole property of Real
 *   Time Logic LLC.  All rights, title, ownership, or other interests in
 *   the software remain the property of Real Time Logic LLC.  This
 *   software may only be used in accordance with the terms and
 *   conditions stipulated in the corresponding license agreement under
 *   which the software has been supplied.  Any unauthorized use,
 *   duplication, transmission, distribution, or disclosure of this
 *   software is expressly forbidden.
 *
 *   This Copyright notice may not be removed or modified without prior
 *   written consent of Real Time Logic LLC.
 *
 *   Real Time Logic LLC. reserves the right to modify this software
 *   without notice.
 *
 *               http://realtimelogic.com
 *               http://sharkssl.com
 ****************************************************************************
 */


#include <selib.h>

#ifndef SharkSSLLwIP
#error SharkSSLLwIP not defined -> Using incorrect selibplat.h
#endif


int se_accept(SOCKET** listenSock, U32 timeout, SOCKET** outSock)
{
   err_t err;
   memset(*outSock, 0, sizeof(SOCKET));
   if(timeout != INFINITE_TMO)
      netconn_set_recvtimeout((*listenSock)->con, timeout);
  L_accept:
   err = netconn_accept((*listenSock)->con, &(*outSock)->con);
   if(err != ERR_OK)
   {
      if(err == ERR_TIMEOUT)
      {
         if(timeout == INFINITE_TMO)
         {
            netconn_set_recvtimeout((*listenSock)->con, 100000);
            goto L_accept;
         }
         return 0;
      }
      return -1;
   }
   return 1;
}


int se_bind(SOCKET* sock, U16 port)
{
   int err;
   memset(sock, 0, sizeof(SOCKET));
   sock->con = netconn_new(NETCONN_TCP);
   if( ! sock->con )
      return -1;
   if(netconn_bind(sock->con, IP_ADDR_ANY, port) == ERR_OK)
   {
      if(netconn_listen(sock->con) == ERR_OK)
         return 0;
      err = -2;
   }
   else
      err = -3;
   netconn_delete(sock->con);
   sock->con=0;
   return err;
}



/* Returns 0 on success.
   Error codes returned:
   -1: Cannot create socket: Fatal
   -2: Cannot resolve 'address'
   -3: Cannot connect
*/
int se_connect(SOCKET* sock, const char* name, U16 port)
{


   ip_addr_t addr;
   memset(sock, 0, sizeof(SOCKET));
   if(netconn_gethostbyname(name, &addr) != ERR_OK)
      return -2;
   sock->con = netconn_new(NETCONN_TCP);
   if( ! sock->con )
      return -1;
   if(netconn_connect(sock->con, &addr, port) == ERR_OK)
      return 0;
   netconn_delete(sock->con);
   sock->con=0;
   return -3;
}



void se_close(SOCKET* sock)
{
   if(sock->con)
      netconn_delete(sock->con);
   if(sock->nbuf)
      netbuf_delete(sock->nbuf);
   memset(sock, 0, sizeof(SOCKET));
}



S32 se_send(SOCKET* sock, const void* buf, U32 len)
{
   err_t err=netconn_write(sock->con, buf, len, NETCONN_COPY);
   if(err != ERR_OK)
   {
      se_close(sock);
	  return (S32)err;
   }
   return len;
}



S32 se_recv(SOCKET* sock, void* buf, U32 len, U32 timeout)
{
   if( ! sock->nbuf )
   {
      err_t err;
      if(timeout != INFINITE_TMO)
         netconn_set_recvtimeout(sock->con, timeout);
     L_read:
      err = netconn_recv(sock->con, &sock->nbuf);
      if(err != ERR_OK)
      {
         if(err == ERR_TIMEOUT)
         {
            if(timeout == INFINITE_TMO)
            {
               netconn_set_recvtimeout(sock->con, 100000);
               goto L_read;
            }
            return 0;
         }
         se_close(sock);
         return (S32)err;
      }
   }
   if( ! sock->data )
      netbuf_data(sock->nbuf, (void**)&sock->data, &sock->len);
   if(sock->len <= len)
   {
      memcpy(buf,sock->data,sock->len);
      sock->data=0;
      if(netbuf_next(sock->nbuf) < 0)
      {
         netbuf_delete(sock->nbuf);
         sock->nbuf=0;
      }
	  len = sock->len;
   }
   else
   {
      memcpy(buf,sock->data,len);
      sock->data += len;
      sock->len -= len;
   }
   return len;
}



int se_sockValid(SOCKET* sock)
{
   return sock->con != 0;
}