From bd0a099495db37954575879b1322cf60e06311f7 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 28 Aug 2007 01:30:19 +0000 Subject: Can't call system open(), read(), etc. git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@315 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/arch/sim/src/up_tapdev.c | 117 +++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 21 deletions(-) diff --git a/nuttx/arch/sim/src/up_tapdev.c b/nuttx/arch/sim/src/up_tapdev.c index e03304846..539667e6d 100644 --- a/nuttx/arch/sim/src/up_tapdev.c +++ b/nuttx/arch/sim/src/up_tapdev.c @@ -70,6 +70,8 @@ # define DEVTAP "/dev/tap0" #endif /* linux */ +extern int lib_rawprintf(const char *format, ...); + /**************************************************************************** * Private Definitions ****************************************************************************/ @@ -81,6 +83,12 @@ #define UIP_DRIPADDR2 0 #define UIP_DRIPADDR3 1 +#define READ 3 +#define WRITE 4 +#define OPEN 5 +#define IOCTL 54 +#define SELECT 82 + /**************************************************************************** * Private Types ****************************************************************************/ @@ -94,14 +102,81 @@ ****************************************************************************/ #ifdef TAPDEV_DEBUG -static int drop = 0; +static int gdrop = 0; #endif -static int fd; +static int gtapdevfd; /**************************************************************************** * Private Functions ****************************************************************************/ +/* This is REALLY awkward.. we need to compile using the system header files, + * but we can't use any of the libc calls because all of the symbols are + * defined for NuttX (read, write, etc)! So we do hand rolled syscalls + * to get to the Linux functions. + */ + +static inline int up_open(const char *filename, int flags, int mode) +{ + int result; + + __asm__ volatile ("int $0x80" \ + : "=a" (result) \ + : "0" (OPEN), "b" ((int)(filename)), "c" ((int)(flags)), "d" ((int)(mode)) \ + : "memory"); + + return (int)result; +} + +static inline int up_read(int fd, void* buf, size_t count) +{ + ssize_t result; + + __asm__ volatile ("int $0x80" \ + : "=a" (result) \ + : "0" (READ), "b" ((int)(fd)), "c" ((int)(buf)), "d" ((int)(count)) \ + : "memory"); + + return (int)result; +} + +static inline int up_write(int fd, const void* buf, size_t count) +{ + ssize_t result; + + __asm__ volatile ("int $0x80" \ + : "=a" (result) \ + : "0" (WRITE), "b" ((int)(fd)), "c" ((int)(buf)), "d" ((int)(count)) \ + : "memory"); + + return (int)result; +} + +static inline int up_ioctl(int fd, unsigned int cmd, unsigned long arg) +{ + ssize_t result; + + __asm__ volatile ("int $0x80" \ + : "=a" (result) \ + : "0" (IOCTL), "b" ((int)(fd)), "c" ((int)(cmd)), "d" ((long)(arg)) \ + : "memory"); + + return (int)result; +} + +static inline int up_select(int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval *tvp) +{ + ssize_t result; + + __asm__ volatile ("int $0x80" \ + : "=a" (result) \ + : "0" (SELECT),"b" ((long)(n)),"c" ((long)(inp)), \ + "d" ((long)(outp)),"S" ((long)(exp)), "D"((long)tvp) \ + : "memory"); + + return (int)result; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -117,10 +192,10 @@ void tapdev_init(void) { char buf[1024]; - fd = open(DEVTAP, O_RDWR); - if(fd == -1) + gtapdevfd = up_open(DEVTAP, O_RDWR, 0644); + if(gtapdevfd == -1) { - printf("tapdev: tapdev_init: open"); + lib_rawprintf("tapdev: tapdev_init: open"); return; } @@ -129,16 +204,16 @@ void tapdev_init(void) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TAP|IFF_NO_PI; - if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) + if (up_ioctl(gtapdevfd, TUNSETIFF, (void *) &ifr) < 0) { - printf(buf); + lib_rawprintf(buf); return; } } #endif /* Linux */ - snprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d", - UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3); + lib_rawprintf(buf, sizeof(buf), "ifconfig tap0 inet %d.%d.%d.%d", + UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3); system(buf); } @@ -152,29 +227,29 @@ unsigned int tapdev_read(char *buf, unsigned int buflen) tv.tv_usec = 1000; FD_ZERO(&fdset); - FD_SET(fd, &fdset); + FD_SET(gtapdevfd, &fdset); - ret = select(fd + 1, &fdset, NULL, NULL, &tv); + ret = up_select(gtapdevfd + 1, &fdset, NULL, NULL, &tv); if(ret == 0) { return 0; } - ret = read(fd, buf, buflen); + ret = up_read(gtapdevfd, buf, buflen); if(ret == -1) { - printf("tap_dev: tapdev_read: read"); + lib_rawprintf("tap_dev: tapdev_read: read"); } #ifdef TAPDEV_DEBUG - printf("tap_dev: tapdev_read: read %d bytes\n", ret); + lib_rawprintf("tap_dev: tapdev_read: read %d bytes\n", ret); { int i; for(i = 0; i < 20; i++) { - printf("%x ", buf[i]); + lib_rawprintf("%x ", buf[i]); } - printf("\n"); + lib_rawprintf("\n"); } #endif @@ -185,17 +260,17 @@ void tapdev_send(char *buf, unsigned int buflen) { int ret; #ifdef TAPDEV_DEBUG - printf("tapdev_send: sending %d bytes\n", buflen); + lib_rawprintf("tapdev_send: sending %d bytes\n", buflen); - drop++; - if(drop % 8 == 7) + gdrop++; + if(gdrop % 8 == 7) { - printf("Dropped a packet!\n"); + lib_rawprintf("Dropped a packet!\n"); return; } #endif - ret = write(fd, buf, buflen); + ret = up_write(gtapdevfd, buf, buflen); if(ret == -1) { perror("tap_dev: tapdev_send: write"); -- cgit v1.2.3