fix
[mTask.git] / int / main.c
index e7a8f22..bd0a428 100644 (file)
-#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 <sys/socket.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <unistd.h>
+
+#ifndef STM
+#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)
-{
-       printf("%s caught, Bye...\n", strsignal(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)
+void read_message(void)
 {
-       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);
-               break;
-       case '\0':
-               debug("iTasks server shut down\n");
-               exit(EXIT_SUCCESS);
-       default:
-               debug("Unknown message: %X %X?\n", c, EOF);
+       if(input_available()){
+               uint8_t c = read_byte();
+               debug("Receiving input: %c\n", c);
+               switch(c){
+               case MSG_SDS_SPEC:
+                       debug("Receiving an sds");
+                       sds_register();
+                       break;
+               case MSG_SDS_UPD:
+                       debug("Receiving an sds");
+                       //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");
+                       task_register();
+                       break;
+               case '\0':
+                       break;
+               case '\n':
+                       break;
+               default:
+                       debug("Unknown message: %X", c);
+               }
        }
 }
 
-void open_filedescriptors()
+void loop(void)
 {
-       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\n");
-       if(listen(sock_fd, 10) == -1)
-               pdie("bind\n");
+       int ct;
+       long cyclestart;
+       struct task *curtask;
 
-       printf("Listening on %d\n", port);
-       fflush(stdout);
-       if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1)
-               pdie("accept");
-}
+       read_message();
 
-void usage(FILE *o, char *arg0){
-       fprintf(o,
-               "Usage: %s [opts]\n"
-               "\n"
-               "Options\n"
-               "-p PORT  Custom port number, default: 8123\n"
-               , arg0);
+       //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;
+               }
+               debug("Current task to run: %d", ct);
+               run_task(curtask);
+               curtask->lastrun = cyclestart;
+//             write_byte('\n');
+       }
 }
 
-int main(int argc, char *argv[])
-{
-       int ct;
-
-       //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");
-       }
+#ifdef STM
+int main(void){
+#else
+int main(int argc, char *argv[]){
+       gargc = argc;
+       gargv = argv;
+#endif
 
-       //Command line arguments
-       int opt;
-       while((opt = getopt(argc, argv, "hp:")) != -1){
-               switch(opt){
-                       case 'p':
-                               port = atoi(optarg);
-                               if(port < 1)
-                                       die("Port numbers are > 1\n");
-                               break;
-                       case 'h':
-                               usage(stdout, argv[0]);
-                               exit(EXIT_SUCCESS);
-                       default:
-                               usage(stderr, argv[0]);
-                               exit(EXIT_FAILURE);
-               }
+       read_byte();
+       //Initialize systems
+       setup();
+       sds_init();
+       task_init();
 
+       while(!input_available()){
+               delay(100);
        }
+       debug("booting up");
 
-       open_filedescriptors();
-
-       long cyclestart;
-       struct task *curtask;
        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");
+               //Check for newetasks
+               debug("loop");
+               loop();
+               delay(10);
        }
        return 0;
 }