start of JS-based network stuff

This commit is contained in:
Gordon Williams 2015-03-13 16:48:47 +00:00
parent ad2023fccb
commit cbe4067493
7 changed files with 217 additions and 0 deletions

View File

@ -671,6 +671,12 @@ SOURCES += \
libs/network/network.c \
libs/network/socketserver.c
#
WRAPPERSOURCES += libs/network/js/jswrap_jsnetwork.c
INCLUDE += -I$(ROOT)/libs/network/js
SOURCES += \
libs/network/js/network_js.c
ifdef LINUX
INCLUDE += -I$(ROOT)/libs/network/linux
SOURCES += \

View File

@ -0,0 +1,50 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Implementation of JsNetwork that calls into JavaScript
* ----------------------------------------------------------------------------
*/
#include "jswrap_jsnetwork.h"
#include "jshardware.h"
#include "jsinteractive.h"
#include "network.h"
#include "network_js.h"
/*JSON{
"type" : "library",
"class" : "NetworkJS"
}
Library that initialises a network device that calls into JavaScript
*/
/*JSON{
"type" : "staticmethod",
"class" : "NetworkJS",
"name" : "create",
"generate" : "jswrap_networkjs_create",
"params" : [
["obj","JsVar","An object containing functions to access the network device"]
],
"return" : ["JsVar","The object passed in"]
}
Initialise the WIZnet module and return an Ethernet object
*/
JsVar *jswrap_networkjs_create(JsVar *obj) {
JsNetwork net;
networkCreate(&net, JSNETWORKTYPE_JS);
networkSet(&net);
networkState = NETWORKSTATE_ONLINE;
return obj;
}

View File

@ -0,0 +1,16 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Implementation of JsNetwork that calls into JavaScript
* ----------------------------------------------------------------------------
*/
#include "jsvar.h"
JsVar *jswrap_networkjs_create(JsVar *obj);

View File

@ -0,0 +1,126 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Implementation of JsNetwork that calls into JavaScript
* ----------------------------------------------------------------------------
*/
#include "jsinteractive.h"
#include "network.h"
#include "network_js.h"
#include "jswrap_stream.h"
#define JSNET_NAME "JSN"
#define JSNET_DNS_NAME "DNS"
// ------------------------------------------------------------------------------------------------------------------------
/// Get an IP address from a name. Sets out_ip_addr to 0 on failure
void net_js_gethostbyname(JsNetwork *net, char * hostName, unsigned long* out_ip_addr) {
// hacky - save the last checked name so we can put it straight into the request
*out_ip_addr = 0xFFFFFFFF;
jsvUnLock(jsvObjectSetChild(execInfo.hiddenRoot, JSNET_DNS_NAME, jsvNewFromString(hostName)));
}
/// Called on idle. Do any checks required for this device
void net_js_idle(JsNetwork *net) {
}
/// Call just before returning to idle loop. This checks for errors and tries to recover. Returns true if no errors.
bool net_js_checkError(JsNetwork *net) {
return true;
}
/// if host=0, creates a server otherwise creates a client (and automatically connects). Returns >=0 on success
int net_js_createsocket(JsNetwork *net, uint32_t host, unsigned short port) {
JsVar *hostVar = 0;
if (host!=0) {
// client
if (host==0xFFFFFFFF) {
hostVar = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_DNS_NAME, 0);
}
if (!hostVar)
hostVar = networkGetAddressAsString((unsigned char *)&host, 4,10,'.');
}
// else server, hostVar=0
JsVar *netObj = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_NAME, 0);
JsVar *args[2] = {
hostVar,
jsvNewFromInteger(port)
};
int sckt = jsvGetIntegerAndUnLock(jspCallNamedFunction(net, "create", 2, args));
jsvUnLock(args[0]);
jsvUnLock(args[1]);
jsvUnLock(netObj);
return sckt;
}
/// destroys the given socket
void net_js_closesocket(JsNetwork *net, int sckt) {
JsVar *netObj = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_NAME, 0);
JsVar *args[1] = {
jsvNewFromInteger(sckt)
};
jspCallNamedFunction(net, "recv", 2, args);
jsvUnLock(args[0]);
jsvUnLock(netObj);
}
/// If the given server socket can accept a connection, return it (or return < 0)
int net_js_accept(JsNetwork *net, int serverSckt) {
JsVar *netObj = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_NAME, 0);
JsVar *args[1] = {
jsvNewFromInteger(serverSckt)
};
int sckt = jsvGetIntegerAndUnLock(jspCallNamedFunction(net, "accept", 1, args));
jsvUnLock(args[0]);
jsvUnLock(netObj);
return sckt;
}
/// Receive data if possible. returns nBytes on success, 0 on no data, or -1 on failure
int net_js_recv(JsNetwork *net, int sckt, void *buf, size_t len) {
JsVar *netObj = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_NAME, 0);
JsVar *args[1] = {
jsvNewFromInteger(sckt)
};
int r = jsvGetIntegerAndUnLock(jspCallNamedFunction(net, "recv", 1, args));
jsvUnLock(args[0]);
jsvUnLock(netObj);
return r;
}
/// Send data if possible. returns nBytes on success, 0 on no data, or -1 on failure
int net_js_send(JsNetwork *net, int sckt, const void *buf, size_t len) {
JsVar *netObj = jsvObjectGetChild(execInfo.hiddenRoot, JSNET_NAME, 0);
JsVar *args[2] = {
jsvNewFromInteger(sckt),
jsvNewFromEmptyString()
};
jsvAppendStringBuf(args[1], buf, len);
int r = jsvGetIntegerAndUnLock(jspCallNamedFunction(net, "send", 2, args));
jsvUnLock(args[0]);
jsvUnLock(args[1]);
jsvUnLock(netObj);
return r;
}
// ------------------------------------------------------------------------------------------------------------------------
void netSetCallbacks_js(JsNetwork *net) {
net->idle = net_js_idle;
net->checkError = net_js_checkError;
net->createsocket = net_js_createsocket;
net->closesocket = net_js_closesocket;
net->accept = net_js_accept;
net->gethostbyname = net_js_gethostbyname;
net->recv = net_js_recv;
net->send = net_js_send;
}

View File

@ -0,0 +1,16 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Implementation of JsNetwork that calls into JavaScript
* ----------------------------------------------------------------------------
*/
#include "network.h"
void netSetCallbacks_js(JsNetwork *net);

View File

@ -26,6 +26,7 @@
#if defined(LINUX)
#include "network_linux.h"
#endif
#include "network_js.h"
JsNetworkState networkState =
#ifdef LINUX
@ -153,6 +154,7 @@ bool networkGetFromVar(JsNetwork *net) {
#if defined(LINUX)
case JSNETWORKTYPE_SOCKET : netSetCallbacks_linux(net); break;
#endif
case JSNETWORKTYPE_JS : netSetCallbacks_js(net); break;
default:
jsError("Unknown network device %d", net->data.type);
networkFree(net);

View File

@ -36,6 +36,7 @@ typedef enum {
JSNETWORKTYPE_CC3000, ///< TI CC3000 support
JSNETWORKTYPE_W5500, ///< WIZnet W5500 support
JSNETWORKTYPE_ESP8266, ///< ExpressIF ESP8266 support
JSNETWORKTYPE_JS, ///< JavaScript network type
} JsNetworkType;
typedef struct {