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

📄 consolesample.c

📁 一个在Linux下开发的IP摄像头的驱动程序及控制软件
💻 C
📖 第 1 页 / 共 5 页
字号:
	ServerUtl_DeleteDevice(hDevice);
	ServerUtl_Release(&hSrvUtl);

	return 0;
}

int TestMotionGetSet()
{
	HANDLE hSrvUtl;
	HANDLE hDevice;
	char lpszModelName[64]; 
	char szInputBuf[256];

	int nNum;
	DWORD dwWinNum;
	BOOL bSet;
	int i;
	SCODE scRet;
	TSrvDepResource_ServerInfo srvInfo;
	TSERVERUTL_MODETECT_INFO atMotionInfo[3];

	if (ServerUtlConnectToServer(&hSrvUtl, &hDevice, lpszModelName) < 0)
		return -1;

	SrvDepResource_GetParamForServer(lpszModelName, &srvInfo);

	// do a while loop to ask user for item to retrieve
	while (1)
	{
		printf("To end this program, please press Enter directly.\n");
		printf("Choose one operation\n(1) Read Motion Detection Settings, (2) Set Motion Detection Settings:");

		szInputBuf[0] = 0;
		fgets(szInputBuf, 255, stdin);
		if (strlen(szInputBuf) > 0 && szInputBuf[strlen(szInputBuf) - 1] == '\n')
			szInputBuf[strlen(szInputBuf) - 1] = 0;

		if (szInputBuf[0] == 0)
			break;

		nNum = atoi(szInputBuf);
		if (nNum < 1 || nNum > 2)
		{
			printf("\nIndex out of range!\n\n");
			continue;
		}

		if (nNum == 1)
		{
			time_t t1, t2;
			long lMilliSec1, lMilliSec2;
#ifndef _WIN32
			struct timeval tv;
			struct timezone tz;

			gettimeofday(&tv, &tz);

			t1 = tv.tv_sec;
			lMilliSec1 = (long)tv.tv_usec / 1000;
#else
			SYSTEMTIME stNow;
			time(&t1);
			GetLocalTime(&stNow);
			lMilliSec1 = (long) stNow.wMilliseconds;
#endif // 
			scRet = ServerUtl_GetMotionDetectionInfo(hDevice, atMotionInfo, &dwWinNum,
				FALSE, FALSE);
#ifndef _WIN32
			gettimeofday(&tv, &tz);

			t2 = tv.tv_sec;
			lMilliSec2 = (long)tv.tv_usec / 1000;
#else
			time(&t2);
			GetLocalTime(&stNow);
			lMilliSec2 = (long) stNow.wMilliseconds;
#endif // 

			lMilliSec2 -= lMilliSec1;
			t2 -= t1;
			if (lMilliSec2 < 0)
			{
				lMilliSec2 += 1000;
				t2--;
			}

			printf("Total spent time = %d.%03d\n", t2, lMilliSec2);

			if (scRet != S_OK)
			{
				printf("ServerUtl_GetMotionDetectionInfo failed %X\n\n", scRet);
				continue;
			}

			printf("Total Window Number = %d\n", dwWinNum);
			for (i = 0; i < (int) dwWinNum; i++)
			{
				if (atMotionInfo[i].bWindowEnabled)
				{
					if (atMotionInfo[i].bFullSize)
						printf("Sensitivity = %d, Percent = %d\n",
							atMotionInfo[i].nSensitivity, atMotionInfo[i].nPercent);
					else 
						printf("Windows %d: Name = %s, X = %d, Y = %d, W = %d, H = %d, Sensitivity = %d, Percent = %d\n",
							i + 1,
							atMotionInfo[i].szName, atMotionInfo[i].nX, atMotionInfo[i].nY,
							atMotionInfo[i].nW, atMotionInfo[i].nH,
							atMotionInfo[i].nSensitivity, atMotionInfo[i].nPercent);
				}
			}

			printf("\n");

			continue;
		}

		memset(atMotionInfo, 0, sizeof(atMotionInfo));
		bSet = FALSE;
		while (1)
		{
			if (lpszModelName[2] <= '2')
			{
				printf("Type the value settings in format: \"Sensitivity, Percentage\"\n");
				printf("(Note, each one in an individual line without comma):\n");
				if (scanf("%d %d", &atMotionInfo[0].nSensitivity, &atMotionInfo[0].nPercent) != 2)
				{
#ifndef _WIN32
					fgets(szInputBuf, 255, stdin);
#else
					fflush(stdin);
#endif // _WIN32
					printf("Incorrect Format\n\n");
					continue;
				}
				atMotionInfo[0].bWindowEnabled = TRUE;
#ifndef _WIN32
				fgets(szInputBuf, 255, stdin);
#else
				fflush(stdin);
#endif // _WIN32

				break;
			}
			else
			{
		
				printf("Which windows to set? (1-3, Type 4 to end input) ");

				szInputBuf[0] = 0;
				fgets(szInputBuf, 255, stdin);
				if (strlen(szInputBuf) > 0 && szInputBuf[strlen(szInputBuf) - 1] == '\n')
					szInputBuf[strlen(szInputBuf) - 1] = 0;

				if (szInputBuf[0] == 0)
				{
					ServerUtl_DeleteDevice(hDevice);
					ServerUtl_Release(&hSrvUtl);

					return -1;
				}

				nNum = atoi(szInputBuf);

				if (nNum < 1 || nNum > 4)
				{
					printf("\nIndex out of range!\n\n");
					continue;
				}

				if (nNum == 4)
				{
					if (!bSet)
					{
						printf("No windows data is set, must set at least one window\n");
						continue;
					}

					break;
				}
				nNum--;

				printf("Type the value settings in sequence:\n\"Name, X, Y, Width, Height, Sensitivity, Percentage\"\n");
				printf("(Note, each one in an individual line without comma):\n");
				if ((i = scanf("%[a-zA-Z0-9 _-] %d %d %d %d %d %d", atMotionInfo[nNum].szName,
					&atMotionInfo[nNum].nX, &atMotionInfo[nNum].nY, &atMotionInfo[nNum].nW,
					&atMotionInfo[nNum].nH, &atMotionInfo[nNum].nSensitivity, 
					&atMotionInfo[nNum].nPercent)) != 7)
				{
#ifndef _WIN32
					fgets(szInputBuf, 255, stdin);
#else
					fflush(stdin);
#endif // _WIN32
					printf("Incorrect Format %d\n\n", i);
					continue;
				}

				atMotionInfo[nNum].bWindowEnabled = TRUE;
#ifndef _WIN32
				fgets(szInputBuf, 255, stdin);
#else
				fflush(stdin);
#endif // _WIN32

				bSet = TRUE;
			}
		}

		scRet = ServerUtl_SetMotionDetectionInfo(hDevice, atMotionInfo, FALSE, FALSE);

		if (scRet != S_OK)
		{
			printf("ServerUtl_SetMotionDetectionInfo failed %X\n\n", scRet);
			continue;
		}

		printf("ServerUtl_SetMotionDetectionInfo OK\n\n");
	}


	ServerUtl_DeleteDevice(hDevice);
	ServerUtl_Release(&hSrvUtl);

	return 0;
}

