⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oemgpsparam.c

📁 ARM9 OEM CSP FRO MOBILE.
💻 C
字号:
#include <windows.h>
#include <stdio.h>

#ifndef WINDOWS_SIMULATION
#define PARAM_COPY_NUMBER 4
#else
#define PARAM_COPY_NUMBER 1
#endif

WCHAR wszFlashDrvFolder[64];
char szFlashDrvFolder[64];
char parameter_file_fmt[96];

typedef enum tagGPS_PARA_PERSIST
{
	GPS_PARA_PERSIST_FILE,//Do it with file
	GPS_PARA_PERSIST_REGISTRY,//Do it with registry
	GPS_PARA_PERSIST_RSV_NAND//Do it with reserved nandflash
} GPS_PARA_PERSIST;

typedef struct {
	unsigned long Length;		// length of the payload
	unsigned long check_sum;	// check sum of the payload
} DATA_HEADER;

static int g_LatestIndex = -1;

// this variable may change by customer, or use registry setting to assign the value
static GPS_PARA_PERSIST g_mode = GPS_PARA_PERSIST_RSV_NAND;

extern BOOL GPA_Init(VOID);
extern VOID GPA_Deinit(VOID);
extern DWORD GPA_Write(DWORD dwParamIndex, PBYTE pParamBufIn, DWORD dwBufInLen);
extern DWORD GPA_Read(DWORD dwParamIndex, PBYTE pParamBufOut, DWORD dwBufOutLen);

void InitPathVar()
{
#ifndef WINDOWS_SIMULATION
	LONG regError;
	HKEY hKey;
	DWORD dwDataSize;

	regError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		L"Drivers\\BuiltIn\\GPS",
		//L"System\\StorageManager\\Profiles\\FlashDrv",
		0,
		0,
		&hKey);
	if(regError != ERROR_SUCCESS)
	{
		wcscpy(wszFlashDrvFolder,L"ResidentFlash");
	}else
	{
		dwDataSize = sizeof(wszFlashDrvFolder);
		regError = RegQueryValueEx(
			hKey, 
			TEXT("ParamFolder"), 
			NULL, 
			NULL,
			(LPBYTE)(wszFlashDrvFolder), 
			&dwDataSize);
		if(regError == ERROR_SUCCESS)
		{
		}else
		{
			wcscpy(wszFlashDrvFolder,L"ResidentFlash");
		}
		RegCloseKey(hKey);
	}
#else
	wcscpy(wszFlashDrvFolder,L"ResidentFlash");
#endif
	WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK ,wszFlashDrvFolder,-1,szFlashDrvFolder,sizeof(szFlashDrvFolder)
		,NULL,NULL);
}

BOOL InitParamMedia()
{
	if (g_mode == GPS_PARA_PERSIST_FILE || 
		g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		InitPathVar();
#ifndef WINDOWS_SIMULATION
		sprintf(parameter_file_fmt,"\\%s\\gpspara%%c.bin",szFlashDrvFolder);
#else
		sprintf(parameter_file_fmt,"D:\\%s\\gpspara%%c.bin",szFlashDrvFolder);
#endif
	}

	if (g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		return GPA_Init();
	}
	else
		return TRUE;
}

BOOL DeinitParamMedia()
{
	if (g_mode == GPS_PARA_PERSIST_RSV_NAND)
		GPA_Deinit();
	return TRUE;
}

int GetOldestParaIndex(void)
{
	if (g_LatestIndex + 1 >= PARAM_COPY_NUMBER)
		return 0;
	else
		return g_LatestIndex + 1;
}

int GetLatestParaIndex(void)
{
	int i, read_size;
	unsigned char DataHeader[sizeof(DATA_HEADER) + sizeof(FILETIME)];
	unsigned char FileName[128];
	LARGE_INTEGER time_stamp, *ptime;
	int index = -1;
	FILE *fp;

	// If previously determined, use existing value
	if (g_LatestIndex >= 0)
		return g_LatestIndex;

	// else, check time stamp of all copys
	if(g_mode == GPS_PARA_PERSIST_FILE)
	{
		for (i = 0; i < PARAM_COPY_NUMBER; i ++)
		{
			sprintf(FileName, parameter_file_fmt, i+'0');
			if ((fp = fopen(FileName, "rb")) == NULL)
				continue;
			read_size = fread(DataHeader, sizeof(char), sizeof(DataHeader), fp);
			fclose(fp);
			if (read_size != sizeof(DataHeader))
				continue;
			ptime = (LARGE_INTEGER *)(DataHeader + sizeof(DATA_HEADER));
			if (index != -1)
			{
				if (time_stamp.QuadPart < ptime->QuadPart)
				{
					time_stamp.QuadPart = ptime->QuadPart;
					index = i;
				}
			}
			else
			{
				time_stamp.QuadPart = ptime->QuadPart;
				index = i;
			}
		}
	}
	else if(g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		//TODO: This is the first time after boot up, read through all the copies in the reserved blocks
		//TODO: and find out which copy has the latest time stamp. 
		//TODO: Note, the time stamp is put in the head of the payload, the type is FILETIME or can be treated as LARGE_INTEGER
		//TODO: If all the reserved blocks are in the state of uninitialized, return -1
		for (i = 0; i < PARAM_COPY_NUMBER; i ++)
		{
			if (GPA_Read(i, DataHeader, sizeof(DataHeader)) != sizeof(DataHeader))
				continue;
			ptime = (LARGE_INTEGER *)(DataHeader + sizeof(DATA_HEADER));
			if (index != -1)
			{
				if (time_stamp.QuadPart < ptime->QuadPart)
				{
					time_stamp.QuadPart = ptime->QuadPart;
					index = i;
				}
			}
			else
			{
				time_stamp.QuadPart = ptime->QuadPart;
				index = i;
			}
		}
	}

	g_LatestIndex = index;
	return index;
}

