From a8bc5d3f8cd0c2063f60ccc242131e48675b6971 Mon Sep 17 00:00:00 2001
From: michux <michux@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Date: Fri, 4 Apr 2008 08:42:02 +0000
Subject: Add asterisk plugin, thanks to  mcarey@yostengineering.com

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@866 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
---
 plugin.c          |   8 +++
 plugin_asterisk.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 plugins.m4        |  10 ++-
 3 files changed, 197 insertions(+), 1 deletion(-)
 create mode 100644 plugin_asterisk.c

diff --git a/plugin.c b/plugin.c
index 1437c00..3581229 100644
--- a/plugin.c
+++ b/plugin.c
@@ -58,6 +58,8 @@ void plugin_exit_time(void);
 
 int plugin_init_apm(void);
 void plugin_exit_apm(void);
+int plugin_init_asterisk(void);
+void plugin_exit_asterisk(void);
 int plugin_init_cpuinfo(void);
 void plugin_exit_cpuinfo(void);
 int plugin_init_diskstats(void);
@@ -127,6 +129,9 @@ int plugin_init(void)
 #ifdef PLUGIN_APM
     plugin_init_apm();
 #endif
+#ifdef PLUGIN_ASTERISK
+    plugin_init_asterisk();
+#endif
 #ifdef PLUGIN_CPUINFO
     plugin_init_cpuinfo();
 #endif
@@ -222,6 +227,9 @@ void plugin_exit(void)
 #ifdef PLUGIN_APM
     plugin_exit_apm();
 #endif
+#ifdef PLUGIN_ASTERISK
+    plugin_exit_asterisk();
+#endif
 #ifdef PLUGIN_CPUINFO
     plugin_exit_cpuinfo();
 #endif
diff --git a/plugin_asterisk.c b/plugin_asterisk.c
new file mode 100644
index 0000000..75b2f52
--- /dev/null
+++ b/plugin_asterisk.c
@@ -0,0 +1,180 @@
+/* $Id: plugin_sample.c 840 2007-09-09 12:17:42Z michael $
+ * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_sample.c $
+ *
+ * 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], inp;
+    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");
+    while (fgets(line, 100, infile) != NULL) {
+	if (strstr(line, "Zap") != NULL) {
+	    for (i = 0; i < 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);
+		strcpy(Lines[ChannelInt].EndPoint, EndPoint);
+		Lines[ChannelInt].active = 1;
+	    }
+	} else {
+	    if (strlen(line) > 54 && skipline > 1) {
+		for (i = 55; i < 88; i++) {
+		    if (i < 40) {
+			Channel[i - 55] = line[i];
+		    } else {
+			EndPoint[i - 40] = 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 (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 */
+}
diff --git a/plugins.m4 b/plugins.m4
index 9f39c42..8c80b33 100644
--- a/plugins.m4
+++ b/plugins.m4
@@ -54,7 +54,8 @@ for plugin in $plugins; do
          ;;
       all)
          PLUGIN_APM="yes"
-				 PLUGIN_BUTTON_EXEC="yes"
+	 PLUGIN_BUTTON_EXEC="yes"
+	 PLUGIN_ASTERISK="yes"
          PLUGIN_CPUINFO="yes"
          PLUGIN_DISKSTATS="yes"
          PLUGIN_DVB="yes"
@@ -89,6 +90,9 @@ for plugin in $plugins; do
 			button_exec)
 			   PLUGIN_BUTTON_EXEC=$val
 				 ;;
+      asterisk)
+         PLUGIN_ASTERISK=$val
+         ;;
       cpuinfo)
          PLUGIN_CPUINFO=$val
          ;;
@@ -185,6 +189,10 @@ if test "$PLUGIN_BUTTON_EXEC" = "yes"; then
    PLUGINS="$PLUGINS plugin_button_exec.o"
    AC_DEFINE(PLUGIN_BUTTON_EXEC,1,[button_exec plugin])
 fi
+if test "$PLUGIN_ASTERISK" = "yes"; then
+   PLUGINS="$PLUGINS plugin_asterisk.o"
+   AC_DEFINE(PLUGIN_ASTERISK,1,[asterisk plugin])
+fi
 if test "$PLUGIN_CPUINFO" = "yes"; then
    PLUGINS="$PLUGINS plugin_cpuinfo.o"
    AC_DEFINE(PLUGIN_CPUINFO,1,[cpuinfo plugin])
-- 
cgit v1.2.3