started with nucleo support
[mTask.git] / int / main.cpp
1 #define _DEFAULT_SOURCE
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #ifdef ARDUINO
7 #include <Arduino.h>
8 #include <HardwareSerial.h>
9 #else
10 #include <stdbool.h>
11 #include <netdb.h>
12 #include <netinet/in.h>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20
21 #include "interpret.h"
22 #include "mTaskSymbols.h"
23 #include "sds.h"
24 #include "task.h"
25 #include "misc.h"
26
27 #define MAXSDS 50
28
29 #define MSG_GET_TASK 't'
30 #define MSG_DEL_TASK 'd'
31 #define MSG_SDS_SPEC 's'
32 #define MSG_SDS_UPD 'u'
33
34 //Globals
35 #ifndef ARDUINO
36 struct timeval tv1;
37 int sock_fd = -1;
38 int fd = -1;
39 int *argc;
40 char **argv;
41 char bt;
42 #endif
43
44 #ifndef ARDUINO
45 long millis() {
46 if (gettimeofday(&tv1, NULL) == -1)
47 pdie("gettimeofday");
48 return tv1.tv_sec*1000 + tv1.tv_usec/1000;
49 }
50 #endif
51
52 bool input_available(){
53 #ifdef ARDUINO
54 return Serial.available();
55 #else
56 struct timeval tv;
57 fd_set fds;
58 tv.tv_sec = 0;
59 tv.tv_usec = 0;
60 FD_ZERO(&fds);
61 FD_SET(fd, &fds);
62 if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
63 pdie("select");
64 return FD_ISSET(fd, &fds);
65 #endif
66 }
67
68 uint8_t read_byte()
69 {
70 #ifdef ARDUINO
71 return Serial.read();
72 #else
73 read(fd, &bt, 1);
74 return bt;
75 #endif
76 }
77
78 void write_byte(uint8_t b)
79 {
80 #ifdef ARDUINO
81 Serial.write(b);
82 #else
83 write(fd, &b, 1);
84 #endif
85 }
86
87 void sleep(int ms)
88 {
89 #ifdef ARDUINO
90 delay(ms);
91 #else
92 usleep(ms*1000);
93 #endif
94 }
95
96 #ifndef ARDUINO
97 void killHandler(int i)
98 {
99 printf("%i caught, Bye...\n", i);
100 exit(1);
101 }
102 #endif
103
104 void read_message()
105 {
106 //Find next task
107 uint8_t c = read_byte();
108 debug("Receiving input: %c\n", c);
109 switch(c){
110 case MSG_SDS_SPEC:
111 debug("Receiving an sds\n");
112 sds_register();
113 break;
114 case MSG_SDS_UPD:
115 debug("Receiving an sds\n");
116 //TODO do something with the return value
117 sds_update();
118 break;
119 case MSG_DEL_TASK:
120 debug("Receiving a delete task request\n");
121 task_delete();
122 break;
123 case MSG_GET_TASK:
124 debug("Receiving a task\n");
125 c = task_register();
126 // write(fd_out, &c, 1);
127 // write(fd_out,
128 break;
129 case '\n':
130 break;
131 // case '\0':
132 // debug("iTasks server shut down\n");
133 // exit(EXIT_SUCCESS);
134 default:
135 debug("Unknown message: %X\n", c);
136 }
137 }
138
139 void open_filedescriptors()
140 {
141 }
142
143 void usage(FILE *o, char *arg0){
144 fprintf(o,
145 "Usage: %s [opts]\n"
146 "\n"
147 "Options\n"
148 "-p PORT Custom port number, default: 8123\n"
149 , arg0);
150 }
151
152 void setup()
153 {
154 #ifdef ARDUINO
155 Serial.begin(9600);
156 #else
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");
161 }
162 if(signal(SIGTERM, killHandler) == SIG_ERR){
163 die("Couldn't register signal handler...\n");
164 }
165 //Command line arguments
166 while(opti < *argc){
167 if(strcmp((*argv)+opti, "-h") == 0){
168 usage(stdout, argv[0]);
169 exit(EXIT_SUCCESS);
170 } else if(strcmp(argv[opti], "-p") == 0 && opti+1<*argc){
171 port = atoi(argv[++opti]);
172 if(port < 1)
173 die("Port numbers are > 1\n");
174 } else {
175 usage(stderr, argv[0]);
176 exit(EXIT_FAILURE);
177 }
178 opti++;
179 }
180
181 //Open file descriptors
182 struct sockaddr_in sa;
183
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);
188
189 if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
190 pdie("socket");
191 if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
192 pdie("bind");
193 if(listen(sock_fd, 10) == -1)
194 pdie("listen");
195
196 printf("Listening on %d\n", port);
197 fflush(stdout);
198 if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
199 pdie("accept");
200 #endif
201
202 //Initialize systems
203 sds_init();
204 task_init();
205 }
206
207 void loop()
208 {
209 int ct;
210 long cyclestart;
211 struct task *curtask;
212 if(input_available())
213 read_message();
214 //Run tasks
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);
220 continue;
221 }
222 //See whether the task interval has passed
223 if(cyclestart-curtask->lastrun < curtask->interval){
224 // debug("Task %d not scheduled\n", ct);
225 continue;
226 }
227 #ifdef DEBUG
228 printf("Current task to run: %d\n", ct);
229 getchar();
230 #endif
231 run_task(curtask);
232 }
233 debug("Waiting for 500ms\n");
234 sleep(500);
235 debug("done waiting\n");
236 write_byte('\n');
237 }
238
239 int main(int ac, char *av[])
240 {
241 #ifndef ARDUINO
242 argc = &ac;
243 argv = av;
244 #endif
245 setup();
246
247 write_byte('\n');
248
249 while(true){
250 //Check for new tasks
251 }
252 return 0;
253 }