AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
mockVMLibNci.c
Go to the documentation of this file.
1 
2 #define LOG_TAG "mocklibnci"
3 
4 #include "mockVMLibNci.h"
5 #include <string.h>
6 
7 void createBufNdef_TypeURI(uint8_t* strIN, int sizLen, uint8_t* strOUT)
8 {
9  // 0 , 1 , 2 , 3 |,| 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 ,
10  // 18 , 19};
11  //{0xD1,0x01,0x10,0x55|,|
12  // 0x03,0x62,0x6c,0x6f,0x67,0x2e,0x7a,0x65,0x6e,0x69,0x6b,0x61,0x2e,0x63,0x6f,0x6d};
13  // , , , |,| , b , l , o , g , . , z , e , n , i , k , a , . , c ,
14  // o , m };
15  // D1 : MB=1, ME=1, CR=0, SR=1, IL=0, TNF=001
16  // 01 : type sur 1 octet
17  // 10 : payload sur 16 octets
18  // 55 : type U (URI)
19  // 03 : préfixe http://
20  // 626C6F672E7A656E696B612E636F6D : blog.zenika.com
21 
22  int offset = 1; // for prefix type
23  uint8_t rec_hdr = 0x00;
24 
25  rec_hdr |= NDEF_TNF_WKT;
26  rec_hdr |= NDEF_ME_MASK;
27  rec_hdr |= NDEF_MB_MASK;
28  rec_hdr |= NDEF_SR_MASK;
29 
30  strOUT[0] = rec_hdr;
31  strOUT[1] = 0x01;
32  strOUT[2] = sizLen + offset;
33  strOUT[3] = 0x55;
34 
35  strOUT[4] = 0x03;
36  strncat((char*) strOUT, (const char*) strIN, sizLen);
37 
38  return;
39 }
40 
41 void createBufNdef_TypeText(uint8_t* strIN, int sizLen, uint8_t* strOUT)
42 {
43  int offset = 6;
44  uint8_t rec_hdr;
45 
46  rec_hdr = 0x00;
47 
48  rec_hdr |= NDEF_TNF_WKT;
49  rec_hdr |= NDEF_ME_MASK;
50  rec_hdr |= NDEF_MB_MASK;
51  rec_hdr |= NDEF_SR_MASK;
52 
53  strOUT[0] = rec_hdr;
54  strOUT[1] = 0x01;
55  strOUT[2] = sizLen + offset;
56  strOUT[3] = 0x54; // T
57 
58  strOUT[4] = 0x05; // x
59  strOUT[5] = 0x65; // e
60  strOUT[6] = 0x6E; // n
61  strOUT[7] = 0x2D; // n
62  strOUT[8] = 0x55; // U
63  strOUT[9] = 0x53; // S
64 
65  strncat((char*) strOUT, (const char*) strIN, sizLen);
66 
67  return;
68 }
69 
70 void createBufNdef_TypeSmartPoster(uint8_t* strIN, uint8_t* strIN2, int sizLen, uint8_t* strOUT)
71 {
72  int sizehdr = 4;
73  int offset = 6;
74 
75  uint8_t rec_hdr;
76  rec_hdr = 0x00;
77 
78  rec_hdr |= NDEF_TNF_WKT;
79  rec_hdr |= NDEF_ME_MASK;
80  rec_hdr |= NDEF_MB_MASK;
81  rec_hdr |= NDEF_SR_MASK;
82 
83  strOUT[0] = rec_hdr;
84  strOUT[1] = 0x02; // tnf 001
85  strOUT[2] = sizLen + offset + 10;
86  strOUT[3] = 0x53; // S
87 
88  strOUT[4] = 0x70; // p
89  strOUT[5] = 0x91; // x
90  strOUT[6] = 0x01; // x
91  strOUT[7] = 0x10; // x
92  strOUT[8] = 0x55; // U
93  strOUT[9] = 0x03; // x
94 
95  strncat((char*) strOUT, (const char*) strIN, sizLen);
96 
97  strOUT[sizehdr + offset + sizLen + 0] = 0x51;
98  strOUT[sizehdr + offset + sizLen + 1] = 0x01;
99  strOUT[sizehdr + offset + sizLen + 2] = 0x07;
100  strOUT[sizehdr + offset + sizLen + 3] = 0x54;
101  strOUT[sizehdr + offset + sizLen + 4] = 0x02;
102  strOUT[sizehdr + offset + sizLen + 5] = 0x66;
103  strOUT[sizehdr + offset + sizLen + 6] = 0x72;
104  strOUT[sizehdr + offset + sizLen + 7] = 0x42;
105  strOUT[sizehdr + offset + sizLen + 8] = 0x6C;
106  strOUT[sizehdr + offset + sizLen + 9] = 0x6F;
107  strOUT[sizehdr + offset + sizLen + 10] = 0x67;
108 
109  return;
110 }
111 
112 void vshort_actidata(uint8_t* strIN, int sizLen, uint8_t* strOUT)
113 {
114  int ii;
115  int offset = 3;
116 
117  for (ii = 0; ii <= sizLen + offset; ii++)
118  strOUT[ii + offset] = strIN[ii];
119 
120  strOUT[0] = 0x00;
121  strOUT[1] = 0x00;
122  strOUT[2] = 0x00;
123 
124 #define NCI_MSG_RF_INTF_ACTIVATED 5
125 
128 }
129 
130 void vshort_sendata(uint8_t* strIN, int sizLen, uint8_t* strOUT)
131 {
132  int ii;
133  int offset = 3;
134 
135  for (ii = 0; ii <= sizLen + offset; ii++)
136  strOUT[ii + offset] = strIN[ii];
137 
138  strOUT[0] = 0x00;
139  strOUT[1] = 0x00;
140  strOUT[2] = 0x00;
141 
142  NCI_DATA_BLD_HDR(strOUT, 0, sizLen);
143 }
144 
145 int codeNFC(NfcPayload* nfcData, uint8_t* msg)
146 {
147  int Type = nfcData->type;
148  int Lang = nfcData->lang;
149 
150  int argLen = strlen(nfcData->text);
151  int offsetPrefix = 1;
152  int sizehdr = 4; // header + Prefix
153  int msg_len = 0;
154 
155  LOGM("codeNFC - %d %d %s %s", nfcData->type, nfcData->lang, nfcData->text, nfcData->tittle);
156 
157  switch (Type)
158  {
159  case 0:
160  sizehdr += offsetPrefix;
161  msg_len = (argLen + sizehdr);
162  // msg = (uint8_t*)calloc(msg_len, sizeof(uint8_t));
163  createBufNdef_TypeURI((unsigned char*) nfcData->text, argLen, msg);
164  break;
165 
166  case 1: // Type Text need calculate HeaderSize
167  offsetPrefix = 6;
168  if (Lang == 0)
169  {
170  // sizehdr += 6 ;
171  sizehdr += offsetPrefix;
172  msg_len = (argLen + sizehdr);
173  // msg = (uint8_t*)calloc(msg_len, sizeof(uint8_t));
174  createBufNdef_TypeText((unsigned char*) nfcData->text, argLen, msg);
175  }
176  else if (Lang == 1)
177  {
178  // sizehdr += 3 ;
179  sizehdr += offsetPrefix;
180  msg_len = (argLen + sizehdr);
181  // msg = (uint8_t*)calloc(msg_len, sizeof(uint8_t));
182  createBufNdef_TypeText((unsigned char*) nfcData->text, argLen, msg);
183  }
184  break;
185 
186  case 2:
187  offsetPrefix = 6;
188  sizehdr += offsetPrefix;
189  msg_len = (argLen + sizehdr) + 11;
190  // msg = (uint8_t*)calloc(msg_len, sizeof(uint8_t));
191  createBufNdef_TypeSmartPoster((unsigned char*) nfcData->text,
192  (unsigned char*) nfcData->tittle, argLen, msg);
193  break;
194  case 3:
195  sizehdr = 2;
196  msg_len = (argLen + sizehdr);
197  // msg = (uint8_t*)calloc(msg_len, sizeof(uint8_t));
198  // vshort_sendata(argbuff, argLen, msg);
199 
200  default:
201  break;
202  }
203  return msg_len;
204 }
205 
206 /*******************************************************************************
207 **
208 ** Function NDEF_RecGetType
209 **
210 ** Description This function gets a pointer to the record type for the given NDEF record.
211 **
212 ** Returns Pointer to Type (NULL if none). TNF and len are filled in.
213 **
214 *******************************************************************************/
215 uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf, uint8_t* p_type_len)
216 {
217  uint8_t rec_hdr, type_len;
218 
219  /* First byte is the record header */
220  rec_hdr = *p_rec++;
221 
222  /* Next byte is the type field length */
223  type_len = *p_rec++;
224 
225  /* Skip the payload length */
226  if (rec_hdr & NDEF_SR_MASK)
227  p_rec += 1;
228  else
229  p_rec += 4;
230 
231  /* Skip ID field Length, if present */
232  if (rec_hdr & NDEF_IL_MASK)
233  p_rec++;
234 
235  /* At this point, p_rec points to the start of the type field. */
236  *p_type_len = type_len;
237  *p_tnf = rec_hdr & NDEF_TNF_MASK;
238 
239  if (type_len == 0)
240  return (NULL);
241  else
242  return (p_rec);
243 }
244 
245 /*******************************************************************************
246 **
247 ** Function NDEF_RecGetId
248 **
249 ** Description This function gets a pointer to the record id for the given NDEF record.
250 **
251 ** Returns Pointer to Id (NULL if none). ID Len is filled in.
252 **
253 *******************************************************************************/
254 uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len)
255 {
256  uint8_t rec_hdr, type_len;
257 
258  /* First byte is the record header */
259  rec_hdr = *p_rec++;
260 
261  /* Next byte is the type field length */
262  type_len = *p_rec++;
263 
264  /* Skip the payload length */
265  if (rec_hdr & NDEF_SR_MASK)
266  p_rec++;
267  else
268  p_rec += 4;
269 
270  /* ID field Length */
271  if (rec_hdr & NDEF_IL_MASK)
272  *p_id_len = *p_rec++;
273  else
274  *p_id_len = 0;
275 
276  /* p_rec now points to the start of the type field. The ID field follows it */
277  if (*p_id_len == 0)
278  return (NULL);
279  else
280  return (p_rec + type_len);
281 }
282 
283 /*******************************************************************************
284 **
285 ** Function NDEF_RecGetPayload
286 **
287 ** Description This function gets a pointer to the payload for the given NDEF record.
288 **
289 ** Returns a pointer to the payload (or NULL none). Payload len filled in.
290 **
291 *******************************************************************************/
292 uint8_t* NDEF_RecGetPayload(uint8_t* p_rec, uint32_t* p_payload_len)
293 {
294  uint8_t rec_hdr, type_len, id_len;
295  uint32_t payload_len;
296 
297  /* First byte is the record header */
298  rec_hdr = *p_rec++;
299 
300  /* Next byte is the type field length */
301  type_len = *p_rec++;
302 
303  /* Next is the payload length (1 or 4 bytes) */
304  if (rec_hdr & NDEF_SR_MASK)
305  payload_len = *p_rec++;
306  else
307  BE_STREAM_TO_uint32_t(payload_len, p_rec);
308 
309  *p_payload_len = payload_len;
310 
311  /* ID field Length */
312  if (rec_hdr & NDEF_IL_MASK)
313  id_len = *p_rec++;
314  else
315  id_len = 0;
316 
317  /* p_rec now points to the start of the type field. The ID field follows it, then the payload */
318  if (payload_len == 0)
319  return (NULL);
320  else
321  return (p_rec + type_len + id_len);
322 }
323 
324 /*******************************************************************************
325 **
326 ** Function NDEF_MsgValidate
327 **
328 ** Description This function validates an NDEF message.
329 **
330 ** Returns TRUE if all OK, or FALSE if the message is invalid.
331 **
332 *******************************************************************************/
333 tNDEF_STATUS NDEF_MsgValidate(uint8_t* p_msg, uint32_t msg_len, int b_allow_chunks)
334 {
335  uint8_t* p_rec = p_msg;
336  uint8_t* p_end = p_msg + msg_len;
337  uint8_t rec_hdr = 0, type_len, id_len;
338  int count;
339  uint32_t payload_len;
340  int bInChunk = FALSE;
341 
342  if ((p_msg == NULL) || (msg_len < 3))
343  return (NDEF_MSG_TOO_SHORT);
344 
345  /* The first record must have the MB bit set */
346  if ((*p_msg & NDEF_MB_MASK) == 0)
347  return (NDEF_MSG_NO_MSG_BEGIN);
348 
349  /* The first record cannot be a chunk */
350  if ((*p_msg & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
351  return (NDEF_MSG_UNEXPECTED_CHUNK);
352 
353  for (count = 0; p_rec < p_end; count++)
354  {
355  /* if less than short record header */
356  if (p_rec + 3 > p_end)
357  return (NDEF_MSG_TOO_SHORT);
358 
359  rec_hdr = *p_rec++;
360 
361  /* The second and all subsequent records must NOT have the MB bit set */
362  if ((count > 0) && (rec_hdr & NDEF_MB_MASK))
363  return (NDEF_MSG_EXTRA_MSG_BEGIN);
364 
365  /* Type field length */
366  type_len = *p_rec++;
367 
368  /* Payload length - can be 1 or 4 bytes */
369  if (rec_hdr & NDEF_SR_MASK)
370  payload_len = *p_rec++;
371  else
372  {
373  /* if less than 4 bytes payload length */
374  if (p_rec + 4 > p_end)
375  return (NDEF_MSG_TOO_SHORT);
376 
377  BE_STREAM_TO_uint32_t(payload_len, p_rec);
378  }
379 
380  /* ID field Length */
381  if (rec_hdr & NDEF_IL_MASK)
382  {
383  /* if less than 1 byte ID field length */
384  if (p_rec + 1 > p_end)
385  return (NDEF_MSG_TOO_SHORT);
386 
387  id_len = *p_rec++;
388  }
389  else
390  id_len = 0;
391 
392  /* A chunk must have type "unchanged", and no type or ID fields */
393  if (rec_hdr & NDEF_CF_MASK)
394  {
395  if (!b_allow_chunks)
396  return (NDEF_MSG_UNEXPECTED_CHUNK);
397 
398  /* Inside a chunk, the type must be unchanged and no type or ID field i sallowed */
399  if (bInChunk)
400  {
401  if ((type_len != 0) || (id_len != 0) ||
402  ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED))
403  return (NDEF_MSG_INVALID_CHUNK);
404  }
405  else
406  {
407  /* First record of a chunk must NOT have type "unchanged" */
408  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
409  return (NDEF_MSG_INVALID_CHUNK);
410 
411  bInChunk = TRUE;
412  }
413  }
414  else
415  {
416  /* This may be the last guy in a chunk. */
417  if (bInChunk)
418  {
419  if ((type_len != 0) || (id_len != 0) ||
420  ((rec_hdr & NDEF_TNF_MASK) != NDEF_TNF_UNCHANGED))
421  return (NDEF_MSG_INVALID_CHUNK);
422 
423  bInChunk = FALSE;
424  }
425  else
426  {
427  /* If not in a chunk, the record must NOT have type "unchanged" */
428  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNCHANGED)
429  return (NDEF_MSG_INVALID_CHUNK);
430  }
431  }
432 
433  /* An empty record must NOT have a type, ID or payload */
434  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_EMPTY)
435  {
436  if ((type_len != 0) || (id_len != 0) || (payload_len != 0))
438  }
439 
440  if ((rec_hdr & NDEF_TNF_MASK) == NDEF_TNF_UNKNOWN)
441  {
442  if (type_len != 0)
443  return (NDEF_MSG_LENGTH_MISMATCH);
444  }
445 
446  /* Point to next record */
447  p_rec += (payload_len + type_len + id_len);
448 
449  if (rec_hdr & NDEF_ME_MASK)
450  break;
451 
452  rec_hdr = 0;
453  }
454 
455  /* The last record should have the ME bit set */
456  if ((rec_hdr & NDEF_ME_MASK) == 0)
457  return (NDEF_MSG_NO_MSG_END);
458 
459  /* p_rec should equal p_end if all the length fields were correct */
460  if (p_rec != p_end)
461  return (NDEF_MSG_LENGTH_MISMATCH);
462 
463  return (NDEF_OK);
464 }
void createBufNdef_TypeText(uint8_t *strIN, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:41
tNDEF_STATUS NDEF_MsgValidate(uint8_t *p_msg, uint32_t msg_len, int b_allow_chunks)
Definition: mockVMLibNci.c:333
uint8_t * NDEF_RecGetPayload(uint8_t *p_rec, uint32_t *p_payload_len)
Definition: mockVMLibNci.c:292
#define NDEF_ME_MASK
#define NDEF_TNF_UNKNOWN
uint8_t * NDEF_RecGetId(uint8_t *p_rec, uint8_t *p_id_len)
Definition: mockVMLibNci.c:254
void createBufNdef_TypeURI(uint8_t *strIN, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:7
#define NCI_GID_RF_MANAGE
#define NCI_MSG_BLD_HDR0(p, mt, gid)
#define NDEF_TNF_WKT
#define NCI_MT_NTF
#define FALSE
#define BE_STREAM_TO_uint32_t(u32, p)
#define NDEF_TNF_EMPTY
uint8_t tNDEF_STATUS
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:130
int codeNFC(NfcPayload *nfcData, uint8_t *msg)
Definition: mockVMLibNci.c:145
#define NCI_MSG_RF_INTF_ACTIVATED
void createBufNdef_TypeSmartPoster(uint8_t *strIN, uint8_t *strIN2, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:70
#define NDEF_SR_MASK
#define NDEF_IL_MASK
#define NDEF_MB_MASK
#define NDEF_TNF_UNCHANGED
#define LOGM(...)
Log at MESSAGE level.
Definition: logger.h:25
#define NCI_MSG_BLD_HDR1(p, oid)
uint8_t * NDEF_RecGetType(uint8_t *p_rec, uint8_t *p_tnf, uint8_t *p_type_len)
Definition: mockVMLibNci.c:215
#define NDEF_TNF_MASK
void vshort_actidata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:112
#define TRUE
#define NDEF_CF_MASK
#define NCI_DATA_BLD_HDR(p, cid, len)