-
Notifications
You must be signed in to change notification settings - Fork 10
/
gs.h
executable file
·165 lines (137 loc) · 3.78 KB
/
gs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
gs.cpp - HAL driver to talk with Gainspan GS1011 WiFi module
Copyright (C) 2011 DIYSandbox LLC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _gs_h_
#define _gs_h_
#include <avr/pgmspace.h>
#include <WString.h>
typedef uint8_t SOCKET;
class SOCK_STATUS {
public:
static const uint8_t CLOSED = 0x00;
static const uint8_t INIT = 0x01;
static const uint8_t LISTEN = 0x02;
static const uint8_t ESTABLISHED = 0x03;
static const uint8_t CLOSE_WAIT = 0x04;
};
class IPPROTO {
public:
static const uint8_t TCP = 6;
static const uint8_t UDP = 7;
static const uint8_t UDP_CLIENT = 8;
};
// command identifiers
// config
#define CMD_DISABLE_ECHO 0
// wifi
#define CMD_SET_WPA_PSK 1
#define CMD_SET_SSID 2
#define CMD_DISCONNECT 5
#define CMD_GET_MAC_ADDR 8
//network
#define CMD_DISABLE_DHCP 3
#define CMD_ENABLE_DHCP 4
#define CMD_TCP_LISTEN 6
#define CMD_TCP_CONN 7
#define CMD_DNS_LOOKUP 9
#define CMD_CLOSE_CONN 10
#define CMD_NETWORK_SET 11
#define CMD_WIRELESS_MODE 12
#define CMD_ENABLE_DHCPSVR 13
#define CMD_UDP_LISTEN 14
#define CMD_UDP_CONN 15
#define CMD_BAUD115200 16
#define CMD_INVALID 255
// device operation modes
#define DEV_OP_MODE_COMMAND 0
#define DEV_OP_MODE_DATA 1
#define DEV_OP_MODE_DATA_RX 2
// device wireless connection state
#define DEV_CONN_ST_DISCONNECTED 0
#define DEV_CONN_ST_CONNECTED 1
// Baud Rate
#define BAUD_9600 0
#define BAUD_115200 1
// connection ID
#define INVALID_CID 255
// wireless connection params
typedef struct _GS_PROFILE {
String ssid;
String security_key;
String ip;
String subnet;
String gateway;
} GS_PROFILE;
typedef struct _SOCK_TABLE {
uint8_t status;
uint8_t protocol;
uint16_t port;
uint8_t cid;
} SOCK_TABLE;
class GSClass {
public:
uint8_t mode;
uint8_t init();
void configure(GS_PROFILE* prof);
uint8_t connect();
uint8_t connected();
void process();
void setMaxSpeed();
uint8_t connectSocket(SOCKET s, String ip, String port);
String dns_lookup(String url);
void send_data(String data);
void esc_seq_start();
void esc_seq_stop();
String get_dev_id();
void configSocket(SOCKET s, uint8_t protocol, uint16_t port);
void execSocketCmd(SOCKET s, uint8_t cmd);
uint8_t readSocketStatus(SOCKET s);
uint8_t getSocketProtocol(SOCKET s);
uint8_t isDataOnSock(SOCKET s);
uint16_t readData(SOCKET s, uint8_t* buf, uint16_t len);
uint16_t writeData(SOCKET s, const uint8_t* buf, uint16_t len);
static const uint16_t SSIZE = 256; // Max Tx buffer siz
static const uint8_t baudRate = BAUD_9600;
private:
String security_key;
String ssid;
String local_ip;
String subnet;
String gateway;
uint8_t serv_cid;
uint8_t client_cid;
uint8_t dev_mode;
String ip;
String port;
uint8_t connection_state;
String dev_id;
String dns_url_ip;
uint8_t tx_done;
SOCK_TABLE sock_table[4];
uint8_t socket_num;
SOCKET dataOnSock;
String srcIPUDP;
String srcPortUDP;
void (*rx_data_handler)(String data);
String readline(void);
uint8_t send_cmd(uint8_t cmd);
uint8_t parse_resp(uint8_t cmd);
uint8_t send_cmd_w_resp(uint8_t cmd);
void parse_cmd(String buf);
void parse_data(String buf);
void flush();
};
extern GSClass GS;
#endif // _gs_h_