1 #define _DEFAULT_SOURCE
8 #include <HardwareSerial.h>
12 #include <netinet/in.h>
15 #include <sys/socket.h>
17 #include <sys/types.h>
21 #include "interpret.h"
22 #include "mTaskSymbols.h"
29 #define MSG_GET_TASK 't'
30 #define MSG_DEL_TASK 'd'
31 #define MSG_SDS_SPEC 's'
32 #define MSG_SDS_UPD 'u'
46 if (gettimeofday(&tv1
, NULL
) == -1)
48 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
52 bool input_available(){
54 return Serial
.available();
62 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
64 return FD_ISSET(fd
, &fds
);
78 void write_byte(uint8_t b
)
97 void killHandler(int i
)
99 printf("%i caught, Bye...\n", i
);
107 uint8_t c
= read_byte();
108 debug("Receiving input: %c\n", c
);
111 debug("Receiving an sds\n");
115 debug("Receiving an sds\n");
116 //TODO do something with the return value
120 debug("Receiving a delete task request\n");
124 debug("Receiving a task\n");
126 // write(fd_out, &c, 1);
132 // debug("iTasks server shut down\n");
133 // exit(EXIT_SUCCESS);
135 debug("Unknown message: %X\n", c
);
139 void open_filedescriptors()
143 void usage(FILE *o
, char *arg0
){
148 "-p PORT Custom port number, default: 8123\n"
157 int port
= 8123, opti
= 1;
158 //Register signal handler
159 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
160 die("Couldn't register signal handler...\n");
162 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
163 die("Couldn't register signal handler...\n");
165 //Command line arguments
167 if(strcmp((*argv
)+opti
, "-h") == 0){
168 usage(stdout
, argv
[0]);
170 } else if(strcmp(argv
[opti
], "-p") == 0 && opti
+1<*argc
){
171 port
= atoi(argv
[++opti
]);
173 die("Port numbers are > 1\n");
175 usage(stderr
, argv
[0]);
181 //Open file descriptors
182 struct sockaddr_in sa
;
184 memset(&sa
, 0, sizeof(sa
));
185 sa
.sin_family
= AF_INET
;
186 sa
.sin_addr
.s_addr
= INADDR_ANY
;
187 sa
.sin_port
= htons(port
);
189 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
191 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
193 if(listen(sock_fd
, 10) == -1)
196 printf("Listening on %d\n", port
);
198 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
211 struct task
*curtask
;
212 if(input_available())
215 cyclestart
= millis();
216 for(ct
= 0; ct
<MAXTASKS
; ct
++){
217 //See whether the task is even in use
218 if((curtask
= task_get(ct
)) == NULL
){
219 // debug("Task %d not implemented\n", ct);
222 //See whether the task interval has passed
223 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
224 // debug("Task %d not scheduled\n", ct);
228 printf("Current task to run: %d\n", ct
);
233 debug("Waiting for 500ms\n");
235 debug("done waiting\n");
239 int main(int ac
, char *av
[])
250 //Check for new tasks