Ejemplo n.º 1
0
/*
read card serial id
*/
uint8_t rc522_read_card_id(uint8_t *card_id, uint16_t *card_type)
{
	uint8_t status, resx = 0;
	uint8_t buff_data[MAX_LEN]; 

    *card_type = 0;	
	if(mfrc522_is_card(card_type))
	{
		status = mfrc522_get_card_serial(buff_data);
		if (status==CARD_FOUND)
		{
			memcpy(card_id,buff_data,5);//kopi id and checksum at last byte (5th)
			resx = 1;
		}
		else
		{
			resx = 0;
		}
	}
	else
	{
		resx = 0;
	}
	
	return resx;
}
Ejemplo n.º 2
0
void rfid_irq_tag_handler()
{
	__disable_irq();
	uint8_t tag_id[16];
	uint8_t status = mfrc522_get_card_serial(tag_id);
	__enable_irq();

	if (status == CARD_FOUND) {
		static uint8_t led_state = 0;
		led_state ? GPIO_SetBits(GPIOA, GPIO_Pin_1) : GPIO_ResetBits(GPIOA, GPIO_Pin_1);
		led_state ^= 1;

		// Check if tag was cached.
		uint8_t flags;

		tag_cache_entry *cache = nullptr;

		for (auto it = tag_cache.begin(); it != tag_cache.end(); ++it) {
			if (*((uint32_t *) &(it->tag_id[0])) == *((uint32_t *) &(tag_id[0]))) {
				cache = &(*it);
				break;
			}
		}

		// We have to do the next things:
		// a) Get access for the tag.
		// Access can be retreived from network or via cache.
		// b) Track event about access request.
		// Events can be tracked directly by network request or via RabitMQ queue.

		// If no, request data from network and cache result.
		if (cache)
		{
			// If queue has space then use cache and store event to the cache.
			if (!tag_events.full) {
				tag_event event;
				memcpy(event.tag_id, tag_id, 4);
				event.event_time = RTC_GetCounter();
				event.node = get_pcd_id();

				// Add event to the queue.
				tag_events.push_back(event);

				// Get access flags from cache.
				flags = cache->flags;
			}
			// Otherwise initiate direct network request.
			else {
				flags = somi_access_check(tag_id, mac_addr, get_pcd_id());
			}
		}
		// If cache hasn't tag request data from network and store it to the cache.
		else {
			flags = somi_access_check(tag_id, mac_addr, get_pcd_id());
			if (!tag_cache.full) {
				tag_cache_entry cache_entry;

				memcpy(cache_entry.tag_id, tag_id, 4);
				cache_entry.flags = flags;
				cache_entry.cached = RTC_GetCounter();

				tag_cache.push_back(cache_entry);
			}
		}

		// Trigger event handler.
		somi_event_handler(EVENT_ACCESS_REQ, flags);
	}

	// Activate timer.
	rc522_irq_prepare();
}
Ejemplo n.º 3
0
int main()
{
	DDRC &= ~(1<<PC0);				//input for DPDT switch
	increase_day_count_eeprom();	//some more initialization of EEPROM
	
	/*** Students list ***/
	/** These are the bytes that are read from the RFID tags of the students**/
	uint8_t person_byte[MAX_PEOPLE][5] = {
		{0x23, 0x6D, 0xD6, 0x00, 0x98} , 
		{0xF9, 0x46, 0x1D, 0x00, 0xA2}, 
		{0xA3, 0x7E, 0x30, 0x02, 0xEF}
		
	};
	
	//Some tags that we used to experiment
	//{0xF9, 0x46, 0x1D, 0x00, 0xA2}
	//{0x23, 0x6D, 0xD6, 0x00, 0x98} - recognized1
	//{0xF9, 0x46, 0x1D, 0x00, 0xA2} - recognized2 (change any bit in any of these 2 to see effect of unrecognized person entering)
	//{0xA3, 0x7E, 0x30, 0x02, 0xEF} - Nimi - white
	//{0x5B, 0xA8, 0x2C, 0x00, 0xDF} - Adnan - blue
		
	// person name list .. these used to control people names etc
	int detected_person;
	int person_entry_list[MAX_PEOPLE] = {0, 0, 0};
	char *person_name[MAX_LEN] = { (char *)"Sibat", (char *)"Ripon" , (char *)"Nimi" };
	char *entered_msg = (char *)" entered";
	char *left_msg = (char *)" left";
	char msg_to_show[100];
	int person_count = 0;
	
	// iterator and byte array to use later
	uint8_t byte, i;
	uint8_t str[MAX_LEN];
	_delay_ms(50);
	
	// initialize the LCD
	LCDInit(LS_BLINK);
	LCDWriteStringXY(2,0,"RFID Reader");
	
	// spi initialization
	spi_init();
	_delay_ms(1000);
	LCDClear();
	
	//init reader
	mfrc522_init();
	
	// poll until reader is found
	while(1)
	{
		byte = mfrc522_read(VersionReg);
		if(byte == 0x92)
		{
			LCDClear();
			LCDWriteStringXY(2,0,"Detected");
			_delay_ms(1000);
			break;
		}
		else
		{
			LCDClear();
			LCDWriteStringXY(0,0,"No reader found");
			_delay_ms(800);
			LCDClear();
			LCDWriteStringXY(0, 0, "Connect reader");
			_delay_ms(1000);
		}
	}
	
	// ready to run
	_delay_ms(1500);
	LCDClear();
	
	// initializing the RGB LEDs
	DDRA = 0xFF;
	PORTA = 0xFE;
	
	
	//Interrupt INT2
	DDRB |= (1<<PB2);		// Set PB2 as input (Using for interrupt INT2)
	PORTB |= (1<<PB2);		// Enable PB2 pull-up resistor
	
	// setting up the timer codes
	// setting up the LED animation initials
	// TODO timer code ... START HERE ...
	TCCR1A = 0x00;
	TCCR1B = 0x01;
	TIMSK = 0x04;
	min = 0;
	sec = 0;
	hour = 0;
	sec_counter = 0;
	LED_animation_status = 0;
	LED_animation_on = 1;
	program_status_2_first_time = 1;
	program_status_3_first_time = 1;
	program_status_4_first_time = 1;
	
	// TODO timer code ... ENDS  HERE ...
	
	// The loop starts here
	// program status : 1 means entrance period
	//				  : 2 means session period
	//				  : 3 means session ended
	program_status = 1;
	
	//Using interrupt 2 for reading history mode
	GICR &= ~(1<<INT2);		// Disable INT2
	MCUCSR |= (1<<ISC2);	// Trigger INT2 on 1 = rising edge , 0 falling edge
	GIFR |= (1<<INTF2);	// clear INTF2
	GICR |= (1<<INT2);		// Enable INT2
	
	sei();
	while(1)
	{
		spi_init();
		mfrc522_init();
		byte = mfrc522_read(ComIEnReg);
		mfrc522_write(ComIEnReg,byte|0x20);
		byte = mfrc522_read(DivIEnReg);
		mfrc522_write(DivIEnReg,byte|0x80);
		
		if(program_status == 1)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "Show your card.");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				byte = mfrc522_get_card_serial(str);
				if(byte == CARD_FOUND)
				{
					LED_animation_on = 0;
					
					// check for person 1
					detected_person = -1;
					for(i = 0; i < MAX_PEOPLE; i++)
					{
						for(byte = 0; byte < 5; byte++)
						{
							/***The following piece of code was written to test and read the bytes off the tags and print them***/
							//LCDClear();
							//LCDWriteIntXY(0,0,byte,2);
							//LCDWriteIntXY(0,1,str[byte],14);
							//_delay_ms(3000);
							
							if(str[byte] != person_byte[i][byte])
							{
								break;
							}
						}
						if(byte == 5)
						{
							detected_person = i;
							break;
						}
					}
					
					if(i == MAX_PEOPLE)
					{
						detected_person = -1;
					}
					
					// showing message on LCD upon decision of the person
					LCDClear();
					if(detected_person == -1)
					{
						PORTA = 0x7E;
						LCDClear();
						LCDWriteStringXY(0, 0, "Access denied!");
						_delay_ms(2000);
						PORTA = 0xFE;
						LCDClear();
						LCDWriteStringXY(0, 0, "Unrecognized!");
					}
					else
					{
						PORTA = 0xFD;
						LCDClear();
						LCDWriteStringXY(0, 0, "Access granted!");
						_delay_ms(2000);
						if(person_entry_list[i] == 0)
						{
							person_entry_list[i] = 1;
						}
						else
						{
							person_entry_list[i] = 0;
						}
						strcpy(msg_to_show, person_name[detected_person]);
						if(person_entry_list[i] == 0)
						{
							/** Code to sound buzzer when a student is leaving **/
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							// end of buzzer code
							
							//EEPROM WRITE
							if(write_enable_eeprom == 1){
								eeprom_update_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 0);
							}
							person_count--;
							strcat(msg_to_show, left_msg);
						}
						else
						{
							/** Code to sound buzzer when a student is entering **/
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							_delay_ms(100);
							PORTA = 0x7D;
							_delay_ms(200);
							PORTA = 0xFD;
							// end of buzzer code
							
							//EEPROM WRITE
							if(write_enable_eeprom == 1) {
								eeprom_write_byte ((uint8_t*) (&NonVolatileIsPresent[curr_day][i]), 1);
								eeprom_update_byte ((uint8_t*) (&NonVolatileHour[curr_day][i]), hour);
								eeprom_update_byte ((uint8_t*) (&NonVolatileMinute[curr_day][i]), min);
								eeprom_update_byte ((uint8_t*) (&NonVolatileSecond[curr_day][i]), sec);
							}
							person_count++;
							strcat(msg_to_show, entered_msg);
						}
						LCDClear();
						LCDWriteStringXY(0, 0, msg_to_show);
					}
					
					_delay_ms(3000);
					
					PORTA = 0xFE;
					LCDClear();
					LED_animation_on = 1;
				}
				else
				{
					LCDClear();
					LCDWriteStringXY(0,1,"Error");
				}
			}
			
			_delay_ms(200);
		}
		else if(program_status == 2)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "In session now!");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				LED_animation_on = 0;
				PORTA = 0x7E;
				LCDClear();
				LCDWriteStringXY(0, 0, "Warning!!");
				_delay_ms(2000);
				LCDClear();
				LCDWriteStringXY(0, 0, "Session running.");
				_delay_ms(2000);
				PORTA = 0xFE;
				LCDClear();
				LCDWriteStringXY(0, 0, "Can't go out/in.");
				_delay_ms(2000);
				LCDClear();
				PORTA = 0xFB;
				LED_animation_on = 1;
			}
			_delay_ms(200);
		}
		else if(program_status == 3)
		{
			LCDClear();
			LCDWriteStringXY(0, 0, "Session ended!");
			LCDWriteStringXY(0, 1, "#students:");
			LCDWriteIntXY(12,1, person_count ,2 );
			
			byte = mfrc522_request(PICC_REQALL,str);
			if(byte == CARD_FOUND)
			{
				byte = mfrc522_get_card_serial(str);
				if(byte == CARD_FOUND)
				{
					LED_animation_on = 0;
					
					// check for person 1
					detected_person = -1;
					for(i = 0; i < MAX_PEOPLE; i++)
					{
						for(byte = 0; byte < 5; byte++)
						{
							if(str[byte] != person_byte[i][byte])
							{
								break;
							}
						}
						if(byte == 5)
						{
							detected_person = i;
							break;
						}
					}
					
					// showing message on LCD upon decision of the person
					if(detected_person == -1)
					{
						PORTA = 0x7E;
						LCDClear();
						LCDWriteStringXY(0, 0, "Unrecognized!");
						_delay_ms(2000);
						PORTA = 0xFE;
					}
					else
					{
						
						PORTA = 0xFB;
						if(person_entry_list[detected_person] != 0){
							LCDClear();
							strcpy(msg_to_show, "Take care ");
							strcat(msg_to_show, person_name[detected_person]);
							LCDWriteStringXY(0, 0, msg_to_show);
							_delay_ms(1000);
							LCDClear();
							strcpy(msg_to_show, person_name[detected_person]);
							strcat(msg_to_show, left_msg);
							LCDWriteStringXY(0, 0, msg_to_show);
							person_entry_list[detected_person] = 0;
							person_count--;
							//unnecessary sanity check
							if(person_count == 0)
							{
								break;
							}
						}
						
					}
					
					_delay_ms(3000);
					
					PORTA = 0xFD;
					LCDClear();
					LED_animation_on = 1;
				}
				else
				{
					PORTA = 0x7E;
					LCDClear();
					LCDWriteStringXY(0,1,"Error");
					_delay_ms(2000);
					PORTA = 0xFE;
					LCDClear();
					PORTA = 0xFD;
				}
			}
			
			_delay_ms(200);
		}
		else
		{
			/***This is the warning phase.
			When leaving period has ended, but some students were still stuck in the classroom,
			the buzzer buzzes off continuously suspecting that some students might be sick or in trouble.
			***/
			if(person_count != 0)
			{
				LED_animation_on = 0;
				PORTA = 0x7E;
				
				LCDClear();
				LCDWriteIntXY(0,0, person_count ,2 );
				LCDWriteStringXY(3, 0, "student could");
				LCDWriteStringXY(0, 1, "not get out");
				_delay_ms(1500);
				LCDClear();
				LCDWriteStringXY(0, 0, "Take caution!");
				_delay_ms(1500);
				PORTA = 0xFE;
				_delay_ms(400);
			}
			else
			{
				/**When all students have left, nothing to do. **/
				LCDClear();
				break;
			}
		}
	}
	
	LED_animation_on = 1;
	PORTA = 0xFB;
	temp_PORTA = PORTA;
	LCDClear();
	LCDWriteStringXY(0, 0, "Everyone left.");
	while(1)
	{
		;
	}
	
	cli();
	
}