-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicow_iot.c
285 lines (225 loc) · 7.57 KB
/
picow_iot.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "hardware/structs/rosc.h"
#include <string.h>
#include <time.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/pbuf.h"
#include "lwip/tcp.h"
#include "lwip/dns.h"
#include "lwip/altcp_tcp.h"
#include "lwip/altcp_tls.h"
#include "lwip/apps/mqtt.h"
#include "lwip/apps/mqtt_priv.h"
// #include "tusb.h"
#define DEBUG_printf printf
#define MQTT_TLS 1 // needs to be 1 for AWS IoT. Also set published QoS to 0 or 1
//#define CRYPTO_MOSQUITTO_LOCAL
#define CRYPTO_AWS_IOT
#include "crypto_consts.h"
#if MQTT_TLS
#ifdef CRYPTO_CERT
const char *cert = CRYPTO_CERT;
#endif
#ifdef CRYPTO_CA
const char *ca = CRYPTO_CA;
#endif
#ifdef CRYPTO_KEY
const char *key = CRYPTO_KEY;
#endif
#endif
typedef struct MQTT_CLIENT_T_ {
ip_addr_t remote_addr;
mqtt_client_t *mqtt_client;
u32_t received;
u32_t counter;
u32_t reconnect;
} MQTT_CLIENT_T;
err_t mqtt_test_connect(MQTT_CLIENT_T *state);
// Perform initialisation
static MQTT_CLIENT_T* mqtt_client_init(void) {
MQTT_CLIENT_T *state = calloc(1, sizeof(MQTT_CLIENT_T));
if (!state) {
DEBUG_printf("failed to allocate state\n");
return NULL;
}
state->received = 0;
return state;
}
void dns_found(const char *name, const ip_addr_t *ipaddr, void *callback_arg) {
MQTT_CLIENT_T *state = (MQTT_CLIENT_T*)callback_arg;
DEBUG_printf("DNS query finished with resolved addr of %s.\n", ip4addr_ntoa(ipaddr));
state->remote_addr = *ipaddr;
}
void run_dns_lookup(MQTT_CLIENT_T *state) {
DEBUG_printf("Running DNS query for %s.\n", MQTT_SERVER_HOST);
cyw43_arch_lwip_begin();
err_t err = dns_gethostbyname(MQTT_SERVER_HOST, &(state->remote_addr), dns_found, state);
cyw43_arch_lwip_end();
if (err == ERR_ARG) {
DEBUG_printf("failed to start DNS query\n");
return;
}
if (err == ERR_OK) {
DEBUG_printf("no lookup needed");
return;
}
while (state->remote_addr.addr == 0) {
cyw43_arch_poll();
sleep_ms(1);
}
}
u32_t data_in = 0;
u8_t buffer[1025];
u8_t data_len = 0;
static void mqtt_pub_start_cb(void *arg, const char *topic, u32_t tot_len) {
DEBUG_printf("mqtt_pub_start_cb: topic %s\n", topic);
if (tot_len > 1024) {
DEBUG_printf("Message length exceeds buffer size, discarding");
} else {
data_in = tot_len;
data_len = 0;
}
}
static void mqtt_pub_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) {
if (data_in > 0) {
data_in -= len;
memcpy(&buffer[data_len], data, len);
data_len += len;
if (data_in == 0) {
buffer[data_len] = 0;
DEBUG_printf("Message received: %s\n", &buffer);
}
}
}
static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) {
if (status != 0) {
DEBUG_printf("Error during connection: err %d.\n", status);
} else {
DEBUG_printf("MQTT connected.\n");
}
}
void mqtt_pub_request_cb(void *arg, err_t err) {
MQTT_CLIENT_T *state = (MQTT_CLIENT_T *)arg;
DEBUG_printf("mqtt_pub_request_cb: err %d\n", err);
state->received++;
}
void mqtt_sub_request_cb(void *arg, err_t err) {
DEBUG_printf("mqtt_sub_request_cb: err %d\n", err);
}
err_t mqtt_test_publish(MQTT_CLIENT_T *state)
{
char buffer[128];
sprintf(buffer, "{\"message\":\"hello from picow %d / %d\"}", state->received, state->counter);
err_t err;
u8_t qos = 0; /* 0 1 or 2, see MQTT specification. AWS IoT does not support QoS 2 */
u8_t retain = 0;
cyw43_arch_lwip_begin();
err = mqtt_publish(state->mqtt_client, "pico_w/test", buffer, strlen(buffer), qos, retain, mqtt_pub_request_cb, state);
cyw43_arch_lwip_end();
if(err != ERR_OK) {
DEBUG_printf("Publish err: %d\n", err);
}
return err;
}
err_t mqtt_test_connect(MQTT_CLIENT_T *state) {
struct mqtt_connect_client_info_t ci;
err_t err;
memset(&ci, 0, sizeof(ci));
ci.client_id = "PicoW";
ci.client_user = NULL;
ci.client_pass = NULL;
ci.keep_alive = 0;
ci.will_topic = NULL;
ci.will_msg = NULL;
ci.will_retain = 0;
ci.will_qos = 0;
#if MQTT_TLS
struct altcp_tls_config *tls_config;
#if defined(CRYPTO_CA) && defined(CRYPTO_KEY) && defined(CRYPTO_CERT)
DEBUG_printf("Setting up TLS with 2wayauth.\n");
tls_config = altcp_tls_create_config_client_2wayauth(
(const u8_t *)ca, 1 + strlen((const char *)ca),
(const u8_t *)key, 1 + strlen((const char *)key),
(const u8_t *)"", 0,
(const u8_t *)cert, 1 + strlen((const char *)cert)
);
// enable SNI on request
// see mqtt-sni.patch for changes to support this.
altcp_tls_set_server_name(tls_config, MQTT_SERVER_HOST);
#elif defined(CRYPTO_CERT)
DEBUG_printf("Setting up TLS with cert.\n");
tls_config = altcp_tls_create_config_client((const u8_t *) cert, 1 + strlen((const char *) cert));
// enable SNI on request
// see mqtt-sni.patch for changes to support this.
altcp_tls_set_server_name(tls_config, MQTT_SERVER_HOST);
#endif
if (tls_config == NULL) {
DEBUG_printf("Failed to initialize config\n");
return -1;
}
ci.tls_config = tls_config;
#endif
const struct mqtt_connect_client_info_t *client_info = &ci;
err = mqtt_client_connect(state->mqtt_client, &(state->remote_addr), MQTT_SERVER_PORT, mqtt_connection_cb, state, client_info);
if (err != ERR_OK) {
DEBUG_printf("mqtt_connect return %d\n", err);
}
return err;
}
void mqtt_run_test(MQTT_CLIENT_T *state) {
state->mqtt_client = mqtt_client_new();
state->counter = 0;
if (state->mqtt_client == NULL) {
DEBUG_printf("Failed to create new mqtt client\n");
return;
}
// psa_crypto_init();
if (mqtt_test_connect(state) == ERR_OK) {
absolute_time_t timeout = nil_time;
bool subscribed = false;
mqtt_set_inpub_callback(state->mqtt_client, mqtt_pub_start_cb, mqtt_pub_data_cb, 0);
while (true) {
cyw43_arch_poll();
absolute_time_t now = get_absolute_time();
if (is_nil_time(timeout) || absolute_time_diff_us(now, timeout) <= 0) {
if (mqtt_client_is_connected(state->mqtt_client)) {
cyw43_arch_lwip_begin();
if (!subscribed) {
mqtt_sub_unsub(state->mqtt_client, "pico_w/recv", 0, mqtt_sub_request_cb, 0, 1);
subscribed = true;
}
if (mqtt_test_publish(state) == ERR_OK) {
if (state->counter != 0) {
DEBUG_printf("published %d\n", state->counter);
}
timeout = make_timeout_time_ms(5000);
state->counter++;
} // else ringbuffer is full and we need to wait for messages to flush.
cyw43_arch_lwip_end();
} else {
// DEBUG_printf(".");
}
}
}
}
}
int main() {
stdio_init_all();
if (cyw43_arch_init()) {
DEBUG_printf("failed to initialise\n");
return 1;
}
cyw43_arch_enable_sta_mode();
DEBUG_printf("Connecting to WiFi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
DEBUG_printf("failed to connect.\n");
return 1;
} else {
DEBUG_printf("Connected.\n");
}
MQTT_CLIENT_T *state = mqtt_client_init();
run_dns_lookup(state);
mqtt_run_test(state);
cyw43_arch_deinit();
return 0;
}