void __stdcall ServUtlUpgradeCallback(HANDLE hDevice, DWORD dwContext, DWORD dwStatus,
                           DWORD dwParam1, DWORD dwParam2)
{
	char szMsg[MAX_PATH];
	TSERVERUTL_UPGSEND_INFO *ptUpgSendInfo;

	switch (dwParam1)
	{
	case SERVERUTL_UPG_UNPACK_FILE:
		strcpy(szMsg, "File Unpacked...");
		break;
	case SERVERUTL_UPG_FTP_CONNECT:
		strcpy(szMsg, "Server Connected..");
		break;
	case SERVERUTL_UPG_RM_REMOTE_FILE:
		sprintf(szMsg, "Server file or directory %s removed.", (const char *) dwParam2);
		break;
	case SERVERUTL_UPG_CREATE_DIR:
		sprintf(szMsg, "Create directory %s on server.", (const char *) dwParam2);
		break;
	case SERVERUTL_UPG_CHANGE_DIR:
		sprintf(szMsg, "Change to server directory %s.", (const char *) dwParam2);
		break;
	case SERVERUTL_UPG_PUTTING_FILE:
		//sprintf(szMsg, "file %s is uploading.", (const char *) dwParam2);
		{
			ptUpgSendInfo = (TSERVERUTL_UPGSEND_INFO *) dwParam2;

			sprintf(szMsg, "uploading %s %d%%/%d%%", 
				ptUpgSendInfo->szCurFileName,
				ptUpgSendInfo->lCurFileSize>0?(ptUpgSendInfo->lCurFileSentSize * 100 / ptUpgSendInfo->lCurFileSize):0,
				ptUpgSendInfo->lTotalSize>0?(ptUpgSendInfo->lCurSentSize * 100 / ptUpgSendInfo->lTotalSize):0);
		}
		break;
	case SERVERUTL_UPG_PUT_FILE:
		sprintf(szMsg, "file %s uploaded.", (const char *) dwParam2);
		break;
	case SERVERUTL_UPG_RESTART_SRV:
		strcpy(szMsg, "Restart command set");
		break;
	case SERVERUTL_UPG_WAIT_REBOOT:
		strcpy(szMsg, "Waiting for server to reboot");
		break;
	case SERVERUTL_UPG_SERVER_REBOOTING:
		strcpy(szMsg, "Server is rebooting");
		break;
	case SERVERUTL_UPG_SERVER_REBOOTED:
		strcpy(szMsg, "Server rebooted OK");
		break;
	case SERVERUTL_UPG_REMOVE_TMP_FILE:
		sprintf(szMsg, "Local temp file %s removed", (const char *) dwParam2);
		break;
	default:
		return;
	}

	printf("%s\n", szMsg);
}

