⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gecko.c

📁 WiiFuse is a FUSE filesystem module for Linux and Mac OS X. It will let you mount a Wii disc ISO an
💻 C
字号:
/*
 *  Copyright (C) 2008 dhewg, #wiidev efnet
 *
 *  this file is part of wiifuse
 *  http://wiibrew.org/index.php?title=Wiifuse
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

#include "gecko.h"

#define FTDI_PACKET_SIZE 3968

// TODO
// this whole approach blocks, how do i implement a timeout with tty's?

static int fd_gecko = -1;

int gecko_open (const char *dev) {
        struct termios newtio;

        fd_gecko = open (dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);

        if (fd_gecko == -1) {
                perror ("gecko_open");
                return 1;
        }

        if (fcntl (fd_gecko, F_SETFL, 0)) {
                perror ("F_SETFL on serial port");
                return 1;
        }

        if (tcgetattr(fd_gecko, &newtio)) {
                perror ("tcgetattr");
                return 1;
        }

        cfmakeraw (&newtio);

        newtio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD;

        if (tcsetattr (fd_gecko, TCSANOW, &newtio)) {
                perror ("tcsetattr");
                return 1;
        }
 
        gecko_flush ();

        return 0;
}

void gecko_close () {
        if (fd_gecko > 0)
                close (fd_gecko);
}

void gecko_flush () {
        // TODO doesnt seem to work with ftdi-sio
        // i need a way to check if data is actually available
        tcflush (fd_gecko, TCIOFLUSH);
}

int gecko_read (void *buf, size_t count) {
        size_t left, chunk, res;

        left = count;
        while (left) {
                chunk = left;
                if (chunk > FTDI_PACKET_SIZE)
                        chunk = FTDI_PACKET_SIZE;

                res = read (fd_gecko, buf, chunk);
                if (res < 1) {
                        perror ("gecko_read");
                        return 1;
                }

                left -= res;
                buf += res;
        }

        return 0;
}

int gecko_write (const void *buf, size_t count) {
        size_t left, chunk, res;

        left = count;

        while (left) {
                chunk = left;
                if (chunk > FTDI_PACKET_SIZE)
                        chunk = FTDI_PACKET_SIZE;

                res = write (fd_gecko, buf, count);
                if (res < 1) {
                        perror ("gecko_write");
                        return 1;
                }

                left -= res;
                buf += res;

                // does this work with ftdi-sio?
                if (tcdrain (fd_gecko)) {
                        perror ("gecko_drain");
                        return 1;
                }

        }

        return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -