moteleds.C 7.83 KB
Newer Older
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2004, 2005 University of Utah and the Flux Group.
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
 * All rights reserved.
 */

/*
 * moteleds.C - program to read the mote LED pins on a Stargate, and display
 * them: on the stargate's LEDs, on the command line, or out a TCP socket
 */


/*
 * These seem to make things happier when compiling on Linux
 */
#ifdef __linux__
#define __PREFER_BSD
#define __USE_BSD
#define __FAVOR_BSD
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <arpa/inet.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#ifdef __linux__
#include <linux/sockios.h>
#else
#include <sys/sockio.h>
#endif

#include "SGGPIO.h"

/*
 * Mote LEDs
 */
#define MOTELED_RED_PIN    24
#define MOTELED_GREEN_PIN  28
#define MOTELED_YELLOW_PIN 29

/*
 * Stargate LEDs
 */
#define SGLED_YELLOW_PIN 62
#define SGLED_GREEN_PIN  63
#define SGLED_RED_PIN    64

/*
 * LED IOCTL constants - derived from linux/led.h
 */
#define LED_DEV "/dev/platx/led" 
#define CONSUS_LED_IOCTL_MAGIG 'g'
#define CLED_IOSET          _IO (CONSUS_LED_IOCTL_MAGIG, 1)
#define CLED_IOGET          _IOR(CONSUS_LED_IOCTL_MAGIG, 2, int*)
#define CLED_IOSTARTAUTO    _IO (CONSUS_LED_IOCTL_MAGIG, 3)
#define CLED_IOSTOPAUTO     _IO (CONSUS_LED_IOCTL_MAGIG, 4)
#define CLED_IOAUTOSTATUS   _IOR(CONSUS_LED_IOCTL_MAGIG, 5, int*)
#define CLED_RADIO_RESET    _IO (CONSUS_LED_IOCTL_MAGIG, 6)

#define LED_GREEN      (1 << 13)
#define LED_RED        (1 << 15)
#define LED_YELLOW     (1 << 14)
#define LED_MASK       (LED_RED|LED_GREEN|LED_YELLOW)

// Port to listen on
#define PORT 1812

// How many clients we can handle at once
#define MAX_CLIENTS 64

// Which mode we're in
enum modes {
    MODE_NONE   = 0x0,
    MODE_MIRROR = 0x1,
    MODE_PRINT  = 0x2,
    MODE_EVENT  = 0x4,
    MODE_SOCKET = 0x8
};

void usage() {
	fprintf(stderr,"Usage: moteleds <-m | -p | -e | -s> [-t sleeptime]\n");
	fprintf(stderr,"  -m  Mirror mode - set stargate LEDs\n");
	fprintf(stderr,"  -p  Print mode - print LED status to stdout\n");
	//fprintf(stderr,"  -e  Event mode - set events on LED state change\n");
	fprintf(stderr,"  -s  Socket mode - open up a socket\n");
	fprintf(stderr,"  -d  daemonize\n");
	fprintf(stderr,"  -t  Sleep time in microseconds\n");
	exit(1);
}

int main(int argc, char **argv) {

    unsigned int mode = MODE_NONE;
    int naptime = 0;

    // Process command-line args
    int ch;
    bool daemonize = false;
    while ((ch = getopt(argc, argv, "mpesdt:")) != -1)
	switch (ch) {
	    case 'm':
		mode |= MODE_MIRROR;
		break;
	    case 'p':
		mode |= MODE_PRINT;
		break;
	    case 'e':
		mode |= MODE_EVENT;
		break;
	    case 's':
		mode |= MODE_SOCKET;
		break;
	    case 'd':
		daemonize = true;
		break;
	    case 't':
		if (!sscanf(optarg,"%i",&naptime)) {
		    usage();
		}
		break;
	    case '?':
	    default:
		usage();
	}
    argc -= optind;
    argv += optind;

    if (argc) {
	usage();
    }

    if (mode == MODE_NONE) {
	usage();
    }


    // Set the GPIO pins to read from the mote LEDs
    SGGPIO_PORT sggpio;
    sggpio.setDir(MOTELED_YELLOW_PIN,0);
    sggpio.setDir(MOTELED_GREEN_PIN,0);
    sggpio.setDir(MOTELED_RED_PIN,0);

    if (!naptime) {
	naptime = 10 * 1000; // 100Hz
    }

    // We're going to mirror the mote LEDs to the stargate LEDs, so open up
    // a file to control the SG LEDs
    int ledfd = -1;
    if (mode & MODE_MIRROR) {
	ledfd = open(LED_DEV, O_RDWR);
	if (ledfd < 0) {
	    fprintf(stderr, "Open error: %s\n", LED_DEV);
	    return 1;
	}
	// Turn off the automatic LED pattern
	if (ioctl (ledfd, CLED_IOSTOPAUTO) < 0) {
	    fprintf (stderr, "ioctl stop auto error: %s\n", LED_DEV);
	    return 1;
	}
    }

    // If we're supposed to write LED state change events to a socket, open
    // that up now
    int sockfd = -1;
    if (mode & MODE_SOCKET) {
	struct protoent *proto = getprotobyname("TCP");
	sockfd = socket(AF_INET,SOCK_STREAM,proto->p_proto);
	if (sockfd < 0) {     
	    perror("Creating socket");
	    exit(1);
	}                               

	// Set SO_RESEADDUR to make it easier to kill and restart this daemon
        int opt = 1;
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) < 0)
                perror("SO_REUSEADDR");
	
	// Make the socket non-blocking
        if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
                perror("O_NONBLOCK");

	struct sockaddr_in address;
        address.sin_family = AF_INET;
        address.sin_port = htons(PORT);
        address.sin_addr.s_addr = INADDR_ANY;

        if (bind(sockfd,(struct sockaddr*)(&address),sizeof(address))) {
                perror("Binding socket");
                exit(1);
        }

        if (listen(sockfd,-1)) {
                perror("Listening on socket");
                exit(1);
        }

        /*
         * Ignore SIGPIPE - we'll detect the remote end closing the connection
         * by a failed write() .
         */
	struct sigaction action;
        action.sa_handler = SIG_IGN;
        sigaction(SIGPIPE,&action,NULL);

    }

    // Daemonize if we're supposed to
    if (daemonize) {
	daemon(0,0);
    }

    /*
     * Main loop
     */
    unsigned int current_status, old_status;
    // Bogus value so that we will detect a change the first time through
    old_status = 31337;

    // FD of the clients for our socket
    int clientfds[MAX_CLIENTS];
    for (int i = 0; i < MAX_CLIENTS; i++) { clientfds[i] = -1; }
    bool newclient = false;
    int current_clients = 0;
