RedFly.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. RedFly Lib for Arduino
  3. by Watterott electronic (www.watterott.com)
  4. */
  5. #include <inttypes.h>
  6. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  7. # include <avr/io.h>
  8. # include <avr/pgmspace.h>
  9. # include <util/delay.h>
  10. #else
  11. # include <chip.h>
  12. # include <itoa.h>
  13. #endif
  14. #if ARDUINO >= 100
  15. # include "Arduino.h"
  16. #else
  17. # include "WProgram.h"
  18. #endif
  19. #include "digitalWriteFast.h"
  20. #include "RedFlyCommands.h"
  21. #include "RedFly.h"
  22. #include "RedFlyClient.h"
  23. #include "RedFlyServer.h"
  24. #include "RedFlyNBNS.h"
  25. #define RX_PIN UART_RX_PIN //0
  26. #define TX_PIN UART_TX_PIN //1
  27. #define RST_PIN 2
  28. #define CS_PIN 3
  29. #define RST_DISABLE() digitalWriteFast(RST_PIN, HIGH)
  30. #define RST_ENABLE() digitalWriteFast(RST_PIN, LOW)
  31. #define CS_DISABLE() digitalWriteFast(CS_PIN, HIGH)
  32. #define CS_ENABLE() digitalWriteFast(CS_PIN, LOW)
  33. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  34. # if defined(UBRRH) && defined(UBRRL)
  35. # define _UCSRA_ UCSRA
  36. # define _RXC_ RXC
  37. # define _UCSRC_ UCSRC
  38. # define _USBS_ USBS
  39. # define _SERIAL_ Serial
  40. # elif defined(UBRR0H) && !defined(UBRR0L)
  41. # define _UCSRA_ UCSR1A
  42. # define _RXC_ RXC1
  43. # define _UCSRC_ UCSR1C
  44. # define _USBS_ USBS1
  45. # define _SERIAL_ Serial1
  46. # else
  47. # define _UCSRA_ UCSR0A
  48. # define _RXC_ RXC0
  49. # define _UCSRC_ UCSR0C
  50. # define _USBS_ USBS0
  51. # define _SERIAL_ Serial
  52. # endif
  53. #else
  54. # define _SERIAL_ Serial
  55. # define _USART_ USART0
  56. #endif
  57. REDFLY RedFly;
  58. //-------------------- Constructor/Destructor --------------------
  59. REDFLY::REDFLY(void)
  60. {
  61. return;
  62. }
  63. REDFLY::~REDFLY(void)
  64. {
  65. return;
  66. }
  67. //-------------------- Public --------------------
  68. uint8_t REDFLY::init(uint32_t br, uint8_t pwr)
  69. {
  70. uint8_t ret=0xFF, i;
  71. uint32_t ms;
  72. //init pins
  73. #ifdef CS_PIN
  74. pinMode(CS_PIN, OUTPUT);
  75. CS_DISABLE(); //deselect
  76. #endif
  77. #ifdef RST_PIN
  78. pinMode(RST_PIN, OUTPUT);
  79. RST_ENABLE(); //reset on
  80. #endif
  81. #ifdef TX_PIN
  82. pinMode(TX_PIN, OUTPUT);
  83. digitalWrite(TX_PIN, HIGH);
  84. #endif
  85. #ifdef RX_PIN
  86. pinMode(RX_PIN, INPUT);
  87. digitalWrite(RX_PIN, HIGH); //pull-up on
  88. #endif
  89. //reset vars
  90. memset(ipaddr, 0, sizeof(ipaddr));
  91. memset(buffer, 0, sizeof(buffer));
  92. //set serial port config
  93. baudrate = br;
  94. setbaudrate(br);
  95. //enable module communication
  96. enable();
  97. //reset module
  98. RST_ENABLE();
  99. delay_10ms(5); //wait 50ms
  100. RST_DISABLE();
  101. //auto baud rate detection
  102. delay_10ms(11); //wait 110ms for module boot-up
  103. for(i=4; i!=0; i--) //try 4 times
  104. {
  105. write(0x1C); //transmit 0x1C
  106. for(ms=millis(); (millis()-ms) < 200;) //wait 200ms for response
  107. {
  108. if(available())
  109. {
  110. if(read() == 0x55) //wait for 0x55
  111. {
  112. write(0x55); //transmit 0x55
  113. delay_10ms(10); //wait 100ms
  114. //skip firmware upgrade question at power on
  115. write('n');
  116. write('\n');
  117. ret = 0xFE;
  118. i = 1; //break 1st for loop
  119. break; //break 2nd for loop
  120. }
  121. }
  122. }
  123. }
  124. delay_10ms(20); //wait 200ms for booting
  125. //get firmware version and set config
  126. // if(ret == 0xFE)
  127. // {
  128. for(i=3; i!=0; i--) //try 3 times
  129. {
  130. flush();
  131. if(cmd(PSTR(CMD_FWVERSION)) == 0)
  132. {
  133. //cmd(PSTR(CMD_RESET)); //soft reset
  134. cmd(PSTR(CMD_BAND BAND24));
  135. cmd(PSTR(CMD_INIT));
  136. tx_power = pwr;
  137. ret = 0;
  138. break;
  139. }
  140. delay_10ms(10);
  141. }
  142. // }
  143. if(ret)
  144. {
  145. disable();
  146. }
  147. return ret;
  148. }
  149. uint8_t REDFLY::init(uint8_t pwr) { return init(REDFLY_BAUDRATE, pwr); }
  150. uint8_t REDFLY::init(void) { return init(REDFLY_BAUDRATE, HIGH_POWER); }
  151. void REDFLY::enable(void) //select module
  152. {
  153. flush(); //clear buffers
  154. setbaudrate(baudrate);
  155. read_state = 0;
  156. CS_ENABLE();
  157. return;
  158. }
  159. void REDFLY::disable(void) //deselect module
  160. {
  161. flush(); //clear buffers
  162. read_state = 0;
  163. CS_DISABLE();
  164. return;
  165. }
  166. uint8_t REDFLY::getversion(char *ver) //return module firmware version
  167. {
  168. uint8_t ret;
  169. for(uint8_t i=3; i!=0; i--) //try 3 times
  170. {
  171. memset(buffer, 0, sizeof(buffer));
  172. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_FWVERSION)); //OKa.b.c
  173. if(ret == 0)
  174. {
  175. //buffer[0] = 'O', buffer[1] = 'K'
  176. memcpy(&ver[0], &buffer[2], 5);
  177. ver[5] = 0;
  178. return 0;
  179. }
  180. }
  181. return ret;
  182. }
  183. uint8_t REDFLY::getmac(uint8_t *mac) //return module MAC address
  184. {
  185. uint8_t ret;
  186. for(uint8_t i=3; i!=0; i--) //try 3 times
  187. {
  188. memset(buffer, 0, sizeof(buffer));
  189. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_MAC)); //OKabcdef
  190. if(ret == 0)
  191. {
  192. memcpy(&mac[0], &buffer[2], 6); //buffer[0] = 'O', buffer[1] = 'K'
  193. return 0;
  194. }
  195. }
  196. return ret;
  197. }
  198. uint8_t REDFLY::getlocalip(uint8_t *ip) //return module IP address
  199. {
  200. if(ipaddr[0])
  201. {
  202. memcpy(ip, ipaddr, 4);
  203. return 0;
  204. }
  205. return 1;
  206. }
  207. uint8_t REDFLY::getip(char *host, uint8_t *ip) //return IP addr from host/domain
  208. {
  209. uint8_t ret;
  210. for(uint8_t i=3; i!=0; i--) //try 3 times
  211. {
  212. wdt_reset();
  213. memset(buffer, 0, sizeof(buffer));
  214. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_DNSGET), (uint8_t*)host, strlen(host)); //OKx...
  215. if(ret == 0)
  216. {
  217. if(buffer[2]) //IP received?
  218. {
  219. memcpy(&ip[0], &buffer[3], 4);
  220. return 0;
  221. }
  222. }
  223. }
  224. return ret;
  225. }
  226. #define NTP_PORT (123)
  227. #define NTP_PACKETLEN (48)
  228. #define NTP_FLAGOFFSET (0)
  229. #define NTP_TIMEOFFSET (40)
  230. uint32_t REDFLY::gettime(uint8_t *server, uint16_t port)
  231. {
  232. uint8_t buf[64]; //min. NTP_PACKETLEN
  233. uint32_t time=0UL, ms;
  234. uint8_t hNTP, sock, buf_len, *ptr;
  235. uint16_t rd, len;
  236. if(port == 0)
  237. {
  238. port = NTP_PORT;
  239. }
  240. //open connection to server
  241. hNTP = socketConnect(PROTO_UDP, server, port, port);
  242. if(hNTP != INVALID_SOCKET)
  243. {
  244. //send NTP request
  245. memset(buf, 0, NTP_PACKETLEN);
  246. buf[NTP_FLAGOFFSET] = (0<<6)|(1<<3)|(3<<0); //NTP flags: LI=0 | VN=1 | Mode=3 -> Client
  247. if(socketSend(hNTP, buf, NTP_PACKETLEN) == 0)
  248. {
  249. //get data
  250. ptr = buf;
  251. buf_len = 0;
  252. for(ms=millis(); (millis()-ms) < 3000;) //wait max. 3s
  253. {
  254. sock = hNTP;
  255. rd = socketRead(&sock, &len, ptr, sizeof(buf)-buf_len);
  256. if((rd != 0) && (rd != 0xFFFF)) //0xFFFF = connection closed
  257. {
  258. ptr += rd;
  259. buf_len += rd;
  260. }
  261. if(buf_len && (len == 0)) //all data received?
  262. {
  263. break;
  264. }
  265. }
  266. //check data
  267. if((buf_len >= NTP_PACKETLEN) && ((buf[NTP_FLAGOFFSET]&0x07) == 4)) //NTP flags: Mode=4 -> Server
  268. {
  269. //time = (uint32_t)*((uint32_t*)&buf[NTP_TIMEOFFSET]);
  270. time = (((uint32_t)buf[NTP_TIMEOFFSET+0])<<24)|
  271. (((uint32_t)buf[NTP_TIMEOFFSET+1])<<16)|
  272. (((uint32_t)buf[NTP_TIMEOFFSET+2])<< 8)|
  273. (((uint32_t)buf[NTP_TIMEOFFSET+3])<< 0); //swap32
  274. time -= 2208988800UL; //sub seconds 1900-1970
  275. }
  276. }
  277. socketClose(hNTP);
  278. }
  279. return time;
  280. }
  281. uint32_t REDFLY::gettime(uint8_t *server){ return gettime(server, 0); };
  282. uint8_t REDFLY::getrssi(void) //return signal strength for current connection
  283. {
  284. for(uint8_t i=3; i!=0; i--) //try 3 times
  285. {
  286. memset(buffer, 0, sizeof(buffer));
  287. if(cmd(buffer, sizeof(buffer), PSTR(CMD_RSSI)) == 0) //OKx
  288. {
  289. return buffer[2];
  290. }
  291. }
  292. return 0;
  293. }
  294. uint8_t REDFLY::getbssid(char *ssid, uint8_t *mac) //return SSID and MAC, call after scan()
  295. {
  296. uint8_t ret;
  297. memset(buffer, 0, sizeof(buffer));
  298. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_BSSID));
  299. if(ret == 0) //successful
  300. {
  301. //buffer[0] = 'O', buffer[1] = 'K'
  302. memcpy(ssid, &buffer[2], 32); //SSID
  303. ssid[32] = 0; //32+1
  304. memcpy(&mac[0], &buffer[34], 6); //MAC
  305. }
  306. else
  307. {
  308. ssid[0] = 0;
  309. }
  310. return ret;
  311. }
  312. uint8_t REDFLY::gettype(char *ssid, uint8_t *type) //return SSID and network type (0=Ad-hoc, 1=Infrastructure), call after scan()
  313. {
  314. uint8_t ret;
  315. memset(buffer, 0, sizeof(buffer));
  316. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_NWTYPE));
  317. if(ret == 0) //successful
  318. {
  319. //buffer[0] = 'O', buffer[1] = 'K'
  320. memcpy(ssid, &buffer[2], 32); //SSID
  321. ssid[32] = 0; //32+1
  322. *type = buffer[34]; //type
  323. }
  324. else
  325. {
  326. ssid[0] = 0;
  327. }
  328. return ret;
  329. }
  330. uint8_t REDFLY::scan(void)
  331. {
  332. cmd(PSTR(CMD_NUMSCAN "0")); //return all results on scan command
  333. return cmd(PSTR(CMD_SCAN "0"));
  334. }
  335. uint8_t REDFLY::scan(uint8_t chn, char *ssid, uint8_t *mode, uint8_t *rssi) //chn 0 = all
  336. {
  337. uint8_t ret, len;
  338. memset(buffer, 0, sizeof(buffer));
  339. cmd(PSTR(CMD_NUMSCAN "1")); //return only one result on scan command
  340. if(ssid[0] != 0) //scan for ssid
  341. {
  342. uitoa(chn, (char*)&buffer[0]); //chn
  343. len = strlen((char*)buffer);
  344. strcat_P((char*)&buffer[len], PSTR(",")); //,
  345. len = strlen((char*)buffer);
  346. strcat((char*)&buffer[len], ssid); //ssid
  347. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_SCAN), (char*)buffer);
  348. }
  349. else
  350. {
  351. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_SCAN), chn);
  352. }
  353. if(ret == 0) //scan successful
  354. {
  355. memcpy(ssid, &buffer[2], 32); //SSID
  356. ssid[32] = 0; //32+1
  357. if(mode)
  358. {
  359. *mode = buffer[34];
  360. }
  361. if(rssi)
  362. {
  363. *rssi = buffer[35];
  364. }
  365. }
  366. else
  367. {
  368. if(rssi)
  369. {
  370. *rssi = 0;
  371. }
  372. }
  373. return ret;
  374. }
  375. uint8_t REDFLY::scan(char *ssid, uint8_t *mode, uint8_t *rssi) { return scan(0, ssid, mode, rssi); }
  376. uint8_t REDFLY::scan(char *ssid) { return scan(0, ssid, 0, 0); }
  377. uint8_t REDFLY::nextscan(char *ssid, uint8_t *mode, uint8_t *rssi)
  378. {
  379. uint8_t ret;
  380. memset(buffer, 0, sizeof(buffer));
  381. ret = cmd(buffer, sizeof(buffer), PSTR(CMD_NEXTSCAN));
  382. if(ret == 0) //scan successful
  383. {
  384. memcpy(ssid, &buffer[2], 32); //SSID
  385. ssid[32] = 0; //32+1
  386. if(mode)
  387. {
  388. *mode = buffer[34];
  389. }
  390. if(rssi)
  391. {
  392. *rssi = buffer[35];
  393. }
  394. }
  395. else
  396. {
  397. if(rssi)
  398. {
  399. *rssi = 0;
  400. }
  401. }
  402. return ret;
  403. }
  404. uint8_t REDFLY::nextscan(char *ssid) { nextscan(ssid, 0, 0); }
  405. uint8_t REDFLY::join(char *ssid, char *key, uint8_t net, uint8_t chn, uint8_t authmode)
  406. {
  407. uint8_t ret=0;
  408. //network
  409. switch(net)
  410. {
  411. case INFRASTRUCTURE:
  412. cmd(PSTR(CMD_NETWORK "INFRASTRUCTURE"));
  413. break;
  414. case IBSS_JOINER:
  415. if(key)
  416. {
  417. cmd(PSTR(CMD_NETWORK "IBSS_SEC,0,0"));
  418. }
  419. else
  420. {
  421. cmd(PSTR(CMD_NETWORK "IBSS,0,0"));
  422. }
  423. break;
  424. case IBSS_CREATOR:
  425. if(key)
  426. {
  427. cmd(PSTR(CMD_NETWORK "IBSS_SEC,1,"), chn);
  428. }
  429. else
  430. {
  431. cmd(PSTR(CMD_NETWORK "IBSS,1,"), chn);
  432. }
  433. break;
  434. }
  435. //authentication mode
  436. if(authmode <= 4)
  437. {
  438. cmd(PSTR(CMD_AUTHMODE), authmode);
  439. }
  440. //key
  441. if(key)
  442. {
  443. cmd(PSTR(CMD_PSK), key);
  444. }
  445. //join
  446. switch(tx_power)
  447. {
  448. //auto data rate
  449. case LOW_POWER: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW)); break;
  450. case MED_POWER: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW)); break;
  451. case HIGH_POWER: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW)); break;
  452. //1Mbps
  453. case LOW_POWER_1M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW1M)); break;
  454. case MED_POWER_1M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW1M)); break;
  455. case HIGH_POWER_1M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW1M)); break;
  456. //2Mbps
  457. case LOW_POWER_2M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW2M)); break;
  458. case MED_POWER_2M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW2M)); break;
  459. case HIGH_POWER_2M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW2M)); break;
  460. //11Mbps
  461. case LOW_POWER_11M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW11M)); break;
  462. case MED_POWER_11M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW11M)); break;
  463. case HIGH_POWER_11M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW11M)); break;
  464. //12Mbps
  465. case LOW_POWER_12M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW12M)); break;
  466. case MED_POWER_12M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW12M)); break;
  467. case HIGH_POWER_12M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW12M)); break;
  468. //24Mbps
  469. case LOW_POWER_24M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW24M)); break;
  470. case MED_POWER_24M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW24M)); break;
  471. case HIGH_POWER_24M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW24M)); break;
  472. //54Mbps
  473. case LOW_POWER_54M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_LOWPW54M)); break;
  474. case MED_POWER_54M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_MEDPW54M)); break;
  475. case HIGH_POWER_54M: ret = cmd(PSTR(CMD_JOIN), ssid, PSTR(JOIN_HIGHPW54M)); break;
  476. }
  477. return ret;
  478. }
  479. uint8_t REDFLY::join(char *ssid, char *key, uint8_t net, uint8_t chn) { return join(ssid, key, net, chn, 0xFF); } //IBSS_CREATOR
  480. uint8_t REDFLY::join(char *ssid, uint8_t net, uint8_t chn) { return join(ssid, 0, net, chn, 0xFF); } //IBSS_CREATOR
  481. uint8_t REDFLY::join(char *ssid, char *key, uint8_t net) { return join(ssid, key, net, 0, 0xFF); } //INFRASTRUCTURE or IBSS_JOINER
  482. uint8_t REDFLY::join(char *ssid, uint8_t net) { return join(ssid, 0, net, 0, 0xFF); } //INFRASTRUCTURE or IBSS_JOINER
  483. uint8_t REDFLY::join(char *ssid, char *key) { return join(ssid, key, INFRASTRUCTURE, 0, 0xFF); } //INFRASTRUCTURE
  484. uint8_t REDFLY::join(char *ssid) { return join(ssid, 0, INFRASTRUCTURE, 0, 0xFF); } //INFRASTRUCTURE
  485. uint8_t REDFLY::disconnect(void)
  486. {
  487. socketReset();
  488. memset(ipaddr, 0, sizeof(ipaddr));
  489. return cmd(PSTR(CMD_DISCONN));
  490. }
  491. uint8_t REDFLY::begin(uint8_t dhcp, uint8_t *ip, uint8_t *dns, uint8_t *gateway, uint8_t *netmask)
  492. {
  493. uint8_t len;
  494. //reset sockets and IP addr
  495. socketReset();
  496. memset(ipaddr, 0, sizeof(ipaddr));
  497. memset(buffer, 0, sizeof(buffer));
  498. //dhcp or auto ip
  499. if(dhcp == 2) //Auto-IP
  500. {
  501. if(cmd(buffer, sizeof(buffer), PSTR(CMD_IPCONF IPCONF_AUTOIP)) == 0) //OKMACaddrIPaddrSUBNETGateway
  502. {
  503. memcpy(&ipaddr[0], &buffer[8], 4);
  504. return 0;
  505. }
  506. return 2;
  507. }
  508. else if(dhcp) //DHCP
  509. {
  510. if(cmd(buffer, sizeof(buffer), PSTR(CMD_IPCONF IPCONF_DHCP)) == 0) //OKMACaddrIPaddrSUBNETGateway
  511. {
  512. memcpy(&ipaddr[0], &buffer[8], 4);
  513. return 0;
  514. }
  515. return 1;
  516. }
  517. //static ip settings
  518. if(dns)
  519. {
  520. iptoa(dns, (char*)&buffer[0]);
  521. cmd(PSTR(CMD_DNSSERVER), (char*)buffer); //set DNS server (FW >= 4.3.0 required)
  522. }
  523. if(ip)
  524. {
  525. iptoa(ip, (char*)buffer);
  526. memcpy(ipaddr, ip, 4);
  527. }
  528. else
  529. {
  530. strcat_P((char*)buffer, PSTR("192.168.0.1"));
  531. ipaddr[0] = 192;
  532. ipaddr[1] = 168;
  533. ipaddr[2] = 0;
  534. ipaddr[3] = 1;
  535. }
  536. if(netmask)
  537. {
  538. strcat_P((char*)buffer, PSTR(","));
  539. len = strlen((char*)buffer);
  540. iptoa(netmask, (char*)&buffer[len]);
  541. }
  542. else
  543. {
  544. strcat_P((char*)buffer, PSTR(",255.255.255.0"));
  545. }
  546. if(gateway)
  547. {
  548. strcat_P((char*)buffer, PSTR(","));
  549. len = strlen((char*)buffer);
  550. iptoa(gateway, (char*)&buffer[len]);
  551. }
  552. return cmd(PSTR(CMD_IPCONF "0,"), (char*)buffer); //xxx.xxx.xxx.xxx,yyy.yyy.yyy.yyy,zzz.zzz.zzz.zzz
  553. }
  554. uint8_t REDFLY::begin(uint8_t *ip, uint8_t *dns, uint8_t *gateway, uint8_t *netmask) { return begin( 0, ip, dns, gateway, netmask); }
  555. uint8_t REDFLY::begin(uint8_t *ip, uint8_t *dns, uint8_t *gateway) { return begin( 0, ip, dns, gateway, 0); }
  556. uint8_t REDFLY::begin(uint8_t *ip, uint8_t *dns) { return begin( 0, ip, dns, 0, 0); }
  557. uint8_t REDFLY::begin(uint8_t *ip) { return begin( 0, ip, 0, 0, 0); }
  558. uint8_t REDFLY::begin(uint8_t dhcp) { return begin(dhcp, 0, 0, 0, 0); }
  559. uint8_t REDFLY::begin(void) { return begin( 1, 0, 0, 0, 0); }
  560. uint8_t REDFLY::socketConnect(uint8_t proto, uint8_t *ip, uint16_t port, uint16_t lport)
  561. {
  562. uint8_t ret=INVALID_SOCKET, len;
  563. //ip
  564. iptoa(ip, (char*)buffer);
  565. //port
  566. strcat_P((char*)buffer, PSTR(","));
  567. len = strlen((char*)buffer);
  568. uitoa(port, (char*)&buffer[len]);
  569. //local port
  570. strcat_P((char*)buffer, PSTR(","));
  571. len = strlen((char*)buffer);
  572. uitoa(lport, (char*)&buffer[len]);
  573. if(proto == PROTO_MCAST) //Multicast
  574. {
  575. proto = SOCKET_MCAST;
  576. if(cmd(buffer, 8, PSTR(CMD_MCAST), (char*)buffer) == 0) //xxx.xxx.xxx.xxx,aaaaa,bbbbb
  577. {
  578. ret = buffer[2]; //OKx
  579. }
  580. }
  581. else if(proto == PROTO_TCP) //TCP
  582. {
  583. proto = SOCKET_TCP;
  584. if(cmd(buffer, 8, PSTR(CMD_TCP), (char*)buffer) == 0) //xxx.xxx.xxx.xxx,aaaaa,bbbbb
  585. {
  586. ret = buffer[2]; //OKx
  587. }
  588. }
  589. else //UDP
  590. {
  591. proto = SOCKET_UDP;
  592. if(cmd(buffer, 8, PSTR(CMD_UDP), (char*)buffer) == 0) //xxx.xxx.xxx.xxx,aaaaa,bbbbb
  593. {
  594. ret = buffer[2]; //OKx
  595. }
  596. }
  597. if(ret != INVALID_SOCKET) //handle okay -> save socket handle and type
  598. {
  599. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  600. {
  601. if(socket_state[i].handle == INVALID_SOCKET)
  602. {
  603. socket_state[i].handle = ret;
  604. socket_state[i].state = proto;
  605. break;
  606. }
  607. }
  608. }
  609. return ret;
  610. }
  611. uint8_t REDFLY::socketConnect(uint8_t proto, uint8_t *ip, uint16_t port)
  612. {
  613. static uint16_t lport=1024;
  614. if(++lport > 2048)
  615. {
  616. lport = 1024;
  617. }
  618. return socketConnect(proto, ip, port, lport);
  619. }
  620. uint8_t REDFLY::socketListen(uint8_t proto, uint16_t lport)
  621. {
  622. uint8_t ret=INVALID_SOCKET;
  623. //local port
  624. uitoa(lport, (char*)&buffer[0]);
  625. if(proto == PROTO_TCP) //TCP
  626. {
  627. proto = SOCKET_TCP;
  628. if(cmd(buffer, sizeof(buffer), PSTR(CMD_LTCP), (char*)buffer) == 0)
  629. {
  630. ret = buffer[2]; //OKx
  631. }
  632. }
  633. else //UDP
  634. {
  635. proto = SOCKET_UDP;
  636. if(cmd(buffer, sizeof(buffer), PSTR(CMD_LUDP), (char*)buffer) == 0)
  637. {
  638. ret = buffer[2]; //OKx
  639. }
  640. }
  641. if(ret != INVALID_SOCKET) //handle okay -> save socket handle and type
  642. {
  643. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  644. {
  645. if(socket_state[i].handle == INVALID_SOCKET)
  646. {
  647. socket_state[i].handle = ret;
  648. socket_state[i].state = proto;
  649. break;
  650. }
  651. }
  652. }
  653. return ret;
  654. }
  655. uint8_t REDFLY::socketClose(uint8_t socket)
  656. {
  657. uint8_t ret=0;
  658. while(available()) //check for new data, if socket already closed?
  659. {
  660. uint8_t sock=INVALID_SOCKET;
  661. uint16_t len=0;
  662. socketRead(&sock, &len, 0, 0, 0, 0);
  663. if(sock == socket)
  664. {
  665. while(len) //clear buffer
  666. {
  667. uint8_t b[8];
  668. sock = socket;
  669. socketRead(&sock, &len, 0, 0, b, 8);
  670. }
  671. }
  672. else
  673. {
  674. break;
  675. }
  676. }
  677. //close socket if opened
  678. if(socket != INVALID_SOCKET)
  679. {
  680. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  681. {
  682. if(socket_state[i].handle == socket)
  683. {
  684. socket_state[i].handle = INVALID_SOCKET;
  685. socket_state[i].state = SOCKET_CLOSED;
  686. for(i=3; i!=0; i--) //try 3 times
  687. {
  688. ret = cmd(PSTR(CMD_CLS), socket);
  689. if((ret == 0) || (ret == 0xFE)) //(0xFE = socket already closed)
  690. {
  691. ret = 0;
  692. break;
  693. }
  694. }
  695. break;
  696. }
  697. }
  698. }
  699. return ret;
  700. }
  701. uint8_t REDFLY::socketClosed(uint8_t socket)
  702. {
  703. if(available()) //check for new data, if socket closed?
  704. {
  705. uint8_t sock=INVALID_SOCKET;
  706. uint16_t len=0;
  707. socketRead(&sock, &len, 0, 0, 0, 0);
  708. }
  709. if(socket != INVALID_SOCKET)
  710. {
  711. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  712. {
  713. if(socket_state[i].handle == socket) //socket found
  714. {
  715. return 0;
  716. }
  717. }
  718. }
  719. return 1;
  720. }
  721. uint8_t REDFLY::socketStatus(uint8_t socket)
  722. {
  723. if(available()) //check for new data, if socket closed?
  724. {
  725. uint8_t sock=INVALID_SOCKET;
  726. uint16_t len=0;
  727. socketRead(&sock, &len, 0, 0, 0, 0);
  728. }
  729. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  730. {
  731. if(socket_state[i].handle == socket)
  732. {
  733. return cmd(PSTR(CMD_CTCP), socket);
  734. }
  735. }
  736. return 0xFF;
  737. }
  738. uint8_t REDFLY::socketState(uint8_t socket)
  739. {
  740. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  741. {
  742. if(socket_state[i].handle == socket)
  743. {
  744. return socket_state[i].state;
  745. }
  746. }
  747. return SOCKET_CLOSED;
  748. }
  749. uint8_t REDFLY::socketSend(uint8_t socket, uint8_t *stream, uint16_t size, uint8_t *ip, uint16_t port)
  750. {
  751. uint8_t len;
  752. //socket
  753. uitoa(socket, (char*)buffer);
  754. //size
  755. strcat_P((char*)buffer, PSTR(","));
  756. len = strlen((char*)buffer);
  757. uitoa(size, (char*)&buffer[len]);
  758. //ip
  759. if(ip && (socketState(socket) == SOCKET_UDP))
  760. {
  761. strcat_P((char*)buffer, PSTR(","));
  762. len = strlen((char*)buffer);
  763. iptoa(ip, (char*)&buffer[len]);
  764. }
  765. else
  766. {
  767. strcat_P((char*)buffer, PSTR(",0"));
  768. }
  769. //port
  770. if(port && (socketState(socket) == SOCKET_UDP))
  771. {
  772. strcat_P((char*)buffer, PSTR(","));
  773. len = strlen((char*)buffer);
  774. uitoa(port, (char*)&buffer[len]);
  775. }
  776. else
  777. {
  778. strcat_P((char*)buffer, PSTR(",0"));
  779. }
  780. //data
  781. strcat_P((char*)buffer, PSTR(","));
  782. return cmd(PSTR(CMD_SEND), (char*)buffer, stream, size); //x,xxxx,xxx.xxx.xxx.xxx,xxxxx,
  783. }
  784. uint8_t REDFLY::socketSend(uint8_t socket, char *stream, uint8_t *ip, uint16_t port)
  785. {
  786. return socketSend(socket, (uint8_t*)stream, strlen(stream), ip, port);
  787. }
  788. uint8_t REDFLY::socketSendPGM(uint8_t socket, PGM_P stream, uint8_t *ip, uint16_t port)
  789. {
  790. uint8_t len;
  791. uint16_t size = strlen_P(stream);
  792. //socket
  793. uitoa(socket, (char*)buffer);
  794. //size
  795. strcat_P((char*)buffer, PSTR(","));
  796. len = strlen((char*)buffer);
  797. uitoa(size, (char*)&buffer[len]);
  798. //ip
  799. if(ip && (socketState(socket) == SOCKET_UDP))
  800. {
  801. strcat_P((char*)buffer, PSTR(","));
  802. len = strlen((char*)buffer);
  803. iptoa(ip, (char*)&buffer[len]);
  804. }
  805. else
  806. {
  807. strcat_P((char*)buffer, PSTR(",0"));
  808. }
  809. //port
  810. if(port && (socketState(socket) == SOCKET_UDP))
  811. {
  812. strcat_P((char*)buffer, PSTR(","));
  813. len = strlen((char*)buffer);
  814. uitoa(port, (char*)&buffer[len]);
  815. }
  816. else
  817. {
  818. strcat_P((char*)buffer, PSTR(",0"));
  819. }
  820. //data
  821. strcat_P((char*)buffer, PSTR(","));
  822. return cmd(PSTR(CMD_SEND), (char*)buffer, stream); //x,xxxx,xxx.xxx.xxx.xxx,xxxxx,
  823. }
  824. uint8_t REDFLY::socketSend(uint8_t socket, uint8_t *stream, uint16_t size)
  825. {
  826. return socketSend(socket, stream, size, 0, 0);
  827. }
  828. uint8_t REDFLY::socketSend(uint8_t socket, char *stream)
  829. {
  830. return socketSend(socket, (uint8_t*)stream, strlen(stream), 0, 0);
  831. }
  832. uint8_t REDFLY::socketSendPGM(uint8_t socket, PGM_P stream)
  833. {
  834. return socketSendPGM(socket, stream, 0, 0);
  835. }
  836. uint8_t REDFLY::socketSend(uint8_t socket, int val)
  837. {
  838. char buf[8];
  839. itoa(val, buf, 10);
  840. return socketSend(socket, (uint8_t*)buf, strlen(buf), 0, 0);
  841. }
  842. uint16_t REDFLY::socketRead(uint8_t *socket, uint16_t *len, uint8_t *ip, uint16_t *port, uint8_t *dst, uint16_t dst_size)
  843. {
  844. uint8_t stop=0;
  845. uint16_t rd=0;
  846. static uint8_t last_socket=INVALID_SOCKET;
  847. static uint16_t last_len=0;
  848. static uint8_t buf[8], pos=0;
  849. static uint16_t udp_port=0;
  850. static uint8_t udp_ip[4];
  851. if(read_state == 2) //we are currently reading data
  852. {
  853. if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket
  854. {
  855. return 0;
  856. }
  857. if(dst_size == 0) //only get len and socket
  858. {
  859. *socket = last_socket;
  860. *len = last_len;
  861. return 0;
  862. }
  863. }
  864. do
  865. {
  866. switch(read_state)
  867. {
  868. case 0: //restart searching
  869. read_state = 1;
  870. last_socket = INVALID_SOCKET;
  871. last_len = 0;
  872. pos = 0;
  873. udp_port = 0;
  874. udp_ip[0] = 0;
  875. udp_ip[1] = 0;
  876. udp_ip[2] = 0;
  877. udp_ip[3] = 0;
  878. case 1: //search
  879. while((pos<8) && available())
  880. {
  881. buf[pos++] = read();
  882. }
  883. if(pos >= 8)
  884. {
  885. if((buf[0] == 'R') && \
  886. (buf[1] == 'S') && \
  887. (buf[2] == 'I') && \
  888. (buf[3] == '_') && \
  889. (buf[4] == 'R') && \
  890. (buf[5] == 'E') && \
  891. (buf[6] == 'A') && \
  892. (buf[7] == 'D')) //RSI_READ
  893. {
  894. last_socket = readwait();
  895. last_len = readwait();
  896. last_len |= readwait()<<8;
  897. if((last_socket == INVALID_SOCKET) || (last_len == 0))
  898. {
  899. read_state = 0;
  900. last_socket = INVALID_SOCKET;
  901. last_len = 0;
  902. break;
  903. }
  904. read_state = 2;
  905. //get IP and port on UDP connection
  906. if(socketState(last_socket) == SOCKET_UDP)
  907. {
  908. udp_ip[0] = readwait();
  909. udp_ip[1] = readwait();
  910. udp_ip[2] = readwait();
  911. udp_ip[3] = readwait();
  912. udp_port = readwait();
  913. udp_port |= readwait()<<8;
  914. }
  915. if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket
  916. {
  917. return 0;
  918. }
  919. if(dst_size == 0)
  920. {
  921. stop = 1; //break loop
  922. }
  923. }
  924. else if((buf[0] == 'S') && \
  925. (buf[1] == 'I') && \
  926. (buf[2] == '_') && \
  927. (buf[3] == 'C') && \
  928. (buf[4] == 'L') && \
  929. (buf[5] == 'O') && \
  930. (buf[6] == 'S') && \
  931. (buf[7] == 'E')) //SI_CLOSE
  932. {
  933. last_socket = readwait();
  934. readwait(); readwait(); //trailing \r\n
  935. last_len = 0;
  936. rd = 0xFFFF;
  937. read_state = 0;
  938. stop = 1; //break loop
  939. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  940. {
  941. if(socket_state[i].handle == last_socket)
  942. {
  943. socket_state[i].handle = INVALID_SOCKET;
  944. socket_state[i].state = SOCKET_CLOSED;
  945. break;
  946. }
  947. }
  948. }
  949. else
  950. {
  951. //move one byte
  952. pos = 7;
  953. buf[0] = buf[1];
  954. buf[1] = buf[2];
  955. buf[2] = buf[3];
  956. buf[3] = buf[4];
  957. buf[4] = buf[5];
  958. buf[5] = buf[6];
  959. buf[6] = buf[7];
  960. }
  961. }
  962. break;
  963. case 2: //receive
  964. *dst = readwait();
  965. dst_size--;
  966. rd++;
  967. if(--last_len == 0)
  968. {
  969. readwait(); readwait(); //trailing \r\n
  970. read_state = 0;
  971. stop = 1; //break loop
  972. }
  973. else if(dst_size == 0)
  974. {
  975. stop = 1; //break loop
  976. }
  977. break;
  978. }
  979. }while(available() && (stop == 0));
  980. *socket = last_socket;
  981. *len = last_len;
  982. if(ip && udp_ip[0])
  983. {
  984. ip[0] = udp_ip[0];
  985. ip[1] = udp_ip[1];
  986. ip[2] = udp_ip[2];
  987. ip[3] = udp_ip[3];
  988. }
  989. if(port && udp_port)
  990. {
  991. *port = udp_port;
  992. }
  993. return rd;
  994. }
  995. uint16_t REDFLY::socketRead(uint8_t *socket, uint16_t *len, uint8_t *dst, uint16_t dst_size) //TCP connection
  996. {
  997. return socketRead(socket, len, 0, 0, dst, dst_size);
  998. }
  999. void REDFLY::socketReset(void)
  1000. {
  1001. read_state = 0;
  1002. for(uint8_t i=0; i<MAX_SOCKETS; i++)
  1003. {
  1004. socket_state[i].handle = INVALID_SOCKET;
  1005. socket_state[i].state = SOCKET_CLOSED;
  1006. }
  1007. flush();
  1008. return;
  1009. }
  1010. //-------------------- Private --------------------
  1011. uint8_t REDFLY::cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, char *v1, PGM_P p2, uint8_t *v2, uint16_t v2_size)
  1012. {
  1013. uint8_t c, i;
  1014. uint32_t ms;
  1015. uint32_t timeout;
  1016. uint8_t buf[8]; //ERRORx
  1017. if(read_state == 2) //currently receiving data?
  1018. {
  1019. return 0xFF;
  1020. }
  1021. else if(available()) //check for new data
  1022. {
  1023. uint8_t sock=INVALID_SOCKET;
  1024. uint16_t len=0;
  1025. socketRead(&sock, &len, 0, 0, 0, 0);
  1026. if(len != 0) //rx data found
  1027. {
  1028. return 0xFF;
  1029. }
  1030. }
  1031. //send p1 command
  1032. c = pgm_read_byte(p1++);
  1033. while(c != 0)
  1034. {
  1035. write(c);
  1036. c = pgm_read_byte(p1++);
  1037. }
  1038. //send v1 parameter 1
  1039. if(v1)
  1040. {
  1041. while(*v1)
  1042. {
  1043. write(*v1++);
  1044. }
  1045. }
  1046. //send p2 parameter 2
  1047. if(p2)
  1048. {
  1049. c = pgm_read_byte(p2++);
  1050. while(c != 0)
  1051. {
  1052. if(c == 0xDB) //0xDB -> 0xDB 0xDD
  1053. {
  1054. write(0xDB);
  1055. write(0xDD);
  1056. c = pgm_read_byte(p2++);
  1057. }
  1058. else if((c == 0x0D) && (pgm_read_byte(p2) == 0x0A)) //\r\n -> 0xDB 0xDC
  1059. {
  1060. write(0xDB);
  1061. write(0xDC);
  1062. p2++;
  1063. c = pgm_read_byte(p2++);
  1064. }
  1065. else
  1066. {
  1067. write(c);
  1068. c = pgm_read_byte(p2++);
  1069. }
  1070. }
  1071. }
  1072. //send v2 parameter 3
  1073. if(v2_size)
  1074. {
  1075. while(v2_size)
  1076. {
  1077. if(v2[0] == 0xDB) //0xDB -> 0xDB 0xDD
  1078. {
  1079. write(0xDB);
  1080. write(0xDD);
  1081. v2++; v2_size--;
  1082. }
  1083. else if((v2[0] == 0x0D) && (v2[1] == 0x0A) && (v2_size >= 2)) //\r\n -> 0xDB 0xDC
  1084. {
  1085. write(0xDB);
  1086. write(0xDC);
  1087. v2+=2; v2_size-=2;
  1088. }
  1089. else
  1090. {
  1091. write(*v2++);
  1092. v2_size--;
  1093. }
  1094. }
  1095. }
  1096. //flush rx and tx buffer
  1097. flush_nowait();
  1098. //send end characters of command
  1099. write('\r');
  1100. write('\n');
  1101. //read response
  1102. timeout = 10000; //default timeout: 10s
  1103. if(dst_size == 0) //dont save response
  1104. {
  1105. buf[0] = 0;
  1106. buf[5] = 0;
  1107. for(i=0, ms=millis(); (millis()-ms) < timeout;)
  1108. {
  1109. if(available())
  1110. {
  1111. c = read();
  1112. if(i < 8)
  1113. {
  1114. buf[i++] = c;
  1115. if((buf[0] != 'O') && (buf[0] != 'E')) //OK or ERROR
  1116. {
  1117. i = 0;
  1118. }
  1119. }
  1120. ms = millis();
  1121. timeout = 3; //3 ms timeout
  1122. }
  1123. }
  1124. }
  1125. else //save response to dst
  1126. {
  1127. dst[0] = 0;
  1128. dst[5] = 0;
  1129. for(i=0, ms=millis(); (millis()-ms) < timeout;)
  1130. {
  1131. if(available())
  1132. {
  1133. c = read();
  1134. if(i < dst_size)
  1135. {
  1136. dst[i++] = c;
  1137. if((dst[0] != 'O') && (dst[0] != 'E')) //OK or ERROR
  1138. {
  1139. i = 0;
  1140. }
  1141. }
  1142. ms = millis();
  1143. timeout = 3; //3 ms timeout
  1144. }
  1145. }
  1146. buf[0] = dst[0];
  1147. buf[1] = dst[1];
  1148. buf[5] = dst[5];
  1149. }
  1150. //check response
  1151. if((buf[0] == 'O') && (buf[1] == 'K'))
  1152. {
  1153. return 0; //OK
  1154. }
  1155. else if((buf[0] == 'E') && (buf[1] == 'R') && (buf[5] != 0) && (buf[5] != '\r'))
  1156. {
  1157. return buf[5]; //ERROR code
  1158. }
  1159. return 0xFF;
  1160. }
  1161. uint8_t REDFLY::cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, int16_t val)
  1162. {
  1163. char buf[8];
  1164. itoa(val, buf, 10);
  1165. return cmd(dst, dst_size, p1, buf, 0, 0, 0);
  1166. }
  1167. uint8_t REDFLY::cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, uint8_t *v2, uint16_t v2_size) { return cmd(dst, dst_size, p1, 0, 0, v2, v2_size); }
  1168. uint8_t REDFLY::cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, char *v1) { return cmd(dst, dst_size, p1, v1, 0, 0, 0); }
  1169. uint8_t REDFLY::cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1) { return cmd(dst, dst_size, p1, 0, 0, 0, 0); }
  1170. uint8_t REDFLY::cmd( PGM_P p1, char *v1, PGM_P p2) { return cmd( 0, 0, p1, v1, p2, 0, 0); }
  1171. uint8_t REDFLY::cmd( PGM_P p1, char *v1, uint8_t *v2, uint16_t v2_size) { return cmd( 0, 0, p1, v1, 0, v2, v2_size); }
  1172. uint8_t REDFLY::cmd( PGM_P p1, char *v1) { return cmd( 0, 0, p1, v1, 0, 0, 0); }
  1173. uint8_t REDFLY::cmd( PGM_P p1, int16_t v1) { return cmd( 0, 0, p1, v1 ); }
  1174. uint8_t REDFLY::cmd( PGM_P p1) { return cmd( 0, 0, p1, 0, 0, 0, 0); }
  1175. void REDFLY::flush(void)
  1176. {
  1177. uint32_t ms;
  1178. //clear tx buffer
  1179. _SERIAL_.flush();
  1180. //clear rx buffer
  1181. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  1182. for(ms=millis(); ((_UCSRA_&(1<<_RXC_)) || available()) && ((millis()-ms) < 50);) //50ms
  1183. #else
  1184. for(ms=millis(); available() && ((millis()-ms) < 50);) //50ms
  1185. #endif
  1186. {
  1187. read();
  1188. }
  1189. delay_10ms(1);
  1190. return;
  1191. }
  1192. void REDFLY::flush_nowait(void)
  1193. {
  1194. //clear tx buffer
  1195. _SERIAL_.flush();
  1196. //clear rx buffer
  1197. for(int len=available(); len!=0; len--)
  1198. {
  1199. read();
  1200. }
  1201. return;
  1202. }
  1203. int REDFLY::available(void)
  1204. {
  1205. return _SERIAL_.available();
  1206. }
  1207. uint8_t REDFLY::readwait(void) //serial read
  1208. {
  1209. while(!available());
  1210. return (uint8_t)_SERIAL_.read();
  1211. }
  1212. uint8_t REDFLY::read(void) //serial read
  1213. {
  1214. return _SERIAL_.read();
  1215. }
  1216. void REDFLY::write(uint8_t c) //serial write
  1217. {
  1218. _SERIAL_.write(c);
  1219. return;
  1220. }
  1221. void REDFLY::setbaudrate(uint32_t br) //set serial baudrate and config (8n2)
  1222. {
  1223. if(br < 9600)
  1224. {
  1225. br = 9600;
  1226. }
  1227. else if(br > 3686400)
  1228. {
  1229. br = 3686400;
  1230. }
  1231. _SERIAL_.begin(br);
  1232. //8 N 2
  1233. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  1234. _UCSRC_ |= (1<<_USBS_);
  1235. #else
  1236. _USART_->US_MR |= US_MR_NBSTOP_2_BIT;
  1237. //_USART_->US_MR = US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK | US_MR_CHRL_8_BIT | US_MR_PAR_NO | US_MR_NBSTOP_2_BIT | US_MR_CHMODE_NORMAL;
  1238. #endif
  1239. return;
  1240. }
  1241. char* REDFLY::iptoa(uint8_t *ip, char *s) //convert ip to string
  1242. {
  1243. sprintf_P(s, PSTR("%i.%i.%i.%i"), ip[0], ip[1], ip[2], ip[3]);
  1244. return s;
  1245. }
  1246. char* REDFLY::uitoa(uint16_t val, char *s) //convert unsigned int to string
  1247. {
  1248. sprintf_P(s, PSTR("%u"), val);
  1249. return s;
  1250. }
  1251. void REDFLY::delay_10ms(uint8_t ms) //delay of 10ms * x
  1252. {
  1253. for(; ms!=0; ms--)
  1254. {
  1255. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  1256. _delay_ms(10);
  1257. #else
  1258. delay(10);
  1259. #endif
  1260. }
  1261. return;
  1262. }