a
[mTask.git] / int / interface_linux.c
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include <stdio.h>
7 #include <netdb.h>
8 #include <netinet/in.h>
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "interface.h"
16 struct timeval tv1;
17 int sock_fd = -1;
18 int fd = -1;
19 int gargc;
20 char **gargv;
21
22 uint8_t bt;
23
24 void killHandler(int i)
25 {
26 printf("%i caught, Bye...\n", i);
27 exit(1);
28 }
29
30 void usage(FILE *o, char *arg0){
31 fprintf(o, "Usage: %s [opts]\n\nOptions\n"
32 "-p PORT Custom port number, default: 8123\n" , arg0);
33 }
34
35 long millis(void) {
36 if (gettimeofday(&tv1, NULL) == -1)
37 pdie("gettimeofday");
38 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
39 }
40
41 bool input_available(void){
42 struct timeval tv;
43 fd_set fds;
44 tv.tv_sec = 0;
45 tv.tv_usec = 0;
46 FD_ZERO(&fds);
47 FD_SET(fd, &fds);
48 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
49 pdie("select");
50 if(!FD_ISSET(fd, &fds))
51 return false;
52 return true;
53 }
54
55 uint8_t read_byte(void)
56 {
57 read(fd, &bt, 1);
58 return bt;
59 }
60
61 void write_byte(uint8_t b)
62 {
63 write(fd, &b, 1);
64 }
65
66 void write_dpin(uint8_t i, bool b)
67 {
68 debug("dwrite %d: %d", i, b);
69 (void) i;
70 (void) b;
71 }
72
73 bool read_dpin(uint8_t i)
74 {
75 debug("dread %d", i);
76 return true;
77 (void) i;
78 }
79
80 void write_apin(uint8_t i, uint8_t a)
81 {
82 debug("awrite %d: %d", i, a);
83 (void) a;
84 (void) i;
85 }
86
87 uint8_t read_apin(uint8_t i)
88 {
89 debug("aread %d", i);
90 return 0;
91 (void) i;
92 }
93
94 void delay(long ms)
95 {
96 usleep(ms*1000);
97 }
98
99 void setup(void)
100 {
101 int port = 8123, opti = 1;
102 //Register signal handler
103 if(signal(SIGINT, killHandler) == SIG_ERR){
104 die("Couldn't register signal handler...");
105 }
106 if(signal(SIGTERM, killHandler) == SIG_ERR){
107 die("Couldn't register signal handler...");
108 }
109 //Command line arguments
110 while(opti < gargc){
111 if(strcmp((*gargv)+opti, "-h") == 0){
112 usage(stdout, gargv[0]);
113 exit(EXIT_SUCCESS);
114 } else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
115 port = atoi(gargv[++opti]);
116 if(port < 1)
117 die("Port numbers are > 1");
118 } else {
119 usage(stderr, gargv[0]);
120 exit(EXIT_FAILURE);
121 }
122 opti++;
123 }
124
125 //Open file descriptors
126 struct sockaddr_in sa;
127
128 memset(&sa, 0, sizeof(sa));
129 sa.sin_family = AF_INET;
130 sa.sin_addr.s_addr = INADDR_ANY;
131 sa.sin_port = htons(port);
132
133 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
134 pdie("socket");
135 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
136 pdie("bind");
137 if(listen(sock_fd, 10) == -1)
138 pdie("listen");
139
140 printf("Listening on %d\n", port);
141 fflush(stdout);
142 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
143 pdie("accept");
144 }
145
146 void debug(char *fmt, ...)
147 {
148 va_list ap;
149 va_start(ap, fmt);
150 vfprintf(stderr, fmt, ap);
151 fprintf(stderr, "\n");
152 va_end(ap);
153 }
154
155 void pdie(char *s)
156 {
157 perror(s);
158 die("");
159 }
160
161 void die(char *fmt, ...)
162 {
163 va_list ap;
164 va_start(ap, fmt);
165 vfprintf(stderr, fmt, ap);
166 va_end(ap);
167 exit(EXIT_FAILURE);
168 }