Provides Javascript wrappers for MQTT.

Dependencies:   mbed-http DEVI2C_JS MQTTPacket FP

Committer:
kajasu
Date:
Mon Feb 03 09:36:20 2020 +0000
Revision:
1:a8ffffd1beab
Parent:
0:f4dbe435e64c
TT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 1 /*
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 2 * @file MQTT_JS-js.cpp
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 3 * @author ST
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 4 * @version V1.0.0
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 5 * @date 9 October 2017
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 6 * @brief Implementation of MQTT for Javascript.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 7 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 8 * @attention
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 9 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 10 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 11 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 12 * Redistribution and use in source and binary forms, with or without modification,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 13 * are permitted provided that the following conditions are met:
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 14 * 1. Redistributions of source code must retain the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 15 * this list of conditions and the following disclaimer.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 17 * this list of conditions and the following disclaimer in the documentation
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 18 * and/or other materials provided with the distribution.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 19 * 3. Neither the name of STMicroelectronics nor the names of its contributors
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 20 * may be used to endorse or promote products derived from this software
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 21 * without specific prior written permission.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 22 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 33 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 34 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 35 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 36
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 37 /* Includes ------------------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 38
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 39 #include "jerryscript-mbed-library-registry/wrap_tools.h"
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 40 #include "jerryscript-mbed-event-loop/EventLoop.h"
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 41
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 42 #include "MQTT_JS.h"
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 43
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 44 /* Class Implementation ------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 45
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 46 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 47 * MQTT_JS#destructor
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 48 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 49 * Called if/when the MQTT_JS object is GC'ed.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 50 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 51 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(MQTT_JS) (void *void_ptr) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 52 delete static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 53 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 54
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 55 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 56 * Type infomation of the native MQTT_JS pointer
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 57 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 58 * Set MQTT_JS#destructor as the free callback.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 59 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 60 static const jerry_object_native_info_t native_obj_type_info = {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 61 .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(MQTT_JS)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 62 };
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 63
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 64
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 65 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 66 * MQTT_JS#init (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 67 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 68 * Initializes the MQTT service.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 69 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 70 DECLARE_CLASS_FUNCTION(MQTT_JS, init) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 71 CHECK_ARGUMENT_COUNT(MQTT_JS, init, (args_count == 4));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 72 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 0, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 73 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 1, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 74 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 2, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 75 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 3, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 76
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 77 size_t id_length = jerry_get_string_length(args[0]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 78 size_t token_length = jerry_get_string_length(args[1]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 79 size_t url_length = jerry_get_string_length(args[2]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 80 size_t port_length = jerry_get_string_length(args[3]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 81
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 82 if(id_length > 32){
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 83 return jerry_create_number(1);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 84 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 85 if(token_length > 32){
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 86 return jerry_create_number(2);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 87 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 88 if(url_length > 128){
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 89 return jerry_create_number(3);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 90 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 91 if(port_length > 16){
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 92 return jerry_create_number(4);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 93 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 94
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 95 // add an extra character to ensure there's a null character after the device name
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 96 char* id = (char*)calloc(id_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 97 jerry_string_to_char_buffer(args[0], (jerry_char_t*)id, id_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 98 char* token = (char*)calloc(token_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 99 jerry_string_to_char_buffer(args[1], (jerry_char_t*)token, token_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 100 char* url = (char*)calloc(url_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 101 jerry_string_to_char_buffer(args[2], (jerry_char_t*)url, url_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 102 char* port = (char*)calloc(port_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 103 jerry_string_to_char_buffer(args[3], (jerry_char_t*)port, port_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 104
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 105 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 106 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 107 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 108 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 109
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 110 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 111 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 112 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 113 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 114
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 115 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 116
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 117 //int ret = 0; //native_ptr->init();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 118 NetworkInterface_JS::getInstance()->connect();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 119
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 120 int res = native_ptr->init(NetworkInterface_JS::getInstance()->getNetworkInterface(),
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 121 id, token, url, port);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 122
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 123 free(id);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 124 free(token);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 125 free(url);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 126 free(port);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 127
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 128 return jerry_create_number(res);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 129 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 130
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 131
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 132 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 133 * MQTT_JS#yield (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 134 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 135 * Waits to receive from the MQTT Broker.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 136 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 137 DECLARE_CLASS_FUNCTION(MQTT_JS, yield) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 138 CHECK_ARGUMENT_COUNT(MQTT_JS, yield, (args_count == 1));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 139 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 0, number);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 140
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 141 int time = jerry_get_number_value(args[0]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 142
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 143 printf("Yielding for %d ms.\n", time);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 144 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 145 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 146 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 147 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 148
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 149 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 150 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 151 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 152 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 153
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 154 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 155
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 156 //int ret = 0; //native_ptr->yield();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 157 //NetworkInterface_JS::getInstance()->yield();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 158
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 159 int result = native_ptr->yield(time);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 160
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 161 return jerry_create_number(result);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 162 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 163
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 164
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 165 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 166 * MQTT_JS#connect (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 167 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 168 * Connects to the MQTT Broker.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 169 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 170 DECLARE_CLASS_FUNCTION(MQTT_JS, connect) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 171 CHECK_ARGUMENT_COUNT(MQTT_JS, connect, (args_count == 0));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 172
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 173 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 174 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 175 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 176 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 177
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 178 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 179 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 180 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 181 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 182
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 183 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 184
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 185 //int ret = 0; //native_ptr->connect();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 186 //NetworkInterface_JS::getInstance()->connect();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 187
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 188 int result = native_ptr->connect(NetworkInterface_JS::getInstance()->getNetworkInterface());
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 189
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 190 return jerry_create_number(result);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 191 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 192
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 193
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 194 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 195 * MQTT_JS#publish (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 196 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 197 * Publishes to the MQTT.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 198 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 199 DECLARE_CLASS_FUNCTION(MQTT_JS, publish) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 200 CHECK_ARGUMENT_COUNT(MQTT_JS, subscribe, (args_count == 1));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 201 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, subscribe, 0, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 202
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 203 size_t buf_length = jerry_get_string_length(args[0]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 204
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 205 // add an extra character to ensure there's a null character after the device name
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 206 char* buf = (char*)calloc(buf_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 207 jerry_string_to_char_buffer(args[0], (jerry_char_t*)buf, buf_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 208
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 209 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 210 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 211 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 212 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 213
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 214 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 215 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 216 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 217 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 218
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 219 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 220
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 221 int result = native_ptr->publish(buf);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 222
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 223 free(buf);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 224 return jerry_create_number(result);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 225
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 226 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 227
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 228 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 229 * MQTT_JS#run (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 230 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 231 * Runs the MQTT demo.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 232 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 233 DECLARE_CLASS_FUNCTION(MQTT_JS, run) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 234 CHECK_ARGUMENT_COUNT(MQTT_JS, run, (args_count == 0));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 235
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 236 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 237 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 238 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 239 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 240
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 241 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 242 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 243 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 244 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 245
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 246 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 247
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 248 //int ret = 0; //native_ptr->run();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 249 NetworkInterface_JS::getInstance()->connect();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 250
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 251 //MQTT_JS *mqtt = new MQTT_JS();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 252 native_ptr->start_mqtt(NetworkInterface_JS::getInstance()->getNetworkInterface());
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 253
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 254 //native_ptr->run();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 255
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 256 return jerry_create_undefined();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 257 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 258
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 259 DECLARE_CLASS_FUNCTION(MQTT_JS, onSubscribe) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 260 CHECK_ARGUMENT_COUNT(MQTT_JS, onUpdate, (args_count == 1));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 261 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, onUpdate, 0, function);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 262
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 263 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 264 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 265 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 266
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 267 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 268 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 269 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 270 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 271
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 272 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 273
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 274 jerry_value_t fn = args[0];
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 275 jerry_acquire_value(fn);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 276
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 277
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 278 //BLEJS* this_ble = &BLEJS::Instance();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 279 //this_ble->setWriteCallback(native_ptr, f);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 280 int result = native_ptr->onSubscribe(fn);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 281
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 282 return jerry_create_number(result);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 283 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 284
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 285 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 286 * MQTT_JS#subscribe (native JavaScript method)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 287 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 288 * Subscribes to MQTT
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 289 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 290 * @param topic
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 291 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 292 DECLARE_CLASS_FUNCTION(MQTT_JS, subscribe) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 293 CHECK_ARGUMENT_COUNT(MQTT_JS, subscribe, (args_count == 1));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 294 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, subscribe, 0, string);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 295
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 296 size_t topic_length = jerry_get_string_length(args[0]);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 297
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 298 // add an extra character to ensure there's a null character after the device name
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 299 char* topic = (char*)calloc(topic_length + 1, sizeof(char));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 300 jerry_string_to_char_buffer(args[0], (jerry_char_t*)topic, topic_length);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 301
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 302 // Unwrap native MQTT_JS object
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 303 void *void_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 304 const jerry_object_native_info_t *type_ptr;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 305 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 306
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 307 if (!has_ptr || type_ptr != &native_obj_type_info) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 308 return jerry_create_error(JERRY_ERROR_TYPE,
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 309 (const jerry_char_t *) "Failed to get native MQTT_JS pointer");
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 310 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 311
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 312 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 313
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 314 int result = native_ptr->subscribe(topic);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 315
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 316 free(topic);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 317 return jerry_create_number(result);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 318 }
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 319
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 320
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 321 /**
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 322 * MQTT_JS (native JavaScript constructor)
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 323 *
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 324 * @returns a JavaScript object representing the MQTT_JS.
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 325 */
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 326 DECLARE_CLASS_CONSTRUCTOR(MQTT_JS) {
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 327 CHECK_ARGUMENT_COUNT(MQTT_JS, __constructor, (args_count == 0));
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 328
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 329 MQTT_JS *native_ptr = new MQTT_JS();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 330
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 331 jerry_value_t js_object = jerry_create_object();
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 332 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 333
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 334
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 335 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, run);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 336 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, onSubscribe);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 337 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, init);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 338 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, connect);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 339 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, subscribe);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 340 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, publish);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 341 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, yield);
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 342
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 343 return js_object;
akhtar.syedzeeshan@gmail.com 0:f4dbe435e64c 344 }