// Poll network to see if we have anything new. If our network buffer is full, we simply have to block for new input data. static bool netplay_poll(netplay_t *handle) { if (!handle->has_connection) return false; handle->can_poll = false; if (!get_self_input_state(handle)) return false; // We skip reading the first frame so the host has a chance to grab our host info so we don't block forever :') if (handle->frame_count == 0) { handle->buffer[0].used_real = true; handle->buffer[0].is_simulated = false; handle->buffer[0].real_input_state = 0; handle->read_ptr = NEXT_PTR(handle->read_ptr); handle->read_frame_count++; return true; } // We might have reached the end of the buffer, where we simply have to block. int res = poll_input(handle, handle->other_ptr == handle->self_ptr); if (res == -1) { handle->has_connection = false; warn_hangup(); return false; } if (res == 1) { uint32_t first_read = handle->read_frame_count; do { uint32_t buffer[UDP_FRAME_PACKETS * 2]; if (!receive_data(handle, buffer, sizeof(buffer))) { warn_hangup(); handle->has_connection = false; return false; } parse_packet(handle, buffer, UDP_FRAME_PACKETS); } while ((handle->read_frame_count <= handle->frame_count) && poll_input(handle, (handle->other_ptr == handle->self_ptr) && (first_read == handle->read_frame_count)) == 1); } else { // Cannot allow this. Should not happen though. if (handle->self_ptr == handle->other_ptr) { warn_hangup(); return false; } } if (handle->read_ptr != handle->self_ptr) simulate_input(handle); else handle->buffer[PREV_PTR(handle->self_ptr)].used_real = true; return true; }
main() { const char *device = "/dev/spidev1.0"; fd = open(device, O_RDWR); int4 i; int4 status; float f; char *verb, *qual; int4 iverb; char host_name[80]; int4 port; char table_name[80]; char *theString; printf (" \n"); TERM_BLUE_COLOR; printf (" ***************************************************** \n"); printf (" ***************************************************** \n"); printf (" ** ** \n"); printf (" ** Welcome to CRISTAL: ** \n"); printf (" ** ContRol In Sum-Trigger ALgorithm ** \n"); printf (" ** ** \n"); printf (" ** by: Jezabel R. Garcia ** \n"); printf (" ** ** \n"); printf (" ***************************************************** \n"); printf (" ***************************************************** \n"); TERM_RESET_COLOR; printf (" \n"); // signal(SIGINT, myinterrupt); signal(SIGPIPE,brokenpipe); signal(SIGTERM,myabort); signal(SIGBUS,mybuserror); theString=(char *)malloc(sizeof(char)*256); STInfo.hostname[0]=(char*)malloc(sizeof(char)*80); STInfo.hostname[1]=(char*)malloc(sizeof(char)*80); /* * Set the initial values for status/times */ time(&STInfo.startime);// mirar los usos del tiempo time(&STInfo.writetime);// STInfo.istatus=ST_STATUS_BUSY; STInfo.threshold_control_flag=0; //by default off STInfo.Global_Target_rate = 500; //Hz STInfo.telescope=1; STInfo.temp_control=0; STInfo.total_time=3600; STInfo.time_interval=900; //initializes the BBmenu package menu_init(); //allow simulate_input() AllowInput=1; define_file("cristal_define.uic"); simulate_input("@cristal_init"); AllowInput=0; /* * Launch threads for service programs */ // usleep(100);//DBG STInfo.exit = False; printf("init threads \n"); STInfo.monexit=true; //monitoring thread not running //SERVER THREAD (para oir a cc) pthread_attr_init(&serv_attr); pthread_create(&serv_tid, &serv_attr, (void * (*)(void *)) server_thread, NULL); printf("server_thread initiated \n"); //CLIENT THREAD ( para hablar a cc) pthread_attr_init(&client_attr); pthread_create(&client_tid, &client_attr, (void * (*)(void *)) client_thread, NULL); printf("client_thread initiated \n"); //Add a Thread for L3 //Report thread pthread_attr_init(&Report_attr); pthread_create(&Report_tid, &Report_attr, (void * (*)(void *)) Report_thread, NULL); printf("Report_thread initiated \n"); while(True) { verb=action_menu( "ST_Main_Menu", &iverb); if(interrupted==True) { interrupted=False; flush_buffers(); } ////////////////////// if(strcmp(verb,"STOP!")==0) { transfer(0xA4, 0, 0, 0); printf("Stopping gloabl triggers."); } ////////////////////// if(strcmp(verb,"START")==0) { transfer(0xA3, 0, 0, 0); printf("Starting gloabl triggers."); } ////////////////////// if(strcmp(verb,"SETTARGETRATE")==0) { get_integer("Enter Global Target Trigger Rate", &STInfo.Global_Target_rate, 0, 0); } ////////////////////// if(strcmp(verb,"MONSTOP!")==0) //monitoring stop { STInfo.monexit=true; //stop monitpring thread } ////////////////////// if(strcmp(verb,"MONSTART")==0) //monitoring start { pthread_attr_init(&STmon_attr); pthread_create(&STmon_tid, &STmon_attr, (void * (*)(void *)) STmon_thread, NULL); //STInfo.monexit=0; // keep monitoring threat running } ////////////////////// if(strcmp(verb,"REPORT")==0) { error_logger(__FILE__,ERINFO,"Reports ON!"); if(!timer_on)start_timer(imon_sec); } ////////////////////// if(strcmp(verb,"REP!")==0) { stop_timer(); error_logger(__FILE__,ERINFO,"Reports OFF"); } ////////////////////// if(strcmp(verb,"LOCK!")==0) { //switch to remote control error_logger(__FILE__,ERINFO,"Readout ready to be engaged!"); set_async(True);//aqui es cuando cierra la conexion, usando getsring STInfo.lock_mode=True; } ////////////////////// if(strcmp(verb,"ULOCK")==0) { // if(client_conn) { // client_close(); client_conn=False; } error_logger(__FILE__,ERINFO,"Readout disengaged from CC"); set_async(False); STInfo.lock_mode=False; } ////////////////////// if(strcmp(verb,"HOSTNAME")==0) { int ihost; for(ihost=0;ihost<2;ihost++) { get_text("Enter Host Name",(char *)STInfo.hostname[ihost]); printf(" DBG HOSTNAME, %s", STInfo.hostname[ihost]); //if(STInfo.debug==True) // printf("Host Name defined: %s\n", STInfo.hostname[ihost]); } } ////////////////////// if(strcmp(verb,"HOSTNO")==0) { int ihost; for(ihost=0;ihost<2;ihost++) { get_integer("Enter Host Port Number",&STInfo.hostno[ihost],0,0); // if(STInfo.debug==True) printf("Host Port Number defined: %ld\n", STInfo.hostno[ihost]); } } ////////////////////// if(strcmp(verb,"CRISTAL")==0) { printf("\n"); TERM_PURPLE_COLOR; printf("CIRSTAL:Control in Sum-Trigger Algorithm\n"); printf("Author: Jezabel R. Garcia\n"); TERM_RESET_COLOR; printf("This progam was developed following the concept of MIR .\n"); printf("(by Riccardo Paoeletti and Daniel Mazin)\n"); printf("The Mir space station was built by connecting\n"); printf("several modules, each launched into orbit separately\n"); printf("One of the main module of the Mir was Cristal\n"); printf("that performed the astronomical observations and \n"); printf("extended the scientific possibilities of Mir.\n"); printf (" \n"); } ////////////////////// if(strcmp(verb,"TELESCOPE")==0) { get_integer("Who I am?",&STInfo.telescope,1,2); printf("This cristal version is running on M%i\n", STInfo.telescope); } ////////////////////// if(strcmp(verb,"PORTNO")==0) { get_integer("Enter Local Port Number",&STInfo.portno,0,0); // if(STInfo.debug==True) printf("Local Port Number defined: %ld\n", STInfo.portno); } ////////////////////// if(strcmp(verb,"TOOLS")==0) tools_menu(); ////////////////////// if(strcmp(verb,"DUMMY")==0) dummy_menu(); ////////////////////// if(strcmp(verb,"ASTRO")==0) astro_menu(); ////////////////////// if(strcmp(verb,"CLIP")==0) clip_menu(); ////////////////////// if(strcmp(verb,"SUM")==0) sum_menu(); ////////////////////// if(strcmp(verb,"POWER")==0) power_menu(); ////////////////////// if(strcmp(verb,"MESSAGE")==0) { get_text("Enter command", theString); puts(theString); error_logger(__FILE__,ERINFO, theString); } ////////////////////// if(strcmp(verb,"TIME_READ")==0) { int data4=0; get_hexadecimal("time? ",(long unsigned int*) &data4,0,0); int secs=15-data4&0xF; int sect=15-(data4>>4)&0x7; int mins=15-(data4>>7)&0xF; int mint=15-(data4>>11)&0x7; int hout=15-(data4>>14)&0x2; int hour=15-(data4>>16)&0xF; int tickWrong=(data4>>24)&0xFF; int tickRight = 0; int tickc = 0; for (tickc=0;tickc<8;tickc++) { tickRight += ((tickWrong>>(7-tickc))&0x1)*pow(2,tickc); // printf(" right %x wrong %x \n ",tickRight, (tickWrong>>(7-tickc))&0x1); } int tick = tickRight; //super second: // printf("R %X ",data4&0x0000FFFF); //super sub second: printf(" %X ",(data4&0xFFFF0000)>>16); //subsecond: get_hexadecimal("time? ",(long unsigned int*)&data4,0,0); printf("time: %d.%d h %d%d m %d%d %lf %d\n ",hout,hour,mint,mins,sect,secs,1.677721600-(data4&0x00007FFF)*51200e-9, tickRight); } ////////////////////// if(strcmp(verb,"SYSTEM")==0) { get_text("Enter command", theString); system(theString); } /////////////////////: if(strcmp(verb,"READY")==0) { STInfo.istatus=ST_STATUS_BUSY; TERM_BLUE_COLOR; printf (" ***************************************************** \n"); printf (" ***************************************************** \n"); printf (" ** ** \n"); printf (" ** CRISTAL IS NOW READY TO OPERATE ** \n"); printf (" ** ** \n"); printf (" ** ENJOY IT! ** \n"); printf (" ** ** \n"); printf (" ***************************************************** \n"); printf (" ***************************************************** \n"); TERM_RESET_COLOR; printf (" \n"); printf ("Press ? for more info. \n"); } if(strcmp(verb,"EXIT")==0) { stop_timer(); break; } usleep(250); }
/** * netplay_poll: * @netplay : pointer to netplay object * * Polls network to see if we have anything new. If our * network buffer is full, we simply have to block * for new input data. * * Returns: true (1) if successful, otherwise false (0). **/ static bool netplay_poll(netplay_t *netplay) { int res; if (!netplay->has_connection) return false; netplay->can_poll = false; if (!get_self_input_state(netplay)) return false; /* We skip reading the first frame so the host has a chance to grab * our host info so we don't block forever :') */ if (netplay->frame_count == 0) { netplay->buffer[0].used_real = true; netplay->buffer[0].is_simulated = false; memset(netplay->buffer[0].real_input_state, 0, sizeof(netplay->buffer[0].real_input_state)); netplay->read_ptr = NEXT_PTR(netplay->read_ptr); netplay->read_frame_count++; return true; } /* We might have reached the end of the buffer, where we * simply have to block. */ res = poll_input(netplay, netplay->other_ptr == netplay->self_ptr); if (res == -1) { netplay->has_connection = false; warn_hangup(); return false; } if (res == 1) { uint32_t first_read = netplay->read_frame_count; do { uint32_t buffer[UDP_FRAME_PACKETS * UDP_WORDS_PER_FRAME]; if (!receive_data(netplay, buffer, sizeof(buffer))) { warn_hangup(); netplay->has_connection = false; return false; } parse_packet(netplay, buffer, UDP_FRAME_PACKETS); } while ((netplay->read_frame_count <= netplay->frame_count) && poll_input(netplay, (netplay->other_ptr == netplay->self_ptr) && (first_read == netplay->read_frame_count)) == 1); } else { /* Cannot allow this. Should not happen though. */ if (netplay->self_ptr == netplay->other_ptr) { warn_hangup(); return false; } } if (netplay->read_ptr != netplay->self_ptr) simulate_input(netplay); else netplay->buffer[PREV_PTR(netplay->self_ptr)].used_real = true; return true; }