Skip to content

Commit

Permalink
~ now can parse full hex files (bin logs)
Browse files Browse the repository at this point in the history
  • Loading branch information
rascafr committed Aug 30, 2019
1 parent c8d3449 commit 025bc2b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 91 deletions.
95 changes: 4 additions & 91 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const clc = require('cli-color');
const sha1 = require('js-sha1');
const blue = clc.blueBright;
const D = require('./definitions');
const Parser = require('./parser');

const INPUT_FILE_LOG = `unlockThenRideV2Full.log`;
const INPUT_FILE_LOG = `unlockThenRideHexNoParsingV2.log`;
const INPUT_PATH_LOG = `${__dirname}/logfiles/${INPUT_FILE_LOG}`;
const LOG_HEADER = 'Ready...';

Expand All @@ -13,93 +14,5 @@ const OUTPUT_PATH_LOG = `${__dirname}/${OUTPUT_FILE_LOG}`;

console.log(blue('Reading file'), INPUT_PATH_LOG);

const logData = fs.readFileSync(INPUT_PATH_LOG).toString();
const logLines = logData
.split('\n')
.map(l => l.replace('\n', '')
.replace('\r', '')
.trim())
.filter(l => l.length && l !== LOG_HEADER)
.map(l => l.split(' '));

console.log('Got', logLines.length, 'lines of log');

console.log(logLines[1]);

const LINES_TYPES = {};
let strLog = '';

logLines.forEach(line => {
let sign = sha1(line.join(':'));
if (!LINES_TYPES[sign]) {
LINES_TYPES[sign] = line;
}
let msg = messageToString(parseMessage(line));
console.log(msg);
strLog += msg + '\n';
});

fs.writeFileSync(OUTPUT_PATH_LOG, strLog);

console.log('Different lines log values types:', Object.keys(LINES_TYPES).length);

function parseMessage(line) {

const message = {
length: null,
src: null,
dst: null,
cmd: null,
arg: null,
payload: null
}

// check length
if (line.length < 7) {
console.log(line);
throw 'Line error, invalid length';
}

// check header
if (line[0] !== D.headerBytes[0] || line[1] !== D.headerBytes[1]) {
console.log(line);
throw 'Line error, invalid header';
}

// fill message
message.length = line[2];
message.src = line[3];
message.dst = line[4];
message.cmd = line[5];
message.arg = line[6];
// todo payload

return message;
}

function messageToString(message) {
let str = '';

// who -> whom?
str += `[${D.addresses[message.src]} --> `;
str += `${D.addresses[message.dst]}] `;

// command
str += `using cmd ${D.commands[message.cmd] || '-----'} (${message.cmd}) `;

// argument
str += `and arg ${(message.arg < 10 ? ' ' : '') + message.arg} `;

return str;
}

function hashCode(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
const logData = fs.readFileSync(INPUT_PATH_LOG)
Parser.parseHex(logData);
121 changes: 121 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const Parser = module.exports;
const D = require('./definitions');
const fs = require('fs');

Parser.parseHex = function(fileBytes) {
let frame = null;
let index = 0;
let str = '';
do {
frame = extractFrame(fileBytes, index);
if (frame) {
index = frame.cursor;
str += frameToString(frame) + '\n';
//frame = null;
}
} while (frame !== null);

fs.writeFileSync('./resume.log', str);

console.log('Done');
}

function extractFrame(bytes, offset) {
let state = 0;
let c = null;
let curs = offset;
let done = false;
let raw = [];

while(!done && curs < bytes.length - 7) {
c = byteRaw(bytes, curs);

switch(state) {
// detect header
// 5A A5 bLen bSrcAddr bDstAddr bCmd bArg bPayload[bLen] wChecksumLE
// header(2) + args(5) + payload + chksum(2) = 9 + payload length
case 0:
if (c == D.headerBytes[0]) {
state = 1;
raw.push(c); // keep
}
break;

// check second header
// reset if bad
case 1:
if (c != D.headerBytes[1]) {
state = 0;
done = true;
} else {
state = 2;
raw.push(c); // keep
}
break;

// continue
// till we got length
case 2:
if (raw.length < 3 || (raw.length >= 3 && raw.length < 9 + raw[2])) {
raw.push(c);
} else {
//if (data[5] != 0x5)
state = 3;
//else state = 0;
}
break;

// done - print
case 3:
state = 4; // end
done = true;
break;
}

curs++;
}

if (state === 0) return null;

return {
raw: raw,
cursor: curs,
len: raw.length,
blen: raw[2],
src: raw[3],
dst: raw[4],
cmd: raw[5],
arg: raw[6],
payload: raw.filter((v, i) => i > 6 && i < raw.length - 2),
}
}

function byteRaw(bytes, pos) {
return bytes[pos];
}

function byteHex(bytes, pos) {
return bytes[pos].toString(16);
}

function frameToString(frame) {
let str = '------ frame ------';
str += '\n - raw: ' + toHexString(frame.raw);
str += '\n - flow: ' + D.addresses[frame.src] + ' -> ' + D.addresses[frame.dst];
str += '\n - cmd: ' + D.commands[frame.cmd];
str += '\n - arg: ' + frame.arg;
str += '\n - payload: ' + toHexString(frame.payload);
str += '\n - UTF payload: ' + new Buffer(frame.payload).toString();
str += '\n';
return str;
}

function displayFrame(frame) {
console.log(frameToString(frame));
}

function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join(' ')
}

0 comments on commit 025bc2b

Please sign in to comment.