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

/*
i2c-scan -b 0
*/

void help()
{
	printf("authlicense -w \n");
	printf("    -Write new license to flash. Input [128 bytes license]\n");
	printf("authlicense -r \n");
	printf("    -Read license form flash\n");
	printf("authlicense -guid \n");
	printf("    -Read device GUID.\n");
}

void saveToFlash(uint8_t *license)
{
	iflash_t iflash_arg;
	int ret = 0;
	int fd = open("/dev/iflash", O_RDWR);
	ret = ioctl(fd, IFLASH_UNLOCK, NULL);
	if(ret != 0)
	{
		printf("error: ret = %d\n", ret);
	}

	iflash_arg.sector = 7;
	ret = ioctl(fd, IFLASH_ERASESECTOR, &iflash_arg);
	if(ret != 0)
	{
		printf("error: ret = %d\n", ret);
	}

	iflash_arg.address = 0x08060000;
	iflash_arg.len = 128;
	memcpy(iflash_arg.data, license, 128);
	ret = ioctl(fd, IFLASH_WRITEBYTE, &iflash_arg);
	if(ret != 0)
	{
		printf("error: ret = %d\n", ret);
	}

	ret = ioctl(fd, IFLASH_LOCK, NULL);
	if(ret != 0)
	{
		printf("error: ret = %d\n", ret);
	}
	
	close(fd);
}

int main(int argc, char* argv[])
{
	int i;
	char chr;
	uint8_t tmp;
	uint32_t tmp1;
	uint8_t  GUID[12] = {0};
	uint32_t *pUID = (uint32_t*)0x1FFF7A10;
	uint8_t  *pLicense = (uint8_t*)0x08060000; 
    uint8_t  HexLicense[128] = {0};
    uint8_t CharLicense[256+10] = {0};

    iflash_t iflash_arg;

	if(argc != 2)
	{
		help();
		return -1;
	}

	if(strcmp(argv[1], "-test") == 0)
	{
		int fd = open("/dev/iflash", O_RDWR);
		ioctl(fd, IFLASH_UNLOCK, NULL);

		iflash_arg.sector = 7;
		ioctl(fd, IFLASH_ERASESECTOR, &iflash_arg);

		iflash_arg.address = 0x08060000;
		iflash_arg.len = 8;
		iflash_arg.data[0] = 1;
		iflash_arg.data[1] = 2;
		iflash_arg.data[2] = 3;
		iflash_arg.data[3] = 4;
		iflash_arg.data[4] = 5;
		iflash_arg.data[5] = 6;
		iflash_arg.data[6] = 7;
		iflash_arg.data[7] = 8;
		ioctl(fd, IFLASH_WRITEBYTE, &iflash_arg);

		ioctl(fd, IFLASH_LOCK, NULL);
		close(fd);
		return 0;
	}

	//读取License
	if(strcmp(argv[1], "-r") == 0)
	{			
		printf("License: ");
		for(i=0;i<128;i++)
		{
			if(i%32 == 0)
				printf("\t\n");

			printf("%02x", *pLicense);
			pLicense++;
		}
		printf("\n");
	}
	//写入License
	else if(strcmp(argv[1], "-w") == 0)
	{
		while(1)
		{
			memset(CharLicense, 0, sizeof(CharLicense));
			printf("\nPlease input license(128 bytes): ");
			scanf("%s", CharLicense);
			
			//检查License长度
			if(strlen(CharLicense) != 256)
			{
				printf("License length is 128 bytes, but input is %d bytes! Please re input!\n", (strlen(CharLicense)+1)/2);
				continue;
			}

			for(i=0;i<128;i++)
			{
				//获取高4位
				chr = CharLicense[2*i+0];
				if((chr >= '0') && (chr <= '9'))
				{
					tmp = chr - '0';
				}
				else if((chr >= 'a') && (chr <= 'f'))
				{
					tmp = chr - 'a' + 10;
				}
				else if((chr >= 'A') && (chr <= 'F'))
				{
					tmp = chr - 'A' + 10;
				}
				else
				{
					printf("Invalid char %s\n", chr);
					return -1;
				}
				HexLicense[i] = tmp<<4;
				//获取低4位
				chr = CharLicense[2*i+1];
				if((chr >= '0') && (chr <= '9'))
				{
					tmp = chr - '0';
				}
				else if((chr >= 'a') && (chr <= 'f'))
				{
					tmp = chr - 'a' + 10;
				}
				else if((chr >= 'A') && (chr <= 'F'))
				{
					tmp = chr - 'A' + 10;
				}
				else
				{
					printf("Invalid char %s\n", chr);
					return -1;
				}
				HexLicense[i] |= tmp;				
			}

			//获取GUID
			for(i=0;i<3;i++)
		    {
		        tmp1 = *pUID;
		        GUID[4*i+0] = tmp1&0xff;
		        GUID[4*i+1] = (tmp1>>8)&0xff;
		        GUID[4*i+2] = (tmp1>>16)&0xff;
		        GUID[4*i+3] = (tmp1>>24)&0xff;
		        pUID++;	
		    }
		    //检查Licnese是否有效
			if(CheckLicense(GUID, HexLicense) == 0)
			{
				//将license写入Flash
				saveToFlash(HexLicense);
				printf("Save license to falsh ok.\n");

				//唤醒后台监控脚本
				system("killall -9 damon.sh");
				system("damon.sh &");
				return 0;
			}
			else
				continue;
		}
	}
	//读取GUID
	else if(strcmp(argv[1], "-guid") == 0)
	{		
	    printf("GUID: ");
	    for(i=0;i<3;i++)
	    {
	        tmp1 = *pUID;
	        GUID[4*i+0] = tmp1&0xff;
	        GUID[4*i+1] = (tmp1>>8)&0xff;
	        GUID[4*i+2] = (tmp1>>16)&0xff;
	        GUID[4*i+3] = (tmp1>>24)&0xff;
	        pUID++;
	        printf("%02x %02x %02x %02x ",  GUID[4*i+0],GUID[4*i+1],GUID[4*i+2],GUID[4*i+3]);
	    }
	    printf("\n");	
	}
	else
	{
		printf("Invalid param %s\n", argv[1]);
		help();
		return -1;
	}

	return 0;
}