brewpanel/sockio.c

changeset 420
644a6106d712
child 422
13502d0dda65
equal deleted inserted replaced
419:8a7f8272516c 420:644a6106d712
1 /*****************************************************************************
2 * Copyright (C) 2015
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * mbsePi-apps is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with mbsePi-apps; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "brewpanel.h"
24 #include "slcd.h"
25 #include "futil.h"
26 #include "sockio.h"
27
28 #ifdef HAVE_SDL_SDL_H
29
30 #define SLCD_NULL 0x0000
31 #define SLCD_CLEAR 0x0001
32 #define SLCD_MCLEAR 0x1fff
33 #define SLCD_HOME 0x0002
34 #define SLCD_MHOME 0x1ffe
35 #define SLCD_DGRAM 0x0080
36 #define SLCD_MDGRAM 0x1f80
37 #define SLCD_DATA 0x0200
38 #define SLCD_MDATA 0x1e00
39 #define SLCD_LEDS 0x0400
40 #define SLCD_MLEDS 0x1c00
41 #define SLCD_KEYS 0x0800
42 #define SLCD_MKEYS 0x1800
43 #define SLCD_MDEV 0xe000
44
45
46 int sock = -1; /* Unix datagram socket */
47 struct sockaddr_in servaddr; /* Server socket address */
48
49
50
51 void socket_recv(void)
52 {
53 uint16_t data;
54 struct sockaddr_in clntaddr;
55 socklen_t clntlen = sizeof(clntaddr);
56 ssize_t recv_len;
57 int fd, my_error;
58
59 recv_len = recvfrom(sock, &data, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *)&clntaddr, &clntlen);
60 if (recv_len == sizeof(uint16_t)) {
61 /*
62 * Get device from data
63 */
64 fd = data & SLCD_MDEV >> 13;
65 if (fd)
66 fprintf(stdout, "Device %d ", fd);
67 if ((data & SLCD_MCLEAR) == SLCD_CLEAR) {
68 slcdClear(fd);
69 } else if ((data & SLCD_MHOME) == SLCD_HOME) {
70 slcdHome(fd);
71 } else if ((data & SLCD_MDGRAM) == SLCD_DGRAM) {
72 fprintf(stdout, "slcdPosition(%d, %d, %d)\n", fd, data & 0x001f, (data & 0x0060) >> 5);
73 slcdPosition(fd, data & 0x001f, (data & 0x0060) >> 5);
74 } else if ((data & 0xfe00) == SLCD_DATA) {
75 slcdPutchar(fd, data & 0x00ff);
76 } else {
77 fprintf(stdout, "socket_recv got %04x\n", data);
78 }
79 } else if (recv_len < 0) {
80 my_error = errno;
81 if (my_error != EAGAIN) {
82 fprintf(stdout, "socket_recv error: %s", strerror(my_error));
83 }
84 } else {
85 fprintf(stdout, "socket_recv unknown len=%d\n", (int)recv_len);
86 }
87 }
88
89
90
91 int socket_connect(void)
92 {
93 int s;
94
95 s = socket(AF_INET, SOCK_DGRAM, 0);
96 if (s == -1) {
97 syslog(LOG_NOTICE, "socket_connect() can't create: %s", strerror(errno));
98 return -1;
99 }
100
101 /*
102 * Setup address structure for the server socket.
103 */
104 memset(&servaddr, 0, sizeof(servaddr));
105 servaddr.sin_family = AF_INET;
106 servaddr.sin_port = htons(6554);
107 servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
108
109 if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
110 close(s);
111 syslog(LOG_NOTICE, "socket_connect() can't bind socket: %s", strerror(errno));
112 return -1;
113 }
114
115 syslog(LOG_NOTICE, "socket_connect() using socket %d", s);
116 sock = s;
117 return sock;
118 }
119
120
121 #endif

mercurial