diff --git a/lib/dbuffer.js b/lib/dbuffer.js index 4df7af2a..a963ba8f 100644 --- a/lib/dbuffer.js +++ b/lib/dbuffer.js @@ -106,8 +106,8 @@ DBusBuffer.prototype.readVariant = function readVariant() { // var arrayBlobLength = this.readInt32(); // return this.readArray(parseSignature(signatur //} - var tree = parseSignature(signature); - return [tree, this.readStruct(tree)]; + var tree = parseSignature.single(signature); + return [signature, this.readTree(tree)]; } DBusBuffer.prototype.readStruct = function readStruct(struct) { diff --git a/lib/header-signature.json b/lib/header-signature.json deleted file mode 100644 index 70827c60..00000000 --- a/lib/header-signature.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - { - "type": "a", - "child": [ - { - "type": "(", - "child": [ - { - "type": "y", - "child": [] - }, - { - "type": "v", - "child": [] - } - ] - } - ] - } -] diff --git a/lib/marshall.js b/lib/marshall.js index 9b16c037..f91291be 100644 --- a/lib/marshall.js +++ b/lib/marshall.js @@ -77,16 +77,8 @@ function write(ps, ele, data) { child: [] }; write(ps, signatureEle, data[0]); - if (data[0].length == 1) { - var variantEle = { - type: data[0], - child: [] - }; - write(ps, variantEle, data[1]); - } else { - var tree = parseSignature(data[0]); - writeStruct(ps, tree, data[1]); - } + var variantEle = parseSignature.single(data[0]); + write(ps, variantEle, data[1]); break; default: return writeSimple(ps, ele.type, data); diff --git a/lib/message.js b/lib/message.js index 65e95185..78b4acdf 100644 --- a/lib/message.js +++ b/lib/message.js @@ -1,9 +1,10 @@ const marshall = require('./marshall.js'); const constants = require('./constants.js'); -const signature = require('./signature.js'); +const parseSignature = require('./signature.js'); const DBusBuffer = require('./dbuffer.js'); const unmarshall = require('./unmarshall.js'); -const headerSignature = require('./header-signature.json'); + +const headerSignature = parseSignature.single('(yv)'); module.exports.unmarshalMessages = function messageParser(stream, onMessage) { var state = 0; // 0: header, 1: fields + body @@ -30,7 +31,7 @@ module.exports.unmarshalMessages = function messageParser(stream, onMessage) { state = 0; var messageBuffer = new DBusBuffer(fieldsAndBody); - var unmarshalledHeader = messageBuffer.readArray(headerSignature[0].child[0], fieldsLength); + var unmarshalledHeader = messageBuffer.readArray(headerSignature, fieldsLength); messageBuffer.align(3); var headerName; var message = {}; @@ -38,7 +39,7 @@ module.exports.unmarshalMessages = function messageParser(stream, onMessage) { for (var i = 0; i < unmarshalledHeader.length; ++i) { headerName = constants.headerTypeName[unmarshalledHeader[i][0]]; - message[headerName] = unmarshalledHeader[i][1][1][0]; + message[headerName] = unmarshalledHeader[i][1][1]; } message.type = header[1]; @@ -61,13 +62,14 @@ module.exports.unmarshall = function unmarshall(buff) { var msgBuf = new DBusBuffer(buff); var headers = msgBuf.read('yyyyuua(yv)'); var message = {}; - for (var i = 0; i < headers[6].length; ++i) { - headerName = constants.headerTypeName[headers[6][i][0]]; - message[headerName] = headers[6][i][1][1][0]; - } message.type = headers[1]; message.flags = headers[2]; message.serial = headers[5]; + headers = headers[6]; + for (var i = 0; i < headers.length; ++i) { + var headerName = constants.headerTypeName[headers[i][0]]; + message[headerName] = headers[i][1][1]; + } msgBuf.align(3); message.body = msgBuf.read(message.signature); return message; diff --git a/lib/signature.js b/lib/signature.js index 49dbd00b..1f9be69a 100644 --- a/lib/signature.js +++ b/lib/signature.js @@ -10,7 +10,7 @@ var knownTypes = {}; knownTypes[c] = true; }); -var exports = module.exports = function(signature) { +function parseSignature(signature) { var index = 0; function next() { @@ -57,24 +57,36 @@ var exports = module.exports = function(signature) { return ret; }; -module.exports.fromTree = function(tree) { +parseSignature.single = function(signature) { + if (signature.length == 1) + return { type: signature[0], child: [] }; + + var trees = parseSignature(signature); + if (trees.length !== 1) + throw new Error('Signature is not a single type'); + return trees[0]; +}; + +parseSignature.fromTree = function(tree) { var res = ''; for (var i=0; i < tree.length; ++i) { if (tree[i].child.length === 0) res += tree[i].type; else { if (tree[i].type === 'a') { - res += 'a' + module.exports.fromTree(tree[i].child); + res += 'a' + parseSignature.fromTree(tree[i].child); } else { - res += tree[i].type + module.exports.fromTree(tree[i].child) + match[tree[i].type]; + res += tree[i].type + parseSignature.fromTree(tree[i].child) + match[tree[i].type]; } } } return res; -} +}; + +module.exports = parseSignature; // command-line test -//console.log(JSON.stringify(module.exports(process.argv[2]), null, 4)); -//var tree = module.exports('a(ssssbbbbbbbbuasa{ss}sa{sv})a(ssssssbbssa{ss}sa{sv})a(ssssssbsassa{sv})'); +//console.log(JSON.stringify(parseSignature(process.argv[2]), null, 4)); +//var tree = parseSignature('a(ssssbbbbbbbbuasa{ss}sa{sv})a(ssssssbbssa{ss}sa{sv})a(ssssssbsassa{sv})'); //console.log(tree); -//console.log(module.exports.fromTree(tree)) +//console.log(parseSignature.fromTree(tree)) diff --git a/test/unmarshall-basic.js b/test/unmarshall-basic.js index 75b835e9..9c1e5b1d 100644 --- a/test/unmarshall-basic.js +++ b/test/unmarshall-basic.js @@ -154,6 +154,12 @@ describe('marshall/unmarshall', function() { ['aai', [[[300, 400, 500], [1, 2, 3, 4, 5, 6, 7]]] ], ['aiai', [[1, 2, 3], [300, 400, 500]] ], ], + 'variants': [ + ['vv', [['i', 42], ['ai', [1, 2, 3]]]], + ['vv', [['(i)', [42]], ['a(i)', [[1], [2], [3]]]]], + ['v', [['v', ['v', ['y', 42]]]]], + ['vv', [['v', ['av', []]], ['av', [['i', 42]]]]], + ], 'compound types': [ ['iyai', [10, 100, [1, 2, 3, 4, 5, 6]]], // TODO: fix 'array of structs offset problem