📄 axiscamera.cpp
字号:
*buffer = '\n';
}
}
// Set the buffer past the current character ('\n')
++buffer;
}
break;
}
}
// Null terminate.
*buffer = '\0';
return 1;
}
/**
@brief Skips read data until the first empty line.
@param camSock An open tcp socket to the camera to read the data from.
@return sucess 0 if failure; 1 if success
*/
static int CameraSkipUntilEmptyLine(int camSock) {
char buffer[1024];
int success = 0;
while(1) {
success = CameraReadLine(camSock, buffer, sizeof(buffer), true);
if (*buffer == '\0') {
return success;
}
}
return success;
}
/**
@brief Opens a socket.
Issues the given http request with the required added information
and authentication. It cycles through an array of predetermined
encrypted username, password combinations that we expect the users
to have at any point in time. If none of the username, password
combinations work, it outputs a "Unknown user or password" error.
If the request succeeds, it returns the socket number.
@param serverName The information about the host from which this request originates
@param request The request to send to the camera not including boilerplate or
authentication. This is usually in the form of "GET <string>"
@return int - failure = ERROR; success = socket number;
*/
static int CameraOpenSocketAndIssueAuthorizedRequest(const char* serverName, const char* request)
{
char funcName[]="cameraOpenSocketAndIssueAuthorizedRequest";
struct sockaddr_in cameraAddr;
int sockAddrSize;
int camSock = ERROR;
// The camera is expected to have one of the following username, password combinations.
// This routine will return an error if it does not find one of these.
static const char* authenticationStrings[] = {
"RlJDOkZSQw==", /* FRC, FRC */
"cm9vdDpwYXNz", /* root, admin*/
"cm9vdDphZG1pbg==" /* root, pass*/
};
static const int numAuthenticationStrings = sizeof(authenticationStrings)/sizeof(authenticationStrings[0]);
static const char *requestTemplate = "%s " \
"HTTP/1.1\n" \
"User-Agent: HTTPStreamClient\n" \
"Connection: Keep-Alive\n" \
"Cache-Control: no-cache\n" \
"Authorization: Basic %s\n\n";
int i = 0;
for (;i < numAuthenticationStrings;++i) {
char buffer[1024];
sprintf(buffer, requestTemplate, request, authenticationStrings[i]);
/* create camera socket */
//DPRINTF (LOG_DEBUG, "creating camSock" );
if ((camSock = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) {
imaqSetError(ERR_CAMERA_SOCKET_CREATE_FAILED, funcName);
perror ("Failed to create socket");
return (ERROR);
}
sockAddrSize = sizeof (struct sockaddr_in);
bzero ((char *) &cameraAddr, sockAddrSize);
cameraAddr.sin_family = AF_INET;
cameraAddr.sin_len = (u_char) sockAddrSize;
cameraAddr.sin_port = htons (CAMERA_PORT);
if (( (int)(cameraAddr.sin_addr.s_addr = inet_addr (const_cast<char*>(serverName)) ) == ERROR) &&
( (int)(cameraAddr.sin_addr.s_addr = hostGetByName (const_cast<char*>(serverName)) ) == ERROR))
{
imaqSetError(ERR_CAMERA_CONNECT_FAILED, funcName);
return CameraCloseSocket("Failed to get IP, check hostname or IP", camSock);
}
//DPRINTF (LOG_INFO, "connecting camSock" );
if (connect (camSock, (struct sockaddr *) &cameraAddr, sockAddrSize) == ERROR) {
imaqSetError(ERR_CAMERA_CONNECT_FAILED, funcName);
return CameraCloseSocket("Failed to connect to camera - check networ", camSock);
}
//DPRINTF (LOG_DEBUG, "writing GET request to camSock" );
if (write (camSock, buffer, strlen(buffer) ) == ERROR) {
imaqSetError(ERR_CAMERA_CONNECT_FAILED, funcName);
return CameraCloseSocket("Failed to send GET request", camSock);
}
// Read one line with the line ending removed.
if (!CameraReadLine(camSock, buffer, 1024, true)) {
return CameraCloseSocket("Bad response to GET request", camSock);
}
// Check if the response is of the format HTTP/<version> 200 OK.
float discard;
if (sscanf(buffer, "HTTP/%f 200 OK", &discard) == 1) {
break;
}
// We have to close the connection because in the case of failure
// the server closes the connection.
close(camSock);
}
// If none of the attempts were successful, then let the caller know.
if (numAuthenticationStrings == i) {
imaqSetError(ERR_CAMERA_AUTHORIZATION_FAILED, funcName);
perror("Expected username/password combination not found on camera");
return ERROR;
}
return camSock;
}
/**
* @brief Sends a configuration message to the camera
* @param configString configuration message to the camera
* @return success: 0=failure; 1=success
*/
int ConfigureCamera(char *configString){
char funcName[]="ConfigureCamera";
char *serverName = "192.168.0.90"; /* camera @ */
int success = 0;
int camSock = 0;
/* Generate camera configuration string */
char * getStr1 =
"GET /axis-cgi/admin/param.cgi?action=update&ImageSource.I0.Sensor.";
char cameraRequest[strlen(getStr1) + strlen(configString)];
sprintf (cameraRequest, "%s%s", getStr1, configString);
DPRINTF(LOG_DEBUG, "camera configuration string: \n%s", cameraRequest);
camSock = CameraOpenSocketAndIssueAuthorizedRequest(serverName, cameraRequest);
DPRINTF(LOG_DEBUG, "camera socket# = %i", camSock);
//read response
success = CameraSkipUntilEmptyLine(camSock);
//DPRINTF(LOG_DEBUG, "succcess from CameraSkipUntilEmptyLine: %i", success);
char buffer[3]; // set property - 3
success = CameraReadLine(camSock, buffer, 3, true);
//DPRINTF(LOG_DEBUG, "succcess from CameraReadLine: %i", success);
DPRINTF(LOG_DEBUG, "line read from camera \n%s", buffer);
if (strcmp(buffer, "OK") != 0) {
imaqSetError(ERR_CAMERA_COMMAND_FAILURE, funcName);
DPRINTF(LOG_DEBUG, "setting ERR_CAMERA_COMMAND_FAILURE - OK not found");
}
DPRINTF (LOG_INFO, "\nConfigureCamera ENDING success = %i\n", success );
/* clean up */
close (camSock);
return (1);
}
/**
* @brief Sends a request message to the camera
* @param configString request message to the camera
* @param cameraResponse response from camera
* @return success: 0=failure; 1=success
*/
int GetCameraSetting(char *configString, char *cameraResponse){
char *serverName = "192.168.0.90"; /* camera @ */
int success = 0;
int camSock = 0;
/* Generate camera request string */
char * getStr1 =
"GET /axis-cgi/admin/param.cgi?action=list&group=ImageSource.I0.Sensor.";
char cameraRequest[strlen(getStr1) + strlen(configString)];
sprintf (cameraRequest, "%s%s", getStr1, configString);
DPRINTF(LOG_DEBUG, "camera configuration string: \n%s", cameraRequest);
camSock = CameraOpenSocketAndIssueAuthorizedRequest(serverName, cameraRequest);
DPRINTF(LOG_DEBUG, "return from CameraOpenSocketAndIssueAuthorizedRequest %i", camSock);
//read response
success = CameraSkipUntilEmptyLine(camSock);
success = CameraReadLine(camSock, cameraResponse, 1024, true);
DPRINTF(LOG_DEBUG, "succcess from CameraReadLine: %i", success);
DPRINTF(LOG_DEBUG, "line read from camera \n%s", cameraResponse);
DPRINTF (LOG_INFO, "\nGetCameraSetting ENDING success = %i\n", success );
/* clean up */
close (camSock);
return (1);
}
/**
* @brief Sends a request message to the camera for image appearance property
* (resolution, compression, rotation)
* @param configString request message to the camera
* @param cameraResponse response from camera
* @return success: 0=failure; 1=success
*/
int GetImageSetting(char *configString, char *cameraResponse){
char *serverName = "192.168.0.90"; /* camera @ */
int success = 0;
int camSock = 0;
/* Generate camera request string */
char *getStr1 = "GET /axis-cgi/admin/param.cgi?action=list&group=Image.I0.Appearance.";
char cameraRequest[strlen(getStr1) + strlen(configString)];
sprintf (cameraRequest, "%s%s", getStr1, configString);
DPRINTF(LOG_DEBUG, "camera configuration string: \n%s", cameraRequest);
camSock = CameraOpenSocketAndIssueAuthorizedRequest(serverName, cameraRequest);
DPRINTF(LOG_DEBUG, "return from CameraOpenSocketAndIssueAuthorizedRequest %i", camSock);
//read response
success = CameraSkipUntilEmptyLine(camSock);
success = CameraReadLine(camSock, cameraResponse, 1024, true);
DPRINTF(LOG_DEBUG, "succcess from CameraReadLine: %i", success);
DPRINTF(LOG_DEBUG, "line read from camera \n%s", cameraResponse);
DPRINTF (LOG_INFO, "\nGetCameraSetting ENDING success = %i\n", success );
/* clean up */
close (camSock);
return (1);
}
#define MEASURE_SOCKET_TIME 1
/**
* @brief Manage access to the camera. Sets up sockets and reads images
* @param frames Frames per second
* @param compression Camera image compression
* @param resolution Camera image size
* @param rotation Camera image rotation
* @return error
*/
int cameraJPEGServer(int frames, int compression, ImageSize resolution, ImageRotation rotation)
{
char funcName[]="cameraJPEGServer";
char *serverName = "192.168.0.90"; /* camera @ */
cont = 1;
int errorCode = 0;
int printCounter = 0;
int writeIndex;
int authorizeCount = 0;
int authorizeConfirmed = 0;
static const int authenticationStringsCount = 3;
static const char* authenticationStrings[] = {
"cm9vdDphZG1pbg==", /* root, admin*/
"RlJDOkZSQw==", /* FRC, FRC */
"cm9vdDpwYXNz==" /* root, pass*/
};
DPRINTF (LOG_DEBUG, "cameraJPEGServer" );
struct sockaddr_in cameraAddr;
int sockAddrSize;
int camSock = 0;
char resStr[10];
switch (resolution) {
case k640x480: { sprintf(resStr,"640x480"); break; }
case k320x240: { sprintf(resStr,"320x240"); break; }
case k160x120: { sprintf(resStr,"160x120"); break; }
default: {DPRINTF (LOG_DEBUG, "code error - resolution input" ); break; }
}
/* Generate camera initialization string */
/* changed resolution to 160x120 from 320x240 */
/* supported resolutions are: 640x480, 640x360, 320x240, 160x120 */
char * getStr1 =
"GET /axis-cgi/mjpg/video.cgi?showlength=1&camera=1&";
char insertStr[100];
sprintf (insertStr, "des_fps=%i&compression=%i&resolution=%s&rotation=%i",
frames, compression, resStr, (int)rotation);
char * getStr2 = " HTTP/1.1\n\
User-Agent: HTTPStreamClient\n\
Host: 192.150.1.100\n\
Connection: Keep-Alive\n\
Cache-Control: no-cache\n\
Authorization: Basic %s;\n\n";
char getStr[strlen(getStr1) + strlen(insertStr) + strlen(getStr2)];
sprintf (getStr, "%s:%s%s", getStr1, insertStr, getStr2);
DPRINTF(LOG_DEBUG, "revised camera string: \n%s", getStr);
/* Allocation */
char tempBuffer[1024];
RETRY:
while (globalCamera.acquire == 0) Wait(0.1);
if (!authorizeConfirmed){
if (authorizeCount < authenticationStringsCount){
sprintf (tempBuffer, getStr, authenticationStrings[authorizeCount]);
} else {
imaqSetError(ERR_CAMERA_AUTHORIZATION_FAILED, funcName);
perror ("Camera authorization failed ... Incorrect password on camera!!");
return (ERROR);
}
}
while (1)
{
globalCamera.cameraMetrics[CAM_SOCKET_INIT_ATTEMPTS]++;
/* create camera socket */
DPRINTF (LOG_DEBUG, "creating camSock" );
if ((camSock = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) {
imaqSetError(ERR_CAMERA_SOCKET_CREATE_FAILED, funcName);
perror ("Failed to create socket");
cont = 0;
return (ERROR);
}
sockAddrSize = sizeof (struct sockaddr_in);
bzero ((char *) &cameraAddr, sockAddrSize);
cameraAddr.sin_family = AF_INET;
cameraAddr.sin_len = (u_char) sockAddrSize;
cameraAddr.sin_port = htons (CAMERA_PORT);
DPRINTF (LOG_DEBUG, "getting IP" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -