Provides Javascript wrappers for MQTT.

Dependencies:   mbed-http DEVI2C_JS MQTTPacket FP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTT_JS.h Source File

MQTT_JS.h

00001 /*
00002  * @file    MQTT_JS.h
00003  * @author  ST
00004  * @version V1.0.0
00005  * @date    9 October 2017
00006  * @brief   Implementation of MQTT for Javascript.
00007  ******************************************************************************
00008  * @attention
00009  *
00010  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00011  *
00012  * Redistribution and use in source and binary forms, with or without modification,
00013  * are permitted provided that the following conditions are met:
00014  *   1. Redistributions of source code must retain the above copyright notice,
00015  *      this list of conditions and the following disclaimer.
00016  *   2. Redistributions in binary form must reproduce the above copyright notice,
00017  *      this list of conditions and the following disclaimer in the documentation
00018  *      and/or other materials provided with the distribution.
00019  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00020  *      may be used to endorse or promote products derived from this software
00021  *      without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  ******************************************************************************
00035  */
00036 
00037 /* Prevent recursive inclusion -----------------------------------------------*/
00038 
00039 #ifndef _MQTT_JS_H_
00040 #define _MQTT_JS_H_
00041 
00042 /* Includes ------------------------------------------------------------------*/
00043 
00044 #include "mbed.h"
00045 #include "TCPSocket.h"
00046 #include "MQTTClient.h"
00047 #include "MQTTNetwork.h"
00048 #include "MQTTmbed.h"
00049 
00050 #include "NetworkInterface_JS.h"
00051 
00052 #include "jerryscript-mbed-library-registry/wrap_tools.h"
00053 
00054 #include <ctype.h>
00055 
00056 /* Constants -----------------------------------------------------------------*/
00057 
00058 #define MQTT_MAX_PACKET_SIZE 250
00059 #define MQTT_MAX_PAYLOAD_SIZE 300
00060 
00061 #define MAX_SSID_LEN   80
00062 #define MAX_PASSW_LEN  80
00063 
00064 #define HTTP_BROKER_URL "http://customer.cloudmqtt.com/login"
00065 
00066 typedef void (* subscribeCallbackType)(MQTT::MessageData & msgMQTT);
00067 
00068 /* Class Declaration ---------------------------------------------------------*/
00069 
00070 /**
00071  * Abstract class of MQTT for Javascript.
00072  */
00073 class MQTT_JS{    
00074 private:    
00075     char ssid[MAX_SSID_LEN];
00076     char seckey[MAX_PASSW_LEN]; 
00077 
00078     char id[32];
00079     char topic[32];
00080     char auth_token[32];
00081     char hostname[128];
00082     char port[16];
00083 
00084     int connack_rc; // MQTT connack return code
00085     char* ip_addr;
00086     char type[30];
00087     bool netConnecting;
00088     int connectTimeout;
00089     bool mqttConnecting;
00090     bool netConnected;
00091     bool connected;
00092     int retryAttempt;
00093     char subscription_url[300];
00094     MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> * client;
00095     MQTTNetwork* mqttNetwork;
00096 
00097     static jerry_value_t onSubscribeCallback;
00098 
00099 public:
00100 
00101     /* Constructors */
00102     MQTT_JS ();
00103     
00104     /* Destructors */
00105     ~MQTT_JS ();
00106 
00107     /* Functions */
00108     
00109     NetworkInterface* getNetworkInterface();
00110 
00111     int onSubscribe(jerry_value_t cb);
00112     int onDisconnect(jerry_value_t cb, subscribeCallbackType fn);
00113 
00114     static void subscribe_cb(MQTT::MessageData & msgMQTT);
00115 
00116     int init(NetworkInterface* network, char* _id, char* _token, char* _url, char* _port);
00117 
00118     int connect();
00119 
00120     int subscribe(char *pubTopic);
00121 
00122     int unsubscribe(char *pubTopic);
00123 
00124     int connect(NetworkInterface* network);
00125 
00126     int getConnTimeout(int attemptNumber);
00127 
00128     void attemptConnect(NetworkInterface* network) ;
00129 
00130     int publish(char* buf, int n = 0);
00131 
00132     int yield(int time);
00133 
00134     int start_mqtt(NetworkInterface* network);
00135 };
00136 
00137 #endif