12 #define NCI_BRCM_CO_ID 0x2E
16 #define NCI_MSG_HDR_SIZE 3
17 #define NCI_DATA_HDR_SIZE 3
18 #define NCI_MAX_PAYLOAD_SIZE 0xFE
19 #define NCI_MAX_CTRL_SIZE 0xFF
20 #define NCI_CTRL_INIT_SIZE 32
21 #define NCI_MAX_VSC_SIZE 0xFF
22 #define NCI_VSC_MSG_HDR_SIZE \
26 #define NCI_ISO_DEP_MAX_INFO \
29 #define NCI_NFC_DEP_MAX_DATA \
38 #define NCI_MT_MASK 0xE0
39 #define NCI_MT_SHIFT 5
40 #define NCI_MT_DATA 0x00
46 #define NCI_MTS_CMD 0x20
47 #define NCI_MTS_RSP 0x40
48 #define NCI_MTS_NTF 0x60
49 #define NCI_MTS_CFG 0x80
51 #define NCI_NTF_BIT 0x80
52 #define NCI_RSP_BIT 0x40
56 #define NCI_LS_DATA 0x00
57 #define NCI_LS_DATA_PBF 0x01
60 #define NCI_PBF_MASK 0x10
61 #define NCI_PBF_SHIFT 4
62 #define NCI_PBF_NO_OR_LAST 0x00
63 #define NCI_PBF_ST_CONT 0x10
66 #define NCI_GID_MASK 0x0F
67 #define NCI_GID_SHIFT 0
68 #define NCI_GID_CORE 0x00
69 #define NCI_GID_RF_MANAGE 0x01
70 #define NCI_GID_EE_MANAGE 0x02
71 #define NCI_GID_PROP 0x0F
75 #define NCI_OID_MASK 0x3F
76 #define NCI_OID_SHIFT 0
81 #define NCI_TEST_ID 0xFE
84 #define NCI_DEST_TYPE_NFCC 1
85 #define NCI_DEST_TYPE_REMOTE 2
86 #define NCI_DEST_TYPE_NFCEE 3
89 #define NCI_MSG_BLD_HDR0(p, mt, gid) *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | (gid));
91 #define NCI_MSG_PBLD_HDR0(p, mt, pbf, gid) \
92 *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | ((pbf) << NCI_PBF_SHIFT) | (gid));
95 #define NCI_MSG_BLD_HDR1(p, oid) *(p)++ = (uint8_t)(((oid) << NCI_OID_SHIFT));
98 #define NCI_MSG_PRS_HDR0(p, mt, pbf, gid) \
99 mt = (*(p) &NCI_MT_MASK) >> NCI_MT_SHIFT; \
100 pbf = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT; \
101 gid = *(p)++ & NCI_GID_MASK;
104 #define NCI_MSG_PRS_MT_PBF(p, mt, pbf) \
105 mt = (*(p) &NCI_MT_MASK) >> NCI_MT_SHIFT; \
106 pbf = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT;
109 #define NCI_MSG_PRS_HDR1(p, oid) \
110 oid = (*(p) &NCI_OID_MASK); \
118 #define NCI_CID_MASK 0x0F
121 #define NCI_DATA_BLD_HDR(p, cid, len) \
122 *(p)++ = (uint8_t)(cid); \
124 *(p)++ = (uint8_t)(len);
126 #define NCI_DATA_PBLD_HDR(p, pbf, cid, len) \
127 *(p)++ = (uint8_t)(((pbf) << NCI_PBF_SHIFT) | (cid)); \
131 #define NCI_DATA_PRS_HDR(p, pbf, cid, len) \
132 (pbf) = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT; \
133 (cid) = (*(p) &NCI_CID_MASK); \
140 #define BE_STREAM_TO_uint32_t(u32, p) \
142 u32 = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) + \
143 ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
147 #include <netinet/tcp.h>
148 #include <sys/socket.h>
149 #include <netinet/in.h>
150 #include <sys/select.h>
151 #include <sys/socket.h>
152 #include <sys/errno.h>
153 #include <arpa/inet.h>
183 #define NFA_TNF_EMPTY NDEF_TNF_EMPTY
184 #define NFA_TNF_WKT NDEF_TNF_WKT
185 #define NFA_TNF_RFC2046_MEDIA NDEF_TNF_MEDIA
186 #define NFA_TNF_RFC3986_URI NDEF_TNF_URI
187 #define NFA_TNF_EXTERNAL NDEF_TNF_EXT
188 #define NFA_TNF_UNKNOWN case NDEF_TNF_UNKNOWN
189 #define NFA_TNF_UNCHANGED NDEF_TNF_UNCHANGED
190 #define NFA_TNF_RESERVED NDEF_TNF_RESERVED
191 #define NFA_TNF_DEFAULT case 0xFF
194 #define NDEF_MB_MASK 0x80
195 #define NDEF_ME_MASK 0x40
196 #define NDEF_CF_MASK 0x20
197 #define NDEF_SR_MASK 0x10
198 #define NDEF_IL_MASK 0x08
199 #define NDEF_TNF_MASK 0x07
202 #define NDEF_TNF_EMPTY 0
203 #define NDEF_TNF_WKT 1
204 #define NDEF_TNF_MEDIA 2
205 #define NDEF_TNF_URI 3
206 #define NDEF_TNF_EXT 4
207 #define NDEF_TNF_UNKNOWN 5
208 #define NDEF_TNF_UNCHANGED 6
209 #define NDEF_TNF_RESERVED 7
216 printf(
" NDEF_OK==> 0 - OK \n");
219 printf(
" NDEF_REC_NOT_FOUND ==> 1 - No record matching the find criteria \n");
222 printf(
" NDEF_MSG_TOO_SHORT ==> 2 - Message was too short (< 3 bytes) \n");
225 printf(
" NDEF_MSG_NO_MSG_BEGIN ==> - No 'begin' flag at start of message \n");
228 printf(
" NDEF_MSG_NO_MSG_END ==> - No 'end' flag at end of message \n");
231 printf(
" NDEF_MSG_EXTRA_MSG_BEGIN ==> 5 - 'begin' flag after start of message \n");
234 printf(
" NDEF_MSG_UNEXPECTED_CHUNK ==> 6 - Unexpected chunk found \n");
237 printf(
" NDEF_MSG_INVALID_EMPTY_REC ==> 7 - Empty record with non-zero contents \n");
240 printf(
" NDEF_MSG_INVALID_CHUNK ==> 8 - Invalid chunk found \n");
243 printf(
" NDEF_MSG_LENGTH_MISMATCH ==> 9 - Overall message length doesn't match \n");
246 printf(
" NDEF_MSG_INSUFFICIENT_MEM ==> 10 - Insuffiecient memory to add record \n");
254 uint8_t* p_rec = p_msg;
255 uint8_t* p_end = p_msg + msg_len;
256 uint8_t rec_hdr = 0, type_len, id_len;
258 uint32_t payload_len;
261 if ((p_msg == NULL) || (msg_len < 3))
272 for (count = 0; p_rec < p_end; count++)
275 if (p_rec + 3 > p_end)
279 printf(
" A-> %x", rec_hdr);
282 if ((count > 0) && (rec_hdr & NDEF_MB_MASK))
287 printf(
" type_len-> %d", type_len);
292 payload_len = *p_rec++;
293 printf(
" payload_len-> %d", payload_len);
298 if (p_rec + 4 > p_end)
308 if (p_rec + 1 > p_end)
312 printf(
" id_len-> %d", id_len);
320 printf(
" E-> %x", rec_hdr);
327 if ((type_len != 0) || (id_len != 0) ||
345 if ((type_len != 0) || (id_len != 0) ||
362 printf(
"An empty record must NOT have a type, ID or payload \n");
363 if ((type_len != 0) || (id_len != 0) || (payload_len != 0))
369 printf(
" F-> %x", rec_hdr);
375 p_rec += (payload_len + type_len + id_len);
379 printf(
"p_rec=%p", p_rec);
399 printf(
" p_rec-> %p", p_rec);
400 printf(
" p_end-> %p", p_end);
410 for (count = 0; count < 35; count++)
411 printf(
"%x", p_rec[count]);
412 printf(
" <- %s \n", repere);
419 for (count = 0; count < len - 1; count++)
420 printf(
"0x%0x,", p_rec[count]);
422 printf(
"0x%0x", p_rec[len - 1]);
423 printf(
"}\n count %d\n", count + 1);
437 uint8_t rec_hdr, type_len;
456 *p_type_len = type_len;
476 uint8_t rec_hdr, type_len;
492 *p_id_len = *p_rec++;
500 return (p_rec + type_len);
514 uint8_t rec_hdr, type_len, id_len;
515 uint32_t payload_len;
525 payload_len = *p_rec++;
529 *p_payload_len = payload_len;
538 if (payload_len == 0)
541 return (p_rec + type_len + id_len);
560 uint8_t rec_hdr = 0x00;
562 printf(
"createBufNdef_TypeURI \n");
573 strOUT[2] = sizLen + offset;
576 printf(
"createBufNdef_TypeURI hdr=%x08\n", *strOUT);
579 strncat((
char*) strOUT, (
char*) strIN, sizLen);
582 printf(
" Check tnf=%d & prefix%x", (
short) strOUT[0] & 0x07, strOUT[4]);
592 uint8_t p_new_hdr[sizehdr];
594 bzero(p_new_hdr,
sizeof(p_new_hdr));
608 strOUT[2] = sizLen + offset;
624 strncat((
char*) strOUT, (
char*) strIN, sizLen);
626 printf(
" tnf=%d", (
short) p_new_hdr[0] & 0x07);
635 uint8_t p_new_hdr[sizehdr];
636 bzero(p_new_hdr,
sizeof(p_new_hdr));
651 strOUT[2] = sizLen + offset + 10;
670 strncat((
char*) strOUT, (
char*) strIN, sizLen);
672 strOUT[sizehdr + offset + sizLen + 0] = 0x51;
673 strOUT[sizehdr + offset + sizLen + 1] = 0x01;
674 strOUT[sizehdr + offset + sizLen + 2] = 0x07;
675 strOUT[sizehdr + offset + sizLen + 3] = 0x54;
676 strOUT[sizehdr + offset + sizLen + 4] = 0x02;
677 strOUT[sizehdr + offset + sizLen + 5] = 0x66;
678 strOUT[sizehdr + offset + sizLen + 6] = 0x72;
679 strOUT[sizehdr + offset + sizLen + 7] = 0x42;
680 strOUT[sizehdr + offset + sizLen + 8] = 0x6C;
681 strOUT[sizehdr + offset + sizLen + 9] = 0x6F;
682 strOUT[sizehdr + offset + sizLen + 10] = 0x67;
684 printf(
" tnf=%d", (
short) p_new_hdr[0] & 0x07);
692 #define INVALID_SOCKET -1
702 printf(
"Can’t create socket...\n");
707 memset(&sin, 0,
sizeof(sin));
708 inet_aton(
"192.168.122.129", &sin.sin_addr);
711 sin.sin_port = htons(port);
712 sin.sin_family = AF_INET;
714 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes,
sizeof(
int));
718 printf(
"Can't connect socket %d to %s:%d, invalid socket...\n", sock, ip, port);
722 printf(
" Socket %d connected to port %s:%d \n", sock, ip, port);
728 return (send(sock, data, len, 0));
734 int flag_connect = 0;
739 while (!flag_connect)
744 printf(
"Cannot connect to aicNfcd (TCP %d) - VM down or access denied\n", _PORT);
750 printf(
"Connected to aicNfcd (TCP %d)\n", _PORT);
788 for (ii = 0; ii <= sizLen + offset; ii++)
789 strOUT[ii + offset] = strIN[ii];
803 #define NCI_MSG_RF_INTF_ACTIVATED 5
808 printf(
"\n--------------------------\n");
809 for (ii = 0; ii < sizLen; ii++)
810 printf(
"0x%0x-", strOUT[ii]);
812 printf(
"\n--------------------------\n");
826 for (ii = 0; ii <= sizLen + offset; ii++)
827 strOUT[ii + offset] = strIN[ii];
845 static int wait_for_client(
int server)
849 if (listen(server, 1) < 0)
851 printf(
"Unable to listen to socket, errno=%d\n", errno);
855 printf(
"wait_for_client\n");
856 client = accept(server, NULL, 0);
860 printf(
"Unable to accept socket for main conection, errno=%d\n", errno);
868 int main(
int argc,
char* argv[])
907 printf(
" args[0]:Type(0:Uri 1:Text 2:SmartPoster) ; [1]:Lang(0:En-US 1:Fr) ; "
908 "[2]:payload(blahblah)\n");
910 unsigned char* argbuff = (
unsigned char*) malloc(
sizeof(
unsigned char) * 1024);
911 int Type = atoi(argv[1]);
912 int Lang = atoi(argv[2]);
913 int sendORacti = atoi(argv[3]);
915 int argLen = strlen(argv[4]);
916 int offsetPrefix = 1;
922 uint8_t msg1[6] =
"303";
924 strncpy((
char*) argbuff, argv[4], 1024);
927 printf(
"argLen=%d argBuff=%s\n", argLen, argbuff);
934 printf(
"case 0 ; Type %d Lang %d\n", Type, Lang);
935 sizehdr += offsetPrefix;
936 msg_len = (argLen + sizehdr);
937 msg = (uint8_t*) calloc(msg_len,
sizeof(uint8_t));
943 printf(
"case 1 ; Type %d Lang %d\n", Type, Lang);
947 sizehdr += offsetPrefix;
948 printf(
"Type Text Lang:En-US Sizehdr=%d \n", sizehdr);
949 msg_len = (argLen + sizehdr);
950 msg = (uint8_t*) calloc(msg_len,
sizeof(uint8_t));
956 sizehdr += offsetPrefix;
957 printf(
"Type Text Lang:FR Sizehdr=%d \n", sizehdr);
958 msg_len = (argLen + sizehdr);
959 msg = (uint8_t*) calloc(msg_len,
sizeof(uint8_t));
971 printf(
"case 2 ; Type %d Lang %d\n", Type, Lang);
972 sizehdr += offsetPrefix;
973 msg_len = (argLen + sizehdr) + 11;
974 msg = (uint8_t*) calloc(msg_len,
sizeof(uint8_t));
978 printf(
"case 3 ; Type %d Lang %d\n", Type, Lang);
980 msg_len = (argLen + sizehdr);
981 msg = (uint8_t*) calloc(msg_len,
sizeof(uint8_t));
986 printf(
"default ; Type %d Lang %d\n", Type, Lang);
1000 printf(
"\nResult : \n Status Validate %d \n", status);
1002 printf(
"\nDone !\n");
1004 int size, player_fd = -1;
1008 uint32_t p_payload_len;
1016 printf(
"p_tnf=%d ; p_type_len=%d ; p_id_len =%d; p_payload_len=%d\n", p_tnf, p_type_len,
1017 p_id_len, p_payload_len);
1020 int len2send = strlen((
char*) msg) + 3;
1021 snprintf((
char*) c,
sizeof(c),
"%d", len2send);
1023 strncat((
char*) msg1, (
char*) c,
sizeof(msg1) - strlen((
char*) msg1) - 1);
1046 if (sendORacti == 0)
1049 recv(player_fd, &cmd,
sizeof(cmd), MSG_WAITALL);
1050 printf(
"\n received %d \n", cmd);
1053 printf(
"Unknown cmd : %d, exit\n", cmd);
1065 recv(player_fd, &cmd,
sizeof(cmd), MSG_DONTWAIT);
1066 printf(
"\n received %d \n", cmd);
1069 printf(
"NFC is off cmd : %d, exit\n", cmd);
1079 printf(
"\n writting msg1 %d %lu!\n", size, strlen((
char*) msg1));
1081 msg2 = (uint8_t*) calloc(strlen((
char*) msg),
sizeof(uint8_t));
1083 if (sendORacti == 0)
1088 printf(
"\n writting sendata %d %d %lu!\n", size, len2send, strlen((
char*) msg));
1091 else if (sendORacti == 2)
1096 printf(
"\n writting sendata %d %d %lu!\n", size, len2send, strlen((
char*) msg));
1104 printf(
"\n writting msg2 activate %d %d %lu!\n", size, len2send, strlen((
char*) msg2));
int main(int argc, char *argv[])
void createBufNdef_TypeText(uint8_t *strIN, int sizLen, uint8_t *strOUT)
void createBufNdef_TypeSmartPoster(uint8_t *strIN, int sizLen, uint8_t *strOUT)
uint8_t * NDEF_RecGetType(uint8_t *p_rec, uint8_t *p_tnf, uint8_t *p_type_len)
tNDEF_STATUS NDEF_MsgValidate(uint8_t *p_msg, uint32_t msg_len, BOOLEAN b_allow_chunks)
int tcp_write_buff(int sock, unsigned char *data, int len)
uint8_t * NDEF_RecGetPayload(uint8_t *p_rec, uint32_t *p_payload_len)
void printbuff(uint8_t *p_rec, int len)
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
#define NCI_GID_RF_MANAGE
SOCKET open_socket(char *ip, short port)
#define NCI_MSG_BLD_HDR0(p, mt, gid)
#define BE_STREAM_TO_uint32_t(u32, p)
void vshort_actidata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
void printbuff0(uint8_t *p_rec, uint8_t *repere)
void createBufNdef_TypeURI(uint8_t *strIN, int sizLen, uint8_t *strOUT)
#define NCI_MSG_RF_INTF_ACTIVATED
#define NDEF_TNF_UNCHANGED
uint8_t * NDEF_RecGetId(uint8_t *p_rec, uint8_t *p_id_len)
#define NCI_MSG_BLD_HDR1(p, oid)
Define common buffer sizes.
struct sockaddr_in SOCKADDR_IN
void printError(tNDEF_STATUS status)
#define NCI_DATA_BLD_HDR(p, cid, len)