NFC API for mbed using the MicroNFCBoard as a peripheral

Dependents:   MicroNFCBoardAPI_P2P_Client MicroNFCBoardAPI_Blink MicroNFCBoardAPI_Tag_Emulator MicroNFCBoardAPI_Tag_Reader ... more

Files at this revision

API Documentation at this revision

Comitter:
AppNearMe
Date:
Thu May 14 16:41:27 2015 +0000
Parent:
1:1d246e0872c6
Commit message:
https://github.com/AppNearMe/micronfcboard-mbed-peripheral/commit/ed5d798243e8eb6a959c756dbcdfdad4c6e59979

Changed in this revision

micronfcboard.cpp Show annotated file Show diff for this revision Revisions of this file
micronfcboard.h Show annotated file Show diff for this revision Revisions of this file
transport.cpp Show annotated file Show diff for this revision Revisions of this file
transport.h Show annotated file Show diff for this revision Revisions of this file
diff -r 1d246e0872c6 -r 9b0733b8fa95 micronfcboard.cpp
--- a/micronfcboard.cpp	Fri Apr 24 12:59:31 2015 +0000
+++ b/micronfcboard.cpp	Thu May 14 16:41:27 2015 +0000
@@ -26,9 +26,12 @@
 #define STATUS_NDEF_BUSY        (1 << 5)
 #define STATUS_NDEF_SUCCESS     (1 << 6)
 
-#define STATUS_TYPE_MASK        (0xFF << 8)
-#define STATUS_TYPE2            (2 << 8)
-#define STATUS_P2P              (8 << 8)
+#define STATUS_TYPE_MASK        (0xFFUL << 8)
+#define STATUS_TYPE2            (2UL << 8)
+#define STATUS_TYPE4            (4UL << 8)
+#define STATUS_P2P              (8UL << 8)
+
+#define STATUS_INITIATOR        (1UL << 16)
 
 #define RECORD_URI  1
 #define RECORD_TEXT 2
@@ -64,10 +67,16 @@
   return _status & STATUS_CONNECTED;
 }
 
-bool MicroNFCBoard::type2()
+bool MicroNFCBoard::type2Tag()
 {
   updateStatus();
-  return (_status & STATUS_TYPE_MASK) == STATUS_TYPE2;
+  return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE2) && (_status & STATUS_INITIATOR);
+}
+
+bool MicroNFCBoard::type4Emulator()
+{
+  updateStatus();
+  return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE4) && !(_status & STATUS_INITIATOR);
 }
 
 bool MicroNFCBoard::p2p()
@@ -112,14 +121,14 @@
   return _status & STATUS_NDEF_SUCCESS;
 }
 
-void MicroNFCBoard::startPolling()
+void MicroNFCBoard::startPolling(bool readerWriter, bool emulator, bool p2p)
 {
-  _transport.nfcPoll(true);
+  _transport.nfcPoll(readerWriter, emulator, p2p);
 }
 
 void MicroNFCBoard::stopPolling()
 {
-  _transport.nfcPoll(false);
+  _transport.nfcPoll(false, false, false);
 }
 
 void MicroNFCBoard::ndefRead()
@@ -142,7 +151,6 @@
   size_t recordCount = 0;
   _transport.nfcGetMessageInfo(&recordCount);
 
-
   size_t recordNumber = 0;
   uint16_t info[4];
   uint16_t type;
@@ -260,5 +268,91 @@
   return true;
 }
 
