Blame view

PN532/llcp.cpp 6.43 KB
1a2e5ee4   henyxia   Big revision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  
  #include "llcp.h"
  #include "PN532_debug.h"
  
  // LLCP PDU Type Values
  #define PDU_SYMM    0x00
  #define PDU_PAX     0x01
  #define PDU_CONNECT 0x04
  #define PDU_DISC    0x05
  #define PDU_CC      0x06
  #define PDU_DM      0x07
  #define PDU_I       0x0c
  #define PDU_RR      0x0d
  
  uint8_t LLCP::SYMM_PDU[2] = {0, 0};
  
  inline uint8_t getPType(const uint8_t *buf)
  {
      return ((buf[0] & 0x3) << 2) + (buf[1] >> 6);
  }
  
  inline uint8_t getSSAP(const uint8_t *buf)
  {
      return  buf[1] & 0x3f;
  }
  
  inline uint8_t getDSAP(const uint8_t *buf)
  {
      return buf[0] >> 2;
  }
  
  int8_t LLCP::activate(uint16_t timeout)
  {
      return link.activateAsTarget(timeout);
  }
  
  int8_t LLCP::waitForConnection(uint16_t timeout)
  {
      uint8_t type;
  
      mode = 1;
      ns = 0;
      nr = 0;
  
      // Get CONNECT PDU
      DMSG("wait for a CONNECT PDU\n");
      do {
          if (2 > link.read(headerBuf, headerBufLen)) {
              return -1;
          }
  
          type = getPType(headerBuf);
          if (PDU_CONNECT == type) {
              break;
          } else if (PDU_SYMM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return -2;
              }
          } else {
              return -3;
          }
  
      } while (1);
  
      // Put CC PDU
      DMSG("put a CC(Connection Complete) PDU to response the CONNECT PDU\n");
      ssap = getDSAP(headerBuf);
      dsap = getSSAP(headerBuf);
      headerBuf[0] = (dsap << 2) + ((PDU_CC >> 2) & 0x3);
      headerBuf[1] = ((PDU_CC & 0x3) << 6) + ssap;
      if (!link.write(headerBuf, 2)) {
          return -2;
      }
  
      return 1;
  }
  
  int8_t LLCP::waitForDisconnection(uint16_t timeout)
  {
      uint8_t type;
  
      // Get DISC PDU
      DMSG("wait for a DISC PDU\n");
      do {
          if (2 > link.read(headerBuf, headerBufLen)) {
              return -1;
          }
  
          type = getPType(headerBuf);
          if (PDU_DISC == type) {
              break;
          } else if (PDU_SYMM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return -2;
              }
          } else {
              return -3;
          }
  
      } while (1);
  
      // Put DM PDU
      DMSG("put a DM(Disconnect Mode) PDU to response the DISC PDU\n");
      // ssap = getDSAP(headerBuf);
      // dsap = getSSAP(headerBuf);
      headerBuf[0] = (dsap << 2) + (PDU_DM >> 2);
      headerBuf[1] = ((PDU_DM & 0x3) << 6) + ssap;
      if (!link.write(headerBuf, 2)) {
          return -2;
      }
  
      return 1;
  }
  
  int8_t LLCP::connect(uint16_t timeout)
  {
      uint8_t type;
  
      mode = 0;
      dsap = LLCP_DEFAULT_DSAP;
      ssap = LLCP_DEFAULT_SSAP;
      ns = 0;
      nr = 0;
  
      // try to get a SYMM PDU
      if (2 > link.read(headerBuf, headerBufLen)) {
          return -1;
      }
      type = getPType(headerBuf);
      if (PDU_SYMM != type) {
          return -1;
      }
  
      // put a CONNECT PDU
      headerBuf[0] = (LLCP_DEFAULT_DSAP << 2) + (PDU_CONNECT >> 2);
      headerBuf[1] = ((PDU_CONNECT & 0x03) << 6) + LLCP_DEFAULT_SSAP;
      uint8_t body[] = "  urn:nfc:sn:snep";
      body[0] = 0x06;
      body[1] = sizeof(body) - 2 - 1;
      if (!link.write(headerBuf, 2, body, sizeof(body) - 1)) {
          return -2;
      }
  
      // wait for a CC PDU
      DMSG("wait for a CC PDU\n");
      do {
          if (2 > link.read(headerBuf, headerBufLen)) {
              return -1;
          }
  
          type = getPType(headerBuf);
          if (PDU_CC == type) {
              break;
          } else if (PDU_SYMM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return -2;
              }
          } else {
              return -3;
          }
  
      } while (1);
  
      return 1;
  }
  
  int8_t LLCP::disconnect(uint16_t timeout)
  {
      uint8_t type;
  
      // try to get a SYMM PDU
      if (2 > link.read(headerBuf, headerBufLen)) {
          return -1;
      }
      type = getPType(headerBuf);
      if (PDU_SYMM != type) {
          return -1;
      }
  
      // put a DISC PDU
      headerBuf[0] = (LLCP_DEFAULT_DSAP << 2) + (PDU_DISC >> 2);
      headerBuf[1] = ((PDU_DISC & 0x03) << 6) + LLCP_DEFAULT_SSAP;
      if (!link.write(headerBuf, 2)) {
          return -2;
      }
  
      // wait for a DM PDU
      DMSG("wait for a DM PDU\n");
      do {
          if (2 > link.read(headerBuf, headerBufLen)) {
              return -1;
          }
  
          type = getPType(headerBuf);
          if (PDU_CC == type) {
              break;
          } else if (PDU_DM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return -2;
              }
          } else {
              return -3;
          }
  
      } while (1);
  
      return 1;
  }
  
  bool LLCP::write(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
  {
      uint8_t type;
      uint8_t buf[3];
  
      if (mode) {
          // Get a SYMM PDU
          if (2 != link.read(buf, sizeof(buf))) {
              return false;
          }
      }
  
      if (headerBufLen < (hlen + 3)) {
          return false;
      }
  
      for (int8_t i = hlen - 1; i >= 0; i--) {
          headerBuf[i + 3] = header[i];
      }
  
      headerBuf[0] = (dsap << 2) + (PDU_I >> 2);
      headerBuf[1] = ((PDU_I & 0x3) << 6) + ssap;
      headerBuf[2] = (ns << 4) + nr;
      if (!link.write(headerBuf, 3 + hlen, body, blen)) {
          return false;
      }
  
      ns++;
  
      // Get a RR PDU
      int16_t status;
      do {
          status = link.read(headerBuf, headerBufLen);
          if (2 > status) {
              return false;
          }
  
          type = getPType(headerBuf);
          if (PDU_RR == type) {
              break;
          } else if (PDU_SYMM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return false;
              }
          } else {
              return false;
          }
      } while (1);
  
      if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
          return false;
      }
  
      return true;
  }
  
  int16_t LLCP::read(uint8_t *buf, uint8_t length)
  {
      uint8_t type;
      uint16_t status;
  
      // Get INFO PDU
      do {
          status = link.read(buf, length);
          if (2 > status) {
              return -1;
          }
  
          type = getPType(buf);
          if (PDU_I == type) {
              break;
          } else if (PDU_SYMM == type) {
              if (!link.write(SYMM_PDU, sizeof(SYMM_PDU))) {
                  return -2;
              }
          } else {
              return -3;
          }
  
      } while (1);
  
      uint8_t len = status - 3;
      ssap = getDSAP(buf);
      dsap = getSSAP(buf);
  
      headerBuf[0] = (dsap << 2) + (PDU_RR >> 2);
      headerBuf[1] = ((PDU_RR & 0x3) << 6) + ssap;
      headerBuf[2] = (buf[2] >> 4) + 1;
      if (!link.write(headerBuf, 3)) {
          return -2;
      }
  
      for (uint8_t i = 0; i < len; i++) {
          buf[i] = buf[i + 3];
      }
  
      nr++;
  
      return len;
  }