sim-card
/
sam7-util
Archived
10
0
Fork 0

sam: Select the IO Backend at runtime when a device name is specified

The configure part is more messy than I had hoped for. One can not
just specify @VARIABLE_THAT_HAS_SOURCES@, I need to go through the
every backend and add the sourcecode.
This commit is contained in:
Holger Hans Peter Freyther 2011-10-09 18:37:31 +02:00
parent 587730174f
commit 74ae64b355
5 changed files with 48 additions and 19 deletions

View File

@ -4,7 +4,21 @@ AUTOMAKE_OPTIONS = gnu
bin_PROGRAMS = sam7
sam7_SOURCES = main.c io.c io_@IOTYPE@.c samba.c cmd.c
sam7_SOURCES = main.c io.c samba.c cmd.c
if HAVE_IOKIT
sam7_SOURCES += io_iokit.c
endif
if HAVE_WIN32
sam7_SOURCES += io_win32.c
endif
if HAVE_POSIX
sam7_SOURCES += io_posix.c
endif
if HAVE_LIBUSB
sam7_SOURCES += io_libusb.c
endif
noinst_HEADERS = io.h samba.h cmd.h loader128_data.h loader256_data.h
EXTRA_DIST = driver/Makefile driver/at91.c \

View File

@ -15,14 +15,15 @@ AC_PROG_MAKE_SET
case "${host}" in
*-*-darwin* )
LIBS="$LIBS -framework IOKIT -framework CoreFoundation"
IOTYPE="iokit"
have_iokit="true"
;;
*-*-cygwin* )
LIBS="$LIBS -lsetupapi"
IOTYPE="win32"
have_win32="true"
;;
* )
AC_CHECK_LIB(usb,usb_init,IOTYPE="libusb";LIBS="$LIBS -lusb",IOTYPE="posix")
have_posix="true"
AC_CHECK_LIB(usb,usb_init,have_libusb="true";LIBS="$LIBS -lusb")
;;
esac
@ -37,8 +38,9 @@ AC_CHECK_HEADERS(ctype.h,,)
AC_CHECK_HEADERS(endian.h,,)
AC_SUBST(IOTYPE)
AM_CONDITIONAL([HAVE_IOKIT], [test x$have_iokit = xtrue])
AM_CONDITIONAL([HAVE_WIN32], [test x$have_win32 = xtrue])
AM_CONDITIONAL([HAVE_POSIX], [test x$have_posix = xtrue])
AM_CONDITIONAL([HAVE_LIBUSB],[test x$have_libusb = xtrue])
AC_OUTPUT(Makefile)

30
io.c
View File

@ -31,14 +31,7 @@ int io_driver_register(struct io_driver *driver)
return 0;
}
struct io_driver *io_driver_at(int i)
{
if (i >= last_driver)
return NULL;
return drivers[i];
}
void io_driver_activate(struct io_driver *driver)
static void io_driver_activate(struct io_driver *driver)
{
printf("Activating driver: %s\n", driver->name);
g_driver = driver;
@ -63,3 +56,24 @@ int io_read(void *buff, int len)
{
return g_driver->io_read(buff, len);
}
void io_driver_select_backend(const char *name)
{
int i;
struct io_driver *driver = drivers[0];
for ( i = 0; ;++i) {
if (!drivers[i])
break;
if (name && strcmp("posix", drivers[i]->name) == 0) {
driver = drivers[i];
break;
} else if (!name && strcmp("libusb", drivers[i]->name) == 0) {
driver = drivers[i];
break;
}
}
io_driver_activate(driver);
}

3
io.h
View File

@ -28,8 +28,7 @@ struct io_driver {
};
int io_driver_register(struct io_driver *driver);
struct io_driver *io_driver_at(int);
void io_driver_activate(struct io_driver *driver);
void io_driver_select_backend(const char *line);
int io_init( char *dev );
int io_cleanup( void );

4
main.c
View File

@ -88,8 +88,8 @@ int main( int argc, char *argv[] )
}
}
/* Activate a default driver */
io_driver_activate(io_driver_at(0));
/* If we have a path, attempt to use the posix backend */
io_driver_select_backend(line);
if( io_init( line ) < 0 ) {
fprintf(stderr, "Failed to initialize the SAM7 device.\n");