00001 #include "EventBase.h"
00002 #include <stdio.h>
00003 #include <sstream>
00004 #include <libxml/tree.h>
00005 #include "Shared/debuget.h"
00006
00007 const char* const EventBase::EventGeneratorNames[numEGIDs] = {
00008 "unknownEGID",
00009 "aiEGID",
00010 "audioEGID",
00011 "buttonEGID",
00012 "erouterEGID",
00013 "estopEGID",
00014 "locomotionEGID",
00015 "micOSndEGID",
00016 "micRawEGID",
00017 "micFFTEGID",
00018 "motmanEGID",
00019 "powerEGID",
00020 "sensorEGID",
00021 "stateMachineEGID",
00022 "stateTransitionEGID",
00023 "textmsgEGID",
00024 "timerEGID",
00025 "visOFbkEGID",
00026 "visRawCameraEGID",
00027 "visInterleaveEGID",
00028 "visJPEGEGID",
00029 "visSegmentEGID",
00030 "visRLEEGID",
00031 "visRegionEGID",
00032 "visObjEGID",
00033 "wmVarEGID",
00034 "worldModelEGID",
00035 };
00036
00037 const char* const EventBase::EventTypeNames[numETIDs] = {
00038 "activate",
00039 "status",
00040 "deactivate"
00041 };
00042
00043 const char* const EventBase::EventTypeAbbr[numETIDs] = { "A", "S", "D" };
00044
00045 EventBase::EventBase()
00046 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(unknownEGID), typeID(statusETID), sourceID((unsigned int)-1), duration(0)
00047 {
00048 genName();
00049 }
00050
00051 EventBase::EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur)
00052 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), duration(dur)
00053 {
00054 genName();
00055 if(tid==deactivateETID)
00056 setMagnitude(0.0);
00057 else
00058 setMagnitude(1.0);
00059 }
00060
00061 EventBase::EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n)
00062 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), duration(dur)
00063 {
00064 setName(n);
00065 if(tid==deactivateETID)
00066 setMagnitude(0.0);
00067 else
00068 setMagnitude(1.0);
00069 }
00070
00071 EventBase::EventBase(EventGeneratorID_t gid, unsigned int sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag)
00072 : XMLLoadSave(), stim_id(), magnitude(mag), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), duration(dur)
00073 {
00074 setName(n);
00075 }
00076
00077 EventBase&
00078 EventBase::setName(const std::string& sourcename) {
00079 stim_id='(';
00080 if(genID<numEGIDs) {
00081 stim_id+=EventGeneratorNames[genID];
00082 } else {
00083 stim_id+=std::string("InvalidGen");
00084 char tmp2[16];
00085 snprintf(tmp2,16,"(%d)",genID);
00086 stim_id+=tmp2;
00087 }
00088 stim_id+=',';
00089 stim_id+=sourcename;
00090 stim_id+=',';
00091 stim_id+=EventTypeAbbr[getTypeID()];
00092 stim_id+=')';
00093 nameisgen=false;
00094 return *this;
00095 }
00096
00097 std::string
00098 EventBase::getDescription(bool , unsigned int verbosity) const {
00099 std::ostringstream logdata;
00100 logdata << getName();
00101 if(verbosity>=1)
00102 logdata << '\t' << getGeneratorID() << '\t' << getSourceID() << '\t' << getTypeID();
00103 if(verbosity>=2)
00104 logdata << '\t' << getDuration() << '\t' << getTimeStamp();
00105 if(verbosity>=3)
00106 logdata << '\t' << getMagnitude();
00107 return logdata.str();
00108 }
00109
00110 unsigned int
00111 EventBase::getBinSize() const {
00112 if(saveFormat==XML)
00113 return XMLLoadSave::getBinSize();
00114 unsigned int used=0;
00115 used+=creatorSize("EventBase");
00116 used+=stim_id.size()+stringpad;
00117 used+=sizeof(magnitude);
00118 used+=sizeof(timestamp);
00119 used+=sizeof(nameisgen);
00120 used+=sizeof(char);
00121 used+=sizeof(char);
00122 used+=sizeof(sourceID);
00123 used+=sizeof(duration);
00124 return used;
00125 }
00126
00127 unsigned int
00128 EventBase::LoadBinaryBuffer(const char buf[], unsigned int len) {
00129 unsigned int origlen=len;
00130 unsigned int used=0;
00131 if(0==(used=checkCreator("EventBase",buf,len,true))) return 0;
00132 len-=used; buf+=used;
00133 if(0==(used=decode(stim_id,buf,len))) return 0;
00134 len-=used; buf+=used;
00135 if(0==(used=decode(magnitude,buf,len))) return 0;
00136 len-=used; buf+=used;
00137 if(0==(used=decode(timestamp,buf,len))) return 0;
00138 len-=used; buf+=used;
00139 if(0==(used=decode(nameisgen,buf,len))) return 0;
00140 len-=used; buf+=used;
00141 char tmp;
00142 if(0==(used=decode(tmp,buf,len))) return 0;
00143 genID=(EventGeneratorID_t)tmp;
00144 len-=used; buf+=used;
00145 if(0==(used=decode(tmp,buf,len))) return 0;
00146 typeID=(EventTypeID_t)tmp;
00147 len-=used; buf+=used;
00148 if(0==(used=decode(sourceID,buf,len))) return 0;
00149 len-=used; buf+=used;
00150 if(0==(used=decode(duration,buf,len))) return 0;
00151 len-=used; buf+=used;
00152 return origlen-len;
00153 }
00154
00155 unsigned int
00156 EventBase::SaveBinaryBuffer(char buf[], unsigned int len) const {
00157 unsigned int origlen=len;
00158 unsigned int used=0;
00159 if(0==(used=saveCreator("EventBase",buf,len))) return 0;
00160 len-=used; buf+=used;
00161 if(0==(used=encode(stim_id,buf,len))) return 0;
00162 len-=used; buf+=used;
00163 if(0==(used=encode(magnitude,buf,len))) return 0;
00164 len-=used; buf+=used;
00165 if(0==(used=encode(timestamp,buf,len))) return 0;
00166 len-=used; buf+=used;
00167 if(0==(used=encode(nameisgen,buf,len))) return 0;
00168 len-=used; buf+=used;
00169 if(0==(used=encode((char)genID,buf,len))) return 0;
00170 len-=used; buf+=used;
00171 if(0==(used=encode((char)typeID,buf,len))) return 0;
00172 len-=used; buf+=used;
00173 if(0==(used=encode(sourceID,buf,len))) return 0;
00174 len-=used; buf+=used;
00175 if(0==(used=encode(duration,buf,len))) return 0;
00176 len-=used; buf+=used;
00177 return origlen-len;
00178 }
00179
00180 void EventBase::LoadXML(xmlNode* node) {
00181 if(xmlStrcmp(node->name, (const xmlChar *)"event"))
00182 throw bad_format(node,"Load of the wrong type -- expecting 'event' node");
00183 unsigned int i;
00184 xmlChar* str = xmlGetProp(node,(const xmlChar*)"egid");
00185 if(str==NULL)
00186 throw bad_format(node,"missing generator id");
00187 for(i=0; i<numEGIDs; i++) {
00188 if(xmlStrcmp(str,(const xmlChar*)EventGeneratorNames[i])==0)
00189 break;
00190 }
00191 xmlFree(str);
00192 if(i==numEGIDs)
00193 throw bad_format(node,"bad event generator name");
00194 genID=static_cast<EventGeneratorID_t>(i);
00195 str = xmlGetProp(node,(const xmlChar*)"sid");
00196 if(str==NULL)
00197 throw bad_format(node,"missing source id");
00198 sourceID=atoi((const char*)str);
00199 xmlFree(str);
00200 str = xmlGetProp(node,(const xmlChar*)"etid");
00201 if(str==NULL)
00202 throw bad_format(node,"missing type id");
00203 for(i=0; i<numETIDs; i++) {
00204 if(xmlStrcmp(str,(const xmlChar*)EventTypeAbbr[i])==0)
00205 break;
00206 if(xmlStrcmp(str,(const xmlChar*)EventTypeNames[i])==0)
00207 break;
00208 }
00209 xmlFree(str);
00210 if(i==numETIDs)
00211 throw bad_format(node,"bad event type name");
00212 typeID=static_cast<EventTypeID_t>(i);
00213 str = xmlGetProp(node,(const xmlChar*)"time");
00214 if(str==NULL)
00215 throw bad_format(node,"missing timestamp");
00216 timestamp=atoi((const char*)str);
00217 xmlFree(str);
00218 str = xmlGetProp(node,(const xmlChar*)"duration");
00219 if(str==NULL)
00220 throw bad_format(node,"missing duration");
00221 duration=atoi((const char*)str);
00222 xmlFree(str);
00223 str = xmlGetProp(node,(const xmlChar*)"magnitude");
00224 if(str==NULL)
00225 throw bad_format(node,"missing magnitude");
00226 magnitude=atoi((const char*)str);
00227 xmlFree(str);
00228 str = xmlGetProp(node,(const xmlChar*)"custom_name");
00229 if(str!=NULL) {
00230 setName((const char*)str);
00231 xmlFree(str);
00232 } else
00233 genName();
00234 }
00235 void EventBase::SaveXML(xmlNode * node) const {
00236 xmlNodeSetName(node,(const xmlChar*)"event");
00237 xmlSetProp(node,(const xmlChar*)"egid",(const xmlChar*)EventGeneratorNames[genID]);
00238 char buf[20];
00239 snprintf(buf,20,"%x",sourceID);
00240 xmlSetProp(node,(const xmlChar*)"sid",(const xmlChar*)buf);
00241 xmlSetProp(node,(const xmlChar*)"etid",(const xmlChar*)EventTypeAbbr[typeID]);
00242 snprintf(buf,20,"%u",timestamp);
00243 xmlSetProp(node,(const xmlChar*)"time",(const xmlChar*)buf);
00244 snprintf(buf,20,"%u",duration);
00245 xmlSetProp(node,(const xmlChar*)"duration",(const xmlChar*)buf);
00246 snprintf(buf,20,"%g",magnitude);
00247 xmlSetProp(node,(const xmlChar*)"magnitude",(const xmlChar*)buf);
00248 if(!nameisgen) {
00249
00250 size_t b=stim_id.find(',');
00251 ASSERTRET(b!=std::string::npos,"malformed event name! (no comma)");
00252 size_t e=stim_id.find(',',++b);
00253 ASSERTRET(e!=std::string::npos,"malformed event name! (no 2nd comma)");
00254 xmlSetProp(node,(const xmlChar*)"custom_name",(const xmlChar*)stim_id.substr(b,e-b).c_str());
00255 }
00256 }
00257
00258 void
00259 EventBase::genName() {
00260 if(!nameisgen)
00261 return;
00262 char tmp[16];
00263 snprintf(tmp,16,"%d",sourceID);
00264 setName(tmp);
00265 nameisgen=true;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278