234
    int since_last_update = 0;
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
    while(1) {
	// If we have a socket, try to accept connections
	if ((mode & MODE_SOCKET) && sockfd) {

	    struct sockaddr client_addr;
	    size_t client_addrlen;

	    int newclientfd = accept(sockfd,&client_addr,&client_addrlen);
	    if (newclientfd >= 0) {
		if (current_clients == MAX_CLIENTS) {
		    // Bummer, too many
		    close(newclientfd);
		}
		newclient = true;
		for (int i = 0; i < MAX_CLIENTS; i++) {
		    if (clientfds[i] == -1) {
			clientfds[i] = newclientfd;
			current_clients++;
			break;
		    }
		}
	    }
	}

	// Read mote pin connectors
	int yellow = sggpio.readPin(MOTELED_YELLOW_PIN);
	int green = sggpio.readPin(MOTELED_GREEN_PIN);
	int red = sggpio.readPin(MOTELED_RED_PIN);

	// Mote LEDs use negative logic, reverse them
	if (yellow) { yellow = 0; } else { yellow = 1; }
	if (green) { green = 0; } else { green = 1; }
	if (red) { red = 0; } else { red = 1; }

	// In mirroring mode, set up a patter for the SG LEDs and ioctl() it
	// into place
	if (mode & MODE_MIRROR) {
	    int ledpattern = 0;
	    if (yellow) { ledpattern |= LED_RED; }
	    if (green) { ledpattern |= LED_GREEN; }
	    if (red) { ledpattern |= LED_YELLOW; }
	    if (ioctl (ledfd, CLED_IOSET, ledpattern) < 0) {
		fprintf (stderr, "ioctl set error: %s\n", LED_DEV);
		exit(1);
	    } 
	}

	// Check to see if what we got this time was different from last time
	current_status = (red << 2) | (green << 1) | yellow;
	if (old_status != current_status) {
	    // In printing mode, do just that
	    if (mode & MODE_PRINT) {
		printf("%i %i %i\n",red,green,yellow);
	    }
	}

291 292 293
	if ((old_status != current_status) || 
	    newclient || 
	    (since_last_update > (naptime * 150))) {
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	    // If we have a socket, print on that
	    if ((mode & MODE_SOCKET) && (current_clients > 0)) {
		char outbuf[1024];
		snprintf(outbuf,1024,"%i %i %i\n",red,green,yellow);
		for (int i = 0; i <= MAX_CLIENTS; i++) {
		    int clientfd = clientfds[i];
		    if (clientfd == -1) {
			continue;
		    }
		    if (write(clientfd,outbuf,strlen(outbuf)) < 0) {
			// Detect disconnected clients
			if (errno != EPIPE) {
			    perror("write");
			}
			close(clientfd);
			clientfds[i] = -1;
			current_clients--;
		    }
		}
	    }
	    newclient = false;
315 316 317 318
	    since_last_update = 0;
	}
	else {
	    since_last_update += naptime;
319 320 321 322 323 324 325
	}
	old_status = current_status;

	// Get some, go again
	usleep(naptime);
    }
}