#include <stdio.h>
#include <stdint.h>
#include "driver.h"
#include "hal_interface_api.h"
#include <fcntl.h>

/*
i2c-scan -b 0
*/

void help()
{
	printf("i2c-scan -b [channel] <-t [target address]>\n");
	printf("    -b: set i2c channel\n");
	printf("    -t: set target address, option.\n");
	printf("        If no -t, san all address. form 1 to 255.\n");
}
int main(int argc, char* argv[])
{
	int fd;
	int i;
	int ret;
	uint8_t buf[100];
	int channel;
	uint8_t target_addr, start_addr = 2, end_addr = 254;
	char devname[20] = {0};
	char chr;

	if((argc != 3) && (argc != 5))
	{
		help();
		return -1;
	}

	if(strcmp(argv[1], "-b") != 0)
	{
		printf("Invalid param %s\n", argv[1]);
		help();
		return -1;
	}

	//Get Channel
	if((strncmp(argv[2], "0x", 2) == 0) || (strncmp(argv[2], "0X", 2) == 0))
	{			
		if(strlen(argv[2]) > 4)
		{
			printf("Invalid Channel %s\n", argv[2]);
			return -1;
		}

		chr = argv[2][2];
		if((chr >= '0') && (chr <= '9'))
		{
			channel = chr - '0';
		}
		else if((chr >= 'a') && (chr <= 'f'))
		{
			channel = chr - 'a' + 10;
		}
		else if((chr >= 'A') && (chr <= 'F'))
		{
			channel = chr - 'A' + 10;
		}
		else
		{
			printf("Invalid channel %s\n", argv[2]);
			return -1;
		}

		if(argv[2][3] != '\0')
		{
			chr = argv[2][3];
			channel <<= 4; 
			if((chr >= '0') && (chr <= '9'))
				channel += chr - '0';
			else if((chr >= 'a') && (chr <= 'f'))
				channel += chr - 'a' + 10;
			else if((chr >= 'A') && (chr <= 'F'))
				channel += chr - 'A' + 10;
			else
			{
				printf("Invalid channel %s\n", argv[2]);
				return -1;
			}
		}
	}
	else
	{
		channel = atoi(argv[2]);
	}

	printf("i2c-scan -b %d ", channel);
	sprintf(devname, "/dev/i2c%d", channel);

	if(argc == 5)
	{
		if(strcmp(argv[3], "-t") != 0)
		{
			printf("Invalid Param %s\n", argv[3]);
			help();
			return -1;
		}

		//Get Target Addr
		if((strncmp(argv[4], "0x", 2) == 0) || (strncmp(argv[4], "0X", 2) == 0))
		{			
			if(strlen(argv[4]) > 4)
			{
				printf("Invalid target Address %s\n", argv[4]);
				return -1;
			}

			chr = argv[4][2];
			if((chr >= '0') && (chr <= '9'))
			{
				target_addr = chr - '0';
			}
			else if((chr >= 'a') && (chr <= 'f'))
			{
				target_addr = chr - 'a' + 10;
			}
			else if((chr >= 'A') && (chr <= 'F'))
			{
				target_addr = chr - 'A' + 10;
			}
			else
			{
				printf("Invalid target Address %s\n", argv[4]);
				return -1;
			}

			if(argv[4][3] != '\0')
			{
				chr = argv[4][3];				
				target_addr <<= 4; 
				if((chr >= '0') && (chr <= '9'))
					target_addr += chr - '0';
				else if((chr >= 'a') && (chr <= 'f'))
					target_addr += chr - 'a' + 10;
				else if((chr >= 'A') && (chr <= 'F'))
					target_addr += chr - 'A' + 10;
				else
				{
					printf("Invalid target Address %s\n", argv[4]);					
					return -1;
				}
			}
		}
		else
		{
			target_addr = atoi(argv[4]);
		}

		printf(" -t %#02x", target_addr);
		start_addr = target_addr;
		end_addr = target_addr;
	}
	printf("\n");
 	printf("Find Slave I2C device:\n");
	for(i=start_addr;i<=end_addr;i+=2)
	{
		fd = open(devname, O_RDWR);
		ret = stm32_i2c_master_write(fd, i, buf, 0);
		if(ret == 0)
		{
			printf("--->  %#02x\n", i);
		}
		// else
		// {
		// 	printf("%#02x\n", i);
		// }
		close(fd);
	}
	printf("\n");
	return 0;
}