-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathserial_chrome_socket.js
121 lines (106 loc) · 3.87 KB
/
serial_chrome_socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
Copyright (c) 2014 Espruino Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Patrick Van Oosterwijck (patrick@silicognition.com)
**/
(function() {
if (typeof chrome === 'undefined' || chrome.sockets===undefined) {
console.log("No chrome.sockets - Chrome Socket disabled");
return;
}
function init() {
Espruino.Core.Config.add("SERIAL_TCPIP", {
section : "Communications",
name : "Connect over TCP Address",
description : "When connecting, add a menu item to connect to a given TCP/IP address (eg. `192.168.1.2` or `192.168.1.2:23`). Leave blank to disable. Separate multiple ip addresses with a semi-colon.",
type : "string",
defaultValue : "",
});
}
var connectionInfo;
var readListener;
var connectionDisconnectCallback;
var connectionReadCallback;
var getPorts = function(callback) {
if (Espruino.Config.SERIAL_TCPIP.trim() != "") {
var ips = Espruino.Config.SERIAL_TCPIP.trim().split(";");
var portList = [];
ips.forEach(function(s) {
s = s.trim();
if (s.length) portList.push({path:'TCP/IP: '+s, description:"Network connection", type:"socket"});
})
callback(portList, true/*instantPorts*/);
} else
callback(undefined, true/*instantPorts*/);
};
var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
if (serialPort.substr(0,8)!='TCP/IP: ') {
console.error("Invalid connection "+JSON.stringify(serialPort));
return;
}
var host = serialPort.substr(8);
var port = 23;
if (host.indexOf(":") >= 0) {
var i = host.indexOf(":");
port = parseInt(host.substr(i+1).trim());
host = host.substr(0,i).trim();
if (host=="") host="localhost";
}
connectionReadCallback = receiveCallback;
connectionDisconnectCallback = disconnectCallback;
chrome.sockets.tcp.create({}, function(createInfo) {
chrome.sockets.tcp.connect(createInfo.socketId,
host, port, function (result) {
if (result < 0) {
console.log("Failed to open socket " + host+":"+port);
openCallback(undefined);
} else {
connectionInfo = { socketId: createInfo.socketId };
openCallback(connectionInfo);
}
});
});
};
var closeSerial = function() {
if (connectionInfo) {
chrome.sockets.tcp.disconnect(connectionInfo.socketId,
function () {
connectionInfo=null;
connectionDisconnectCallback();
connectionDisconnectCallback = undefined;
});
}
};
var writeSerial = function(data, callback) {
chrome.sockets.tcp.send(connectionInfo.socketId, Espruino.Core.Utils.stringToArrayBuffer(data), callback);
};
// ----------------------------------------------------------
chrome.sockets.tcp.onReceive.addListener(function(info) {
if (info.socketId != connectionInfo.socketId)
return;
if (connectionReadCallback!==undefined)
connectionReadCallback(info.data);
});
chrome.sockets.tcp.onReceiveError.addListener(function(info) {
if (info.socketId != connectionInfo.socketId)
return;
console.error("RECEIVE ERROR:", JSON.stringify(info));
connectionDisconnectCallback();
});
Espruino.Core.Serial.devices.push({
"name" : "Chrome Socket",
"init" : init,
"getPorts": getPorts,
"open": openSerial,
"write": writeSerial,
"close": closeSerial,
});
})();