Changeset 869

Show
Ignore:
Timestamp:
04/10/08 16:45:45 (7 months ago)
Author:
michux
Message:

Add basic FIFO plugin

Location:
trunk
Files:
3 modified
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Makefile.am

    r868 r869  
    115115plugin_dvb.c                  \ 
    116116plugin_exec.c                 \ 
     117plugin_fifo.c                 \ 
    117118plugin_file.c                 \ 
    118119plugin_gps.c                  \ 
  • trunk/plugin.c

    r866 r869  
    6969int plugin_init_exec(void); 
    7070void plugin_exit_exec(void); 
     71int plugin_init_fifo(void); 
     72void plugin_exit_fifo(void); 
    7173int plugin_init_file(void); 
    7274void plugin_exit_file(void); 
     
    145147    plugin_init_exec(); 
    146148#endif 
     149#ifdef PLUGIN_FIFO 
     150    plugin_init_fifo(); 
     151#endif 
    147152#ifdef PLUGIN_FILE 
    148153    plugin_init_file(); 
     
    242247#ifdef PLUGIN_EXEC 
    243248    plugin_exit_exec(); 
     249#endif 
     250#ifdef PLUGIN_FIFO 
     251    plugin_exit_fifo(); 
    244252#endif 
    245253#ifdef PLUGIN_FILE 
  • trunk/plugin_fifo.c

    r840 r869  
    44 * plugin template 
    55 * 
    6  * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at> 
    7  * Copyright (C) 2004, 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> 
     6 * Copyright (C) 2008 Michael Vogt <michu@neophob.com> 
     7 * Copyright (C) 2004, 2005, 2006, 2007, 2008 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> 
    88 * 
    99 * This file is part of LCD4Linux. 
     
    2323 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
    2424 * 
    25  */ 
    26  
     25 */   
     26     
    2727/*  
    28  * exported functions: 
    29  * 
    30  * int plugin_init_sample (void) 
    31  *  adds various functions 
    32  * 
    33  */ 
    34  
    35  
    36 /* define the include files you need */ 
     28 * Quick fifo hack for lcd4linux 
     29 *  
     30 * most code is ripped ... 
     31 * 
     32 */  
     33     
     34 
     35/* define the include files you need */  
    3736#include "config.h" 
    38  
     37     
    3938#include <stdlib.h> 
    4039#include <string.h> 
    4140#include <ctype.h> 
    42  
    43 /* these should always be included */ 
     41#include <errno.h> 
     42#include <unistd.h> 
     43#include <fcntl.h> 
     44#include <sys/stat.h> 
     45     
     46/* these should always be included */  
    4447#include "debug.h" 
    4548#include "plugin.h" 
    46  
     49#include "cfg.h" 
     50     
    4751#ifdef WITH_DMALLOC 
    4852#include <dmalloc.h> 
    49 #endif 
    50  
    51  
    52  
    53 /* sample function 'mul2' */ 
    54 /* takes one argument, a number */ 
    55 /* multiplies the number by 2.0 */ 
    56 /* Note: all local functions should be declared 'static' */ 
    57  
    58 static void my_mul2(RESULT * result, RESULT * arg1) 
    59 { 
    60     double param; 
    61     double value; 
    62  
    63     /* Get Parameter */ 
    64     /* R2N stands for 'Result to Number' */ 
    65     param = R2N(arg1); 
    66  
    67     /* calculate value */ 
    68     value = param * 2.0; 
    69  
    70     /* store result */ 
    71     /* when called with R_NUMBER, it assumes the */ 
    72     /* next parameter to be a pointer to double */ 
    73     SetResult(&result, R_NUMBER, &value); 
    74 } 
    75  
    76  
    77 /* sample function 'mul3' */ 
    78 /* takes one argument, a number */ 
    79 /* multiplies the number by 3.0 */ 
    80 /* same as 'mul2', but shorter */ 
    81  
    82 static void my_mul3(RESULT * result, RESULT * arg1) 
    83 { 
    84     /* do it all in one line */ 
    85     double value = R2N(arg1) * 3.0; 
    86  
    87     /* store result */ 
    88     SetResult(&result, R_NUMBER, &value); 
    89 } 
    90  
    91  
    92 /* sample function 'diff' */ 
    93 /* takes two arguments, both numbers */ 
    94 /* returns |a-b| */ 
    95  
    96 static void my_diff(RESULT * result, RESULT * arg1, RESULT * arg2) 
    97 { 
    98     /* do it all in one line */ 
    99     double value = R2N(arg1) - R2N(arg2); 
    100  
    101     /* some more calculations... */ 
    102     if (value < 0) 
    103   value = -value; 
    104  
    105     /* store result */ 
    106     SetResult(&result, R_NUMBER, &value); 
    107 } 
    108  
    109  
    110 /* sample function 'answer' */ 
    111 /* takes no argument! */ 
    112 /* returns the answer to all questions */ 
    113  
    114 static void my_answer(RESULT * result) 
    115 { 
    116     /* we have to declare a variable because */ 
    117     /* SetResult needs a pointer  */ 
    118     double value = 42; 
    119  
    120     /* store result */ 
    121     SetResult(&result, R_NUMBER, &value); 
    122 } 
    123  
    124  
    125 /* sample function 'length' */ 
    126 /* takes one argument, a string */ 
    127 /* returns the string length */ 
    128  
    129 static void my_length(RESULT * result, RESULT * arg1) 
    130 { 
    131     /* Note #1: value *must* be double!  */ 
    132     /* Note #2: R2S stands for 'Result to String' */ 
    133     double value = strlen(R2S(arg1)); 
    134  
    135     /* store result */ 
    136     SetResult(&result, R_NUMBER, &value); 
    137 } 
    138  
    139  
    140  
    141 /* sample function 'upcase' */ 
    142 /* takes one argument, a string */ 
    143 /* returns the string in upper case letters */ 
    144  
    145 static void my_upcase(RESULT * result, RESULT * arg1) 
    146 { 
    147     char *value, *p; 
    148  
    149     /* create a local copy of the argument */ 
    150     /* Do *NOT* try to modify the original string! */ 
    151     value = strdup(R2S(arg1)); 
    152  
    153     /* process the string */ 
    154     for (p = value; *p != '\0'; p++) 
    155   *p = toupper(*p); 
    156  
    157     /* store result */ 
    158     /* when called with R_STRING, it assumes the */ 
    159     /* next parameter to be a pointer to a string */ 
    160     /* 'value' is already a char*, so use 'value', not '&value' */ 
    161     SetResult(&result, R_STRING, value); 
    162  
    163     /* free local copy again */ 
    164     /* Note that SetResult() makes its own string copy  */ 
    165     free(value); 
    166 } 
    167  
    168  
    169 /* sample function 'cat' */ 
    170 /* takes variable number of arguments, all strings */ 
    171 /* returns all prameters concatenated */ 
    172  
    173 static void my_concat(RESULT * result, int argc, RESULT * argv[]) 
    174 { 
    175     int i, len; 
    176     char *value, *part; 
    177  
    178     /* start with a empty string */ 
    179     value = strdup(""); 
    180  
    181     /* process all arguments */ 
    182     for (i = 0; i < argc; i++) { 
    183   part = R2S(argv[i]); 
    184   len = strlen(value) + strlen(part); 
    185   value = realloc(value, len + 1); 
    186   strcat(value, part); 
    187     } 
    188  
    189     /* store result */ 
    190     SetResult(&result, R_STRING, value); 
    191  
    192     /* free local string */ 
    193     free(value); 
    194 } 
    195  
    196  
    197 /* plugin initialization */ 
    198 /* MUST NOT be declared 'static'! */ 
    199 int plugin_init_sample(void) 
    200 { 
    201  
    202     /* register all our cool functions */ 
    203     /* the second parameter is the number of arguments */ 
    204     /* -1 stands for variable argument list */ 
    205     AddFunction("sample::mul2", 1, my_mul2); 
    206     AddFunction("sample::mul3", 1, my_mul3); 
    207     AddFunction("sample::answer", 0, my_answer); 
    208     AddFunction("sample::diff", 2, my_diff); 
    209     AddFunction("sample::length", 1, my_length); 
    210     AddFunction("sample::upcase", 1, my_upcase); 
    211     AddFunction("sample::concat", -1, my_concat); 
    212  
    213     return 0; 
    214 } 
    215  
    216 void plugin_exit_sample(void) 
    217 { 
    218     /* free any allocated memory */ 
    219     /* close filedescriptors */ 
    220 } 
     53#endif  /*  
     54 */ 
     55     
     56#define FIFO_BUFFER_SIZE 80 
     57     
     58typedef struct _FifoData { 
     59     
     60char *path; 
     61     
     62 int input; 
     63     
     64 int created; 
     65 
     66  
     67} FifoData; 
     68 
     69 
     70static char Section[] = "Plugin:FIFO"; 
     71 
     72 
     73static FifoData fd; 
     74 
     75static char msg[FIFO_BUFFER_SIZE]; 
     76 
     77static char fifopath[1024]; 
     78 
     79 
     80static void configure_fifo(void)  
     81{ 
     82     
     83char *s; 
     84     
     85s = cfg_get(Section, "fifopath", "/tmp/lcd4linux.fifo"); 
     86     
     87if (*s == '\0') { 
     88   
     89info("[FIFO] empty '%s.fifopath' entry from %s, assuming '/tmp/lcd4linux.fifo'", Section, cfg_source()); 
     90   
     91strcpy(fifopath, "/tmp/lcd4linux.fifo"); 
     92     
     93} else 
     94   
     95strcpy(fifopath, s); 
     96     
     97 
     98free(s); 
     99 
     100} 
     101 
     102 
     103 
     104static void removeFifo()  
     105{ 
     106     
     107debug("Removing FIFO \"%s\"\n", fd.path); 
     108     
     109 
     110if (unlink(fd.path) < 0) { 
     111   
     112error("Could not remove FIFO \"%s\": %s\n", fd.path, strerror(errno)); 
     113   
     114return; 
     115     
     116} 
     117     
     118 
     119fd.created = 0; 
     120 
     121} 
     122 
     123 
     124static void closeFifo()  
     125{ 
     126     
     127struct stat st; 
     128     
     129 
     130if (fd.input >= 0) { 
     131   
     132close(fd.input); 
     133   
     134fd.input = -1; 
     135     
     136} 
     137     
     138 
     139if (fd.created && (stat(fd.path, &st) == 0)) 
     140   
     141removeFifo(fd); 
     142 
     143} 
     144 
     145 
     146static int makeFifo()  
     147{ 
     148     
     149if (mkfifo(fd.path, 0666) < 0) { 
     150   
     151error("Couldn't create FIFO \"%s\": %s\n", fd.path, strerror(errno)); 
     152   
     153return -1; 
     154     
     155} 
     156     
     157 
     158fd.created = 1; 
     159     
     160 
     161return 0; 
     162 
     163} 
     164 
     165 
     166 
     167static int checkFifo()  
     168{ 
     169     
     170struct stat st; 
     171     
     172if (stat(fd.path, &st) < 0) { 
     173   
     174if (errno == ENOENT) { 
     175       
     176    /* Path doesn't exist */  
     177    return makeFifo(fd); 
     178   
     179} 
     180   
     181 
     182error("Failed to stat FIFO \"%s\": %s\n", fd.path, strerror(errno)); 
     183   
     184return -1; 
     185     
     186} 
     187     
     188 
     189if (!S_ISFIFO(st.st_mode)) { 
     190   
     191error("\"%s\" already exists, but is not a FIFO\n", fd.path); 
     192   
     193return -1; 
     194     
     195} 
     196     
     197 
     198return 0; 
     199 
     200} 
     201 
     202 
     203static int openFifo()  
     204{ 
     205     
     206if (checkFifo() < 0) 
     207   
     208return -1; 
     209     
     210 
     211fd.input = open(fd.path, O_RDONLY | O_NONBLOCK); 
     212     
     213if (fd.input < 0) { 
     214   
     215error("Could not open FIFO \"%s\" for reading: %s\n", fd.path, strerror(errno)); 
     216   
     217closeFifo(); 
     218   
     219return -1; 
     220     
     221} 
     222     
     223 
     224return 0; 
     225 
     226} 
     227 
     228 
     229static void fiforead(RESULT * result)  
     230{ 
     231     
     232char buf[FIFO_BUFFER_SIZE]; 
     233     
     234int i, bytes = 1; 
     235     
     236memset(buf, 0, FIFO_BUFFER_SIZE); 
     237     
     238 
     239strcat(buf, "ERROR"); 
     240     
     241if (checkFifo() == 0) { 
     242   
     243memset(buf, 0, FIFO_BUFFER_SIZE); 
     244   
     245while (bytes > 0 && errno != EINTR) 
     246       
     247bytes = read(fd.input, buf, FIFO_BUFFER_SIZE); 
     248   
     249 
     250if (bytes < 0) { 
     251       
     252error("[FIFO] Error %i: %s\n", errno, strerror(errno)); 
     253   
     254} else { 
     255       
     256 
     257if (strlen(buf) > 0) { 
     258     
     259strcpy(msg, buf); 
     260       
     261} 
     262       
     263 
     264for (i = 0; i < strlen(buf); i++) { 
     265     
     266if (msg[i] < 0x20) 
     267         
     268msg[i] = ' '; 
     269       
     270} 
     271   
     272} 
     273     
     274} 
     275     
     276 
     277  /* store result */  
     278  SetResult(&result, R_STRING, msg); 
     279 
     280} 
     281 
     282 
     283 
     284/* plugin initialization */  
     285int plugin_init_fifo(void)  
     286{ 
     287     
     288configure_fifo(); 
     289     
     290 
     291fd.path = fifopath; 
     292     
     293fd.input = -1; 
     294     
     295fd.created = 0; 
     296     
     297 
     298if (openFifo() < 0) { 
     299   
     300return -1; 
     301     
     302} 
     303     
     304 
     305memset(msg, 0, FIFO_BUFFER_SIZE); 
     306     
     307AddFunction("fifo::read", 0, fiforead); 
     308     
     309 
     310return 0; 
     311 
     312} 
     313 
     314 
     315void plugin_exit_fifo(void)  
     316{ 
     317     
     318  /* close filedescriptors */  
     319  closeFifo(); 
     320 
     321}  
  • trunk/plugins.m4

    r867 r869  
    6161         PLUGIN_DVB="yes" 
    6262         PLUGIN_EXEC="yes" 
     63         PLUGIN_FIFO="yes" 
    6364         PLUGIN_FILE="yes" 
    6465         PLUGIN_GPS="yes" 
     
    106107         PLUGIN_EXEC=$val 
    107108         ;; 
     109      fifo) 
     110         PLUGIN_FIFO=$val 
     111         ;; 
    108112      file) 
    109113         PLUGIN_FILE=$val 
     
    219223   PLUGINS="$PLUGINS plugin_file.o" 
    220224   AC_DEFINE(PLUGIN_FILE,1,[file plugin]) 
     225fi 
     226if test "$PLUGIN_FIFO" = "yes"; then 
     227   PLUGINS="$PLUGINS plugin_fifo.o" 
     228   AC_DEFINE(PLUGIN_FIFO,1,[fifo plugin]) 
    221229fi 
    222230if test "$PLUGIN_GPS" = "yes"; then