+void MicroNFCBoard::writeNdefUri(const char* uri)
+{
+  _transport.nfcPrepareMessage(true, false);
+
+  size_t uriPrefixLength = strlen(uri);
+  if( uriPrefixLength > 36 )
+  {
+    uriPrefixLength = 36;
+  }
+
+  uint8_t prefix = 0;
+    
+  _transport.nfcEncodePrefix(&prefix, uri, &uriPrefixLength);
+    
+  if( uriPrefixLength > strlen(uri) )
+  {
+      uriPrefixLength = 0;
+  }
+
+  size_t uriLength = strlen(uri) - uriPrefixLength;
+  uri += uriPrefixLength;
+
+  const uint16_t info[] = {prefix, uriLength};
+  _transport.nfcSetRecordInfo(0, RECORD_URI, info, 2);
+
+  _transport.nfcSetMessageInfo(1);
+  _transport.nfcPrepareMessage(false, true);
+
+  size_t off = 0;
+  while(uriLength > 0)
+  {
+    size_t cpyLength = uriLength;
+    if(cpyLength > 32)
+    {
+      cpyLength = 32;
+    }
+    _transport.nfcSetRecordData(0, 0, off, (uint8_t*)uri, cpyLength);
+    uriLength -= cpyLength;
+    off += cpyLength;
+    uri += cpyLength;
+  }
+}
+
+void MicroNFCBoard::writeNdefText(const char* lang, const char* text)
+{
+  _transport.nfcPrepareMessage(true, false);
+
+  size_t langLength = strlen(lang);
+  size_t textLength = strlen(text);
+
+  const uint16_t info[] = {0 /* UTF-8 */, langLength, textLength};
+  _transport.nfcSetRecordInfo(0, RECORD_TEXT, info, 3);
+
+  _transport.nfcSetMessageInfo(1);
+  _transport.nfcPrepareMessage(false, true);
+
+  size_t off = 0;
+  while(langLength > 0)
+  {
+    size_t cpyLength = langLength;
+    if(cpyLength > 32)
+    {
+      cpyLength = 32;
+    }
+    _transport.nfcSetRecordData(0, 0, off, (uint8_t*)lang, cpyLength);
+    langLength -= cpyLength;
+    off += cpyLength;
+    lang += cpyLength;
+  }
+
+  off = 0;
+  while(textLength > 0)
+  {
+    size_t cpyLength = textLength;
+    if(cpyLength > 32)
+    {
+      cpyLength = 32;
+    }
+    _transport.nfcSetRecordData(0, 1, off, (uint8_t*)text, cpyLength);
+    textLength -= cpyLength;
+    off += cpyLength;
+    text += cpyLength;
+  }
+
+}
 
 
+
diff -r 1d246e0872c6 -r 9b0733b8fa95 micronfcboard.h
--- a/micronfcboard.h	Fri Apr 24 12:59:31 2015 +0000
+++ b/micronfcboard.h	Thu May 14 16:41:27 2015 +0000
@@ -15,6 +15,7 @@
 See the License for the specific language governing permissions and
 limitations under the License.
  */
+
 #ifndef SRC_MICRONFCBOARD_H_
 #define SRC_MICRONFCBOARD_H_
 
@@ -36,7 +37,9 @@
 
   bool connected();
 
-  bool type2();
+  bool type2Tag();
+
+  bool type4Emulator();
 
   bool p2p();
 
@@ -52,7 +55,7 @@
 
   bool ndefSuccess();
 
-  void startPolling();
+  void startPolling(bool readerWriter, bool emulator, bool p2p);
 
   void stopPolling();
 
@@ -64,9 +67,9 @@
 
   bool readNdefText(char* text, size_t maxTextLength);
 
-  void writeNdefUri(char* uri);
+  void writeNdefUri(const char* uri);
 
-  void writeNdefText(char* text);
+  void writeNdefText(const char* lang, const char* text);
 
 protected:
   Transport _transport;
diff -r 1d246e0872c6 -r 9b0733b8fa95 transport.cpp
--- a/transport.cpp	Fri Apr 24 12:59:31 2015 +0000
+++ b/transport.cpp	Thu May 14 16:41:27 2015 +0000
@@ -28,8 +28,8 @@
                                            *(((uint8_t*)(addr)) + 1) = ((val) >> 0 ) & 0xFF; } while(0)
 
 //MSB first
-#define READ_UINT32( addr, val ) do{ val = (*(((uint8_t*)(addr)) + 0) << 24 ) \
-                                              | (*(((uint8_t*)(addr)) + 1) << 16 ) \
+#define READ_UINT32( addr, val ) do{ val = (*(uint32_t*)(((uint8_t*)(addr)) + 0) << 24 ) \
+                                              | (*(uint32_t*)(((uint8_t*)(addr)) + 1) << 16 ) \
                                               | (*(((uint8_t*)(addr)) + 2) << 8 ) \
                                               | (*(((uint8_t*)(addr)) + 3) << 0 ); } while(0)
 #define READ_UINT16( addr, val ) do{ val = (*(((uint8_t*)(addr)) + 0) << 8 ) \
@@ -76,9 +76,9 @@
   return status;
 }
 
-void Transport::nfcPoll(bool enable)
+void Transport::nfcPoll(bool readerWriter, bool emulator, bool p2p)
 {
-  uint8_t out[] = {enable?1:0};
+  uint8_t out[] = {(readerWriter?1:0) | (emulator?2:0) | (p2p?4:0)};
   command(Transport::NFC_POLL, out, sizeof(out), NULL, 0);
 }
 
