Skip to content

Commit

Permalink
v0.1.11: Add taichunmin recoder and code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Jan 9, 2023
1 parent 40707b8 commit db976c0
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ console.log(JSON.stringify(resp6)) // {"success":[1,1,1,1,... (truncated)]}
## Related links

* [PN532 User Manual v1.6 | NXP](https://www.nxp.com/docs/en/user-guide/141520.pdf)
* [PN532/C1 Product data sheet v3.6 | NXP](https://www.nxp.com/docs/en/nxp/data-sheets/PN532_C1.pdf)
* [MF1S50YYX_V1 v3.2 | NXP](https://www.nxp.com/docs/en/data-sheet/MF1S50YYX_V1.pdf)
* [A 2018 practical guide to hacking NFC/RFID](https://smartlockpicking.com/slides/Confidence_A_2018_Practical_Guide_To_Hacking_RFID_NFC.pdf)
* [Magic Cards Notes](https://github.com/RfidResearchGroup/proxmark3/blob/master/doc/magic_cards_notes.md)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pn532.js",
"version": "0.1.10",
"version": "0.1.11",
"author": "taichunmin <[email protected]>",
"browser": "dist/pn532.min.js",
"description": "pn532.js is a JavaScript library for PN532 base on Web Bluetooth and Web Serial.",
Expand Down
12 changes: 6 additions & 6 deletions src/Packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class Packet extends Uint8Array {
static fromHex (hex, reverse = false) {
hex = hex.replace(/[^0-9A-Fa-f]/g, '')
if (hex.length & 1) throw new TypeError('invalid hex string')
const pack = new Packet(hex.length >> 1)
const pack = new Packet(hex.length >>> 1)
for (let i = 0; i < pack.length; i++) pack[i] = parseInt(hex.substr(i << 1, 2), 16)
if (reverse) pack.reverse()
return pack
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class Packet extends Uint8Array {
for (let i = 0; i < this.length; i += 3) {
let u24 = 0
for (let j = 0; j < 3; j++) u24 |= ((i + j) < this.length ? this[i + j] : 0) << (16 - j * 8)
tmp.push(_.times(Math.min(this.length - i + 1, 4), j => BASE64URL_CHAR[(u24 >> (18 - 6 * j)) & 0x3F]).join(''))
tmp.push(_.times(Math.min(this.length - i + 1, 4), j => BASE64URL_CHAR[(u24 >>> (18 - 6 * j)) & 0x3F]).join(''))
}
return tmp.join('')
}
Expand Down Expand Up @@ -452,7 +452,7 @@ export default class Packet extends Uint8Array {
*/
setUint24 (offset, value, little = true) {
const [p8, p16] = little ? [offset + 2, offset] : [offset, offset + 1]
this.setUint8(p8, (value >> 16) & 0xFF)
this.setUint8(p8, (value >>> 16) & 0xFF)
this.setUint16(p16, value & 0xFFFF, little)
return this
}
Expand Down Expand Up @@ -494,8 +494,8 @@ export default class Packet extends Uint8Array {
* @returns {number} A bit value.
*/
getBit (bitOffset, little = false) {
const byteOffset = little ? bitOffset >> 3 : this.length - (bitOffset >> 3) - 1
return (this[byteOffset] >> (bitOffset & 7)) & 1
const byteOffset = little ? bitOffset >>> 3 : this.length - (bitOffset >>> 3) - 1
return (this[byteOffset] >>> (bitOffset & 7)) & 1
}

/**
Expand All @@ -508,7 +508,7 @@ export default class Packet extends Uint8Array {
* @param {boolean} [littleEndian=false] Indicates whether the bit value is stored in [little- or big-endian](https://developer.mozilla.org/docs/Glossary/Endianness) format. If `false` a big-endian value is written.
*/
setBit (bitOffset, value, little = false) {
const byteOffset = little ? bitOffset >> 3 : this.length - (bitOffset >> 3) - 1
const byteOffset = little ? bitOffset >>> 3 : this.length - (bitOffset >>> 3) - 1
bitOffset &= 7
value = value ? 1 : 0
this[byteOffset] = (this[byteOffset] & ~(1 << bitOffset)) | (value << bitOffset)
Expand Down
13 changes: 7 additions & 6 deletions src/Pn532.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export default class Pn532 {
if (data.byteLength > 65533) throw new TypeError('data.byteLength > 65533') // TFI + CMD + Data (65533 bytes)
const pack = new Packet(data.byteLength + 12) // PREAMBLE (5 bytes) + LEN (2 bytes) + LCS + TFI + CMD + Data (65533 bytes) + DCS + POSTAMBLE
const len = data.byteLength + 2
const [lenM, lenL] = [len >> 8, len & 0xFF]
const [lenM, lenL] = [len >>> 8, len & 0xFF]
pack.set(new Packet([0xFF, 0xFF, 0xFF, lenM, lenL, -(lenM + lenL), 0xD4]), 2)
if (data.byteLength) pack.set(data, 10)
const dcs = pack.byteLength - 2
Expand Down Expand Up @@ -678,9 +678,9 @@ export default class Pn532 {
await this.sendCommandNormal({ cmd: 0x0C })
const { data: resData } = await this.readRespTimeout({ cmd: 0x0D })
return _.fromPairs([
..._.times(6, i => [`p3${i}`, (resData[0] >> i) & 1]),
..._.times(2, i => [`p7${i + 1}`, (resData[1] >> (i + 1)) & 1]),
..._.times(2, i => [`i${i}`, (resData[2] >> i) & 1]),
..._.times(6, i => [`p3${i}`, (resData[0] >>> i) & 1]),
..._.times(2, i => [`p7${i + 1}`, (resData[1] >>> (i + 1)) & 1]),
..._.times(2, i => [`i${i}`, (resData[2] >>> i) & 1]),
])
}

Expand Down Expand Up @@ -898,12 +898,13 @@ protocol.
* @param {object} args
* @param {Packet} args.data An array of raw data to be sent to the target by the PN532 (max. 264 bytes, see §7.4.7, p:186).
* @param {number} args.timeout The maxinum timeout for waiting response.
* @param {function} args.respValidator Custom validator of resp.
* @returns {Promise<Pn532Frame>} Resolve with raw response need to be parsed.
*/
async inCommunicateThru ({ data = new Packet(), timeout } = {}) {
async inCommunicateThru ({ data = new Packet(), timeout, respValidator } = {}) {
this.clearRespBuf()
await this.sendCommandNormal({ cmd: 0x42, data })
const resp = await this.readRespTimeout({ cmd: 0x43, timeout })
const resp = await this.readRespTimeout({ cmd: 0x43, timeout, respValidator })
const statusErr = hasPn532StatusError(resp.data[0])
if (statusErr) throw statusErr
resp.data = resp.data.subarray(1)
Expand Down
Loading

0 comments on commit db976c0

Please sign in to comment.