airkiss.c 26 KB

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