diff -r 8a7f8272516c -r 644a6106d712 brewpanel/sockio.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/brewpanel/sockio.c Sat Nov 14 16:52:33 2015 +0100 @@ -0,0 +1,121 @@ +/***************************************************************************** + * Copyright (C) 2015 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * mbsePi-apps 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 2, or (at your option) any + * later version. + * + * mbsePi-apps 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 mbsePi-apps; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "brewpanel.h" +#include "slcd.h" +#include "futil.h" +#include "sockio.h" + +#ifdef HAVE_SDL_SDL_H + +#define SLCD_NULL 0x0000 +#define SLCD_CLEAR 0x0001 +#define SLCD_MCLEAR 0x1fff +#define SLCD_HOME 0x0002 +#define SLCD_MHOME 0x1ffe +#define SLCD_DGRAM 0x0080 +#define SLCD_MDGRAM 0x1f80 +#define SLCD_DATA 0x0200 +#define SLCD_MDATA 0x1e00 +#define SLCD_LEDS 0x0400 +#define SLCD_MLEDS 0x1c00 +#define SLCD_KEYS 0x0800 +#define SLCD_MKEYS 0x1800 +#define SLCD_MDEV 0xe000 + + +int sock = -1; /* Unix datagram socket */ +struct sockaddr_in servaddr; /* Server socket address */ + + + +void socket_recv(void) +{ + uint16_t data; + struct sockaddr_in clntaddr; + socklen_t clntlen = sizeof(clntaddr); + ssize_t recv_len; + int fd, my_error; + + recv_len = recvfrom(sock, &data, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *)&clntaddr, &clntlen); + if (recv_len == sizeof(uint16_t)) { + /* + * Get device from data + */ + fd = data & SLCD_MDEV >> 13; + if (fd) + fprintf(stdout, "Device %d ", fd); + if ((data & SLCD_MCLEAR) == SLCD_CLEAR) { + slcdClear(fd); + } else if ((data & SLCD_MHOME) == SLCD_HOME) { + slcdHome(fd); + } else if ((data & SLCD_MDGRAM) == SLCD_DGRAM) { + fprintf(stdout, "slcdPosition(%d, %d, %d)\n", fd, data & 0x001f, (data & 0x0060) >> 5); + slcdPosition(fd, data & 0x001f, (data & 0x0060) >> 5); + } else if ((data & 0xfe00) == SLCD_DATA) { + slcdPutchar(fd, data & 0x00ff); + } else { + fprintf(stdout, "socket_recv got %04x\n", data); + } + } else if (recv_len < 0) { + my_error = errno; + if (my_error != EAGAIN) { + fprintf(stdout, "socket_recv error: %s", strerror(my_error)); + } + } else { + fprintf(stdout, "socket_recv unknown len=%d\n", (int)recv_len); + } +} + + + +int socket_connect(void) +{ + int s; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + syslog(LOG_NOTICE, "socket_connect() can't create: %s", strerror(errno)); + return -1; + } + + /* + * Setup address structure for the server socket. + */ + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(6554); + servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); + + if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + close(s); + syslog(LOG_NOTICE, "socket_connect() can't bind socket: %s", strerror(errno)); + return -1; + } + + syslog(LOG_NOTICE, "socket_connect() using socket %d", s); + sock = s; + return sock; +} + + +#endif