int TestUpgrade()
{
	HANDLE hSrvUtl;
	HANDLE hDevice;
	int nCnt;
	char lpszModelName[64]; 
	TSERVERUTL_UPG_INFO tUpgInfo;
	char szCurrentDir[MAX_PATH];

	SCODE scRet, scRet2;
	TSrvDepResource_ServerInfo srvInfo;

	getcwd(szCurrentDir, MAX_PATH);

	if (ServerUtlConnectToServer(&hSrvUtl, &hDevice, lpszModelName) < 0)
		return -1;

	SrvDepResource_GetParamForServer(lpszModelName, &srvInfo);

	while (1)
	{
		if (g_szUpgradeFile[0] == 0 || access(g_szUpgradeFile, 0) != 0)
		{
			printf("To end this program, please press Enter directly.\n");
			printf("Please type the file name (could include path) to be upgraded:\n");

			g_szUpgradeFile[0] = 0;
			fgets(g_szUpgradeFile, 255, stdin);
			if (strlen(g_szUpgradeFile) > 0 && g_szUpgradeFile[strlen(g_szUpgradeFile) - 1] == '\n')
				g_szUpgradeFile[strlen(g_szUpgradeFile) - 1] = 0;

			if (g_szUpgradeFile[0] == 0)
			{
				ServerUtl_DeleteDevice(hDevice);
				ServerUtl_Release(&hSrvUtl);
				return -1;
			}
		}
		else
			break;
	}

	for (nCnt = 0; nCnt < g_nUpgradeTimes; nCnt++)
	{
		chdir(szCurrentDir);

		if (g_nUpgradeTimes > 1)
			printf("\n\nLoop Number = %d\n", nCnt + 1);

		if (nCnt != 0)
		{
			if (ServerUtlConnectToServer(&hSrvUtl, &hDevice, lpszModelName) < 0)
				return -1;
		}

///		printf("hSrvUtl = %X, hDevice = %X\n", hSrvUtl, hDevice);

		// start to do upgrade
		memset(&tUpgInfo, 0, sizeof(tUpgInfo));
		tUpgInfo.dwContext = (DWORD) 0;
		tUpgInfo.pfUpgradeCB = ServUtlUpgradeCallback;
		strcpy(tUpgInfo.szFileName, g_szUpgradeFile);
//		printf("FileName = %s\n", g_szUpgradeFile);

		scRet = ServerUtl_Upgrade(hDevice, &tUpgInfo);
		scRet2 = ServerUtl_DeleteDevice(hDevice);
		if (scRet2 != S_OK)
		{
			printf("ServerUtl_DeleteDevice error: %X (%X)\n", scRet2, hDevice); 
		}
		
		scRet2 = ServerUtl_Release(&hSrvUtl);
		if (scRet2 != S_OK)
		{
			printf("ServerUtl_Release error: %X (%X)\n", scRet2, hSrvUtl); 
		}

		if (scRet == S_OK)
		{	
			printf("ServerUtl_Upgrade success\n");
		}
		else
		{
			printf("ServerUtl_Upgrade Fail, error = %X\n", scRet);
			break;
		}

#ifdef _WIN32
		Sleep(g_nUpgradeLoopWait);
#else
		sleep(g_nUpgradeLoopWait / 1000);
#endif // _WIN32

	}

	return 0;
}

