Embedded SDK
Embedded SDK
nfc_ndef.c
Go to the documentation of this file.
1 #ifdef HAVE_NFC
2 #ifdef HAVE_NDEF_SUPPORT
3 #include "nfc_ndef.h"
4 #include "string.h"
5 #include "os_apdu.h"
6 
7 /**********************
8  * GLOBAL FUNCTIONS
9  **********************/
10 
18 uint16_t os_get_uri_header(uint8_t uri_id, char *uri_header)
19 {
20  switch (uri_id) {
21  case URI_ID_0x01:
22  strcpy(uri_header, URI_ID_0x01_STRING);
23  break;
24  case URI_ID_0x02:
25  strcpy(uri_header, URI_ID_0x02_STRING);
26  break;
27  case URI_ID_0x03:
28  strcpy(uri_header, URI_ID_0x03_STRING);
29  break;
30  case URI_ID_0x04:
31  strcpy(uri_header, URI_ID_0x04_STRING);
32  break;
33  case URI_ID_0x05:
34  strcpy(uri_header, URI_ID_0x05_STRING);
35  break;
36  case URI_ID_0x06:
37  strcpy(uri_header, URI_ID_0x06_STRING);
38  break;
39  case URI_ID_0x07:
40  strcpy(uri_header, URI_ID_0x07_STRING);
41  break;
42  case URI_ID_0x08:
43  strcpy(uri_header, URI_ID_0x08_STRING);
44  break;
45  case URI_ID_0x09:
46  strcpy(uri_header, URI_ID_0x09_STRING);
47  break;
48  case URI_ID_0x0A:
49  strcpy(uri_header, URI_ID_0x0A_STRING);
50  break;
51  case URI_ID_0x0B:
52  strcpy(uri_header, URI_ID_0x0B_STRING);
53  break;
54  case URI_ID_0x0C:
55  strcpy(uri_header, URI_ID_0x0C_STRING);
56  break;
57  case URI_ID_0x0D:
58  strcpy(uri_header, URI_ID_0x0D_STRING);
59  break;
60  case URI_ID_0x0E:
61  strcpy(uri_header, URI_ID_0x0E_STRING);
62  break;
63  case URI_ID_0x0F:
64  strcpy(uri_header, URI_ID_0x0F_STRING);
65  break;
66  case URI_ID_0x10:
67  strcpy(uri_header, URI_ID_0x10_STRING);
68  break;
69  case URI_ID_0x11:
70  strcpy(uri_header, URI_ID_0x11_STRING);
71  break;
72  case URI_ID_0x12:
73  strcpy(uri_header, URI_ID_0x12_STRING);
74  break;
75  case URI_ID_0x13:
76  strcpy(uri_header, URI_ID_0x13_STRING);
77  break;
78  case URI_ID_0x14:
79  strcpy(uri_header, URI_ID_0x14_STRING);
80  break;
81  case URI_ID_0x15:
82  strcpy(uri_header, URI_ID_0x15_STRING);
83  break;
84  case URI_ID_0x16:
85  strcpy(uri_header, URI_ID_0x16_STRING);
86  break;
87  case URI_ID_0x17:
88  strcpy(uri_header, URI_ID_0x17_STRING);
89  break;
90  case URI_ID_0x18:
91  strcpy(uri_header, URI_ID_0x18_STRING);
92  break;
93  case URI_ID_0x19:
94  strcpy(uri_header, URI_ID_0x19_STRING);
95  break;
96  case URI_ID_0x1A:
97  strcpy(uri_header, URI_ID_0x1A_STRING);
98  break;
99  case URI_ID_0x1B:
100  strcpy(uri_header, URI_ID_0x1B_STRING);
101  break;
102  case URI_ID_0x1C:
103  strcpy(uri_header, URI_ID_0x1C_STRING);
104  break;
105  case URI_ID_0x1D:
106  strcpy(uri_header, URI_ID_0x1D_STRING);
107  break;
108  case URI_ID_0x1E:
109  strcpy(uri_header, URI_ID_0x1E_STRING);
110  break;
111  case URI_ID_0x1F:
112  strcpy(uri_header, URI_ID_0x1F_STRING);
113  break;
114  case URI_ID_0x20:
115  strcpy(uri_header, URI_ID_0x20_STRING);
116  break;
117  case URI_ID_0x21:
118  strcpy(uri_header, URI_ID_0x21_STRING);
119  break;
120  case URI_ID_0x22:
121  strcpy(uri_header, URI_ID_0x22_STRING);
122  break;
123  case URI_ID_0x23:
124  strcpy(uri_header, URI_ID_0x23_STRING);
125  break;
126  default:
127  return 0;
128  break;
129  }
130  return strlen(uri_header);
131 }
132 
140 uint8_t os_parse_ndef(uint8_t *in_buffer, ndef_struct_t *parsed)
141 {
142  uint8_t text_length, info_length;
143  parsed->ndef_type = in_buffer[APDU_OFF_P1];
144  parsed->uri_id = in_buffer[APDU_OFF_P2];
145  text_length = in_buffer[APDU_OFF_DATA];
146  if (text_length > NFC_TEXT_MAX_LEN) {
147  return 1;
148  }
149  memcpy(parsed->text, &in_buffer[APDU_OFF_DATA + 1], text_length);
150  parsed->text[text_length] = '\0';
151  info_length = in_buffer[APDU_OFF_DATA + 1 + text_length];
152  if (info_length > NFC_INFO_MAX_LEN) {
153  return 1;
154  }
155  if (info_length) {
156  memcpy(parsed->info, &in_buffer[APDU_OFF_DATA + 1 + text_length + 1], info_length);
157  parsed->info[info_length] = '\0';
158  }
159  return 0;
160 }
161 
169 uint16_t os_ndef_to_string(ndef_struct_t *ndef_message, char *out_string)
170 {
171  uint16_t tot_length = 0;
172  uint16_t length_internal = 0;
173  if (ndef_message->ndef_type == NFC_NDEF_TYPE_TEXT) {
174  tot_length += strlen(ndef_message->text);
175  if (tot_length > NFC_TEXT_MAX_LEN) {
176  return 0;
177  }
178  strcpy(out_string, ndef_message->text);
179  }
180  else if (ndef_message->ndef_type == NFC_NDEF_TYPE_URI) {
181  tot_length += os_get_uri_header(ndef_message->uri_id, out_string);
182  length_internal = strlen(ndef_message->text);
183  if (tot_length + length_internal > URI_ID_STRING_MAX_LEN + NFC_TEXT_MAX_LEN) {
184  return 0;
185  }
186  strcpy(&out_string[tot_length], ndef_message->text);
187  tot_length += length_internal;
188  if (ndef_message->info[0] != '\0') {
189  out_string[tot_length++] = '\n';
190  length_internal = strlen(ndef_message->info);
191  if (tot_length + length_internal > NFC_NDEF_MAX_SIZE) {
192  return 0;
193  }
194  strcpy(&out_string[tot_length], ndef_message->info);
195  tot_length += length_internal;
196  }
197  }
198  return tot_length;
199 }
200 #endif // HAVE_NDEF_SUPPORT
201 #endif // HAVE_NFC
#define URI_ID_0x07_STRING
Definition: nfc_ndef.h:55
#define URI_ID_0x23_STRING
Definition: nfc_ndef.h:83
#define URI_ID_0x12_STRING
Definition: nfc_ndef.h:66
ndef_struct_t
Definition: nfc_ndef.h:107
#define URI_ID_0x18
Definition: nfc_ndef.h:35
#define URI_ID_0x17
Definition: nfc_ndef.h:34
#define URI_ID_0x0A_STRING
Definition: nfc_ndef.h:58
#define URI_ID_0x1D
Definition: nfc_ndef.h:40
#define URI_ID_0x14_STRING
Definition: nfc_ndef.h:68
#define URI_ID_0x11
Definition: nfc_ndef.h:28
#define URI_ID_0x0E_STRING
Definition: nfc_ndef.h:62
#define URI_ID_0x21_STRING
Definition: nfc_ndef.h:81
#define URI_ID_0x15
Definition: nfc_ndef.h:32
#define URI_ID_0x18_STRING
Definition: nfc_ndef.h:72
#define URI_ID_0x07
Definition: nfc_ndef.h:18
#define NFC_INFO_MAX_LEN
Definition: nfc_ndef.h:88
#define URI_ID_0x05_STRING
Definition: nfc_ndef.h:53
#define URI_ID_0x06_STRING
Definition: nfc_ndef.h:54
#define NFC_TEXT_MAX_LEN
Definition: nfc_ndef.h:87
#define URI_ID_0x0D
Definition: nfc_ndef.h:24
#define URI_ID_0x1E
Definition: nfc_ndef.h:41
#define URI_ID_0x0B
Definition: nfc_ndef.h:22
#define URI_ID_0x20
Definition: nfc_ndef.h:43
#define URI_ID_0x11_STRING
Definition: nfc_ndef.h:65
#define URI_ID_0x0C_STRING
Definition: nfc_ndef.h:60
#define URI_ID_0x0A
Definition: nfc_ndef.h:21
#define URI_ID_0x01
Definition: nfc_ndef.h:12
#define URI_ID_0x1C_STRING
Definition: nfc_ndef.h:76
#define URI_ID_0x03
Definition: nfc_ndef.h:14
uint8_t os_parse_ndef(uint8_t *in_buffer, ndef_struct_t *parsed)
#define URI_ID_0x1A_STRING
Definition: nfc_ndef.h:74
#define URI_ID_0x22
Definition: nfc_ndef.h:45
#define URI_ID_0x1D_STRING
Definition: nfc_ndef.h:77
#define URI_ID_0x10
Definition: nfc_ndef.h:27
#define URI_ID_0x1B_STRING
Definition: nfc_ndef.h:75
#define URI_ID_0x1B
Definition: nfc_ndef.h:38
#define URI_ID_0x0F_STRING
Definition: nfc_ndef.h:63
#define URI_ID_0x08_STRING
Definition: nfc_ndef.h:56
#define URI_ID_0x12
Definition: nfc_ndef.h:29
#define URI_ID_0x10_STRING
Definition: nfc_ndef.h:64
#define URI_ID_0x0D_STRING
Definition: nfc_ndef.h:61
#define URI_ID_0x0C
Definition: nfc_ndef.h:23
#define URI_ID_0x21
Definition: nfc_ndef.h:44
#define URI_ID_0x09_STRING
Definition: nfc_ndef.h:57
#define URI_ID_0x17_STRING
Definition: nfc_ndef.h:71
#define URI_ID_0x20_STRING
Definition: nfc_ndef.h:80
#define URI_ID_0x1F_STRING
Definition: nfc_ndef.h:79
#define URI_ID_0x13_STRING
Definition: nfc_ndef.h:67
#define URI_ID_0x1A
Definition: nfc_ndef.h:37
#define URI_ID_0x22_STRING
Definition: nfc_ndef.h:82
#define URI_ID_0x19_STRING
Definition: nfc_ndef.h:73
#define URI_ID_0x09
Definition: nfc_ndef.h:20
#define URI_ID_0x0E
Definition: nfc_ndef.h:25
#define NFC_NDEF_TYPE_URI
Definition: nfc_ndef.h:93
#define URI_ID_0x0F
Definition: nfc_ndef.h:26
#define URI_ID_0x1F
Definition: nfc_ndef.h:42
#define URI_ID_STRING_MAX_LEN
Definition: nfc_ndef.h:85
#define URI_ID_0x02_STRING
Definition: nfc_ndef.h:50
#define URI_ID_0x15_STRING
Definition: nfc_ndef.h:69
#define NFC_NDEF_TYPE_TEXT
Definition: nfc_ndef.h:92
#define URI_ID_0x0B_STRING
Definition: nfc_ndef.h:59
#define URI_ID_0x02
Definition: nfc_ndef.h:13
#define URI_ID_0x13
Definition: nfc_ndef.h:30
#define URI_ID_0x04
Definition: nfc_ndef.h:15
#define URI_ID_0x04_STRING
Definition: nfc_ndef.h:52
#define URI_ID_0x1C
Definition: nfc_ndef.h:39
uint16_t os_get_uri_header(uint8_t uri_id, char *uri_header)
#define URI_ID_0x19
Definition: nfc_ndef.h:36
#define URI_ID_0x14
Definition: nfc_ndef.h:31
#define URI_ID_0x16
Definition: nfc_ndef.h:33
#define NFC_NDEF_MAX_SIZE
Definition: nfc_ndef.h:90
#define URI_ID_0x23
Definition: nfc_ndef.h:46
#define URI_ID_0x03_STRING
Definition: nfc_ndef.h:51
#define URI_ID_0x1E_STRING
Definition: nfc_ndef.h:78
#define URI_ID_0x06
Definition: nfc_ndef.h:17
#define URI_ID_0x05
Definition: nfc_ndef.h:16
#define URI_ID_0x01_STRING
Definition: nfc_ndef.h:49
#define URI_ID_0x08
Definition: nfc_ndef.h:19
uint16_t os_ndef_to_string(ndef_struct_t *ndef_message, char *out_string)
#define URI_ID_0x16_STRING
Definition: nfc_ndef.h:70
unsigned short uint16_t
Definition: usbd_conf.h:54
unsigned char uint8_t
Definition: usbd_conf.h:53