X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=int%2Fint.c;h=c89a6a89561498dca52ea887b243003a0c7b4c01;hb=73d6866bdbab8f393b53c9951b05b62005a5423a;hp=2bf2cef134c8faf1eeb4dbd83add2c2e7b83448b;hpb=ce48250dbf5d93a0617c7683db9959f2fcacacc6;p=mTask.git diff --git a/int/int.c b/int/int.c index 2bf2cef..c89a6a8 100644 --- a/int/int.c +++ b/int/int.c @@ -1,10 +1,15 @@ -#include +#include +#include +#include #include +#include +#include #include -#include #include +#include +#include +#include #include -#include #include "mTaskSymbols.h" @@ -13,18 +18,36 @@ #define MAXSDS 50 #define STACKSIZE 1024 +#define MSG_TASK 't' +#define MSG_DELTASK 'd' +#define MSG_SDS 's' + #define DEBUG + #ifdef DEBUG #define debug(s, ...) printf(s, ##__VA_ARGS__); +#define trace(op, ...) printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__); #else #define debug(s, ...) ; +#define trace(pc, sp, op) ; #endif #define pdie(s) {perror(s); exit(1);} #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);} -char tasks[MAXTASKS][MAXTASKSIZE] = {0}; -bool tasks_used[MAXTASKS] = {0}; +struct task { + uint8_t bc[MAXTASKSIZE]; + uint16_t tlen; + uint16_t interval; + long lastrun; + bool used; +}; + +struct timeval tv1; +struct task tasks[MAXTASKS] = {0}; +int sock_fd = -1; +int fd = -1; +int port = 8123; void killHandler(int i) { @@ -39,197 +62,284 @@ bool input_available(int fd){ tv.tv_usec = 0; FD_ZERO(&fds); FD_SET(fd, &fds); - if(select(fd+1, &fds, NULL, NULL, &tv) == -1){ + 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) { //Find next task - uint8_t c; - uint16_t tasklen; - uint8_t ct; + uint8_t c, ct; for(ct = 0; ct MAXTASKSIZE) - die("Task is too long: %d\n", tasklen); - for(int i = 0; i MAXTASKSIZE) + die("Task is too long: %d\n", tasks[ct].tlen); + //Read task bytecode + for(int i = 0; i= PROGRAMSIZE) - die("Max program size: %d\n", PROGRAMSIZE); - pl = pc; - debug("Done reading, program length: %d\n", pl); - - //Evaluate program - //Reset program counter and stack counter - pc = 0; - sp = 0; - while(pc != pl){ + uint8_t *program = tasks[tasknum].bc; + int plen = tasks[tasknum].tlen; + int pc = 0; + int sp = 0; + char stack[STACKSIZE] = {0}; + printf("Running task: %d\nWith length: %d\n", tasknum, plen); + while(pc != plen){ + printf("program: %x\n", program[pc]); switch(program[pc++]){ - case BCNOP:; + case BCNOP: trace("nop\n"); break; - case BCPUSH: + case BCPUSH: trace("push %d\n", program[pc]); stack[sp++] = program[pc++]; break; - case BCPOP: + case BCPOP: trace("pop\n"); sp--; break; - case BCNOT: + case BCSDSSTORE: trace("sds store\n"); + break; + case BCSDSFETCH: trace("sds fetch\n"); + break; + case BCSDSPUBLISH: trace("sds publish\n"); + break; + case BCNOT: trace("not\n"); stack[sp] = stack[sp] > 0 ? 0 : 1; break; - case BCADD: + case BCADD: trace("add\n"); stack[sp-1] = stack[sp] + stack[sp-1]; sp -= 1; break; - case BCSUB: + case BCSUB: trace("sub\n"); stack[sp-1] = stack[sp] - stack[sp-1]; sp -= 1; break; - case BCMUL: + case BCMUL: trace("mul\n"); stack[sp-1] = stack[sp] * stack[sp-1]; sp -= 1; break; - case BCDIV: + case BCDIV: trace("div\n"); stack[sp-1] = stack[sp] / stack[sp-1]; sp -= 1; break; - case BCAND: + case BCAND: trace("and\n"); stack[sp-1] = stack[sp] && stack[sp-1]; sp -= 1; break; - case BCOR: + case BCOR: trace("or\n"); stack[sp-1] = stack[sp] || stack[sp-1]; sp -= 1; break; - case BCEQ: + case BCEQ: trace("eq\n"); stack[sp-1] = stack[sp] == stack[sp-1]; sp -= 1; break; - case BCNEQ: + case BCNEQ: trace("neq\n"); stack[sp-1] = stack[sp] != stack[sp-1]; sp -= 1; break; - case BCLES: stack[sp-1] = stack[sp] < stack[sp-1]; + case BCLES: trace("les\n"); + stack[sp-1] = stack[sp] < stack[sp-1]; sp -= 1; break; - case BCGRE: + case BCGRE: trace("gre\n"); stack[sp-1] = stack[sp] > stack[sp-1]; sp -= 1; break; - case BCLEQ: + case BCLEQ: trace("leq\n"); stack[sp-1] = stack[sp] <= stack[sp-1]; sp -= 1; break; - case BCGEQ: + case BCGEQ: trace("geq\n"); stack[sp-1] = stack[sp] >= stack[sp-1]; sp -= 1; break; - case BCJMP: + case BCJMP: trace("jmp to %d\n", program[pc]); pc = pc + program[pc]; break; - case BCJMPT: + case BCJMPT: trace("jmpt to %d\n", program[pc]); pc += stack[sp] ? program[pc] : 1; break; - case BCJMPF: + case BCJMPF: trace("jmpf to %d\n", program[pc]); pc += stack[sp] ? 1 : program[pc]; break; - case BCSERIALAVAIL: - printf("SerialAvailable()\n"); + case BCSERIALAVAIL: trace("SerialAvailable()\n"); break; - case BCSERIALPRINT: - printf("SerialPrint()\n"); + case BCSERIALPRINT: trace("SerialPrint()\n"); break; - case BCSERIALPRINTLN: - printf("SerialPrintln()\n"); + case BCSERIALPRINTLN: trace("SerialPrintln()\n"); break; - case BCSERIALREAD: - printf("SerialRead()\n"); + case BCSERIALREAD: trace("SerialRead()\n"); break; - case BCSERIALPARSEINT: - printf("SerialParseInt()\n"); + case BCSERIALPARSEINT: trace("SerialParseInt()\n"); break; - case BCANALOGREAD: - printf("AnalogRead()\n"); + case BCANALOGREAD: trace("AnalogRead(%d)\n", program[pc]); + pc++; + break; + case BCANALOGWRITE: trace("AnalogWrite(%d)\n", program[pc]); + pc++; + break; + case BCDIGITALREAD: trace("DigitalRead(%d)\n", program[pc]); + pc++; + break; + case BCDIGITALWRITE: trace("DigitalWrite(%d)\n", program[pc]); + pc++; break; - case BCANALOGWRITE: - printf("AnalogWrite()\n"); default: - die("Unrecognized command: %X\n", program[--pc]); + die("Unrecognized command: %d\n", program[--pc]); + } + } + debug("Task %d terminated\n", tasknum); +} + +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\n"); + if(listen(sock_fd, 10) == -1) + pdie("bind\n"); + + 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[]) +{ + 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"); + } + + //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); + } + + } + + open_filedescriptors(); + + long cyclestart; + while(true){ + //Check for new tasks + if(input_available(fd)) + read_message(fd, fd); + //Run tasks + cyclestart = millis(); + for(ct = 0; ct