void MessageToNdef(byte[] message) { /* Parse the received data - it must be an NDEF message */ if (!Ndef.Parse(message, NdefFound)) { Trace.WriteLine("SNEP Server: got an invalid message"); } }
/**m* SpringCardNFC/NfcTlv.add_child * * SYNOPSIS * public void add_child(Ndef ndef) * * DESCRIPTION * Adds a child (ndef) to the NfcTlv object. * **/ public void add_child(Ndef ndef) { Ndef[] tmp = new Ndef[child.Length + 1]; Array.Copy(child, tmp, child.Length); tmp[tmp.Length - 1] = ndef; child = new Ndef[tmp.Length]; Array.Copy(tmp, child, child.Length); }
public SNEP_Client(Ndef _NdefDataToSend) : base("SNEP Client", SNEP.SERVER_PORT) { if (_NdefDataToSend != null) { SnepDataToSend = _NdefDataToSend.GetBytes(); } Trace.WriteLine("SNEP Client: creation with NDEF=" + (new CardBuffer(SnepDataToSend)).AsString()); }
public RtdHandoverSelector(byte[] payload, ref byte[] buffer, ref int next_ndef_starting_point) : base(Ndef.NDEF_HEADER_TNF_NFC_RTD_WKN, "Hs") { /* Take care of Payload */ int offset = 1; Ndef ndef = null; bool terminated = true; if ((payload != null) && (payload.Length > 0)) { version = payload[0]; } if (version < 0x10) { Trace.WriteLine("Incompatible version: " + String.Format("{0:x02}", version)); offset = payload.Length - 1; /* so that it won't be parsed */ } List <RtdAlternativeCarrier> altenative_carriers_list = new List <RtdAlternativeCarrier>(); while (Ndef.Parse(payload, ref offset, ref ndef, ref terminated)) { if (ndef is RtdAlternativeCarrier) { Trace.WriteLine("Got a new Alternative Carrier"); altenative_carriers_list.Add((RtdAlternativeCarrier)ndef); } if (terminated) { break; } } _alternative_carriers = altenative_carriers_list.ToArray(); /* Take care of following NDEFs */ terminated = true; List <AbsoluteUri> related_absolute_uris_list = new List <AbsoluteUri>(); while (Ndef.Parse(buffer, ref next_ndef_starting_point, ref ndef, ref terminated)) { if (ndef is AbsoluteUri) { Trace.WriteLine("Got a new Absolute Uri"); related_absolute_uris_list.Add((AbsoluteUri)ndef); } if (terminated) { break; } } _related_absolute_uris = related_absolute_uris_list.ToArray(); }
public RtdSmartPoster(byte[] payload) : base(Ndef.NDEF_HEADER_TNF_NFC_RTD_WKN, "Sp") { int offset = 0; Ndef ndef = null; bool terminated = true; while (Ndef.Parse(payload, ref offset, ref ndef, ref terminated)) { if (ndef is RtdUri) { Trace.WriteLine("Got a new URI"); Uri = (RtdUri)ndef; } else if (ndef is RtdText) { Trace.WriteLine("Got a new Text"); Title.Add((RtdText)ndef); } else if (ndef is RtdSmartPosterAction) { Trace.WriteLine("Got a new SmartPoster Action"); Action = (RtdSmartPosterAction)ndef; } else if (ndef is RtdSmartPosterTargetIcon) { Trace.WriteLine("Got a new SmartPoster Icon"); TargetIcon = (RtdSmartPosterTargetIcon)ndef; } else if (ndef is RtdSmartPosterTargetType) { Trace.WriteLine("Got a new SmartPoster MIME type"); TargetType = (RtdSmartPosterTargetType)ndef; } else if (ndef is RtdSmartPosterTargetSize) { Trace.WriteLine("Got a new SmartPoster Size"); TargetSize = (RtdSmartPosterTargetSize)ndef; } else { Trace.WriteLine("Got an unknown child in the SmartPoster"); } if (terminated) { break; } } }
void NdefFound(Ndef ndef) { if (ndef == null) { Trace.WriteLine("SNEP Server: empty NDEF received"); } else { Trace.WriteLine("SNEP Server: valid NDEF received"); } if (OnNdefReceived != null) { OnNdefReceived(ndef); } }
/**m* SpringCardNFC/Ndef.Parse * * SYNOPSIS * public static bool Parse(byte [] buffer, NdefFoundCallback callback) * public static bool Parse(byte[] buffer, ref int offset, ref Ndef ndef, ref bool terminated) * public static Ndef[] Parse(byte[] buffer) * * DESCRIPTION * Analyses a bytes array to retrieve one or several NDEFs in it * * SEE ALSO * Ndef.Parse * **/ public static bool Parse(byte[] buffer, NdefFoundCallback callback) { int offset = 0; Ndef ndef = null; bool terminated = true; while (Ndef.Parse(buffer, ref offset, ref ndef, ref terminated)) { if (callback != null) { callback(ndef); } if (terminated) { return(true); } } Trace.WriteLine("Parsing failed at offset " + offset); return(false); }
public static Ndef[] Parse(byte[] buffer) { int offset = 0; List <Ndef> ndefs = new List <Ndef>(); Ndef ndef = null; bool terminated = true; while (Ndef.Parse(buffer, ref offset, ref ndef, ref terminated)) { ndefs.Add(ndef); if (terminated) { break; } } if (ndefs.Count == 0) { return(null); } return(ndefs.ToArray()); }
protected override bool Read() { long ndef_file_size = 0; byte[] buffer; if (!Recognize(_channel, ref _locked, ref _max_le, ref _max_lc, ref _ndef_file_id, ref ndef_file_size)) { return(false); } if (ndef_file_size > 2) { _capacity = ndef_file_size - 2; } else { _capacity = 0; } _formatted = true; _application_selected = true; _file_selected = NDEF_CC_FILE_ID; if (!SelectFile(_ndef_file_id)) { Trace.WriteLine("Failed to select the NDEF file"); return(false); } buffer = ReadBinary(0, 2); if (buffer == null) { Trace.WriteLine("Failed to read from the NDEF file"); return(false); } if ((buffer[0] == 0) && (buffer[1] == 0)) { Trace.WriteLine("Tag is empty"); _is_empty = true; return(true); } _is_empty = false; long ndef_announced_size = (long)(buffer[0] * 0x0100 + buffer[1]); if ((ndef_announced_size > (ndef_file_size - 2)) || (ndef_announced_size > 0xFFFF)) { Trace.WriteLine("The NDEF file contains an invalid length"); return(false); } byte[] content = new byte[ndef_announced_size]; ushort offset_in_file = 2; long offset_in_content = 0; while (offset_in_content < content.Length) { if (_max_le > 254) { buffer = ReadBinary(offset_in_file, 254); } else { buffer = ReadBinary(offset_in_file, _max_le); } if ((buffer == null) || (buffer.Length == 0)) { buffer = ReadBinary(offset_in_file, 0); if ((buffer == null) || (buffer.Length == 0)) { Trace.WriteLine("Failed to read the NDEF file at offset " + offset_in_file); return(false); } } for (int i = 0; i < buffer.Length; i++) { if (offset_in_content >= content.Length) { break; } content[offset_in_content++] = buffer[i]; } offset_in_file += (ushort)buffer.Length; if ((offset_in_content >= content.Length) || (offset_in_file >= ndef_file_size)) { break; } } Ndef[] ndefs = Ndef.Parse(content); if (ndefs == null) { Trace.WriteLine("The NDEF is invalid or unsupported"); return(true); } Trace.WriteLine(ndefs.Length + " NDEF record(s) found in the tag"); /* This NDEF is the new content of the tag */ Content.Clear(); for (int i = 0; i < ndefs.Length; i++) { Content.Add(ndefs[i]); } return(true); }
void ReaderStatusChanged(uint ReaderState, CardBuffer CardAtr) { if (ReaderState == SCARD.STATE_UNAWARE) { Trace.WriteLine("*** Reader removed ***"); if (_AdapterErrorCallback != null) { _AdapterErrorCallback(); } if (_llcp != null) { _llcp.Stop(); _llcp = null; } _reader = null; return; } if ((ReaderState & SCARD.STATE_EMPTY) != 0) { if (_llcp != null) { _llcp.Stop(); _llcp = null; } Trace.WriteLine("*** Target removed ***"); } else if ((ReaderState & SCARD.STATE_UNAVAILABLE) != 0) { } else if ((ReaderState & SCARD.STATE_MUTE) != 0) { } else if ((ReaderState & SCARD.STATE_INUSE) != 0) { } else if ((ReaderState & SCARD.STATE_PRESENT) != 0) { Trace.WriteLine("*** Target arrived ***"); if (_llcp == null) { _llcp = new LlcpInitiator(_reader); if (_CreateNdefCallback != null) { Ndef ndef = _CreateNdefCallback(); if (ndef != null) { Trace.WriteLine("Create SNEP Client (sender)"); SNEP_Client client = new SNEP_Client(ndef); client.OnMessageSent = new SNEP_Client.SNEPMessageSentCallback(_NdefSentCallback); client.MessageSentCallbackOnAcknowledge = true; _llcp.RegisterClient(client); } } _llcp.Start(); } } }
protected override bool Read() { Trace.WriteLine("Reading the NFC Forum type 2 Tag"); ushort page = 0; if (!Recognize(_channel, ref _formatted, ref _formattable, ref _locked)) { return(false); } CardBuffer buffer = new CardBuffer(); for (page = 0; page < 256; page += 4) { byte[] data = ReadBinary(_channel, page, READ_4_PAGES); if (data == null) { break; } if (page > 0) { bool same_as_header = true; for (int i = 0; i < OFFSET_USER_DATA; i++) { if (data[i] != buffer.GetByte(i)) { same_as_header = false; break; } } if (same_as_header) { break; } } buffer.Append(data); } Trace.WriteLine("Read " + buffer.Length + "B of data from the Tag"); _raw_data = buffer.GetBytes(); _capacity = _raw_data.Length; if (_capacity <= OFFSET_USER_DATA) { _capacity = 0; return(false); } if (!_formatted) { /* Guess the capacity from the read area */ if ((_capacity > 64) && !_formatted) { /* Drop the 16 last bytes if they are not empty (locks on Mifare UltraLight C) */ bool locks_found = false; for (long i = _capacity - 16; i < _capacity; i++) { if (_raw_data[i] != 0) { locks_found = true; break; } } if (locks_found) { Trace.WriteLine("Locks found at the end"); _capacity -= 16; } } _capacity -= OFFSET_USER_DATA; Trace.WriteLine("The Tag is not formatted, capacity=" + _capacity + "B"); } else { /* Read the capacity in the CC */ _capacity = 8 * _raw_data[14]; Trace.WriteLine("The Tag is formatted, capacity read from the CC=" + _capacity + "B"); } /* Is the tag empty ? */ _is_empty = true; for (long i = 0; i < _capacity; i++) { if (_raw_data[OFFSET_USER_DATA + i] != 0) { _is_empty = false; break; } } if (_is_empty) { Trace.WriteLine("The Tag is empty"); return(true); } byte[] ndef_data = null; if (!ParseUserData(buffer.GetBytes(OFFSET_USER_DATA, -1), ref ndef_data)) { Trace.WriteLine("The parsing of the Tag failed"); return(false); } if (ndef_data == null) { Trace.WriteLine("The Tag doesn't contain a NDEF"); _is_empty = true; return(true); } _is_empty = false; Ndef[] t = Ndef.Parse(ndef_data); if (t == null) { Trace.WriteLine("The NDEF is invalid or unsupported"); return(false); } Trace.WriteLine(t.Length + " NDEF record(s) found in the Tag"); /* This NDEF is the new content of the tag */ Content.Clear(); for (int i = 0; i < t.Length; i++) { Content.Add(t[i]); } return(true); }
public RtdUri(Ndef record) : base(record) { }
public Ndef(Ndef ndef) { TNF = ndef.TNF; TYPE = ndef.TYPE; PAYLOAD = ndef.PAYLOAD; }
public static bool Parse(byte[] buffer, ref int offset, ref Ndef ndef, ref bool terminated) { if (offset > buffer.Length) { return(false); } terminated = false; ndef = null; /* Header */ if (offset + 1 > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Header' byte"); return(false); } byte header = buffer[offset++]; if (header == 0) { Trace.WriteLine("Empty byte?"); return(false); } /* Type length */ if (offset + 1 > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Type Length' byte"); return(false); } int type_length = buffer[offset++]; /* Payload length */ int payload_length = 0; if ((header & NDEF_HEADER_SHORT_RECORD) != 0) { if (offset + 1 > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Payload Length' byte"); return(false); } payload_length = buffer[offset++]; } else { if (offset + 4 > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Payload Length' dword"); return(false); } payload_length = buffer[offset++]; payload_length *= 0x00000100; payload_length += buffer[offset++]; payload_length *= 0x00000100; payload_length += buffer[offset++]; payload_length *= 0x00000100; payload_length += buffer[offset++]; } /* ID Length */ int id_length = 0; if ((header & NDEF_HEADER_ID_LENGTH_PRESENT) != 0) { if (offset + 1 > buffer.Length) { Trace.WriteLine("NDEF truncated after 'ID Length' byte"); return(false); } id_length = buffer[offset++]; } /* Type */ byte[] type = null; if (type_length > 0) { if (offset + type_length > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Type' bytes"); return(false); } type = new byte[type_length]; for (int i = 0; i < type_length; i++) { type[i] = buffer[offset++]; } } /* ID */ byte[] id = null; if (id_length > 0) { if (offset + id_length > buffer.Length) { Trace.WriteLine("NDEF truncated after 'ID' bytes"); return(false); } id = new byte[id_length]; for (int i = 0; i < id_length; i++) { id[i] = buffer[offset++]; } } /* Payload */ byte[] payload = null; if (payload_length > 0) { if (offset + payload_length > buffer.Length) { Trace.WriteLine("NDEF truncated after 'Payload' bytes"); return(false); } payload = new byte[payload_length]; for (int i = 0; i < payload_length; i++) { payload[i] = buffer[offset++]; } } /* OK */ string type_s = CardBuffer.StringFromBytes(type); switch (header & NDEF_HEADER_TNF_MASK) { case NDEF_HEADER_TNF_EMPTY: break; case NDEF_HEADER_TNF_NFC_RTD_WKN: if (type_s.Equals("Sp")) { Trace.WriteLine("Found a SmartPoster"); ndef = new RtdSmartPoster(payload); } else if (type_s.Equals("U")) { Trace.WriteLine("Found an URI"); ndef = new RtdUri(payload); } else if (type_s.Equals("T")) { Trace.WriteLine("Found a Text"); ndef = new RtdText(payload); } else if (type_s.Equals("act")) { Trace.WriteLine("Found an Action"); ndef = new RtdSmartPosterAction(payload); } else if (type_s.Equals("s")) { Trace.WriteLine("Found a Size"); ndef = new RtdSmartPosterTargetSize(payload); } else if (type_s.Equals("t")) { Trace.WriteLine("Found a MIME-Type"); ndef = new RtdSmartPosterTargetType(payload); } else if (type_s.Equals("Hs")) { Trace.WriteLine("Found a Handover Selector"); ndef = new RtdHandoverSelector(payload, ref buffer, ref offset); } else if (type_s.Equals("ac")) { Trace.WriteLine("Found a Alternative Carrier"); ndef = new RtdAlternativeCarrier(payload); } else { Trace.WriteLine("Found an unknown RTD : " + type_s); } break; case NDEF_HEADER_TNF_MEDIA_TYPE: if (type_s.ToLower().Equals("text/x-vcard")) { Trace.WriteLine("Found a vCard"); ndef = new RtdVCard(payload); } else { Trace.WriteLine("Found a MIME Media : " + type_s); ndef = new RtdMedia(type_s, payload); } break; case NDEF_HEADER_TNF_ABSOLUTE_URI: if (type_s.Equals("U")) { Trace.WriteLine("Found an absolute URI"); ndef = new AbsoluteUri(id, payload); } break; case NDEF_HEADER_TNF_NFC_RTD_EXT: Trace.WriteLine("Found TNF urn:nfc:ext"); break; case NDEF_HEADER_TNF_UNKNOWN: Trace.WriteLine("Found TNF unknown"); break; case NDEF_HEADER_TNF_UNCHANGED: Trace.WriteLine("Found TNF unchanged"); break; case NDEF_HEADER_TNF_RESERVED: Trace.WriteLine("Found TNF reserved"); break; default: return(false); // Invalid } if (ndef == null) { ndef = new Ndef(header, type, id, payload); } if (offset >= buffer.Length) { Trace.WriteLine("Done!"); terminated = true; } return(true); }
public Rtd(Ndef record) : base(record) { }