hsanjuan / go-ndef

A Go implementation of the NFC Data Exchange Format (NDEF) specification
GNU Lesser General Public License v3.0
10 stars 5 forks source link

Unable to create message with multiple records #1

Closed taglme closed 5 years ago

taglme commented 5 years ago

It seems that ndef record header (first byte) not correctly encoded when there is several records in message. Order of records in message should be considered when Marshal records. For example, I'm trying to create smart poster message recordUri := ndef.NewURIRecord("http://example.com") recordTitle := ndef.NewTextRecord("hello", "en") payloadMessage:= &ndef.Message{[]*Record{recordUri, recordTitle}} payloadByte, _ := payloadMessage.Marshal() message := ndef.NewMessage(ndef.NFCForumWellKnownType, "Sp", "", payloadByte)

In this example payloadMessage not correctly Marshaled

hsanjuan commented 5 years ago

Hmm I don't have the smart poster spec around, but from the internet I see that:

. Smart Posters are a more complex type of NDEF record, where the payload is actually another NDEF message. The message embedded in the Smart Poster payload contains two NDEF records of its own, a URI and a text record. Since Smart Poster records have multiple records, they can deliver additional information about the URI, such as a title, icon, or suggested processing actions.

I think the problem is that my library does not know how to produce a payload for this type of well known format and defaults to a generic , because I could never access the spec. If you are so kind to explain or provide information on how it should look like I can try to add it. Otherwise, you need to make sure you adapt payloadByte to whatever the smart poster needs, as right now it's used without any modification when Marshaling the final message..

taglme commented 5 years ago

Ok, there is more generic example. If I want to create message with two records recordUrl1 := ndef.NewURIRecord("http://example1.com") recordUrl2 := ndef.NewURIRecord("http://example2.com") payloadMessage:= &Message{[]*Record{recordUrl1, recordUrl2}} payloadByte, _ := payloadMessage.Marshal()

Here we will get two records that starts with the same byte 0xD1 This is because in records marshal MB and ME flags always set to true tempChunk.MB = true tempChunk.ME = true

By specs we should set MB byte for first record and ME byte for last record. When marshal record we should pay attention to their order in message in order to set first byte of record (ndef header flags) properly. So in example first record should start from 0x91 byte and the second record from 0x51 byte

taglme commented 5 years ago

Hi! I make some fixes and create pull request. Could you check it? It seems that message unmarshal need to be fixed too. As soon as there is an error occurred when try to unmarshal message with multiple record Error is "Chunk Flag missing from some records"

taglme commented 5 years ago

By the way, why chunks flags should be set in all records but last (according to chunk check function)? As I know this flag should be set only if we have chunked payload. I think this check should omitted.