#include "XNetSDKTest.h"
#ifdef WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib,"ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<net/if.h>
#include<net/if_arp.h>
#include<sys/ioctl.h>
#include<unistd.h>
#endif

int TestDevSearch()
{
	printf("%s\r\n", __FUNCTION__);
	char c = 0;
	do 
	{
		XSDK_SearchDevices(4000);
	}
	while((c = getchar()) != 'q');
	return 0;
}

bool GetMacByGetAdaptersInfo(char *pMacRet)
{
	bool ret = false;
#ifdef WIN32
	ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
	PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
	if(pAdapterInfo == NULL)
	{
		return false;
	}

	if(GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) 
	{
		free(pAdapterInfo);
		pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
		if (pAdapterInfo == NULL) 
		{
			return false;
		}
	}

	if(GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR)
	{
		for(PIP_ADAPTER_INFO pAdapter = pAdapterInfo; pAdapter != NULL; pAdapter = pAdapter->Next)
		{
			char acMAC[32] = {0};
			sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
				int (pAdapter->Address[0]),
				int (pAdapter->Address[1]),
				int (pAdapter->Address[2]),
				int (pAdapter->Address[3]),
				int (pAdapter->Address[4]),
				int (pAdapter->Address[5]));
			strcpy(pMacRet, acMAC);
			ret = true;
			break;
		}
	}

	free(pAdapterInfo);
#else     
	int sock_mac = socket( AF_INET, SOCK_STREAM, 0 );  
	if( sock_mac == -1)  
	{  
		perror("create socket falise...mac/n");  
		return false;  
	}  

	struct ifreq ifr_mac;
	memset(&ifr_mac,0,sizeof(ifr_mac));     
	strncpy(ifr_mac.ifr_name, "eth0", sizeof(ifr_mac.ifr_name)-1);     

	if( (ioctl( sock_mac, SIOCGIFHWADDR, &ifr_mac)) < 0)  
	{  
		printf("mac ioctl error/n");  
		return false;  
	}  

	char acMAC[32] = {0};
	sprintf(acMAC,"%02X:%02X:%02X:%02X:%02X:%02X",  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],  
		(unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);  

	strcpy(pMacRet, acMAC);
	ret = true;
	close(sock_mac);  
#endif

	return ret;
}

void OnDevSearchResult(int nResult, SXSDK_CONFIG_NET_COMMON *pDevs, int nCount)
{
	printf("OnDevSearchResult[SearchResutl:%d][DeviceCount:%d]\r\n", nResult, nCount);
	for (int i = 0; i < nCount; i++)
	{
		printf("[%d][SN:%s][IP:%d.%d.%d.%d][Port:%d]\r\n", i, pDevs[i].sSn, 
			pDevs[i].HostIP.c[0], pDevs[i].HostIP.c[1], pDevs[i].HostIP.c[2], pDevs[i].HostIP.c[3],
			pDevs[i].TCPPort);
	}

	bool bModyfyIP = false;
	if (bModyfyIP)
	{
		char localMac[64] = {0};
		bool bRet = GetMacByGetAdaptersInfo(localMac);

		if(!bRet)
		{
			printf("Get LocalMac Fail\r\n");
			return;
		}

		signed char newEncryptPsw[512];
		memset(newEncryptPsw, 0, 512);
		XSDK_EncryptPassword((const char*)"", newEncryptPsw, 512);

		if(pDevs != NULL)
		{
			char ip[16] = {0};
			char gateway[16] = {0};
			char submask[16] = {0};
			sprintf(ip, "%d.%d.%d.%d", pDevs[0].HostIP.c[0], pDevs[0].HostIP.c[1], pDevs[0].HostIP.c[2], pDevs[0].HostIP.c[3]);
			sprintf(gateway, "%d.%d.%d.%d", pDevs[0].Gateway.c[0], pDevs[0].Gateway.c[1], pDevs[0].Gateway.c[2], pDevs[0].Gateway.c[3]);
			sprintf(submask, "%d.%d.%d.%d", pDevs[0].Submask.c[0], pDevs->Submask.c[1], pDevs[0].Submask.c[2], pDevs[0].Submask.c[3]);

			strcpy(ip, "10.10.88.219");

			SXSDK_CONFIG_NET_COMMON_V2 devInfo;
			memset(&devInfo, 0, sizeof(SXSDK_CONFIG_NET_COMMON_V2));

			devInfo.HostIP.l = inet_addr(ip);
			devInfo.Gateway.l = inet_addr(gateway);
			devInfo.Submask.l = inet_addr(submask);
			strcpy(devInfo.HostName, pDevs[0].HostName);
			devInfo.HttpPort = pDevs[0].HttpPort;
			strcpy(devInfo.LocalMac, localMac);
			devInfo.MaxBps = pDevs[0].MaxBps;
			devInfo.MaxConn = pDevs[0].MaxConn;
			devInfo.MonMode = 0;
			devInfo.nPasswordType = 1;
			strcpy(devInfo.Password, (const char*)newEncryptPsw);
			strcpy(devInfo.sMac, pDevs[0].sMac);
			devInfo.SSLPort = pDevs[0].SSLPort;
			devInfo.TCPPort = pDevs[0].TCPPort;
			devInfo.TransferPlan = pDevs[0].TransferPlan;
			devInfo.UDPPort = pDevs[0].UDPPort;
			strcpy(devInfo.UserName, "admin");

			XSDK_ModifyDevIP((const char*)&devInfo, sizeof(SXSDK_CONFIG_NET_COMMON_V2), 5000);
		}
	}
}