X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=int%2Fmain.c;h=bd0a42854324dc5e33ae0c38632a735cbb9b3d25;hb=2520cfc83a29d273b17441c5cdf411f1182bbb62;hp=02852efbce945274e620852137084c455acd44a2;hpb=1383ec4c1733cc6f07fe68e0619446d60fe5277e;p=mTask.git diff --git a/int/main.c b/int/main.c index 02852ef..bd0a428 100644 --- a/int/main.c +++ b/int/main.c @@ -1,194 +1,109 @@ -#define _BSD_SOURCE -#include -#include -#include #include #include -#include #include #include -#include -#include -#include -#include + +#ifndef STM +#include +#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("%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) +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); -// 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\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) fd_out; -} - -void open_filedescriptors() -{ - struct sockaddr_in sa; - - memset(&sa, 0, 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 1\n"); - } else { - usage(stderr, argv[0]); - exit(EXIT_FAILURE); + //Run tasks + cyclestart = millis(); + for(ct = 0; ctlastrun < 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'); } +} +#ifdef STM +int main(void){ +#else +int main(int argc, char *argv[]){ + gargc = argc; + gargv = argv; +#endif + + read_byte(); //Initialize systems + setup(); sds_init(); task_init(); - //Open communication - open_filedescriptors(); - write(fd, "\n", 1); + while(!input_available()){ + delay(100); + } + debug("booting up"); - 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; ctlastrun < 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(10); } return 0; }