/* $Id$ * $URL$ * * LCD4Linux driver for 4D Systems Display Graphics Modules * * Copyright (C) 2008 Sven Killig * Modified from sample code by: * Copyright (C) 2005 Michael Reinelt * Copyright (C) 2005, 2006, 2007 The LCD4Linux Team * * This file is part of LCD4Linux. * * LCD4Linux 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. * * LCD4Linux 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 this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * * exported fuctions: * * struct DRIVER drv_D4D * */ #include "config.h" #include #include #include #include #include #include #include #include #include "debug.h" #include "cfg.h" #include "qprintf.h" #include "udelay.h" #include "plugin.h" #include "widget.h" #include "widget_text.h" #include "widget_icon.h" #include "widget_bar.h" #include "drv.h" #include "drv_generic_text.h" #include "drv_generic_graphic.h" #include "drv_generic_serial.h" static char Name[] = "D4D"; char NAME_[20]; int FONT = 1, MODE = 0, EXTRA = 0, SECTOR = 0, SECTOR_SIZE, NOPOWERCYCLE = 0; /* int CONTRAST_; */ #define address_mmsb(variable) ((variable >> 24) & 0xFF) #define address_mlsb(variable) ((variable >> 16) & 0xFF) #define address_lmsb(variable) ((variable >> 8) & 0xFF) #define address_llsb(variable) (variable & 0xFF) #define address_hi(variable) ((variable >> 16) & 0xFF) #define address_mid(variable) ((variable >> 8) & 0xFF) #define address_lo(variable) (variable & 0xFF) #define msb(variable) ((variable >> 8) & 0xFF) #define lsb(variable) (variable & 0xFF) RGBA FG_COL_ = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0xff }; RGBA BG_COL_ = {.R = 0xff,.G = 0xff,.B = 0xff,.A = 0xff }; short int FG_COLOR, BG_COLOR; int RGB_24to16(int red, int grn, int blu) { return (((red >> 3) << 11) | ((grn >> 2) << 5) | (blu >> 3)); } int RGB_24to8(int red, int grn, int blu) { return (((red >> 5) << 5) | ((grn >> 5) << 2) | (blu >> 6)); } /****************************************/ /*** hardware dependant functions ***/ /****************************************/ static int drv_D4D_open(const char *section) { int fd; fd = drv_generic_serial_open(section, Name, 0); if (fd < 0) return -1; fcntl(fd, F_SETFL, 0); /* blocking read */ return 0; } static int drv_D4D_close(void) { drv_generic_serial_close(); return 0; } static void drv_D4D_receive_ACK() { char ret[1]; while (drv_generic_serial_read(ret, sizeof(ret)) != 1) usleep(1); /* loop should be unneccessary */ if (ret[0] == 0x15) { error("NAK!"); } else if (ret[0] != 6) { error("no ACK!"); } } static void drv_D4D_send_nowait(const char *data, const unsigned int len) { drv_generic_serial_write(data, len); } static void drv_D4D_send(const char *data, const unsigned int len) { drv_D4D_send_nowait(data, len); drv_D4D_receive_ACK(); } static void drv_D4D_send_nowait_extra(const char *data, const unsigned int len, unsigned char pos1, unsigned char pos2) { /* possibly leave out bytes at pos1 and pos2 for older protocol format */ if (EXTRA) { drv_D4D_send_nowait(data, len); } else { unsigned int i; char send[1]; for (i = 0; i < len; i++) { if (!pos1 || i != pos1) { if (!pos2 || i != pos2) { send[0] = data[i]; drv_generic_serial_write(send, 1); } } } } } static void drv_D4D_send_extra(const char *data, const unsigned int len, char pos1, char pos2) { drv_D4D_send_nowait_extra(data, len, pos1, pos2); drv_D4D_receive_ACK(); } static void drv_D4D_clear(void) { char cmd[] = { 'E' }; drv_D4D_send(cmd, sizeof(cmd)); } static void drv_D4D_write(const int row, const int col, const char *data, int len) { char out[len]; char user_char[len]; int user_x[len]; int user_y[len]; int i, k = 0; if (!SECTOR) { /* font in ROM */ for (i = 0; i < len; i++) { if (data[i] >= 31 && data[i] < CHAR0) { out[i] = data[i]; } else if (data[i] == 'µ') { /* mu */ if (FONT == 2) out[i] = 31; /* undocumented */ else if (FONT == 0) out[i] = 'u'; else out[i] = 127; /* undocumented */ } else { out[i] = ' '; if (data[i] == '°') user_char[k] = 0; /* degree */ else user_char[k] = data[i] - CHAR0; user_x[k] = (col + i) * XRES; user_y[k] = row * YRES; k++; } } char cmd[] = { 's', col, row, FONT, msb(FG_COLOR), lsb(FG_COLOR) }; /* normal chars */ drv_D4D_send_nowait(cmd, sizeof(cmd)); if (len > 256) len = 256; drv_D4D_send_nowait(out, len); char cmdNull[1]; cmdNull[0] = 0; drv_D4D_send(cmdNull, 1); char cmd_user[] = { 'D', 0, 0, 0, 0, 0, msb(FG_COLOR), lsb(FG_COLOR) }; /* user defined symbols */ for (i = 0; i < k; i++) { cmd_user[2] = user_char[i]; cmd_user[3] = user_x[i]; cmd_user[4] = msb(user_y[i]); cmd_user[5] = lsb(user_y[i]); drv_D4D_send_extra(cmd_user, sizeof(cmd_user), 1, 4); } } else { /* font on SD card */ int sec; char cmd_sd[] = { '@', 'I', 0, msb(row * YRES), lsb(row * YRES), XRES, msb(YRES), lsb(YRES), 16, 0, 0, 0 }; for (i = 0; i < len; i++) { cmd_sd[2] = (col + i) * XRES; sec = SECTOR + (unsigned char) data[i] * SECTOR_SIZE; cmd_sd[9] = address_hi(sec); cmd_sd[10] = address_mid(sec); cmd_sd[11] = address_lo(sec); drv_D4D_send_extra(cmd_sd, sizeof(cmd_sd), 3, 6); } } } static void drv_D4D_defchar(const int ascii, const unsigned char *matrix) { /* error("drv_D4D_defchar"); */ char cmd[11]; int i; cmd[0] = 'A'; cmd[1] = 0; cmd[2] = ascii - CHAR0; for (i = 0; i < 8; i++) { cmd[i + 3] = *matrix++; } drv_D4D_send_extra(cmd, sizeof(cmd), 1, 0); } static void drv_D4D_blit(const int row, const int col, const int height, const int width) { /* error("drv_D4D_blit(%i, %i, %i, %i)",row, col, height, width); */ int r, c; RGBA rgb, pixel0_0, pixel; short int color; char colorArray[2]; /* optimization: single colour rectangle? */ pixel0_0 = drv_generic_graphic_rgb(0, 0); char unicolor = 1; for (r = row; r < row + height; r++) { if (!unicolor) break; for (c = col; c < col + width; c++) { if (!unicolor) break; pixel = drv_generic_graphic_rgb(r, c); if (pixel0_0.R != pixel.R || pixel0_0.G != pixel.G || pixel0_0.B != pixel.B || pixel0_0.A != pixel.A) unicolor = 0; } } if (unicolor) { color = RGB_24to16(pixel0_0.R, pixel0_0.G, pixel0_0.B); char row2 = row + height - 1; char cmdRect[] = { 'r', col, msb(row), lsb(row), col + width - 1, msb(row2), lsb(row2), msb(color), lsb(color) }; drv_D4D_send_extra(cmdRect, sizeof(cmdRect), 2, 5); } else { char cmd[] = { 'I', col, msb(row), lsb(row), width, msb(height), lsb(height), MODE }; drv_D4D_send_nowait_extra(cmd, sizeof(cmd), 2, 5); for (r = row; r < row + height; r++) { for (c = col; c < col + width; c++) { rgb = drv_generic_graphic_rgb(r, c); if (MODE == 8) { colorArray[0] = RGB_24to8(rgb.R, rgb.G, rgb.B); drv_D4D_send_nowait(colorArray, 1); } else { color = RGB_24to16(rgb.R, rgb.G, rgb.B); colorArray[0] = msb(color); drv_D4D_send_nowait(colorArray, 1); /* doesn't werk if sent together (error: "partial write(/dev/tts/1): len=2 ret=1") */ /* colorArray[1]=lsb(color); */ colorArray[0] = lsb(color); drv_D4D_send_nowait(colorArray, 1); } /* drv_D4D_send_nowait(colorArray, MODE/8); */ } } drv_D4D_receive_ACK(); } } static int drv_D4D_contrast(int contrast) { if (contrast < 0) contrast = 0; if (contrast > 15) contrast = 15; char cmd[] = { 'Y', 2, contrast }; drv_D4D_send(cmd, sizeof(cmd)); /* CONTRAST_=contrast; */ return contrast; } static int drv_D4D_start(const char *section) { /* error("drv_D4D_start()"); */ int contrast; int xres_cfg = -1, yres_cfg = -1; char *s; char *color; if (drv_D4D_open(section) < 0) { return -1; } char cmd[] = { 'U' }; drv_D4D_send(cmd, sizeof(cmd));
/* $Id$
 * $URL$
 *
 * plugin for asterisk
 *
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004, 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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.
 *
 * LCD4Linux 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_sample (void)
 *  adds various functions
 *
 */


