-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMMMqtt.c
273 lines (250 loc) · 9.87 KB
/
MMMqtt.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
/***********************************************************************************************************************
PicoMite MMBasic
custom.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#if LWIP_TCP
/** Define this to a compile-time IP address initialization
* to connect anything else than IPv4 loopback
*/
#ifndef LWIP_MQTT_EXAMPLE_IPADDR_INIT
#if LWIP_IPV4
#define LWIP_MQTT_EXAMPLE_IPADDR_INIT = IPADDR4_INIT(PP_HTONL(0x144F466D))
#else
#define LWIP_MQTT_EXAMPLE_IPADDR_INIT
#endif
#endif
#undef LWIP_PLATFORM_DIAG
#define LWIP_PLATFORM_DIAG
enum {
TCP_DISCONNECTED,
TCP_CONNECTING,
MQTT_CONNECTING,
MQTT_CONNECTED
};
//static ip_addr_t mqtt_ip LWIP_MQTT_EXAMPLE_IPADDR_INIT;
static mqtt_client_t* mqtt_client=NULL;
typedef struct mqtt_dns_t_ {
ip_addr_t remote;
int complete;
} mqtt_dns_t;
mqtt_dns_t mqtt_dns = {
{0},
0
};
typedef struct mqtt_subs_t_ {
char topic[STRINGSIZE];
int complete;
} mqtt_subs_t;
mqtt_subs_t mqtt_subs={
"",
0
};
static void mqtt_dns_found(const char *hostname, const ip_addr_t *ipaddr, void *arg) {
mqtt_dns_t *dns=(mqtt_dns_t *)arg;
if (ipaddr) {
dns->remote = *ipaddr;
dns->complete=1;
char buff[STRINGSIZE]={0};
sprintf(buff,"tcp address %s\r\n", ip4addr_ntoa(ipaddr));
if(!optionsuppressstatus)MMPrintString(buff);
}
}
struct mqtt_connect_client_info_t mqtt_client_info =
{
NULL,
NULL, /* user */
NULL, /* pass */
100, /* keep alive */
NULL, /* will_topic */
NULL, /* will_msg */
1, /* will_qos */
1 /* will_retain */
#if LWIP_ALTCP && LWIP_ALTCP_TLS
, NULL
#endif
};
static void
mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
{
void *v;
int mylen=len;
if(mylen>255)mylen=255;
v = findvar((unsigned char *)"MM.MESSAGE$", T_STR | V_NOFIND_NULL); // create the variable
if(v==NULL)findvar((unsigned char *)"MM.MESSAGE$", V_FIND | V_DIM_VAR | T_CONST);
u8_t *p=(u8_t *)v;
memcpy(&p[1],data,mylen);
p[0]=mylen;
MQTTComplete=1;
}
static void
mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
{
void *v;
int mylen=strlen(topic);
if(mylen>255)mylen=255;
v = findvar((unsigned char *)"MM.TOPIC$", T_STR | V_NOFIND_NULL); // create the variable
if(v==NULL)findvar((unsigned char *)"MM.TOPIC$", V_FIND | V_DIM_VAR | T_CONST);
u8_t *p=(u8_t *)v;
memcpy(&p[1],topic,mylen);
p[0]=mylen;
}
static void
mqtt_submessage_cb(void *arg, err_t err)
{
mqtt_subs_t *state=(mqtt_subs_t *)arg;
state->complete=1;
}
static void
mqtt_unsubmessage_cb(void *arg, err_t err)
{
mqtt_subs_t *state=(mqtt_subs_t *)arg;
state->complete=1;
}
static void
mqtt_request_cb(void *arg, err_t err)
{
// const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
// LWIP_PLATFORM_DIAG(("MQTT client \"%s\" request cb: err %d\n", client_info->client_id, (int)err));
}
static void
mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
{
// const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
// LWIP_UNUSED_ARG(client);
// LWIP_PLATFORM_DIAG(("MQTT client \"%s\" mqtt connection cb: status %d\n", client_info->client_id, (int)status));
}
#endif /* LWIP_TCP */
void
mqtt_example_init(ip_addr_t *mqtt_ip, int port)
{
#if LWIP_TCP
//mqtt_connection_status_t status;
mqtt_client = mqtt_client_new();
mqtt_client->keep_alive=100;
if(!optionsuppressstatus)printf("Connecting to %s port %u\r\n", ip4addr_ntoa(mqtt_ip), port);
mqtt_client_connect(mqtt_client,
mqtt_ip, port,
mqtt_connection_cb, &mqtt_client_info,
&mqtt_client_info);
Timer4=5000;
while(Timer4 && mqtt_client->conn_state != MQTT_CONNECTED)if(startupcomplete)cyw43_arch_poll();
if(Timer4==0)error("Failed to connect");
mqtt_set_inpub_callback(mqtt_client,
mqtt_incoming_publish_cb,
mqtt_incoming_data_cb,
&mqtt_client_info);
#endif /* LWIP_TCP */
}
void closeMQTT(void){
if(mqtt_client){
mqtt_disconnect(mqtt_client);
mqtt_client_free(mqtt_client);
mqtt_client=NULL;
}
}
//static struct altcp_tls_config *tls_config = NULL;
int cmd_mqtt(void){
unsigned char *tp=checkstring(cmdline,(unsigned char *)"MQTT CONNECT");
if(tp){
getargs(&tp,9,(unsigned char *)",");
char *IP=GetTempMemory(STRINGSIZE);
char *ID=GetTempMemory(STRINGSIZE);
if(mqtt_client)error("Already connected");
int timeout=5000;
if(!(argc==7 || argc==9))error("Syntax");
IP=(char *)getCstring(argv[0]);
int port=getint(argv[2],1,65535);
ip4_addr_t remote_addr;
// if(port==8883){
// tls_config = altcp_tls_create_config_client(NULL, 0);
// mqtt_client_info.tls_config=tls_config;
// }
mqtt_client_info.client_user=(char *)getCstring(argv[4]);
mqtt_client_info.client_pass=(char *)getCstring(argv[6]);
if(argc==9){
MQTTInterrupt=(char *)GetIntAddress(argv[8]);
InterruptUsed=true;
}
else MQTTInterrupt=NULL;
MQTTComplete=0;
strcpy(ID,"WebMite");
strcat(ID,id_out);
IntToStr(&ID[strlen(ID)],time_us_64(),16);
mqtt_client_info.client_id=ID;
if(!isalpha((uint8_t)*IP) && strchr(IP,'.') && strchr(IP,'.')<IP+4){
if(!ip4addr_aton(IP, &remote_addr))error("Invalid address format");
mqtt_dns.remote=remote_addr;
} else {
int err = dns_gethostbyname(IP, &remote_addr, mqtt_dns_found, &mqtt_dns);
Timer4=timeout;
while(!mqtt_dns.complete && Timer4 && !(err==ERR_OK))if(startupcomplete)cyw43_arch_poll();
if(!Timer4)error("Failed to convert web address");
mqtt_dns.complete=0;
}
mqtt_example_init(&mqtt_dns.remote,port);
return 1;
}
tp=checkstring(cmdline,(unsigned char *)"MQTT PUBLISH");
if(tp){
getargs(&tp,7,(unsigned char *)",");
int qos=1;
int retain=1;
if(argc<3)error("Syntax");
char *topic=(char *)getCstring(argv[0]);
char *msg=(char *)getCstring(argv[2]);
if(argc>=5 && *argv[4])qos=getint(argv[4],0,2);
if(argc==7)retain=getint(argv[6],0,1);
mqtt_publish(mqtt_client, topic, msg, strlen(msg), qos, retain, mqtt_request_cb , (void *)&mqtt_client_info);
return 1;
}
tp=checkstring(cmdline,(unsigned char *)"MQTT SUBSCRIBE");
if(tp){
getargs(&tp,3,(unsigned char *)",");
if(!(argc>=1))error("Syntax");
strcpy(mqtt_subs.topic,(char *)getCstring(argv[0]));
int qos=0;
if(argc==3)qos=getint(argv[2],0,2);
mqtt_subs.complete=0;
Timer4=4000;
mqtt_sub_unsub(mqtt_client, mqtt_subs.topic, qos, mqtt_submessage_cb, &mqtt_subs, 1);
while(Timer4 && !mqtt_subs.complete)if(startupcomplete)cyw43_arch_poll();
if(Timer4==0)error("Failed to subscribe to $",mqtt_subs.topic);
return 1;
}
tp=checkstring(cmdline,(unsigned char *)"MQTT UNSUBSCRIBE");
if(tp){
strcpy(mqtt_subs.topic,(char *)getCstring(tp));
mqtt_subs.complete=0;
Timer4=4000;
mqtt_sub_unsub(mqtt_client, mqtt_subs.topic, 0, mqtt_unsubmessage_cb, &mqtt_subs, 0);
while(Timer4 && !mqtt_subs.complete)if(startupcomplete)cyw43_arch_poll();
if(Timer4==0)error("Failed to unsubscribe to $",mqtt_subs.topic);
return 1;
}
tp=checkstring(cmdline,(unsigned char *)"MQTT CLOSE");
if(tp){
if(!mqtt_client)return 1;
closeMQTT();
return 1;
}
return 0;
}