| |
| |
| |
|
|
| import { invoke } from '@tauri-apps/api/core' |
|
|
| export const RTD_TEXT = [0x54] |
| export const RTD_URI = [0x55] |
|
|
| export interface UriFilter { |
| scheme?: string |
| host?: string |
| pathPrefix?: string |
| } |
|
|
| export enum TechKind { |
| IsoDep, |
| MifareClassic, |
| MifareUltralight, |
| Ndef, |
| NdefFormatable, |
| NfcA, |
| NfcB, |
| NfcBarcode, |
| NfcF, |
| NfcV |
| } |
|
|
| export type ScanKind = |
| | { |
| type: 'tag' |
| uri?: UriFilter |
| mimeType?: string |
| } |
| | { |
| type: 'ndef' |
| uri?: UriFilter |
| mimeType?: string |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| techLists?: TechKind[][] |
| } |
|
|
| export interface ScanOptions { |
| keepSessionAlive?: boolean |
| |
| message?: string |
| |
| successMessage?: string |
| } |
|
|
| export interface WriteOptions { |
| kind?: ScanKind |
| |
| message?: string |
| |
| successfulReadMessage?: string |
| |
| successMessage?: string |
| } |
|
|
| export enum NFCTypeNameFormat { |
| Empty = 0, |
| NfcWellKnown = 1, |
| Media = 2, |
| AbsoluteURI = 3, |
| NfcExternal = 4, |
| Unknown = 5, |
| Unchanged = 6 |
| } |
|
|
| export interface TagRecord { |
| tnf: NFCTypeNameFormat |
| kind: number[] |
| id: number[] |
| payload: number[] |
| } |
|
|
| export interface Tag { |
| id: number[] |
| kind: string[] |
| records: TagRecord[] |
| } |
|
|
| export interface NFCRecord { |
| format: NFCTypeNameFormat |
| kind: number[] |
| id: number[] |
| payload: number[] |
| } |
|
|
| export function record( |
| format: NFCTypeNameFormat, |
| kind: string | number[], |
| id: string | number[], |
| payload: string | number[] |
| ): NFCRecord { |
| return { |
| format, |
| kind: |
| typeof kind === 'string' |
| ? Array.from(new TextEncoder().encode(kind)) |
| : kind, |
| id: typeof id === 'string' ? Array.from(new TextEncoder().encode(id)) : id, |
| payload: |
| typeof payload === 'string' |
| ? Array.from(new TextEncoder().encode(payload)) |
| : payload |
| } |
| } |
|
|
| export function textRecord( |
| text: string, |
| id?: string | number[], |
| language: string = 'en' |
| ): NFCRecord { |
| const payload = Array.from(new TextEncoder().encode(language + text)) |
| payload.unshift(language.length) |
| return record(NFCTypeNameFormat.NfcWellKnown, RTD_TEXT, id ?? [], payload) |
| } |
|
|
| const protocols = [ |
| '', |
| 'http://www.', |
| 'https://www.', |
| 'http://', |
| 'https://', |
| 'tel:', |
| 'mailto:', |
| 'ftp://anonymous:anonymous@', |
| 'ftp://ftp.', |
| 'ftps://', |
| 'sftp://', |
| 'smb://', |
| 'nfs://', |
| 'ftp://', |
| 'dav://', |
| 'news:', |
| 'telnet://', |
| 'imap:', |
| 'rtsp://', |
| 'urn:', |
| 'pop:', |
| 'sip:', |
| 'sips:', |
| 'tftp:', |
| 'btspp://', |
| 'btl2cap://', |
| 'btgoep://', |
| 'tcpobex://', |
| 'irdaobex://', |
| 'file://', |
| 'urn:epc:id:', |
| 'urn:epc:tag:', |
| 'urn:epc:pat:', |
| 'urn:epc:raw:', |
| 'urn:epc:', |
| 'urn:nfc:' |
| ] |
|
|
| function encodeURI(uri: string): number[] { |
| let prefix = '' |
|
|
| protocols.slice(1).forEach(function (protocol) { |
| if ( |
| (prefix.length === 0 || prefix === 'urn:') |
| && uri.indexOf(protocol) === 0 |
| ) { |
| prefix = protocol |
| } |
| }) |
|
|
| if (prefix.length === 0) { |
| prefix = '' |
| } |
|
|
| const encoded = Array.from(new TextEncoder().encode(uri.slice(prefix.length))) |
| const protocolCode = protocols.indexOf(prefix) |
| |
| encoded.unshift(protocolCode) |
|
|
| return encoded |
| } |
|
|
| export function uriRecord(uri: string, id?: string | number[]): NFCRecord { |
| return record( |
| NFCTypeNameFormat.NfcWellKnown, |
| RTD_URI, |
| id ?? [], |
| encodeURI(uri) |
| ) |
| } |
|
|
| function mapScanKind(kind: ScanKind): Record<string, unknown> { |
| const { type: scanKind, ...kindOptions } = kind |
| return { [scanKind]: kindOptions } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function scan( |
| kind: ScanKind, |
| options?: ScanOptions |
| ): Promise<Tag> { |
| return await invoke('plugin:nfc|scan', { |
| kind: mapScanKind(kind), |
| ...options |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function write( |
| records: NFCRecord[], |
| options?: WriteOptions |
| ): Promise<void> { |
| const { kind, ...opts } = options ?? {} |
| if (kind) { |
| |
| opts.kind = mapScanKind(kind) |
| } |
| await invoke('plugin:nfc|write', { |
| records, |
| ...opts |
| }) |
| } |
|
|
| export async function isAvailable(): Promise<boolean> { |
| const { available }: { available: boolean } = await invoke( |
| 'plugin:nfc|is_available' |
| ) |
| return available |
| } |
|
|