f37022007704dac1c6778f62f2a6fb8a9d0e9281
[cascardo/b2hw.git] / main.c
1 /*
2  * Copyright (C) 2016  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, visit the http://fsf.org website.
16  */
17
18 #include "init.h"
19 #include <stdio.h>
20
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <sys/ioctl.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #define PREFIX "/sys/class/android_usb/android0/"
31
32 int do_write(char *fname, char *text)
33 {
34         int fd;
35         fd = open(fname, O_WRONLY);
36         write(fd, text, strlen(text));
37         close(fd);
38         return 0;
39 }
40
41 int do_read(char *fname, char *buffer, size_t len)
42 {
43         int fd;
44         int r;
45         fd = open(fname, O_RDONLY);
46         r = read(fd, buffer, len);
47         close(fd);
48         return r;
49 }
50
51 int main(int argc, char **argv)
52 {
53         int r;
54         int fd = -1;
55         char dev[33];
56         int maj, min;
57         r = devmount_setup();
58         if (r < 0) {
59                 fprintf(stderr, "failed to mount devtmpfs, proceeding anyway\n");
60         }
61         r = sysfsmount_setup();
62         if (r < 0) {
63                 fprintf(stderr, "failed to mount sysfs, proceeding anyway\n");
64         }
65         do_write(PREFIX "/enable", "0");
66         do_write(PREFIX "/idVendor", "04E8");
67         do_write(PREFIX "/idProduct", "6860");
68         do_write(PREFIX "/f_acm/acm_transports", "tty");
69         do_write(PREFIX "/functions", "acm");
70         do_write(PREFIX "/enable", "1");
71         while (do_read("/sys/class/tty/ttyGS0/dev", dev, sizeof(dev)) < 0) {
72                 sleep(1);
73         }
74         sscanf(dev, "%d:%d", &maj, &min);
75         mknod("/dev/ttyGS0", 0666 | S_IFCHR, makedev(maj, min));
76         fd = open("/dev/ttyGS0", O_RDWR);
77         while (1) {
78                 write(fd, "Hello World!\n", 13);
79                 sleep(1);
80         }
81         return 0;
82 }