a2dd7458e62a0aec5da308abc8cb06e657283bc3
2 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
13 #include "mTaskSymbols.h"
16 #define MAXTASKSIZE 1024
18 #define STACKSIZE 1024
22 #define debug(s, ...) printf(s, ##__VA_ARGS__);
24 #define debug(s, ...) ;
27 #define pdie(s) {perror(s); exit(1);}
28 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
30 char tasks
[MAXTASKS
][MAXTASKSIZE
] = {0};
31 bool tasks_used
[MAXTASKS
] = {0};
35 void killHandler(int i
)
37 printf("%s caught, Bye...\n", strsignal(i
));
41 bool input_available(int fd
){
48 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
50 return FD_ISSET(fd
, &fds
);
53 void read_message(int fd_in
, int fd_out
)
59 for(ct
= 0; ct
<MAXTASKS
; ct
++)
63 die("Trying to add too much tasks...\n");
65 debug("Receiving input for task %d\n", ct
);
68 debug("Receiving an sds\n");
74 if(tasklen
> MAXTASKSIZE
)
75 die("Task is too long: %d\n", tasklen
);
76 for(int i
= 0; i
<tasklen
; i
++){
77 debug("Read %d\n", i
);
78 read(fd_in
, tasks
[ct
]+i
, 1);
79 debug("t[][%i]: %d\n", i
,
82 //Return the task number for later removal
83 write(fd_out
, (void *)&ct
, 1);
84 debug("Received a task of length %d\n", tasklen
);
85 tasks_used
[ct
] = true;
87 debug("Unknown message: %X?\n", c
);
91 void run_task(char *program
)
95 int plen
= strlen(program
);
96 char stack
[1024] = {0};
98 switch(program
[pc
++]){
102 stack
[sp
++] = program
[pc
++];
108 stack
[sp
] = stack
[sp
] > 0 ? 0 : 1;
111 stack
[sp
-1] = stack
[sp
] + stack
[sp
-1];
115 stack
[sp
-1] = stack
[sp
] - stack
[sp
-1];
119 stack
[sp
-1] = stack
[sp
] * stack
[sp
-1];
123 stack
[sp
-1] = stack
[sp
] / stack
[sp
-1];
127 stack
[sp
-1] = stack
[sp
] && stack
[sp
-1];
131 stack
[sp
-1] = stack
[sp
] || stack
[sp
-1];
135 stack
[sp
-1] = stack
[sp
] == stack
[sp
-1];
139 stack
[sp
-1] = stack
[sp
] != stack
[sp
-1];
142 case BCLES
: stack
[sp
-1] = stack
[sp
] < stack
[sp
-1];
146 stack
[sp
-1] = stack
[sp
] > stack
[sp
-1];
150 stack
[sp
-1] = stack
[sp
] <= stack
[sp
-1];
154 stack
[sp
-1] = stack
[sp
] >= stack
[sp
-1];
158 pc
= pc
+ program
[pc
];
161 pc
+= stack
[sp
] ? program
[pc
] : 1;
164 pc
+= stack
[sp
] ? 1 : program
[pc
];
167 printf("SerialAvailable()\n");
170 printf("SerialPrint()\n");
172 case BCSERIALPRINTLN
:
173 printf("SerialPrintln()\n");
176 printf("SerialRead()\n");
178 case BCSERIALPARSEINT
:
179 printf("SerialParseInt()\n");
182 printf("AnalogRead(%d)\n", program
[pc
++]);
185 printf("AnalogWrite(%d)\n", program
[pc
++]);
188 printf("DigitalRead(%d)\n", program
[pc
++]);
191 printf("DigitalWrite(%d)\n", program
[pc
++]);
194 die("Unrecognized command: %d\n", program
[--pc
]);
199 void open_filedescriptors()
201 struct sockaddr_in sa
;
203 bzero((char *) &sa
, sizeof(sa
));
204 sa
.sin_family
= AF_INET
;
205 sa
.sin_addr
.s_addr
= INADDR_ANY
;
206 sa
.sin_port
= htons(8124);
208 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
210 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
212 if(listen(sock_fd
, 10) == -1)
215 printf("Listinging on 8123\n");
217 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
225 //Register signal handler
226 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
227 die("Couldn't register signal handler...\n");
229 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
230 die("Couldn't register signal handler...\n");
233 open_filedescriptors();
236 //Check for new tasks
237 if(input_available(fd
)){
238 read_message(fd
, fd
);
241 for(ct
= 0; ct
<MAXTASKS
; ct
++){
243 debug("Task %d not implemented\n", ct
);
246 debug("Going to execute task %d\n", ct
);
256 while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
258 if (pc >= PROGRAMSIZE)
259 die("Max program size: %d\n", PROGRAMSIZE);
261 debug("Done reading, program length: %d\n", pl);
264 //Reset program counter and stack counter