From 80ee446f74dc7165d46cf64d73e04dfd0742e650 Mon Sep 17 00:00:00 2001
From: Philippe Teuwen <yobibe@gmail.com>
Date: Mon, 4 Oct 2010 12:36:29 +0000
Subject: [PATCH] pn53x-tamashell now accepts scripts, see examples

---
 .../UltraLightRead.cmd                        | 12 +++++
 .../UltraLightReadWrite.cmd                   | 49 +++++++++++++++++++
 examples/pn53x-tamashell.c                    | 11 +++--
 3 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 examples/pn53x-tamashell-scripts/UltraLightRead.cmd
 create mode 100644 examples/pn53x-tamashell-scripts/UltraLightReadWrite.cmd

diff --git a/examples/pn53x-tamashell-scripts/UltraLightRead.cmd b/examples/pn53x-tamashell-scripts/UltraLightRead.cmd
new file mode 100644
index 0000000..2affe85
--- /dev/null
+++ b/examples/pn53x-tamashell-scripts/UltraLightRead.cmd
@@ -0,0 +1,12 @@
+02;                               // Get firmware version
+
+// Reads content of a Mifare UltraLight
+// PLEASE PUT ULTRALIGHT TAG NOW
+4A  01  00;                       // 1 target requested
+
+// Read memory content from address 4
+40 01 30 00;                      Read 16 bytes from address 0x00
+40 01 30 04;                      Read 16 bytes from address 0x04
+40 01 30 08;                      Read 16 bytes from address 0x08
+40 01 30 0C;                      Read 16 bytes from address 0x0C
+
diff --git a/examples/pn53x-tamashell-scripts/UltraLightReadWrite.cmd b/examples/pn53x-tamashell-scripts/UltraLightReadWrite.cmd
new file mode 100644
index 0000000..f1bbdcb
--- /dev/null
+++ b/examples/pn53x-tamashell-scripts/UltraLightReadWrite.cmd
@@ -0,0 +1,49 @@
+02;                               Get firmware version
+
+// Create NFC-Forum tag type2 with URL
+// WARNING It burns the OTP bits of sector 3!!
+// PLEASE PUT ULTRALIGHT TAG NOW
+4A  01  00;                       1 target requested
+
+// Clear memory from address 0x04
+40 01 A2 04 00 00 00 00;          Write 4 bytes from address 0x04
+40 01 A2 05 00 00 00 00;          Write 4 bytes from address 0x05
+40 01 A2 06 00 00 00 00;          Write 4 bytes from address 0x06
+40 01 A2 07 00 00 00 00;          Write 4 bytes from address 0x07
+40 01 A2 08 00 00 00 00;          Write 4 bytes from address 0x08
+40 01 A2 09 00 00 00 00;          Write 4 bytes from address 0x09
+40 01 A2 0A 00 00 00 00;          Write 4 bytes from address 0x0A
+40 01 A2 0B 00 00 00 00;          Write 4 bytes from address 0x0B
+40 01 A2 0C 00 00 00 00;          Write 4 bytes from address 0x0C
+
+// Read memory content from address 4
+40 01 30 04;                      Read 16 bytes from address 0x04
+40 01 30 08;                      Read 16 bytes from address 0x08
+40 01 30 0C;                      Read 16 bytes from address 0x0C
+
+// cf NFC-Forum Type 1 Tag Operation Specification TS
+// Write @ address 0x03 (OTP):    NDEF, v1.0, 48 bytes, RW
+40 01 A2 03 E1 10 06 00;
+// Write @ address 0x04:          NDEF TLV, 15 bytes,...
+// cf NFC-Forum NFC Data Exchange Format (NDEF) TS
+//                                ...,MB,ME,SR,TNF=1 (wkt), typeL=1 byte
+40 01 A2 04 03 0F D1 01;
+// Write @ address 0x05:          payloadL=11 bytes,...
+// cf NFC-Forum NFC Record Type Definition (RTD) TS
+//                                ...,type=urn:nfc:wkt:U = URI
+// cf NFC-Forum URI Record Type Definition TS
+//                                ...,01=>URI id code=http://www.
+//                                ...,"l"
+40 01 A2 05 0B 55 01 6C;
+// Write @ address 0x06:          "ibnf"
+40 01 A2 06 69 62 6E 66;
+// Write @ address 0x07:          "c.or"
+40 01 A2 07 63 2E 6F 72;
+// Write @ address 0x08:          "g",TLV:FE
+40 01 A2 08 67 FE 00 00;
+
+// Read memory content from address 4
+40 01 30 04;                      Read 16 bytes from address 0x04
+40 01 30 08;                      Read 16 bytes from address 0x08
+40 01 30 0C;                      Read 16 bytes from address 0x0C
+
diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c
index f2d08a7..c7572d7 100644
--- a/examples/pn53x-tamashell.c
+++ b/examples/pn53x-tamashell.c
@@ -78,15 +78,20 @@ int main(int argc, const char* argv[])
     }
     add_history(cmd);
 #else
-    cmd = malloc(1024);
+    cmd = NULL;
     printf("%s", prompt);
     fflush(0);
-    int s = read(0, cmd, 1024);
-    if (s == 0) {
+    size_t n;
+    extern FILE* stdin;
+    //FIXME: getline not in stdio.h ???
+    int s = getline(&cmd, &n, stdin);
+    if (s <= 0) {
       printf("Bye!\n");
       free(cmd);
       break;
     }
+    //FIXME: print only if read from redirected stdin (i.e. script)
+    printf("%s", cmd);
 #endif //HAVE_READLINE
     if (cmd[0]=='q') {
       printf("Bye!\n");