RedFly.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef REDFLY_h
  2. #define REDFLY_h
  3. #include <inttypes.h>
  4. #if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
  5. # include <avr/pgmspace.h>
  6. # include <avr/wdt.h>
  7. #endif
  8. //default baud rate
  9. #define REDFLY_BAUDRATE (9600)
  10. //Tx power (init cmd)
  11. #define LOW_POWER (0)
  12. #define MED_POWER (1)
  13. #define HIGH_POWER (2)
  14. #define LOW_POWER_1M (3)
  15. #define MED_POWER_1M (4)
  16. #define HIGH_POWER_1M (5)
  17. #define LOW_POWER_2M (6)
  18. #define MED_POWER_2M (7)
  19. #define HIGH_POWER_2M (8)
  20. #define LOW_POWER_11M (9)
  21. #define MED_POWER_11M (10)
  22. #define HIGH_POWER_11M (11)
  23. #define LOW_POWER_12M (12)
  24. #define MED_POWER_12M (13)
  25. #define HIGH_POWER_12M (14)
  26. #define LOW_POWER_24M (15)
  27. #define MED_POWER_24M (16)
  28. #define HIGH_POWER_24M (17)
  29. #define LOW_POWER_54M (18)
  30. #define MED_POWER_54M (19)
  31. #define HIGH_POWER_54M (20)
  32. //net type (join cmd)
  33. #define INFRASTRUCTURE (0)
  34. #define IBSS_JOINER (1)
  35. #define IBSS_CREATOR (2)
  36. //protocol (socket cmds)
  37. #define PROTO_TCP (0)
  38. #define PROTO_UDP (1)
  39. #define PROTO_MCAST (2)
  40. //max. sockets (WiFi module supports up to 8 sockets and FW 4.7.1 only 7 sockets)
  41. #define MAX_SOCKETS (7)
  42. #define INVALID_SOCKET (0xFF)
  43. //socket states
  44. #define SOCKET_TCP PROTO_TCP
  45. #define SOCKET_UDP PROTO_UDP
  46. #define SOCKET_MCAST PROTO_MCAST
  47. #define SOCKET_CLOSED (0xFF)
  48. #ifndef SWAP16
  49. #define SWAP16(x) ((((x)&0x00FF)<<8)| \
  50. (((x)&0xFF00)>>8))
  51. #endif
  52. #ifndef SWAP32
  53. #define SWAP32(x) ((((x)&0xFF000000UL)>>24)| \
  54. (((x)&0x00FF0000UL)>> 8)| \
  55. (((x)&0x0000FF00UL)<< 8)| \
  56. (((x)&0x000000FFUL)<<24))
  57. #endif
  58. typedef struct
  59. {
  60. uint8_t handle;
  61. uint8_t state;
  62. } SOCKET_STATE;
  63. extern class REDFLY RedFly;
  64. class REDFLY
  65. {
  66. public:
  67. REDFLY(void);
  68. ~REDFLY(void);
  69. uint8_t init(uint32_t br, uint8_t pwr); //br=baudrate, pwr=tx_power
  70. uint8_t init(uint8_t pwr); //pwr=tx_power
  71. uint8_t init(void);
  72. void enable(void);
  73. void disable(void);
  74. uint8_t getversion(char *ver);
  75. uint8_t getmac(uint8_t *mac);
  76. uint8_t getlocalip(uint8_t *ip);
  77. uint8_t getip(char *host, uint8_t *ip);
  78. uint32_t gettime(uint8_t *server, uint16_t port);
  79. uint32_t gettime(uint8_t *server);
  80. uint8_t getrssi(void);
  81. uint8_t getbssid(char *ssid, uint8_t *mac);
  82. uint8_t gettype(char *ssid, uint8_t *type);
  83. uint8_t scan(void);
  84. uint8_t scan(uint8_t chn, char *ssid, uint8_t *mode, uint8_t *rssi); //returns: ssid, mode, rssi
  85. uint8_t scan(char *ssid, uint8_t *mode, uint8_t *rssi); //returns: ssid, mode, rssi
  86. uint8_t scan(char *ssid); //returns: ssid
  87. uint8_t nextscan(char *ssid, uint8_t *mode, uint8_t *rssi); //returns: ssid, mode, rssi
  88. uint8_t nextscan(char *ssid); //returns: ssid
  89. uint8_t join(char *ssid, char *key, uint8_t net, uint8_t chn, uint8_t authmode); //INFRASTRUCTURE or IBSS_JOINER or IBSS_CREATOR
  90. uint8_t join(char *ssid, char *key, uint8_t net, uint8_t chn); //IBSS_CREATOR
  91. uint8_t join(char *ssid, uint8_t net, uint8_t chn); //IBSS_CREATOR
  92. uint8_t join(char *ssid, char *key, uint8_t net); //INFRASTRUCTURE or IBSS_JOINER
  93. uint8_t join(char *ssid, uint8_t net); //INFRASTRUCTURE or IBSS_JOINER
  94. uint8_t join(char *ssid, char *key); //INFRASTRUCTURE
  95. uint8_t join(char *ssid); //INFRASTRUCTURE
  96. uint8_t disconnect(void); //disassociate
  97. uint8_t begin(uint8_t dhcp, uint8_t *ip, uint8_t *dns, uint8_t *gateway, uint8_t *netmask);
  98. uint8_t begin(uint8_t *ip, uint8_t *dns, uint8_t *gateway, uint8_t *netmask); //dhcp off
  99. uint8_t begin(uint8_t *ip, uint8_t *dns, uint8_t *gateway); //dhcp off
  100. uint8_t begin(uint8_t *ip, uint8_t *dns); //dhcp off
  101. uint8_t begin(uint8_t *ip); //dhcp off
  102. uint8_t begin(uint8_t dhcp); //dhcp or autoip
  103. uint8_t begin(void); //dhcp on
  104. uint8_t socketConnect(uint8_t proto, uint8_t *ip, uint16_t port, uint16_t lport); //ret 0xFF=error
  105. uint8_t socketConnect(uint8_t proto, uint8_t *ip, uint16_t port); //ret 0xFF=error (lport=1024...2048)
  106. uint8_t socketListen(uint8_t proto, uint16_t lport); //ret 0xFF=error
  107. uint8_t socketClose(uint8_t socket);
  108. uint8_t socketClosed(uint8_t socket); //0!=closed
  109. uint8_t socketStatus(uint8_t socket); //ret 0xFF=error
  110. uint8_t socketState(uint8_t socket); //ret 0xFF=closed
  111. uint8_t socketSend(uint8_t socket, uint8_t *stream, uint16_t size, uint8_t *ip, uint16_t port); //ip+port only for UDP
  112. uint8_t socketSend(uint8_t socket, char *stream, uint8_t *ip, uint16_t port); //ip+port only for UDP
  113. uint8_t socketSendPGM(uint8_t socket, PGM_P stream, uint8_t *ip, uint16_t port); //ip+port only for UDP
  114. uint8_t socketSend(uint8_t socket, uint8_t *stream, uint16_t size); //TCP
  115. uint8_t socketSend(uint8_t socket, char *stream); //TCP
  116. uint8_t socketSendPGM(uint8_t socket, PGM_P stream); //TCP
  117. uint8_t socketSend(uint8_t socket, int value); //TCP
  118. uint16_t socketRead(uint8_t *socket, uint16_t *len, uint8_t *ip, uint16_t *port, uint8_t *dst, uint16_t dst_size); //ret 0xFFFF=closed
  119. uint16_t socketRead(uint8_t *socket, uint16_t *len, uint8_t *dst, uint16_t dst_size);
  120. void socketReset(void);
  121. private:
  122. uint8_t tx_power, read_state;
  123. uint8_t buffer[48]; //min. 48 (receive and data buffer)
  124. uint32_t baudrate;
  125. uint8_t ipaddr[4];
  126. SOCKET_STATE socket_state[MAX_SOCKETS];
  127. uint8_t cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, char *v1, PGM_P p2, uint8_t *v2, uint16_t v2_size);
  128. uint8_t cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, int16_t v1);
  129. uint8_t cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, uint8_t *v2, uint16_t v2_size);
  130. uint8_t cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1, char *v1);
  131. uint8_t cmd(uint8_t *dst, uint8_t dst_size, PGM_P p1);
  132. uint8_t cmd(PGM_P p1, char *v1, PGM_P p2);
  133. uint8_t cmd(PGM_P p1, char *v1, uint8_t *v2, uint16_t v2_size);
  134. uint8_t cmd(PGM_P p1, char *v1);
  135. uint8_t cmd(PGM_P p1, int16_t v1);
  136. uint8_t cmd(PGM_P p1);
  137. void flush(void);
  138. void flush_nowait(void);
  139. int available(void);
  140. uint8_t readwait(void);
  141. uint8_t read(void);
  142. void write(uint8_t c);
  143. void setbaudrate(uint32_t br);
  144. char* iptoa(uint8_t *ip, char *s);
  145. char* uitoa(uint16_t val, char *s);
  146. void delay_10ms(uint8_t ms);
  147. };
  148. #endif //REDFLY_h