From a5e52274b042cc381ecfd677909c46eabc09e11a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Thu, 1 Feb 2018 23:58:54 -0800 Subject: [PATCH] refactored bus packet so packet cmd: is bus function changed examples accordingly --- .esmrc.json | 3 -- examples/ipc-relay.mjs | 32 +++++++------- examples/ipc-scan.mjs | 22 ++++++---- examples/tcp-relay.mjs | 30 +++++++------- examples/tcp-scan.mjs | 11 ++--- src/bus-packet.mjs | 94 ++++++++++++++++++++++++++++-------------- 6 files changed, 112 insertions(+), 80 deletions(-) delete mode 100644 .esmrc.json diff --git a/.esmrc.json b/.esmrc.json deleted file mode 100644 index f195c03..0000000 --- a/.esmrc.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "@std/esm": "cjs" -} diff --git a/examples/ipc-relay.mjs b/examples/ipc-relay.mjs index 2a12334..e610a86 100644 --- a/examples/ipc-relay.mjs +++ b/examples/ipc-relay.mjs @@ -12,35 +12,35 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let relays = new Base({sockets:'uc', path:PATH, log:true}) + let relays = new Base({sockets:'uc', path:PATH}) relays.reply = function (packet) { // console.log(packet.bus) - console.log('response from relays',packet.bus.func, packet.bus.args.cmd, packet.bus.response) + console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`) } await relays.init() console.log('=============sending============') - let packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd: 0}} } - console.dir(packet.bus) + let packet = {cmd:'read', args:{address:39 ,cmd: 0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 0, byte:0}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:0}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:255}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:9} } + console.dir(packet) await relays.send(packet) await delay(1000) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:0}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:9} } + console.dir(packet) await relays.send(packet) await delay(1000) diff --git a/examples/ipc-scan.mjs b/examples/ipc-scan.mjs index c28fe10..0d6d8ed 100644 --- a/examples/ipc-scan.mjs +++ b/examples/ipc-scan.mjs @@ -3,25 +3,29 @@ * */ -import Bus from '../src/bus-packet' +import Base from '../../uci-base/src/base' + +const PATH = '/opt/uci/uci-base/src/unix.sock' const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { - let i2cbus = new Bus({id:'i2c-bus', sockets:'us,uc'}) + let scanner = new Base({id:'pipe-i2c-client', sockets:'uc', path:PATH}) - i2cbus.reply = function (packet) { - console.log('==== response from i2cbus =>',packet.bus.response) + scanner.reply = function (packet) { + let addresses = packet.response.map(device => { + return device.toString(16)}) + // console.log(packet) + console.log('==== device hex addreses on i2cbus ===\n',addresses) } - - await i2cbus.init() + await scanner.init() console.log('=============sending============') - let packet = {cmd:'rw', bus:{func:'scan'} } + let packet = {cmd:'scan'} console.dir(packet) - await i2cbus.send(packet) + await scanner.send(packet) - await delay(1000) + await delay(3000) process.kill(process.pid, 'SIGTERM') diff --git a/examples/tcp-relay.mjs b/examples/tcp-relay.mjs index d6ac0a7..c9c236a 100644 --- a/examples/tcp-relay.mjs +++ b/examples/tcp-relay.mjs @@ -16,31 +16,31 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) relays.reply = function (packet) { // console.log(packet.bus) - console.log('response from relays',packet.bus.func, packet.bus.args.cmd, packet.bus.response) + console.log(`response from relays for ${packet.cmd_sent}:`,packet.args, `was ${packet.response}`) } await relays.init() console.log('=============sending============') - let packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd: 0}} } - console.dir(packet.bus) + let packet = {cmd:'read', args:{address:39 ,cmd: 0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 0, byte:0}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 0, byte:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:0}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:255}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 9, byte:255} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:9} } + console.dir(packet) await relays.send(packet) await delay(1000) - packet = {cmd:'rw', bus:{func:'write', args:{address:39,cmd: 9, byte:0}} } - console.dir(packet.bus) + packet = {cmd:'write', args:{address:39,cmd: 9, byte:0} } + console.dir(packet) await relays.send(packet) - packet = {cmd:'rw', bus:{func:'read', args:{address:39 ,cmd:9}} } - console.dir(packet.bus) + packet = {cmd:'read', args:{address:39 ,cmd:9} } + console.dir(packet) await relays.send(packet) await delay(1000) diff --git a/examples/tcp-scan.mjs b/examples/tcp-scan.mjs index 85a735d..c4c859e 100644 --- a/examples/tcp-scan.mjs +++ b/examples/tcp-scan.mjs @@ -2,11 +2,8 @@ * A tcp customer/client to talk with the i2c bus and scan the bus for devices * */ - +import Base from '../../uci-base/src/base' const HOST = 'sbc' - - - const delay = time => new Promise(res=>setTimeout(()=>res(),time)) ; (async () => { @@ -14,14 +11,14 @@ const delay = time => new Promise(res=>setTimeout(()=>res(),time)) let scanner = new Base({id:'tcp-i2c-client', sockets:'tc', host:HOST}) scanner.reply = function (packet) { - let addresses = packet.bus.response.map(device => { + let addresses = packet.response.map(device => { return device.toString(16)}) - console.log(packet) + // console.log(packet) console.log('==== device hex addreses on i2cbus ===\n',addresses) } await scanner.init() console.log('=============sending============') - let packet = {cmd:'rw', bus:{func:'scan'} } + let packet = {cmd:'scan'} console.dir(packet) await scanner.send(packet) diff --git a/src/bus-packet.mjs b/src/bus-packet.mjs index 598e0af..a64865d 100644 --- a/src/bus-packet.mjs +++ b/src/bus-packet.mjs @@ -11,66 +11,100 @@ export default class Bus extends Base { this.busnum = opts.busnum || 1 this.i2cbus = i2c.open(this.busnum, () => {}) this.funcs = bus_funcs + this.registerPacketProcessor.bind(this)(process) // this.init = this.init.bind(this) } async init(){ - // console.log('init', this.rw) await super.init() } - async rw (packet){ - // console.dir('=>',packet.bus.fusncs) - if (!packet.bus.func) return {error: 'no i2c bus function in packet', packet: packet.bus } - if (this.funcs[packet.bus.func]) { - packet.bus.response = await this.funcs[packet.bus.func].bind(this)(packet.bus.args) - packet.cmd = 'reply' - return packet - } - return {error: 'no i2c bus function available for packet function', packet: packet.bus } +} // end of Bus Packet Class + +// replace base processor with one for i2c bus functions +const process = async function (packet){ + if (!packet.cmd) return {error: 'no cmd: key in packet', packet: packet } + if (this.context.funcs[packet.cmd]) { + // let res = validateArgs(packet) + // if (res.error) return res.error + packet.cmd_sent = packet.cmd + packet.response = await this.context.funcs[packet.cmd].bind(this.context)(packet.args) + packet.cmd = 'reply' + return packet + } else return {error: 'no i2c bus function available for packet command', packet: packet } +} + +const validateArgs = function (packet) { + let missing = [] + console.log(packet) + if (packet.cmd==='scan' || packet.cmd ==='close') return {} + if (!packet.args.address) missing.push('address') + switch (packet.cmd) + { + case 'readRaw': + case 'writeRaw': + if (!packet.args.length) missing.push('length') + if (!packet.args.buffer) missing.push('buffer') + break + case 'read': + case 'read2': + case 'write': + case 'write2': + if (!packet.args.cmd) missing.push('cmd') } -} // end of Bus Packet Class + switch (packet.cmd) + { + case 'write': + case 'write2': + case 'send': + if (!packet.args.byte) missing.push('byte') + } + + if (missing.length > 0) { + return {error: `following arguments are missing ${missing}`, packet: packet } + } + return {} +} const bus_funcs = { scan: function () { return pify(this.i2cbus.scan).bind(this.i2cbus)() }, close: function () { return pify(this.i2cbus.close).bind(this.i2cbus)() }, - readRaw: function (arg) { - return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(arg.address, arg.length, arg.buffer) + readRaw: function (args) { + return pify(this.i2cbus.i2cRead).bind(this.i2cbus)(args.address, args.length, args.buffer) }, - writeRaw: function (arg) { - return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(arg.address, arg.length, arg.buffer) + writeRaw: function (args) { + return pify(this.i2cbus.i2cWrite).bind(this.i2cbus)(args.address, args.length, args.buffer) }, - read: function (arg) { + read: function (args) { // console.log('read: address, cmd', address, cmd) - return pify(this.i2cbus.readByte).bind(this.i2cbus)(arg.address, arg.cmd) + return pify(this.i2cbus.readByte).bind(this.i2cbus)(args.address, args.cmd) }, - write: function (arg) { - // console.log('write: address, cmd, byte', arg.address, arg.cmd, arg.byte) - return pify(this.i2cbus.writeByte.bind(this.i2cbus))(arg.address, arg.cmd, arg.byte) + write: function (args) { + // console.log('write: address, cmd, byte', args.address, args.cmd, args.byte) + return pify(this.i2cbus.writeByte.bind(this.i2cbus))(args.address, args.cmd, args.byte) }, - read2: function (arg) { - return pify(this.i2cbus.readWord.bind(this.i2cbus))(arg.address, arg.cmd) + read2: function (args) { + return pify(this.i2cbus.readWord.bind(this.i2cbus))(args.address, args.cmd) }, - write2: function (arg) { - return pify(this.i2cbus.writeWord.bind(this.i2cbus))(arg.address, arg.cmd, arg.bytes) + write2: function (args) { + return pify(this.i2cbus.writeWord.bind(this.i2cbus))(args.address, args.cmd, args.byte) }, - receive: function (arg) { + receive: function (args) { // console.log('receivebyte', address) - return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(arg.address) + return pify(this.i2cbus.receiveByte.bind(this.i2cbus))(args.address) }, - send: function (arg) { + send: function (args) { // console.log('sendbyte', address,byte) - return pify(this.i2cbus.sendByte.bind(this.i2cbus))(arg.address, arg.byte) + return pify(this.i2cbus.sendByte.bind(this.i2cbus))(args.address, args.byte) } - -} +} //end i2c functions