fix if statements and sds publishing
[mTask.git] / int / main.c
1 #define _BSD_SOURCE
2 #include <netdb.h>
3 #include <netinet/in.h>
4 #include <signal.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15
16 #include "interpret.h"
17 #include "mTaskSymbols.h"
18 #include "sds.h"
19 #include "task.h"
20 #include "misc.h"
21
22 #define MAXSDS 50
23
24 #define MSG_GET_TASK 't'
25 #define MSG_DEL_TASK 'd'
26 #define MSG_SDS_SPEC 's'
27 #define MSG_SDS_UPD 'u'
28
29 struct timeval tv1;
30 int sock_fd = -1;
31 int fd = -1;
32 int port = 8123;
33
34 void killHandler(int i)
35 {
36 printf("%i caught, Bye...\n", i);
37 exit(1);
38 }
39
40 bool input_available(int fd){
41 struct timeval tv;
42 fd_set fds;
43 tv.tv_sec = 0;
44 tv.tv_usec = 0;
45 FD_ZERO(&fds);
46 FD_SET(fd, &fds);
47 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
48 pdie("select");
49 return FD_ISSET(fd, &fds);
50 }
51
52 long millis() {
53 if (gettimeofday(&tv1, NULL) == -1)
54 pdie("gettimeofday");
55 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
56 }
57
58 void read_message(int fd_in, int fd_out)
59 {
60 uint8_t c;
61 //Find next task
62
63 read(fd_in, &c, 1);
64 debug("Receiving input: %c\n", c);
65 switch(c){
66 case MSG_SDS_SPEC:
67 debug("Receiving an sds\n");
68 sds_register(fd_in);
69 break;
70 case MSG_SDS_UPD:
71 debug("Receiving an sds\n");
72 //TODO do something with the return value
73 sds_update(fd_in);
74 break;
75 case MSG_DEL_TASK:
76 debug("Receiving a delete task request\n");
77 task_delete(fd);
78 break;
79 case MSG_GET_TASK:
80 debug("Receiving a task\n");
81 c = task_register(fd_in);
82 // write(fd_out, &c, 1);
83 // write(fd_out,
84 break;
85 case '\n':
86 break;
87 // case '\0':
88 // debug("iTasks server shut down\n");
89 // exit(EXIT_SUCCESS);
90 default:
91 debug("Unknown message: %X\n", c);
92 }
93 (void) fd_out;
94 }
95
96 void open_filedescriptors()
97 {
98 struct sockaddr_in sa;
99
100 bzero((char *) &sa, sizeof(sa));
101 sa.sin_family = AF_INET;
102 sa.sin_addr.s_addr = INADDR_ANY;
103 sa.sin_port = htons(port);
104
105 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
106 pdie("socket");
107 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
108 pdie("bind");
109 if(listen(sock_fd, 10) == -1)
110 pdie("listen");
111
112 printf("Listening on %d\n", port);
113 fflush(stdout);
114 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
115 pdie("accept");
116 }
117
118 void usage(FILE *o, char *arg0){
119 fprintf(o,
120 "Usage: %s [opts]\n"
121 "\n"
122 "Options\n"
123 "-p PORT Custom port number, default: 8123\n"
124 , arg0);
125 }
126
127 int main(int argc, char *argv[])
128 {
129 int ct;
130
131 //Register signal handler
132 if(signal(SIGINT, killHandler) == SIG_ERR){
133 die("Couldn't register signal handler...\n");
134 }
135 if(signal(SIGTERM, killHandler) == SIG_ERR){
136 die("Couldn't register signal handler...\n");
137 }
138
139 //Command line arguments
140 int opti = 1;
141 while(opti < argc){
142 if(strcmp(argv[opti], "-h") == 0){
143 usage(stdout, argv[0]);
144 exit(EXIT_SUCCESS);
145 } else if(strcmp(argv[opti], "-p") == 0 && opti+1<argc){
146 port = atoi(argv[++opti]);
147 if(port < 1)
148 die("Port numbers are > 1\n");
149 } else {
150 usage(stderr, argv[0]);
151 exit(EXIT_FAILURE);
152 }
153 opti++;
154 }
155
156 open_filedescriptors();
157 write(fd, "\n", 1);
158
159 long cyclestart;
160 struct task *curtask;
161 while(true){
162 //Check for new tasks
163 if(input_available(fd))
164 read_message(fd, fd);
165 //Run tasks
166 cyclestart = millis();
167 for(ct = 0; ct<MAXTASKS; ct++){
168 //See whether the task is even in use
169 if((curtask = task_get(ct)) == NULL){
170 // debug("Task %d not implemented\n", ct);
171 continue;
172 }
173 //See whether the task interval has passed
174 if(cyclestart-curtask->lastrun < curtask->interval){
175 // debug("Task %d not scheduled\n", ct);
176 continue;
177 }
178 #ifdef DEBUG
179 printf("Current task to run: %d\n", ct);
180 getchar();
181 #endif
182 run_task(curtask, fd);
183 }
184 debug("Waiting for 500ms\n");
185 usleep(500000);
186 debug("done waiting\n");
187 write(fd, "\n", 1);
188 }
189 return 0;
190 }