Ejemplo n.º 1
0
// convert 96 bit HID FSK data to 8 digit BCD UID
BOOL hid26_hex_to_uid(unsigned char *response, unsigned char *hid26)
{
    BYTE tmp[96], tmp1[7];
    unsigned int site;
    unsigned int id;
    // todo parity checking etc.

    if(!hextobinarray(tmp, hid26))
        return FALSE;
    
    // skip 44 bit header
    memcpy(tmp, tmp + 44, 52);
    
    // manchester decode
    if(manchester_decode(tmp, tmp, 52) != 26)
        return FALSE;

    // convert to hex, ignoring parity bits
    if(!binarraytohex(tmp1, tmp + 1, 24))
        return FALSE;
    // convert hex to site/id
    sscanf(tmp1,"%2X%4X", &site, &id);
    // final output 8 byte BCD
    sprintf(response,"%03d%05d", site, id);

    return TRUE;
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: Smitc114/3010
void main(void) {
	BRD_init();	//Initalise NP2
	Hardware_init();
	HAL_Delay(3000);
	struct PanTilt pantiltvars;
	pantilt = &pantiltvars;
	pantilt->write_angles = 0;
	pantilt->read_angles = 0;
	pantilt->set_angle_pan = 0;
	pantilt->set_angle_tilt = 0;
	struct Variables variables;
	vars = &variables;
	vars->count = 0;
	vars->bit_half = 1;
	vars->bit_count = 0;
	vars->encoded_bit_count = -1;
	vars->encoded_bit = 0;
	vars->encoded_char = hamming_byte_encoder('<');
	vars->transmit_frequency = 1000;
	vars->period_multiplyer = ((1.000000/vars->transmit_frequency)*500)*0.998;
	vars->recieve_element = 0;
	vars->recieve_flag = 0;
	Pb_init();
	s4353096_radio_setchan(s4353096_chan);
	s4353096_radio_settxaddress(s4353096_tx_addr);
	s4353096_radio_setrxaddress(s4353096_rx_addr);
  while (1) {
		s4353096_radio_setfsmrx();
		s4353096_radio_fsmprocessing();
		if (vars->recieve_flag == 1) {
			manchester_decode();
			vars->recieve_flag = 0;
		}
		if (mode == S4353096_RADIO) {
		/*Processes the current fsm state*/
		RxChar = debug_getc();
		if (RxChar != '\0') {
			s4353096_keystroke = 1;
			while(s4353096_keystroke == 1){
				if (RxChar == '\r' || s4353096_payload_length == 7) {
					for (int j = s4353096_payload_length; j < 7; j++) {
						s4353096_payload_buffer[j] = '-';
					}
					s4353096_radio_fsmcurrentstate = S4353096_IDLE_STATE;
					s4353096_radio_fsmprocessing();
					s4353096_radio_fsmcurrentstate = S4353096_TX_STATE;
					debug_printf("\n");
					/*Compiles the transmit packet. Transmits packet if in TX state*/
					s4353096_radio_sendpacket(s4353096_radio_getchan(), s4353096_addr_get, s4353096_payload_buffer);
					s4353096_radio_fsmprocessing();
					s4353096_radio_setfsmrx();
					s4353096_radio_fsmprocessing();
					s4353096_payload_length = 0;
					s4353096_keystroke = 0;
				} else if (RxChar != '\0') {
					s4353096_payload_buffer[s4353096_payload_length] = RxChar;
					s4353096_payload_length++;
					debug_putc(RxChar);				//reflect byte using putc - puts character into buffer
					debug_flush();					//Must call flush, to send character		//reflect byte using printf - must delay before calling printf again.
				} else {

				}
				HAL_Delay(125);
				RxChar = debug_getc();
			}
		} else {

		}
		s4353096_radio_fsmprocessing();
		if (s4353096_radio_getrxstatus() == 1) { //Checks if packet has been recieved
				/*Prints recieved packet to console*/
			s4353096_radio_getpacket(s4353096_rx_buffer);
		} else {

		}
	}
		if (pantilt->write_angles == 1) {
			s4353096_pantilt_angle_write(1, pantilt->set_angle_pan);
			s4353096_pantilt_angle_write(0, pantilt->set_angle_tilt);
			pantilt->write_angles = 0;
		}
		if (((HAL_GetTick()/10000) % 100) == 0) {
			debug_printf("\nPan: %d Tlit: %d\n", pantilt->set_angle_pan, pantilt->set_angle_tilt);
		}
		if (pantilt->read_angles == 1) { /*Delay for 1 second or setup to delay for 0.2 seconds and set angle to += or -= 1 each time*/
			if (mode == S4353096_JOYSTICK) {
				y_value = s4353096_joystick_y_read();
				if ((y_value > 2500) && (pantilt->set_angle_pan < 76)) {
					pantilt->set_angle_pan += 1;
				} else if ((y_value < 1550) && (pantilt->set_angle_pan > -76)) {
					pantilt->set_angle_pan -= 1;
				} else { //Joystick is stationary, no input
				}
				x_value = s4353096_joystick_x_read();
				if ((x_value > 2500) && (pantilt->set_angle_tilt < 76)) {
					pantilt->set_angle_tilt += 1;
				} else if ((x_value < 1550) && (pantilt->set_angle_tilt > -76)) {
					pantilt->set_angle_tilt -= 1;
				} else {
				}
			} else if (mode == S4353096_TERMINAL) {
				/* Receive characters using getc */
				RxChar = debug_getc();
				/* Check if character is not Null */
				if (RxChar != '\0') {
					switch (RxChar) {
						case 'w':
							pantilt->set_angle_tilt += 1;
							break;
						case 's':
							pantilt->set_angle_tilt -= 1;
							break;
						case 'a':
							pantilt->set_angle_pan += 1;
							break;
						case 'd':
							pantilt->set_angle_pan -= 1;
							break;
						case 't':
							mode = S4353096_LASER_TRANSMIT;
							break;
						default:
							break;
					}
					debug_flush();
				}
				s4353096_terminal_angle_check();

			}
			pantilt->read_angles = 0;
		}
	}
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
	fd_set rfds, wfds, efds;
	struct timeval tv;
	int i;
	int tun_fd = -1, tty_fd = -1, serialPortFd = -1;
	int nread, nwrite, pid, retVal, nfds = 0;
	int sync_counter = 0;
	int sync_passed = 0;
	long baud;
	long tx_message_interval;
	long tx_predelay;
	long tx_postdelay;
	long max_allowed_tx_interval;
	unsigned char read_buffer[8192];
	unsigned char extraction_buffer[8192];
	unsigned int extraction_index = 0;
	unsigned char* manchester_buffer = NULL;
	unsigned int manchester_buffer_size;
	unsigned char* ax25_buffer = NULL;
	unsigned int ax25_buffer_size;
	unsigned int frame_length;
	char serial_device[64];
	char tun_name[IFNAMSIZ];
	char subnet[24];
	char ax25_destination[7];
	time_t last_transmission_time;
	time_t last_reception_time;

	if(argc < 2) {
		usage();
		exit(0);
	}

	if(!strcmp("uhf", argv[1]))
	{
		openLogFile(UHF_LOGFILE_NAME);
		baud = RADIOTUNNEL_BIM2A_DEFAULT_BAUD_RATE;
		tx_predelay = RADIOTUNNEL_BIM2A_TX_PREDELAY;
		tx_postdelay = RADIOTUNNEL_BIM2A_TX_POSTDELAY;
		max_allowed_tx_interval = RADIOTUNNEL_BIM2A_MAX_ALLOWED_TRANSMISSION_TIME;
		tx_message_interval = RADIOTUNNEL_BIM2A_TX_MESSAGE_INTERVAL;
	}
	else if(!strcmp("vhf", argv[1]))
	{
		openLogFile(VHF_LOGFILE_NAME);
		baud = RADIOTUNNEL_UHX1_DEFAULT_BAUD_RATE;
		tx_predelay = RADIOTUNNEL_UHX1_TX_PREDELAY;
		tx_postdelay = RADIOTUNNEL_UHX1_TX_POSTDELAY;
		max_allowed_tx_interval = RADIOTUNNEL_UHX1_MAX_ALLOWED_TRANSMISSION_TIME;
		tx_message_interval = RADIOTUNNEL_UHX1_TX_MESSAGE_INTERVAL;
	}
	else
	{
		usage();
		printf("\nwrong invocation!\n");
		exit(0);
	}


	if (argc < 3)
		strcpy(tun_name, IF_NAME);
	else
		strcpy(tun_name, argv[2]);

	if(argc < 4)
		strcpy(subnet, IF_SUBNET);
	else
		strcpy(subnet, argv[3]);

	if (argc < 5)
		strcpy(my_ax25_callsign, nocall_callsign);
	else
		strcpy(my_ax25_callsign, argv[4]);
		my_ax25_callsign[6]=atoi(argv[4]+6);

	if (argc < 6)
		strcpy(serial_device, "/dev/ttyUSB0");
	else
		strcpy(serial_device, argv[5]);

	if(argc < 7)
		ax25_get_broadcast_callsign(ax25_destination);
	else
	{
		strncpy(ax25_destination, argv[6], 6);
		ax25_destination[6]=atoi(argv[6]+6);
	}

	serialPortFd = serial_openSerialPort(serial_device, baud, tx_predelay, tx_postdelay);
	if (serialPortFd < 0)
	{
		perror("serial_openSerialPort()");
		exit(EXIT_FAILURE);
	}
	else
	{
		printf("Opened serial device \'%s\'\n", serial_device);
	}

	tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI);
	if (tun_fd < 0)
	{
		perror("tun_alloc()");
		exit(EXIT_FAILURE);
	}
	else
	{
		printf("Interface \'%s\' allocated\n", tun_name);
	}

	tty_fd = open("/dev/tty", O_RDONLY | O_NOCTTY);
	if (tty_fd < 0)
	{
		perror("open tty");
		exit(EXIT_FAILURE);
	}
	else
	{
		printf("/dev/tty opened\n");
	}

	pid = fork();
	if (pid == 0) //if child process
	{
		printf("Starting the interface \'%s\' with address and subnet %s\n", tun_name, subnet);
		if (execlp("ifconfig", " ", tun_name, subnet, "mtu", IF_MTU, "txqueuelen", IF_QUEUE_LENGTH "up", NULL))
			perror("interface up");
		exit(EXIT_FAILURE);
	}

	ax25_initialize_network(my_ax25_callsign);
	time(&last_transmission_time);
	time(&last_reception_time);
	serial_setRTS(0);

	while (1)
	{
		FD_ZERO(&rfds);
		FD_ZERO(&wfds);
		FD_ZERO(&efds);
		FD_SET(serialPortFd, &rfds);
		FD_SET(tun_fd, &rfds);
		FD_SET(tty_fd, &rfds);
		nfds = max(nfds, serialPortFd);
		nfds = max(nfds, tun_fd);
		nfds = max(nfds, tty_fd);
		tv.tv_sec = 5;
		tv.tv_usec = 0;
		retVal = select(nfds + 1, &rfds, &wfds, &efds, NULL/*&tv*/);
		if (retVal < 0 && errno == EINTR)
		{
			continue;
		}
		if (retVal < 0)
		{
			perror("select()");
			exit(EXIT_FAILURE);
		}
		//TTY INTERFACE
		if (FD_ISSET(tty_fd, &rfds))
		{
			nread = read(tty_fd, read_buffer, sizeof(read_buffer));
			printf("Read %d bytes from device fd=%d\n", nread, tty_fd);
			for (i = 0; i < nread; i++)
			{
				if (read_buffer[i] == 0x1b) /*esc*/
					exit(EXIT_SUCCESS);
				putchar(read_buffer[i]);
			}
		}
		//SERIAL PORT
		if (FD_ISSET(serialPortFd, &rfds))
		{
			if ((nread = read(serialPortFd, read_buffer, sizeof(read_buffer))) > 0)
			{
				//printf("# of bytes read = %d\n", nread);
				for (i = 0; i < nread; i++)
				{
					//printf("0x%02x\n",read_buffer[i]);
					if (sync_counter < SYNC_LENGTH && read_buffer[i] == syncword[sync_counter])
					{
						sync_counter++; /* sync continued */
						//printf("sync counting %d\n", sync_counter);
					}
					else
					{
						//printf("sync reset %d -> 0x%02x 0x%02x 0x%02x 0x%02x\n", sync_counter, syncword[0], syncword[1], syncword[2], syncword[3]);
						sync_counter = 0; /* not a preamble, reset counter */
					}
					if (sync_counter >= SYNC_LENGTH && sync_passed == 0)
					{
						sync_passed = 1;
						//printf("sync_passed\n");
					}
					else if (sync_passed)
					{
						time(&last_reception_time);
						//printf("getting data '%c'\n", io[i]);
						if (read_buffer[i] == END_OF_FILE && !isManchester_encoded(read_buffer[i]))
						{
//							printf("non-manchester character received\n");
							manchester_buffer = malloc((extraction_index/2)+1);
							if(manchester_buffer == NULL)
							{
								perror("manchester_buffer malloc()");
							}
							frame_length = manchester_decode(extraction_buffer, manchester_buffer, extraction_index);
//							printf("MANCHESTER DECODED\n");
//							printAsciiHex(manchester_buffer, frame_length);
							ax25_buffer = malloc(frame_length+1);
							if(ax25_buffer == NULL)
							{
								if(manchester_buffer)
								{
									free(manchester_buffer);
									manchester_buffer=NULL;
								}
								perror("ax25_buffer malloc()");
							}
							frame_length = ax25_open_ui_packet(NULL, NULL, ax25_buffer, manchester_buffer, frame_length);
							if (frame_length)
							{
								printf("Read %d bytes from device %s\n", frame_length, serial_device);
								printAsciiHex(ax25_buffer, frame_length);
								registerEvent("RX","");
								nwrite = write(tun_fd, ax25_buffer, frame_length);
								if(nwrite < 0)
								{
									perror("error writing to tun device");
								}
							}
							else
							{
								printf("!ax.25 discarded!\n");
								registerEvent("Discard","RX");
							}
							if(ax25_buffer)
							{
								free(ax25_buffer);
								ax25_buffer = NULL;
							}
							if(manchester_buffer)
							{
								free(manchester_buffer);
								manchester_buffer = NULL;
							}
							sync_passed = 0;
							sync_counter = 0;
							extraction_index = 0;
						}
						else
						{
							//printf("saved data '%c'\n", io[i]);
//							printf("save_index=%d/%d\n",save_index, sizeof(buf));
							if (extraction_index >= sizeof(extraction_buffer))
							{
								sync_passed = 0;
								sync_counter = 0;
								extraction_index = 0;
							}
							else
							{
								extraction_buffer[extraction_index++] = read_buffer[i];
								extraction_buffer[extraction_index + 1] = 0;
							}
							//printf("-\n%s\n-\n", buf);
						}
					}
				}
			}
		}
		//TUNTAP DEVICE
		if (FD_ISSET(tun_fd, &rfds))
		{
			nread = read(tun_fd, read_buffer, sizeof(read_buffer));
			if (nread < 0)
			{
				perror("Reading from if interface");
				close(tun_fd);
				exit(EXIT_FAILURE);
			}

			printf("Read %d bytes from device %s\n", nread, tun_name);
			printAsciiHex(read_buffer, nread);

			/*
			 * ping modifier for 10.0.0.4
			 * simply swaps the last bytes of the ping packet's source and destination ips
			 * and writes it back by setting the ICMP type 0
			 */
#if 0
			printf("--------------PING MODIFIER/RESPONDER ACTIVE---------------");
			for (i = 0; i < MODIFY_LIST_LENGTH; i++)
			{
				if (!memcmp(read_buffer + 16, modify[i], 4)) //ip match
				{
					if (read_buffer[9] == 1 && read_buffer[1] == 0)
					{
						if (read_buffer[20] == 8 && read_buffer[21] == 0)
						{
							read_buffer[nread] = read_buffer[15];
							read_buffer[12] = 10; //src
							read_buffer[13] = 0; //src
							read_buffer[14] = 1; //src
							read_buffer[15] = 2; //src
							read_buffer[16] = 10; //dst
							read_buffer[17] = 0; //dst
							read_buffer[18] = 1; //dst
							read_buffer[19] = 1; //dst
//							read_buffer[20] = 0;
							write(tun_fd, read_buffer, nread);
						}
					}
				}
			}
#endif
			ax25_buffer_size = AX25_TOTAL_HEADERS_LENGTH + nread + 1; //+1 for safety
			manchester_buffer_size = (ax25_buffer_size * 2) + PREAMBLE_LENGTH + SYNC_LENGTH + 2; //+2 for EOF and NULL
			ax25_buffer = malloc(ax25_buffer_size);
			if (ax25_buffer == NULL)
			{
				perror("ax25 malloc()");
			}
			manchester_buffer = malloc(manchester_buffer_size);
			if (manchester_buffer == NULL)
			{
				perror("manchester malloc()");
			}
			//encapsulate the ip packet
			frame_length = ax25_create_ui_packet(my_ax25_callsign, ax25_destination, read_buffer, nread, ax25_buffer);
			if (frame_length == 0)
			{
				printf("couldn't prepare ax25 frame");
			}
//			printf("AX25 FRAMED\n");
//			printAsciiHex(ax25_buffer, frame_length);
			//manchester encode the ax25 frame
			frame_length = manchester_encode(ax25_buffer, manchester_buffer, frame_length);
			if (frame_length == 0)
			{
				printf("couldn't manchester encode the package");
			}
			//check for last wireless activity
			if(time(NULL) <= last_transmission_time+max_allowed_tx_interval)
			{
//				sleep(1);
				registerEvent("Discard","TX");
				printf("early transmission discard\n");
			}
			else
			{
				if(time(NULL) <= last_reception_time)
				{
					usleep(tx_message_interval);
				}
				registerEvent("TX","");
				retVal=serial_transmitCapsulatedData(manchester_buffer, frame_length);
				time(&last_transmission_time);
				if(retVal)
				{
					printf("error writing to serial port device\n");
				}
			}
			if(ax25_buffer)
			{
				free(ax25_buffer);
				ax25_buffer = NULL;
			}
			if(manchester_buffer)
			{
				free(manchester_buffer);
				manchester_buffer = NULL;
			}
			/*
			 * ping responder for 10.0.0.2
			 * simply swaps the last bytes of the ping packet's source and destination ips
			 * and writes it back by setting the ICMP type 0
			 */
#if 0		
			printf("--------------PING MODIFIER/RESPONDER ACTIVE---------------");
			for (i = 0; i < RESPOND_LIST_LENGTH; i++)
			{
				if (!memcmp(read_buffer + 16, respond[i], 4)) //ip match
				{
					if (read_buffer[9] == 1 && read_buffer[1] == 0)
					{
						if (read_buffer[20] == 8 && read_buffer[21] == 0)
						{
							read_buffer[nread] = read_buffer[15];
							read_buffer[15] = read_buffer[19];
							read_buffer[19] = read_buffer[nread];
							read_buffer[20] = 0;
							write(tun_fd, read_buffer, nread);
						}
					}
				}
			}
#endif
		}
	}
	return 0;
}
Ejemplo n.º 4
0
int main(int ac, char *av[])
{
	//TODO extract the serial port codes into another source file, this is too ugly!
	uint16_t i, j, len;
	int16_t res=0, result=0;
	uint8_t destination_ip[32];
	uint8_t outbuf[512];
	uint8_t linebuf[32];
	uint8_t local_filename[32]="\0";
	FILE* sptr;

	if(ac == 1)
		usage();

	//for(i=0; i<ac; i++)
	//	printf("%s\n",av[i]);

	if(!strcmp("uhf", av[1]))
	{
		mode=RADIOTFTP_MODE_UHF;
		preamble_length=RADIOTFTP_BIM2A_PREAMBLE_LENGTH;
		baud=RADIOTFTP_BIM2A_BAUD_RATE;
		printf("Running with UHF band 19200 baud version\n");
		openLogFile(RADIOTFTP_BIM2A_EVENTLOG);
	}
	else if(!strcmp("vhf", av[1]))
	{
		mode=RADIOTFTP_MODE_VHF;
		preamble_length=RADIOTFTP_UHX1_PREAMBLE_LENGTH;
		baud=RADIOTFTP_UHX1_BAUD_RATE;
		printf("Running with VHF band 2400 baud version\n");
		openLogFile(RADIOTFTP_UHX1_EVENTLOG);
	}
	else if(!strcmp("serial", av[1]))
	{
		mode=RADIOTFTP_MODE_SERIAL;
		preamble_length=RADIOTFTP_SERIAL_PREAMBLE_LENGTH;
		baud=RADIOTFTP_SERIAL_BAUD_RATE;
		printf("Couldn't understand the uhf/vhf mode: using %d mode\n", mode);
		printf("Running with NO BAND %d baud version\n", baud);
		printf("Opening file %s for logging\n",RADIOTFTP_SERIAL_EVENTLOG);
		openLogFile(RADIOTFTP_SERIAL_EVENTLOG);
	}
	else
	{
		printf("Mode not entered, exiting...\n");
		return 0;
	}

	//setting defaults
	udp_get_broadcast_ip(destination_ip);
	strcpy(dial_tty, "/dev/ttyUSB0");

	//scanning command line parameters
	for(i=2; (i < ac) && (av[i][0] == '-'); i++)
	{
		if(strcmp(av[i], "-b") == 0)
		{
			background=1;
		}
		else if(strncmp(av[i], "-f", 2) == 0)
		{
			strncpy(local_filename, av[i] + 2, 32);
			printf("different filename = '%s'\n", local_filename);
		}
		else if(strncmp(av[i], "-dst", 4) == 0)
		{
			memset(destination_ip, 0, 32);
			memcpy(destination_ip, av[i] + 4, strlen(av[i]) - 4);
			/*! convert text ip to numerical */
			text_to_ip(destination_ip, strlen(av[i]) - 4 + 1);
			printf("Destination: ");
			print_addr_dec(destination_ip);
		}
		else
		{
			printf("unknown parameter: %s\n", av[i]);
			usage();
		}
	}
	res=0;
	result=0;

	strncpy(dial_tty, devtag_get(av[i]), sizeof(dial_tty));

	while(!get_lock(dial_tty))
	{
		if(decrementLockRetries() == 0)
			exit(-1);
		sleep(1);
	}

	if((serialportFd=open(devtag_get(av[i]), O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
	{
		perror("bad terminal device, try another");
		exit(-1);
	}
	signal(SIGIO, SIG_IGN);
	signal(SIGINT, sigINT_handler);
	// Make the file descriptor asynchronous (the manual page says only
	// O_APPEND and O_NONBLOCK, will work with F_SETFL...)
	fcntl(serialportFd, F_GETFL);
	fcntl(serialportFd, F_SETFL, O_RDWR | O_SYNC);

	if(tcgetattr(serialportFd, &tp) < 0)
	{
		perror("Couldn't get term attributes");
		exit(-1);
	}
	old=tp;

	/*
	 SANE is a composite flag that sets the following parameters from termio(M):

	 CREAD BRKINT IGNPAR ISTRIP ICRNL IXON ISIG ICANON
	 ECHO ECHOK OPOST ONLCR

	 SANE also clears the following modes:

	 CLOCAL
	 IGNBRK PARMRK INPCK INLCR IUCLC IXOFF
	 XCASE ECHOE ECHONL NOFLSH
	 OLCUC OCRNL ONOCR ONLRET OFILL OFDEL NLDLY CRDLY
	 TABDLY BSDLY VTDLY FFDLY

	 */
	/* 8 bits + baud rate + local control */
	tp.c_cflag=baud | CS8 | CLOCAL | CREAD;
	tp.c_cflag&=~PARENB;
	tp.c_cflag&=~CSTOPB;
	tp.c_cflag&=~CSIZE;
	tp.c_cflag|=CS8;
	tp.c_cflag|=CRTSCTS;
	tp.c_oflag=0; /* Raw Input */
	tp.c_lflag=0; /* No conoical */
	tp.c_cc[VTIME]=0;
	tp.c_cc[VMIN]=1;

	/* ignore CR, ignore parity */
	//ISTRIP is a dangerous flag, it strips the 8th bit of bytes
	//tp.c_iflag= ~( ISTRIP ) | IGNPAR | IGNBRK;
	tp.c_iflag=~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IUCLC | IXOFF) | BRKINT | IGNPAR | ICRNL | IXON | ISIG | ICANON;

	/* set output and input baud rates */

	cfsetospeed(&tp, baud);
	cfsetispeed(&tp, baud);

	tcflush(serialportFd, TCIFLUSH);
	tcflush(serialportFd, TCOFLUSH);

	if(tcsetattr(serialportFd, TCSANOW, &tp) < 0)
	{
		perror("Couldn't set term attributes");
		goto error;
	}

	restore=1;

	//TODO go over the background codes
	if(background)
	{
		int i;
		if(getppid() == 1)
			return 0; /* Already a daemon */

		i=fork();

		if(i < 0)
			exit(1); /* error */

		if(i > 0)
			_exit(0); /* parent exits */

		/* child */

		setsid(); /* obtain a new process group */
		for(i=getdtablesize(); i >= 0; --i)
		{
			if(i == serialportFd)
				continue;
			if(i == 1)
				continue;
			close(i); /* close all descriptors */
		}

		i=open("/dev/null", O_RDWR);
		dup(i);
		dup(i); /* handle standard I/O */
		umask(027); /* set newly created file permissions */
		chdir("/"); /* change running directory */

	}

	srand((unsigned) time(NULL));

	j=0;
	i++;
	for(; i < ac; i++)
	{
		len=strlen(av[i]);
		memcpy(command_buffer + j, av[i], len);
		j+=len;
		if(i != ac - 1)
		{
			command_buffer[j++]=' ';
		}
	}

	//process the escape characters
	for(i=0; i < j; i++)
	{
		if(command_buffer[i] == '\\')
		{
			if(i + 1 < j)
			{
				if(command_buffer[i + 1] == 'n')
				{
					command_buffer[i]='\r';
					command_buffer[i + 1]='\n';
				}
			}
		}
	}

	timers_initialize(&sigRTALRM_handler);

	/*! read settings from radiotftp.conf file */
	sptr=fopen("radiotftp.conf", "r");
	if(sptr != NULL)
	{
		readnline(sptr, linebuf, 32);
		linebuf[6]=atoi(linebuf + 6);
		linebuf[8]=0;
		printf("AX.25 Callsign: ");
		print_callsign(linebuf);
#if AX25_ENABLED==1
		ax25_initialize_network(linebuf);
		printf("USING AX25 LINK LAYER!!!\n");
#else AX25_ENABLED==0
		printf("NOT USING ANY LINK LAYER!!!\n");
#endif
		readnline(sptr, linebuf, 32);
		text_to_ip(linebuf, strlen(linebuf));
		printf("IPv4 Address: ");
		print_addr_dec(linebuf);
		udp_initialize_ip_network(linebuf, &queueSerialData);
	}
	else
	{
#if AX25_ENABLED==1
		ax25_initialize_network(my_ax25_callsign);
		printf("AX.25 CALLSIGN = ");
		print_callsign(ax25_get_local_callsign(NULL));
#endif
		udp_initialize_ip_network(my_ip_address, &queueSerialData);
		printf("IPv4 Address = ");
		print_addr_dec(udp_get_localhost_ip(NULL));
	}

	switch(mode)
	{
		case RADIOTFTP_MODE_UHF:
			tftp_initialize(udp_get_data_queuer_fptr(), RADIOTFTP_BIM2A_ACK_TIMEOUT_MIN, RADIOTFTP_BIM2A_ACK_TIMEOUT_MAX, RADIOTFTP_BIM2A_READ_TIMEOUT);
			break;
		case RADIOTFTP_MODE_VHF:
			tftp_initialize(udp_get_data_queuer_fptr(), RADIOTFTP_UHX1_ACK_TIMEOUT_MIN, RADIOTFTP_UHX1_ACK_TIMEOUT_MAX, RADIOTFTP_UHX1_READ_TIMEOUT);
			break;
		case RADIOTFTP_MODE_SERIAL:
			tftp_initialize(udp_get_data_queuer_fptr(), RADIOTFTP_SERIAL_ACK_TIMEOUT_MIN, RADIOTFTP_SERIAL_ACK_TIMEOUT_MAX, RADIOTFTP_SERIAL_READ_TIMEOUT);
			break;
		default:
			perror("unknown baud rate");
			safe_exit(-1);
			break;
	}

	if(!strncasecmp(RADIOTFTP_COMMAND_PUT, command_buffer, strlen(RADIOTFTP_COMMAND_PUT)))
	{
		printf(RADIOTFTP_COMMAND_PUT"\n");
		registerEvent(RADIOTFTP_COMMAND_PUT, command_buffer + strlen(RADIOTFTP_COMMAND_PUT) + 1);
		if((res=tftp_sendRequest(TFTP_OPCODE_WRQ, destination_ip, local_filename, command_buffer + strlen(RADIOTFTP_COMMAND_PUT) + 1, j - strlen(RADIOTFTP_COMMAND_PUT) - 1, 0)))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}
	else if(!strncasecmp(RADIOTFTP_COMMAND_BEACON, command_buffer, strlen(RADIOTFTP_COMMAND_BEACON)))
	{
		printf(RADIOTFTP_COMMAND_BEACON"\n");
		registerEvent(RADIOTFTP_COMMAND_BEACON, command_buffer + strlen(RADIOTFTP_COMMAND_BEACON) + 1);
		if((res=tftp_sendSingleBlockData(destination_ip, command_buffer + strlen(RADIOTFTP_COMMAND_BEACON) + 1, j - strlen(RADIOTFTP_COMMAND_BEACON) - 1, local_filename)))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}
	else if(!strncasecmp(RADIOTFTP_COMMAND_APPEND_LINE, command_buffer, strlen(RADIOTFTP_COMMAND_APPEND_LINE)))
	{
		printf(RADIOTFTP_COMMAND_APPEND_LINE"\n");
		registerEvent(RADIOTFTP_COMMAND_APPEND_LINE, command_buffer + strlen(RADIOTFTP_COMMAND_APPEND_LINE) + 1);
		i=createTempFile(TEMPFILE_PREFIX, TEMPFILE_POSTFIX);
		if(tempFile == NULL)
		{
			perror("couldn't create temp file to store the line feed");
			goto error;
		}
		fwrite(command_buffer + strlen(RADIOTFTP_COMMAND_APPEND_LINE) + 1, 1, j - strlen(RADIOTFTP_COMMAND_APPEND_LINE) - 1, tempFile);
		fflush(tempFile);
		fclose(tempFile);
		printf("tempfileName=%s\nlocal_filename=%s\n", tempFileName, local_filename);
		if((res=tftp_sendRequest(TFTP_OPCODE_WRQ, destination_ip, tempFileName, local_filename, strlen(local_filename), 1)))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}
	else if(!strncasecmp(RADIOTFTP_COMMAND_APPEND_FILE, command_buffer, strlen(RADIOTFTP_COMMAND_APPEND_FILE)))
	{
		printf(RADIOTFTP_COMMAND_APPEND_FILE"\n");
		registerEvent(RADIOTFTP_COMMAND_APPEND_FILE, command_buffer + strlen(RADIOTFTP_COMMAND_APPEND_FILE) + 1);
		if((res=tftp_sendRequest(TFTP_OPCODE_WRQ, destination_ip, local_filename, command_buffer + strlen(RADIOTFTP_COMMAND_APPEND_FILE) + 1, j - strlen(RADIOTFTP_COMMAND_APPEND_FILE) - 1, 1)))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}
	else if(!strncasecmp(RADIOTFTP_COMMAND_GET, command_buffer, strlen(RADIOTFTP_COMMAND_GET)))
	{
		printf(RADIOTFTP_COMMAND_GET"\n");
		registerEvent(RADIOTFTP_COMMAND_GET, command_buffer + strlen(RADIOTFTP_COMMAND_GET) + 1);
		if((res=tftp_sendRequest(TFTP_OPCODE_RRQ, destination_ip, local_filename, command_buffer + strlen(RADIOTFTP_COMMAND_GET) + 1, j - strlen(RADIOTFTP_COMMAND_GET) - 1, 0)))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}
	else
	{
		printf("hello radio world!\n");
		registerEvent("helloWorld", "");
		if((res=queueSerialData(udp_get_localhost_ip(NULL), HELLO_WORLD_PORT, udp_get_broadcast_ip(NULL), HELLO_WORLD_PORT, "hello world\n\0x00", strlen("hello world\n\0x00"))))
		{
			printf("%d\n", res);
			perror("tftp request fail");
			goto error;
		}
	}

	int sync_counter=0;
	int sync_passed=0;
	int save_index=0;

	//entering the main while loop
	printf("started listening...\n");
	while(1)
	{
		if(timer_flag)
		{
			idle_timer_handler();
			tftp_timer_handler();
			timer_flag=0;
		}
		if(idle_flag)
		{
			//print_time("System Idle");
			idle_flag=0;
		}
		if(queue_flag)
		{
//			printf("queue flag\n");
			if(!sync_passed && sync_counter < 1)
			{
				//wait_some
				switch(mode)
				{
					case RADIOTFTP_MODE_UHF:
						usleep(RADIOTFTP_BIM2A_TX_MESSAGE_INTERVAL);
						break;
					case RADIOTFTP_MODE_VHF:
						usleep(RADIOTFTP_UHX1_TX_MESSAGE_INTERVAL);
						break;
					case RADIOTFTP_MODE_SERIAL:
						usleep(RADIOTFTP_SERIAL_TX_MESSAGE_INTERVAL);
						break;
					default:
						perror("unknown baud rate");
						safe_exit(-1);
						break;
				}
				transmitSerialData();
				queue_flag=0;
			}
		}
		if((res=read(serialportFd, io, IO_BUFSIZE)) > 0)
		{
			io_index=0;
			//printf("# of bytes read = %d\n", res);
			for(i=0; i < res; i++)
			{
				//putchar(io[i]);
				//printf("%\n",io[i]);
				if(sync_counter < SYNC_LENGTH && io[i] == syncword[sync_counter])
				{
					sync_counter++; /* sync continued */
					//printf("sync counting %d\n",sync_counter);
				}
				else
				{
					//printf("sync reset %d\n",sync_counter);
					sync_counter=0; /* not a preamble, reset counter */
				}
				if(sync_counter >= SYNC_LENGTH && sync_passed == 0)
				{ /* preamble passed */
					sync_passed=1;
				}
				if(sync_passed)
				{
					//printf("getting data '%c'\n", io[i]);
					if(io[i] == END_OF_FILE && !isManchester_encoded(io[i]))
					{
//							printf("non-manchester character received\n");
						outbuf[0]=0;
						result=manchester_decode(buf + 1, manchester_buffer, save_index);
#if AX25_ENABLED==1
						result=ax25_open_ui_packet(NULL, NULL, ax25_buffer, manchester_buffer, result);
#else
						result=1;
#endif
						if(result)
						{
//								printf("opened link-layer packet\n");
#if AX25_ENABLED==1
							result=udp_open_packet(udp_src, &udp_src_prt, udp_dst, &udp_dst_prt, udp_buffer, ax25_buffer);
#else
							result = udp_open_packet(udp_src, &udp_src_prt, udp_dst, &udp_dst_prt, udp_buffer, manchester_buffer);
#endif
							if(result)
							{
//									printf("opened transport-layer packet\n");
								udp_packet_demultiplexer(udp_src, udp_src_prt, udp_dst, udp_dst_prt, udp_buffer, result);
							}
							else
							{
								strcat(outbuf, "!udp discarded!");
								if(write(1, outbuf, strlen(outbuf)) <= 0)
								{
									fputs("couldn't write to tty\n", stderr);
								}
								if(write(1, "\n", 1) <= 0)
								{
									fputs("couldn't write to tty\n", stderr);
								}
							}
						}
						else
						{
							strcat(outbuf, "!ax25 discarded!");
							if(write(1, outbuf, strlen(outbuf)) <= 0)
							{
								fputs("couldn't write to tty\n", stderr);
							}
							if(write(1, "\n", 1) <= 0)
							{
								fputs("couldn't write to tty\n", stderr);
							}
						}
						sync_passed=0;
						sync_counter=0;
						save_index=0;
					}
					else
					{
//						printf("saved data '%c'\n", io[i]);
//						printf("save_index=%d/%d\n",save_index, sizeof(buf));
						if(save_index >= sizeof(buf))
						{
							sync_passed=0;
							sync_counter=0;
							save_index=0;
						}
						buf[save_index++]=io[i];
						buf[save_index + 1]=0;
//						printf("-\n%s\n-\n", buf);
					}
				}
			}
			io_flag=0;
		}
	}

	safe_exit(0);
	error: safe_exit(-1);

	return -2;
}