BOOL LoadParamData(void *pData, int size)
{
	int i, j, index;
	unsigned char FileName[128];
	DATA_HEADER DataHeader;
	unsigned long check_sum, *p;
	int header_size, data_size;
	FILE *fp;
    unsigned int TotalSize = sizeof(DATA_HEADER) + size;
    unsigned char *rdBuf;
	DATA_HEADER *pDataHeader;
	char param_flag[8];

	index = GetLatestParaIndex();
	// no copy exist
	if (index < 0)
		return FALSE;

	if (g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		rdBuf = malloc(TotalSize);
		pDataHeader = (DATA_HEADER *)rdBuf;
	}
	else
		pDataHeader = &DataHeader;

	// try load from the latest copy
	for (i = 0; i < PARAM_COPY_NUMBER; i ++)
	{
		// try load the previous one
		if (i != 0)
		{
			index --;
			if (index < 0)
				index = PARAM_COPY_NUMBER - 1;
		}

		// use the index to load the corresponding copy, the total size is size of data header and payload length
		if(g_mode == GPS_PARA_PERSIST_FILE)
		{
			sprintf(FileName, parameter_file_fmt, index+'0');
			if ((fp = fopen(FileName, "rb")) == NULL)
				continue;
			header_size = fread(&DataHeader, sizeof(DataHeader), 1, fp);
			data_size = fread(pData, 1, size, fp);
			fclose(fp);
			if (header_size == 0 || (int)(DataHeader.Length) != size || data_size != size)
				continue;
		}
		else if(g_mode == GPS_PARA_PERSIST_RSV_NAND)
		{
			if (GPA_Read(index, rdBuf, TotalSize) != TotalSize)
				continue;
			if ((int)(pDataHeader->Length) != size)
				continue;
			memcpy(pData, rdBuf + sizeof(DATA_HEADER), size);
		}

		// if check sum of the payload is OK
		check_sum = 0;
		p = (unsigned long *)pData;
		for (j = 0; j < size / 4; j ++)
			check_sum += *p ++;
		if (check_sum == pDataHeader->check_sum)
		{
			memcpy(param_flag, (char *)pData + size - 8, sizeof(char) * 8);
			if (param_flag[0] == 'G' && param_flag[1] == 'P' && param_flag[2] == 'S' && param_flag[3] == 'P' &&
				param_flag[4] == 'A' && param_flag[5] == 'R' && param_flag[6] == 'A' && param_flag[7] == 'M')
				break;
		}
	}

	if (g_mode == GPS_PARA_PERSIST_RSV_NAND)
		free(rdBuf);

	if (i < PARAM_COPY_NUMBER)
		return TRUE;
	else
		return FALSE;
}

BOOL SaveParamData(void *pData, int size)
{
	int i, index;
	unsigned long *p;
	DATA_HEADER DataHeader;
	unsigned char FileName[128];
	FILE *fp;
    unsigned int TotalSize = sizeof(DATA_HEADER) + size;
    unsigned char *wtBuf;
	DATA_HEADER *pDataHeader;

	if (g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		wtBuf = malloc(TotalSize);
		pDataHeader = (DATA_HEADER *)wtBuf;
	}
	else
		pDataHeader = &DataHeader;

	index = GetOldestParaIndex();
	pDataHeader->check_sum = 0;
	p = (unsigned long *)pData;
	for (i = 0; i < size / 4; i ++)
		pDataHeader->check_sum += *p ++;
	pDataHeader->Length = size;

	// use the index to save to the corresponding copy, the total size is size of data header and payload length
	if(g_mode == GPS_PARA_PERSIST_FILE)
	{
		sprintf(FileName, parameter_file_fmt, index+'0');
		if ((fp = fopen(FileName, "wb")) == NULL)
			return FALSE;
		fwrite(&DataHeader, sizeof(DataHeader), 1, fp);
		fwrite(pData, 1, size, fp);
		fclose(fp);
	}
	else if(g_mode == GPS_PARA_PERSIST_RSV_NAND)
	{
		memcpy(wtBuf + sizeof(DATA_HEADER), pData, size);
		if (GPA_Write(index, wtBuf, TotalSize) != TotalSize)
		{
			free(wtBuf);
			return FALSE;
		}
	}

	if(g_mode == GPS_PARA_PERSIST_RSV_NAND)
		free(wtBuf);

	g_LatestIndex = index;

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -