#if !defined(_SDKDEMO_H_)
#define _SDKDEMO_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <stdio.h>
#include<iostream>
#include <stdlib.h>

#ifdef WIN32
#include <windows.h>

#else
#include <semaphore.h>
#include <pthread.h>
#include <sys/times.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
//#include "atomic.h"
#include "dlfcn.h"

#define Sleep(x) sleep(x * 1000)

typedef long LONG;
typedef unsigned long       DWORD;
typedef char  BYTE;
typedef unsigned int        UINT;
typedef unsigned short      WORD;
typedef int					BOOL;

#define HANDLE			int
#define HWND			void*
#define INFINITE		0xFFFFFFFF
#define HDC				void*
#define LPVOID			void*
#define LPDWORD			DWORD*
#define LPBYTE			BYTE*
#define STATUS_WAIT_0   ((DWORD)0x00000000L)
#define WAIT_OBJECT_0   ((STATUS_WAIT_0) + 0)

#define TRUE					1
#define FALSE					0

#ifndef NULL
#define NULL					0
#endif
#define CALLBACK
#define WINAPI

typedef DWORD (WINAPI *LPTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);
#endif

#include "NetSdkModule.h"


class COSThread
{
public:
	COSThread();
	virtual ~COSThread();

#ifdef WIN32
	HANDLE		m_hThread;
#else
	pthread_t	m_hThread;
#endif
	BOOL		m_bCreate;
};
class COSEvent
{
public:
	COSEvent()
	{
		m_bCreate = FALSE;
	}
	virtual ~COSEvent()
	{
		if (m_bCreate)
		{
			sem_destroy(&m_hEvent);
			m_bCreate = FALSE;
		}
	}


	sem_t		m_hEvent;
	BOOL		m_bCreate;
	BOOL		m_bManualReset;
};
#define OS_EVENT		COSEvent
#define OS_THREAD		COSThread

int		CreateEventEx(OS_EVENT &event, BOOL bManualReset, BOOL bInitialState)
{
	unsigned int nValue = bInitialState?1:0;
	int ret = sem_init(&event.m_hEvent, 0, nValue);
	if (ret != 0)
	{
		event.m_bCreate = FALSE;
		return -1;
	}
	event.m_bCreate = TRUE;

	return 0;
}


void	CloseEventEx(OS_EVENT &event)
{
	if (event.m_bCreate)
	{
#ifdef WIN32
		if (event.m_hEvent != NULL)
		{
			CloseHandle(event.m_hEvent);
			event.m_hEvent = NULL;
		}
#else
		sem_destroy(&event.m_hEvent);
#endif
		event.m_bCreate = FALSE;
	}
}


BOOL	SetEventEx(OS_EVENT &event)
{
	BOOL bRet = FALSE;
	if (event.m_bCreate)
	{
		int ret = sem_post(&event.m_hEvent);
		bRet = (ret == 0) ? TRUE:FALSE;
	}

	return bRet;
}


BOOL	ResetEventEx(OS_EVENT &event)
{
	BOOL bRet = FALSE;
	if (event.m_bCreate)
	{
		int sval = 0;
		do 
		{
			sem_trywait(&event.m_hEvent);
			sem_getvalue(&event.m_hEvent, &sval);
		} while(sval > 0);

		bRet = TRUE;
	}

	return bRet;
}

DWORD	WaitForSingleObjectEx(OS_EVENT &event, DWORD dwMilliseconds)
{
	DWORD dwRet = 0xFFFFFFFF;
	if (event.m_bCreate)
	{
		if (dwMilliseconds == 0)
		{
			int ret = sem_trywait(&event.m_hEvent);
			if (ret == 0)
			{
				dwRet = 0;
			}
		}
		else if (dwMilliseconds == INFINITE)
		{
			sem_wait(&event.m_hEvent);
			dwRet = 0;
		}
		else
		{
			int roopMax = dwMilliseconds/5;
			do 
			{
				usleep(5*1000);
				int ret = sem_trywait(&event.m_hEvent);
				if (ret == 0)
				{
					dwRet = 0;
					break;
				}

				roopMax--;
			} while(roopMax > 0);
		}

		ResetEventEx(event);
	}

	return dwRet;
}

using namespace std;

/////////////////////////////////辅助函数/////////////////////////////////////////
//接回车键继续
void EnterKeyContinue(char *szStr = "continue");

//写测试文件
int WriteFile(const char *pFileName, char *pData, int nLenght);
//---------------------------------------------------------------------------------------


////////////////////////////////回调函数//////////////////////////////////////////
//断线回调函数
void CALL_METHOD DisConnectCBFunc(long lLoginID, char *pchDVRIP, long nDVRPort, unsigned long dwUser);

//断线重连成功回调函数
//---------------------------------------------------------------------------------------

////////////////////////////////测试函数//////////////////////////////////////////
void TestMedia();			// 测试实时预览----实时预览参数程序
void TestRcord();			// 录像查询、下载、回放----录像处理参数程序
void TestPTZ();				// 云台操作----操作云台参数程序
void TestCodeCfg();			// 测试编码配置----参数配置参数程序
void TestAlarmCallBack();	// 测试报警回调----报警处理参考程序
void TestFaceDetectMsg();
void TestFDLibBaseCfg();
void TestFDSearchResult();
void TestReconnect();		// 测试断线重连功能
void TestTalk();			// 测试语音对讲
void TestRecordDownload();	// 录像下载
void TestSearchDvr();		// 搜索设备
void TestTransCom();		// 透明串口
void TestRecordDownByTime();// 录像按时间下载
void TestCHGChannelName();	// 测试修改通道名称
int TestRtspServer();		// 测试Rtsp服务功能
void TestRtspVideo();		// 测试Rtsp方式请求媒体功能
void TestWriteVideoFile();	// 测试写媒体文件
void TestDecodeCfg();		// 测试解码器配置
void TestModifyDeviceByBroadcast();	// 测试通过广播的方式修改设备信息
void TestNetKeybaord();		// 测试网络键盘
void TestRemSnapPic();		// 测试远程抓图
void TestRegServer();		// 
void TestIOControl();		// 测试IOControl

void TestUpgrade();
//不常用
void TestLoginOut();		// 测试登录，登出

void TestPlayback();
void TestDigiEncode();
void TestGetID();

void TestIPV6();
void Test3GConfig();
void TestModiPsw();

void TestSwitchVideo();
void TestCustomOSD();
void TestUpgradeFromServer();	
void Login();
void Login2();

long g_lLoginID = 0;	// 登录ID
long g_lLoginID2 = 0;	// 登录ID2
int g_start=0;
long g_lUpchannel = 0;
long g_totalSize = 0;

long g_totalCHannel = 0;


long g_Playhandle[32]={0};
#endif // !defined(_SDKDEMO_H_)