brewpanel/sockio.c

Thu, 19 Nov 2015 21:17:26 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 19 Nov 2015 21:17:26 +0100
changeset 426
e54611453d29
parent 425
c51265b518ce
child 427
e8e548922e31
permissions
-rw-r--r--

Moved global bit defines to config.h

/*****************************************************************************
 * Copyright (C) 2015
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * 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


int			sock = -1;		/* Unix datagram socket		*/
struct sockaddr_in	servaddr;		/* Server socket address	*/
uint16_t		keys = SLCD_KEYS;	/* Pressed keys bits		*/


void socket_recv(void)
{
    uint16_t		data;
    struct sockaddr_in	clntaddr;
    socklen_t		clntlen = sizeof(clntaddr);
    ssize_t		recv_len;
    int			fd = 0, 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) {
	    slcdPosition(fd, data & 0x001f, (data & 0x0060) >> 5);
	} else if ((data & 0xfe00) == SLCD_DATA) {
	    slcdPutchar(fd, data & 0x00ff);
	} else if (data != 0x0000) {
	    fprintf(stdout, "socket_recv got %04x\n", data);
	}

	/*
	 * Reply with the current keys state
	 */
	if (sendto(sock, &keys, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *) &clntaddr, clntlen) == -1) {
	    fprintf(stdout, "sendto error: %s\n", strerror(errno));
	}
    } 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

mercurial