// GVCPControl.cpp: implementation of the GVCPControl class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "gigeimagegraber.h"
#include "GVCPControl.h"
#include "stdio.h"
#include "SystemService.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
GVCPControl::GVCPControl()
:currentCamera(NULL)
{
udpstream.Init();
//Test();
}
GVCPControl::~GVCPControl()
{
}
ErrorCode GVCPControl::GrabOne()
{
return GrabOneFromHW();
}
CamerInfo* GVCPControl::RetrieveCameraInfo(const unsigned char * const pdata,const int payload_len)
{
CamerInfo &cinfoTmp = CameraInfo;
memcpy(cinfoTmp.szCameraName,pdata+0x68,20);
memcpy(&cinfoTmp.ipAdress.L,pdata+0x0024,4);
memcpy(cinfoTmp.macAdress.CA,pdata+0x0008,8);
printf("\nCameraName:\t%s",cinfoTmp.szCameraName);
strcpy(SystemService::CameraName,cinfoTmp.szCameraName);
printf("\nCamera MAC:\t%02x %02x %02x %02x %02x %02x"
,cinfoTmp.macAdress.CA[2]
,cinfoTmp.macAdress.CA[3]
,cinfoTmp.macAdress.CA[4]
,cinfoTmp.macAdress.CA[5]
,cinfoTmp.macAdress.CA[6]
,cinfoTmp.macAdress.CA[7]);
cinfoTmp.ipAdress.L = ntohl(cinfoTmp.ipAdress.L);
printf("\nCamera IP:\t%02x %02x %02x %02x"
,cinfoTmp.ipAdress.CA[0]
,cinfoTmp.ipAdress.CA[1]
,cinfoTmp.ipAdress.CA[2]
,cinfoTmp.ipAdress.CA[3]);
return &cinfoTmp;
}
CamerInfo* GVCPControl::SearchACamera()
{
int retryCnt;
retryCnt = 30;
while (retryCnt--)
{
udpstream.SendPacketTypeListen();
udpstream.TimerStart(200);
while(!udpstream.TimerCheckIfIsOver())
{
//read udp
gvcp_packet *packtRecved = udpstream.RecieveOnePacket();
if (packtRecved != NULL)
{
//got a valid upd packet, check if a valid gvcp discovery response
register unsigned long cmd_type = packtRecved->head.cmd_type;
register unsigned short payload_len = packtRecved->head.payload_len;
const unsigned char * const pdata = (unsigned char*)(&packtRecved->data);
if (cmd_type == 0x00000003 && payload_len == 0x00f8)
{
//an valid gvcp discovery reponse, retrieve the camera infomation
CamerInfo *cmInfo = RetrieveCameraInfo(pdata,payload_len);
ValidateSWAutherization(cmInfo);
return cmInfo;
}
}
Sleep(1);
}
}
return NULL;
}
ErrorCode GVCPControl::GrabOneFromHW()
{
ErrorCode errorCode;
//search
if (!currentCamera && !(currentCamera = SearchACamera()))
return ecCameraSeachingTimeout;
//setup ip
if (currentCamera->ipAdress.L != defaulCameraIP)
{
//setup the camera ip adress
udpstream.SendPacketTypeSetIP(currentCamera->macAdress.CA,defaulCameraIP,defaulCameraIPMask);
if(!(currentCamera = SearchACamera())
&& currentCamera->ipAdress.L != defaulCameraIP)
{
return ecCameraSetIPFailed;
}
}
udpstream.SendHeartBeat();
//if not connected try connect
unsigned long dat;
errorCode = udpstream.GVCPGetValue(0x0a00,dat);
if (ecNoError != errorCode)
{
currentCamera = NULL;
strcpy(SystemService::CameraName,"------- ");
SystemService::CameraStatues = 0;
return errorCode;
}
if (dat!=0x02)
{
//not connect ,try to connect
errorCode = udpstream.GVCPSetValue(0x0a00,0x02);
if (errorCode != ecNoError)
{
return errorCode;
}
else
{
////connected SetVals
errorCode = udpstream.GVCPSetValue(0x0938,0x0bb8);//set heart beat,0xbb8 is from labview sniffer file
udpstream.SendHeartBeat();
errorCode = udpstream.GVCPSetValue(0xbb34,1);//frames to aquire 1
errorCode = udpstream.GVCPSetValue(0xd310,0x02);//aquire mode = continius
errorCode = udpstream.GVCPSetValue(0xe8a4,0x0180);//gain raw
errorCode = udpstream.GVCPSetValue(0xe904,0x01);//Bining
errorCode = udpstream.GVCPSetValue(0xe908,0x02);//Shutter Mode
errorCode = udpstream.GVCPSetValue(0xe90c,0x01);//Work Mode
errorCode = udpstream.GVCPSetValue(0xd32c,0x00);//DeviceScanType
//ip setup ignored @ 0x0014
errorCode = udpstream.GVCPSetValue(0xd304,0x0400);//height 1024
errorCode = udpstream.GVCPSetValue(0xd300,0x0400);//width 1024
errorCode = udpstream.GVCPSetValue(0xd31c,0x04);//OffsetX
errorCode = udpstream.GVCPSetValue(0xd320,0x04);//OffsetY
errorCode = udpstream.GVCPSetValue(0xd324,0x00);//SensorDigitalizationTAP
//errorCode = udpstream.GVCPSetValue(0xd33c,0x80000000);//turn on test patten
errorCode = udpstream.GVCPSetValue(0xd33c,0x00000000);//turn off test patten
errorCode = udpstream.GVCPSetValue(0x0d08,0);//GevSCPD
errorCode = udpstream.GVCPSetValue(0x0d04,0x40001f40);//GevSCPSPacketSize
errorCode = udpstream.GVCPSetValue(0x0d04,0x40000000+1024);//GevSCPSPacketSize
errorCode = udpstream.GVCPSetValue(0x0d18,0xc0a80201);//GevSCDA
errorCode = udpstream.GVCPSetValue(0x0d00,0x00000ba6);//GevSCPHostPort
if (errorCode != ecNoError)
{
return ecCameraSetVarFailed;
}
}
}
SystemService::CameraStatues = 1;
////connected SetVals
udpstream.SendHeartBeat();
// printf("\nstarted capture image");
udpstream.GVCPSetValue(0xd314,0x01);//start
// udpstream.GVCPSetValue(0xd318,0x01);//stop
// printf("\ncapture image stopped");
return ecNoError;
}
ErrorCode GVCPControl::GrabOneFromSimulated()
{
return ecNoError;
}
#include "des.h"
BOOL GVCPControl::CheckIfCameraValidated(unsigned char *MacAddressMSBFirst)
{
char sz[16],encyptedCode[64];
char skey[8]={1,0,0,1,0,0,0,0};
char inbuff[8],oubuff[8];
FILE *fh;
//macAddress to Hash File Name
sprintf(sz,".\\%02x%02x%02x%02x%02x%02x.lic"
,MacAddressMSBFirst[2]
,MacAddressMSBFirst[3]
,MacAddressMSBFirst[4]
,MacAddressMSBFirst[5]
,MacAddressMSBFirst[6]
,MacAddressMSBFirst[7]
);
MacAddressMSBFirst[0] = MacAddressMSBFirst[1] = 0;
//try to get the file content
fh = fopen(sz,"r+");
if (NULL == fh && NULL== (fh = fopen(sz,"w+")))
{
return false;
}
for(int i = 0 ; i < 8 ; i++)
{
int tmp;
if (fscanf(fh,"%X",&tmp)<=0)
{
fclose(fh);
return false;
}
inbuff[i] = tmp;
}
fclose(fh);
//decrytion
{
DES jm ;
jm.Des_Go(oubuff, inbuff, sizeof(inbuff),(const char*)skey, sizeof(skey), DECRYPT);
for(int i = 0 ; i < 8 ; i++)
{
if ((unsigned char)oubuff[i] != MacAddressMSBFirst[i] )
break;
}
if (i == 8)
{
return true;
}
}
return false;
}
void GVCPControl::ValidateSWAutherization(CamerInfo* cameraInfo)
{
SystemService::IsSWAutherized = 0;
SystemService::currentCamera = cameraInfo;
if (cameraInfo == NULL)
{
return;
}
//get cammera ID and check if valid;
if (CheckIfCameraValidated(cameraInfo->macAdress.CA))
{
SystemService::IsSWAutherized = 1;
}
return ;
}
void GVCPControl::Test()
{
unsigned char camAddress[8]={0x00,0x00,0x00,0x11,0x1c,0x00,0x38,0xe5};
memcpy(CameraInfo.macAdress.CA,camAddress,8);
ValidateSWAutherization(&CameraInfo);
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页