remove traces
[mTask.git] / int / main.c
index 886170e..c4417f8 100644 (file)
-#define _BSD_SOURCE
-#include <netdb.h>
-#include <netinet/in.h>
-#include <signal.h>
 #include <stdbool.h>
 #include <stdint.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <unistd.h>
+
+#ifdef STM
+#else
+#include <stdio.h>
+#endif
 
 #include "interpret.h"
 #include "mTaskSymbols.h"
 #include "sds.h"
 #include "task.h"
-#include "misc.h"
-
-#define MAXSDS 50
+#include "interface.h"
 
 #define MSG_GET_TASK 't'
 #define MSG_DEL_TASK 'd'
 #define MSG_SDS_SPEC 's'
 #define MSG_SDS_UPD 'u'
 
-struct timeval tv1;
-int sock_fd = -1;
-int fd = -1;
-int port = 8123;
-
-void killHandler(int i)
+void read_message(void)
 {
-       printf("%i caught, Bye...\n", i);
-       exit(1);
-}
-
-bool input_available(int fd){
-       struct timeval tv;
-       fd_set fds;
-       tv.tv_sec = 0;
-       tv.tv_usec = 0;
-       FD_ZERO(&fds);
-       FD_SET(fd, &fds);
-       if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
-               pdie("select");
-       return FD_ISSET(fd, &fds);
-}
-
-long millis() {
-       if (gettimeofday(&tv1, NULL) == -1)
-               pdie("gettimeofday");
-       return tv1.tv_sec*1000 + tv1.tv_usec/1000;
-}
-
-void read_message(int fd_in, int fd_out)
-{
-       uint8_t c;
        //Find next task
-
-       read(fd_in, &c, 1);
-       debug("Receiving input: %c\n", c);
-       switch(c){
-       case MSG_SDS_SPEC:
-               debug("Receiving an sds\n");
-               sds_register(fd_in);
-               break;
-       case MSG_SDS_UPD:
-               debug("Receiving an sds\n");
-               //TODO do something with the return value
-               sds_update(fd_in);
-               break;
-       case MSG_DEL_TASK:
-               debug("Receiving a delete task request\n");
-               task_delete(fd);
-               break;
-       case MSG_GET_TASK:
-               debug("Receiving a task\n");
-               c = task_register(fd_in);
-//             write(fd_out, &c, 1);
-//             write(fd_out, 
-               break;
-       case '\n':
-               break;
-//     case '\0':
-//             debug("iTasks server shut down\n");
-//             exit(EXIT_SUCCESS);
-       default:
-               debug("Unknown message: %X\n", c);
+       if(input_available()){
+               uint8_t c = read_byte();
+               debug("Receiving input: %c %02x\n", c, c);
+               switch(c){
+               case MSG_SDS_SPEC:
+                       debug("Receiving an sds");
+                       sds_register();
+                       break;
+               case MSG_SDS_UPD:
+                       debug("Receiving an sds update");
+                       //TODO do something with the return value
+                       sds_update();
+                       break;
+               case MSG_DEL_TASK:
+                       debug("Receiving a delete task request");
+                       task_delete();
+                       break;
+               case MSG_GET_TASK:
+                       debug("Receiving a task");
+       //              write_dpin(0, true);
+                       task_register();
+       //              write_dpin(0, false);
+                       break;
+               case '\0':
+                       break;
+               case '\n':
+                       break;
+               default:
+                       debug("Unknown message: %X", c);
+               }
        }
-       (void) fd_out;
-}
-
-void open_filedescriptors()
-{
-       struct sockaddr_in sa;
-
-       bzero((char *) &sa, sizeof(sa));
-       sa.sin_family = AF_INET;
-       sa.sin_addr.s_addr = INADDR_ANY;
-       sa.sin_port = htons(port);
-
-       if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
-               pdie("socket");
-       if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
-               pdie("bind");
-       if(listen(sock_fd, 10) == -1)
-               pdie("listen");
-
-       printf("Listening on %d\n", port);
-       fflush(stdout);
-       if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
-               pdie("accept");
 }
 
-void usage(FILE *o, char *arg0){
-       fprintf(o,
-               "Usage: %s [opts]\n"
-               "\n"
-               "Options\n"
-               "-p PORT  Custom port number, default: 8123\n"
-               , arg0);
-}
-
-int main(int argc, char *argv[])
+void loop(void)
 {
        int ct;
+       long cyclestart;
+       struct task *curtask;
 
-       //Register signal handler
-       if(signal(SIGINT, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
-       if(signal(SIGTERM, killHandler) == SIG_ERR){
-               die("Couldn't register signal handler...\n");
-       }
+       read_message();
 
-       //Command line arguments
-       int opti = 1;
-       while(opti < argc){
-               if(strcmp(argv[opti], "-h") == 0){
-                       usage(stdout, argv[0]);
-                       exit(EXIT_SUCCESS);
-               } else if(strcmp(argv[opti], "-p") == 0 && opti+1<argc){
-                       port = atoi(argv[++opti]);
-                       if(port < 1)
-                               die("Port numbers are > 1\n");
-               } else {
-                       usage(stderr, argv[0]);
-                       exit(EXIT_FAILURE);
+       //Run tasks
+       cyclestart = millis();
+       for(ct = 0; ct<MAXTASKS; ct++){
+               //See whether the task is even in use
+               if((curtask = task_get(ct)) == NULL){
+//                     debug("Task %d not implemented\n", ct);
+                       continue;
+               }
+               //See whether the task interval has passed
+               if(cyclestart-curtask->lastrun < curtask->interval){
+//                     debug("Task %d not scheduled\n", ct);
+                       continue;
                }
-               opti++;
+               debug("Current task to run: %d", ct);
+               run_task(curtask);
+               curtask->lastrun = cyclestart;
        }
+}
 
-       open_filedescriptors();
-       write(fd, "\n", 1);
+#ifdef STM
+int main(void){
+#else
+int main(int argc, char *argv[]){
+       gargc = argc;
+       gargv = argv;
+#endif
 
-       long cyclestart;
-       struct task *curtask;
+       //Initialize systems
+       setup();
+       sds_init();
+       task_init();
+       //debug("booting up");
        while(true){
-               //Check for new tasks
-               if(input_available(fd))
-                       read_message(fd, fd);
-               //Run tasks
-               cyclestart = millis();
-               for(ct = 0; ct<MAXTASKS; ct++){
-                       //See whether the task is even in use
-                       if((curtask = task_get(ct)) == NULL){
-//                             debug("Task %d not implemented\n", ct);
-                               continue;
-                       }
-                       //See whether the task interval has passed
-                       if(cyclestart-curtask->lastrun < curtask->interval){
-//                             debug("Task %d not scheduled\n", ct);
-                               continue;
-                       }
-#ifdef DEBUG
-                       printf("Current task to run: %d\n", ct);
-                       getchar();
-#endif
-                       run_task(curtask, fd);
-               }
-               debug("Waiting for 500ms\n");
-               usleep(500000);
-               debug("done waiting\n");
-               write(fd, "\n", 1);
+               //Check for newetasks
+//             debug("loop");
+               loop();
+               delay(50);
        }
        return 0;
 }