Go to the documentation of this file.00001 #include <sys/time.h>
00002 #include <assert.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <stdio.h>
00006
00007 #include "canon_vbc50i/libCanon/Videogram.h"
00008
00009
00010 VideoGram::VideoGram(unsigned int id)
00011 {
00012 timestamp = 0;
00013 size = 8;
00014 data = (unsigned char*)malloc(size);
00015 assert(data != NULL);
00016 data[0] = id;
00017 bzero(data+1,size-1);
00018 }
00019
00020 VideoGram::~VideoGram()
00021 {
00022 free(data);
00023 }
00024
00025 void VideoGram::reset()
00026 {
00027 assert(data != NULL);
00028 timestamp = 0;
00029 bzero(data,size);
00030 }
00031
00032 void VideoGram::setId(unsigned int id)
00033 {
00034 data[0] = id;
00035 }
00036
00037 unsigned int VideoGram::getId()
00038 {
00039 return data[0];
00040 }
00041
00042 void VideoGram::setData(unsigned char * s, unsigned int l)
00043 {
00044 size = 8 + l;
00045 data = (unsigned char *)realloc(data,size);
00046 assert(data != NULL);
00047 memcpy(data+8,s,l);
00048 data[4] = (l >> 24) & 0xFF;
00049 data[5] = (l >> 16) & 0xFF;
00050 data[6] = (l >> 8) & 0xFF;
00051 data[7] = (l >> 0) & 0xFF;
00052 }
00053
00054 unsigned int VideoGram::getSize()
00055 {
00056 return size;
00057 }
00058
00059 unsigned int VideoGram::getLength()
00060 {
00061 return size - 8;
00062 }
00063
00064 unsigned char * VideoGram::getData()
00065 {
00066 return data + 8;
00067 }
00068
00069 const unsigned char * VideoGram::getData() const
00070 {
00071 return data + 8;
00072 }
00073
00074
00075 bool VideoGram::send(Socket * sock) const
00076 {
00077 return sock->SendAll(data,size,100);
00078 }
00079
00080
00081 bool VideoGram::receive(Socket * sock, unsigned int timeout_milli)
00082 {
00083 reset();
00084 if (!sock->WaitBuffer(data,8,timeout_milli)) {
00085 return false;
00086 }
00087 struct timeval tv;
00088 gettimeofday(&tv,NULL);
00089 timestamp = tv.tv_sec + tv.tv_usec*1e-6;
00090
00091 unsigned int l = 0;
00092 l = (l<<8) + data[4];
00093 l = (l<<8) + data[5];
00094 l = (l<<8) + data[6];
00095 l = (l<<8) + data[7];
00096 if (data[0] == 0x02) {
00097 if (l > 2e6) return false;
00098 } else {
00099 if (l > 1e3) return false;
00100 }
00101 size = l + 8;
00102 data = (unsigned char *)realloc(data,size);
00103 assert(data != NULL);
00104 if (!sock->WaitBuffer(data+8,l,timeout_milli)) {
00105 return false;
00106 }
00107 return true;
00108 }
00109
00110 void VideoGram::print(FILE * fp)
00111 {
00112 unsigned int i;
00113 fprintf(fp,"Id %02X [%02X %02X %02X] L %X : [ ",data[0],data[1],data[2],data[3],size-8);
00114 for (i=8;i<size;i++) {
00115 fprintf(fp,"%02X ",data[i]);
00116 }
00117 fprintf(fp,"]\n");
00118 }
00119
00120 double VideoGram::getTimeStamp()
00121 {
00122 return timestamp;
00123 }
00124
00125