Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication support #4

Merged
merged 4 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,42 +170,75 @@ See above how to set up reader.
```javascript
reader.on('card', async card => {


// Notice: reading data from Mifare Classic cards (e.g. Mifare 1K) requires,
// that the data block must be authenticated first
// don't forget to fill your keys and types
// reader.authenticate(blockNumber, keyType, key)
// uncomment when you need it

// try {
//
// const key = 'FFFFFFFFFFFF';
// const keyType = KEY_TYPE_A;
//
// // we will authenticate block 4, 5, 6, 7 (which we want to read)
// await Promise.all([
// reader.authenticate(4, keyType, key),
// reader.authenticate(5, keyType, key),
// reader.authenticate(6, keyType, key),
// reader.authenticate(7, keyType, key)
// ]);
//
// console.log(`blocks successfully authenticated`);
//
// } catch (err) {
// console.error(`error when authenticating data`, { reader: reader.name, card, err });
// return;
// }


// example reading 16 bytes assuming containing 16bit integer
try {

// reader.read(blockNumber, length, blockSize = 4, packetSize = 16)
// blockNumber - memory block number where to start reading
// length - how many bytes to read
// Caution! length must be divisible by blockSize
// - blockNumber - memory block number where to start reading
// - length - how many bytes to read
// ! Caution! length must be divisible by blockSize

const data = await reader.read(4, 16);

pretty.info(`data read`, { reader: reader.name, card, data });
console.log(`data read`, { reader: reader.name, card, data });

const payload = data.readInt16BE();

pretty.info(`data converted`, payload);
console.log(`data converted`, payload);

} catch (err) {
pretty.error(`error when reading data`, { reader: reader.name, card, err });
console.error(`error when reading data`, { reader: reader.name, card, err });
}


// example write 16bit integer
try {

// reader.write(blockNumber, data, blockSize = 4)
// blockNumber - memory block number where to start writing
// data - what to write
// Caution! data.length must be divisible by blockSize
// - blockNumber - memory block number where to start writing
// - data - what to write
// ! Caution! data.length must be divisible by blockSize

const data = Buffer.allocUnsafe(16);
data.writeInt16BE(789);
data.writeInt16BE(800);

await reader.write(4, data);

pretty.info(`data written`, { reader: reader.name, card });
console.log(`data written`, { reader: reader.name, card });

} catch (err) {
pretty.error(`error when writing data`, { reader: reader.name, card, err });
console.error(`error when writing data`, { reader: reader.name, card, err });
}


});
```

Expand Down
3 changes: 2 additions & 1 deletion src/NFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import EventEmitter from 'events';
import Reader from './Reader';


export { TAG_ISO_14443_3, TAG_ISO_14443_4 } from './Reader';
export * from './Reader';
export * from './errors';


class NFC extends EventEmitter {
Expand Down
Loading