query_handlers.c from EmStar at Krugle
Show query_handlers.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "emrun/emrun.h"
#include "link/link.h"
#include <link/link_headers.h>
#include "libmisc/misc.h"
#include <libdev/status_dev.h>
//#include "connectsock.h"
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "camctrl_i.h"
#include "camdefs.h"
#define PTZF_QUERY_BEGINNING_FRAME "9050"
#define PTZF_QUERY_ENDING_FRAME "FF"
#define PTZF_QUERY_REPLY_MIN_LENGTH 4
#define PAN_SIZE 4
#define TILT_SIZE 4
#define ZOOM_SIZE 4
// offsets from the beginning of the reply string
// offsets are HORRIBLE...but strings are horrible too...
#define PAN_OFFSET 4
#define TILT_OFFSET 12
#define ZOOM_OFFSET 4
#define INT_BASE 16
#define PAN_VTD 14.4
#define TILT_VTD 14.4
float convert_pan_val_to_deg(int16_t pan_val)
{
return ((float)pan_val / PAN_VTD);
}
float convert_tilt_val_to_deg(int16_t tilt_val)
{
return ((float)(tilt_val) / TILT_VTD);
}
// check that the first two bytes are correct
int check_frame(char *buf, char *frame, int size)
{
return (strncmp(buf, frame, size));
}
void build_buf(char *src_buf, char *dst_buf, int offset, int bufsize)
{
int i=0;
int j=0;
char *tmp=src_buf+offset; // pointer to the buf, offset by the
// right amount
for (j=0; j<bufsize; j++) {
if ((i%2) ==0) {
// if i is even, skip it and go to the next
i++;
}
// now assign the values
dst_buf[j]=tmp[i];
// increment i
i++;
}
}
void handle_ptpos_query(void *ptr, int32_t length, cam_state_t *cstate)
{
char *reply_buf = (char *)ptr;
// add 1 to the buf sizes to make them 0-terminated strings
char pan_buf[PAN_SIZE+1]={0};
char tilt_buf[TILT_SIZE+1]={0};
if (length<PTZF_QUERY_REPLY_MIN_LENGTH) {
elog(LOG_ERR, "Length too short: %d, min is %d", length,
PTZF_QUERY_REPLY_MIN_LENGTH);
exit(1);
}
if (check_frame(reply_buf, PTZF_QUERY_BEGINNING_FRAME,
strlen(PTZF_QUERY_BEGINNING_FRAME))!=0) {
elog(LOG_ERR, "Invalid PTZF beginning frame (%s)", reply_buf);
}
// PTPOS query reply format is:
// 4 framing bytes
// 8 bytes pan, in 0p format
// 8 bytes tilt, in 0t format
// 2 framing bytes
//
// The replies are STRINGS, so each byte is a printable character
// This means I need to first build the string, then use atoi to
// make it an integer
build_buf(reply_buf, pan_buf, PAN_OFFSET, PAN_SIZE);
build_buf(reply_buf, tilt_buf, TILT_OFFSET, TILT_SIZE);
cstate->cam_status.pan_value=(int16_t)strtol(pan_buf, NULL, INT_BASE);
cstate->cam_status.tilt_value=(int16_t)strtol(tilt_buf, NULL, INT_BASE);
cstate->cam_status.pan_degrees =
convert_pan_val_to_deg(cstate->cam_status.pan_value);
cstate->cam_status.tilt_degrees =
convert_tilt_val_to_deg(cstate->cam_status.tilt_value);
#if 0
// copy the status struct to the request struct
memcpy(&cstate->cmd_request, &cstate->cam_status,
sizeof(cstate->cam_status));
#endif
g_status_dev_notify(cstate->cam_status_ev);
}
// c-p from ptpos....sigh
void handle_zpos_query(void *ptr, int32_t length, cam_state_t *cstate)
{
char *reply_buf = (char *)ptr;
// add 1 to the buf sizes to make them 0-terminated strings
char zoom_buf[ZOOM_SIZE+1]={0};
// int8_t num_tasks=0;
if (length<PTZF_QUERY_REPLY_MIN_LENGTH) {
elog(LOG_ERR, "Length too short: %d, min is %d", length,
PTZF_QUERY_REPLY_MIN_LENGTH);
exit(1);
}
if (check_frame(reply_buf, PTZF_QUERY_BEGINNING_FRAME,
strlen(PTZF_QUERY_BEGINNING_FRAME))!=0) {
elog(LOG_ERR, "Invalid PTZF beginning frame (%s)", reply_buf);
}
// PTPOS query reply format is:
// 4 framing bytes
// 8 bytes zoom, in 0p format
// 2 framing bytes
//
// The replies are STRINGS, so each byte is a printable character
// This means I need to first build the string, then use atoi to
// make it an integer
build_buf(reply_buf, zoom_buf, ZOOM_OFFSET, ZOOM_SIZE);
cstate->cam_status.zoom_value=(int16_t)strtol(zoom_buf, NULL, INT_BASE);
#if 0
// copy the status struct to the request struct
// SAVE the num_tasks in the request struct!!!
num_tasks = cstate->cmd_request.num_tasks;
memcpy(&cstate->cmd_request, &cstate->cam_status,
sizeof(cstate->cam_status));
memcpy(&cstate->cmd_request.num_tasks, &num_tasks, sizeof(num_tasks));
#endif
g_status_dev_notify(cstate->cam_status_ev);
}
void handle_all_inquiry(void *ptr, int32_t length, cam_state_t *cstate)
{
buf_t **buf = &cstate->inq_all_status_buf;
// clear out any old stuff
if (*buf!=NULL) {
buf_free(*buf);
*buf=NULL;
}
// get a new buf
*buf = buf_new();
// copy stuff over
bufprintf(*buf, (char *)ptr, length);
g_status_dev_notify(cstate->inq_status_ev);
}
See more files for this project here