#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>

#include "config.h"
#include "client.h"
#include "server.h"


void usage()
{
	fprintf(stderr, "\n                 	   ***********BLUETOOTH MEASUREMENT*********** \n\n");
	fprintf(stderr, "Usage: bt_measurement [-p <protocol>] [-m <mode>] [-i <interface>] [-c <server address>] filename\n");
	fprintf(stderr, "  -p  used protocol\n");
	fprintf(stderr, "        possible value is:\n");
	fprintf(stderr, "        (l2cap)\n");
	fprintf(stderr, "  -m  mode, defined for l2cap only\n");
	fprintf(stderr, "        possible values are: \n");
	fprintf(stderr, "        (basic, streaming)\n");
	fprintf(stderr, "  -i  choose an interface\n");
	fprintf(stderr, "  -c  run as client <server address>\n");
	fprintf(stderr, "  -h  print this help\n");
}


int main(int argc, char ** argv)
{
	char is_client = 0;
	char protocol_flag = 0;
	char mode_flag = 0;
	char filename_flag = 0;
	char debug_flag = 0;
	char * protocol_str = NULL;
	char * mode_str = NULL;
	int index = 0;
	int dev=0;

	bt_intfr_config config = {
		L2CAP, 0, NULL
	};

	int c;
	while ((c = getopt(argc, argv, "dhc:p:m:i:")) != -1)
	{
		switch(c) {
		case 'd':
			debug_flag = 1;
			break;
		case 'h':
			usage();
			return EXIT_SUCCESS;
		case 'c':
			is_client = 1;
			config.bdaddr = strndup(optarg, 17);
			break;
		case 'p':
			protocol_flag = 1;
			protocol_str = strndup(optarg, 10);
			break;
		case 'i':
			config.interface = strndup(optarg, 17);
			break;
		case 'm':
			mode_flag = 1;
			mode_str = strndup(optarg, 10);
			break;
		case '?':
			if (optopt == 'c' || optopt == 'p' || optopt == 'm') {
				fprintf(stderr, "Option -%c requires an argument.\n", optopt);
			}
			else if (isprint(optopt)) {
				fprintf(stderr, "Unknown option `-%c'\n", optopt);
			}
			else {
				fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
			}
			return 1;
		default:
			abort();
		}
	}

 	if (optind < argc) {
		filename_flag = 1;
		// staci vzit pouze jedno jmeno souboru
		config.filename = strndup(argv[optind],INTFR_CONFIG_FILENAME_LENGTH);
	}


	if (debug_flag) {
		printf("is_client = %s\n", is_client?"true":"false");
		printf("protocol_str = %s\n", protocol_str);
		printf("mode_str = %s\n", mode_str);
	}

	if (protocol_flag) {
		if (strncmp(protocol_str, "l2cap", 5) == 0) {
			config.protocol = L2CAP;
		} else
		if (strncmp(protocol_str, "sco", 3) == 0) {
			config.protocol = SCO;	
		} else {
			fprintf(stderr, "%s: unknown protocol value: %s\n", argv[0], protocol_str);
			usage();
			return EXIT_FAILURE;
		}
	} else {
		fprintf(stderr, "%s: protocol option is missing\n", argv[0]);
		usage();
		return EXIT_FAILURE;
	}

	if (mode_flag) {
		
		if (strncmp(mode_str, "basic", 5) == 0) {
			config.mode = L2CAP_MODE_BASIC;
		} else
		if (strncmp(mode_str, "retrans", 7) == 0) {
			config.mode = L2CAP_MODE_RETRANS;	
		} else
		if (strncmp(mode_str, "flowctl", 7) == 0) {
			config.mode = L2CAP_MODE_FLOWCTL;	
		} else
		if (strncmp(mode_str, "ertm", 4) == 0) {
			config.mode = L2CAP_MODE_ERTM;	
		} else
		if (strncmp(mode_str, "streaming", 9) == 0) {
			config.mode = L2CAP_MODE_STREAMING;	
		} else {
			fprintf(stderr, "%s: unknown protocol value: %s\n", argv[0], mode_str);
			usage();
			return EXIT_FAILURE;
		}
	} else if (config.protocol == L2CAP) {
		fprintf(stderr, "%s: L2CAP protocol specified but mode option missing\n", argv[0]);
		usage();
		return EXIT_FAILURE;
	}


	if (!filename_flag) {
		fprintf(stderr, "%s: missing filename\n", argv[0]);
		usage();
		return EXIT_FAILURE;
	}

	if (is_client) {
		
		run_client(&config);
	} else {
		run_server(&config);
	}

	return EXIT_SUCCESS;
}