/* define the include files you need */
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "debug.h"
#include "plugin.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

struct Line {
    char Channel[25];		/* Zap Channel */
    char EndPoint[25];
    unsigned char active;
};

static char *rtrim(char *string, char junk)
{
    char *original = string + strlen(string);
    while (*--original == junk);
    *(original + 1) = '\0';

    return string;
}

static void zapstatus(RESULT * result, RESULT * arg1)
{
    FILE *infile;
    int skipline = 0;		// Skip the first in the file, it throws off the detection
    char line[100], *SipLoc, Channel[25], Location[25], State[9], Application[25], EndPoint[8], Ret[50];
    int i = 0, ChannelInt = 0, ZapLine = 0;
    struct Line Lines[32];	// Setup 32 lines, ZAP 1-32 (memory is cheap)

    ZapLine = R2N(arg1);

    // Set all the lines status's default to inactive
    for (i = 0; i < 32; i++) {
	strcpy(Lines[i].Channel, "ZAP/");
	Lines[i].Channel[4] = (char) (i + 49);
	Lines[i].Channel[5] = '\0';
	Lines[i].active = 0;
    }

    system("touch /tmp/asterisk.state");	// Touch the file in it's naughty place
    system("chmod 744 /tmp/asterisk.state");
    system("asterisk -rx \"show channels\" > /tmp/asterisk.state");	// Crappy CLI way to do it

    infile = fopen("/tmp/asterisk.state", "r");

    for (i = 0; i < 100; i++) {
	line[i] = ' ';
    }
    line[99] = '\0';

    while (fgets(line, 100, infile) != NULL) {
	if (strstr(line, "Zap") != NULL) {
	    for (i = 0; i < (int) strlen(line); i++) {
		if (i < 20) {
		    Channel[i] = line[i];
		} else if (i < 42) {
		    Location[i - 21] = line[i];
		} else if (i < 50) {
		    State[i - 42] = line[i];
		} else {
		    Application[i - 50] = line[i];
		}
	    }
	    strncpy(Channel, Channel, 7);
	    Channel[7] = '\0';
	    strcpy(Location, rtrim(Location, ' '));
	    State[4] = '\0';
	    memcpy(EndPoint, Application + 13, 7);
	    EndPoint[7] = '\0';

	    if (strstr(Application, "Bridged Call") != NULL) {
		// Subtract 48 from the character value to get the int
		// value. Subtract one more because arrays start at 0.
		ChannelInt = (int) (Channel[4]) - 49;
		strcpy(Lines[ChannelInt].Channel, Channel);
		strncpy(Lines[ChannelInt].EndPoint, EndPoint, 8);
		Lines[ChannelInt].active = 1;
	    } else {
		SipLoc = strstr(Application, "SIP");
		if (SipLoc != NULL) {
		    strncpy(EndPoint, SipLoc, 7);
		} else {
		    EndPoint[0] = '\0';
		}
		ChannelInt = (int) (Channel[4]) - 49;
		strcpy(Lines[ChannelInt].Channel, Channel);
		Lines[ChannelInt].active = 1;
	    }
	} else {
	    if (strlen(line) > 54 && skipline > 1) {
		for (i = 55; i < 88; i++) {
		    if (i < 80) {
			Channel[i - 55] = line[i];
		    } else {
			EndPoint[i - 80] = line[i];
		    }
		}
		strncpy(Channel, rtrim(Channel, ' '), 5);
		strncpy(EndPoint, rtrim(EndPoint, ' '), 7);

		ChannelInt = (int) (Channel[4]) - 49;
		strcpy(Lines[ChannelInt].Channel, Channel);
		strcpy(Lines[ChannelInt].EndPoint, EndPoint);
		Lines[ChannelInt].active = 1;
	    }
	}
	skipline += 1;
    }
    fclose(infile);

    ZapLine -= 1;
    if (ZapLine < 0 || ZapLine > 31) {
	memset(Ret, ' ', 50);
	Ret[0] = '\0';
	strcat(Ret, "Invalid ZAP #");
	SetResult(&result, R_STRING, &Ret);
    } else if (Lines[ZapLine].active == 1) {
	memset(Ret, ' ', 50);
	Ret[0] = '\0';
	strcat(Ret, Lines[ZapLine].Channel);
	strcat(Ret, " -> ");
	strncat(Ret, Lines[ZapLine].EndPoint, 8);
	SetResult(&result, R_STRING, &Ret);
    } else {
	memset(Ret, ' ', 50);
	Ret[0] = '\0';
	strcat(Ret, Lines[ZapLine].Channel);
	strcat(Ret, ": inactive");
	SetResult(&result, R_STRING, &Ret);
    }
    return;
}

int plugin_init_asterisk(void)
{
    AddFunction("asterisk::zapstatus", 1, zapstatus);
    return 0;
}

void plugin_exit_asterisk(void)
{
    /* free any allocated memory */
    /* close filedescriptors */
}