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.

TDRObuSDK.js 27KB

2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
1 vuosi sitten
2 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. const mServiceId = '0000FEE7-0000-1000-8000-00805F9B34FB';
  2. const mWriteUUID = '0000FEC7-0000-1000-8000-00805F9B34FB';
  3. const mReadUUID = '0000FEC7-0000-1000-8000-00805F9B34FB';
  4. const mNotifyUUID = '0000FEC8-0000-1000-8000-00805F9B34FB';
  5. const mActiviceReadUUID = '0000FEC9-0000-1000-8000-00805F9B34FB';
  6. const WECHAT_PRO_HEAD_MAGIC_NUMBER = 0xFE;
  7. const WECHAT_PRO_HEAD_PACK_VERSION = 0x01;
  8. const WECHAT_CMD_ECI_REQ_AUTH = 0x2711; //握手数据(设备--->手机)
  9. const WECHAT_CMD_ECI_REQ_SEND_DATA = 0x2712;
  10. const WECHAT_CMD_ECI_REQ_INIT = 0x2713; //初始化 (设备--->手机)
  11. const WECHAT_CMD_ECI_RESP_AUTH = 0x4E21; //握手数据 (手机--->设备)
  12. const WECHAT_CMD_ECI_RESP_INIT = 0x4E23; //初始化 (手机--->设备)
  13. const WECHAT_CMD_ECI_PUSH_RECV_DATA = 0x7531; //发送指令数据(手机--->设备)
  14. const ETC_PRO_SINGLE_PACK_SIZE = 105; /* ETC–≠“È∑÷∞¸≥§∂» */
  15. const BLE_CTL_START_BIT = 0x80; //分包后,此包是否为起始包
  16. const BLE_ETC_PRO_HEAD_ST = 0x33; /* ETC(33协议头标识) */
  17. const MAX_WECHAT_SEND_LEN = 255;
  18. const MAX_BLE_SIZE = 20;
  19. const KYunNanObuMultiplePackages = 1;
  20. let mDeviceId = 0;
  21. let service = {}; //服务
  22. let writeCharVal = {}; //写入特征值
  23. let readCharVal = {}; //读取特征值
  24. let notifyCharVal = {}; //通知特征值
  25. let activiceReadCharVal = {}; //读取特征值
  26. let mPacketLen = 0;
  27. let mRecvDataCache = [];
  28. let mUnpacketDataCache = [];
  29. let mIsInitCmdSend = false;
  30. let mEtcRawData = [];
  31. let mEtcRawDataLen = 0;
  32. let maxLenPerPacket = 0;
  33. let mReturnedRawCmdData = 0;
  34. let mAbCmdBuf = [];
  35. let mnCmdBufLen = 0;
  36. let mTransData = [];
  37. let mSingleEtcProtoData = [];
  38. let mLenBuf = [];
  39. let mBytesNumOfDataLen = 0;
  40. let receiveDataCallBack = null;
  41. let m_isConn = false;
  42. let mCurrentPacketNum = 0;
  43. let mConnCount = 2;
  44. let beginTime = 0;
  45. /**
  46. 通过蓝牙,连接OBU设备
  47. @param deviceId 需要连接的蓝牙外围设备ID
  48. @param callback_1 回调函数 连接结果
  49. code=0表示连接成功,code = 非0 连接失败
  50. @param callback_2 回调函数 时时监听连接状态
  51. code=0表示监听连接成功,code = 1表示监听到断开连接
  52. */
  53. function connectDevice(device, callback_1, callback_2) {
  54. beginTime = +new Date();
  55. let deviceId = device.deviceId
  56. // if (m_isConn && mDeviceId == deviceId) {
  57. // callback_1(makeRes(0, [] , '已连接'))
  58. // return;
  59. // }
  60. // if (mDeviceId == 0) {
  61. conn(device, callback_1, callback_2)
  62. // } else {
  63. // wx.closeBLEConnection({
  64. // deviceId: mDeviceId,
  65. // complete: function (e) {
  66. // conn(device, callback_1, callback_2)
  67. // }
  68. // })
  69. // }
  70. }
  71. function conn(device, callback_1, callback_2) {
  72. let deviceId = device.deviceId
  73. wx.createBLEConnection({
  74. deviceId: deviceId,
  75. timeout: 5000,
  76. success: function(res) {
  77. mDeviceId = deviceId;
  78. wx.getBLEDeviceServices({
  79. deviceId: deviceId,
  80. success: function(res) {
  81. console.log('getBLEDeviceServices:' + JSON.stringify(res))
  82. var isExistsService = false
  83. for (var i = 0; i < res.services.length; i++) {
  84. if (res.services[i].uuid == mServiceId) {
  85. service: res.services[i]
  86. isExistsService = true;
  87. break;
  88. }
  89. }
  90. if (isExistsService) {
  91. wx.getBLEDeviceCharacteristics({
  92. deviceId: deviceId,
  93. serviceId: mServiceId,
  94. success: function(res) {
  95. for (var i = 0; i < res.characteristics
  96. .length; i++) {
  97. if (res.characteristics[i].uuid == mWriteUUID) {
  98. writeCharVal: res.characteristics[i]
  99. }
  100. if (res.characteristics[i].uuid == mReadUUID) {
  101. readCharVal: res.characteristics[i]
  102. }
  103. if (res.characteristics[i].uuid ==
  104. mNotifyUUID) {
  105. notifyCharVal: res.characteristics[i]
  106. }
  107. if (res.characteristics[i].uuid ==
  108. mActiviceReadUUID) {
  109. activiceReadCharVal: res.characteristics[i]
  110. }
  111. }
  112. wx.onBLEConnectionStateChange(function(res) {
  113. // 该方法回调中可以用于处理连接意外断开等异常情况
  114. console.log(
  115. '改变onBLEConnectionStateChange');
  116. if (res.connected) {
  117. console.log('connected true')
  118. m_isConn = true;
  119. console.log(callback_2)
  120. callback_2(makeRes(0, [], '蓝牙已连接'))
  121. } else {
  122. console.log('connected false')
  123. var endTime = +new Date();
  124. if ((endTime - beginTime) >= 3000) {
  125. mIsInitCmdSend = false;
  126. m_isConn = false;
  127. console.log(callback_2)
  128. callback_2(makeRes(-1, [],
  129. '蓝牙已断开'))
  130. }
  131. }
  132. })
  133. wx.notifyBLECharacteristicValueChange({
  134. deviceId: deviceId,
  135. serviceId: mServiceId,
  136. characteristicId: mNotifyUUID,
  137. state: true,
  138. success: function(res) {
  139. wx.onBLECharacteristicValueChange(
  140. function(res) {
  141. console.log(
  142. '收到数据:' +
  143. ab2hex(res
  144. .value))
  145. let pRead =
  146. ab2DecArr(res
  147. .value)
  148. if (pRead.length >
  149. 0 && pRead !=
  150. null) {
  151. if (pRead[0] ==
  152. WECHAT_PRO_HEAD_MAGIC_NUMBER &&
  153. pRead[1] ==
  154. WECHAT_PRO_HEAD_PACK_VERSION
  155. ) {
  156. mPacketLen =
  157. (pRead[
  158. 2
  159. ] <<
  160. 8) |
  161. pRead[
  162. 3];
  163. }
  164. mRecvDataCache
  165. .push.apply(
  166. mRecvDataCache,
  167. pRead)
  168. if (mRecvDataCache
  169. .length ==
  170. mPacketLen
  171. ) {
  172. let pData =
  173. mRecvDataCache
  174. let nCmdID =
  175. (pData[
  176. 4
  177. ] <<
  178. 8) |
  179. pData[5]
  180. switch (
  181. nCmdID
  182. ) {
  183. case WECHAT_CMD_ECI_REQ_AUTH: {
  184. console
  185. .log(
  186. 'WECHAT_CMD_ECI_REQ_AUTH'
  187. )
  188. let data =
  189. makeHandShakeStep1WithETC()
  190. mRecvDataCache
  191. = []
  192. write
  193. (
  194. data
  195. )
  196. }
  197. break;
  198. case WECHAT_CMD_ECI_REQ_INIT: {
  199. console
  200. .log(
  201. 'WECHAT_CMD_ECI_REQ_INIT'
  202. )
  203. let data =
  204. makeHandShakeStep2WithETC()
  205. write
  206. (
  207. data
  208. )
  209. setTimeout
  210. (function() {
  211. let dataInit =
  212. makeInitWithETC()
  213. mRecvDataCache
  214. = []
  215. write
  216. (
  217. dataInit
  218. )
  219. mIsInitCmdSend
  220. =
  221. true
  222. },
  223. 20
  224. )
  225. }
  226. break;
  227. case WECHAT_CMD_ECI_REQ_SEND_DATA: {
  228. console
  229. .log(
  230. 'WECHAT_CMD_ECI_REQ_SEND_DATA'
  231. )
  232. if (
  233. mIsInitCmdSend
  234. ) {
  235. m_isConn
  236. =
  237. true
  238. callback_1
  239. (makeRes(
  240. 0,
  241. [],
  242. '连接成功'
  243. ))
  244. mIsInitCmdSend
  245. =
  246. false
  247. }
  248. mEtcRawData
  249. = []
  250. mEtcRawDataLen
  251. =
  252. 0
  253. let op =
  254. unpaketProto(
  255. mRecvDataCache
  256. )
  257. if (!
  258. op
  259. ) {
  260. if (mEtcRawData[
  261. 0
  262. ] ==
  263. 0xB2
  264. ) {
  265. maxLenPerPacket
  266. =
  267. mEtcRawData[
  268. 3
  269. ];
  270. } else {
  271. mUnpacketDataCache
  272. .push
  273. .apply(
  274. mUnpacketDataCache,
  275. mEtcRawData
  276. )
  277. console
  278. .log(
  279. '******返回未解析的指令数据包为(' +
  280. mUnpacketDataCache
  281. .length +
  282. '):' +
  283. ab2hexArr(
  284. mUnpacketDataCache
  285. )
  286. )
  287. if (mCurrentPacketNum ==
  288. 0
  289. ) {
  290. mReturnedRawCmdData
  291. = []
  292. let errorcode =
  293. unpackXXCmdPacket(
  294. mUnpacketDataCache
  295. )
  296. console
  297. .log(
  298. '******解析后的指令数据为(' +
  299. mReturnedRawCmdData
  300. .length +
  301. '):' +
  302. mReturnedRawCmdData
  303. )
  304. if (
  305. receiveDataCallBack
  306. ) {
  307. receiveDataCallBack
  308. (makeRes(
  309. errorcode,
  310. mReturnedRawCmdData,
  311. errorcode ==
  312. 0 ?
  313. '收到数据成功' :
  314. '数据透传失败,请重试'
  315. ))
  316. }
  317. mUnpacketDataCache
  318. = []
  319. }
  320. }
  321. } else {
  322. mUnpacketDataCache
  323. .push
  324. .apply(
  325. mUnpacketDataCache,
  326. mEtcRawData
  327. )
  328. }
  329. mRecvDataCache
  330. = []
  331. }
  332. break;
  333. default:
  334. break;
  335. }
  336. }
  337. }
  338. })
  339. },
  340. fail: function(e) {
  341. console.log(
  342. 'notifyBLECharacteristicValueChange fail'
  343. )
  344. callback_1(makeRes(-1, [],
  345. '连接失败'))
  346. }
  347. })
  348. },
  349. fail: function(res) {
  350. console.log('getBLEDeviceCharacteristics fail')
  351. callback_1(makeRes(-1, [], '连接失败'))
  352. }
  353. })
  354. }
  355. },
  356. fail: function(res) {
  357. console.log('getBLEDeviceServices fail')
  358. callback_1(makeRes(-1, [], '连接失败'))
  359. }
  360. })
  361. },
  362. fail: function(res) {
  363. // console.log('createBLEConnection fail')
  364. // if (mConnCount > 0) {
  365. // console.log(mConnCount)
  366. // mConnCount--
  367. // connectDevice(device, callback_1, callback_2)
  368. // } else {
  369. // console.log(mConnCount)
  370. // mConnCount = 2
  371. // m_isConn = false;
  372. // callback_1(-1, [] , '连接失败')
  373. // }
  374. callback_1(makeRes(-1, [], '连接失败'))
  375. }
  376. })
  377. }
  378. //断开与OBU设备的蓝牙连接
  379. function disconnectDevice(callBack) {
  380. if (!m_isConn) {
  381. callBack(makeRes(-1, [], '未连接'))
  382. return;
  383. }
  384. let data = makeDisconnectWithETC();
  385. mIsInitCmdSend = false;
  386. mRecvDataCache = []
  387. write(data)
  388. wx.closeBLEConnection({
  389. deviceId: mDeviceId,
  390. success: function(res) {
  391. m_isConn = false;
  392. callBack(makeRes(0, [], '断开成功'))
  393. },
  394. fail: function(e) {
  395. console.log('error:' + e.errMsg)
  396. callBack(makeRes(-1, [], '断开失败'))
  397. }
  398. })
  399. }
  400. /**
  401. 数据透传
  402. @param cmd 指令[]
  403. @param cmdtype 明文:IC:10、ESAM:20
  404. @param callBack 回调函数
  405. status 成功为 true,失败为 false
  406. data NSData 回应数据(明文)+ 签名
  407. errorMsg 报错信息
  408. */
  409. function transCmd(cmd, cmdtype, callBack) {
  410. if (callBack) {
  411. receiveDataCallBack = callBack;
  412. }
  413. if (!m_isConn) {
  414. receiveDataCallBack(makeRes(-1, [], '连接失败'))
  415. return;
  416. }
  417. let cmdType = 0;
  418. if (cmdtype == '10') {
  419. cmdType = 0xA3;
  420. } else if (cmdtype == '20') {
  421. cmdType = 0xA8;
  422. }
  423. let totalMutableData = []
  424. for (var i = 0; i < cmd.length; i++) {
  425. let obj = str2Bytes(cmd[i]);
  426. console.log('##### cmd_:' + ab2hexArr(obj))
  427. let oneData = makeMutiPackageData(obj, i + 1)
  428. console.log('##### oneData:' + ab2hexArr(oneData))
  429. totalMutableData.push.apply(totalMutableData, oneData);
  430. }
  431. console.log('totalMutableData:' + ab2hexArr(totalMutableData))
  432. let totalChar = totalMutableData;
  433. let pbChar = [];
  434. pbChar[0] = 0xA1;
  435. pbChar[1] = totalMutableData.length;
  436. pbChar.push.apply(pbChar, totalChar);
  437. let reallyData = pbChar;
  438. console.log('reallyData:' + ab2hexArr(reallyData))
  439. mTransData = []
  440. if (!makeXXCmdPacket(cmdType, reallyData)) {
  441. subReqDataAndMakeEtcProtoPacket(mTransData)
  442. }
  443. }
  444. function makeMutiPackageData(xxCmdData, dataIndex) {
  445. let index = 0;
  446. let cmdDataLen = xxCmdData.length;
  447. let cmdPacketDataLen = cmdDataLen + 12;
  448. let pbData = []
  449. pbData[index++] = 0xA4; //1
  450. pbData[index++] = 0x00; //Len2 1字节长度,真实数值后续代码算 2
  451. //stable data
  452. pbData[index++] = 0x38; //3
  453. pbData[index++] = 0x01; //4
  454. pbData[index++] = dataIndex; //5
  455. pbData[index++] = 0x39; //6
  456. pbData[index++] = cmdDataLen; //Len3
  457. pbData.push.apply(pbData, xxCmdData)
  458. index += cmdDataLen;
  459. //stable data
  460. pbData[index++] = 0x3C;
  461. pbData[index++] = 0x01;
  462. pbData[index++] = 0x00;
  463. // pbData[index++] = 0x00;
  464. // pbData[index] = 0x00;
  465. pbData[1] = cmdDataLen + 3 + 2 + 3;
  466. return pbData;
  467. }
  468. function subReqDataAndMakeEtcProtoPacket(etcProtoData) {
  469. let hr = 0;
  470. let totalDataLen = etcProtoData.length;
  471. console.log('**** maxLenPerPacket:' + maxLenPerPacket)
  472. let blockSize = maxLenPerPacket;
  473. let offset = 0
  474. if (blockSize == undefined) {
  475. blockSize = 100
  476. }
  477. let YuShu = totalDataLen % blockSize;
  478. let PackNumOffset = 0;
  479. let PackNum = (YuShu == 0) ? (totalDataLen / blockSize) : (parseInt(totalDataLen / blockSize) + 1);
  480. let LastPackSize = (YuShu == 0) ? ETC_PRO_SINGLE_PACK_SIZE : YuShu;
  481. let pSendBuf = etcProtoData;
  482. let sendBuff = [];
  483. mSingleEtcProtoData = [];
  484. let _pack = {}
  485. _pack.perPackSize = blockSize
  486. _pack.finalPackSize = LastPackSize
  487. _pack.packNum = PackNum
  488. if (totalDataLen > blockSize) {
  489. for (; offset < (totalDataLen - blockSize); offset += blockSize) {
  490. _pack.packNumOffset = PackNumOffset;
  491. sendBuff.push.apply(sendBuff, pSendBuf.slice(offset, offset + blockSize))
  492. hr = makeSingleEtcProtocolPaket(sendBuff, _pack)
  493. if (hr != 0) {
  494. return hr;
  495. }
  496. PackNumOffset++;
  497. hr = tranmitBleBlock()
  498. if (hr != 0) {
  499. return hr;
  500. }
  501. sendBuff = []
  502. }
  503. }
  504. _pack.packNumOffset = PackNumOffset;
  505. sendBuff.push.apply(sendBuff, pSendBuf.slice(offset, totalDataLen))
  506. let data = sendBuff.slice(0, totalDataLen - offset)
  507. hr = makeSingleEtcProtocolPaket(data, _pack)
  508. if (hr != 0) {
  509. return hr;
  510. }
  511. console.log('tranmitBleBlock(' + mSingleEtcProtoData.length + '):' + ab2hexArr(mSingleEtcProtoData))
  512. return tranmitBleBlock()
  513. }
  514. function makeSingleEtcProtocolPaket(ectCmdRawData, sPack) {
  515. let SendCTL = sPack.packNum - sPack.packNumOffset - 1;
  516. let IsLastPack = ((sPack.packNumOffset + 1) == sPack.packNum) ? 1 : 0;
  517. let DataLen = 0;
  518. let SendLen = 0;
  519. let etcCmdRawDataLen = ectCmdRawData.length;
  520. let etcCmdRawBufLen = etcCmdRawDataLen + 5;
  521. let etcCmdRawBuf = [];
  522. SendCTL = (sPack.packNumOffset == 0) ? (BLE_CTL_START_BIT | SendCTL) : SendCTL;
  523. DataLen = IsLastPack ? sPack.finalPackSize : sPack.perPackSize; //ETC_PRO_SINGLE_PACK_SIZE;
  524. /* 33协议组包*/
  525. etcCmdRawBuf[SendLen++] = BLE_ETC_PRO_HEAD_ST; //ST
  526. etcCmdRawBuf[SendLen++] = 0x00; //sPack.packNumOffset+1;//0x00;
  527. etcCmdRawBuf[SendLen++] = SendCTL; //CTL
  528. etcCmdRawBuf[SendLen++] = etcCmdRawDataLen; //LEN
  529. etcCmdRawBuf.push.apply(etcCmdRawBuf, ectCmdRawData);
  530. SendLen += ectCmdRawData.length;
  531. etcCmdRawBuf[SendLen++] = XorVerify(etcCmdRawBuf.slice(1), (ectCmdRawData.length + 3)); //BCC校验,从SN到DATA
  532. console.log('*****makeSingleEtcPacket : ' + ab2hexArr(etcCmdRawBuf))
  533. return makeSingleProtoPacket(etcCmdRawBuf)
  534. }
  535. function XorVerify(buf, len) {
  536. let TempVerify = 0x00;
  537. let i = 0;
  538. for (i = 0; i < len; i++) {
  539. TempVerify ^= buf[i];
  540. }
  541. return TempVerify
  542. }
  543. function AppSetVarints(inValue) {
  544. if (inValue < 128) {
  545. mLenBuf[0] = inValue;
  546. mBytesNumOfDataLen = 1;
  547. } else {
  548. mLenBuf[0] = (inValue & 0x7F) | 0x80;
  549. mLenBuf[1] = (inValue >> 7) & 0x7F;
  550. mBytesNumOfDataLen = 2;
  551. }
  552. }
  553. function makeSingleProtoPacket(etcEnclosurBuf) {
  554. let index = 0;
  555. let ManufactoryDefineKeyValue = 0; //厂商自定义数据长度,根据不同值设置不同数据
  556. let sDataLen = etcEnclosurBuf.length;
  557. if (sDataLen == 0 || sDataLen > (MAX_WECHAT_SEND_LEN - 12)) {
  558. console.log('[ERROR] Wechat send len error, return!')
  559. return 1;
  560. }
  561. mLenBuf = []
  562. mBytesNumOfDataLen = 0;
  563. AppSetVarints(sDataLen)
  564. let protoBufLen = sDataLen + 11 + mBytesNumOfDataLen + ManufactoryDefineKeyValue;
  565. let protoBuf = [];
  566. protoBuf[index++] = WECHAT_PRO_HEAD_MAGIC_NUMBER;
  567. protoBuf[index++] = WECHAT_PRO_HEAD_PACK_VERSION;
  568. protoBuf[index++] = (protoBufLen >> 8) & 0xff;
  569. protoBuf[index++] = (protoBufLen) & 0xff;
  570. protoBuf[index++] = (WECHAT_CMD_ECI_PUSH_RECV_DATA >> 8) & 0xff;
  571. protoBuf[index++] = (WECHAT_CMD_ECI_PUSH_RECV_DATA) & 0xff;
  572. protoBuf[index++] = 0x00;
  573. protoBuf[index++] = 0x00;
  574. /* 变长包体,key1-value1 - key2-value2 - key3-value3... ,value=len+data 具体可参考protobuf协议*/
  575. protoBuf[index++] = 0x0A; //可认为固定值
  576. protoBuf[index++] = 0x00;
  577. protoBuf[index++] = 0x12; //data域key值;
  578. protoBuf[index++] = mLenBuf[0]; //data域lenth
  579. if (mBytesNumOfDataLen == 2) {
  580. protoBuf[index++] = mLenBuf[1];
  581. }
  582. protoBuf.push.apply(protoBuf, etcEnclosurBuf)
  583. index += sDataLen;
  584. if (ManufactoryDefineKeyValue == 2) //暂时未用到
  585. {
  586. protoBuf[index++] = 0x18;
  587. protoBuf[index++] = 0x00;
  588. }
  589. if (ManufactoryDefineKeyValue == 3) //暂时未用到
  590. {
  591. protoBuf[index++] = 0x18;
  592. protoBuf[index++] = 0x91;
  593. protoBuf[index++] = 0x4e;
  594. }
  595. mSingleEtcProtoData = []
  596. mSingleEtcProtoData.push.apply(mSingleEtcProtoData, protoBuf.slice(0, index))
  597. console.log('*****makeSingleProtoPacket : ' + ab2hexArr(mSingleEtcProtoData))
  598. return 0;
  599. }
  600. function tranmitBleBlock() {
  601. let totalDataLen = mSingleEtcProtoData.length;
  602. let blockSize = MAX_BLE_SIZE;
  603. let offset = 0;
  604. if (blockSize <= 0) {
  605. return 1;
  606. }
  607. let pSendBuf = mSingleEtcProtoData;
  608. let sendBuff = [];
  609. if (totalDataLen > blockSize) {
  610. for (; offset < totalDataLen - blockSize; offset += blockSize) {
  611. sendBuff.push.apply(sendBuff, pSendBuf.slice(offset, blockSize + offset))
  612. let data = sendBuff.slice(0, blockSize)
  613. console.log('tranmitBleBlock part:(' + blockSize + '),data:' + ab2hexArr(data))
  614. writeData(data)
  615. sendBuff = []
  616. }
  617. }
  618. sendBuff.push.apply(sendBuff, pSendBuf.slice(offset, totalDataLen))
  619. let data = sendBuff.slice(0, totalDataLen - offset)
  620. console.log('tranmitBleBlock part:(' + (totalDataLen - offset) + '),data:' + ab2hexArr(data))
  621. writeData(data)
  622. return 0;
  623. }
  624. function write(arrayBuffer) {
  625. wx.writeBLECharacteristicValue({
  626. deviceId: mDeviceId,
  627. serviceId: mServiceId,
  628. characteristicId: mWriteUUID,
  629. value: arrayBuffer,
  630. success: function(res) {
  631. console.log('发送成功')
  632. },
  633. fail: function(e) {}
  634. })
  635. }
  636. function writeData(data) {
  637. let arrayBuffer = new Uint8Array(data).buffer;
  638. wx.writeBLECharacteristicValue({
  639. deviceId: mDeviceId,
  640. serviceId: mServiceId,
  641. characteristicId: mWriteUUID,
  642. value: arrayBuffer,
  643. success: function(res) {
  644. console.log('发送成功')
  645. },
  646. })
  647. }
  648. function ab2hex(buffer) {
  649. let hexArr = Array.prototype.map.call(
  650. new Uint8Array(buffer),
  651. function(bit) {
  652. return ('00' + bit.toString(16)).slice(-2)
  653. }
  654. )
  655. return hexArr.join('');
  656. }
  657. function ab2DecArr(buffer) {
  658. return new Uint8Array(buffer);
  659. }
  660. function ab2hexArr(arr) {
  661. let hexArr = Array.prototype.map.call(
  662. arr,
  663. function(bit) {
  664. return ('00' + bit.toString(16)).slice(-2)
  665. }
  666. )
  667. return hexArr;
  668. }
  669. function makeHandShakeStep1WithETC() {
  670. let cBuff = []
  671. let index = 0
  672. cBuff[index++] = 0xFE;
  673. cBuff[index++] = 0x01;
  674. cBuff[index++] = 0x00;
  675. cBuff[index++] = 0x0E;
  676. cBuff[index++] = 0x4E;
  677. cBuff[index++] = 0x21;
  678. cBuff[index++] = 0x00;
  679. cBuff[index++] = 0x01;
  680. cBuff[index++] = 0x0A;
  681. cBuff[index++] = 0x02;
  682. cBuff[index++] = 0x08;
  683. cBuff[index++] = 0x00;
  684. cBuff[index++] = 0x12;
  685. cBuff[index] = 0x00;
  686. return new Uint8Array(cBuff).buffer;
  687. }
  688. function makeHandShakeStep2WithETC() {
  689. let cBuff = []
  690. let index = 0
  691. cBuff[index++] = 0xFE;
  692. cBuff[index++] = 0x01;
  693. cBuff[index++] = 0x00;
  694. cBuff[index++] = 0x13;
  695. cBuff[index++] = 0x4E;
  696. cBuff[index++] = 0x23;
  697. cBuff[index++] = 0x00;
  698. cBuff[index++] = 0x02;
  699. cBuff[index++] = 0x0A;
  700. cBuff[index++] = 0x02;
  701. cBuff[index++] = 0x08;
  702. cBuff[index++] = 0x00;
  703. cBuff[index++] = 0x10;
  704. cBuff[index++] = 0xB4;
  705. cBuff[index++] = 0x24;
  706. cBuff[index++] = 0x18;
  707. cBuff[index++] = 0xF8;
  708. cBuff[index++] = 0xAC;
  709. cBuff[index] = 0x01;
  710. return new Uint8Array(cBuff).buffer;
  711. }
  712. function makeInitWithETC() {
  713. let cBuff = []
  714. let index = 0
  715. cBuff[index++] = 0xFE;
  716. cBuff[index++] = 0x01;
  717. cBuff[index++] = 0x00;
  718. cBuff[index++] = 0x14;
  719. cBuff[index++] = 0x75;
  720. cBuff[index++] = 0x31;
  721. cBuff[index++] = 0x00;
  722. cBuff[index++] = 0x02;
  723. cBuff[index++] = 0x0A;
  724. cBuff[index++] = 0x00;
  725. cBuff[index++] = 0x12;
  726. cBuff[index++] = 0x06;
  727. cBuff[index++] = 0x33;
  728. cBuff[index++] = 0x0A;
  729. cBuff[index++] = 0x80;
  730. cBuff[index++] = 0x01;
  731. cBuff[index++] = 0xA2;
  732. cBuff[index++] = 0x29;
  733. cBuff[index++] = 0x18;
  734. cBuff[index] = 0x00;
  735. return new Uint8Array(cBuff).buffer;
  736. }
  737. function makeDisconnectWithETC() {
  738. let cBuff = []
  739. let index = 0
  740. cBuff[index++] = 0xFE;
  741. cBuff[index++] = 0x01;
  742. cBuff[index++] = 0x00;
  743. cBuff[index++] = 0x14;
  744. cBuff[index++] = 0x75;
  745. cBuff[index++] = 0x31;
  746. cBuff[index++] = 0x00;
  747. cBuff[index++] = 0x02;
  748. cBuff[index++] = 0x0A;
  749. cBuff[index++] = 0x00;
  750. cBuff[index++] = 0x12;
  751. cBuff[index++] = 0x06;
  752. cBuff[index++] = 0x33;
  753. cBuff[index++] = 0x0A;
  754. cBuff[index++] = 0x80;
  755. cBuff[index++] = 0x01;
  756. cBuff[index++] = 0xAE;
  757. cBuff[index] = 0x25;
  758. return new Uint8Array(cBuff).buffer;
  759. }
  760. function unpaketProto(recvDataCache) {
  761. let data = recvDataCache.slice(12)
  762. let dataLen = recvDataCache[13]
  763. let st = data[0];
  764. let ctl = data[2];
  765. let dataLet = data[3];
  766. let ndata = data.slice(4);
  767. let StartPackFlag = ((ctl & 0x80) > 0) ? true : false
  768. let CurrentPackNum = ctl & 0x7F;
  769. mCurrentPacketNum = CurrentPackNum;
  770. let operatorStatus = 0;
  771. if (st != 0x33) {
  772. return -1;
  773. console.log('[ERROR] The received data head ST is not right!')
  774. }
  775. if (StartPackFlag) {
  776. operatorStatus = 1;
  777. }
  778. mEtcRawData = ndata.slice(0, dataLen - 1)
  779. mEtcRawDataLen = dataLen
  780. return operatorStatus & CurrentPackNum;
  781. }
  782. function unpaketProto_1(recvDataCache) {
  783. console.log('***** recvDataCache:' + ab2hexArr(recvDataCache))
  784. let data = recvDataCache.slice(12)
  785. let dataLen = recvDataCache[13]
  786. let st = data[0];
  787. let ctl = data[2];
  788. let dataLet = data[3];
  789. let ndata = data.slice(4);
  790. let StartPackFlag = ((ctl & 0x80) > 0) ? true : false
  791. let CurrentPackNum = ctl & 0x7F;
  792. let operatorStatus = 0;
  793. if (st != 0x33) {
  794. return -1;
  795. console.log('[ERROR] The received data head ST is not right!')
  796. }
  797. if (StartPackFlag) {
  798. operatorStatus = 1;
  799. }
  800. mAbCmdBuf = ndata.slice(0, dataLen - 1)
  801. mnCmdBufLen = dataLen
  802. return operatorStatus & CurrentPackNum;
  803. }
  804. function unpackXXCmdPacket(recvDataCache) {
  805. mAbCmdBuf = recvDataCache;
  806. mnCmdBufLen = 0
  807. // unpaketProto_1(recvDataCache)
  808. // let cmdStatus = mAbCmdBuf[1];
  809. // if (cmdStatus != 0) {
  810. // return 4;
  811. // }
  812. let abCmdTLVLen = (mAbCmdBuf[4] << 8) + mAbCmdBuf[3];
  813. if (abCmdTLVLen <= 0) {
  814. receiveDataCallBack(makeRes(-1, [], '未插卡'))
  815. return
  816. }
  817. console.log('mAbCmdBuf[4]' + mAbCmdBuf[4] + ',mAbCmdBuf[3]:' + mAbCmdBuf[3])
  818. console.log('mAbCmdBuf:' + ab2hexArr(mAbCmdBuf))
  819. let abCmdTLVBuf = mAbCmdBuf.slice(5, abCmdTLVLen + 5)
  820. console.log('abCmdTLVLen' + abCmdTLVLen)
  821. console.log('abCmdTLVBuf:' + ab2hexArr(abCmdTLVBuf))
  822. if (abCmdTLVBuf[0] == 0xA3 && abCmdTLVBuf[2] == 0xA2) {
  823. let abCmdTLVBuf1 = abCmdTLVBuf.slice(2, abCmdTLVLen)
  824. let len = abCmdTLVLen - 2;
  825. while (len > 0) {
  826. let rData = abCmdTLVBuf1.slice(3, abCmdTLVBuf1[1] - 1 + 3)
  827. console.log('rData:' + ab2hexArr(rData))
  828. mReturnedRawCmdData.push(bytes2Str(rData))
  829. abCmdTLVBuf1 = abCmdTLVBuf1.slice(3 + abCmdTLVBuf1[1] - 1, abCmdTLVBuf1.length)
  830. console.log('abCmdTLVBuf1:' + ab2hexArr(abCmdTLVBuf1))
  831. len = abCmdTLVBuf1.length;
  832. console.log('len:' + len)
  833. }
  834. } else {
  835. return 5;
  836. }
  837. return 0;
  838. }
  839. function makeXXCmdPacket(cmdType, reqData) {
  840. let index = 0;
  841. let cmdDataLen = reqData.length
  842. let cmdPacketDataLen = cmdDataLen + 10;
  843. let pbData = [];
  844. if (KYunNanObuMultiplePackages == 0) {
  845. pbData[index++] = ((cmdType == 0xA3) ? cmdType : 0xA8); // PICC 通道 0
  846. pbData[index++] = 0x00; // 明文数据 01 加密数据 1
  847. pbData[index++] = 0x00; //总长度 2
  848. pbData[index++] = 0x00; //总长度,后续代码会算 3
  849. pbData[index++] = 0xA1; //4
  850. pbData[index++] = 0x00; //Len1 1字节长度,真实数值后续代码算 5
  851. pbData[index++] = 0xA4; //6
  852. pbData[index++] = 0x00; //Len2 1字节长度,真实数值后续代码算 7
  853. //stable data
  854. pbData[index++] = 0x38; //8
  855. pbData[index++] = 0x01; //9
  856. pbData[index++] = 0x01; //10
  857. pbData[index++] = 0x39; //11
  858. pbData[index++] = cmdDataLen; //Len3
  859. pbData.push.apply(pbData, reqData)
  860. index += cmdDataLen;
  861. //stable data
  862. pbData[index++] = 0x3C;
  863. pbData[index++] = 0x03;
  864. pbData[index++] = 0x90;
  865. pbData[index++] = 0x00;
  866. pbData[index] = 0x00;
  867. pbData[7] = cmdDataLen + 1 + 9; //Len2长度 = 指令数据长度+两组固定数据长度+cmdDatalen自身长度标识1字节
  868. pbData[5] = pbData[7] + 2; //2包含固定数据0xA4与 pbData[7]本身长度标识1字节
  869. pbData[2] = ((pbData[5] + 2) & 0xff);
  870. pbData[3] = ((pbData[5]) >> 8) & 0xff; //总长度 小端模式
  871. } else {
  872. pbData[index++] = ((cmdType == 0xA3) ? cmdType : 0xA8);
  873. pbData[index++] = 0x00; //0 明文 1密文
  874. pbData[index++] = (cmdDataLen & 0xff); //总长度
  875. pbData[index++] = (cmdDataLen >> 8) & 0xff; //总长度,后续代码会算
  876. pbData.push.apply(pbData, reqData)
  877. }
  878. mTransData.push.apply(mTransData, pbData.slice(0, pbData[5] + 6))
  879. return 0;
  880. }
  881. function str2Bytes(str) {
  882. var pos = 0;
  883. var len = str.length;
  884. if (len % 2 != 0) {
  885. return null;
  886. }
  887. len /= 2;
  888. var hexA = new Array();
  889. for (var i = 0; i < len; i++) {
  890. var s = str.substr(pos, 2);
  891. var v = parseInt(s, 16);
  892. hexA.push(v);
  893. pos += 2;
  894. }
  895. return hexA;
  896. }
  897. function bytes2Str(arr) {
  898. var str = "";
  899. for (var i = 0; i < arr.length; i++) {
  900. var tmp = arr[i].toString(16);
  901. if (tmp.length == 1) {
  902. tmp = "0" + tmp;
  903. }
  904. str += tmp;
  905. }
  906. return str;
  907. }
  908. function makeRes(code, data, msg) {
  909. return {
  910. code: code,
  911. data: data,
  912. msg: msg
  913. }
  914. }
  915. module.exports = {
  916. connectDevice: connectDevice,
  917. transCmd: transCmd,
  918. disconnectDevice: disconnectDevice
  919. }