AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
testValidateNFC.c
Go to the documentation of this file.
1 #include "buffer_sizes.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 
8 #define BOOLEAN int
9 #define TRUE 1
10 #define FALSE 0
11 
12 #define NCI_BRCM_CO_ID 0x2E
13 
14 /* Define the message header size for all NCI Commands and Notifications.
15 */
16 #define NCI_MSG_HDR_SIZE 3 /* per NCI spec */
17 #define NCI_DATA_HDR_SIZE 3 /* per NCI spec */
18 #define NCI_MAX_PAYLOAD_SIZE 0xFE
19 #define NCI_MAX_CTRL_SIZE 0xFF /* max control message size */
20 #define NCI_CTRL_INIT_SIZE 32 /* initial NFCC control payload size */
21 #define NCI_MAX_VSC_SIZE 0xFF
22 #define NCI_VSC_MSG_HDR_SIZE \
23  12 /* NCI header (3) + callback function pointer(8; use 8 to be safe) + HCIT (1 byte) */
24 #define NCI_TL_SIZE 2
25 
26 #define NCI_ISO_DEP_MAX_INFO \
27  253 /* Max frame size (256) - Prologue (1) - Epilogue (2) in ISO-DEP, CID and NAD are not \
28  used*/
29 #define NCI_NFC_DEP_MAX_DATA \
30  251 /* Max payload (254) - Protocol Header (3) in NFC-DEP, DID and NAD are not used */
31 
32 /* NCI Command and Notification Format:
33  * 3 byte message header:
34  * byte 0: MT PBF GID
35  * byte 1: OID
36  * byte 2: Message Length */
37 /* MT: Message Type (byte 0) */
38 #define NCI_MT_MASK 0xE0
39 #define NCI_MT_SHIFT 5
40 #define NCI_MT_DATA 0x00
41 #define NCI_MT_CMD 1 /* (NCI_MT_CMD << NCI_MT_SHIFT) = 0x20 */
42 #define NCI_MT_RSP 2 /* (NCI_MT_RSP << NCI_MT_SHIFT) = 0x40 */
43 #define NCI_MT_NTF 3 /* (NCI_MT_NTF << NCI_MT_SHIFT) = 0x60 */
44 #define NCI_MT_CFG 4 /* (NCI_MT_CFG << NCI_MT_SHIFT) = 0x80 */
45 
46 #define NCI_MTS_CMD 0x20
47 #define NCI_MTS_RSP 0x40
48 #define NCI_MTS_NTF 0x60
49 #define NCI_MTS_CFG 0x80
50 
51 #define NCI_NTF_BIT 0x80 /* the tNFC_VS_EVT is a notification */
52 #define NCI_RSP_BIT 0x40 /* the tNFC_VS_EVT is a response */
53 
54 /* for internal use only; not from specification */
55 /* the following 2 flags are used in layer_specific for fragmentation/reassembly of data packets */
56 #define NCI_LS_DATA 0x00
57 #define NCI_LS_DATA_PBF 0x01
58 
59 /* PBF: Packet Boundary Flag (byte 0) */
60 #define NCI_PBF_MASK 0x10
61 #define NCI_PBF_SHIFT 4
62 #define NCI_PBF_NO_OR_LAST 0x00 /* not fragmented or last fragment */
63 #define NCI_PBF_ST_CONT 0x10 /* start or continuing fragment */
64 
65 /* GID: Group Identifier (byte 0) */
66 #define NCI_GID_MASK 0x0F
67 #define NCI_GID_SHIFT 0
68 #define NCI_GID_CORE 0x00 /* 0000b NCI Core group */
69 #define NCI_GID_RF_MANAGE 0x01 /* 0001b RF Management group */
70 #define NCI_GID_EE_MANAGE 0x02 /* 0010b NFCEE Management group */
71 #define NCI_GID_PROP 0x0F /* 1111b Proprietary */
72 /* 0111b - 1110b RFU */
73 
74 /* OID: Opcode Identifier (byte 1) */
75 #define NCI_OID_MASK 0x3F
76 #define NCI_OID_SHIFT 0
77 
78 /* For routing */
79 #define NCI_DH_ID 0 /* for DH */
80 /* To identify the loopback test */
81 #define NCI_TEST_ID 0xFE /* for loopback test */
82 
83 /* Destination Type */
84 #define NCI_DEST_TYPE_NFCC 1 /* NFCC - loopback */
85 #define NCI_DEST_TYPE_REMOTE 2 /* Remote NFC Endpoint */
86 #define NCI_DEST_TYPE_NFCEE 3 /* NFCEE */
87 
88 /* builds byte0 of NCI Command and Notification packet */
89 #define NCI_MSG_BLD_HDR0(p, mt, gid) *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | (gid));
90 
91 #define NCI_MSG_PBLD_HDR0(p, mt, pbf, gid) \
92  *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | ((pbf) << NCI_PBF_SHIFT) | (gid));
93 
94 /* builds byte1 of NCI Command and Notification packet */
95 #define NCI_MSG_BLD_HDR1(p, oid) *(p)++ = (uint8_t)(((oid) << NCI_OID_SHIFT));
96 
97 /* parse byte0 of NCI packet */
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;
102 
103 /* parse MT and PBF bits of NCI packet */
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;
107 
108 /* parse byte1 of NCI Cmd/Ntf */
109 #define NCI_MSG_PRS_HDR1(p, oid) \
110  oid = (*(p) &NCI_OID_MASK); \
111  (p)++;
112 
113 /* NCI Data Format:
114  * byte 0: MT(0) PBF CID
115  * byte 1: RFU
116  * byte 2: Data Length */
117 /* CID: Connection Identifier (byte 0) 1-0xF Dynamically assigned (by NFCC), 0 is predefined */
118 #define NCI_CID_MASK 0x0F
119 
120 /* builds 3-byte message header of NCI Data packet */
121 #define NCI_DATA_BLD_HDR(p, cid, len) \
122  *(p)++ = (uint8_t)(cid); \
123  *(p)++ = 0; \
124  *(p)++ = (uint8_t)(len);
125 
126 #define NCI_DATA_PBLD_HDR(p, pbf, cid, len) \
127  *(p)++ = (uint8_t)(((pbf) << NCI_PBF_SHIFT) | (cid)); \
128  *(p)++ = 0; \
129  *(p)++ = (len);
130 
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); \
134  p++; \
135  p++; \
136  (len) = *(p)++;
137 
138 /* Logical target ID 0x01-0xFE */
139 
140 #define BE_STREAM_TO_uint32_t(u32, p) \
141  { \
142  u32 = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) + \
143  ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
144  (p) += 4; \
145  }
146 
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>
154 
155 typedef struct
156 {
157  uint16_t event;
158  uint16_t len;
159  uint16_t offset;
160  uint16_t layer_specific;
161 } BT_HDR;
162 
163 /* Define the status code returned from the Validate, Parse or Build functions
164 */
165 enum
166 {
167  NDEF_OK, /* 0 - OK */
168 
169  NDEF_REC_NOT_FOUND, /* 1 - No record matching the find criteria */
170  NDEF_MSG_TOO_SHORT, /* 2 - Message was too short (< 3 bytes) */
171  NDEF_MSG_NO_MSG_BEGIN, /* 3 - No 'begin' flag at start of message */
172  NDEF_MSG_NO_MSG_END, /* 4 - No 'end' flag at end of message */
173  NDEF_MSG_EXTRA_MSG_BEGIN, /* 5 - 'begin' flag after start of message */
174  NDEF_MSG_UNEXPECTED_CHUNK, /* 6 - Unexpected chunk found */
175  NDEF_MSG_INVALID_EMPTY_REC, /* 7 - Empty record with non-zero contents */
176  NDEF_MSG_INVALID_CHUNK, /* 8 - Invalid chunk found */
177  NDEF_MSG_LENGTH_MISMATCH, /* 9 - Overall message length doesn't match */
178  NDEF_MSG_INSUFFICIENT_MEM /* 10 - Insuffiecient memory to add record */
179 };
180 typedef uint8_t tNDEF_STATUS;
181 
182 /* Definitions for tNFA_TNF (NDEF type name format ID) */
183 #define NFA_TNF_EMPTY NDEF_TNF_EMPTY /* Empty or no type specified */
184 #define NFA_TNF_WKT NDEF_TNF_WKT /* NFC Forum well-known type [NFC RTD] */
185 #define NFA_TNF_RFC2046_MEDIA NDEF_TNF_MEDIA /* Media-type as defined in RFC 2046 [RFC 2046] */
186 #define NFA_TNF_RFC3986_URI NDEF_TNF_URI /* Absolute URI as defined in RFC 3986 [RFC 3986] */
187 #define NFA_TNF_EXTERNAL NDEF_TNF_EXT /* NFC Forum external type [NFC RTD] */
188 #define NFA_TNF_UNKNOWN case NDEF_TNF_UNKNOWN /* Unknown */
189 #define NFA_TNF_UNCHANGED NDEF_TNF_UNCHANGED /* Unchanged */
190 #define NFA_TNF_RESERVED NDEF_TNF_RESERVED /* Reserved */
191 #define NFA_TNF_DEFAULT case 0xFF /* Used to register default NDEF type handler */
192 typedef uint8_t tNFA_TNF;
193 
194 #define NDEF_MB_MASK 0x80 /* Message Begin */
195 #define NDEF_ME_MASK 0x40 /* Message End */
196 #define NDEF_CF_MASK 0x20 /* Chunk Flag */
197 #define NDEF_SR_MASK 0x10 /* Short Record */
198 #define NDEF_IL_MASK 0x08 /* ID Length */
199 #define NDEF_TNF_MASK 0x07 /* Type Name Format */
200 
201 /* NDEF Type Name Format */
202 #define NDEF_TNF_EMPTY 0 /* Empty (type/id/payload len =0) */
203 #define NDEF_TNF_WKT 1 /* NFC Forum well-known type/RTD */
204 #define NDEF_TNF_MEDIA 2 /* Media-type as defined in RFC 2046 */
205 #define NDEF_TNF_URI 3 /* Absolute URI as defined in RFC 3986 */
206 #define NDEF_TNF_EXT 4 /* NFC Forum external type/RTD */
207 #define NDEF_TNF_UNKNOWN 5 /* Unknown (type len =0) */
208 #define NDEF_TNF_UNCHANGED 6 /* Unchanged (type len =0) */
209 #define NDEF_TNF_RESERVED 7 /* Reserved */
210 
212 {
213  switch (status)
214  {
215  case NDEF_OK:
216  printf(" NDEF_OK==> 0 - OK \n");
217  break;
218  case NDEF_REC_NOT_FOUND:
219  printf(" NDEF_REC_NOT_FOUND ==> 1 - No record matching the find criteria \n");
220  break;
221  case NDEF_MSG_TOO_SHORT:
222  printf(" NDEF_MSG_TOO_SHORT ==> 2 - Message was too short (< 3 bytes) \n");
223  break;
225  printf(" NDEF_MSG_NO_MSG_BEGIN ==> - No 'begin' flag at start of message \n");
226  break;
227  case NDEF_MSG_NO_MSG_END:
228  printf(" NDEF_MSG_NO_MSG_END ==> - No 'end' flag at end of message \n");
229  break;
231  printf(" NDEF_MSG_EXTRA_MSG_BEGIN ==> 5 - 'begin' flag after start of message \n");
232  break;
234  printf(" NDEF_MSG_UNEXPECTED_CHUNK ==> 6 - Unexpected chunk found \n");
235  break;
237  printf(" NDEF_MSG_INVALID_EMPTY_REC ==> 7 - Empty record with non-zero contents \n");
238  break;
240  printf(" NDEF_MSG_INVALID_CHUNK ==> 8 - Invalid chunk found \n");
241  break;
243  printf(" NDEF_MSG_LENGTH_MISMATCH ==> 9 - Overall message length doesn't match \n");
244  break;
246  printf(" NDEF_MSG_INSUFFICIENT_MEM ==> 10 - Insuffiecient memory to add record \n");
247  break;
248  }
249  return;
250 }
251 
252 tNDEF_STATUS NDEF_MsgValidate(uint8_t* p_msg, uint32_t msg_len, BOOLEAN b_allow_chunks)
253 {
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;
257  int count;
258  uint32_t payload_len;
259  BOOLEAN bInChunk = FALSE;
260 
261  if ((p_msg == NULL) || (msg_len < 3))
262  return (NDEF_MSG_TOO_SHORT);
263 
264  /* The first record must have the MB bit set */
265  if ((*p_msg & NDEF_MB_MASK) == 0)
266  return (NDEF_MSG_NO_MSG_BEGIN);
267 
268  /* The first record cannot be a chunk */
269  if ((*p_msg & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
270  return (NDEF_MSG_UNEXPECTED_CHUNK);
271 
272  for (count = 0; p_rec < p_end; count++)
273  {
274  /* if less than short record header */
275  if (p_rec + 3 > p_end)
276  return (NDEF_MSG_TOO_SHORT);
277 
278  rec_hdr = *p_rec++;
279  printf(" A-> %x", rec_hdr);
280 
281  /* The second and all subsequent records must NOT have the MB bit set */
282  if ((count > 0) && (rec_hdr & NDEF_MB_MASK))
283  return (NDEF_MSG_EXTRA_MSG_BEGIN);
284 
285  /* Type field length */
286  type_len = *p_rec++;
287  printf(" type_len-> %d", type_len);
288 
289  /* Payload length - can be 1 or 4 bytes */
290  if (rec_hdr & NDEF_SR_MASK)
291  {
292  payload_len = *p_rec++;
293  printf(" payload_len-> %d", payload_len);
294  }
295  else
296  {
297  /* if less than 4 bytes payload length */
298  if (p_rec + 4 > p_end)
299  return (NDEF_MSG_TOO_SHORT);
300 
301  BE_STREAM_TO_uint32_t(payload_len, p_rec);
302  }
303 
304  /* ID field Length */
305  if (rec_hdr & NDEF_IL_MASK)
306  {
307  /* if less than 1 byte ID field length */
308  if (p_rec + 1 > p_end)
309  return (NDEF_MSG_TOO_SHORT);
310 
311  id_len = *p_rec++;
312  printf(" id_len-> %d", id_len);
313  }
314  else
315  id_len = 0;
316 
317  /* A chunk must have type "unchanged", and no type or ID fields */
318  if (rec_hdr & NDEF_CF_MASK)
319  {
320  printf(" E-> %x", rec_hdr);
321  if (!b_allow_chunks)
322  return (NDEF_MSG_UNEXPECTED_CHUNK);
323 
324  /* Inside a chunk, the type must be unchanged and no type or ID field i sallowed */
325  if (bInChunk)
326  {
327  if ((type_len != 0) || (id_len != 0) ||
328  ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED))
329  return (NDEF_MSG_INVALID_CHUNK);
330  }
331  else
332  {
333  /* First record of a chunk must NOT have type "unchanged" */
334  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
335  return (NDEF_MSG_INVALID_CHUNK);
336 
337  bInChunk = TRUE;
338  }
339  }
340  else
341  {
342  /* This may be the last guy in a chunk. */
343  if (bInChunk)
344  {
345  if ((type_len != 0) || (id_len != 0) ||
346  ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED))
347  return (NDEF_MSG_INVALID_CHUNK);
348 
349  bInChunk = FALSE;
350  }
351  else
352  {
353  /* If not in a chunk, the record must NOT have type "unchanged" */
354  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
355  return (NDEF_MSG_INVALID_CHUNK);
356  }
357  }
358 
359  /* An empty record must NOT have a type, ID or payload */
360  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EMPTY)
361  {
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))
365  }
366 
367  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNKNOWN)
368  {
369  printf(" F-> %x", rec_hdr);
370  if (type_len != 0)
371  return (NDEF_MSG_LENGTH_MISMATCH);
372  }
373 
374  /* Point to next record */
375  p_rec += (payload_len + type_len + id_len);
376 
377  if (rec_hdr & NDEF_ME_MASK)
378  {
379  printf("p_rec=%p", p_rec);
380  break;
381  }
382  else
383  {
384  printf("p_rec");
385  }
386 
387  rec_hdr = 0;
388  }
389 
390  /* The last record should have the ME bit set */
391  if ((rec_hdr & NDEF_ME_MASK) == 0)
392  {
393  return (NDEF_MSG_NO_MSG_END);
394  }
395 
396  /* p_rec should equal p_end if all the length fields were correct */
397  if (p_rec != p_end)
398  {
399  printf(" p_rec-> %p", p_rec);
400  printf(" p_end-> %p", p_end);
401  return (NDEF_MSG_LENGTH_MISMATCH);
402  }
403 
404  return (NDEF_OK);
405 }
406 
407 void printbuff0(uint8_t* p_rec, uint8_t* repere)
408 {
409  uint32_t count;
410  for (count = 0; count < 35; count++)
411  printf("%x", p_rec[count]);
412  printf(" <- %s \n", repere);
413 }
414 
415 void printbuff(uint8_t* p_rec, int len)
416 {
417  int count;
418  printf("\n{");
419  for (count = 0; count < len - 1; count++)
420  printf("0x%0x,", p_rec[count]);
421 
422  printf("0x%0x", p_rec[len - 1]);
423  printf("}\n count %d\n", count + 1);
424 }
425 
426 /*******************************************************************************
427 **
428 ** Function NDEF_RecGetType
429 **
430 ** Description This function gets a pointer to the record type for the given NDEF record.
431 **
432 ** Returns Pointer to Type (NULL if none). TNF and len are filled in.
433 **
434 *******************************************************************************/
435 uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf, uint8_t* p_type_len)
436 {
437  uint8_t rec_hdr, type_len;
438 
439  /* First byte is the record header */
440  rec_hdr = *p_rec++;
441 
442  /* Next byte is the type field length */
443  type_len = *p_rec++;
444 
445  /* Skip the payload length */
446  if (rec_hdr & NDEF_SR_MASK)
447  p_rec += 1;
448  else
449  p_rec += 4;
450 
451  /* Skip ID field Length, if present */
452  if (rec_hdr & NDEF_IL_MASK)
453  p_rec++;
454 
455  /* At this point, p_rec points to the start of the type field. */
456  *p_type_len = type_len;
457  *p_tnf = rec_hdr & NDEF_TNF_MASK;
458 
459  if (type_len == 0)
460  return (NULL);
461  else
462  return (p_rec);
463 }
464 
465 /*******************************************************************************
466 **
467 ** Function NDEF_RecGetId
468 **
469 ** Description This function gets a pointer to the record id for the given NDEF record.
470 **
471 ** Returns Pointer to Id (NULL if none). ID Len is filled in.
472 **
473 *******************************************************************************/
474 uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len)
475 {
476  uint8_t rec_hdr, type_len;
477 
478  /* First byte is the record header */
479  rec_hdr = *p_rec++;
480 
481  /* Next byte is the type field length */
482  type_len = *p_rec++;
483 
484  /* Skip the payload length */
485  if (rec_hdr & NDEF_SR_MASK)
486  p_rec++;
487  else
488  p_rec += 4;
489 
490  /* ID field Length */
491  if (rec_hdr & NDEF_IL_MASK)
492  *p_id_len = *p_rec++;
493  else
494  *p_id_len = 0;
495 
496  /* p_rec now points to the start of the type field. The ID field follows it */
497  if (*p_id_len == 0)
498  return (NULL);
499  else
500  return (p_rec + type_len);
501 }
502 
503 /*******************************************************************************
504 **
505 ** Function NDEF_RecGetPayload
506 **
507 ** Description This function gets a pointer to the payload for the given NDEF record.
508 **
509 ** Returns a pointer to the payload (or NULL none). Payload len filled in.
510 **
511 *******************************************************************************/
512 uint8_t* NDEF_RecGetPayload(uint8_t* p_rec, uint32_t* p_payload_len)
513 {
514  uint8_t rec_hdr, type_len, id_len;
515  uint32_t payload_len;
516 
517  /* First byte is the record header */
518  rec_hdr = *p_rec++;
519 
520  /* Next byte is the type field length */
521  type_len = *p_rec++;
522 
523  /* Next is the payload length (1 or 4 bytes) */
524  if (rec_hdr & NDEF_SR_MASK)
525  payload_len = *p_rec++;
526  else
527  BE_STREAM_TO_uint32_t(payload_len, p_rec);
528 
529  *p_payload_len = payload_len;
530 
531  /* ID field Length */
532  if (rec_hdr & NDEF_IL_MASK)
533  id_len = *p_rec++;
534  else
535  id_len = 0;
536 
537  /* p_rec now points to the start of the type field. The ID field follows it, then the payload */
538  if (payload_len == 0)
539  return (NULL);
540  else
541  return (p_rec + type_len + id_len);
542 }
543 
544 void createBufNdef_TypeURI(uint8_t* strIN, int sizLen, uint8_t* strOUT)
545 {
546  // 0 , 1 , 2 , 3 |,| 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 ,
547  // 18 , 19};
548  //{0xD1,0x01,0x10,0x55|,|
549  // 0x03,0x62,0x6c,0x6f,0x67,0x2e,0x7a,0x65,0x6e,0x69,0x6b,0x61,0x2e,0x63,0x6f,0x6d};
550  // , , , |,| , b , l , o , g , . , z , e , n , i , k , a , . , c ,
551  // o , m };
552  // D1 : MB=1, ME=1, CR=0, SR=1, IL=0, TNF=001
553  // 01 : type sur 1 octet
554  // 10 : payload sur 16 octets
555  // 55 : type U (URI)
556  // 03 : préfixe http://
557  // 626C6F672E7A656E696B612E636F6D : blog.zenika.com
558 
559  int offset = 1; // for prefix type
560  uint8_t rec_hdr = 0x00;
561 
562  printf("createBufNdef_TypeURI \n");
563  // rec_hdr |= NDEF_TNF_URI;
564  rec_hdr |= NDEF_TNF_WKT;
565  rec_hdr |= NDEF_ME_MASK;
566  rec_hdr |= NDEF_MB_MASK;
567  rec_hdr |= NDEF_SR_MASK;
568  // rec_hdr |= NDEF_IL_MASK;
569  // rec_hdr |= ~NDEF_CF_MASK;
570 
571  strOUT[0] = rec_hdr;
572  strOUT[1] = 0x01;
573  strOUT[2] = sizLen + offset;
574  strOUT[3] = 0x55;
575 
576  printf("createBufNdef_TypeURI hdr=%x08\n", *strOUT);
577 
578  strOUT[4] = 0x03;
579  strncat((char*) strOUT, (char*) strIN, sizLen);
580  // strOUT[sizLen+sizehdr]=NDEF_ME_MASK;
581 
582  printf(" Check tnf=%d & prefix%x", (short) strOUT[0] & 0x07, strOUT[4]);
583  return;
584 }
585 
586 void createBufNdef_TypeText(uint8_t* strIN, int sizLen, uint8_t* strOUT)
587 {
588  int sizehdr = 4;
589 
590  int offset = 6;
591 
592  uint8_t p_new_hdr[sizehdr];
593  uint8_t rec_hdr;
594  bzero(p_new_hdr, sizeof(p_new_hdr));
595 
596  rec_hdr = 0x00;
597 
598  // rec_hdr |= NDEF_TNF_URI;
599  rec_hdr |= NDEF_TNF_WKT;
600  rec_hdr |= NDEF_ME_MASK;
601  rec_hdr |= NDEF_MB_MASK;
602  rec_hdr |= NDEF_SR_MASK;
603  // rec_hdr |= NDEF_IL_MASK;
604  // rec_hdr |= ~NDEF_CF_MASK;
605 
606  strOUT[0] = rec_hdr;
607  strOUT[1] = 0x01;
608  strOUT[2] = sizLen + offset;
609  strOUT[3] = 0x54; // T
610 
611  strOUT[4] = 0x05; // x
612  strOUT[5] = 0x65; // e
613  strOUT[6] = 0x6E; // n
614  strOUT[7] = 0x2D; // n
615  strOUT[8] = 0x55; // U
616  strOUT[9] = 0x53; // S
617 
618  // for (count = sizehdr + offset ; count<(sizLen+sizehdr); count ++ )
619  // strOUT[count] = strIN[count-sizehdr];
620  //
621  // strOUT[sizLen+sizehdr] = strIN[sizLen];
622  // strOUT[sizLen+sizehdr]=NDEF_ME_MASK;
623 
624  strncat((char*) strOUT, (char*) strIN, sizLen);
625 
626  printf(" tnf=%d", (short) p_new_hdr[0] & 0x07);
627  return;
628 }
629 
630 void createBufNdef_TypeSmartPoster(uint8_t* strIN, int sizLen, uint8_t* strOUT)
631 {
632  int sizehdr = 4;
633  int offset = 6;
634 
635  uint8_t p_new_hdr[sizehdr];
636  bzero(p_new_hdr, sizeof(p_new_hdr));
637  uint8_t rec_hdr;
638  rec_hdr = 0x00;
639 
640  // rec_hdr |= NDEF_TNF_URI;
641  rec_hdr |= NDEF_TNF_WKT;
642  rec_hdr |= NDEF_ME_MASK;
643  rec_hdr |= NDEF_MB_MASK;
644  rec_hdr |= NDEF_SR_MASK;
645 
646  // rec_hdr |= NDEF_IL_MASK;
647  // rec_hdr |= ~NDEF_CF_MASK;
648 
649  strOUT[0] = rec_hdr;
650  strOUT[1] = 0x02; // tnf 001
651  strOUT[2] = sizLen + offset + 10;
652  strOUT[3] = 0x53; // S
653 
654  strOUT[4] = 0x70; // p
655  strOUT[5] = 0x91; // x
656  strOUT[6] = 0x01; // x
657  strOUT[7] = 0x10; // x
658  strOUT[8] = 0x55; // U
659  strOUT[9] = 0x03; // x
660 
661  // for (count = 0 ; count<sizehdr; count ++ )
662  // strOUT[count] = p_new_hdr[count];
663  //
664  // for (count = sizehdr + offset ; count<(sizLen+sizehdr +offset); count ++ )
665  // strOUT[count] = strIN[count-sizehdr+offset];
666  //
667  // strOUT[sizLen+sizehdr+offset] = strIN[sizLen];
668  // //strOUT[sizLen+sizehdr]=NDEF_ME_MASK;
669 
670  strncat((char*) strOUT, (char*) strIN, sizLen);
671 
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;
683 
684  printf(" tnf=%d", (short) p_new_hdr[0] & 0x07);
685  return;
686 }
687 
688 typedef int SOCKET;
689 typedef struct sockaddr_in SOCKADDR_IN;
690 typedef struct sockaddr SOCKADDR;
691 typedef struct in_addr IN_ADDR;
692 #define INVALID_SOCKET -1
693 
694 SOCKET open_socket(char* ip, short port)
695 {
696  SOCKET sock;
697  SOCKADDR_IN sin;
698  int yes = 1;
699 
700  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
701  {
702  printf("Can’t create socket...\n");
703  close(sock);
704  return (INVALID_SOCKET);
705  }
706 
707  memset(&sin, 0, sizeof(sin));
708  inet_aton("192.168.122.129", &sin.sin_addr);
709  // inet_aton("192.168.122.42", &sin.sin_addr);
710 
711  sin.sin_port = htons(port);
712  sin.sin_family = AF_INET;
713 
714  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int)); // 2
715 
716  if (connect(sock, (SOCKADDR*) &sin, sizeof(SOCKADDR)) == INVALID_SOCKET)
717  {
718  printf("Can't connect socket %d to %s:%d, invalid socket...\n", sock, ip, port);
719  close(sock);
720  return (INVALID_SOCKET);
721  }
722  printf(" Socket %d connected to port %s:%d \n", sock, ip, port);
723  return (sock);
724 }
725 
726 int tcp_write_buff(int sock, unsigned char* data, int len)
727 {
728  return (send(sock, data, len, 0));
729 }
730 
732 {
733  SOCKET player_fd;
734  int flag_connect = 0;
735  int _PORT = 22800;
736 
737  flag_connect = 0;
738 
739  while (!flag_connect)
740  {
741  player_fd = open_socket("192.168.122.129", _PORT);
742  if (player_fd == INVALID_SOCKET)
743  {
744  printf("Cannot connect to aicNfcd (TCP %d) - VM down or access denied\n", _PORT);
745  close(player_fd);
746  sleep(1);
747  }
748  else
749  {
750  printf("Connected to aicNfcd (TCP %d)\n", _PORT);
751  flag_connect = 1;
752  }
753  }
754 
755  return player_fd;
756 }
757 /*
758 int w_msg2sock( SOKCKET player_fd, char *write_buffer )
759 {
760 
761  char inbuf[3];
762 
763  // while(strncmp(inbuf,"202",3)!=0)
764 // {
765 // int bytecount = recv (player_fd, inbuf, 3, MSG_WAITALL ) ;
766 // printf( "Connected to aicNfcd (TCP %d) Receiving \n", bytecount);
767 // sleep(1);
768 // }
769 
770  int size = tcp_write_buff( player_fd, write_buffer, strlen(write_buffer) );
771  printf( "Write to aicNfcd len %d\n", size);
772 
773 
774  return 1;
775 }*/
776 
777 void vshort_actidata(uint8_t* strIN, int sizLen, uint8_t* strOUT)
778 {
779  int ii;
780  int offset = 3;
781 
782  // printf("\n--------------------------\n");
783  // for( ii=0;ii<sizLen; ii++)
784  // printf("0x%0x-",strIN[ii]);
785  //
786  // printf("\n--------------------------\n");
787 
788  for (ii = 0; ii <= sizLen + offset; ii++)
789  strOUT[ii + offset] = strIN[ii];
790 
791  strOUT[0] = 0x00;
792  strOUT[1] = 0x00;
793  strOUT[2] = 0x00;
794 
795 // for( ii=0;ii<sizLen; ii++)
796 // printf("0x%0x-",strOUT[ii]);
797 //
798 // printf("\n--------------------------\n");
799 //
800 // for( ii=0;ii<sizLen; ii++)
801 // printf("0x%0x-",strOUT[ii]);
802 
803 #define NCI_MSG_RF_INTF_ACTIVATED 5
804 
807 
808  printf("\n--------------------------\n");
809  for (ii = 0; ii < sizLen; ii++)
810  printf("0x%0x-", strOUT[ii]);
811 
812  printf("\n--------------------------\n");
813 }
814 
815 void vshort_sendata(uint8_t* strIN, int sizLen, uint8_t* strOUT)
816 {
817  int ii;
818  int offset = 3;
819 
820  // printf("\n--------------------------\n");
821  // for( ii=0;ii<sizLen; ii++)
822  // printf("0x%0x-",strIN[ii]);
823  //
824  // printf("\n--------------------------\n");
825 
826  for (ii = 0; ii <= sizLen + offset; ii++)
827  strOUT[ii + offset] = strIN[ii];
828 
829  strOUT[0] = 0x00;
830  strOUT[1] = 0x00;
831  strOUT[2] = 0x00;
832 
833  // for( ii=0;ii<sizLen; ii++)
834  // printf("0x%0x-",strOUT[ii]);
835  //
836  // printf("\n--------------------------\n");
837 
838  NCI_DATA_BLD_HDR(strOUT, 0, sizLen);
839 
840  // for( ii=0;ii<sizLen; ii++)
841  // printf("0x%0x-",strOUT[ii]);
842 }
843 
844 #if 0
845 static int wait_for_client(int server)
846 {
847  int client = -1;
848 
849  if (listen(server, 1) < 0)
850  {
851  printf("Unable to listen to socket, errno=%d\n", errno);
852  return -1;
853  }
854 
855  printf("wait_for_client\n");
856  client = accept(server, NULL, 0);
857 
858  if (client < 0)
859  {
860  printf("Unable to accept socket for main conection, errno=%d\n", errno);
861  return -1;
862  }
863 
864  return client;
865 }
866 #endif
867 
868 int main(int argc, char* argv[])
869 {
870  (void) argc;
871  // uint8_t p_new_rec[] = {0xFF, 0x00, 0x1E, 0x00, 0x6, 0x2, 0x6, 0xC, 0x6, 0xF, 0x6,
872  // 0x7, 0x2, 0xE, 0x7, 0xA, 0x6, 0x5, 0x6, 0xE, 0x6, 0x9, 0x6, 0xB, 0x6, 0x1, 0x2, 0xE, 0x6,
873  // 0x3, 0x6, 0xF, 0x6, 0xD};
874  // uint8_t p_new_rec[] = {0xFF, 0x00, 0x1E, 0x00, 0x6, 0x2, 0x6, 0xC, 0x6, 0xF, 0x6,
875  // 0x7, 0x2, 0xE, 0x7, 0xA, 0x6, 0x5, 0x6, 0xE, 0x6, 0x9, 0x6, 0xB, 0x6, 0x1, 0x2, 0xE, 0x6,
876  // 0x3, 0x6, 0xF, 0x6, 0xD};
877  // ^ ^ ^ ^
878  // | | | |
879  // rec_hdr |= NDEF_TNF_URI; | | |
880  // rec_hdr |= NDEF_SR_MASK; | | |
881  // rec_hdr |= ~NDEF_IL_MASK; | | |
882  // rec_hdr |= ~NDEF_CF_MASK; | | |
883  // | | |
884  // | | |
885  // tnf | |
886  // payload |
887  // |
888  // id_len
889  // msg_len = 4 + tnf + payload + id_len
890  // here -> 30 + 4
891  //
892 
893  // 0 , 1 , 2 , 3 |,| 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 ,
894  // 18 , 19};
895  //{0xD1,0x01,0x10,0x54|,|0x05,0x65,0x6e,0x2d,0x55,0x53,0x48,0x65,0x6C,0x6C,0x6F,0x20,0x57,0x6F,0x72,0x6C};
896  // , , , |,| , e , n , - , U , S , H , e , l , l , o , , W , o ,
897  // r , l };
898  // D1 : MB=1, ME=1, CR=0, SR=1, IL=0, TNF=001
899  // 01 : type sur 1 octet
900  // 10 : payload sur 16 octets
901  // 54 : type T (Text)
902  // 05 : pas de préfixe
903  // 656e2d555348656C6C6F20576F726C : Hello Worl
904  // , , , |,| , F , r , H , e , l , l , o , , W , o , r , l , d ,
905  // , ! };
906 
907  printf(" args[0]:Type(0:Uri 1:Text 2:SmartPoster) ; [1]:Lang(0:En-US 1:Fr) ; "
908  "[2]:payload(blahblah)\n");
909 
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]);
914 
915  int argLen = strlen(argv[4]);
916  int offsetPrefix = 1;
917  int sizehdr = 4; // header + Prefix
918  int msg_len = 0;
919 
920  uint8_t* msg = 0;
921  uint8_t* msg2;
922  uint8_t msg1[6] = "303";
923 
924  strncpy((char*) argbuff, argv[4], 1024);
925  // ccpy (argbuff, "blog.zenika.com"); argLen=20;
926  printbuff(argbuff, argLen);
927  printf("argLen=%d argBuff=%s\n", argLen, argbuff);
928 
929  int flag = 1;
930 
931  switch (Type)
932  {
933  case 0:
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));
938  createBufNdef_TypeURI(argbuff, argLen, msg);
939  break;
940 
941  case 1: // Type Text need calculate HeaderSize
942  offsetPrefix = 6;
943  printf("case 1 ; Type %d Lang %d\n", Type, Lang);
944  if (Lang == 0)
945  {
946  // sizehdr += 6 ;
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));
951  createBufNdef_TypeText(argbuff, argLen, msg);
952  }
953  else if (Lang == 1)
954  {
955  // sizehdr += 3 ;
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));
960  createBufNdef_TypeText(argbuff, argLen, msg);
961  }
962  else
963  {
964  free(argbuff);
965  return 0;
966  }
967  break;
968 
969  case 2:
970  offsetPrefix = 6;
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));
975  createBufNdef_TypeSmartPoster(argbuff, argLen, msg);
976  break;
977  case 3:
978  printf("case 3 ; Type %d Lang %d\n", Type, Lang);
979  sizehdr = 2;
980  msg_len = (argLen + sizehdr);
981  msg = (uint8_t*) calloc(msg_len, sizeof(uint8_t));
982  vshort_sendata(argbuff, argLen, msg);
983  // w_msg2sock(msg);
984  // flag=1;
985  default:
986  printf("default ; Type %d Lang %d\n", Type, Lang);
987  free(argbuff);
988  return 0;
989  break;
990  }
991 
992  // if (flag)
993  // return ;
994  free(argbuff);
995 
996  printbuff(msg, msg_len);
997 
998  tNDEF_STATUS status = NDEF_MsgValidate(msg, msg_len, TRUE);
999 
1000  printf("\nResult : \n Status Validate %d \n", status);
1001  printError(status);
1002  printf("\nDone !\n");
1003 
1004  int size, player_fd = -1;
1005  uint8_t p_tnf;
1006  uint8_t p_type_len;
1007  uint8_t p_id_len;
1008  uint32_t p_payload_len;
1009 
1010  NDEF_RecGetType(msg, &p_tnf, &p_type_len);
1011 
1012  NDEF_RecGetId(msg, &p_id_len);
1013 
1014  NDEF_RecGetPayload(msg, &p_payload_len);
1015 
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);
1018 
1019  uint8_t c[3];
1020  int len2send = strlen((char*) msg) + 3;
1021  snprintf((char*) c, sizeof(c), "%d", len2send);
1022 
1023  strncat((char*) msg1, (char*) c, sizeof(msg1) - strlen((char*) msg1) - 1);
1024 
1025  printbuff(msg1, 6);
1026  /*
1027  int test =(int)strtol( msg1+3, NULL, 10 );
1028  printf ( "\n--%d--\n" , test);
1029  */
1030 
1031  /*
1032  int rd_client, rd_server;
1033 
1034 
1035  if ((rd_server = start_server()) == -1) {
1036  ALOGE("unfcd_read_thread : start_server unable to create socket\n");
1037  return 0;
1038  }
1039 
1040  // Listen for main connection
1041  rd_client = wait_for_client(rd_server);
1042  //player_fd, inbuf, 3, MSG_WAITALL*/
1043 
1044  player_fd = getconnect();
1045 
1046  if (sendORacti == 0)
1047  {
1048  int16_t cmd;
1049  recv(player_fd, &cmd, sizeof(cmd), MSG_WAITALL);
1050  printf("\n received %d \n", cmd);
1051  if (cmd != 1001)
1052  {
1053  printf("Unknown cmd : %d, exit\n", cmd);
1054  exit(-1);
1055  }
1056  else
1057  {
1058  flag = 0;
1059  }
1060  }
1061 
1062  if (flag)
1063  {
1064  int16_t cmd;
1065  recv(player_fd, &cmd, sizeof(cmd), MSG_DONTWAIT);
1066  printf("\n received %d \n", cmd);
1067  if (cmd == 1002)
1068  {
1069  printf("NFC is off cmd : %d, exit\n", cmd);
1070  exit(-1);
1071  }
1072  }
1073 
1074  // w_msg2sock(msg1);
1075  size = tcp_write_buff(player_fd, msg1, 5);
1076 
1077  // w_msg2sock(msg);
1078  sleep(3);
1079  printf("\n writting msg1 %d %lu!\n", size, strlen((char*) msg1));
1080 
1081  msg2 = (uint8_t*) calloc(strlen((char*) msg), sizeof(uint8_t));
1082 
1083  if (sendORacti == 0)
1084  {
1085  vshort_sendata(msg, msg_len, msg2);
1086  // w_msg2sock(msg2);
1087  size = tcp_write_buff(player_fd, msg2, len2send);
1088  printf("\n writting sendata %d %d %lu!\n", size, len2send, strlen((char*) msg));
1089  printbuff(msg2, msg_len + 3);
1090  }
1091  else if (sendORacti == 2)
1092  {
1093  vshort_sendata(msg, msg_len, msg2);
1094  // w_msg2sock(msg2);
1095  size = tcp_write_buff(player_fd, msg2, len2send);
1096  printf("\n writting sendata %d %d %lu!\n", size, len2send, strlen((char*) msg));
1097  printbuff(msg2, msg_len + 3);
1098  }
1099  else
1100  {
1101  vshort_actidata(msg, msg_len, msg2);
1102  // w_msg2sock(msg2);
1103  size = tcp_write_buff(player_fd, msg2, len2send);
1104  printf("\n writting msg2 activate %d %d %lu!\n", size, len2send, strlen((char*) msg2));
1105  printbuff(msg2, msg_len + 3);
1106  }
1107 
1108  // w_msg2sock(msg1);sleep(3);printf ("writting again msg1\n");
1109  // w_msg2sock(msg1);sleep(3);printf ("writting again msg1\n");
1110  // w_msg2sock(msg1);sleep(3);printf ("writting again msg1\n");
1111  // w_msg2sock(msg1);sleep(3);printf ("writting again msg1\n");
1112 
1113  return 0;
1114 }
int main(int argc, char *argv[])
uint8_t tNFA_TNF
void createBufNdef_TypeText(uint8_t *strIN, int sizLen, uint8_t *strOUT)
#define NDEF_ME_MASK
#define NDEF_TNF_UNKNOWN
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)
struct sockaddr SOCKADDR
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
uint16_t len
#define NCI_GID_RF_MANAGE
int SOCKET
SOCKET open_socket(char *ip, short port)
#define NCI_MSG_BLD_HDR0(p, mt, gid)
#define NDEF_TNF_WKT
struct in_addr IN_ADDR
#define INVALID_SOCKET
#define NCI_MT_NTF
#define FALSE
#define BE_STREAM_TO_uint32_t(u32, p)
#define NDEF_TNF_EMPTY
void vshort_actidata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
uint8_t tNDEF_STATUS
void printbuff0(uint8_t *p_rec, uint8_t *repere)
uint16_t layer_specific
uint16_t offset
SOCKET getconnect()
#define NDEF_SR_MASK
#define NDEF_IL_MASK
void createBufNdef_TypeURI(uint8_t *strIN, int sizLen, uint8_t *strOUT)
#define NCI_MSG_RF_INTF_ACTIVATED
#define NDEF_MB_MASK
#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 NDEF_TNF_MASK
#define TRUE
Define common buffer sizes.
uint16_t event
struct sockaddr_in SOCKADDR_IN
#define NDEF_CF_MASK
#define BOOLEAN
void printError(tNDEF_STATUS status)
#define NCI_DATA_BLD_HDR(p, cid, len)