You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenvictDataUtil.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. const DATA_LENGTH = (95 * 2);
  2. const NORMAL_DATA_LENGTH = (13 * 2);
  3. const FRAME_LENGTH = (20 * 2);
  4. const Protocol_Type = "genvict";
  5. var x = 3;
  6. var y = 1;
  7. function protocolType() {
  8. return Protocol_Type
  9. };
  10. function numberToHexString(z, A, B) {
  11. let hex = z.toString(16);
  12. for (let i = hex.length; i < A * 2; i++) {
  13. hex = '0' + hex
  14. };
  15. if (hex.length > A * 2) {
  16. hex = hex.substring(hex.length - A * 2)
  17. };
  18. if (!B) {
  19. let temp = '';
  20. for (let i = hex.length - 2; i >= 0; i -= 2) {
  21. temp = temp + hex.substring(i, i + 2)
  22. };
  23. hex = temp
  24. };
  25. return hex
  26. };
  27. function makeAuthResponse() {
  28. let send = "FE0100124E2100010A06080012024F4B1200";
  29. let bufferArray = new Array();
  30. let bufferStr = send.substring(0, 36);
  31. let typedArray = new Uint8Array(bufferStr.match(/[\da-f]{2}/gi).map(function(z) {
  32. return parseInt(z, 16)
  33. }));
  34. bufferArray.push(typedArray.buffer);
  35. return bufferArray
  36. };
  37. function makeInitResponse() {
  38. let send = "FE0100164E2300020A06080012024F4B100018002000";
  39. let bufferArray = new Array();
  40. let bufferStr = send.substring(0, FRAME_LENGTH);
  41. let typedArray = new Uint8Array(bufferStr.match(/[\da-f]{2}/gi).map(function(z) {
  42. return parseInt(z, 16)
  43. }));
  44. bufferArray.push(typedArray.buffer);
  45. bufferStr = send.substring(40);
  46. typedArray = new Uint8Array(bufferStr.match(/[\da-f]{2}/gi).map(function(z) {
  47. return parseInt(z, 16)
  48. }));
  49. bufferArray.push(typedArray.buffer);
  50. return bufferArray
  51. };
  52. function makeFrame(z) {
  53. let send = "" + z;
  54. let u = parseInt(send.length / DATA_LENGTH);
  55. let frameBalance = send.length % DATA_LENGTH;
  56. let t = new Array();
  57. for (let i = 0; i < u; i++) {
  58. t.push(send.substring(i * DATA_LENGTH, i * DATA_LENGTH + DATA_LENGTH))
  59. };
  60. if (frameBalance > 0) {
  61. t.push(send.substring(send.length - frameBalance))
  62. };
  63. let bufferArray = new Array();
  64. let frame;
  65. y++;
  66. if (y > 0xf) {
  67. y = 1
  68. };
  69. for (let i = 0; i < t.length; i++) {
  70. let ctl = "";
  71. if (protocolType() == "xinguobiao") {
  72. if (i == 0) {
  73. ctl = numberToHexString(0x8000 + t.length, 2, true)
  74. } else {
  75. ctl = numberToHexString(i + 1, 2, true)
  76. };
  77. frame = xinGuoBiaoPackData(t[i], ctl)
  78. } else if (protocolType() == "genvict") {
  79. if (i == 0) {
  80. ctl = numberToHexString(0x80 + t.length - 1, 1, true)
  81. } else {
  82. ctl = numberToHexString(t.length - i - 1, 1, true)
  83. };
  84. frame = GVPackData(t[i], ctl)
  85. };
  86. frame = wechatPackData(frame);
  87. let bufferCount = parseInt(frame.length / FRAME_LENGTH);
  88. let bufferBalance = frame.length % FRAME_LENGTH;
  89. for (let j = 0; j < bufferCount; j++) {
  90. let bufferStr = frame.substring(j * FRAME_LENGTH, j * FRAME_LENGTH + FRAME_LENGTH);
  91. let typedArray = new Uint8Array(bufferStr.match(/[\da-f]{2}/gi).map(function(A) {
  92. return parseInt(A, 16)
  93. }));
  94. bufferArray.push(typedArray.buffer)
  95. };
  96. if (bufferBalance > 0) {
  97. let bufferStr = frame.substring(frame.length - bufferBalance);
  98. let typedArray = new Uint8Array(bufferStr.match(/[\da-f]{2}/gi).map(function(A) {
  99. return parseInt(A, 16)
  100. }));
  101. bufferArray.push(typedArray.buffer)
  102. }
  103. };
  104. return bufferArray
  105. };
  106. function makeNormalCMD(z) {
  107. y++;
  108. if (y > 0xf) {
  109. y = 1
  110. };
  111. let bufferArray = new Array();
  112. let frameNum = numberToHexString(0x80 + y, 1, true);
  113. let frame = normalPackData("", frameNum, "00", z);
  114. let typedArray = new Uint8Array(frame.match(/[\da-f]{2}/gi).map(function(A) {
  115. return parseInt(A, 16)
  116. }));
  117. bufferArray.push(typedArray.buffer);
  118. return bufferArray
  119. };
  120. function makeNormalFrame(z, A) {
  121. let send = "" + A;
  122. let u = parseInt(send.length / NORMAL_DATA_LENGTH);
  123. let frameBalance = send.length % NORMAL_DATA_LENGTH;
  124. let t = new Array();
  125. for (let i = 0; i < u; i++) {
  126. t.push(send.substring(i * NORMAL_DATA_LENGTH, i * NORMAL_DATA_LENGTH + NORMAL_DATA_LENGTH))
  127. };
  128. if (frameBalance > 0) {
  129. t.push(send.substring(send.length - frameBalance))
  130. };
  131. let bufferArray = new Array();
  132. let frame;
  133. y++;
  134. if (y > 0xf) {
  135. y = 1
  136. };
  137. for (let i = 0; i < t.length; i++) {
  138. let frameNum = "";
  139. let sequNum = numberToHexString(t.length - i - 1, 1, true);
  140. if (i == 0) {
  141. frameNum = numberToHexString(0x80 + y, 1, true)
  142. } else {
  143. frameNum = numberToHexString(y, 1, true)
  144. };
  145. frame = normalPackData(t[i], frameNum, sequNum, z);
  146. let typedArray = new Uint8Array(frame.match(/[\da-f]{2}/gi).map(function(B) {
  147. return parseInt(B, 16)
  148. }));
  149. bufferArray.push(typedArray.buffer)
  150. };
  151. return bufferArray
  152. };
  153. function initDeviceSendData() {
  154. let send;
  155. if (protocolType() == "xinguobiao") {
  156. send = "80"
  157. } else if (protocolType() == "genvict") {
  158. send = "A2"
  159. } else if (protocolType() == "normal") {
  160. send = "000D0074043000";
  161. return makeNormalFrame("01", send)
  162. };
  163. return makeFrame(send)
  164. };
  165. function makeDeviceChannelSendData(z) {
  166. let order = "" + z;
  167. let send;
  168. if (protocolType() == "xinguobiao") {
  169. send = "81" + numberToHexString(order.length / 2, 2, false) + order
  170. } else if (protocolType() == "genvict") {
  171. send = "A5" + numberToHexString(order.length / 2, 1, false) + order
  172. } else if (protocolType() == "normal") {
  173. return makeNormalCMD(z)
  174. };
  175. return makeFrame(send)
  176. };
  177. function makeObuChannel(z) {
  178. let order = "" + z;
  179. let send;
  180. if (protocolType() == "genvict") {
  181. send = "AC" + numberToHexString(order.length / 2, 2, false) + order
  182. };
  183. return makeFrame(send)
  184. };
  185. function makeAuthenChannelSendData(z, A) {
  186. let order = z + A;
  187. let send;
  188. if (protocolType() == "genvict") {
  189. send = "A6" + numberToHexString(order.length / 2, 2, false) + order
  190. };
  191. return makeFrame(send)
  192. };
  193. function makecosSendData(z, A) {
  194. if (protocolType() == "normal") {
  195. let send = customPackData(A);
  196. return makeNormalFrame("03", send)
  197. };
  198. let order;
  199. if (parseInt(z.substring(1, 2), 10) == 0) {
  200. order = "80" + tlvPackData(A)
  201. } else {
  202. order = A[0]
  203. };
  204. let send;
  205. if (protocolType() == "xinguobiao") {
  206. send = "82" + z + numberToHexString(order.length / 2, 2, false) + order;
  207. return makeFrame(send)
  208. } else if (protocolType() == "genvict") {
  209. send = "A3" + z + numberToHexString(order.length / 2, 2, false) + order;
  210. return makeFrame(send)
  211. }
  212. };
  213. function makeResetCard() {
  214. let order = "0200";
  215. if (protocolType() == "normal") {
  216. return makeNormalFrame("07", order)
  217. }
  218. };
  219. function maketransSendData(z, A) {
  220. let order;
  221. order = A;
  222. let send;
  223. if (protocolType() == "xinguobiao") {
  224. send = "82" + z + numberToHexString(order.length / 2, 2, false) + order
  225. } else if (protocolType() == "genvict") {
  226. send = "A3" + z + numberToHexString(order.length / 2, 2, false) + order
  227. };
  228. return makeFrame(send)
  229. };
  230. function makeGVEsamSendData(z, A) {
  231. if (protocolType() == "normal") {
  232. let send = customPackData(A);
  233. return makeNormalFrame("02", send)
  234. };
  235. let order;
  236. if (parseInt(z.substring(1, 2), 10) == 0) {
  237. order = "80" + tlvPackData(A)
  238. } else {
  239. order = A[0]
  240. };
  241. let send;
  242. if (protocolType() == "genvict") {
  243. send = "A1" + z + numberToHexString(order.length / 2, 2, false) + order
  244. };
  245. return makeFrame(send)
  246. };
  247. function makeGVtransEsamSendData(z, A) {
  248. let order;
  249. order = A;
  250. let send;
  251. if (protocolType() == "genvict") {
  252. send = "A1" + z + numberToHexString(order.length / 2, 2, false) + order
  253. };
  254. return makeFrame(send)
  255. };
  256. function makeSelfDeviceChannelSendData(z) {
  257. let order = "" + z;
  258. let send;
  259. if (protocolType() == "genvict") {
  260. send = "AE" + numberToHexString(order.length / 2, 1, false) + order
  261. };
  262. return makeFrame(send)
  263. };
  264. function makeAuthenSendData(z) {
  265. let order = "" + z;
  266. let send = "84" + numberToHexString(order.length / 2, 2, false) + order;
  267. return makeFrame(send)
  268. };
  269. function makeDeviceAuthenSendData(z, A) {
  270. let order = "C1" + z + A;
  271. let send = "AD" + numberToHexString(order.length / 2, 2, false) + order;
  272. return makeFrame(send)
  273. };
  274. function makeTransA1reqData(z) {
  275. let send = "A6" + numberToHexString(z.length / 2 + 1, 2, false) + "C9" + z;
  276. return makeFrame(send)
  277. };
  278. function makeLoadCreditT0reqData(z) {
  279. let str = "01" + numberToHexString(z.length / 2, 1, false) + z;
  280. let send = "A6" + numberToHexString(str.length / 2 + 1, 2, false) + "C8" + str;
  281. return makeFrame(send)
  282. };
  283. function makeEsamMiWen(z) {
  284. let send = "A6" + numberToHexString(z.length / 2 + 1, 2, false) + "CA" + z;
  285. return makeFrame(send)
  286. };
  287. function tlvPackData(z) {
  288. let content = "";
  289. for (let i = 0; i < z.length; i++) {
  290. content = content + numberToHexString(i + 1, 1, false) + numberToHexString(z[i].length / 2, 1, false) + z[i]
  291. };
  292. let contentLen = content.length / 2;
  293. let tlvData = '';
  294. if (contentLen >= 0x100) {
  295. tlvData = '82' + numberToHexString(contentLen, 2, false) + content
  296. } else if (contentLen > 0x80) {
  297. tlvData = '81' + numberToHexString(contentLen, 1, false) + content
  298. } else {
  299. tlvData = numberToHexString(contentLen, 1, false) + content
  300. };
  301. return tlvData
  302. };
  303. function customPackData(z) {
  304. let content = "";
  305. for (let i = 0; i < z.length; i++) {
  306. content = content + numberToHexString(z[i].length / 2, 1, true) + z[i]
  307. };
  308. let tlvData = numberToHexString(z.length, 1, true) + content;
  309. return tlvData
  310. };
  311. function wechatPackData(z) {
  312. z = "0A0012" + numberToHexString(z.length / 2, 1, true) + z + "1800";
  313. let bMagic = "FE";
  314. let bVer = "01";
  315. let nLength = numberToHexString(z.length / 2 + 8, 2, true);
  316. let nCmdId = "7531";
  317. let nSeq = numberToHexString(x, 2, true);
  318. z = bMagic + bVer + nLength + nCmdId + nSeq + z;
  319. x++;
  320. if (x > 0xf) {
  321. x = 1
  322. };
  323. return z
  324. };
  325. function xinGuoBiaoPackData(z, A) {
  326. let st = "50";
  327. let temp = "" + z;
  328. let len = numberToHexString(temp.length / 2, 1, true);
  329. let frame = st + A + len + temp;
  330. let bcc = 0;
  331. for (let j = 0; j < frame.length / 2; j++) {
  332. let bit = parseInt(frame.substring(j * 2, j * 2 + 2), 16);
  333. bcc = bcc ^ bit
  334. };
  335. frame = frame + numberToHexString(bcc, 1, true);
  336. return frame
  337. };
  338. function GVPackData(z, A) {
  339. let st = "33";
  340. let sn = numberToHexString(y, 1, true);
  341. let temp = "" + z;
  342. let len = numberToHexString(temp.length / 2, 1, true);
  343. let frame = st + sn + A + len + temp;
  344. let bcc = 0;
  345. for (let j = 1; j < frame.length / 2; j++) {
  346. let bit = parseInt(frame.substring(j * 2, j * 2 + 2), 16);
  347. bcc = bcc ^ bit
  348. };
  349. frame = frame + numberToHexString(bcc, 1, true);
  350. return frame
  351. };
  352. function normalPackData(z, A, B, C) {
  353. let st = "33";
  354. let temp = "" + z;
  355. let len = numberToHexString(temp.length / 2, 1, true);
  356. let frame = st + A + C + B + len + temp;
  357. let bcc = 0;
  358. for (let j = 0; j < frame.length / 2; j++) {
  359. let bit = parseInt(frame.substring(j * 2, j * 2 + 2), 16);
  360. bcc = bcc ^ bit
  361. };
  362. frame = frame + numberToHexString(bcc, 1, true);
  363. return frame
  364. };
  365. module.exports = {
  366. makeAuthResponse: makeAuthResponse,
  367. makeInitResponse: makeInitResponse,
  368. initDeviceSendData: initDeviceSendData,
  369. makeSelfDeviceChannelSendData: makeSelfDeviceChannelSendData,
  370. makeDeviceChannelSendData: makeDeviceChannelSendData,
  371. makeFrame: makeFrame,
  372. makecosSendData: makecosSendData,
  373. makeAuthenSendData: makeAuthenSendData,
  374. numberToHexString: numberToHexString,
  375. protocolType: protocolType,
  376. makeDeviceAuthenSendData: makeDeviceAuthenSendData,
  377. makeGVEsamSendData: makeGVEsamSendData,
  378. makeGVtransEsamSendData: makeGVtransEsamSendData,
  379. maketransSendData: maketransSendData,
  380. makeResetCard: makeResetCard,
  381. makeAuthenChannelSendData: makeAuthenChannelSendData,
  382. makeTransA1reqData: makeTransA1reqData,
  383. makeLoadCreditT0reqData: makeLoadCreditT0reqData,
  384. makeEsamMiWen: makeEsamMiWen,
  385. makeObuChannel: makeObuChannel
  386. };