-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
126 lines (109 loc) · 2.78 KB
/
lib.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
122
123
124
/**
* IMPORTS
*/
const ffi = require('ffi'),
ref = require('ref');
/**
* VARS
*/
let libA1 = null;
let funcs = [];
/**
* Response Types for consuming clients
* OK: ok,
* ERR: Error
*/
exports.ResponseTypes = {
SUCCESS: "OK",
ERROR: "FAILURE"
};
/**
* initialize()
*
* uses FFI to initialize the liba1.so shared object
* file and binds to the supplised functions
* [{
* FuncName: "GetHDInfo",
* ReturnType: "int",
* Parameters: ["string", "string"]
* },...]
*/
exports.initialize = (funcsToBind) => {
try {
libA1 = ffi.DynamicLibrary('/usr/lib/libA1.so');
if (funcsToBind) {
for (let i = 0; i < funcsToBind.length; i++) {
const funcObject = funcsToBind[i];
const funcName = funcObject.FunctionName;
const funcReturnType = funcObject.ReturnType;
const funcParameters = funcObject.Parameters || [];
let funcPointer = libA1.get(funcName);
let func = ffi.ForeignFunction(funcPointer, funcReturnType, funcParameters);
let obj = {
functionName: funcName,
func
}
funcs.push(obj)
}
}
}
catch (err) {
console.log(err)
return err;
}
};
/**
* runs the function specified
*/
exports.run = (functionName) => {
switch (functionName.toLowerCase()) {
case "gethdinfo":
let f = getFunction(functionName)
if (f) {
return _getHdInfo(f);
}
break;
}
};
/*********************
* HELPER FUNCTIONS *
*********************/
//generic return
const Ok = (rObject) => {
return {
status: "OK",
responseObject: rObject
};
};
//helper function to see if functions array contains the function being ran
const getFunction = (func) => {
const foundFunction = funcs.filter((f) => {
return f.functionName.toLowerCase() === func.toLowerCase();
});
if (foundFunction.length == 1) {
return foundFunction[0];
}
throw new Error("Function either does not exist or not binded correctly");
};
/*********************
* PRIVATE FUNCTIONS *
*********************/
//get hd info binding
const _getHdInfo = (f) => {
try {
var modelPtr = Buffer.alloc(41);
var serialNumberPtr = Buffer.alloc(21);
var didSucceed = f.func(modelPtr, serialNumberPtr);
if (didSucceed) {
var hdInfo = {
model: ref.readCString(modelPtr, 0).trim(),
serialNo: ref.readCString(serialNumberPtr, 0).trim()
}
return Ok(hdInfo);
}
throw new Error("Function did not succeed");
}
catch (err) {
throw err;
}
};