void loop()
{
    Serial.print(F("Before encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    time = micros(); // time start
    // ------------------------------------------
//        for (int piece = 0; piece < N_BLOCK; piece += N_BLOCK, pPlain += N_BLOCK)
    for (int piece = 0; piece < DATALENGTH; piece += N_BLOCK, pPlain += N_BLOCK)
    {
	// Divide message into a block of 128 bit = 16 byte
	//	byte cipher[N_BLOCK];
	//	getBlockOfMsg(pPlain, cipher);

	//	if (beVerbose)
	//	{
	//	    Serial.println();
	//	    Serial.println("OT:");
	//	    for (unsigned int i = 0; i < N_BLOCK; i++)
	//		Serial.print(char(cipher[i]));
	//	}

	//	time = micros(); // time start
	//  aes192_enc(cipher, &key_init); // Encrypt message
	aes192_enc(pPlain, &key_init); // Encrypt message
	//	emit = micros(); // time end
	//	Serial.print(F("Encryption total takes: "));
	//	Serial.print(emit - time);
	//	Serial.println(F(" [us]"));

	//	if (beVerbose)
	//	{
	//		    Serial.println();
	//		    Serial.println("CT:");
	//		    for (int i = 0; i < N_BLOCK; i++)
	//			Serial.print(cipher[i]);
	//		    
	//		    Serial.println();
	//		    Serial.println("HASH:");
	//		    for (int i = 0; i < N_BLOCK; i++)
	//			Serial.print(hash[i]);
	//	}
    }
    //	void hmac_md5(void* dest, void* key, uint16_t keylength_b, void* msg, uint32_t msglength_b);	
    byte hash[20];
    hmac_sha1(hash, key, 192, plain, 8192);
    // ------------------------------------------
    emit = micros(); // time start

    Serial.print(F("After encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    Serial.print(F("Encryption total takes: "));
    Serial.print(emit - time);
    Serial.println(F(" [us]"));

    Serial.println();

    Serial.println(F("Light"));
    digitalWrite(ledPin, HIGH); // set the LED on
    delay(1000);

    Serial.println(F("Dark"));
    digitalWrite(ledPin, LOW); // set the LED off
    delay(1000);
}
void loop()
{
    Serial.print(F("Before encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    time = micros(); // time start
    // ------------------------------------------
//    for (int piece = 0; piece < N_BLOCK; piece += N_BLOCK, pPlain += N_BLOCK)
	    for (int piece = 0; piece < DATALENGTH; piece += N_BLOCK, pPlain += N_BLOCK)
    {
	// Divide message into a block of 128 bit = 16 byte
	byte cipher[N_BLOCK];
	getBlockOfMsg(pPlain, cipher);

	//	if (beVerbose)
	//	{
	//	    Serial.println();
	//	    Serial.println("OT:");
	//	    for (unsigned int i = 0; i < N_BLOCK; i++)
	//		Serial.print(char(cipher[i]));
	//	}

	//	time = micros(); // time start
	aes256_enc(cipher, &key_init); // Encrypt message
	//	emit = micros(); // time end

	//	Serial.print(F("Encryption total takes: "));
	//	Serial.print(emit - time);
	//	Serial.println(F(" [ms]"));

	//	if (beVerbose)
	//	{
	//	    Serial.println();
	//	    Serial.println("CT:");
	//	    for (int i = 0; i < N_BLOCK; i++)
	//		Serial.print(cipher[i]);
	//	}

	//	delete [] cipher; // do not forget to free ram
    }
    // ------------------------------------------
    emit = micros(); // time start

    Serial.print(F("After encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    Serial.print(F("Encryption total takes: "));
    Serial.print(emit - time);
    Serial.println(F(" [us]"));

    Serial.println();

    Serial.println(F("Light"));
    digitalWrite(ledPin, HIGH); // set the LED on
    delay(1000);

    Serial.println(F("Dark"));
    digitalWrite(ledPin, LOW); // set the LED off
    delay(1000);
}
Esempio n. 3
0
void test_memory()
{
	DEBUG("Free RAM now ... %d\n", freeRam());
}
Esempio n. 4
0
// Provides a quick test of several methods as a pseudo-unit test.
void Sodaq_RN2483::runTestSequence(SerialType& loraStream, Stream& debugStream)
{
#ifdef DEBUG
    debugPrint("free ram: ");
    debugPrintLn(freeRam());

    init(loraStream);

    this->loraStream = &loraStream;
    this->diagStream = &debugStream;

    // expectString
    debugPrintLn("write \"testString\" and then CRLF");
    if (expectString("testString", 5000)) {
        debugPrintLn("[expectString] positive case works!");
    }

    debugPrintLn("");
    debugPrintLn("write something other than \"testString\" and then CRLF");
    if (!expectString("testString", 5000)) {
        debugPrintLn("[expectString] negative case works!");
    }

    debugPrint("free ram: ");
    debugPrintLn(freeRam());

    // setMacParam(array)
    debugPrintLn("");
    debugPrintLn("");
    uint8_t testValue[] = {0x01, 0x02, 0xDE, 0xAD, 0xBE, 0xEF};
    setMacParam("testParam ", testValue, ARRAY_SIZE(testValue));

    // macTransmit
    debugPrintLn("");
    debugPrintLn("");
    uint8_t testValue2[] = {0x01, 0x02, 0xDE, 0xAD, 0xBE, 0xEF};
    macTransmit(STR_CONFIRMED, 1, testValue2, ARRAY_SIZE(testValue2));

    debugPrint("free ram: ");
    debugPrintLn(freeRam());

    // receive
    debugPrintLn("");
    debugPrintLn("==== receive");
    char mockResult[] = "303132333435363738";
    memcpy(this->receivedPayloadBuffer, mockResult, strlen(mockResult) + 1);
    uint8_t payload[64];
    debugPrintLn("* without having received packet");
    uint8_t length = receive(payload, sizeof(payload));
    debugPrintLn(reinterpret_cast<char*>(payload));
    debugPrint("Length: ");
    debugPrintLn(length);
    debugPrintLn("* having received packet");
    this->packetReceived = true;
    length = receive(payload, sizeof(payload));
    debugPrintLn(reinterpret_cast<char*>(payload));
    debugPrint("Length: ");
    debugPrintLn(length);

    // onMacRX
    debugPrintLn("");
    debugPrintLn("==== onMacRX");
    char mockRx[] = "mac_rx 1 303132333435363738";
    memcpy(this->inputBuffer, mockRx, strlen(mockRx) + 1);
    this->packetReceived = false;// reset
    debugPrint("Input buffer now is: ");
    debugPrintLn(this->inputBuffer);
    debugPrint("onMacRX result code: ");
    debugPrintLn(onMacRX());
    uint8_t payload2[64];
    if (receive(payload2, sizeof(payload2)) != 9) {
        debugPrintLn("len is wrong!");
    }
    debugPrintLn(reinterpret_cast<char*>(payload2));
    if (receive(payload2, sizeof(payload2), 2) != 7) {
        debugPrintLn("len is wrong!");
    }
    debugPrintLn(reinterpret_cast<char*>(payload2));
    if (receive(payload2, sizeof(payload2), 3) != 6) {
        debugPrintLn("len is wrong!");
    }
    debugPrintLn(reinterpret_cast<char*>(payload2));

    debugPrint("free ram: ");
    debugPrintLn(freeRam());

    // lookup error
    debugPrintLn("");
    debugPrintLn("");

    debugPrint("empty string: ");
    debugPrintLn((lookupMacTransmitError("") == NoResponse) ? "passed" : "wrong");

    debugPrint("\"random\": ");
    debugPrintLn((lookupMacTransmitError("random") == NoResponse) ? "passed" : "wrong");

    debugPrint("\"invalid_param\": ");
    debugPrintLn((lookupMacTransmitError("invalid_param") == InternalError) ? "passed" : "wrong");

    debugPrint("\"not_joined\": ");
    debugPrintLn((lookupMacTransmitError("not_joined") == NotConnected) ? "passed" : "wrong");

    debugPrint("\"busy\": ");
    debugPrintLn((lookupMacTransmitError("busy") == Busy) ? "passed" : "wrong");

    debugPrint("\"invalid_param\": ");
    debugPrintLn((lookupMacTransmitError("invalid_param") == InternalError) ? "passed" : "wrong");

    debugPrint("free ram: ");
    debugPrintLn(freeRam());
#endif
}
Esempio n. 5
0
File: main.c Progetto: maxxir/test1
int main(void)
{
    // INIT MCU
	avr_init();
	uint32_t prev_time = 0;
	float uptime_float = 0;
	//char msg[64] = "Unknown time\r\n";
	

	// Print program metrics
	/*
	USARTE0_WriteString_P(str_prog_name);// Название программы
	USARTE0_WriteString_P(PSTR("Compiled at: "));
	USARTE0_WriteString_P(compile_time); // Время компиляции
	USARTE0_WriteString_P(PSTR(" ")); 
	USARTE0_WriteString_P(compile_date); // Дата компиляции 
	USARTE0_WriteString_P(PSTR("\r\n"));
	*/
	//Working via printf
	printf_P(str_prog_name);// Название программы
	printf_P(PSTR("Compiled at: "));
	printf_P(compile_time); // Время компиляции
	printf_P(PSTR(" ")); 
	printf_P(compile_date); // Дата компиляции 
	printf_P(PSTR("\r\n"));
	
	
	// FreeRam DEBUG
	//sprintf_P(msg, PSTR(">>>Free RAM is: %u bytes\r\n\r\n"), freeRam());
	//USARTE0_WriteString(msg);
	printf_P(PSTR(">>>Free RAM is: %u bytes\r\n\r\n"), freeRam());
	
	
//***************************Profiling micros: BEGIN
//Accurate Measure time interval from 1us till ~ 65.5ms
	uint16_t _micros;
	
	
	// Profiling time for 10us
	_micros = TCD0.CNT;
	_delay_us(10);
	_micros = TCD0.CNT - _micros;
	printf_P(PSTR(">>>10us is: %u us\r\n"), _micros);


	// Profiling time for 100us
	_micros = TCD0.CNT;
	_delay_us(100);
	_micros = TCD0.CNT - _micros;
	printf_P(PSTR(">>>100us is: %u us\r\n"), _micros);


	// Profiling time for 1ms
	_micros = TCD0.CNT;
	_delay_ms(1);
	_micros = TCD0.CNT - _micros;
	printf_P(PSTR(">>>1ms is: %u us\r\n"), _micros);


	// Profiling time for 65ms
	_micros = TCD0.CNT;
	_delay_ms(65);
	_micros = TCD0.CNT - _micros;
	printf_P(PSTR(">>>65ms is: %u us\r\n"), _micros);

//***************************Profiling micros: END
	
//**************************Bench IO speed
	BENCH_IO_std_XMEGA_profile();
	BENCH_IO_std_XMEGA();  			//!! 2 cycle per instruction MIDDLE SPEED
	//BENCH_IO_tgl_XMEGA(); 		//!! 2 cycle per instruction MIDDLE SPEED
	//BENCH_IO_VirtualPort_XMEGA(); //!! 1 cycle per instruction FASTEST
	//BENCH_IO_std_MEGA(); //!! 5 cycle per instruction SLOWEST
	
	while(1);
	
    for(;;)
    {
		//Nothing to do
		//asm("nop");
		
		// Print OUT second tick & Received from RX
		if (prev_time != uptime)
		{
			// Here every second
			//!! Uptime Handle
			prev_time = uptime;
			//sprintf(msg, "Uptime is: %lu sec\r\n", prev_time);
			//USARTE0_WriteString(msg);
			printf_P(PSTR("Uptime is: %lu sec\r\n"), prev_time);
			uptime_float += 0.01;
			
			//!! Don't forget correct <makefile> PRINTF_LIB for:
			/*
			PRINTF_LIB = $(PRINTF_LIB_FLOAT)
			*/
			printf_P(PSTR("Uptime float is: %.2f \r\n"), uptime_float);
			
			//!! Received from RX Handle
			// Check received index
			if (USARTE0_rx_buf_idx > 0)
			{
				cli(); // Disable IRQ
				static char rx_buf[rx_buf_MAX+1];
				uint8_t idx = USARTE0_rx_buf_idx;
				memset(rx_buf, 0, sizeof(rx_buf)); // Fill RX_Buffer with Zeros
				memcpy(rx_buf, USARTE0_rx_buf, idx); // Transfer RX data to Temporary buffer
				USARTE0_rx_buf_idx = 0; // Set zero Received RX counter
				sei(); // Enable IRQ
				
				// Print OUT RX Received data
				//sprintf(msg, ">>RX: %s\r\n", rx_buf);
				//USARTE0_WriteString(msg);
				printf_P(PSTR(">>RX: %s\r\n"), rx_buf);
			}
		}
		/*
		// Not used because USARTE0_RX via IRQ
		else
		{
			// Check receive data from USARTE0 RX
			if (USARTE0_RX_Available())
			{
				//Print OUT recieved from USARTE0 RX
				rx_char[0] = USARTE0_ReadChar();
				sprintf(msg, ">> %s\r\n", rx_char);
				USARTE0_WriteString(msg);
			}
		}
		*/
    }
    return(0);
}
void loop()
{
    Serial.print(F("Before encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    time = micros(); // time start
    // ------------------------------------------
//    Serial.println(F("CT:"));
    for (int piece = 0; piece < DATALENGTH; piece++)
//	for (int piece = 0; piece < 1; piece++)
    {
	// Divide message into a block of 128 bit = 16 byte
	//	byte* cipher = getBlockOfMsg(pPlain);
	//	byte vysledok = grain_getbyte(&reg);

	//	if (beVerbose)
	//	{
	//	    Serial.println();
	//	    Serial.println("OT:");
	//	    for (unsigned int i = 0; i < N_BLOCK; i++)
	//		Serial.print(char(cipher[i]));
	//	}

	//	time = micros(); // time start
	//	present128_enc(cipher, &key_init);

	plain[piece] ^= mickey128_getbyte(&reg);

	//	cipher[offset] = plainSent[offset]^vysledok;

	//	emit = micros(); // time end

	//	Serial.print(F("Encryption total takes: "));
	//	Serial.print(emit - time);
	//	Serial.println(F(" [us]"));
//		if (beVerbose)
//		{
//		    Serial.println();
//		    Serial.println("CT:");
//		    for (int i = 0; i < N_BLOCK; i++)
//			Serial.print(plain[piece]);
//		}

	//	delete [] cipher; // do not forget to free ram
    }

    byte hash[16];
    //    void hmac_md5(void* dest, void* key, uint16_t keylength_b, void* msg, uint32_t msglength_b){ /* a one-shot*/
    hmac_md5(hash, key, 80, plain, 8192);
//    hmac_md5(hash, key, 80, plain, 8);
    // ------------------------------------------
    emit = micros(); // time start

    Serial.print(F("After encryption of message freeMemory()= "));
    Serial.print(freeRam());
    Serial.println(F(" [byte]"));

    Serial.print(F("Encryption total takes: "));
    Serial.print(emit - time);
    Serial.println(F(" [us]"));

//    Serial.println();
//    Serial.println(F("OT:"));
//    for (int piece = 0; piece < DATALENGTH; piece++)
//    {
    //	plain[piece] ^= grain_getbyte(&reg2);
//	Serial.print(char(plain[piece]));
//    }
    Serial.println();

    Serial.println(F("Light"));
    digitalWrite(ledPin, HIGH); // set the LED on
    delay(1000);

    Serial.println(F("Dark"));
    digitalWrite(ledPin, LOW); // set the LED off
    delay(1000);
}
Esempio n. 7
0
int main(void){
	init();

	while (1){
		/*
		 * This function will look for new messages from the AFSK channel.
		 * It will call the message_callback() function when a new message is received.
		 * If there's nothing to do, this function will call cpu_relax()
		 */
		ax25_poll(&g_ax25);

		check_run_mode();

		switch(currentMode){
			case MODE_CFG:
#if MOD_CONSOLE
				console_poll();
#endif
#if MOD_BEACON
				beacon_broadcast_poll();
#endif
				break;
#if MOD_TRACKER
			case MODE_TRACKER:
				tracker_poll();
				break;
#endif

#if MOD_KISS
			case MODE_KISS:{
				kiss_poll();
				break;
			}
#endif

#if MOD_DIGI
			case MODE_DIGI:{
				console_poll();
				beacon_broadcast_poll();
				break;
			}
#endif

			default:
				break;
		}// end of switch(runMode)

#if DEBUG_FREE_RAM
		{
			static ticks_t ts = 0;

			if(timer_clock_unlocked() -  ts > ms_to_ticks(5000)){
				ts = timer_clock_unlocked();
				uint16_t ram = freeRam();
				SERIAL_PRINTF((&g_serial),"%u\r\n",ram);
			}
		}
#endif
#if DEBUG_SOFT_SER
		// Dump the isr changes
		{
			SoftSerial *softSer = radioPort;
			static uint32_t i = 0;
			//static uint32_t j = 0;
			if(i++ == 30000){
				i = 0;

				char c;
				while(softser_avail(softSer)){
					c = softser_read(softSer);
					kfile_putc(c,&(g_serial.fd));
				}
				char buf[8];
				sprintf_P(buf,PSTR("0K\n\r"));
				softser_print(softSer,buf);
			}
		}
#endif

	} // end of while(1)
	return 0;
}
Esempio n. 8
0
void
checkForAdminMsg ()
{
  if (Serial.peek () == startAdminChar)
    {
      timeBeforeScript = millis ();
      Serial.read ();		//DROP ADMIN START CHAR                                       
      while (!Serial.available () > 0)
	{
	  delay (10);
	}
      ackToken = Serial.read ();
      if (ackToken == 'g' || ackToken == 'p')
	{
	  if (ackToken == 'g')
	    {
	      printScriptFromEEPROM ();
	    }
	  else if (ackToken == 'p')
	    {
	      while (!Serial.available () > 0)
		{
		  delay (10);
		}
	      ackToken = Serial.read ();
	      Serial.print ("ack");
	      Serial.println (ackToken);
	    }
	  else
	    {
	      // error
	    }
	}
      else
	{
	  while (!Serial.available () > 0)
	    {
	      delay (10);
	    }
	  if (Serial.read () == startBAdminChar)
	    {
	      parsingAdmin = true;
	      eepromPreviousIndex = eepromIndex;
	      eepromIndex++;
	      firstAdd = true;
	      while (parsingAdmin)
		{
		  if (Serial.available () > 0 && serialIndex < BUFFERSIZE)
		    {
		      inBytes[serialIndex] = Serial.read ();
		      if (inBytes[serialIndex] == sepAdminChar)
			{
			  inBytes[serialIndex] = '\0';
			  //saveScriptCommand();
			  if (!firstAdd)
			    {
			      save2Memory (sepAdminChar);
			    }
			  parseAndSaveAdminMsg ();
			  flushAdminBuffer ();
			  firstAdd = false;
			}
		      else
			{
			  if (inBytes[serialIndex] == endAdminChar)
			    {
			      parsingAdmin = false;
			      inBytes[serialIndex] = '\0';
			      //saveScriptCommand();

			      if (!firstAdd)
				{
				  save2Memory (sepAdminChar);
				}
			      parseAndSaveAdminMsg ();
			      flushAdminBuffer ();
			      save2MemoryNoInc (eepromIndex, endAdminChar);
			      eepromIndex = eepromPreviousIndex + 1;
			      executeScriptFromEEPROM ();

			      save2MemoryNoInc (eepromPreviousIndex, sepAdminChar);	//CLOSE TRANSACTION
			      //COMPRESS EEPROM IF NECESSARY , DON'T GO TO LIMIT
			      if (eepromIndex > (EEPROM_MAX_SIZE - 100))
				{	//TODO REMOVE MAGIC NUMBER !!!
				  compressEEPROM ();
				}
			      kprint ("ms");
			      Serial.println (millis () - timeBeforeScript);
			      kprint ("mem");
			      Serial.println (freeRam ());
			      kprint ("emem");
			      Serial.println (eepromIndex);
			      kprint ("ack");
			      Serial.println (ackToken);
			      firstAdd = false;
			    }
			  else
			    {
			      serialIndex++;
			    }
			}
		    }
		  if (serialIndex >= BUFFERSIZE)
		    {
		      kprintln ("BFO");
		      flushAdminBuffer ();
		      Serial.flush ();
		      parsingAdmin = false;	//KILL PARSING ADMIN
		    }
		}

	    }
	  else
	    {
	      kprintln ("BAM");
	      flushAdminBuffer ();
	      Serial.flush ();
	    }

	}
    }
  else
    {
      processUserMessage ();
    }


}
Esempio n. 9
0
void printRam() 
{
  NewSerial.print(F(" RAM:"));
  NewSerial.println(freeRam());
}
Esempio n. 10
0
int main(void) {
	int16_t command = -1;
	uint16_t freemem;
	
	// set for 8 MHz clock because of 3.3v regulator
	CPU_PRESCALE(1);
	
	// set for 16 MHz clock
	//CPU_PRESCALE(0);

	//disable JTAG
	MCUCR = (1<<JTD) | (1<<IVCE) | (0<<PUD);
	MCUCR = (1<<JTD) | (0<<IVSEL) | (0<<IVCE) | (0<<PUD);

	// set all i/o lines to input
	releaseports();

	//Init SPI
	SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);
	hwspi_init();
	
	// Initialize the USB, and then wait for the host to set configuration.
	// If the Teensy is powered without a PC connected to the USB port,
	// this will wait forever.
	usb_init();

	while (!usb_configured()) /* wait */ ;

	// Wait an extra second for the PC's operating system to load drivers
	// and do whatever it does to actually be ready for input
	_delay_ms(1000);

	while (1) {
		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();

		while (usb_configured()) { // is user still connected?
			command = usb_serial_getchar();
			if (command == -1) continue;

			switch (command) {
			case CMD_PING1:
				usb_serial_putchar(VERSION_MAJOR);
				break;
				
			case CMD_PING2:
				freemem = freeRam();
				usb_serial_putchar(VERSION_MINOR);
				usb_serial_putchar((freemem >> 8) & 0xFF);
				usb_serial_putchar(freemem & 0xFF);
				break;
				
			case CMD_BOOTLOADER:
				bootloader();
				break;
				
			case CMD_IO_LOCK:
				break;
				
			case CMD_IO_RELEASE:
				break;
				
			case CMD_PULLUPS_DISABLE:
				IO_PULLUPS = 0;
				break;
				
			case CMD_PULLUPS_ENABLE:
				IO_PULLUPS = 0xFF;
				break;
				
			case CMD_SPI_ID:
				handle_read_id();
				break;
				
			case CMD_SPI_READBLOCK:
				handle_read_block();
				break;
				
			case CMD_SPI_WRITESECTOR:
				handle_write_block();
				break;
				
			case CMD_SPI_ERASEBLOCK:
				handle_erase_block();
				break;
				
			case CMD_SPI_ERASECHIP:
				handle_erase_chip();
				break;
				
			case CMD_SPI_3BYTE_ADDRESS:
				SPI_ADDRESS_LENGTH = 3;
				break;
			
			case CMD_SPI_4BYTE_ADDRESS:
				SPI_ADDRESS_LENGTH = 4;
				break;
			
			case CMD_SPI_3BYTE_CMDS:
				SPI_USE_3B_CMDS = 1;
				break;
			
			case CMD_SPI_4BYTE_CMDS:
				SPI_USE_3B_CMDS = 0;
				break;
			
			default:
				break;
			}
		}		
	}
}
Esempio n. 11
0
void show_freeram()
{
  char rammsg[16];
  sprintf(rammsg, "RAM:%dB", freeRam());
  show_english(10, 99 + 3 * 16, rammsg, 0, 0, 0, 1);
}