brewpanel/sockio.c

Sat, 25 Apr 2020 20:31:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 25 Apr 2020 20:31:31 +0200
changeset 605
e00f8ff4de9a
parent 443
6b80a37fdf8d
child 637
21e542c15832
permissions
-rw-r--r--

Version 0.9.8. Added extra path to the fonts for Debian buster. Changed the PID to work on Proportional on Measurement. Added loops so that it looks like the PID is running at 100 mSec intervals.

/*****************************************************************************
 * 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 "sdlgui.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		*/
extern int		debug;


void socket_recv(SGOBJ *dlg)
{
    uint16_t		data;
    unsigned char	cgdata[8];
    struct sockaddr_in	clntaddr;
    socklen_t		clntlen = sizeof(clntaddr);
    ssize_t		recv_len;
    int			i, index, 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 && debug)
	    fprintf(stdout, "Device %d ", fd);
	if ((data & SLCD_MCLEAR) == SLCD_CLEAR) {
	    slcdClear(dlg, fd);
	} else if ((data & SLCD_MHOME) == SLCD_HOME) {
	    slcdHome(dlg, fd);
	} else if ((data & SLCD_MCGRAM) == SLCD_CGRAM) {
	    index = (data & 0x038) >> 3;
	    for (i = 0; i < 8; i++) {
		/*
		 * Reply with the current keyboard state. Looks too early but the very last reply
		 * will be sent at the end of this function and we need to take 8 data words.
		 */
		if (sendto(sock, &keys, sizeof(uint16_t), MSG_DONTWAIT, (struct sockaddr *) &clntaddr, clntlen) == -1) {
		    syslog(LOG_NOTICE, "socket_recv() sendto error: %s", strerror(errno));
		}
		recv_len = recvfrom(sock, &data, sizeof(uint16_t), 0, (struct sockaddr *)&clntaddr, &clntlen);
		cgdata[i] = data & 0x00ff;
	    }
	    fprintf(stdout, "Got new CGRAM index=%d, %02x %02x %02x %02x %02x %02x %02x %02x\n", index,
			    cgdata[0], cgdata[1], cgdata[2], cgdata[3], cgdata[4], cgdata[5], cgdata[6], cgdata[7]);
	    slcdCharDef(dlg, fd, index, cgdata);
	} else if ((data & SLCD_MDGRAM) == SLCD_DGRAM) {
	    slcdPosition(dlg, fd, data & 0x001f, (data & 0x0060) >> 5);
	} else if ((data & 0xfe00) == SLCD_DATA) {
	    slcdPutchar(dlg, fd, data & 0x00ff);
	} else if ((data & SLCD_MLEDS) == SLCD_LEDS) {
	    if (debug)
	    	fprintf(stdout, "socket_recv leds fd=%d bits=%02x\n", fd, (data & 0x00ff));
	    slcdBacklight(dlg, fd, data & SLED_LCD);
	    slcdLED(dlg, fd, SGLEDGREEN,  data & SLED_TFLED);
	    slcdLED(dlg, fd, SGLEDRED,    data & SLED_HLTH);
	    slcdLED(dlg, fd, SGLEDBLUE,   data & SLED_MLTH);
	    slcdLED(dlg, fd, SGLEDYELLOW, data & SLED_MLTP);

	} else if (data != 0x0000) {
	    if (debug)
	    	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) {
	    syslog(LOG_NOTICE, "socket_recv() sendto error: %s", strerror(errno));
	}
    } else if (recv_len < 0) {
	my_error = errno;
	if (my_error != EAGAIN) {
	    syslog(LOG_NOTICE, "socket_recv() error: %s", strerror(my_error));
	}
    } else {
	syslog(LOG_NOTICE, "socket_recv() unknown len=%d", (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