@@ -137,7 +137,7 @@
   }
 }
 
-void Transport::nfcSetRecordInfo(size_t recordNumber, uint16_t type, uint16_t* info, size_t infoCount)
+void Transport::nfcSetRecordInfo(size_t recordNumber, uint16_t type, const uint16_t* info, size_t infoCount)
 {
   uint8_t out[2+2+2*infoCount];
   WRITE_UINT16(&out[0], recordNumber);
@@ -159,7 +159,7 @@
   command(Transport::NFC_GET_RECORD_DATA, out, sizeof(out), data, length);
 }
 
-void Transport::nfcSetRecordData(size_t recordNumber, size_t item, size_t offset, uint8_t* data, size_t length)
+void Transport::nfcSetRecordData(size_t recordNumber, size_t item, size_t offset, const uint8_t* data, size_t length)
 {
   uint8_t out[7+length];
   WRITE_UINT16(&out[0], recordNumber);
@@ -185,7 +185,7 @@
   {
     out[0] = 0;
   }
-  command(Transport::NFC_POLL, out, sizeof(out), NULL, 0);
+  command(Transport::NFC_PREPARE_MESSAGE, out, sizeof(out), NULL, 0);
 }
 
 void Transport::nfcDecodePrefix(uint8_t prefix, char* data, size_t* pDataLength)
@@ -202,17 +202,15 @@
   memcpy(data, &in[2], *pDataLength);
 }
 
-void Transport::nfcEncodePrefix(uint8_t* pPrefix, char* data, size_t dataLength)
+void Transport::nfcEncodePrefix(uint8_t* pPrefix, const char* data, size_t* pDataLength)
 {
-  uint8_t out[2 + dataLength];
-  uint8_t in[1];
-
-  WRITE_UINT16(&out[0], dataLength);
-  memcpy(data, &out[2], dataLength);
-
+  uint8_t out[2 + *pDataLength];
+  uint8_t in[3];
+  WRITE_UINT16(&out[0], *pDataLength);
+  memcpy(&out[2], data, *pDataLength);
   command(Transport::NFC_ENCODE_PREFIX, out, sizeof(out), in, sizeof(in));
-
   *pPrefix = in[0];
+  READ_UINT16(&in[1], *pDataLength);
 }
 
 void Transport::leds(bool led1, bool led2)
diff -r 1d246e0872c6 -r 9b0733b8fa95 transport.h
--- a/transport.h	Fri Apr 24 12:59:31 2015 +0000
+++ b/transport.h	Thu May 14 16:41:27 2015 +0000
@@ -15,6 +15,7 @@
 See the License for the specific language governing permissions and
 limitations under the License.
  */
+
 #ifndef SRC_TRANSPORT_H_
 #define SRC_TRANSPORT_H_
 
@@ -34,7 +35,7 @@
 
   uint32_t status();
 
-  void nfcPoll(bool enable);
+  void nfcPoll(bool readerWriter, bool emulator, bool p2p);
 
   void nfcOperation(bool readOp, bool writeOp);
 
@@ -46,17 +47,17 @@
 
   void nfcGetRecordInfo(size_t recordNumber, uint16_t* pType, uint16_t* info, size_t infoCount);
 
-  void nfcSetRecordInfo(size_t recordNumber, uint16_t type, uint16_t* info, size_t infoCount);
+  void nfcSetRecordInfo(size_t recordNumber, uint16_t type, const uint16_t* info, size_t infoCount);
 
   void nfcGetRecordData(size_t recordNumber, size_t item, size_t offset, uint8_t* data, size_t length);
 
-  void nfcSetRecordData(size_t recordNumber, size_t item, size_t offset, uint8_t* data, size_t length);
+  void nfcSetRecordData(size_t recordNumber, size_t item, size_t offset, const uint8_t* data, size_t length);
 
   void nfcPrepareMessage(bool lock, bool generate);
 
   void nfcDecodePrefix(uint8_t prefix, char* data, size_t* pDataLength);
 
-  void nfcEncodePrefix(uint8_t* pPrefix, char* data, size_t dataLength);
+  void nfcEncodePrefix(uint8_t* pPrefix, const char* data, size_t* pDataLength);
 
   void leds(bool led1, bool led2);