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.

Committer:
wini
Date:
Fri Mar 25 23:07:22 2016 +0000
Revision:
2:bac2873dbd15
Parent:
0:bd3aeb15634e
Updated to latest SMQ source base

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wini 0:bd3aeb15634e 1 /**
wini 0:bd3aeb15634e 2 * ____ _________ __ _
wini 0:bd3aeb15634e 3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
wini 0:bd3aeb15634e 4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
wini 0:bd3aeb15634e 5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
wini 0:bd3aeb15634e 6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
wini 0:bd3aeb15634e 7 * /____/
wini 0:bd3aeb15634e 8 *
wini 0:bd3aeb15634e 9 * SharkSSL Embedded SSL/TLS Stack
wini 0:bd3aeb15634e 10 ****************************************************************************
wini 0:bd3aeb15634e 11 * PROGRAM MODULE
wini 0:bd3aeb15634e 12 *
wini 2:bac2873dbd15 13 * $Id: seLwIP.c 3749 2015-07-28 20:30:23Z wini $
wini 0:bd3aeb15634e 14 *
wini 2:bac2873dbd15 15 * COPYRIGHT: Real Time Logic LLC, 2014 - 2015
wini 0:bd3aeb15634e 16 *
wini 0:bd3aeb15634e 17 * This software is copyrighted by and is the sole property of Real
wini 0:bd3aeb15634e 18 * Time Logic LLC. All rights, title, ownership, or other interests in
wini 0:bd3aeb15634e 19 * the software remain the property of Real Time Logic LLC. This
wini 0:bd3aeb15634e 20 * software may only be used in accordance with the terms and
wini 0:bd3aeb15634e 21 * conditions stipulated in the corresponding license agreement under
wini 0:bd3aeb15634e 22 * which the software has been supplied. Any unauthorized use,
wini 0:bd3aeb15634e 23 * duplication, transmission, distribution, or disclosure of this
wini 0:bd3aeb15634e 24 * software is expressly forbidden.
wini 0:bd3aeb15634e 25 *
wini 0:bd3aeb15634e 26 * This Copyright notice may not be removed or modified without prior
wini 0:bd3aeb15634e 27 * written consent of Real Time Logic LLC.
wini 0:bd3aeb15634e 28 *
wini 0:bd3aeb15634e 29 * Real Time Logic LLC. reserves the right to modify this software
wini 0:bd3aeb15634e 30 * without notice.
wini 0:bd3aeb15634e 31 *
wini 0:bd3aeb15634e 32 * http://realtimelogic.com
wini 0:bd3aeb15634e 33 * http://sharkssl.com
wini 0:bd3aeb15634e 34 ****************************************************************************
wini 0:bd3aeb15634e 35 */
wini 0:bd3aeb15634e 36
wini 0:bd3aeb15634e 37
wini 0:bd3aeb15634e 38 #include <selib.h>
wini 0:bd3aeb15634e 39
wini 0:bd3aeb15634e 40 #ifndef SharkSSLLwIP
wini 0:bd3aeb15634e 41 #error SharkSSLLwIP not defined -> Using incorrect selibplat.h
wini 0:bd3aeb15634e 42 #endif
wini 0:bd3aeb15634e 43
wini 0:bd3aeb15634e 44
wini 2:bac2873dbd15 45 #if LWIP_SO_RCVTIMEO != 1
wini 2:bac2873dbd15 46 #error LWIP_SO_RCVTIMEO must be set
wini 2:bac2873dbd15 47 #endif
wini 2:bac2873dbd15 48
wini 2:bac2873dbd15 49 #ifndef netconn_set_recvtimeout
wini 2:bac2873dbd15 50 #define OLD_LWIP
wini 2:bac2873dbd15 51 #define netconn_set_recvtimeout(conn, timeout) \
wini 2:bac2873dbd15 52 ((conn)->recv_timeout = (timeout))
wini 2:bac2873dbd15 53 #endif
wini 2:bac2873dbd15 54
wini 2:bac2873dbd15 55
wini 2:bac2873dbd15 56
wini 2:bac2873dbd15 57
wini 0:bd3aeb15634e 58 int se_accept(SOCKET** listenSock, U32 timeout, SOCKET** outSock)
wini 0:bd3aeb15634e 59 {
wini 0:bd3aeb15634e 60 err_t err;
wini 0:bd3aeb15634e 61 memset(*outSock, 0, sizeof(SOCKET));
wini 2:bac2873dbd15 62 netconn_set_recvtimeout(
wini 2:bac2873dbd15 63 (*listenSock)->con, timeout == INFINITE_TMO ? 0 : timeout);
wini 2:bac2873dbd15 64 #ifdef OLD_LWIP
wini 2:bac2873dbd15 65 (*outSock)->con = netconn_accept((*listenSock)->con);
wini 2:bac2873dbd15 66 err = (*outSock)->con->err;
wini 2:bac2873dbd15 67 if(!(*outSock)->con && !err) err = ERR_CONN;
wini 2:bac2873dbd15 68 #else
wini 0:bd3aeb15634e 69 err = netconn_accept((*listenSock)->con, &(*outSock)->con);
wini 2:bac2873dbd15 70 #endif
wini 0:bd3aeb15634e 71 if(err != ERR_OK)
wini 0:bd3aeb15634e 72 {
wini 2:bac2873dbd15 73 return err == ERR_TIMEOUT ? 0 : -1;
wini 0:bd3aeb15634e 74 }
wini 0:bd3aeb15634e 75 return 1;
wini 0:bd3aeb15634e 76 }
wini 0:bd3aeb15634e 77
wini 0:bd3aeb15634e 78
wini 0:bd3aeb15634e 79 int se_bind(SOCKET* sock, U16 port)
wini 0:bd3aeb15634e 80 {
wini 0:bd3aeb15634e 81 int err;
wini 0:bd3aeb15634e 82 memset(sock, 0, sizeof(SOCKET));
wini 0:bd3aeb15634e 83 sock->con = netconn_new(NETCONN_TCP);
wini 0:bd3aeb15634e 84 if( ! sock->con )
wini 0:bd3aeb15634e 85 return -1;
wini 0:bd3aeb15634e 86 if(netconn_bind(sock->con, IP_ADDR_ANY, port) == ERR_OK)
wini 0:bd3aeb15634e 87 {
wini 0:bd3aeb15634e 88 if(netconn_listen(sock->con) == ERR_OK)
wini 0:bd3aeb15634e 89 return 0;
wini 0:bd3aeb15634e 90 err = -2;
wini 0:bd3aeb15634e 91 }
wini 0:bd3aeb15634e 92 else
wini 0:bd3aeb15634e 93 err = -3;
wini 0:bd3aeb15634e 94 netconn_delete(sock->con);
wini 0:bd3aeb15634e 95 sock->con=0;
wini 0:bd3aeb15634e 96 return err;
wini 0:bd3aeb15634e 97 }
wini 0:bd3aeb15634e 98
wini 0:bd3aeb15634e 99
wini 0:bd3aeb15634e 100
wini 0:bd3aeb15634e 101 /* Returns 0 on success.
wini 0:bd3aeb15634e 102 Error codes returned:
wini 0:bd3aeb15634e 103 -1: Cannot create socket: Fatal
wini 0:bd3aeb15634e 104 -2: Cannot resolve 'address'
wini 0:bd3aeb15634e 105 -3: Cannot connect
wini 0:bd3aeb15634e 106 */
wini 0:bd3aeb15634e 107 int se_connect(SOCKET* sock, const char* name, U16 port)
wini 0:bd3aeb15634e 108 {
wini 2:bac2873dbd15 109 #ifdef OLD_LWIP
wini 2:bac2873dbd15 110 struct ip_addr addr;
wini 2:bac2873dbd15 111 #else
wini 0:bd3aeb15634e 112 ip_addr_t addr;
wini 2:bac2873dbd15 113 #endif
wini 0:bd3aeb15634e 114 memset(sock, 0, sizeof(SOCKET));
wini 0:bd3aeb15634e 115 if(netconn_gethostbyname(name, &addr) != ERR_OK)
wini 0:bd3aeb15634e 116 return -2;
wini 0:bd3aeb15634e 117 sock->con = netconn_new(NETCONN_TCP);
wini 0:bd3aeb15634e 118 if( ! sock->con )
wini 0:bd3aeb15634e 119 return -1;
wini 0:bd3aeb15634e 120 if(netconn_connect(sock->con, &addr, port) == ERR_OK)
wini 0:bd3aeb15634e 121 return 0;
wini 0:bd3aeb15634e 122 netconn_delete(sock->con);
wini 0:bd3aeb15634e 123 sock->con=0;
wini 0:bd3aeb15634e 124 return -3;
wini 0:bd3aeb15634e 125 }
wini 0:bd3aeb15634e 126
wini 0:bd3aeb15634e 127
wini 0:bd3aeb15634e 128
wini 0:bd3aeb15634e 129 void se_close(SOCKET* sock)
wini 0:bd3aeb15634e 130 {
wini 0:bd3aeb15634e 131 if(sock->con)
wini 0:bd3aeb15634e 132 netconn_delete(sock->con);
wini 0:bd3aeb15634e 133 if(sock->nbuf)
wini 0:bd3aeb15634e 134 netbuf_delete(sock->nbuf);
wini 0:bd3aeb15634e 135 memset(sock, 0, sizeof(SOCKET));
wini 0:bd3aeb15634e 136 }
wini 0:bd3aeb15634e 137
wini 0:bd3aeb15634e 138
wini 0:bd3aeb15634e 139
wini 0:bd3aeb15634e 140 S32 se_send(SOCKET* sock, const void* buf, U32 len)
wini 0:bd3aeb15634e 141 {
wini 0:bd3aeb15634e 142 err_t err=netconn_write(sock->con, buf, len, NETCONN_COPY);
wini 0:bd3aeb15634e 143 if(err != ERR_OK)
wini 0:bd3aeb15634e 144 {
wini 0:bd3aeb15634e 145 se_close(sock);
wini 0:bd3aeb15634e 146 return (S32)err;
wini 0:bd3aeb15634e 147 }
wini 0:bd3aeb15634e 148 return len;
wini 0:bd3aeb15634e 149 }
wini 0:bd3aeb15634e 150
wini 0:bd3aeb15634e 151
wini 0:bd3aeb15634e 152
wini 2:bac2873dbd15 153 S32 se_recv(SOCKET* sock, void* data, U32 len, U32 timeout)
wini 0:bd3aeb15634e 154 {
wini 2:bac2873dbd15 155 int rlen;
wini 2:bac2873dbd15 156 netconn_set_recvtimeout(sock->con, timeout == INFINITE_TMO ? 0 : timeout);
wini 0:bd3aeb15634e 157 if( ! sock->nbuf )
wini 0:bd3aeb15634e 158 {
wini 0:bd3aeb15634e 159 err_t err;
wini 2:bac2873dbd15 160 sock->pbOffs = 0;
wini 2:bac2873dbd15 161 #ifdef OLD_LWIP
wini 2:bac2873dbd15 162 sock->nbuf = netconn_recv(sock->con);
wini 2:bac2873dbd15 163 err = sock->con->err;
wini 2:bac2873dbd15 164 if(!sock->nbuf && !err) err = ERR_CONN;
wini 2:bac2873dbd15 165 #else
wini 0:bd3aeb15634e 166 err = netconn_recv(sock->con, &sock->nbuf);
wini 2:bac2873dbd15 167 #endif
wini 2:bac2873dbd15 168 if(ERR_OK != err)
wini 0:bd3aeb15634e 169 {
wini 2:bac2873dbd15 170 if(sock->nbuf)
wini 2:bac2873dbd15 171 netbuf_delete(sock->nbuf);
wini 2:bac2873dbd15 172 sock->nbuf=0;
wini 2:bac2873dbd15 173 return err == ERR_TIMEOUT ? 0 : (S32)err;
wini 0:bd3aeb15634e 174 }
wini 0:bd3aeb15634e 175 }
wini 2:bac2873dbd15 176 rlen=(int)netbuf_copy_partial(sock->nbuf,(U8*)data,len,sock->pbOffs);
wini 2:bac2873dbd15 177 if(!rlen)
wini 2:bac2873dbd15 178 return -1;
wini 2:bac2873dbd15 179 sock->pbOffs += rlen;
wini 2:bac2873dbd15 180 if(sock->pbOffs >= netbuf_len(sock->nbuf))
wini 0:bd3aeb15634e 181 {
wini 2:bac2873dbd15 182 netbuf_delete(sock->nbuf);
wini 2:bac2873dbd15 183 sock->nbuf=0;
wini 0:bd3aeb15634e 184 }
wini 2:bac2873dbd15 185 return rlen;
wini 0:bd3aeb15634e 186 }
wini 0:bd3aeb15634e 187
wini 0:bd3aeb15634e 188
wini 0:bd3aeb15634e 189
wini 0:bd3aeb15634e 190 int se_sockValid(SOCKET* sock)
wini 0:bd3aeb15634e 191 {
wini 0:bd3aeb15634e 192 return sock->con != 0;
wini 0:bd3aeb15634e 193 }