-
Notifications
You must be signed in to change notification settings - Fork 3
/
flipdigit.c
1916 lines (1645 loc) · 45.5 KB
/
flipdigit.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
/* flipdigit.c - Driver for 7seg flip digits display with 4 rows 7cols.
* Copyright (C) 2023 Werner Koch (dd9jn)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
* Build with
* - ENABLE_JABBER to enable XMPP support
* - ENABLE_GPIO to enable using GPIOs to enable transmit mode
* (Default is to assume auto transmit/receive switching)
* - ENABLE_CUSTOM to compile with flipdigit-custom.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <stdint.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <assert.h>
#ifdef ENABLE_GPIO
# include <linux/gpio.h>
#endif
#ifdef ENABLE_JABBER
# include <strophe.h> /* The low-level xmpp library. */
#endif
#define PGM "flipdigit"
#define PGM_VERSION "0.1"
#define PGM_BUGREPORT "[email protected]"
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
#define DIMof(type,member) DIM(((type *)0)->member)
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
#define ATTR_PRINTF(a,b) __attribute__ ((format (printf,a,b)))
#define ATTR_NR_PRINTF(a,b) __attribute__ ((noreturn,format (printf,a,b)))
#else
#define ATTR_PRINTF(a,b)
#define ATTR_NR_PRINTF(a,b)
#endif
#if __GNUC__ >= 4
# define ATTR_SENTINEL(a) __attribute__ ((sentinel(a)))
#else
# define ATTR_SENTINEL(a)
#endif
/* Option flags. */
static int verbose;
static int debug;
static int line_speed = 19200;
static const char *opt_user;
static const char *opt_pass;
static const char *opt_resource;
static const char *opt_muc;
#ifdef ENABLE_GPIO
static const char *gpio_device = "/dev/gpiochip0";
static int gpio_recv_enable = 18; /* (negated) */
#endif
/* The file descriptor of our motion sensor. */
static int motion_fd= -1;
/* True if connected to a jabber server and a global contect. */
#ifdef ENABLE_JABBER
static int jabber_connected;
static xmpp_ctx_t *jabber_ctx;
static xmpp_conn_t *jabber_conn;
#endif
/* Error counter. */
static int any_error;
/* We use a global for the RS485 fd. */
static int rs485_fd;
/* We use a global for the GPIO fd. */
#ifdef ENABLE_GPIO
static int gpio_fd;
#endif
/* if true the daemon will exit. */
static int shutdown_pending;
/* If set to true, the default clock will be displayed. */
static int enable_clock;
static int suppress_seconds;
/* If set to true everything received will be displayed. */
static int talk_mode;
/* The timeout after which the clock will be re-enabled. */
static unsigned int auto_clock = 60;
/* A linked list of recent senders. */
struct sender_list_item_s
{
struct sender_list_item_s *next;
unsigned int count; /* How often seen. */
char excerpt[20]; /* The last seen command from this jid. */
char jid[1];
};
typedef struct sender_list_item_s *sender_list_item_t;
static sender_list_item_t sender_list;
/* The last string shown on the display - basically the current
* thing. (malloced) */
static char *last_displayed_string;
/* The current glyphs in 7segment coding. */
static unsigned char current_glyphs[7*4];
static unsigned char digit_map[10] =
{
/* 0 */ 0b01111110,
/* 1 */ 0b00110000,
/* 2 */ 0b01101101,
/* 3 */ 0b01111001,
/* 4 */ 0b00110011,
/* 5 */ 0b01011011,
/* 6 */ 0b01011111,
/* 7 */ 0b01110000,
/* 8 */ 0b01111111,
/* 9 */ 0b01111011
};
static unsigned char letter_map[26] =
{
/* A */ 0b01110111,
/* b */ 0b00011111,
/* C */ 0b01001110,
/* d */ 0b00111101,
/* E */ 0b01001111,
/* F */ 0b01000111,
/* G */ 0b01011110,
/* h */ 0b00010111, /* H 0b00110111 */
/* I */ 0b00000110,
/* J */ 0b00111100,
/* K */ 0b01010111,
/* L */ 0b00001110,
/* M */ 0b01101010,
/* n */ 0b00010101,
/* o */ 0b00011101,
/* P */ 0b01100111,
/* q */ 0b01110011,
/* r */ 0b00000101,
/* S */ 0b00011011,
/* t */ 0b00001111,
/* u */ 0b00011100, /* U 0b00111110 */
/* V */ 0b00101010,
/* W */ 0b00111111,
/* X */ 0b01001001,
/* Y */ 0b00111011,
/* Z */ 0b01101101 /* Same as '2' */
};
static struct {
char ascii;
unsigned char value;
} special_map[] =
{
{ '+', 0b00000111 },
{ '-', 0b00000001 },
{ '/', 0b00100101 },
{ '@', 0b01111101 },
{ '\\',0b00010011 },
{ 0, 0 }
};
struct private_membuf_s
{
size_t len;
size_t size;
char *buf;
int out_of_core;
};
typedef struct private_membuf_s membuf_t;
/*-- Hooks which may be initialized by flipdigit-custom.c --*/
/* The whoop notification is emitted if this hook is NULL or if the
* hook function returns true. */
static int (*hook_allow_whoop)(time_t t);
/* Hook used by the is_admin function. */
static int (*hook_is_admin)(const char *name);
/* A hook to replace the default clock formatting. */
static int (*hook_show_time)(char *, size_t, time_t, struct tm *);
/* Hook to handle cusom commands. */
static char *(*hook_extra_commands)(const char *, char *, const char *, int *);
/*-- Prototypes --*/
static void die (const char *format, ...) ATTR_NR_PRINTF(1,2);
static void err (const char *format, ...) ATTR_PRINTF(1,2);
static void inf (const char *format, ...) ATTR_PRINTF(1,2);
static void dbg (const char *format, ...) ATTR_PRINTF(1,2);
static char *strconcat (const char *s1, ...) ATTR_SENTINEL(0);
static char *xstrconcat (const char *s1, ...) ATTR_SENTINEL(0);
static void add_to_sender_list (const char *jid, const char *body);
static char *cmd_scroll (char *args);
static char *handle_cmd (char *cmd, const char *sender);
#ifdef ENABLE_CUSTOM
static void custom_main (void);
#endif /*ENABLE_CUSTOM*/
/* Print diagnostic message and exit with failure. */
static void
die (const char *format, ...)
{
va_list arg_ptr;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
if (!*format || format[strlen(format)-1] != '\n')
putc ('\n', stderr);
exit (1);
}
/* Print diagnostic message. */
static void
err (const char *format, ...)
{
va_list arg_ptr;
any_error = 1;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
if (!*format || format[strlen(format)-1] != '\n')
putc ('\n', stderr);
}
/* Print an info message. */
static void
inf (const char *format, ...)
{
va_list arg_ptr;
if (verbose)
{
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
if (!*format || format[strlen(format)-1] != '\n')
putc ('\n', stderr);
}
}
/* Print a debug message. */
static void
dbg (const char *format, ...)
{
va_list arg_ptr;
if (debug)
{
fprintf (stderr, "%s: DBG: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
if (!*format || format[strlen(format)-1] != '\n')
putc ('\n', stderr);
}
}
static void *
xmalloc (size_t n)
{
void *p = malloc (n);
if (!p)
die ("out of core\n");
return p;
}
/* static void * */
/* xrealloc (void *a, size_t newsize) */
/* { */
/* void *p = realloc (a, newsize); */
/* if (!p && newsize) */
/* die ("out of core\n"); */
/* return p; */
/* } */
static char *
xstrdup (const char *string)
{
char *buf = xmalloc (strlen (string) + 1);
strcpy (buf, string);
return buf;
}
/* Helper for xstrconcat and strconcat. */
static char *
do_strconcat (int xmode, const char *s1, va_list arg_ptr)
{
const char *argv[48];
size_t argc;
size_t needed;
char *buffer, *p;
argc = 0;
argv[argc++] = s1;
needed = strlen (s1);
while (((argv[argc] = va_arg (arg_ptr, const char *))))
{
needed += strlen (argv[argc]);
if (argc >= DIM (argv)-1)
die ("too may args for strconcat\n");
argc++;
}
needed++;
buffer = xmode? xmalloc (needed) : malloc (needed);
for (p = buffer, argc=0; argv[argc]; argc++)
p = stpcpy (p, argv[argc]);
return buffer;
}
/* Concatenate the string S1 with all the following strings up to a
NULL. Returns a malloced buffer with the new string or NULL on a
malloc error or if too many arguments are given. */
static char *
strconcat (const char *s1, ...)
{
va_list arg_ptr;
char *result;
if (!s1)
result = strdup ("");
else
{
va_start (arg_ptr, s1);
result = do_strconcat (0, s1, arg_ptr);
va_end (arg_ptr);
}
return result;
}
static char *
xstrconcat (const char *s1, ...)
{
va_list arg_ptr;
char *result;
if (!s1)
result = xstrdup ("");
else
{
va_start (arg_ptr, s1);
result = do_strconcat (1, s1, arg_ptr);
va_end (arg_ptr);
}
return result;
}
/* A simple implementation of a dynamic buffer. Use init_membuf() to
* create a buffer, put_membuf to append bytes and get_membuf to
* release and return the buffer. Allocation errors are detected but
* only returned at the final get_membuf(), this helps not to clutter
* the code with out of core checks. */
void
init_membuf (membuf_t *mb, int initiallen)
{
mb->len = 0;
mb->size = initiallen;
mb->out_of_core = 0;
mb->buf = malloc (initiallen);
if (!mb->buf)
mb->out_of_core = errno;
}
void
put_membuf (membuf_t *mb, const void *buf, size_t len)
{
if (mb->out_of_core || !len)
return;
if (mb->len + len >= mb->size)
{
char *p;
mb->size += len + 1024;
p = realloc (mb->buf, mb->size);
if (!p)
{
mb->out_of_core = errno ? errno : ENOMEM;
return;
}
mb->buf = p;
}
if (buf)
memcpy (mb->buf + mb->len, buf, len);
else
memset (mb->buf + mb->len, 0, len);
mb->len += len;
}
void
put_membuf_str (membuf_t *mb, const char *string)
{
if (!string)
string= "";
put_membuf (mb, string, strlen (string));
}
void *
get_membuf (membuf_t *mb, size_t *len)
{
char *p;
if (mb->out_of_core)
{
if (mb->buf)
{
free (mb->buf);
mb->buf = NULL;
}
errno = mb->out_of_core;
return NULL;
}
p = mb->buf;
if (len)
*len = mb->len;
mb->buf = NULL;
mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
return p;
}
/* Remove leading and trailing white space from STR. */
static char *
trim_spaces (char *string)
{
unsigned char *s, *p, *mark;
p = s = (unsigned char *)string;
for (; *p && isspace (*p); p++)
;
for (mark=NULL; (*s = *p); s++, p++)
{
if (isspace (*p))
{
if (!mark)
mark = s;
}
else
mark = NULL;
}
if (mark)
*mark = 0;
return string;
}
/* Return true if SENDER (a jid) is allowed to issue admin commands. */
static int
is_admin (const char *sender)
{
char *shortjid, *p;
int result;
if (!sender || !*sender)
return 0;
if (!hook_is_admin)
return 0;
shortjid = strdup (sender);
if (!shortjid)
{
err ("out of core in is_admin()");
return 0;
}
p = strchr (shortjid, '/');
if (p)
*p = 0;
result = hook_is_admin (shortjid);
free (shortjid);
return result;
}
#ifdef ENABLE_JABBER
static xmpp_stanza_t *
new_name_stanza (xmpp_ctx_t *ctx, const char *name)
{
xmpp_stanza_t *stanza;
int rc;
stanza = xmpp_stanza_new (ctx);
if (!stanza)
die ("xmpp_stanza_new failed\n");
rc = xmpp_stanza_set_name (stanza, name);
if (rc)
die ("xmpp_stanza_set_name failed: rc=%d\n", rc);
return stanza;
}
static xmpp_stanza_t *
new_text_stanza (xmpp_ctx_t *ctx, const char *text)
{
xmpp_stanza_t *stanza;
int rc;
stanza = xmpp_stanza_new (ctx);
if (!stanza)
die ("xmpp_stanza_new failed\n");
rc = xmpp_stanza_set_text (stanza, text);
if (rc)
die ("xmpp_stanza_set_text failed: rc=%d\n", rc);
return stanza;
}
const char *
get_bound_jid (const xmpp_conn_t * const conn)
{
const char *s = xmpp_conn_get_bound_jid (conn);
if (!s)
die ("xmpp_conn_get_bound_jid failed\n");
return s;
}
/* Send STRING as a MUC message to RECIPIENT. A resource has already
* been stripped from RECIPIENT; the recipient itself should have a
* valid syntax. */
static void
jabber_send_muc (xmpp_ctx_t *ctx, xmpp_conn_t *const conn,
const char *recipient, const char *string)
{
int rc;
char *p;
char *nickbuf = NULL;
char *recpbuf = NULL;
const char *nick;
const char *recp;
xmpp_stanza_t *stanza = NULL;
xmpp_stanza_t *stanza2= NULL;
xmpp_stanza_t *stanza3= NULL;
nickbuf = strdup (get_bound_jid (conn));
if (!nickbuf || !(p = strchr (nickbuf, '@')))
{
err ("invalid own jid at %d\n", __LINE__);
goto leave;
}
*p = 0;
nick = nickbuf;
inf ("sending MUC message to '%s' nick '%s'\n", recipient, nick);
recp = recpbuf = strconcat (recipient, "/", nick, NULL);
if (!recp)
{
err ("out of core at %d\n", __LINE__);
goto leave;
}
dbg ("sending presence to the room\n");
stanza = xmpp_presence_new (ctx);
if (!stanza)
{
err ("xmpp_presence_new failed for MUC\n");
goto leave;
}
rc = xmpp_stanza_set_from (stanza, get_bound_jid (conn));
if (rc)
{
err ("xmpp_stanza_set_from failed: rc=%d\n", rc);
goto leave;
}
rc = xmpp_stanza_set_to (stanza, recp);
if (rc)
{
err ("xmpp_stanza_set_to failed: rc=%d\n", rc);
goto leave;
}
rc = xmpp_stanza_set_id (stanza, "pres1");
if (rc)
{
err ("xmpp_stanza_set_id failed: rc=%d\n", rc);
goto leave;
}
/* Tell server that we support the Basic MUC protocol and that we
* don't want any history. */
stanza2 = new_name_stanza (ctx, "x");
rc = xmpp_stanza_set_ns (stanza2, "http://jabber.org/protocol/muc");
if (rc)
{
err ("xmpp_stanza_set_ns failed: rc=%d\n", rc);
goto leave;
}
stanza3 = new_name_stanza (ctx, "history");
rc = xmpp_stanza_set_attribute (stanza3, "maxchars", "0");
if (rc)
{
err ("xmpp_stanza_set_attribute failed: rc=%d\n", rc);
goto leave;
}
rc = xmpp_stanza_add_child (stanza2, stanza3);
if (rc)
{
err ("xmpp_stanza_add_child failed: rc=%d\n", rc);
goto leave;
}
xmpp_stanza_release (stanza3);
stanza3 = NULL;
rc = xmpp_stanza_add_child (stanza, stanza2);
if (rc)
{
err ("xmpp_stanza_add_child failed: rc=%d\n", rc);
goto leave;
}
xmpp_stanza_release (stanza2);
stanza2 = NULL;
xmpp_send (conn, stanza);
xmpp_stanza_release (stanza);
stanza = NULL;
stanza = xmpp_message_new (ctx, "groupchat", recipient, "chat1");
if (!stanza)
{
err ("xmpp_message_new failed for '%s'\n", recipient);
goto leave;
}
rc = xmpp_message_set_body (stanza, string);
if (rc)
{
err ("xmpp_message_set_body failed: rc=%d\n", rc);
goto leave;
}
xmpp_send (conn, stanza);
leave:
if (stanza3)
xmpp_stanza_release (stanza3);
if (stanza2)
xmpp_stanza_release (stanza2);
if (stanza)
xmpp_stanza_release (stanza);
free (nickbuf);
free (recpbuf);
}
/* Handle iq:version stanzas. */
static int
jabber_version_handler (xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const opaque)
{
xmpp_ctx_t *ctx = opaque;
int rc;
xmpp_stanza_t *reply, *query, *name, *version, *value;
const char *s;
inf ("received version request from %s\n", xmpp_stanza_get_from (stanza));
reply = xmpp_stanza_reply (stanza);
if (!reply)
die ("xmpp_stanza_reply failed\n");
xmpp_stanza_set_type (reply, "result");
query = new_name_stanza (ctx, "query");
s = xmpp_stanza_get_ns (xmpp_stanza_get_children (stanza));
if (s)
xmpp_stanza_set_ns (query, s);
name = new_name_stanza (ctx, "name");
rc = xmpp_stanza_add_child (query, name);
if (rc)
die ("xmpp_stanza_add_child failed: rc=%d\n", rc);
xmpp_stanza_release (name);
value = new_text_stanza (ctx, PGM);
rc = xmpp_stanza_add_child (name, value);
if (rc)
die ("xmpp_stanza_add_child failed: rc=%d\n", rc);
xmpp_stanza_release (value);
version = new_name_stanza (ctx, "version");
rc = xmpp_stanza_add_child (query, version);
if (rc)
die ("xmpp_stanza_add_child failed: rc=%d\n", rc);
xmpp_stanza_release (version);
value = new_text_stanza (ctx, PGM_VERSION);
rc = xmpp_stanza_add_child (version, value);
if (rc)
die ("xmpp_stanza_add_child failed: rc=%d\n", rc);
xmpp_stanza_release (value);
rc = xmpp_stanza_add_child (reply, query);
if (rc)
die ("xmpp_stanza_add_child failed: rc=%d\n", rc);
xmpp_stanza_release (query);
xmpp_send (conn, reply);
xmpp_stanza_release (reply);
return 1; /* Keep this handler. */
}
/* Handle message stanzas. */
static int
jabber_message_handler (xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const opaque)
{
xmpp_ctx_t *ctx = opaque;
const char *type;
xmpp_stanza_t *child, *achild;
char *subject, *body;
const char *code, *errtype;
type = xmpp_stanza_get_type (stanza);
dbg ("msg: type='%s'", type);
if (type && !strcmp (type, "error"))
{
child = xmpp_stanza_get_child_by_name (stanza, "error");
errtype = child? xmpp_stanza_get_attribute (child, "type") : NULL;
code = child? xmpp_stanza_get_attribute (child, "code") : NULL;
err ("received error from <%s>: %s=%s\n",
xmpp_stanza_get_from (stanza),
code? "code":"type",
code? code : errtype);
achild = xmpp_stanza_get_child_by_name (child, "text");
body = achild? xmpp_stanza_get_text (achild) : NULL;
if (body)
inf ("->%s<-\n", body);
xmpp_free (ctx, body);
}
else if (xmpp_stanza_get_child_by_name (stanza, "body"))
{
/* No type but has a body. */
child = xmpp_stanza_get_child_by_name (stanza, "subject");
subject = child? xmpp_stanza_get_text (child) : NULL;
child = xmpp_stanza_get_child_by_name (stanza, "body");
body = child? xmpp_stanza_get_text (child) : NULL;
inf ("received message from <%s> %s%s%s\n", xmpp_stanza_get_from (stanza),
subject? "(subject: ":"",
subject? subject:"",
subject? ")":"");
if (body)
{
char *replystr;
xmpp_stanza_t *reply;
const char *sender;
trim_spaces (body);
inf ("->%s<-\n", body);
sender = xmpp_stanza_get_from (stanza);
add_to_sender_list (sender, body);
if (*body == '!' && opt_muc && is_admin (sender))
{
jabber_send_muc (ctx, conn, opt_muc, body+1);
replystr = NULL;
}
else
replystr = handle_cmd (body, sender); /* (modifies BODY) */
if (replystr)
{
reply = xmpp_stanza_reply (stanza);
if (!xmpp_stanza_get_type (reply))
xmpp_stanza_set_type (reply, "chat");
xmpp_message_set_body (reply, replystr);
xmpp_send (conn, reply);
xmpp_stanza_release (reply);
free (replystr);
}
}
xmpp_free (ctx, body);
xmpp_free (ctx, subject);
if (shutdown_pending)
{
inf ("requesting disconnect\n");
xmpp_disconnect (conn);
}
}
return 1; /* Keep this handler. */
}
/* Handle connection events. */
static void
jabber_conn_handler (xmpp_conn_t * const conn, const xmpp_conn_event_t status,
const int error, xmpp_stream_error_t * const stream_error,
void * const userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t* pres;
if (status == XMPP_CONN_CONNECT)
{
inf ("connected\n");
xmpp_handler_add (conn, jabber_version_handler,
"jabber:iq:version", "iq", NULL, ctx);
xmpp_handler_add (conn, jabber_message_handler,
NULL, "message", NULL, ctx);
/* Send initial presence so that we appear online to contacts */
pres = xmpp_presence_new (ctx);
xmpp_send (conn, pres);
xmpp_stanza_release (pres);
}
else
{
inf ("disconnected\n");
xmpp_stop (ctx);
shutdown_pending = 2;
}
}
#endif /*ENABLE_JABBER*/
/* Open GPIO device and prepare pins. Returns the file descriptor to
* change the GPIO data. */
#ifdef ENABLE_GPIO
static int
gpio_open (void)
{
struct gpiohandle_request greq;
int fd;
int ret;
fd = open (gpio_device, O_RDONLY);
if (fd == -1)
die ("can't open `%s': %s", gpio_device, strerror (errno));
/* greq.lineoffsets[0] = gpio_recv_enable; */
greq.lineoffsets[0] = gpio_data_enable;
greq.lines = 1;
greq.flags = GPIOHANDLE_REQUEST_OUTPUT;
ret = ioctl (fd, GPIO_GET_LINEHANDLE_IOCTL, &greq);
close (fd);
if (ret == -1)
die ("GPIO_GET_LINEHANDLE_IOCTL for '%s' failed: %s\n",
gpio_device, strerror (errno));
return greq.fd;
}
static void
gpio_close (int fd)
{
if (fd != -1)
close (fd);
}
static void
gpio_write (int fd, int recv_enable, int data_enable)
{
struct gpiohandle_data gdata;
int ret;
/* gdata.values[0] = !!recv_enable; */
gdata.values[0] = !!data_enable;
ret = ioctl (fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &gdata);
if (ret == -1)
err ("error setting GPIOs: %s\n", strerror (errno));
}
#endif /*ENABLE_GPIO*/
static void
dump_mcbits (int fd)
{
int mcbits;
if (ioctl (fd, TIOCMGET, &mcbits))
err ("TIOCMGET failed: %s\n", strerror (errno));
else
inf ("mc: %3s %3s %3s %3s %3s %3s %3s %3s %3s",
(mcbits & TIOCM_LE )? "LE":"",
(mcbits & TIOCM_DTR)? "DTR":"",
(mcbits & TIOCM_DSR)? "DSR":"",
(mcbits & TIOCM_CAR)? "DCD":"",
(mcbits & TIOCM_RNG)? "RI":"",
(mcbits & TIOCM_RTS)? "RTS":"",
(mcbits & TIOCM_CTS)? "CTS":"",
(mcbits & TIOCM_ST )? "TX2":"",
(mcbits & TIOCM_SR )? "RX2":"");
}
static int
open_line (const char *fname)
{
int fd;
struct termios term;
speed_t speed;
switch (line_speed)
{
case 300 : speed = B300 ; break;
case 600 : speed = B600 ; break;
case 1200 : speed = B1200 ; break;
case 2400 : speed = B2400 ; break;
case 4800 : speed = B4800 ; break;
case 9600 : speed = B9600 ; break;
case 19200 : speed = B19200 ; break;
case 38400 : speed = B38400 ; break;
case 57600 : speed = B57600 ; break;
case 115200: speed = B115200; break;
default:
die ("unsupported line speed %d given", line_speed);
}
fd = open (fname, O_WRONLY);
if (fd == -1)
die ("can't open '%s': %s", fname, strerror (errno));
if (tcgetattr (fd, &term))
die ("tcgetattr(%d) failed: %s", fd, strerror (errno));
term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
term.c_oflag &= ~OPOST;
term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
term.c_cflag &= ~(CSIZE | PARENB);
term.c_cflag |= CS8;
if (cfsetospeed (&term, speed) || cfsetispeed (&term, speed))
die ("setting terminal speed to %d failed: %s",
line_speed, strerror (errno));
if (tcsetattr (fd, TCSANOW, &term ) )
die ("tcsetattr(%d) failed: %s", fd, strerror (errno));
inf ("connected to '%s' at %dbps", fname, line_speed);
dump_mcbits (fd);
return fd;
}