int main() { int i; int id1, id2; struct _pulse pulse; // Request I/O privileges ThreadCtl( _NTO_TCTL_IO, 0 ); chid = ChannelCreate( 0 ); coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 ); SIGEV_PULSE_INIT( &event1, coid, getprio(0), MY_PULSE_CODE1, 0 ); id1=InterruptAttach( SYSPAGE_ENTRY(qtime)->intr, &handler1, NULL, 0, 0 ); SIGEV_PULSE_INIT( &event2, coid, getprio(0), MY_PULSE_CODE2, 0 ); id2=InterruptAttach( SYSPAGE_ENTRY(qtime)->intr, &handler2, NULL, 0, 0 ); for( i = 0; i < 10; ++i ) { // Wait for ISR to wake us up MsgReceivePulse( chid, &pulse, sizeof( pulse ), NULL ); if(pulse.code == MY_PULSE_CODE1) { printf( "1000 events\n" ); } else if(pulse.code == MY_PULSE_CODE2) { printf( "2500 events\n" ); } } // Disconnect the ISR handler InterruptDetach(id1); InterruptDetach(id2); return 0; }
static void* server(void* arg) { int number = 0; struct _pulse pulse; int res = 0; channelId = ChannelCreate(0); if(channelId == -1) { printf("msgSendReceivePulse: server: error creating channel!\n");fflush(stdout); exit(EXIT_FAILURE); } sleep(5); while(run) { res = MsgReceivePulse(channelId, &pulse, sizeof(pulse), NULL); if(res == -1) { printf("msgSendReceivePulse: server: error receiving pulse!\n");fflush(stdout); exit(EXIT_FAILURE); } number++; printf("Server: Pulse message #%d received\n", number);fflush(stdout); } res = ChannelDestroy(channelId); if(res == -1) { printf("msgSendReceivePulse: server: error destroying channel!\n");fflush(stdout); exit(EXIT_FAILURE); } pthread_exit(NULL); }
void Petri_Conveyor::execute(void* arg) { struct _pulse pulse; init_places(); while (!isStopped()) { if (-1 == MsgReceivePulse(conveyor_dispatcher_Chid, &pulse, sizeof(pulse), NULL)) { if (isStopped()) { break; // channel destroyed, Thread ending } perror("Petri_Motor: MsgReceivePulse"); exit(EXIT_FAILURE); } conveyor_tmpArr = conveyor_dispatcher->get_conveyor_inputs(); set_conveyor_inputs(); process_transitions(); calculate_outputs(); NotifyReactor(); //conveyor_dispatcher->set_disp_Outputs(conveyor_lokal_outputs); } }
int main3(int argc, char **argv) { struct sigevent event_frame; struct itimerspec itime_frame; timer_t frame_timer_id; int chid; int rcvid; int srv_coid; my_message_t msg; if(argc != 3) { printf("proper usage: ./lab5 hyperperiod_duration_in_sec frames_count\n"); return 0; } int duration = atoi(argv[1]); int frames_count = atoi(argv[2]); float frame_duration = (float)duration / (float)frames_count; int frame_duration_seconds = (int) frame_duration; int frame_duration_ns = (int) ((frame_duration - frame_duration_seconds) * 1000000000); chid = ChannelCreate(0); srv_coid = ConnectAttach(ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0); event_frame.sigev_notify = SIGEV_PULSE; event_frame.sigev_coid = srv_coid; event_frame.sigev_priority = getprio(0); event_frame.sigev_code = FRAME_PULSE_CODE; timer_create(CLOCK_REALTIME, &event_frame, &frame_timer_id); itime_frame.it_value.tv_sec = frame_duration_seconds; itime_frame.it_value.tv_nsec = frame_duration_ns; itime_frame.it_interval.tv_sec = frame_duration_seconds; itime_frame.it_interval.tv_nsec = frame_duration_ns; timer_settime(frame_timer_id, 0, &itime_frame, NULL); int tid; int i = 0; pthread_create(&tid, NULL, print_functions, (void *)i); for (;;) { rcvid = MsgReceivePulse(chid, &msg, sizeof(msg), NULL); if (rcvid == 0) { if (msg.pulse.code == FRAME_PULSE_CODE) { i++; pthread_cancel(tid); pthread_create(&tid, NULL, print_functions, (void *)i); printf("\n\n\tKoniec ramki %d\n\n", i); if(i == frames_count) { printf("\n\n\t\tKoniec hiper-okresu\n\n"); i = -1; } } } } }
void PeriodicThreadImplQNX::waitPeriod() { int rcvid; struct _pulse msg; // TODO: Catch missed periods! rcvid = MsgReceivePulse(m_chid, &msg, sizeof(msg), NULL); return; }
static void * _async_event_thread(void *arg) { int chid = (int)arg, coid; struct _pulse pulse; struct _asyncmsg_connect_context *acc; struct _asyncmsg_connection_descriptor *acd; for (;;) { if (MsgReceivePulse(chid, &pulse, sizeof(pulse), NULL) == -1) return NULL; coid = pulse.value.sival_int; if ((acc = _asyncmsg_handle(coid, _ASYNCMSG_HANDLE_LOOKUP, 0)) == NULL) continue; acd = &acc->acd; _mutex_lock(&acd->mu); if (pulse.code == 'T') { /* triger timer expired */ if (acc->acd.num_curmsg) { MsgSendAsync(coid); } acc->flags |= _ASYNCMSG_CONNECT_TIMEROFF; _mutex_unlock(&acd->mu); continue; } if (pulse.code == 'P') { /* collect some done message from "free" ptr */ struct _asyncmsg_put_header *aph; while (acd->sendq_free != acd->sendq_head) { aph = &acd->sendq[acd->sendq_free]; if (aph->cb) { aph->cb(aph->err, aph->iov, aph->handle); } else if (acd->attr.call_back) { acd->attr.call_back(aph->err, aph->iov, aph->handle); } if (++acd->sendq_free >= acd->sendq_size) acd->sendq_free = 0; } /* Sigh! have to use broadcast cause the condvar both * use for queue full, and asyncmsg_flush() */ pthread_cond_broadcast(&acd->block_con); _mutex_unlock(&acd->mu); } } }
// ------------------------------Simulator Monitor Thread------------------------ void* SIM_thread(void*notused) { struct _pulse intrPulse; while(killFlg == 0) { // Receive message from SIM MsgReceivePulse (1,&intrPulse,sizeof(intrPulse),NULL); temp_last_input = last_input; simPulse = intrPulse; // Extract simulator pulse value simPulseVal = simPulse.value.sival_int; delay(100); } }
// ------------------------------Timer2 Thread-------------------------------------- void* timer2_thread(void*notused) { int rcvid_timer; while(1) { rcvid_timer = MsgReceivePulse(timerChid2, &timerPulse, sizeof(timerPulse), NULL); if (rcvid_timer==-1) { printf("Error in timer2 pulse!\n"); flushall(); } else pulse_count++; } }
void B2S02_Seg1::timerSeg1Max() { puckHandler->removePuckFromBand(controller); actorHAL->engineStop(); printf("Debug State <B2S02_Seg1>: ERROR<ERR_STATE_ERROR_MAX> called by puck%d\n", controller->getID()); int replyChid = errfsm->getReplyChid(); int errorfsmChid = errfsm->getErrorFSMChid(); int errorfsmCoid; int rc; struct _pulse pulse; if ((errorfsmCoid = ConnectAttach(0, 0, errorfsmChid, _NTO_SIDE_CHANNEL, 0)) == -1) { printf("B2S02_Seg1: Error in ConnectAttach\n"); } //rc = MsgSendPulse(errorfsmCoid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_PUCK, ERR_STATE_CRITICAL_ERROR); rc = MsgSendPulse(errorfsmCoid, SIGEV_PULSE_PRIO_INHERIT, PULSE_FROM_PUCK, ERR_STATE_ERROR_MAX); if (rc < 0) { printf("B2S02_Seg1: Error in MsgSendPulse"); } rc = MsgReceivePulse(replyChid, &pulse, sizeof(pulse), NULL); if (rc < 0) { printf("B2S02_Seg1: Error in recv pulse\n"); } if (ConnectDetach(errorfsmCoid) == -1) { printf("B2S02_Seg1: Error in ConnectDetach\n"); } bool tmpBand1Waiting = controller->isBand1Waiting(); controller->resetController(); if(tmpBand1Waiting){ rs232_1->sendMsg(RS232_BAND2_READY); ActorHAL::getInstance()->engineRight(false); ActorHAL::getInstance()->engineUnstop(); controller->handOverTimer = timerHandler->createTimer(puckHandler->getDispChid(), TIME_VALUE_HAND_OVER_SEC, TIME_VALUE_HAND_OVER_MSEC, TIMER_HAND_OVER); timerHandler->startTimer(controller->handOverTimer); } }
void Sensorik::execute(void *arg) { struct _pulse pulse; while (!isStopped()) { if (-1 == MsgReceivePulse(isr_Chid, &pulse, sizeof(pulse), NULL)) { if (isStopped()) { break; // channel destroyed, Thread ending } perror("SensorCtrl: MsgReceivePulse"); exit(EXIT_FAILURE); } // printf("Sensorik:: BLAU: Coid %d\n", sensorik_Coid); // fflush(stdout); MsgSendPulse(sensorik_Coid, SIGEV_PULSE_PRIO_INHERIT, pulse.code, pulse.value.sival_int); } }
//------------------------------Timer Thread-------------------------------------- void* timer_thread(void*notused) { int time_count; int rcvid_timer; while(1) { rcvid_timer = MsgReceivePulse(timerChid, &timerPulse, sizeof(timerPulse), NULL); if (rcvid_timer==-1) { printf("Error in timer pulse!\n"); flushall(); } else {writeMsg(COMMEND_RESET);} delay(50); } }
// ------------------------------GUI Monitor Thread------------------------------- void* GUI_thread(void*notused) { int guiPulseVal; highOutput.hiOutputSetByte = 0; lowOutput.lowOutputSetByte = 0; while(1) { // Receive message from GUI MsgReceivePulse (2,&guiPulse,sizeof(guiPulse),NULL); // initialize guiPulseVal // extract the pulse value if (guiPulse.value.sival_int != COMMEND_RESET) { guiPulseVal = 0; guiPulseVal = guiPulse.value.sival_int; last_input = guiPulseVal; } // print GUI pulse // printf("GUI: Pulse: %i \n", guiPulseVal); // printf("Temp: Temp = %i \n", temp_guiPulse); // Quit loop if guiPulseVal == GUIQUIT if (guiPulseVal == GUIQUIT) {killFlg = 1; break;} // Command simulator writeMsg(guiPulseVal); if ((guiPulseVal == BASECW) || (guiPulseVal == BASECCW) || (guiPulseVal == SHOULDERUP) || (guiPulseVal == SHOULDERDW) || (guiPulseVal == ELBOWUP) || (guiPulseVal == ELBOWDW) || (guiPulseVal == PITCHUP) || (guiPulseVal == PITCHDW) || (guiPulseVal == UNLOADLEFT) || (guiPulseVal == UNLOADRIGHT) || (guiPulseVal == GRIPPEROP) || (guiPulseVal == GRIPPERCLOSE)) { timer_settime(timerid, 0, &timer,NULL); } } }
int omap3530_wait(omap3530_spi_t *dev, int len) { struct _pulse pulse; int rx_idx = dev->sdma_rx_chid; while (1) { if (len) { uint64_t to = dev->dtime * 1000 * 50; /* 50 times for time out */ to *= len ; TimerTimeout(CLOCK_REALTIME, _NTO_TIMEOUT_RECEIVE, NULL, &to, NULL); } if (MsgReceivePulse(dev->chid, &pulse, sizeof(pulse), NULL) == -1){ fprintf(stderr, "omap3530_wait: errono %s(%d), status %x\n", strerror(errno), errno, in32(dev->vbase+ OMAP3530_MCSPI_IRQ_STATUS_OFFSET)); return -1; } switch (pulse.code) { case OMAP3530_SPI_EVENT: return 0; case OMAP3530_SDMA_EVENT: { /* Unmask the Interrupt */ InterruptUnmask(dev->irq_sdma+rx_idx, dev->iid_sdma); if ((dev->dma4->channel[rx_idx].csr & DMA4_CSR_FRAME)){ /* clear interrupt status line 0 for transmit channel */ dev->dma4->channel[rx_idx].csr |= DMA4_CSR_FRAME; return 0; }else { continue; } } } } return 0; }
void Dispatcher::execute(void*) { int rc; running = false; #ifdef BAND_2 Timer* handOverTimer = NULL; #endif struct _pulse pulse; while(!isStopped()){ rc = MsgReceivePulse(chid, &pulse, sizeof(pulse), NULL); if (rc < 0) { printf("Dispatcher: Error in recv pulse\n"); if (isStopped()) { break; } } int funcIdx = pulse.value.sival_int; if (pulse.code == PULSE_FROM_ISRHANDLER) { if(funcIdx == BTN_START_PRESSED && !running && !eStop && !error){ running = true; lc->operatingNormal(); } else if(funcIdx == BTN_STOP_PRESSED && running && !eStop && !error){ running = false; } if(running && !eStop && !error){ #ifdef DEBUG_DISPATCHER printf("--------------------------------------------\n"); printf("Dispatcher received ISR pulse: %d\n", pulse.value); #endif if (funcIdx == SB_START_OPEN && !error) { PuckHandler::getInstance()->activatePuck(); #ifdef BAND_2 if(handOverTimer != NULL){ TimerHandler::getInstance()->deleteTimer(handOverTimer); handOverTimer = NULL; } #endif #ifdef DEBUG_DISPATCHER printf("Dispatcher called activatePuck \n"); #endif } for (uint32_t i = 0; i < controllersForSensorFunc[funcIdx].size(); i++) { (controllersForSensorFunc[funcIdx].at(i)->*sensorFuncArr[funcIdx])(); } #ifdef DEBUG_DISPATCHER printf("Dispatcher called func%d \n", funcIdx); #endif #ifdef BAND_1 PuckHandler::getInstance()->reInitFirstElemInSegBools(); #endif } } else if(pulse.code == PULSE_FROM_RS232){ if(running && !eStop){ #ifdef DEBUG_DISPATCHER printf("--------------------------------------------\n"); printf("Dispatcher received RS232 pulse: %d\n", pulse.value); #endif #ifdef BAND_2 if(funcIdx == RS232_BAND1_WAITING && PuckHandler::getInstance()->isBandEmpty()){ RS232_1::getInstance()->sendMsg(RS232_BAND2_READY); handOverTimer = TimerHandler::getInstance()->createTimer(chid, TIME_VALUE_HAND_OVER_SEC, TIME_VALUE_HAND_OVER_MSEC, TIMER_HAND_OVER); handOverTimer->start(); HAL_A::getInstance()->engine_right(false); HAL_A::getInstance()->engine_unstop(); } else if(funcIdx == RS232_BAND1_WAITING) { RS232_1::getInstance()->sendMsg(RS232_BAND2_ACK); #endif #ifdef BAND_1 if(!error) { #endif for (uint32_t i = 0; i < controllersForRS232Func[funcIdx].size(); i++) { (controllersForRS232Func[funcIdx].at(i)->*rs232FuncArr[funcIdx])(); } #ifdef BAND_1 } #endif #ifdef BAND_2 }else if((funcIdx == RS232_PUCK_ACC) || (funcIdx == RS232_PUCK_TURNOVER) || (funcIdx == RS232_PUCK_METALL_TURNOVER)){ PuckTyp = funcIdx; } #endif #ifdef DEBUG_DISPATCHER printf("Dispatcher called func%d \n", funcIdx); #endif } } else if(pulse.code == PULSE_FROM_TIMER) { #ifdef DEBUG_DISPATCHER printf("Dispatcher received TIMER pulse: %d\n", pulse.value); #endif #ifdef BAND_2 if(funcIdx == TIMER_HAND_OVER && handOverTimer != NULL){ TimerHandler::getInstance()->deleteTimer(handOverTimer); handOverTimer = NULL; } #endif for (uint32_t i = 0; i < controllersForTimerFunc[funcIdx].size(); i++) { (controllersForTimerFunc[funcIdx].at(i)->*timerFuncArr[funcIdx])(); } } } }
int _waitfor( __const char *path, int delay_ms, int poll_ms, int (*checkfunc)(__const char *, void *), void *handle ) { int notify_id = -1; struct sigevent event; int chid = -1, coid = -1; struct _pulse pulse; int ret_val = -1; uint64_t polltimeout = 100 * (uint64_t)1000000; uint64_t timeout_nsec; timeout_nsec = _syspage_time(CLOCK_MONOTONIC); timeout_nsec += delay_ms * (uint64_t)1000000; /* Check to make sure we don't wait for something already there... */ ret_val = checkfunc( path, handle ); if ( ret_val >= 0 || ret_val == WAITFOR_CHECK_ABORT ) { return ret_val; } if ( poll_ms > 0 ) { polltimeout = poll_ms * (uint64_t)1000000; } /* * We make the channel fixed since we want to remain at the current * thread's priority */ chid = ChannelCreate(_NTO_CHF_FIXED_PRIORITY); if ( chid == -1 ) { goto fail1; } coid = ConnectAttach( ND_LOCAL_NODE, 0, chid, _NTO_SIDE_CHANNEL, 0 ); if ( coid == -1 ) { goto fail2; } SIGEV_PULSE_INIT( &event, coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_CODE_PATHSPACE, 0 ); notify_id = procmgr_event_notify_add( PROCMGR_EVENT_PATHSPACE, &event ); if ( notify_id == -1 ) { goto fail3; } while(_syspage_time(CLOCK_MONOTONIC) < timeout_nsec) { /* we need to poll in case we are checking for a subdirectory/file, * since we won't get a pathmgr event for those. */ SIGEV_UNBLOCK_INIT(&event); if ( TimerTimeout( CLOCK_MONOTONIC, _NTO_TIMEOUT_RECEIVE, &event, &polltimeout, NULL) == -1 ) { ret_val = -1; break; } if ( MsgReceivePulse( chid, &pulse, sizeof(pulse), NULL ) != -1 || errno == ETIMEDOUT ) { ret_val = checkfunc( path, handle ); if ( ret_val >= 0 || ret_val == WAITFOR_CHECK_ABORT ) { break; } } else { break; } } (void)procmgr_event_notify_delete( notify_id ); fail3: (void)ConnectDetach(coid); fail2: (void)ChannelDestroy(chid); fail1: return ret_val; }
int clsThread::ReceivePulse(_pulse *pPulse) { return MsgReceivePulse(m_chThread, pPulse, sizeof(_pulse), NULL); }
/** * Here is where all the demultiplexer's work will be done * It gets pulses from Halsensoric, interprets them * and send the appropriate message to Dispatcher */ void Demultiplexer::execute(void* arg){ #ifdef DEBUG_ cout << "Demultiplexer thread started" << endl; #endif struct _pulse pulse; int new_value = -1; int old_port_value; // used to compare new value from pulse with the old value to know what has been changed int changed_bits = -1; // bits which had been changed by interrupt from old value int current_bit = -1; // needed in for-loop int message = -1; // message to be delivered to Dispatcher. See Messages.h // get the default value of port B and C. This will be called once old_port_value = (in8(DIGITAL_CARD_CROUP0_PORTB)<<8)|(in8(DIGITAL_CARD_CROUP0_PORTC)); while (!isStopped()) { #ifdef DEBUG_ cout << "Demultiplexer: waiting for MsgReceivePulse " << endl; #endif // Receive pulse messages from HAL Sensoric's (Interrupt handler) channel if (-1 == MsgReceivePulse(hal_sensoric_->getChannelId(), &pulse, sizeof(pulse), NULL)) { if (isStopped()) { break; // channel destroyed, Thread ending } perror("Demultiplexer: MsgReceivePulse"); exit(EXIT_FAILURE); } new_value = pulse.value.sival_int; changed_bits = new_value ^ old_port_value; // which bits have been changed old_port_value = pulse.value.sival_int; // update old value with the new one // shift 12 bits right. First 4 bits from port C are useless here //TODO is 4 here a magic number !! changed_bits >>= 4; new_value >>= 4; #ifdef DEBUG_ cout << "Demultplexer: port : " << hex << (int) pulse.code << " Value : "<< (int) new_value << endl; #endif // check all the 12 bits and assign the right message to be send for (int i=0;i<=12;i++){ current_bit = (1<<i); if (changed_bits & current_bit){ // if the bit has changed #ifdef DEBUG_ cout << " current_bit: "<< hex << (int)current_bit << endl; #endif switch (current_bit) { case START_BUTTON: // Start Button if (new_value & START_BUTTON) { message = START_PRESSED; }else { message = START_RELEASED; } sendMsg2Dispatcher(message); break; case STOP_BUTTON: //Stop Button if (!(new_value & STOP_BUTTON)) { message = STOP_PRESSED; } else { message = STOP_RELEASED; } sendMsg2Dispatcher(message); break; case RESET_BUTTON: //Reset Button if (new_value & RESET_BUTTON) { message = RESET_PRESSED; } else { message = RESET_RELEASED; } sendMsg2Dispatcher(message); break; case E_STOP_BUTTON: //E-Stop if (new_value & E_STOP_BUTTON) { message = E_STOP_RELEASED; } else { message = E_STOP_PRESSED; } sendMsg2Dispatcher(message); break; case ENGINE_START: //Light Barrier 1 if ( !(new_value & ENGINE_START)) { message = WP_IN_ENGINE_START; }else { message = WP_OUT_ENGINE_START; } sendMsg2Dispatcher(message); break; case HEIGHT_SENSOR: //WP in HeightSensor LB2 if (!(new_value & HEIGHT_SENSOR)) { message = WP_IN_HEIGHT_M; // cout << "Demultiplexer: WP_IN_HEIGHT_M" <<endl; }else { message = WP_OUT_HEIGHT_M; // cout << "Demultiplexer: WP_OUT_HEIGHT_M" <<endl; } sendMsg2Dispatcher(message); break; case HEIGHT_STATUS: //HeightSensor tolerance range if (new_value & HEIGHT_STATUS) { message = WP_IN_TOLERANCE_R; // cout << "Demultiplexer: WP_INTOL" <<endl; } else { message = WP_NOT_IN_TOLERANCE_R; // cout << "Demultiplexer: WP_NOT_INTOL" <<endl; } sendMsg2Dispatcher(message); break; case WP_IS_IN_SWITCH: // P in switch LB3 if (new_value & WP_IS_IN_SWITCH) { message = WP_OUT_SWITCH; }else { message = WP_IN_SWITCH; } sendMsg2Dispatcher(message); break; case METAL_STATUS: // Metal Sensor if (new_value & METAL_STATUS) { message = WP_IS_METAL; } else { message = WP_NOT_METAL; } sendMsg2Dispatcher(message); break; case SWITCH_STATUS: //Switch if (new_value & SWITCH_STATUS) { message = SWITCH_IS_OPEN; } else { message = SWITCH_IS_CLOSED; } sendMsg2Dispatcher(message); break; case SLIDE_STATUS: //Slide if (new_value & SLIDE_STATUS) { message = WP_OUT_SLIDE; } else { message = WP_IN_SLIDE; } sendMsg2Dispatcher(message); break; case ENGINE_END: // LB end if (!(new_value & ENGINE_END)) { message = WP_IN_ENGINE_END; }else { message = WP_OUT_ENGINE_END; } sendMsg2Dispatcher(message); break; case WP_IS_MISSING: message = WP_IS_MISSING; sendMsg2Dispatcher(message); break; default : break; } // switch (current_bit) } //if (changed_bits & current_bit) } // for } // while }