2 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
14 #include "interpret.h"
15 #include "mTaskSymbols.h"
22 #define MSG_GET_TASK 't'
23 #define MSG_DEL_TASK 'd'
24 #define MSG_SDS_SPEC 's'
25 #define MSG_SDS_UPD 'u'
32 void killHandler(int i
)
34 printf("%s caught, Bye...\n", strsignal(i
));
38 bool input_available(int fd
){
45 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
47 return FD_ISSET(fd
, &fds
);
51 if (gettimeofday(&tv1
, NULL
) == -1)
53 return tv1
.tv_sec
*1000 + tv1
.tv_usec
/1000;
56 void read_message(int fd_in
, int fd_out
)
62 debug("Receiving input: %c\n", c
);
65 debug("Receiving an sds\n");
69 debug("Receiving an sds\n");
70 //TODO do something with the return value
74 debug("Receiving a delete task request\n");
78 debug("Receiving a task\n");
79 c
= task_register(fd_in
);
83 debug("Unknown message: %X?\n", c
);
87 void open_filedescriptors()
89 struct sockaddr_in sa
;
91 bzero((char *) &sa
, sizeof(sa
));
92 sa
.sin_family
= AF_INET
;
93 sa
.sin_addr
.s_addr
= INADDR_ANY
;
94 sa
.sin_port
= htons(port
);
96 if((sock_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
98 if(bind(sock_fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == -1)
100 if(listen(sock_fd
, 10) == -1)
103 printf("Listening on %d\n", port
);
105 if((fd
= accept(sock_fd
, (struct sockaddr
*)NULL
, NULL
)) == -1)
109 void usage(FILE *o
, char *arg0
){
114 "-p PORT Custom port number, default: 8123\n"
118 int main(int argc
, char *argv
[])
122 //Register signal handler
123 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
124 die("Couldn't register signal handler...\n");
126 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
127 die("Couldn't register signal handler...\n");
130 //Command line arguments
132 while((opt
= getopt(argc
, argv
, "hp:")) != -1){
137 die("Port numbers are > 1\n");
140 usage(stdout
, argv
[0]);
143 usage(stderr
, argv
[0]);
149 open_filedescriptors();
152 struct task
*curtask
;
154 //Check for new tasks
155 if(input_available(fd
))
156 read_message(fd
, fd
);
158 cyclestart
= millis();
159 for(ct
= 0; ct
<MAXTASKS
; ct
++){
160 //See whether the task is even in use
161 if((curtask
= task_get(ct
)) == NULL
){
162 debug("Task %d not implemented\n", ct
);
165 //See whether the task interval has passed
166 if(cyclestart
-curtask
->lastrun
< curtask
->interval
){
167 debug("Task %d not scheduled\n", ct
);
171 printf("Current task to run: %d\n", ct
);