Skip to content

Commit

Permalink
v0.1.6: minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed Dec 10, 2022
1 parent bce3b6f commit 0e4fc59
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 62 deletions.
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.5",
"version": "0.1.6",
"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
1 change: 1 addition & 0 deletions src/Pn532.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ export default class Pn532 {
async (ctx, next) => {
ctx.startedAt = Date.now()
while (true) {
if (!this?.$adapter?.isOpen?.()) throw new Error('device disconnected')
ctx.nowts = Date.now()
if (ctx.nowts > ctx.startedAt + ctx.timeout) throw new Error(`readRespTimeout ${ctx.timeout}ms`)
while (this.respBuf.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/HexMockAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class HexMockAdapter {
return {
connect () {},
disconnect () {},
isOpen () {},
isOpen () { return true },
isSupport () {},
}
}
Expand Down
72 changes: 42 additions & 30 deletions src/plugin/Hf14a.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default class Pn532Hf14a {
const { Packet, pn532, utils } = context
const { retry } = utils

function isAdapterOpen () {
return pn532?.$adapter?.isOpen?.()
}

/**
* @typedef {object} Pn532Hf14a~MifareTarget
* @property {Packet} atqa 2 bytes ATQA of target (aka `SENS_RES`).
Expand Down Expand Up @@ -95,6 +99,10 @@ export default class Pn532Hf14a {
return isPresent
}

async function inReleaseIfOpened () {
if (isAdapterOpen()) await pn532.inRelease().catch(() => {})
}

/**
* This function is used to detect one mifare target in passive mode.
* @memberof Pn532Hf14a
Expand All @@ -106,7 +114,7 @@ export default class Pn532Hf14a {
try {
return (await inListPassiveTarget())?.[0]
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -168,6 +176,7 @@ export default class Pn532Hf14a {
try {
return await pn532.inDataExchange(readOpts)
} catch (err) {
if (!isAdapterOpen()) throw err // rethrow error if adapter is closed
await pn532.inDeselect({ tg: 1 }).catch(() => {})
await mfAuthBlock(authOpts).catch(() => {})
throw new Error(`Failed to read block ${readOpts?.data?.[1]}`)
Expand Down Expand Up @@ -203,7 +212,7 @@ export default class Pn532Hf14a {
{ block, isKb, key, uid },
)
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -234,7 +243,7 @@ export default class Pn532Hf14a {
}
throw new Error(`Failed to read block ${block}`)
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -275,7 +284,7 @@ export default class Pn532Hf14a {
data.set(key, [48, 58][isKb])
return { data, success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -324,7 +333,7 @@ export default class Pn532Hf14a {
}
return { data, success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -369,7 +378,7 @@ export default class Pn532Hf14a {
}
break
} catch (err) {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
uid = (await inListPassiveTarget())?.[0]?.uid
}
}
Expand All @@ -381,7 +390,7 @@ export default class Pn532Hf14a {
}
return { data, success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand All @@ -390,6 +399,7 @@ export default class Pn532Hf14a {
try {
await pn532.inDataExchange(writeOpts)
} catch (err) {
if (!isAdapterOpen()) throw err // rethrow error if adapter is closed
await pn532.inDeselect({ tg: 1 }).catch(() => {})
await mfAuthBlock(authOpts).catch(() => {})
throw new Error(`Failed to write block ${writeOpts?.data?.[1]}`)
Expand Down Expand Up @@ -422,7 +432,7 @@ export default class Pn532Hf14a {
{ block, isKb, key, uid },
)
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -457,7 +467,7 @@ export default class Pn532Hf14a {
}
if (!isSuccess) throw new Error(`Failed to write block ${block}`)
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -505,7 +515,7 @@ export default class Pn532Hf14a {
}
if (!isSuccess) throw new Error('Failed to write block 0')
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -540,15 +550,15 @@ export default class Pn532Hf14a {
)
success[i] = 1
if (block === 0) { // if block 0 write successfully, need to read new uid
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
uid = (await inListPassiveTarget())?.[0]?.uid
if (!uid) throw new Error('Failed to select card')
}
} catch (err) {}
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -586,7 +596,7 @@ export default class Pn532Hf14a {
)
success[i] = 1
if (block === 0) { // if block 0 write successfully, need to read new uid
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
uid = (await inListPassiveTarget())?.[0]?.uid
if (!uid) throw new Error('Failed to select card')
}
Expand All @@ -596,7 +606,7 @@ export default class Pn532Hf14a {
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -638,23 +648,23 @@ export default class Pn532Hf14a {
success[block] = 1
successBlocks++
if (block === 0) { // if block 0 write successfully, need to read new uid
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
uid = (await inListPassiveTarget())?.[0]?.uid
if (!uid) throw new Error('Failed to select card')
}
} catch (err) {}
}
break
} catch (err) {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
uid = (await inListPassiveTarget())?.[0]?.uid
}
}
}
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand All @@ -666,6 +676,7 @@ export default class Pn532Hf14a {
if (src.block !== dist.block && src.isKb === dist.isKb) await mfAuthBlock(dist)
await pn532.inDataExchange({ data: new Packet([0xB0, dist.block]) })
} catch (err) {
if (!isAdapterOpen()) throw err // rethrow error if adapter is closed
await pn532.inDeselect({ tg: 1 }).catch(() => {})
const cmdStr = {
0xA0: 'write',
Expand Down Expand Up @@ -704,7 +715,7 @@ export default class Pn532Hf14a {
if (!uid) throw new Error('Failed to select card')
await mfIdrTransferBlockHelper(data, { ...src, uid }, { ...dist, uid })
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -733,7 +744,7 @@ export default class Pn532Hf14a {
if (!uid) throw new Error('Failed to select card')
await mfIdrTransferBlockHelper(data, { ...src, uid }, { ...dist, uid })
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand All @@ -760,7 +771,7 @@ export default class Pn532Hf14a {
if (!uid) throw new Error('Failed to select card')
await mfIdrTransferBlockHelper(data, { ...src, uid }, { ...dist, uid })
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand All @@ -786,9 +797,10 @@ export default class Pn532Hf14a {
await pn532.writeRegisters({ 0x633D: 0x00 })
await pn532.inCommunicateThru({ data: new Packet([0x43]) })
} catch (err) {
if (!isAdapterOpen()) throw err // rethrow error if adapter is closed
throw new Error('Failed to open backdoor')
} finally {
await pn532.writeRegisters({ 0x6302: 0x80, 0x6303: 0x80 })
if (isAdapterOpen()) await pn532.writeRegisters({ 0x6302: 0x80, 0x6303: 0x80 })
}
}

Expand All @@ -797,7 +809,7 @@ export default class Pn532Hf14a {
try {
return await pn532.inDataExchange(readOpts)
} catch (err) {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
await mfBackdoorGen1a().catch(() => {})
throw new Error(`Failed to read block ${readOpts?.data?.[1]}`)
}
Expand All @@ -819,7 +831,7 @@ export default class Pn532Hf14a {
await mfBackdoorGen1a()
return await mfReadBlockGen1aHelper({ data: new Packet([0x30, block]), respValidator: mfBlockRespValidator })
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -849,7 +861,7 @@ export default class Pn532Hf14a {
}
return { data, success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -881,7 +893,7 @@ export default class Pn532Hf14a {
}
return { data, success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand All @@ -890,7 +902,7 @@ export default class Pn532Hf14a {
try {
await pn532.inDataExchange(writeOpts)
} catch (err) {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
await mfBackdoorGen1a().catch(() => {})
throw new Error(`Failed to write block ${writeOpts?.data?.[1]}`)
}
Expand All @@ -913,7 +925,7 @@ export default class Pn532Hf14a {
await mfBackdoorGen1a()
await mfWriteBlockGen1aHelper({ data: new Packet([0xA0, block, ...data]) })
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -943,7 +955,7 @@ export default class Pn532Hf14a {
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -973,7 +985,7 @@ export default class Pn532Hf14a {
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down Expand Up @@ -1042,7 +1054,7 @@ export default class Pn532Hf14a {
}
return { success }
} finally {
await pn532.inRelease().catch(() => {})
await inReleaseIfOpened()
}
}

Expand Down
Loading

0 comments on commit 0e4fc59

Please sign in to comment.