int main(int argc, char* argv[])
{
	memset(g_Channel_Info, 0, sizeof(g_Channel_Info));
	g_Channel_Info[0].dwProtocol = 1;
	g_Channel_Info[0].dwHTTPPort = 80;
	
	if (HandleCommandLineArgument(argc, argv) < 0)
		return 1;

	switch (g_nType)
	{
	case 0:	// test connection to server, it will get the data and decode to bmp/pcm format
		HandleNormalConnect();
		break;
	case 1:	// detect the model of specified server
		DetectServerModel();
		break;
	case 2:	// test to get system setting from server (by config.ini)
		TestGetSystemSettings();
		break;
	case 3:	// test DI/DO control
		TestDIDO();
		break;
	case 4:	// test PTZ control
		TestPTZ();
		break;
	case 5:	// test UART read/write
		TestUart();
		break;
	case 6:	// test Get/Set Motion detection argument
		TestMotionGetSet();
		break;
	case 7:	// test upgrade
		TestUpgrade();
		break;
	case 8:	// list the supported model
		ListSupportedModel();
		break;
	}

	return 0;
}

void OutputMoreOptionsForNormal(int nType)
{
	printf("More options for type %d:\n[-P<protocol> -H<standard 263 or not> -C<use network callback or not> -R<output raw type>]\n", nType);
	printf("Where <protocol> is the protocl used to connect to server, the possible value is:\n");
	printf("1: TCP, 2: UDP, 3: HTTP\n");
	printf("<use network callback or not> means to callback the raw data receive from network before assembling to a frame\n");
	printf("<output raw type> means to decode the video into which format value is from 1-11\n");
}

void OutputMoreOptionsForUart(int nType)
{
	printf("More options for type %d:\n[-O<timeout in milliseconds> -F<flush UART or not before reading>]\n", nType);
	printf("Where <timeout in milliseconds> is the UART reading timeout, program will block before timeout or wanted bytes are read\n");
	printf("<flush UART or not before reading> means if server need to flush UART before reading from it\n");
}

void OutputMoreOptionsForUpgrade(int nType)
{
	printf("More options for type %d:\n[-F<file to be upgraded> -L<loop times> -W<millisecs between upgrade>]\n", nType);
	printf("Where <file to be upgraded> is the flash file for all series or package file for 4/5/6K series\n");
	printf("      <loop times> is the loop count this program will run before stop, default is 1\n");
	printf("      <file to be upgraded> is the time to wait before next run for upgrade\n");
}

void OutputUsage(const char *pszProgName, int nType)
{
	char szBuf[MAX_PATH];
	char *pszPtr;

	strcpy(szBuf, pszProgName);
	pszPtr = strrchr(szBuf, CHAR_PATH_SEP);
	if (pszPtr == NULL)
		pszPtr = szBuf;
	else
		pszPtr++;


	printf("Usage: %s -Z<type> -N<host ip> -U<user id> -S<password> -M<model> [-T<port> -B<DecodeType>] [More options]\n", pszPtr);
	printf("Where <type> is the program mode:\n");
	printf("  0: Connect to server to get media and/or decode the media\n");
	printf("  1: Retrieve the server model\n");
	printf("  2: Retrieve the system settings from server and list the entry by index\n");
	printf("  3: Test the DI/DO retrieving/setting\n");
	printf("  4: Test PTZ control for the camera\n");
	printf("  5: Test UART read/write\n");
	printf("  6: Test to get/set motion detection settings on servers\n");
	printf("  7: Test upgrade function\n");
	printf("  8: List the supported model for VNDP\n");
	printf("<model> is the friendly model name such as VS3102-VVTK or PT31x4-VVTK\n");
	printf("<port> is the http port for the server\n");
	printf("<DecodeType> is the type to input data to decoder channel, yes to use callback way, no to use direct call\n");

	if (nType < 0)
		printf("To see more options for each type, please type %s -Z<type>\n", pszProgName);
	else
	{
		if (nType == 0)
			OutputMoreOptionsForNormal(nType);
		else if (nType == 5)
			OutputMoreOptionsForUart(nType);

⌨️ 快捷键说明

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