-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlua-mosquitto.c
1347 lines (1170 loc) · 32.8 KB
/
lua-mosquitto.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
lua-mosquitto.c - Lua bindings to libmosquitto
Copyright (c) 2014 Bart Van Der Meerssche <bart@flukso.net>
Natanael Copa <ncopa@alpinelinux.org>
Karl Palsson <karlp@remake.is>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/***
* Lua bindings to libmosquitto
*
* This documentation is partial, and doesn't cover all functionality yet.
* @module mosquitto
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <mosquitto.h>
#include "compat.h"
/* re-using mqtt3 message types as callback types */
#define CONNECT 0x10
#define PUBLISH 0x30
#define SUBSCRIBE 0x80
#define UNSUBSCRIBE 0xA0
#define DISCONNECT 0xE0
/* add two extra callback types */
#define MESSAGE 0x01
#define LOG 0x02
enum connect_return_codes {
CONN_ACCEPT,
CONN_REF_BAD_PROTOCOL,
CONN_REF_BAD_ID,
CONN_REF_SERVER_NOAVAIL,
CONN_REF_BAD_LOGIN,
CONN_REF_NO_AUTH,
CONN_REF_BAD_TLS
};
/* unique naming for userdata metatables */
#define MOSQ_META_CTX "mosquitto.ctx"
typedef struct {
lua_State *L;
struct mosquitto *mosq;
int on_connect;
int on_disconnect;
int on_publish;
int on_message;
int on_subscribe;
int on_unsubscribe;
int on_log;
} ctx_t;
static int mosq_initialized = 0;
/* handle mosquitto lib return codes */
static int mosq__pstatus(lua_State *L, int mosq_errno) {
switch (mosq_errno) {
case MOSQ_ERR_SUCCESS:
lua_pushboolean(L, true);
return 1;
break;
case MOSQ_ERR_INVAL:
case MOSQ_ERR_NOMEM:
case MOSQ_ERR_PROTOCOL:
case MOSQ_ERR_NOT_SUPPORTED:
return luaL_error(L, mosquitto_strerror(mosq_errno));
break;
case MOSQ_ERR_NO_CONN:
case MOSQ_ERR_CONN_LOST:
case MOSQ_ERR_PAYLOAD_SIZE:
lua_pushnil(L);
lua_pushinteger(L, mosq_errno);
lua_pushstring(L, mosquitto_strerror(mosq_errno));
return 3;
break;
case MOSQ_ERR_ERRNO:
lua_pushnil(L);
lua_pushinteger(L, errno);
lua_pushstring(L, strerror(errno));
return 3;
break;
}
return 0;
}
/***
* Library functions
* @section lib_functions
*/
/***
* Return mosquitto library version.
* @function version
* @treturn string version string "major.minor.revision"
* @see mosquitto_lib_version
*/
static int mosq_version(lua_State *L)
{
int major, minor, rev;
char version[16];
mosquitto_lib_version(&major, &minor, &rev);
sprintf(version, "%i.%i.%i", major, minor, rev);
lua_pushstring(L, version);
return 1;
}
/***
* Does a topic match a subscription string?
* @function topic_matches_sub
* @tparam string subscription eg, blah/+/wop/#
* @tparam string topic to test
* @return[1] boolean result
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
* @see mosquitto_topic_matches_sub
*/
static int mosq_topic_matches_sub(lua_State *L)
{
const char *sub = luaL_checkstring(L, 1);
const char *topic = luaL_checkstring(L, 2);
bool result;
int rc = mosquitto_topic_matches_sub(sub, topic, &result);
if (rc != MOSQ_ERR_SUCCESS) {
return mosq__pstatus(L, rc);
} else {
lua_pushboolean(L, result);
return 1;
}
}
/***
* @function init
* @see mosquitto_lib_init
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int mosq_init(lua_State *L)
{
if (!mosq_initialized)
mosquitto_lib_init();
return mosq__pstatus(L, MOSQ_ERR_SUCCESS);
}
/***
* Cleanup mosquitto library.
* This is called automatically by garbage collection, you shouldn't normally
* have to call this.
* @function cleanup
* @see mosquitto_lib_cleanup
* @return[1] true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int mosq_cleanup(lua_State *L)
{
mosquitto_lib_cleanup();
mosq_initialized = 0;
return mosq__pstatus(L, MOSQ_ERR_SUCCESS);
}
static void ctx__on_init(ctx_t *ctx)
{
ctx->on_connect = LUA_REFNIL;
ctx->on_disconnect = LUA_REFNIL;
ctx->on_publish = LUA_REFNIL;
ctx->on_message = LUA_REFNIL;
ctx->on_subscribe = LUA_REFNIL;
ctx->on_unsubscribe = LUA_REFNIL;
ctx->on_log = LUA_REFNIL;
}
static void ctx__on_clear(ctx_t *ctx)
{
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_connect);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_disconnect);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_publish);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_message);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_subscribe);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_unsubscribe);
luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_log);
}
/***
* Create a new mosquitto instance
* @function new
* @tparam[opt=nil] string client id, optional. nil to allow library to generate
* @tparam[opt=true] boolean clean_session
* @return[1] a mosquitto instance
* @raise For some out of memory or illegal states, or both nil id and clean session true
* @see mosquitto_new
*/
static int mosq_new(lua_State *L)
{
const char *id = luaL_optstring(L, 1, NULL);
bool clean_session = (lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true);
if (id == NULL && !clean_session) {
return luaL_argerror(L, 2, "if 'id' is nil then 'clean session' must be true");
}
ctx_t *ctx = (ctx_t *) lua_newuserdata(L, sizeof(ctx_t));
/* ctx will be passed as void *obj arg in the callback functions */
ctx->mosq = mosquitto_new(id, clean_session, ctx);
if (ctx->mosq == NULL) {
return luaL_error(L, strerror(errno));
}
ctx->L = L;
ctx__on_init(ctx);
luaL_getmetatable(L, MOSQ_META_CTX);
lua_setmetatable(L, -2);
return 1;
}
static ctx_t * ctx_check(lua_State *L, int i)
{
return (ctx_t *) luaL_checkudata(L, i, MOSQ_META_CTX);
}
/***
* Instance functions
* @section instance_functions
*/
/***
* Destroy context
* This is called automatically by garbage collection, you shouldn't normally
* have to call this.
* @function destroy
* @see mosquitto_destroy
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_destroy(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
mosquitto_destroy(ctx->mosq);
/* clean up Lua callback functions in the registry */
ctx__on_clear(ctx);
/* remove all methods operating on ctx */
lua_newtable(L);
lua_setmetatable(L, -2);
return mosq__pstatus(L, MOSQ_ERR_SUCCESS);
}
/***
* Reinitialise
* @function reinitialise
* @tparam[opt=nil] string client id, nil to allow the library to determine
* @tparam[opt=true] boolean clean session.
* @see mosquitto_reinitialise
* @see new
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_reinitialise(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *id = luaL_optstring(L, 1, NULL);
bool clean_session = (lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true);
if (id == NULL && !clean_session) {
return luaL_argerror(L, 3, "if 'id' is nil then 'clean session' must be true");
}
int rc = mosquitto_reinitialise(ctx->mosq, id, clean_session, ctx);
/* clean up Lua callback functions in the registry */
ctx__on_clear(ctx);
ctx__on_init(ctx);
return mosq__pstatus(L, rc);
}
/***
* Set a Will
* @function will_set
* @tparam string topic as per mosquitto_will_set
* @tparam string payload as per mosquitto_will_set (but a proper lua string)
* @tparam[opt=0] number qos 0, 1 or 2
* @tparam[opt=false] boolean retain
* @see mosquitto_will_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_will_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *topic = luaL_checkstring(L, 2);
size_t payloadlen = 0;
const void *payload = NULL;
if (!lua_isnil(L, 3)) {
payload = lua_tolstring(L, 3, &payloadlen);
};
int qos = luaL_optinteger(L, 4, 0);
bool retain = lua_toboolean(L, 5);
int rc = mosquitto_will_set(ctx->mosq, topic, payloadlen, payload, qos, retain);
return mosq__pstatus(L, rc);
}
/***
* Clear a will
* @function will_clear
* @see mosquitto_will_clear
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_will_clear(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_will_clear(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* Set login details
* @function login_set
* @tparam[opt=nil] string username may be nil
* @tparam[opt=nil] string password may be nil
* @see mosquitto_username_pw_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_login_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *username = (lua_isnil(L, 2) ? NULL : luaL_checkstring(L, 2));
const char *password = (lua_isnil(L, 3) ? NULL : luaL_checkstring(L, 3));
int rc = mosquitto_username_pw_set(ctx->mosq, username, password);
return mosq__pstatus(L, rc);
}
/***
* Set TLS details
* This doesn't currently support callbacks for passphrase prompting!
* @function tls_set
* @tparam[opt=nil] string cafile may be nil
* @tparam[opt=nil] string capath may be nil
* @tparam[opt=nil] string certfile may be nil
* @tparam[opt=nil] string keyfile may be nil
* @see mosquitto_tls_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_tls_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *cafile = luaL_optstring(L, 2, NULL);
const char *capath = luaL_optstring(L, 3, NULL);
const char *certfile = luaL_optstring(L, 4, NULL);
const char *keyfile = luaL_optstring(L, 5, NULL);
// the last param is a callback to a function that asks for a passphrase for a keyfile
// our keyfiles should NOT have a passphrase
int rc = mosquitto_tls_set(ctx->mosq, cafile, capath, certfile, keyfile, 0);
return mosq__pstatus(L, rc);
}
/***
* Set TLS insecure flags
* @function tls_insecure_set
* @tparam boolean value true or false
* @see mosquitto_tls_insecure_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_tls_insecure_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
bool value = lua_toboolean(L, 2);
int rc = mosquitto_tls_insecure_set(ctx->mosq, value);
return mosq__pstatus(L, rc);
}
/***
* Set TLS PSK options
* @function tls_psk_set
* @tparam string psk
* @tparam string identity
* @tparam[opt] string ciphers
* @see mosquitto_tls_psk_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_tls_psk_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *psk = luaL_checkstring(L, 2);
const char *identity = luaL_checkstring(L, 3);
const char *ciphers = luaL_optstring(L, 4, NULL);
int rc = mosquitto_tls_psk_set(ctx->mosq, psk, identity, ciphers);
return mosq__pstatus(L, rc);
}
/***
* Set TLS options, must be called before connect.
* @function tls_opts_set
* @tparam bool cert true for SSL_VERIFY_PEER, false for SSL_VERIFY_NONE
* @tparam[opt=nil] string tls_version nil to use default
* @tparam[opt=nil] string ciphers nil to use the default set
* @see mosquitto_tls_opts_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_tls_opts_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const bool cert_required = lua_toboolean(L, 2);
const char *tls_version = luaL_optstring(L, 3, NULL);
const char *ciphers = luaL_optstring(L, 4, NULL);
int rc = mosquitto_tls_opts_set(ctx->mosq, cert_required ? 1 : 0, tls_version, ciphers);
return mosq__pstatus(L, rc);
}
/***
* Set/clear threaded flag
* @function threaded_set
* @tparam boolean value true or false
* @see mosquitto_threaded_set
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_threaded_set(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
bool value = lua_toboolean(L, 2);
int rc = mosquitto_threaded_set(ctx->mosq, value);
return mosq__pstatus(L, rc);
}
/***
* Set client options, normally required before connect.
* This will call _string_option or _int_option automatically based on the
* parameters provided.
* @function option
* @tparam number option code, eg M.OPT_TLS_ALPN or M.OPT_PROTOCOL_VERSION
* @tparam string_or_number value the value of the option to set
* @see mosquitto_string_option
* @see mosquitto_int_option
* @see option_types
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For illegal arguments
*/
static int ctx_option(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
enum mosq_opt_t option = luaL_checkint(L, 2);
int type = lua_type(L, 3);
int rc;
if (type == LUA_TNUMBER) {
int val = lua_tonumber(L, 3);
rc = mosquitto_int_option(ctx->mosq, option, val);
} else if (type == LUA_TSTRING) {
const char *val = lua_tolstring(L, 3, NULL);
rc = mosquitto_string_option(ctx->mosq, option, val);
} else {
return luaL_argerror(L, 3, "values must be numeric or string");
}
return mosq__pstatus(L, rc);
}
/***
* Connect to a broker
* @function connect
* @tparam[opt=localhost] string host
* @tparam[opt=1883] number port
* @tparam[opt=60] number keepalive in seconds
* @see mosquitto_connect
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_connect(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *host = luaL_optstring(L, 2, "localhost");
int port = luaL_optinteger(L, 3, 1883);
int keepalive = luaL_optinteger(L, 4, 60);
int rc = mosquitto_connect(ctx->mosq, host, port, keepalive);
return mosq__pstatus(L, rc);
}
/***
* connect (async)
* @function connect_async
* @tparam[opt=localhost] string host
* @tparam[opt=1883] number port
* @tparam[opt=60] number keepalive in seconds
* @see mosquitto_connect_async
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_connect_async(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
const char *host = luaL_optstring(L, 2, "localhost");
int port = luaL_optinteger(L, 3, 1883);
int keepalive = luaL_optinteger(L, 4, 60);
int rc = mosquitto_connect_async(ctx->mosq, host, port, keepalive);
return mosq__pstatus(L, rc);
}
/***
* @function reconnect
* @see mosquitto_reconnect
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_reconnect(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_reconnect(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* @function reconnect_async
* @see mosquitto_reconnect_async
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_reconnect_async(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_reconnect_async(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* @function disconnect
* @see mosquitto_disconnect
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_disconnect(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_disconnect(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* Publish a message
* @function publish
* @tparam string topic
* @tparam string payload (may be nil)
* @tparam[opt=0] number qos 0, 1 or 2
* @tparam[opt=nil] boolean retain flag
* @return
* @see mosquitto_publish
* @treturn[1] number MID can be used for correlation with callbacks
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_publish(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int mid; /* message id is referenced in the publish callback */
const char *topic = luaL_checkstring(L, 2);
size_t payloadlen = 0;
const void *payload = NULL;
if (!lua_isnil(L, 3)) {
payload = lua_tolstring(L, 3, &payloadlen);
};
int qos = luaL_optinteger(L, 4, 0);
bool retain = lua_toboolean(L, 5);
int rc = mosquitto_publish(ctx->mosq, &mid, topic, payloadlen, payload, qos, retain);
if (rc != MOSQ_ERR_SUCCESS) {
return mosq__pstatus(L, rc);
} else {
lua_pushinteger(L, mid);
return 1;
}
}
/***
* Subscribe to a topic
* @function subscribe
* @tparam string topic eg "blah/+/json/#"
* @tparam[opt=0] number qos 0, 1 or 2
* @treturn[1] number MID can be used for correlation with callbacks
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
* @see mosquitto_subscribe
*/
static int ctx_subscribe(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int mid;
const char *sub = luaL_checkstring(L, 2);
int qos = luaL_optinteger(L, 3, 0);
int rc = mosquitto_subscribe(ctx->mosq, &mid, sub, qos);
if (rc != MOSQ_ERR_SUCCESS) {
return mosq__pstatus(L, rc);
} else {
lua_pushinteger(L, mid);
return 1;
}
}
/***
* Unsubscribe from a topic
* @function unsubscribe
* @tparam string topic to unsubscribe from
* @see mosquitto_unsubscribe
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_unsubscribe(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int mid;
const char *sub = luaL_checkstring(L, 2);
int rc = mosquitto_unsubscribe(ctx->mosq, &mid, sub);
if (rc != MOSQ_ERR_SUCCESS) {
return mosq__pstatus(L, rc);
} else {
lua_pushinteger(L, mid);
return 1;
}
}
static int mosq_loop(lua_State *L, bool forever)
{
ctx_t *ctx = ctx_check(L, 1);
int timeout = luaL_optinteger(L, 2, -1);
int max_packets = luaL_optinteger(L, 3, 1);
int rc;
if (forever) {
rc = mosquitto_loop_forever(ctx->mosq, timeout, max_packets);
} else {
rc = mosquitto_loop(ctx->mosq, timeout, max_packets);
}
return mosq__pstatus(L, rc);
}
/***
* run the loop manually
* @function loop
* @tparam[opt=-1] number timeout how long in ms to wait for traffic (-1 for library default)
* @tparam[opt=1] number max_packets
* @see mosquitto_loop
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop(lua_State *L)
{
return mosq_loop(L, false);
}
/***
* run the loop forever, blocking
* @function loop_forever
* @tparam[opt=-1] number timeout how long in ms to wait for traffic (-1 for library default)
* @tparam[opt=1] number max_packets
* @see mosquitto_loop_forever
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_forever(lua_State *L)
{
return mosq_loop(L, true);
}
/***
* Start a loop thread
* @function loop_start
* @see mosquitto_loop_start
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_start(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_loop_start(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* Stop an existing loop thread
* @function loop_stop
* @see mosquitto_loop_stop
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_stop(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
bool force = lua_toboolean(L, 2);
int rc = mosquitto_loop_stop(ctx->mosq, force);
return mosq__pstatus(L, rc);
}
/***
* Get the underlying socket
* @function socket
* @treturn[1] number the socket number
* @treturn[2] boolean false if the socket was uninitialized
* @see mosquitto_socket
*/
static int ctx_socket(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int fd = mosquitto_socket(ctx->mosq);
switch (fd) {
case -1:
lua_pushboolean(L, false);
break;
default:
lua_pushinteger(L, fd);
break;
}
return 1;
}
/***
* Handle loop read events manually
* @function loop_read
* @tparam[opt=1] number max_packets
* @see mosquitto_loop_read
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_read(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int max_packets = luaL_optinteger(L, 2, 1);
int rc = mosquitto_loop_read(ctx->mosq, max_packets);
return mosq__pstatus(L, rc);
}
/***
* Handle loop write events manually
* @function loop_write
* @tparam[opt=1] number max_packets
* @see mosquitto_loop_write
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_write(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int max_packets = luaL_optinteger(L, 2, 1);
int rc = mosquitto_loop_write(ctx->mosq, max_packets);
return mosq__pstatus(L, rc);
}
/***
* Handle loop misc events manually
* @function loop_misc
* @see mosquitto_loop_misc
* @return[1] boolean true
* @return[2] nil
* @treturn[2] number error code
* @treturn[2] string error description.
* @raise For some out of memory or illegal states
*/
static int ctx_loop_misc(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
int rc = mosquitto_loop_misc(ctx->mosq);
return mosq__pstatus(L, rc);
}
/***
* Does the library want to write?
* @function want_write
* @see mosquitto_want_write
* @treturn boolean result
*/
static int ctx_want_write(lua_State *L)
{
ctx_t *ctx = ctx_check(L, 1);
lua_pushboolean(L, mosquitto_want_write(ctx->mosq));
return 1;
}
static void ctx_on_connect(
struct mosquitto *mosq,
void *obj,
int rc)
{
ctx_t *ctx = obj;
bool success = false;
char *str = "reserved for future use";
switch(rc) {
case CONN_ACCEPT:
success = true;
str = "connection accepted";
break;
case CONN_REF_BAD_PROTOCOL:
str = "connection refused - incorrect protocol version";
break;
case CONN_REF_BAD_ID:
str = "connection refused - invalid client identifier";
break;
case CONN_REF_SERVER_NOAVAIL:
str = "connection refused - server unavailable";
break;
case CONN_REF_BAD_LOGIN:
str = "connection refused - bad username or password";
break;
case CONN_REF_NO_AUTH:
str = "connection refused - not authorised";
break;
case CONN_REF_BAD_TLS:
str = "connection refused - TLS error";
break;
}
lua_rawgeti(ctx->L, LUA_REGISTRYINDEX, ctx->on_connect);
lua_pushboolean(ctx->L, success);
lua_pushinteger(ctx->L, rc);
lua_pushstring(ctx->L, str);
lua_call(ctx->L, 3, 0);
}
static void ctx_on_disconnect(
struct mosquitto *mosq,
void *obj,
int rc)
{
ctx_t *ctx = obj;
bool success = true;
char *str = "client-initiated disconnect";
if (rc) {
success = false;
str = "unexpected disconnect";
}
lua_rawgeti(ctx->L, LUA_REGISTRYINDEX, ctx->on_disconnect);
lua_pushboolean(ctx->L, success);