コード例 #1
0
ファイル: main.c プロジェクト: ntujoseph/AODV
int main(void)
{
	uint8_t src_packet[128] = {0x05, 0x30, 0x00, 0x00, 0x0A};
	
	uint8_t rcvd_msg[128] = {0};
	uint8_t rcvd_payload[128] = {0};
	uint8_t rcvd_length;
	uint8_t rcvd_payloadLength;
	uint8_t rcvd_rssi;
	
	uint8_t Type;
	uint16_t Addr;
	uint8_t radio_channel;
	uint16_t radio_panID;
	
	Type = Type_Light;
	Addr = 0x0001;
	radio_channel = 18;
	radio_panID = 0x00AA;
	
	Initial(Addr, Type, radio_channel, radio_panID);
	setTimer(1,RETRANSMIT_PERIOD,UNIT_MS);
	
	while(1){
		
		// Periodically send the msg
		if(checkTimer(1)){
			RF_Tx(0xFFFF,src_packet,5);
		}
		
		// When received some packet
		if(RF_Rx(rcvd_msg, &rcvd_length, &rcvd_rssi)){
			getPayloadLength(&rcvd_payloadLength, rcvd_msg);
			getPayload(rcvd_payload, rcvd_msg, rcvd_payloadLength);
			
			// Check 1)header, 2)sequence number, 3)isACK field
			if(rcvd_payload[0]==0x05 && rcvd_payload[1]==0x30 && 
				 rcvd_payload[2]==src_packet[2] && rcvd_payload[3]==1){
				src_packet[2]++;
				// Change the payload here
			}
		}
		
		if(src_packet[2]==0x14)
			break;
	}
	
	while(1){
		if(checkTimer(1)){
			setGPIO(1,1);
		}	
	}
}
コード例 #2
0
ファイル: control-widget.cpp プロジェクト: altagir/x10-widget
void ControlWidget::setUnitTo(int value)
{
    if (value == m_unit.value)
        return;

    m_timerSuspendUpdate.start(6000);

    m_unit.value = value;

    m_serviceUnit = m_parent->getDataEngine()->serviceForSource(m_unit.address());
    if (!m_serviceUnit || !m_serviceUnit->isOperationEnabled("Set"))
    {
        Logger::Log(ERROR, "No %s available in %s",
                    m_serviceUnit ? "SET Operation" : "Service",
                    m_unit.address());
        return;
    }

    KConfigGroup op = m_serviceUnit->operationDescription("Set");
    op.writeEntry("Value", value);

    KJob* job = m_serviceUnit->startOperationCall(op);
    connect(job, SIGNAL(finished(KJob*)), m_serviceUnit, SLOT(deleteLater()));

    checkTimer(value);

    refreshSlider();
    refreshOnOff();
}
コード例 #3
0
ファイル: control-widget.cpp プロジェクト: altagir/x10-widget
void ControlWidget::send_command(COMMAND_TYPE command)
{
    char fullCommand[512];
    sprintf(fullCommand, "%c%s", command, m_unit.address());

    // not updating this crash after some reuse???
// TODO investigate.
    m_serviceUnit = m_parent->getDataEngine()->serviceForSource(m_unit.address());
    if (!m_serviceUnit->isOperationEnabled("Send"))
    {
        Logger::Log(INFO, "No Send Operation available");
        return;
    }

    KConfigGroup op = m_serviceUnit->operationDescription("Send");
    op.writeEntry("Command", QString(command));

    KJob* job = m_serviceUnit->startOperationCall(op);
    connect(job, SIGNAL(finished(KJob*)), m_serviceUnit, SLOT(deleteLater()));

    checkTimer((command == CMD_OFF) ? -1 : m_unit.value);

    refreshSlider();
    refreshOnOff();
}
コード例 #4
0
void TheorySets::check(Effort e) {
  if (done() && !fullEffort(e)) {
    return;
  }
  TimerStat::CodeTimer checkTimer(d_checkTime);
  d_internal->check(e);
}
コード例 #5
0
ファイル: fight.cpp プロジェクト: q4a/attal
void Fight::slot_animateFighting()
{
	attalFightData dataFight;

	if( !_attData->isEmpty() ) {
		if(  !_animatedUnit || !_animatedUnit->isMoving() ) { 
			dataFight = _attData->first();
			processData( dataFight );
			_attData->removeFirst();
		}
		if(  _animatedUnit && _animatedUnit->isMoving() ) {
			dataFight = _attData->first();
			while(( _animatedUnit == dataFight.unit && dataFight.dataType == FIGHT_DATA_MOVE ) ) {
				processData( dataFight );
				_attData->removeFirst();
				dataFight = _attData->first();
			}
		}
	} else {
		_animatedUnit = NULL;
	}

	checkTimer();

}
コード例 #6
0
ファイル: theory_sets.cpp プロジェクト: CVC4/CVC4
void TheorySets::check(Effort e) {
  if (done() && e < Theory::EFFORT_FULL) {
    return;
  }
  TimerStat::CodeTimer checkTimer(d_checkTime);
  d_internal->check(e);
}
コード例 #7
0
ファイル: main.c プロジェクト: Nrpickle/EECS162
//The check state function checks if a flag has been set, and if it has, it both resets the flag and changes and counters / states
void checkState(){
   if(checkTimer()){  //If the timer flag is set
      if(state == RED)
	 state = GREEN;
      else
	 ++state;

      //PORTC = ~PORTC;  //Debugging code
      resetTimer();

      if(state == YELLOW)
	 OCR1A = 7813; //Set the timer to 1 second (with 1024 prescaler)
      else
	 OCR1A = 23438;//Set the timer to 3 seconds (with 1024 prescaler)

      //Debugging USART statements
      if(state == RED)
	 USART_send_string("RED                           \r");
      else if (state == GREEN)
	 USART_send_string("GREEN                         \r");
      else
	 USART_send_string("YELLOW                        \r");
   }

}
void rayCrossed_isr()
{
   if(INPUT(PIN_B2))
   {
      rayTwo = 1;
      if(checkTimer() == 2 && rayOne == 1)
      {
         rayOne = 0;
      }
      if(rayOne == 1)
      {
         if(checkTimer() == 1)
         {
            noOfPeople++ ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
  else if(INPUT(PIN_B1))
   {
      rayOne = 1;
      if(checkTimer() == 2 && rayTwo == 1)
      {
         rayTwo = 0;
      }
      if(rayTwo == 1)
      {
         if(checkTimer() == 1 && noOfPeople>0)
         {
            noOfPeople-- ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
 }
void rayCrossed_isr()
{
   if(INPUT(PIN_B2)&& rayTwo!=1)
   {
      rayTwo = 1;
      if(rayOne == 1)
      {
         if(checkTimer() == 1)
         {
            noOfPeople++ ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
  else  if(INPUT(PIN_B1)&& rayOne!=1)
   {
      rayOne = 1;
      if(rayTwo == 1)
      {
         if(checkTimer() == 1)
         {
            noOfPeople-- ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
   delay_ms(MIN_DELAY_BETWEEN_TWO_INTERRUPTS/2);
   //OUTPUT_HIGH(PIN_B7);
   delay_ms(MIN_DELAY_BETWEEN_TWO_INTERRUPTS/2);
   //OUTPUT_LOW(PIN_B7);
 }
void rayCrossed_isr()
{
 //  if(INPUT(PIN_B2)&& rayTwo!=1)
   if(INPUT(PIN_B2))
   {
      rayTwo = 1;
      if(rayOne == 1)
      {
         if(checkTimer() == 1)
         {
            noOfPeople++ ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
 // else  if(INPUT(PIN_B1)&& rayOne!=1)
   else if(INPUT(PIN_B1))
   {
      rayOne = 1;
      if(rayTwo == 1)
      {
         if(checkTimer() == 1)
         {
            noOfPeople-- ;
            number_changed = 1;
         }
         rayOne = 0;
         rayTwo = 0;
      }
      setTimer();
   }
   delay_ms(MIN_DELAY_BETWEEN_TWO_INTERRUPTS);
 }
コード例 #11
0
//this function sends temp sensor data and battery state to coordinator
//If in router mode the function will use a timer to send at the set interval
//if in end device mode this function will send data and go to sleep for specified time interval
bool WSNode::sendSensorData() {
    bool xMit = true; //this stay true if transmit was good
    if(!batShutdown| !batCheckOn) { //check to see if we are in battery shutdown, if so all the module will do is sleep
        network.update(); //check to see if there is any network traffic that needs to be passed on, technically an end device does not need this

        if(timer) { //If in router mode this used to track when it is time to transmit again
            //create packet to transmit. First argument calculates temperature, second gets ADC reading from A2, third gives state of battery
            payload_t payload = { getSTTS751Temp(), checkBatteryVolt()};
            RF24NetworkHeader header(rXNode); //Create transmit header. This goes in transmit packet to help route it where it needs to go, in this case it is the coordinator
            //send data onto network and make sure it gets there1
            if (network.write(header,&payload,sizeof(payload))) {
                digitalWrite(7,LOW); //transmit was successful so make sure status LED is off
            }
            else  { //transmit failed, try again
                if (!network.write(header,&payload,sizeof(payload))) {
                    PIND |= (1<<PIND7); //this toggles the status LED at pin seven to show transmit failed
                    xMit = false; //transmit failed so let the user know
                }
                else  digitalWrite(7,LOW); //transmit was successful so make sure status LED is off
            }
        }
    }

    if(!mMode) { //if we are in end device mode get ready to go to sleep
        timer = true; //wer are not using the timer so make sure variable is set to true
        radio.powerDown(); //power down the nRF24L01 before we go to sleep
        ADCSRA &= ~(1<<ADEN); //Turn off ADC before going to sleep (set ADEN bit to 0). this saves even more power
        goToSleep(); //function for putting the Atmega328 to sleep
        ADCSRA |= (1<<ADEN); //Turn the ADC back on
        radio.powerUp();
    }
    else { //we are in router mode
        timer = checkTimer(); //update timer and check to see if it is time to transmit
        batShutdown = false; //this should never be true when in router mode
    }
    return xMit; //let user know if transmit was successful
}
コード例 #12
0
ファイル: theory_idl.cpp プロジェクト: jinala/CVC4
void TheoryIdl::check(Effort level) {
    if (done() && !fullEffort(level)) {
        return;
    }

    TimerStat::CodeTimer checkTimer(d_checkTime);

    while(!done()) {

        // Get the next assertion
        Assertion assertion = get();
        Debug("theory::idl") << "TheoryIdl::check(): processing " << assertion.assertion << std::endl;

        // Convert the assertion into the internal representation
        IDLAssertion idlAssertion(assertion.assertion);
        Debug("theory::idl") << "TheoryIdl::check(): got " << idlAssertion << std::endl;

        if (idlAssertion.ok()) {
            if (idlAssertion.getOp() == kind::DISTINCT) {
                // We don't handle dis-equalities
                d_out->setIncomplete();
            } else {
                // Process the convex assertions immediately
                bool ok = processAssertion(idlAssertion);
                if (!ok) {
                    // In conflict, we're done
                    return;
                }
            }
        } else {
            // Not an IDL assertion, set incomplete
            d_out->setIncomplete();
        }
    }

}
コード例 #13
0
ファイル: sm_macro.c プロジェクト: sigvartmh/heis3.0
//1 is up and 0 is down
current_state_t sm_door_open(int queues[N_QUEUES][N_FLOORS], int previousState) {

	elev_set_speed(0);
	int timer = 0;
	int currentFloor = elev_get_floor_sensor_signal();

	ui_set_door_open_lamp(1);
	startTimer();
	//bool
	while(timer != 1)
	{
		if(elev_get_stop_signal())
		{
            return STATE_STOP;
		}
		//listens for button signals, to take stop and order while the door is open
		ui_button_signals(queues);
		timer = checkTimer(3);
	}
	while(elev_get_obstruction_signal() != 0)
	{
		if(elev_get_stop_signal())
		{
			ui_set_door_open_lamp(0);
            return STATE_STOP;
		}
		ui_set_door_open_lamp(1);
	}
	ui_set_door_open_lamp(0);

    if (previousState == STATE_UP)
	{
		queues[QUEUE_COMMAND][currentFloor] = 0;
		//if on the top floor
		if(currentFloor == (N_FLOORS-1))
		{
			queues[QUEUE_DOWN][currentFloor] = 0;
		}
		//if orders over current in up queue
		else if(!queue_up_empty(queues, QUEUE_UP, currentFloor))
		{
			queues[QUEUE_UP][currentFloor] = 0;
		}
		//if no orders over current in up queue AND orders below in down queue
		else if(queue_up_empty(queues, QUEUE_UP, currentFloor) && !queue_down_empty(queues, QUEUE_DOWN, currentFloor))
		{
			queues[QUEUE_DOWN][currentFloor] = 0;
			return STATE_DOWN;
		}
		return STATE_UP;
	}

	else if(previousState == STATE_DOWN)
	{
		queues[QUEUE_COMMAND][currentFloor] = 0;
		//if on bottom floor
		if(currentFloor == 0)
		{
			queues[QUEUE_UP][currentFloor] = 0;
			//return IDLE??????????????????????????????
		}
		//if orders below in down queue
		else if(!queue_down_empty(queues, QUEUE_DOWN, currentFloor))
		{
			queues[QUEUE_DOWN][currentFloor] = 0;
		}
		//if no orders below in down queue and orders over in up queue
		else if(queue_down_empty(queues, QUEUE_DOWN, currentFloor) && !queue_up_empty(queues, QUEUE_UP, currentFloor))
		{
			queues[QUEUE_UP][currentFloor] = 0;
			return STATE_UP;
		}
		return STATE_DOWN;
	}
	return STATE_IDLE;
}
コード例 #14
0
/**
 * Initiats the main loop of the client that is taking non-blocking input and
 * outputing responses from the server
 * @param sm
 * Shared memory structure used to communicate between server and client
 */
void startClient(sharedMemory sm){
    char cmd[STRSIZE];
    int cmdLen,inNum;
    cmdLen = inNum = 0;
    
	#if defined(_WIN32)
		SYSTEMTIME timer;
	#else
		timeval timer;
	#endif
    
	
	resetTimer(timer);
    //TODO:refactor to initiation
    for(int i=0;i<MAXQUERIES;i++){
        queries[i].running = 0;
    }
    int nrOfOutstandingQueries = 0;
    
    sharedMemoryPtrs smptrs;
	getSharedVariablePtrs(&sm,&smptrs);
#if defined(_WIN32)
#else
	nonblock(NB_ENABLE);
#endif
    cout << "Enter nr to rotate and factor or quit/ctrl-c" <<endl;
    
    while(!quit){
        
        //Check if timeout has gone
        if(kbhit()){    //Checking for keyboard input
            
            if(getKBHitString(cmd,cmdLen)){ //Checking if the user is done typing command (end with CR)
                
                //Check that we can write to shared memory, should it block?
                //TODO: Check that nrofqueries hasnt exceeded 10, should block
                int res = getInNum(cmd);    //converting input to int, will be -1 if input is quit
                
                if(res == -1){
                    quit = 1;
                    *smptrs.number = res;
                    *smptrs.clientflag = '1';  //Setting 
                }else if(res == 0){
                    for(int i=0;i<3;i++){
                        writeToNumber(0,smptrs.number,smptrs.clientflag,nrOfOutstandingQueries);
                    }
                    
                }else{
					
                    writeToNumber(res,smptrs.number,smptrs.clientflag,nrOfOutstandingQueries);

                    
                }
            }   
        }
        //check if anything is to be read from shared mem
        if(nrOfOutstandingQueries){
            readResponse(queries,smptrs.slot,nrOfOutstandingQueries,smptrs.serverflag,timer);
            checkTimer(timer,smptrs.progress);
            
            
        }
        #if defined(_WIN32)
			Sleep(10);
		#else
			usleep(10000);
		#endif
        
    }
    //WAIT FOR SERVERSHUTDOWN SOMEHOW
#if defined(_WIN32)
#else
	nonblock(NB_DISABLE);
#endif
    cout << endl<<"quiting client after telling server and nicely wrapping myself up" <<endl;
}
コード例 #15
0
ファイル: riven.cpp プロジェクト: AlbanBedel/scummvm
void MohawkEngine_Riven::handleEvents() {
	// Update background running things
	checkTimer();
	bool needsUpdate = _gfx->runScheduledWaterEffects();
	needsUpdate |= _video->updateMovies();

	Common::Event event;

	while (_eventMan->pollEvent(event)) {
		switch (event.type) {
		case Common::EVENT_MOUSEMOVE:
			checkHotspotChange();

			if (!(getFeatures() & GF_DEMO)) {
				// Check to show the inventory, but it is always "showing" in the demo
				if (_eventMan->getMousePos().y >= 392)
					_gfx->showInventory();
				else
					_gfx->hideInventory();
			}

			needsUpdate = true;
			break;
		case Common::EVENT_LBUTTONDOWN:
			if (_curHotspot >= 0) {
				checkSunnerAlertClick();
				runHotspotScript(_curHotspot, kMouseDownScript);
			}
			break;
		case Common::EVENT_LBUTTONUP:
			// See RivenScript::switchCard() for more information on why we sometimes
			// disable the next up event.
			if (!_ignoreNextMouseUp) {
				if (_curHotspot >= 0)
					runHotspotScript(_curHotspot, kMouseUpScript);
				else
					checkInventoryClick();
			}
			_ignoreNextMouseUp = false;
			break;
		case Common::EVENT_KEYDOWN:
			switch (event.kbd.keycode) {
			case Common::KEYCODE_d:
				if (event.kbd.flags & Common::KBD_CTRL) {
					_console->attach();
					_console->onFrame();
				}
				break;
			case Common::KEYCODE_SPACE:
				pauseGame();
				break;
			case Common::KEYCODE_F4:
				_showHotspots = !_showHotspots;
				if (_showHotspots) {
					for (uint16 i = 0; i < _hotspotCount; i++)
						_gfx->drawRect(_hotspots[i].rect, _hotspots[i].enabled);
					needsUpdate = true;
				} else
					refreshCard();
				break;
			case Common::KEYCODE_F5:
				runDialog(*_optionsDialog);
				updateZipMode();
				break;
			case Common::KEYCODE_r:
				// Return to the main menu in the demo on ctrl+r
				if (event.kbd.flags & Common::KBD_CTRL && getFeatures() & GF_DEMO) {
					if (_curStack != kStackAspit)
						changeToStack(kStackAspit);
					changeToCard(1);
				}
				break;
			case Common::KEYCODE_p:
				// Play the intro videos in the demo on ctrl+p
				if (event.kbd.flags & Common::KBD_CTRL && getFeatures() & GF_DEMO) {
					if (_curStack != kStackAspit)
						changeToStack(kStackAspit);
					changeToCard(6);
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	if (_curHotspot >= 0)
		runHotspotScript(_curHotspot, kMouseInsideScript);

	// Update the screen if we need to
	if (needsUpdate)
		_system->updateScreen();

	// Cut down on CPU usage
	_system->delayMillis(10);
}
コード例 #16
0
ファイル: base.c プロジェクト: KuanSheng/CS502_OS
void    svc( SYSTEM_CALL_DATA *SystemCallData ) {
    short               call_type;
    static short        do_print = 10;
    short               i;
    int                 Time;
    int                 ID;
    int                 k=0,m=0,n=0;
    int                 PR;
    int                 sid,tid,mlen;
    int                 actual_length;
    long                tmp;
    INT32               diskstatus;
    long                tmpid;
    void                *next_context;
    char                *tmpmsg;
    PCB                 *head;
    PCB                 *head1;
    PCB *pcb = (PCB *)malloc(sizeof(PCB));


    call_type = (short)SystemCallData->SystemCallNumber;
    if ( do_print > 0 ) {
        printf( "SVC handler: %s\n", call_names[call_type]);
        for (i = 0; i < SystemCallData->NumberOfArguments - 1; i++ ) {
            //Value = (long)*SystemCallData->Argument[i];
            printf( "Arg %d: Contents = (Decimal) %8ld,  (Hex) %8lX\n", i,
                    (unsigned long )SystemCallData->Argument[i],
                    (unsigned long )SystemCallData->Argument[i]);
        }
    }
    switch(call_type) {
    case SYSNUM_GET_TIME_OF_DAY:
        MEM_READ(Z502ClockStatus, &Time);
        //Z502_REG1=Time;
        *SystemCallData->Argument[0]=Time;
        break;

    case SYSNUM_TERMINATE_PROCESS:
        tmpid = (long)SystemCallData->Argument[0];
        if(tmpid>=0) {
            os_delete_process_ready(tmpid);
            Z502_REG9 = ERR_SUCCESS;
        }
        else if(tmpid == -1)
        {
            head =Running;
            Running = NULL;
            while(readyfront==NULL&&timerfront==NULL) {
                Z502Halt();
            }
            //free(head);
            dispatcher();
            Z502SwitchContext( SWITCH_CONTEXT_SAVE_MODE, &(Running->context) );
        }
        else
            Z502Halt();
        break;
    //the execution of sleep();
    case SYSNUM_SLEEP:
        start_timer( (int)SystemCallData->Argument[0] );

        break;
    case SYSNUM_GET_PROCESS_ID:
        *SystemCallData->Argument[1] = os_get_process_id((char *)SystemCallData->Argument[0]);
        break;
    case SYSNUM_CREATE_PROCESS:
        strcpy(pcb->Processname , (char *)SystemCallData->Argument[0]);
        pcb->Priority = (long)SystemCallData->Argument[2];
        head = readyfront;
        head1 = readyfront;
        if(Pid < 20) {
            if(pcb->Priority >0) {
                if(readyfront == NULL&&readyrear == NULL) {
                    readyfront = pcb;
                    readyrear = pcb;
                    //*SystemCallData->Argument[4] = ERR_SUCCESS;
                    Z502_REG9 = ERR_SUCCESS;
                    pcb->pid = Pid;
                    pcb->next=NULL;
                    Toppriority = pcb->Priority;
                    *SystemCallData->Argument[3] = Pid;
                    //pcb->context = (void *)SystemCallData->Argument[1];
                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                    pcb->context = next_context;
                    Pid++;
                }
                else if(readyfront!=NULL&&readyrear!=NULL) {

                    if(checkPCBname(pcb) == 1) {

                        if(pcb->Priority < Toppriority) {
                            Z502_REG9 = ERR_SUCCESS;
                            pcb->next = readyfront;
                            readyfront = pcb;
                            pcb->pid = Pid;
                            pcb->next=NULL;
                            Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                            pcb->context = next_context;
                            *SystemCallData->Argument[3] = Pid;
                            Pid++;
                        }
                        else {
                            while(head->next!=NULL) {
                                if(pcb->Priority < head->Priority)
                                    break;
                                else
                                    head = head->next;
                            }

                            if(head->next!=NULL) {
                                while(head1->next!=head)
                                {
                                    head1=head1->next;
                                }
                                Z502_REG9 = ERR_SUCCESS;
                                head1->next = pcb;
                                pcb->next=head;
                                pcb->pid = Pid;
                                Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                pcb->context = next_context;
                                *SystemCallData->Argument[3] = Pid;
                                Pid++;
                            }
                            else {
                                if(pcb->Priority < head->Priority) {
                                    while(head1->next!=head)
                                    {
                                        head1=head1->next;
                                    }
                                    Z502_REG9 = ERR_SUCCESS;
                                    head1->next=pcb;
                                    pcb->next=head;
                                    pcb->pid = Pid;
                                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                    pcb->context = next_context;
                                    *SystemCallData->Argument[3] = Pid;
                                    Pid++;
                                }
                                else {
                                    Z502_REG9 = ERR_SUCCESS;
                                    head->next = pcb;
                                    readyrear = pcb;
                                    pcb->next=NULL;
                                    pcb->pid = Pid;
                                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                    pcb->context = next_context;
                                    *SystemCallData->Argument[3] = Pid;
                                    Pid++;
                                }
                            }

                        }
                    }
                    else free(pcb);
                }
            } else free(pcb);
        }
        else {
            Z502_REG9++;
            free(pcb);
        }
        break;

    case SYSNUM_SUSPEND_PROCESS:
        ID = (int)SystemCallData->Argument[0];
        head = suspendfront;
        while(head!=NULL) {
            if(head->pid == ID) {
                n = 1;
                break;
            }
            else
                head = head->next;
        }

        if(n!=1) {
            head = readyfront;
            while(head!=NULL) {
                if(head->pid == ID) {
                    Z502_REG9 = ERR_SUCCESS;
                    suspend_process_ready(head);
                    k = 1;
                    break;
                }
                else
                    head = head->next;
            }

            if(k == 0) {
                head = timerfront;
                while(head!=NULL) {
                    if(head->pid == ID) {
                        Z502_REG9 = ERR_SUCCESS;
                        suspend_process_timer(head);
                        m = 1;
                        break;
                    }
                    else
                        head=head->next;
                }
                if(m == 0&&k == 0) {
                    printf("illegal PID\n");
                }
            }
        }
        if(n == 1) {
            printf("can not suspend suspended process\n");
        }
        break;

    case SYSNUM_RESUME_PROCESS:
        ID = (int)SystemCallData->Argument[0];
        head = suspendfront;
        while(head!=NULL) {
            if(head->pid == ID) {
                k=1;
                break;
            }
            else
                head=head->next;
        }
        if(k==1)
            resume_process(head);
        else
            printf("error\n");
        break;
    case SYSNUM_CHANGE_PRIORITY:
        ID = (int)SystemCallData->Argument[0];
        PR = (int)SystemCallData->Argument[1];
        if(ID == -1) {
            Running->Priority = PR;
            Z502_REG9 = ERR_SUCCESS;
        }
        else {
            if(PR < 100) {
                if(checkReady(ID) == 1) {
                    head = readyfront;
                    while(head->pid != ID)
                        head=head->next;
                    change_priority_ready(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else if(checkTimer(ID) == 1) {
                    head = timerfront;
                    while(head->pid != ID)
                        head=head->next;
                    change_priority_timer(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else if(checkSuspend(ID) == 1) {
                    head = suspendfront;
                    while(head->pid != ID)
                        head = head->next;
                    change_priority_suspend(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else {
                    printf("ID ERROR!\n");
                }
            }
            else {
                printf("illegal Priority");
            }
        }
        break;
    case SYSNUM_SEND_MESSAGE:
        sid = Running->pid;
        tid = (int)SystemCallData->Argument[0];
        tmpmsg =(char *)SystemCallData->Argument[1];
        mlen = (int)SystemCallData->Argument[2];
        if(maxbuffer < 8) {
            if(tid < 100) {
                if(mlen < 100)
                {
                    if(tid>0)
                    {   send_message(sid,tid,mlen,tmpmsg);
                        maxbuffer++;
                    }
                    else if(tid == -1) {
                        send_message_to_all(sid,mlen,tmpmsg);
                        maxbuffer++;
                    }
                }
                else {
                    printf("illegal length!\n");
                }
            } else
                printf("illegal id!\n");
        }
        else
        {   printf("no space!\n");
            Z502_REG9++;
        }
        break;
    case  SYSNUM_RECEIVE_MESSAGE:
        sid = (int)SystemCallData->Argument[0];
        mlen = (int)SystemCallData->Argument[2];

        if(sid < 100) {
            if(mlen < 100) {
                if(sid == -1) {

                    receive_message_fromall();
                    if(msgnum>0) {
                        actual_length = strlen(checkmsg->msg_buffer);
                        if(mlen >actual_length) {
                            msg_out_queue(checkmsg);
                            *SystemCallData->Argument[3] = actual_length;
                            *SystemCallData->Argument[4] = checkmsg->source_pid;
                            strcpy((char *)SystemCallData->Argument[1] ,checkmsg->msg_buffer);
                            Z502_REG9 = ERR_SUCCESS;
                        }
                        else {
                            printf("small buffer!\n");
                        }
                    }
                }
                else {
                    receive_message_fromone(sid);
                    if(msgnum>0) {
                        actual_length = strlen(checkmsg->msg_buffer);
                        if(mlen >actual_length) {
                            msg_out_queue(checkmsg);
                            *SystemCallData->Argument[3] = actual_length;
                            *SystemCallData->Argument[4] = checkmsg->source_pid;
                            strcpy((char *)SystemCallData->Argument[1], checkmsg->msg_buffer);
                            Z502_REG9 = ERR_SUCCESS;
                        }
                        else {
                            printf("small buffer!\n");
                        }
                    }
                }
            }
            else
                printf("illegal length!\n");
        }
        else
            printf("illegal id!\n");
        break;
    case  SYSNUM_DISK_READ:
        disk_read(SystemCallData->Argument[0],SystemCallData->Argument[1],SystemCallData->Argument[2]);

        break;
    case SYSNUM_DISK_WRITE:
        disk_write(SystemCallData->Argument[0],SystemCallData->Argument[1],SystemCallData->Argument[2]);
        break;
    default:
        printf("call_type %d cannot be recognized\n",call_type);
        break;
    }

    do_print--;
}
コード例 #17
0
ファイル: sm_macro.c プロジェクト: ElevatorC/heis3.0
sm_state_t sm_open_door(int queues[N_QUEUES][N_FLOORS],sm_state_t previousState){

	elev_set_speed(0); //Stops the elevator
	
	int currentFloor = elev_get_floor_sensor_signal(); //Gets the current floor the elevator.

	// If the elevator has an obstruction between floors stop the elevator
	while(elev_get_obstruction_signal() && currentFloor == -1)
	{
		return previousState;
	}
	
	if(currentFloor == -1)
	{
		return previousState;
	}

	ui_set_door_open_lamp(1); //Open doors
	
	startTimer();
	while(checkTimer(3) != 1){
		
		if(elev_get_floor_sensor_signal() == -1)
		{
			break; //return previousState? both possible depends on wanted behaviour
		}

		else if(ui_get_stop_signal()) //Checks stop button for input while the door is open.
		{
            return STATE_STOP;
		}
		
		ui_button_signals(queues); //Checks for button input while the door is open.
		 
	}

	while(elev_get_obstruction_signal() != 0){

		ui_button_signals(queues); //Checks for button input while the obstruction is active.

		if(ui_get_stop_signal())  //Checks stop button for input when obstructed.
		{
            return STATE_STOP;
		}  

		ui_set_door_open_lamp(1); //Sets door lamp to Open.

	}

	ui_set_door_open_lamp(0); //Sets door lamp to closed.

	//If it came from the state of moving up the it checks the following cases.
    if (previousState == STATE_MOVE_UP)
	{
		queues[QUEUE_COMMAND][currentFloor] = 0; //Reached ordered command floor and clears queue element.

		//If the top floor is reached it switches state to check if there is any orders bellow.
		if(currentFloor == N_FLOORS-1) 
		{
			queues[QUEUE_DOWN][currentFloor] = 0;  //Clear ordered floor in down queue.
			return STATE_MOVE_DOWN;
		}

		//Reached queue element in up that is ordered and clears it.
		else if(!queue_from_and_up_empty(queues, QUEUE_UP, currentFloor)) 
		{
			queues[QUEUE_UP][currentFloor] = 0;
		}
		
		//If no orders over current in up queue AND orders below in down queue.
		else if(queue_from_and_up_empty(queues, QUEUE_UP, currentFloor) && !queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor))
		{ 
			queues[QUEUE_DOWN][currentFloor] = 0;
			return STATE_MOVE_DOWN; 
		}

		//More orders in up queue so returns to its previous state and continoues.
		return STATE_MOVE_UP;

	}
	
	//If it came from the state of moving down the it checks the following cases.
	else if(previousState == STATE_MOVE_DOWN)
	{
		queues[QUEUE_COMMAND][currentFloor] = 0; //Reached ordered command floor and clears queue element

		//If the bottom floor is reached it switches state to check if there is any orders abow.
		if(currentFloor == 0)
		{
			queues[QUEUE_UP][currentFloor] = 0;
			return STATE_MOVE_UP;
		}

		//Reached queue element in down that is ordered and clears it.
		else if(!queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor))
		{
			queues[QUEUE_DOWN][currentFloor] = 0;
		}

		//If no orders below in down queue AND orders over in up queue.
		else if(queue_from_and_down_empty(queues, QUEUE_DOWN, currentFloor) && !queue_from_and_up_empty(queues, QUEUE_UP, currentFloor))
		{
			queues[QUEUE_UP][currentFloor] = 0;
			return STATE_MOVE_UP;
		}
		
		//More orders in down queue so returns to its previous state and continoues.
		return STATE_MOVE_DOWN;

	}
	
	//Returns to previous state
	else if(previousState == STATE_MOVE_UP)
	{
			return STATE_MOVE_UP;
	}

	else
	{
			return STATE_MOVE_DOWN;
	}

}