airkiss.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-05-16 heyuanjie87 first version
  9. */
  10. #include "airkiss.h"
  11. #include <string.h>
  12. #include <stdint.h>
  13. #ifdef AIRKISS_LOG_ENABLE
  14. #define AKLOG_D \
  15. if (lc->cfg && lc->cfg->printf) \
  16. lc->cfg->printf
  17. #else
  18. #define AKLOG_D(...)
  19. #endif
  20. #define AKSTATE_WFG 0
  21. #define AKSTATE_WFM 1
  22. #define AKSTATE_WFP 2
  23. #define AKSTATE_WFD 4
  24. #define AKSTATE_CMP 5
  25. typedef uint16_t akwire_seq_t;
  26. typedef struct
  27. {
  28. uint16_t val[6];
  29. uint8_t pos;
  30. uint8_t scnt; /* 成功计数 */
  31. uint8_t err;
  32. uint8_t rcnt; /* 接收计数 */
  33. } akcode_t;
  34. typedef struct
  35. {
  36. akwire_seq_t ws;
  37. uint8_t crc;
  38. uint8_t ind;
  39. }akdatf_seq_t;
  40. typedef struct
  41. {
  42. uint8_t crc;
  43. uint8_t ind;
  44. }akdatf_header_t;
  45. typedef struct
  46. {
  47. uint8_t id[4];
  48. }akaddr_t;
  49. typedef struct
  50. {
  51. akwire_seq_t ws;
  52. uint8_t val[4];
  53. uint8_t pos : 4;
  54. uint8_t scnt : 4;
  55. uint8_t err;
  56. uint8_t wval;
  57. uint8_t seqcnt;
  58. akaddr_t sa;
  59. } akcode_guide_t;
  60. typedef struct
  61. {
  62. char data[16]; /* 保留后4个 */
  63. uint8_t pos[16];
  64. }akdatf_conflict_t;
  65. typedef struct
  66. {
  67. union {
  68. akcode_guide_t code1[3];
  69. akcode_t code2[1];
  70. } uc;
  71. akdatf_conflict_t dcfl; /* 记录有冲突的数据 */
  72. akwire_seq_t prews;
  73. akdatf_seq_t preseq;
  74. akdatf_seq_t curseq;
  75. akdatf_header_t pendseq[10]; /* 未完成的序列 */
  76. akaddr_t locked;
  77. uint8_t seqstep;/* 序列增量 */
  78. uint8_t reclen;
  79. uint8_t state;
  80. uint8_t nossid;
  81. uint8_t seq[16]; /* 标记已完成的序列 */
  82. char data[66];
  83. uint8_t random;
  84. uint8_t baselen;
  85. uint8_t prslen;
  86. uint8_t ssidcrc;
  87. uint8_t pwdlen;
  88. const airkiss_config_t *cfg;
  89. } akloc_context_t;
  90. #define AKLOC_CODE1(x, i) ((x)->uc.code1[i])
  91. #define AKLOC_CODE2(x) (&(x)->uc.code2[0])
  92. #define AKLOC_DFSEQ_PREV(lc) ((lc)->preseq)
  93. #define AKLOC_DFSEQ_CUR(lc) ((lc)->curseq)
  94. unsigned char airkiss_crc8(unsigned char *message, unsigned char len)
  95. {
  96. uint8_t crc = 0;
  97. uint8_t i;
  98. while (len--)
  99. {
  100. crc ^= *message++;
  101. for (i = 0; i < 8; i++)
  102. {
  103. if (crc & 0x01)
  104. crc = (crc >> 1) ^ 0x8c;
  105. else
  106. crc >>= 1;
  107. }
  108. }
  109. return crc;
  110. }
  111. static akwire_seq_t akwseq_make(uint8_t seq[2])
  112. {
  113. akwire_seq_t ws = 0;
  114. ws = (seq[1] << 4) | (seq[0] >> 4);
  115. return ws;
  116. }
  117. static void akloc_reset(akloc_context_t *lc)
  118. {
  119. const airkiss_config_t *cfg;
  120. cfg = lc->cfg;
  121. memset(lc, 0, sizeof(*lc));
  122. lc->cfg = cfg;
  123. }
  124. static uint8_t akinfo_getu8(uint16_t v[2])
  125. {
  126. uint8_t ret = 0;
  127. ret = ((v[0] & 0xF) << 4) | (v[1] & 0xF);
  128. return ret;
  129. }
  130. static uint16_t aklen_udp(akloc_context_t *lc, uint16_t len)
  131. {
  132. return (len - lc->baselen);
  133. }
  134. static int ak_get_magicfield(akloc_context_t *lc, akcode_t *ac)
  135. {
  136. int ret = 1;
  137. if (ac->val[0] == 8)
  138. ac->val[0] = 0;
  139. lc->prslen = akinfo_getu8(&ac->val[0]);
  140. lc->ssidcrc = akinfo_getu8(&ac->val[2]);
  141. if (lc->prslen > (sizeof(lc->data) - 1))
  142. {
  143. ret = 0;
  144. AKLOG_D("prslen(%d) large than(%d)", lc->prslen, (sizeof(lc->data) - 1));
  145. }
  146. return ret;
  147. }
  148. static int ak_magicfield_input(akcode_t *ac, uint16_t len)
  149. {
  150. int mc;
  151. mc = len >> 4;
  152. if (mc == 0)
  153. {
  154. ac->val[0] = len;
  155. ac->pos = 1;
  156. }
  157. else if (mc == ac->pos)
  158. {
  159. ac->val[ac->pos] = len;
  160. ac->pos ++;
  161. }
  162. else
  163. {
  164. ac->pos = 0;
  165. }
  166. return (ac->pos == 4);
  167. }
  168. static int ak_get_prefixfield(akloc_context_t *lc, akcode_t *ac)
  169. {
  170. int ret;
  171. uint8_t crc;
  172. lc->pwdlen = akinfo_getu8(&ac->val[0]);
  173. crc = akinfo_getu8(&ac->val[2]);
  174. if (airkiss_crc8(&lc->pwdlen, 1) != crc)
  175. ret = 0;
  176. return ret;
  177. }
  178. static int ak_prefixfield_input(akcode_t *ac, uint16_t len)
  179. {
  180. int mc;
  181. mc = len >> 4;
  182. if (mc == 4)
  183. {
  184. ac->val[0] = len;
  185. ac->pos = 1;
  186. }
  187. else if (mc == (ac->pos + 4))
  188. {
  189. ac->val[ac->pos] = len;
  190. ac->pos ++;
  191. }
  192. else
  193. {
  194. ac->pos = 0;
  195. }
  196. return (ac->pos == 4);
  197. }
  198. static int ak_get_datafield(akloc_context_t *lc, akcode_t *ac)
  199. {
  200. uint8_t tmp[6];
  201. int n;
  202. int ret = 0;
  203. int pos;
  204. int seqi;
  205. seqi = ac->val[1] & 0x7f;
  206. if (seqi > (lc->prslen/4))
  207. {
  208. return 0;
  209. }
  210. if (lc->seq[seqi])
  211. return 0;
  212. pos = seqi * 4;
  213. n = lc->prslen - pos;
  214. if (n > 4)
  215. n = 4;
  216. tmp[0] = ac->val[0] & 0x7F;
  217. tmp[1] = ac->val[1] & 0x7F;
  218. tmp[2] = ac->val[2] & 0xFF;
  219. tmp[3] = ac->val[3] & 0xFF;
  220. tmp[4] = ac->val[4] & 0xFF;
  221. tmp[5] = ac->val[5] & 0xFF;
  222. ret = ((airkiss_crc8(&tmp[1], n + 1) & 0x7F) == tmp[0]);
  223. if (ret)
  224. {
  225. memcpy(&lc->data[pos], &tmp[2], n);
  226. lc->reclen += n;
  227. lc->seq[seqi] = 1;
  228. #ifdef AIRKISS_LOG_GDO_ENABLE
  229. AKLOG_D("getdata(%d, %d)\n", seqi, n);
  230. #endif
  231. }
  232. return ret;
  233. }
  234. static void akaddr_fromframe(akaddr_t *a, uint8_t *f)
  235. {
  236. f += 10;
  237. a->id[0] = f[4];
  238. a->id[1] = f[5];
  239. a->id[2] = f[10];
  240. a->id[3] = f[11];
  241. }
  242. static akcode_guide_t *ak_guide_getcode(akloc_context_t *lc, unsigned char *f)
  243. {
  244. akcode_guide_t *ac;
  245. if (f == NULL) /* 是模拟测试 */
  246. {
  247. ac = &AKLOC_CODE1(lc, 2);
  248. }
  249. else
  250. {
  251. akaddr_t sa;
  252. unsigned i;
  253. int found = 0;
  254. akcode_guide_t *imin;
  255. akaddr_fromframe(&sa, f);
  256. imin = &AKLOC_CODE1(lc, 0);
  257. ac = imin;
  258. for (i = 0; i < sizeof(lc->uc.code1) / sizeof(lc->uc.code1[0]); i++)
  259. {
  260. /* 匹配地址 */
  261. found = !memcmp(&sa, &ac->sa, sizeof(ac->sa));
  262. if (found)
  263. break;
  264. /* 记录权值最小的 */
  265. if (ac->wval < imin->wval)
  266. imin = ac;
  267. ac++;
  268. }
  269. if (!found)
  270. {
  271. /* 淘汰输入最少的 */
  272. ac = imin;
  273. ac->pos = 0;
  274. ac->err = 0;
  275. ac->scnt = 0;
  276. ac->wval = 0;
  277. ac->sa = sa;
  278. }
  279. }
  280. return ac;
  281. }
  282. static int ak_guidefield_input(akcode_guide_t *ac, uint8_t *f, uint16_t len)
  283. {
  284. akwire_seq_t ws = 0;
  285. if (f)
  286. ws = akwseq_make(f + 22);
  287. if (ac->pos < 4)
  288. {
  289. if ((ac->pos != 0) && ((len - ac->val[ac->pos - 1]) != 1))
  290. {
  291. ac->pos = 0;
  292. if (ac->wval > 0)
  293. ac->wval--;
  294. }
  295. if (ac->pos == 0)
  296. {
  297. ac->ws = ws;
  298. ac->seqcnt = 0;
  299. }
  300. ac->seqcnt += (ws - ac->ws);
  301. ac->val[ac->pos] = len;
  302. ac->pos++;
  303. ac->wval += ac->pos;
  304. }
  305. return (ac->pos == 4);
  306. }
  307. static int ak_waitfor_guidefield(akloc_context_t *lc, uint8_t *f, uint16_t len)
  308. {
  309. int ret = AIRKISS_STATUS_CONTINUE;
  310. akcode_guide_t *ac;
  311. ac = ak_guide_getcode(lc, f);
  312. if (ak_guidefield_input(ac, f, len))
  313. {
  314. ac->pos = 0;
  315. ac->scnt++;
  316. /* 至少两次相同的guide code才算获取成功 */
  317. if ((ac->scnt >= 2) && ac->wval >= 20)
  318. {
  319. lc->state = AKSTATE_WFM;
  320. lc->baselen = ac->val[0] - 1;
  321. lc->seqstep = ac->seqcnt/6;
  322. AKLOG_D("guide baselen(%d) seqstep(%d)\n", lc->baselen, lc->seqstep);
  323. }
  324. if (lc->state == AKSTATE_WFM)
  325. {
  326. lc->locked = ac->sa;
  327. memset(&lc->uc, 0, sizeof(lc->uc));
  328. ret = AIRKISS_STATUS_CHANNEL_LOCKED;
  329. }
  330. }
  331. return ret;
  332. }
  333. static int ak_waitfor_magicfield(akloc_context_t *lc, uint16_t len)
  334. {
  335. int ret = AIRKISS_STATUS_CONTINUE;
  336. akcode_t *ac = AKLOC_CODE2(lc);
  337. int udplen;
  338. udplen = aklen_udp(lc, len);
  339. if (ak_magicfield_input(ac, udplen))
  340. {
  341. ac->pos = 0;
  342. if (ak_get_magicfield(lc, ac))
  343. {
  344. lc->state = AKSTATE_WFP;
  345. AKLOG_D("magic: prslen(%d) ssidcrc(%X)\n", lc->prslen, lc->ssidcrc);
  346. }
  347. }
  348. if (ac->rcnt++ > 250)
  349. {
  350. akloc_reset(lc);
  351. AKLOG_D("reset from magic\n");
  352. }
  353. return ret;
  354. }
  355. static int ak_waitfor_prefixfield(akloc_context_t *lc, uint16_t len)
  356. {
  357. int ret = AIRKISS_STATUS_CONTINUE;
  358. akcode_t *ac = AKLOC_CODE2(lc);
  359. int udplen;
  360. udplen = aklen_udp(lc, len);
  361. if (ak_prefixfield_input(ac, udplen))
  362. {
  363. ac->pos = 0;
  364. if (ak_get_prefixfield(lc, ac))
  365. {
  366. lc->state = AKSTATE_WFD;
  367. AKLOG_D("prefix: pwdlen(%d)\n", lc->pwdlen);
  368. }
  369. }
  370. return ret;
  371. }
  372. #ifdef AIRKISS_LOG_DFDUMP_ENABLE
  373. static void akdata_dump(akloc_context_t *lc, uint8_t *f, uint16_t len)
  374. {
  375. uint8_t seq[2];
  376. uint16_t dseq;
  377. seq[0] = f[22];
  378. seq[1] = f[23];
  379. dseq = (seq[1] << 4) | (seq[0]>> 4);
  380. if (len & 0x100)
  381. {
  382. AKLOG_D("(%d) %X %c", dseq, len, len & 0xff);
  383. }
  384. else
  385. {
  386. AKLOG_D("(%d) %X", dseq, len);
  387. }
  388. }
  389. #endif
  390. /*
  391. 只判断密码和random是否收完
  392. */
  393. static int ak_is_pwdrand_complete(akloc_context_t *lc)
  394. {
  395. int ret = 0;
  396. unsigned i;
  397. int n = 0;
  398. for (i = 0; i < (sizeof(lc->seq) / sizeof(lc->seq[0])); i++)
  399. {
  400. if (lc->seq[i] == 0)
  401. break;
  402. n += 4;
  403. if (n >= (lc->pwdlen + 1))
  404. {
  405. ret = 1;
  406. break;
  407. }
  408. }
  409. return ret;
  410. }
  411. static int ak_datainput_onlylength(akloc_context_t *lc, akcode_t *ac, uint16_t len)
  412. {
  413. int n = 6;
  414. if (len & 0x100)
  415. {
  416. if (ac->pos > 1)
  417. {
  418. int size;
  419. ac->val[ac->pos] = len;
  420. ac->pos ++;
  421. size = (ac->val[1] & 0x7f) * 4;
  422. if (size < lc->prslen)
  423. {
  424. size = lc->prslen - size;
  425. if (size < 4) /* 最后一个包不足4 */
  426. {
  427. n = size + 2;
  428. }
  429. }
  430. }
  431. else
  432. {
  433. ac->pos = 0;
  434. }
  435. }
  436. else
  437. {
  438. if (ac->pos < 2)
  439. {
  440. ac->val[ac->pos] = len;
  441. ac->pos ++;
  442. }
  443. else
  444. {
  445. ac->val[0] = len;
  446. ac->pos = 1;
  447. }
  448. }
  449. return (ac->pos == n);
  450. }
  451. static akdatf_header_t* akseq_getpend(akloc_context_t *lc, uint8_t ind)
  452. {
  453. akdatf_header_t* ret = 0;
  454. unsigned i;
  455. for (i = 0; i < sizeof(lc->pendseq)/sizeof(lc->pendseq[0]); i ++)
  456. {
  457. akdatf_header_t *p = &lc->pendseq[i];
  458. if (p->ind == ind)
  459. {
  460. ret = p;
  461. break;
  462. }
  463. }
  464. return ret;
  465. }
  466. static int ak_pendinput_mark(akloc_context_t *lc, uint8_t ind)
  467. {
  468. int ret = 0;
  469. akdatf_header_t* pd;
  470. pd = akseq_getpend(lc, ind);
  471. if (pd)
  472. {
  473. int size, pos, i;
  474. char d[6] = {0};
  475. uint8_t crc;
  476. ind = ind & 0x7f;
  477. pos = ind * 4;
  478. size = lc->prslen - pos;
  479. if (size > 4)
  480. size = 4;
  481. for (i = 0; i < size; i ++)
  482. {
  483. if (lc->data[pos + i] == 0)
  484. return 0;
  485. }
  486. d[0] = ind;
  487. memcpy(&d[1], &lc->data[pos], size);
  488. crc = airkiss_crc8((uint8_t*)d, size + 1) & 0x7f;
  489. if (crc == (pd->crc & 0x7f))
  490. {
  491. memset(pd, 0, sizeof(*pd));
  492. lc->seq[ind] = 1;
  493. lc->reclen += size;
  494. ret = 1;
  495. #ifdef AIRKISS_LOG_GDO_ENABLE
  496. AKLOG_D("getdata-p(%d, %d)[%s]", ind, size, &d[1]);
  497. #endif
  498. }
  499. }
  500. return ret;
  501. }
  502. static int ak_penddata_getpos(akloc_context_t *lc, akdatf_seq_t *ref, akwire_seq_t ws)
  503. {
  504. int ret = -1;
  505. uint8_t refind, ind;
  506. int offs;
  507. if (ws < ref->ws)
  508. {//todo
  509. AKLOG_D("ws-d overflow(%d, %d)", ws, ref->ws);
  510. }
  511. else
  512. {
  513. int maxoffs;
  514. int fmpos;
  515. offs = (ws - ref->ws)/lc->seqstep;
  516. if ((offs % 6) < 2)
  517. return -1;
  518. maxoffs = lc->prslen + ((lc->prslen + 3)/4) * 2;
  519. if (offs > maxoffs) /* 相差太大出错几率增大 */
  520. return ret;
  521. refind = ref->ind & 0x7f;
  522. fmpos = refind * 6 + offs;
  523. fmpos = fmpos % maxoffs; /* 指向下一轮 */
  524. ind = fmpos/6;
  525. ret = ind * 4 + (fmpos % 6) - 2;
  526. }
  527. return ret;
  528. }
  529. static int ak_pendcrc_getpos(akloc_context_t *lc, akdatf_seq_t *ref, akwire_seq_t ws)
  530. {
  531. int offs;
  532. int pos = -1;
  533. int maxoffs;
  534. maxoffs = lc->prslen + ((lc->prslen + 3)/4) * 2;
  535. if (ws < ref->ws)
  536. {//todo
  537. AKLOG_D("ws-c overflow(%d, %d)", ws, ref->ws);
  538. }
  539. else
  540. {
  541. offs = (ws - ref->ws)/lc->seqstep;
  542. if (offs > maxoffs)
  543. return -1;
  544. offs = offs + (ref->ind & 0x7f) * 6;
  545. offs = offs % maxoffs;
  546. pos = (offs/6) | 0x80;
  547. }
  548. return pos;
  549. }
  550. static void ak_dataconflict_add(akloc_context_t *lc, uint8_t pos, uint8_t d, int mode)
  551. {
  552. unsigned i;
  553. int zi = -1;
  554. int s, e;
  555. pos ++;
  556. if (mode == 0)
  557. {
  558. s = 0;
  559. e = sizeof(lc->dcfl.pos) - 4;
  560. }
  561. else
  562. {
  563. s = sizeof(lc->dcfl.pos) - 4;
  564. e = sizeof(lc->dcfl.pos);
  565. }
  566. for (i = s; i < e; i ++)
  567. {
  568. if ((lc->dcfl.pos[i] == pos) && (lc->dcfl.data[i] == d))
  569. return;
  570. if (lc->dcfl.pos[i] == 0)
  571. zi = i;
  572. }
  573. if (zi >= 0)
  574. {
  575. lc->dcfl.data[zi] = d;
  576. lc->dcfl.pos[zi] = pos;
  577. }
  578. }
  579. static int ak_dataconflict_getchar(akloc_context_t *lc, uint8_t pos, uint8_t *cpos)
  580. {
  581. int ch = -1;
  582. uint8_t i;
  583. if (*cpos >= sizeof(lc->dcfl.pos))
  584. return -1;
  585. pos ++;
  586. for (i = *cpos; i < sizeof(lc->dcfl.pos); i ++)
  587. {
  588. if (lc->dcfl.pos[i] == pos)
  589. {
  590. ch = lc->dcfl.data[i];
  591. i ++;
  592. break;
  593. }
  594. }
  595. *cpos = i;
  596. return ch;
  597. }
  598. static void ak_dataconflict_clear(akloc_context_t *lc, int pos)
  599. {
  600. unsigned i;
  601. if (pos < 0)
  602. {
  603. i = sizeof(lc->dcfl.pos) - 4;
  604. for (; i < sizeof(lc->dcfl.pos); i ++)
  605. {
  606. lc->dcfl.pos[i] = 0;
  607. lc->dcfl.data[i] = 0;
  608. }
  609. }
  610. else
  611. {
  612. pos ++;
  613. for (i = 0; i < sizeof(lc->dcfl.pos) - 4; i ++)
  614. {
  615. if (lc->dcfl.pos[i] == pos)
  616. {
  617. lc->dcfl.pos[i] = 0;
  618. lc->dcfl.data[i] = 0;
  619. }
  620. }
  621. }
  622. }
  623. static int _dataconflict_crccheck(akloc_context_t *lc, akdatf_header_t* pd, uint8_t dpos, char *d, int size)
  624. {
  625. int ret = 0;
  626. uint8_t crc;
  627. crc = airkiss_crc8((uint8_t*)d, size + 1) & 0x7f;
  628. if (crc == (pd->crc & 0x7f))
  629. {
  630. int pos;
  631. pos = (pd->ind & 0x7f) * 4;
  632. memcpy(&lc->data[pos], &d[1], size);
  633. memset(pd, 0, sizeof(*pd));
  634. lc->seq[(uint8_t)d[0]] = 1;
  635. lc->reclen += size;
  636. ak_dataconflict_clear(lc, dpos);
  637. ret = 1;
  638. #ifdef AIRKISS_LOG_GDO_ENABLE
  639. AKLOG_D("getdata-c(%d, %d)[%s]", d[0], size, &d[1]);
  640. #endif
  641. }
  642. return ret;
  643. }
  644. static int ak_dataconflict_crccheck(akloc_context_t *lc, akdatf_header_t* pd, int size)
  645. {
  646. char d[6] = {0};
  647. uint8_t spos;
  648. uint8_t cflpos0 = 0, cflpos1 = 0, cflpos2 = 0, cflpos3 = 0;
  649. int i;
  650. d[0] = pd->ind & 0x7f;
  651. spos = d[0] * 4;
  652. /* 把所有冲突的数据都校验一遍 */
  653. for (i = 0; i < size; i ++)
  654. {
  655. ak_dataconflict_add(lc, spos + i, lc->data[spos + i], 1);
  656. }
  657. while (size > 0)
  658. {
  659. int ch;
  660. ch = ak_dataconflict_getchar(lc, spos + 0, &cflpos0);
  661. if (ch < 0)
  662. break;
  663. d[1] = ch;
  664. cflpos1 = 0;
  665. while (size > 1)
  666. {
  667. int ch;
  668. ch = ak_dataconflict_getchar(lc, spos + 1, &cflpos1);
  669. if (ch < 0)
  670. break;
  671. d[2] = ch;
  672. cflpos2 = 0;
  673. while (size > 2)
  674. {
  675. int ch;
  676. ch = ak_dataconflict_getchar(lc, spos + 2, &cflpos2);
  677. if (ch < 0)
  678. break;
  679. d[3] = ch;
  680. cflpos3 = 0;
  681. while (size > 3)
  682. {
  683. int ch;
  684. ch = ak_dataconflict_getchar(lc, spos + 3, &cflpos3);
  685. if (ch < 0)
  686. break;
  687. d[4] = ch;
  688. if (_dataconflict_crccheck(lc, pd, spos + 3, d, size))
  689. {
  690. goto _out;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. _out:
  697. ak_dataconflict_clear(lc, -1);
  698. return 0;
  699. }
  700. static int ak_dataconflict_input(akloc_context_t *lc, uint8_t ind, uint8_t pos, uint8_t data)
  701. {
  702. int ret = 0;
  703. int i;
  704. int size;
  705. int spos;
  706. akdatf_header_t* pd;
  707. spos = ind * 4;
  708. size = lc->prslen - spos;
  709. if (size > 4)
  710. size = 4;
  711. ak_dataconflict_add(lc, pos, data, 0);
  712. /* 检查接收是否足够 */
  713. for (i = 0; i < size; i ++)
  714. {
  715. if (lc->data[spos + i] == 0)
  716. {
  717. return 0;
  718. }
  719. }
  720. /* 查找包头 */
  721. pd = akseq_getpend(lc, ind | 0x80);
  722. if (!pd)
  723. return 0;
  724. ret = ak_dataconflict_crccheck(lc, pd, size);
  725. return ret;
  726. }
  727. static int ak_databody_input(akloc_context_t *lc, akdatf_seq_t *ref, akwire_seq_t ws, uint8_t data)
  728. {
  729. int pos;
  730. uint8_t ind;
  731. uint8_t dif;
  732. /* 与上一个帧相差太大则不接受 */
  733. dif = (ws - lc->prews)/lc->seqstep;
  734. if (dif > 5)
  735. return 0;
  736. pos = ak_penddata_getpos(lc, ref, ws);
  737. if (pos < 0)
  738. {
  739. return 0;
  740. }
  741. ind = pos / 4;
  742. if (lc->seq[ind])
  743. {
  744. return 0;
  745. }
  746. #if AIRKISS_LOG_RIO_ENABLE
  747. AKLOG_D("ref(%d %X) input(%d) %c", ref->ws, ref->ind, pos, data);
  748. #endif
  749. if (lc->data[pos] == 0)
  750. {
  751. lc->data[pos] = data;
  752. ak_pendinput_mark(lc, ind | 0x80);
  753. }
  754. else if (lc->data[pos] != data)
  755. {
  756. /* 出现数据冲突 */
  757. ak_dataconflict_input(lc, ind, (uint8_t)pos, data);
  758. }
  759. return 1;
  760. }
  761. static void akseq_allocpend(akloc_context_t *lc, uint8_t crc, uint8_t ind)
  762. {
  763. akdatf_header_t *found = 0, *idle = 0;
  764. unsigned i;
  765. if (lc->seq[ind & 0x7f])
  766. return;
  767. AKLOG_D("{%X %X}", crc, ind);
  768. idle = &lc->pendseq[0];
  769. for (i = 0; i < sizeof(lc->pendseq)/sizeof(lc->pendseq[0]); i ++)
  770. {
  771. akdatf_header_t *p = &lc->pendseq[i];
  772. if (p->ind == ind)
  773. {
  774. found = p;
  775. p->crc = crc;
  776. break;
  777. }
  778. if (p->crc == 0)
  779. idle = p;
  780. }
  781. if (found == NULL)
  782. {
  783. found = idle;
  784. found->crc = crc;
  785. found->ind = ind;
  786. }
  787. }
  788. static void ak_datahead_input(akloc_context_t *lc, akdatf_seq_t *cur, akwire_seq_t ws, uint8_t head)
  789. {
  790. int seqmax;
  791. uint8_t dif;
  792. seqmax = (lc->prslen/4) | 0x80;
  793. if (cur->crc != 0)
  794. {
  795. dif = (ws - cur->ws)/lc->seqstep;
  796. if (head <= seqmax)
  797. {
  798. cur->ind = head;
  799. cur->ws = ws - lc->seqstep;
  800. AKLOC_DFSEQ_PREV(lc) = *cur;
  801. if (dif < 3)
  802. {
  803. /* 暂存包头 */
  804. akseq_allocpend(lc, cur->crc, cur->ind);
  805. }
  806. }
  807. if (head > seqmax)
  808. {
  809. cur->crc = head;
  810. cur->ind = 0;
  811. cur->ws = ws;
  812. }
  813. }
  814. else
  815. {
  816. if (head > seqmax) //很大几率是crc
  817. {
  818. cur->crc = head;
  819. cur->ws = ws;
  820. cur->ind = 0;
  821. }
  822. else if (ak_pendcrc_getpos(lc, cur, ws) == head)
  823. {
  824. /* 没收到crc */
  825. cur->ind = head;
  826. cur->ws = ws - lc->seqstep; /* 设置crc的帧序号 */
  827. }
  828. }
  829. }
  830. static int ak_datainput_withwireseq(akloc_context_t *lc, uint8_t *f, uint16_t len)
  831. {
  832. akwire_seq_t ws;
  833. akdatf_seq_t *cur;
  834. ws = akwseq_make(f + 22);
  835. cur = &AKLOC_DFSEQ_CUR(lc);
  836. if (len & 0x100) /* 输入数据 */
  837. {
  838. akdatf_seq_t *ref;
  839. ref = &AKLOC_DFSEQ_PREV(lc);
  840. if ((cur->ind == 0) && (cur->crc != 0))
  841. {
  842. int pos;
  843. /* 如果只收到了crc就根据前一个包推测一个序号 */
  844. pos = ak_pendcrc_getpos(lc, ref, ws);
  845. if (pos > 0)
  846. {
  847. cur->ind = (uint8_t)pos;
  848. akseq_allocpend(lc, cur->crc, cur->ind);
  849. }
  850. }
  851. if (cur->ind)
  852. {
  853. if (!ak_databody_input(lc, cur, ws, len))
  854. {
  855. memset(&AKLOC_DFSEQ_CUR(lc), 0 , sizeof(*cur));
  856. }
  857. if (lc->reclen == lc->prslen)
  858. {
  859. lc->state = AKSTATE_CMP;
  860. }
  861. }
  862. AKLOC_DFSEQ_CUR(lc).crc = 0;/* 标记已收到数据 */
  863. }
  864. else
  865. {
  866. /* 输入包头 */
  867. ak_datahead_input(lc, cur, ws, len);
  868. }
  869. lc->prews = ws;
  870. return 0;
  871. }
  872. static int ak_waitfor_datafield(akloc_context_t *lc, uint8_t *f, uint16_t len, int nossid)
  873. {
  874. int ret = AIRKISS_STATUS_CONTINUE;
  875. akcode_t *ac = AKLOC_CODE2(lc);
  876. uint16_t udplen;
  877. udplen = aklen_udp(lc, len);
  878. if (udplen < 0x80)
  879. {
  880. return ret;
  881. }
  882. #ifdef AIRKISS_LOG_DFDUMP_ENABLE
  883. if (f)
  884. {
  885. akdata_dump(lc, f, udplen);
  886. }
  887. #endif
  888. if (ak_datainput_onlylength(lc, ac, udplen))
  889. {
  890. ac->pos = 0;
  891. ak_get_datafield(lc, ac);
  892. if (lc->reclen == lc->prslen)
  893. {
  894. lc->state = AKSTATE_CMP;
  895. goto _out;
  896. }
  897. }
  898. if (f)
  899. {
  900. ak_datainput_withwireseq(lc, f, udplen);
  901. }
  902. if (nossid && ak_is_pwdrand_complete(lc))
  903. {
  904. lc->state = AKSTATE_CMP;
  905. AKLOG_D("data complete nossid\n");
  906. }
  907. _out:
  908. if (lc->state == AKSTATE_CMP)
  909. {
  910. lc->nossid = nossid;
  911. ret = AIRKISS_STATUS_COMPLETE;
  912. }
  913. return ret;
  914. }
  915. static int ak_sa_filter(akloc_context_t *lc, uint8_t *f)
  916. {
  917. int ret = 0;
  918. if (lc->state != AKSTATE_WFG)
  919. {
  920. akaddr_t sa;
  921. akaddr_fromframe(&sa, f);
  922. ret = memcmp(&lc->locked, &sa, sizeof(sa));
  923. }
  924. return ret;
  925. }
  926. int airkiss_filter(const void *f, int len)
  927. {
  928. int ret = 0;
  929. unsigned char *da, *p;
  930. int i;
  931. p = (unsigned char *)f;
  932. if ((len < 25) || (p[0] != 0x08))
  933. return 1;
  934. da = p + 4;
  935. for (i = 0; i < 6; i++)
  936. {
  937. if (da[i] != 0xFF)
  938. {
  939. ret = 1;
  940. break;
  941. }
  942. }
  943. return ret;
  944. }
  945. static int _ak_recv(airkiss_context_t *c, const void *frame, uint16_t length, int nossid)
  946. {
  947. int ret = AIRKISS_STATUS_CONTINUE;
  948. akloc_context_t *lc = (akloc_context_t *)c;
  949. unsigned char *f = (unsigned char *)frame;
  950. if (frame != NULL) /* 模拟测试时可只传length */
  951. {
  952. if (airkiss_filter(frame, length))
  953. return ret;
  954. if (ak_sa_filter(lc, f))
  955. return ret;
  956. }
  957. switch (lc->state)
  958. {
  959. case AKSTATE_WFG:
  960. {
  961. ret = ak_waitfor_guidefield(lc, f, length);
  962. }
  963. break;
  964. case AKSTATE_WFM:
  965. {
  966. ret = ak_waitfor_magicfield(lc, length);
  967. }
  968. break;
  969. case AKSTATE_WFP:
  970. {
  971. ret = ak_waitfor_prefixfield(lc, length);
  972. }
  973. break;
  974. case AKSTATE_WFD:
  975. {
  976. ret = ak_waitfor_datafield(lc, f, length, nossid);
  977. }
  978. break;
  979. case AKSTATE_CMP:
  980. {
  981. ret = AIRKISS_STATUS_COMPLETE;
  982. }
  983. break;
  984. }
  985. return ret;
  986. }
  987. const char *airkiss_version(void)
  988. {
  989. return "airkiss-1.0.0-open";
  990. }
  991. int airkiss_init(airkiss_context_t *c, const airkiss_config_t *config)
  992. {
  993. akloc_context_t *lc = (akloc_context_t *)c;
  994. lc->cfg = config;
  995. akloc_reset(lc);
  996. return 0;
  997. }
  998. int airkiss_recv(airkiss_context_t *c, const void *frame, unsigned short length)
  999. {
  1000. return _ak_recv(c, frame, length, 0);
  1001. }
  1002. int airkiss_get_result(airkiss_context_t *c, airkiss_result_t *res)
  1003. {
  1004. akloc_context_t *lc = (akloc_context_t *)c;
  1005. if (lc->state != AKSTATE_CMP)
  1006. return -1;
  1007. res->pwd = (char *)&lc->data[0];
  1008. res->pwd_length = lc->pwdlen;
  1009. if (lc->data[lc->pwdlen] == 0)
  1010. {
  1011. res->random = lc->random;
  1012. }
  1013. else
  1014. {
  1015. res->random = lc->data[lc->pwdlen];
  1016. lc->random = lc->data[lc->pwdlen];
  1017. lc->data[lc->pwdlen] = 0;
  1018. }
  1019. res->ssid_crc = lc->ssidcrc;
  1020. if (lc->nossid)
  1021. {
  1022. res->ssid = "";
  1023. res->ssid_length = 0;
  1024. }
  1025. else
  1026. {
  1027. res->ssid = (char *)&lc->data[lc->pwdlen + 1];
  1028. res->ssid_length = lc->prslen - lc->pwdlen - 1;
  1029. }
  1030. lc->data[lc->prslen] = 0;
  1031. return 0;
  1032. }
  1033. int airkiss_recv_nossid(airkiss_context_t *c, const void *frame, unsigned short length)
  1034. {
  1035. return _ak_recv(c, frame, length, 1);
  1036. }
  1037. int airkiss_change_channel(airkiss_context_t *c)
  1038. {
  1039. akloc_context_t *lc = (akloc_context_t *)c;
  1040. akloc_reset(lc);
  1041. return 0;
  1042. }