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