diff mbox series

Add nodejs binding for progress interface

Message ID 20230704103342.44447-1-sbabic@denx.de
State Accepted
Delegated to: Stefano Babic
Headers show
Series Add nodejs binding for progress interface | expand

Commit Message

Stefano Babic July 4, 2023, 10:33 a.m. UTC
Add javascript module that can be used bz nodejs applications to follow
the update via progress interface.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 bindings/nodejs/index.js     | 90 ++++++++++++++++++++++++++++++++++++
 bindings/nodejs/package.json | 17 +++++++
 2 files changed, 107 insertions(+)
 create mode 100644 bindings/nodejs/index.js
 create mode 100644 bindings/nodejs/package.json
diff mbox series

Patch

diff --git a/bindings/nodejs/index.js b/bindings/nodejs/index.js
new file mode 100644
index 00000000..c30a7d8d
--- /dev/null
+++ b/bindings/nodejs/index.js
@@ -0,0 +1,90 @@ 
+var net = require('net');
+var c = require('c-struct');
+
+const msgsize = 2416;
+
+var progressmsg = new c.Schema({
+    magic: c.type.uint32,
+    status: c.type.uint32,
+    dwl_percent : c.type.uint32,
+    dwl_bytes_0 : c.type.uint32,
+    dwl_bytes_1 : c.type.uint32,
+    dwl_bytes_2 : c.type.uint32,
+    nsteps : c.type.uint32,
+    curstep : c.type.uint32,
+    curpercent : c.type.uint32,
+    curimage : c.type.string(256),
+    handler : c.type.string(64),
+    source : c.type.uint32,
+    infolen : c.type.uint32,
+    info : c.type.string(2048)
+})
+
+c.register('Progress', progressmsg);
+
+const statusMap = [
+   "IDLE",
+   "START",
+   "RUN",
+   "SUCCESS",
+   "FAILURE",
+   "DOWNLOAD",
+   "DONE",
+   "SUBPROGRESS",
+   "PROGRESS"
+];
+
+class SWUpdate {
+  constructor(ctrlpath, progresspath, mycallback) {
+    this.ctrlsocket = ctrlpath;
+    this.progresssocket = progresspath;
+    this.mycallback = mycallback;
+    this.connected = false;
+    if (typeof this.mycallback != 'function')
+	console.log("Wrong callback !");
+  }
+
+  isConnected() {
+	return this.connected;
+  }
+
+  receive = (msgsize, buffer) => {
+	var out = [];
+	if (!msgsize)
+	    return;
+	out = (c.unpackSync('Progress', buffer))
+	var status = out.status;
+	out.status = statusMap[out.status];
+	var json = JSON.stringify(out);
+	this.mycallback(json);
+  }
+
+  progress() {
+    if (this.connected) {
+      console.log('WRONG: already connected to SWUpdate');
+      return false;
+    }
+    var clientProgress = net.createConnection({
+	path : this.progresssocket,
+	onread: {
+		buffer : Buffer.alloc(msgsize),
+		callback : this.receive
+	}
+    }, () => {
+        this.connected = true;
+    });
+
+    clientProgress.on('end', () => {
+      console.log('disconnected from server');
+    });
+    clientProgress.on('close', () => {
+      this.connected = false;
+      console.log('close from server');
+    });
+    clientProgress.on('error', () => {
+      console.log('error connecting to SWUpdate');
+    });
+  }
+}
+
+module.exports = (ctrl, progress, fun) => {return new SWUpdate(ctrl, progress, fun)};
diff --git a/bindings/nodejs/package.json b/bindings/nodejs/package.json
new file mode 100644
index 00000000..af38f9be
--- /dev/null
+++ b/bindings/nodejs/package.json
@@ -0,0 +1,17 @@ 
+{
+  "name": "swupdateprog",
+  "version": "1.0.0",
+  "description": "Node Module to connect to SWUpdate",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "keywords": [
+    "SWUpdate"
+  ],
+  "author": "Stefano Babic <stefano.babic@swupdate.org>",
+  "license": "MIT",
+  "dependencies": {
+    "c-struct": "^0.0.5"
+  }
+}