Ignore tags files, merge.
[jelmer/at89prog.git] / pins.c
diff --git a/pins.c b/pins.c
index 1b8f14d7abc001c7c35fabb5224f91bc48f84a97..bfd9ec9dc41251935d679f3213961cef787a3542 100644 (file)
--- a/pins.c
+++ b/pins.c
@@ -1,3 +1,21 @@
+/* 
+       at89prog
+       (c) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
+
+       This program 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 of the License, or
+       (at your option) any later version.
+
+       This program 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.
+*/
 #include <stdio.h>
 #include "pins.h"
 #include <signal.h>
 #define PIN_CHK  3
 #define PIN_MISO 4
 
-char *location = NULL;
-char *pin_names[] = { "MOSI", "SCK", "RST", "CHK", "MISO", NULL };
-int pin_mapping[5] = { -1, -1, -1, -1, -1 };
-int pin_reverse[5] = { 0, 0, 0, 0, 0 };
+
+
+#define MAXLEN 100
+#define MAXSETTINGS 100
 
 static struct pins_backend *backend = NULL;
-extern struct pins_backend serial_raw, serial;
+extern struct pins_backend serial_raw, serial, parallel, ftdi;
 static struct pins_backend *pins_backends[] = {
-       &serial_raw,
        &serial,
+       &serial_raw,
+       &parallel,
+#ifdef HAVE_FTDI
+       &ftdi,
+#endif
        NULL
 };
 
+char *location = NULL;
+char *pin_names[] = { "MOSI", "SCK", "RST", "CHK", "MISO", NULL };
+int pin_mapping[5] = { -1, -1, -1, -1, -1 };
+int pin_reverse[5] = { 0, 0, 0, 0, 0 };
+
+static struct confsetting {
+       char name[MAXLEN];
+       char value[MAXLEN];
+} confsettings[MAXSETTINGS];
+
+static int num_confsettings = 0;
+
 int get_pin_id(char *name)
 {
        int i;
@@ -46,6 +80,66 @@ int backend_get_pin_id(char *name)
        return -1;
 }
 
+int pins_read_config_file(const char *name) 
+{
+       char bit[16], pin[16];
+       int line = 0;
+       FILE *fd = fopen(name, "r");
+
+       if(!fd) { 
+               fprintf(stderr, "Can't open %s!\n"
+                                               "Type 'man 5 at89prog' for information about the RC file format\n", name);
+               perror("fopen"); 
+               return 1; 
+       }
+
+       while(!feof(fd)) 
+       {
+               line++;
+               if(fscanf(fd, "%15s %15s\n", bit, pin) < 2) {
+                       fprintf(stderr, "Invalid line %d in %s, ignoring\n", line, name);
+                       continue;
+               }
+
+               if(!strcasecmp(bit, "type")) pins_set_backend(pin);
+               else if(!strcasecmp(bit, "port")) pins_set_location(pin);
+               else {
+                       if(num_confsettings > MAXSETTINGS) {
+                               fprintf(stderr, "Maximum number of %d configuration settings reached\n", num_confsettings);
+                               return -1;
+                       }
+                       strncpy(confsettings[num_confsettings].name, bit, MAXLEN);
+                       strncpy(confsettings[num_confsettings].value, pin, MAXLEN);
+                       num_confsettings++;
+               }
+       }
+
+       fclose(fd);
+       return 0;
+}
+
+int pins_set_backend(char *name) 
+{
+       int i;
+       for(i = 0; pins_backends[i]; i++) {
+               if(!strcmp(pins_backends[i]->name, name)) {
+                       backend = pins_backends[i];
+                       return 0;
+               }
+       }
+       
+       fprintf(stderr, "No such port type '%s'\n", name);
+       return -1;
+}
+
+
+
+int pins_set_location(char *loc)
+{
+       location = strdup(loc);
+       return 0;
+}
+
 int SetPinVariable(char *name, char *value) 
 {
        int i, j;
@@ -58,7 +152,7 @@ int SetPinVariable(char *name, char *value)
 
        if(name[0] == '!') {
                reverse = 1; 
-               i = get_pin_id(name); 
+               i = get_pin_id(&name[1]); 
        } else {
                i = get_pin_id(name);
        }
@@ -77,6 +171,27 @@ int SetPinVariable(char *name, char *value)
        return -1;
 }
 
+int pins_init()
+{
+       int i;
+       if(!backend) backend = pins_backends[0];
+       for(i = 0; i < num_confsettings; i++) {
+               switch(SetPinVariable(confsettings[i].name, confsettings[i].value)) {
+               case 0:break;
+               case -1:
+                          fprintf(stderr, "Unknown PIN '%s'\n", confsettings[i].name);
+                          return -1;
+               case -2:
+                          fprintf(stderr, "Unknown PIN '%s'\n", confsettings[i].value);
+                          return -1;
+               }
+       }
+
+       return backend->init(location);
+}
+
+
+
 void SetMOSI() { 
        if(pin_mapping[PIN_MOSI] == -1) return;
        if(pin_reverse[PIN_MOSI]) backend->clear(pin_mapping[PIN_MOSI]);
@@ -125,25 +240,7 @@ int GetMISO() {
        return backend->get(pin_mapping[PIN_MISO]); 
 }
 
-int LoadPinBackend(char *name) 
-{
-       int i;
-       for(i = 0; pins_backends[i]; i++) {
-               if(!strcmp(pins_backends[i]->name, name)) {
-                       backend = pins_backends[i];
-                       return 0;
-               }
-       }
-       fprintf(stderr, "No such port type '%s'\n", name);
-       return -1;
-}
-
-int BackendInit()
-{
-       return backend->init(location);
-}
-
-int ClosePinBackend() 
+int pins_fini() 
 { 
        if(backend->close) return backend->close(); 
        return 0;