/* time.c*/ //#include //#include //#include #include #include "global.h" #include "task.h" #include "time.h" #include "scan_inputs.h" #include "process.h" #include "outputs.h" #include "system.h" #include "sock.h" extern pid_t_ pid; static int sample = 0; int main(void) { int i,j; int last_time = 0; int quit; init_log(); h = init_time_base(); //print("base %f \n",h);//change to print to file init_sock();//FIXME rename data sock? //init server sock for incoming cmds? init_sock_cmd(); init_tasks(); init_global(); init_inputs(); init_pid(&pid); init_config(); for(;;) { mytime = GetCurrentTime(); printf("%u \n", mytime); /* if time base has elapsed, execute tasks */ if(last_time != mytime) { scheduler(); } last_time = mytime; if(quit <0) break; /* do nothing */ for(i=0;i < IDLE;i++){} } return 0; } void init_tasks (void) { schedule[0] = &scan_inputs; schedule[1] = &process; schedule[2] = &drive_outputs; schedule[3] = &systm; schedule[4] = &server; } void scheduler (void) { schedule[0](); schedule[1](); schedule[2](); schedule[3](); schedule[4](); } void init_global(void) { } float init_time_base(void) { DWORD prv,next,avg; float diff; float avgh=0; int i; avg =0; next = GetCurrentTime(); prv = next; for (i=0;i sample) { return; } printf("time = %u \n",mytime); // /* send milliseconds */ // itoa(mytime,dgram); // strcat(dgram,"\n"); // // printf("dgram %s",dgram); // z = sendto( s, // dgram, // strlen(dgram), // 0, // (struct sockaddr *)&host_adr, // len_inet); printf("data = %f \n",data[0]); ftoa(data[0],dgram); strcat(dgram,"\n"); /* send data */ z = sendto( s, dgram, strlen(dgram), 0, (struct sockaddr *)&host_adr, len_inet); //-o add wait for acknowledge or command here printf("file desc %d\n",z); printf("dgram %s",dgram); printf("downsample %i \n",downsample); sample=0; n = get_cmd(); // timeout if no activity in get_cmd strcpy(cmd,"");// at this point cmd strlen is 0 if(n>0) { z=recvfrom( s2, cmd, BUF_SIZE, 0, (struct sockaddr *)&client_adr, &len_inet); n = -1; if(strcmp(cmd,"")) { /* parse cmd */ //printf("cmd %s",cmd); strcpy(last_cmd,cmd); printf("cmd %s",cmd); (void)parse_cmd(cmd); } // return 0; } return 0; } /***********************************************/ /* itoasc() */ /* local conversion of integer to ascii string */ void itoa (DWORD dval, char*msg) { FILE *tfp; if((tfp =tmpfile())==NULL) { printf("tmpfile failed \n"); return; } fprintf(tfp,"%d",dval); rewind(tfp); fscanf(tfp,"%s",msg); fclose(tfp); return; } /***********************************************/ /* ftoa() */ /* local conversion of integer to ascii string */ void ftoa (float dval, char *msg) { FILE *tfp; if((tfp =tmpfile())==NULL) { printf("tmpfile failed \n"); return; } fprintf(tfp,"%f",dval); rewind(tfp); fscanf(tfp,"%s",msg); fclose(tfp); printf("msg %s \n",msg); return; } /***********************************************/ /* init_log() */ /* log errors & events to file */ /* */ void init_log(void) { //stub } /***********************************************/ /* init_config() */ /* read configuration from file */ /* */ void init_config(void) { //stub int i; nonv_t *n = &non_volatile; i = get_non_volatile (n); printf("nv %d \n",i); } int get_cmd(void) { struct timeval time_out; int n; int mx; mx = s2+1; //max sockets FD_ZERO(&rx_); FD_SET(s2,&rx_); time_out.tv_sec = 0; time_out.tv_usec =5000; n=select(mx,&rx_,NULL,NULL,&time_out); //check n for null if(n==-1) { printf("select error \n",n); return n; } else if(n==0) { printf("timeout \n"); return 0; } return n; } void send_response(char *resp,float val) { ftoa(val,dtfmt); strcat(resp,dtfmt); strcat(resp,"\n"); /* send data */ z = sendto( s2, resp, strlen(resp), 0, (struct sockaddr *)&client_adr, len_inet); }