Provides Javascript wrappers for MQTT.
Dependencies: mbed-http DEVI2C_JS MQTTPacket FP
MQTT_JS-js.cpp
00001 /* 00002 * @file MQTT_JS-js.cpp 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>© 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 /* Includes ------------------------------------------------------------------*/ 00038 00039 #include "jerryscript-mbed-library-registry/wrap_tools.h" 00040 #include "jerryscript-mbed-event-loop/EventLoop.h" 00041 00042 #include "MQTT_JS.h" 00043 00044 /* Class Implementation ------------------------------------------------------*/ 00045 00046 /** 00047 * MQTT_JS#destructor 00048 * 00049 * Called if/when the MQTT_JS object is GC'ed. 00050 */ 00051 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(MQTT_JS) (void *void_ptr) { 00052 delete static_cast<MQTT_JS*>(void_ptr); 00053 } 00054 00055 /** 00056 * Type infomation of the native MQTT_JS pointer 00057 * 00058 * Set MQTT_JS#destructor as the free callback. 00059 */ 00060 static const jerry_object_native_info_t native_obj_type_info = { 00061 .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(MQTT_JS) 00062 }; 00063 00064 00065 /** 00066 * MQTT_JS#init (native JavaScript method) 00067 * 00068 * Initializes the MQTT service. 00069 */ 00070 DECLARE_CLASS_FUNCTION(MQTT_JS, init) { 00071 CHECK_ARGUMENT_COUNT(MQTT_JS, init, (args_count == 4)); 00072 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 0, string); 00073 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 1, string); 00074 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 2, string); 00075 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 3, string); 00076 00077 size_t id_length = jerry_get_string_length(args[0]); 00078 size_t token_length = jerry_get_string_length(args[1]); 00079 size_t url_length = jerry_get_string_length(args[2]); 00080 size_t port_length = jerry_get_string_length(args[3]); 00081 00082 if(id_length > 32){ 00083 return jerry_create_number(1); 00084 } 00085 if(token_length > 32){ 00086 return jerry_create_number(2); 00087 } 00088 if(url_length > 128){ 00089 return jerry_create_number(3); 00090 } 00091 if(port_length > 16){ 00092 return jerry_create_number(4); 00093 } 00094 00095 // add an extra character to ensure there's a null character after the device name 00096 char* id = (char*)calloc(id_length + 1, sizeof(char)); 00097 jerry_string_to_char_buffer(args[0], (jerry_char_t*)id, id_length); 00098 char* token = (char*)calloc(token_length + 1, sizeof(char)); 00099 jerry_string_to_char_buffer(args[1], (jerry_char_t*)token, token_length); 00100 char* url = (char*)calloc(url_length + 1, sizeof(char)); 00101 jerry_string_to_char_buffer(args[2], (jerry_char_t*)url, url_length); 00102 char* port = (char*)calloc(port_length + 1, sizeof(char)); 00103 jerry_string_to_char_buffer(args[3], (jerry_char_t*)port, port_length); 00104 00105 // Unwrap native MQTT_JS object 00106 void *void_ptr; 00107 const jerry_object_native_info_t *type_ptr; 00108 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00109 00110 if (!has_ptr || type_ptr != &native_obj_type_info) { 00111 return jerry_create_error(JERRY_ERROR_TYPE, 00112 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00113 } 00114 00115 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00116 00117 //int ret = 0; //native_ptr->init(); 00118 NetworkInterface_JS::getInstance()->connect(); 00119 00120 int res = native_ptr->init(NetworkInterface_JS::getInstance()->getNetworkInterface(), 00121 id, token, url, port); 00122 00123 free(id); 00124 free(token); 00125 free(url); 00126 free(port); 00127 00128 return jerry_create_number(res); 00129 } 00130 00131 00132 /** 00133 * MQTT_JS#yield (native JavaScript method) 00134 * 00135 * Waits to receive from the MQTT Broker. 00136 */ 00137 DECLARE_CLASS_FUNCTION(MQTT_JS, yield) { 00138 CHECK_ARGUMENT_COUNT(MQTT_JS, yield, (args_count == 1)); 00139 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, init, 0, number); 00140 00141 int time = jerry_get_number_value(args[0]); 00142 00143 printf("Yielding for %d ms.\n", time); 00144 // Unwrap native MQTT_JS object 00145 void *void_ptr; 00146 const jerry_object_native_info_t *type_ptr; 00147 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00148 00149 if (!has_ptr || type_ptr != &native_obj_type_info) { 00150 return jerry_create_error(JERRY_ERROR_TYPE, 00151 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00152 } 00153 00154 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00155 00156 //int ret = 0; //native_ptr->yield(); 00157 //NetworkInterface_JS::getInstance()->yield(); 00158 00159 int result = native_ptr->yield(time); 00160 00161 return jerry_create_number(result); 00162 } 00163 00164 00165 /** 00166 * MQTT_JS#connect (native JavaScript method) 00167 * 00168 * Connects to the MQTT Broker. 00169 */ 00170 DECLARE_CLASS_FUNCTION(MQTT_JS, connect) { 00171 CHECK_ARGUMENT_COUNT(MQTT_JS, connect, (args_count == 0)); 00172 00173 // Unwrap native MQTT_JS object 00174 void *void_ptr; 00175 const jerry_object_native_info_t *type_ptr; 00176 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00177 00178 if (!has_ptr || type_ptr != &native_obj_type_info) { 00179 return jerry_create_error(JERRY_ERROR_TYPE, 00180 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00181 } 00182 00183 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00184 00185 //int ret = 0; //native_ptr->connect(); 00186 //NetworkInterface_JS::getInstance()->connect(); 00187 00188 int result = native_ptr->connect(NetworkInterface_JS::getInstance()->getNetworkInterface()); 00189 00190 return jerry_create_number(result); 00191 } 00192 00193 00194 /** 00195 * MQTT_JS#publish (native JavaScript method) 00196 * 00197 * Publishes to the MQTT. 00198 */ 00199 DECLARE_CLASS_FUNCTION(MQTT_JS, publish) { 00200 CHECK_ARGUMENT_COUNT(MQTT_JS, subscribe, (args_count == 1)); 00201 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, subscribe, 0, string); 00202 00203 size_t buf_length = jerry_get_string_length(args[0]); 00204 00205 // add an extra character to ensure there's a null character after the device name 00206 char* buf = (char*)calloc(buf_length + 1, sizeof(char)); 00207 jerry_string_to_char_buffer(args[0], (jerry_char_t*)buf, buf_length); 00208 00209 // Unwrap native MQTT_JS object 00210 void *void_ptr; 00211 const jerry_object_native_info_t *type_ptr; 00212 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00213 00214 if (!has_ptr || type_ptr != &native_obj_type_info) { 00215 return jerry_create_error(JERRY_ERROR_TYPE, 00216 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00217 } 00218 00219 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00220 00221 int result = native_ptr->publish(buf); 00222 00223 free(buf); 00224 return jerry_create_number(result); 00225 00226 } 00227 00228 /** 00229 * MQTT_JS#run (native JavaScript method) 00230 * 00231 * Runs the MQTT demo. 00232 */ 00233 DECLARE_CLASS_FUNCTION(MQTT_JS, run) { 00234 CHECK_ARGUMENT_COUNT(MQTT_JS, run, (args_count == 0)); 00235 00236 // Unwrap native MQTT_JS object 00237 void *void_ptr; 00238 const jerry_object_native_info_t *type_ptr; 00239 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00240 00241 if (!has_ptr || type_ptr != &native_obj_type_info) { 00242 return jerry_create_error(JERRY_ERROR_TYPE, 00243 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00244 } 00245 00246 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00247 00248 //int ret = 0; //native_ptr->run(); 00249 NetworkInterface_JS::getInstance()->connect(); 00250 00251 //MQTT_JS *mqtt = new MQTT_JS(); 00252 native_ptr->start_mqtt(NetworkInterface_JS::getInstance()->getNetworkInterface()); 00253 00254 //native_ptr->run(); 00255 00256 return jerry_create_undefined(); 00257 } 00258 00259 DECLARE_CLASS_FUNCTION(MQTT_JS, onSubscribe) { 00260 CHECK_ARGUMENT_COUNT(MQTT_JS, onUpdate, (args_count == 1)); 00261 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, onUpdate, 0, function); 00262 00263 void *void_ptr; 00264 const jerry_object_native_info_t *type_ptr; 00265 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00266 00267 if (!has_ptr || type_ptr != &native_obj_type_info) { 00268 return jerry_create_error(JERRY_ERROR_TYPE, 00269 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00270 } 00271 00272 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00273 00274 jerry_value_t fn = args[0]; 00275 jerry_acquire_value(fn); 00276 00277 00278 //BLEJS* this_ble = &BLEJS::Instance(); 00279 //this_ble->setWriteCallback(native_ptr, f); 00280 int result = native_ptr->onSubscribe(fn); 00281 00282 return jerry_create_number(result); 00283 } 00284 00285 /** 00286 * MQTT_JS#subscribe (native JavaScript method) 00287 * 00288 * Subscribes to MQTT 00289 * 00290 * @param topic 00291 */ 00292 DECLARE_CLASS_FUNCTION(MQTT_JS, subscribe) { 00293 CHECK_ARGUMENT_COUNT(MQTT_JS, subscribe, (args_count == 1)); 00294 CHECK_ARGUMENT_TYPE_ALWAYS(MQTT_JS, subscribe, 0, string); 00295 00296 size_t topic_length = jerry_get_string_length(args[0]); 00297 00298 // add an extra character to ensure there's a null character after the device name 00299 char* topic = (char*)calloc(topic_length + 1, sizeof(char)); 00300 jerry_string_to_char_buffer(args[0], (jerry_char_t*)topic, topic_length); 00301 00302 // Unwrap native MQTT_JS object 00303 void *void_ptr; 00304 const jerry_object_native_info_t *type_ptr; 00305 bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr); 00306 00307 if (!has_ptr || type_ptr != &native_obj_type_info) { 00308 return jerry_create_error(JERRY_ERROR_TYPE, 00309 (const jerry_char_t *) "Failed to get native MQTT_JS pointer"); 00310 } 00311 00312 MQTT_JS *native_ptr = static_cast<MQTT_JS*>(void_ptr); 00313 00314 int result = native_ptr->subscribe(topic); 00315 00316 free(topic); 00317 return jerry_create_number(result); 00318 } 00319 00320 00321 /** 00322 * MQTT_JS (native JavaScript constructor) 00323 * 00324 * @returns a JavaScript object representing the MQTT_JS. 00325 */ 00326 DECLARE_CLASS_CONSTRUCTOR(MQTT_JS) { 00327 CHECK_ARGUMENT_COUNT(MQTT_JS, __constructor, (args_count == 0)); 00328 00329 MQTT_JS *native_ptr = new MQTT_JS(); 00330 00331 jerry_value_t js_object = jerry_create_object(); 00332 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info); 00333 00334 00335 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, run); 00336 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, onSubscribe); 00337 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, init); 00338 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, connect); 00339 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, subscribe); 00340 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, publish); 00341 ATTACH_CLASS_FUNCTION(js_object, MQTT_JS, yield); 00342 00343 return js_object; 00344 }
Generated on Thu Jul 14 2022 04:10:19 by
1.7.2