std::unique_ptr<RamValue> translateValue(const AstArgument* arg, const ValueIndex& index = ValueIndex()) { std::unique_ptr<RamValue> val; if (!arg) return val; if (const AstVariable *var = dynamic_cast<const AstVariable *>(arg)) { ASSERT(index.isDefined(*var) && "variable not grounded"); const Location& loc = index.getDefinitionPoint(*var); val = std::unique_ptr<RamValue>(new RamElementAccess(loc.level, loc.component, loc.name)); } else if (dynamic_cast<const AstUnnamedVariable*>(arg)) { return nullptr; // utilized to identify _ values } else if (const AstConstant *c = dynamic_cast<const AstConstant *>(arg)) { val = std::unique_ptr<RamValue>(new RamNumber(c->getIndex())); } else if (const AstUnaryFunctor *uf = dynamic_cast<const AstUnaryFunctor *>(arg)) { switch(uf->getFunction()) { case AstUnaryFunctor::ORDINAL: val = std::unique_ptr<RamValue>(new RamOrd(translateValue(uf->getOperand(), index))); break; case AstUnaryFunctor::NEGATION: val = std::unique_ptr<RamValue>(new RamNegation(translateValue(uf->getOperand(), index))); break; default: ASSERT(false && "unknown unary function"); } } else if (const AstBinaryFunctor *bf = dynamic_cast<const AstBinaryFunctor *>(arg)) { val = std::unique_ptr<RamValue>(new RamBinaryOperator(bf->getFunction(), translateValue(bf->getLHS(), index), translateValue(bf->getRHS(), index))); } else if (dynamic_cast<const AstCounter *>(arg)){ val = std::unique_ptr<RamValue>(new RamAutoIncrement()); } else if (const AstRecordInit* init = dynamic_cast<const AstRecordInit*>(arg)) { std::vector<std::unique_ptr<RamValue>> values; for(const auto& cur : init->getArguments()) { values.push_back(translateValue(cur, index)); } val = std::unique_ptr<RamValue>(new RamPack(std::move(values))); } else if (const AstAggregator* agg = dynamic_cast<const AstAggregator*>(arg)) { // here we look up the location the aggregation result gets bound auto loc = index.getAggregatorLocation(*agg); val = std::unique_ptr<RamValue>(new RamElementAccess(loc.level, loc.component, loc.name)); } else { std::cout << "Unsupported node type of " << arg << ": " << typeid(*arg).name() << "\n"; ASSERT(false && "unknown AST node type not permissible"); } return val; }
int AbstractMotor::scalePos(int val, bool up) { if (up) { if (val < 0 or val > pow(2, bits)) { throw std::invalid_argument( "Value " + std::to_string(val) + " is not in the range 0, " + std::to_string(pow(2, bits) - 1)); } return translateValue(val, 0, (int) (pow(2, bits) - 1), rawLow, rawHigh); } else { if (val < rawLow) { val = rawLow; } else if (val > rawHigh) { val = rawHigh; } return translateValue(val, rawLow, rawHigh, 0, (int) (pow(2, bits) - 1)); } }
/* * Translate an SNMP V1 or V2 Trap to a NodeBrain ALERT command * * alert '<oid>'=<value>,...; * * Here's an example of a full alert with a single <variable-binding>. * * *** include example here *** */ static char *translate(NB_MOD_Snmptrap *snmptrap,unsigned char *buf,int len,char *cmd,int cmdlen,char **handlerNameP){ char *cmdcur=cmd; char *cmdend=cmdcur+cmdlen; unsigned char *cursor=buf; unsigned char *bufend=buf+len; unsigned char *enterpriseOid; int objlen,generic; char *msg,version,senderAddr[40]; char trapOID[64],*trapOIDCur; // value of '1.3.6.1.6.3.1.1.4.1.0' (SNMP_TRAP_OID) //char *handler; nbCELL cell; char *oid,*value,*syntax,*attribute; fprintf(stderr,"translate: called\n"); fflush(stderr); *trapOID=0; if(*cursor!=0x30) return("packet not recognized"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("trap length error"); if(*cursor!=0x02 || *(cursor+1)!=0x01) return("expecting 02.01 to start trap"); cursor+=2; version=*cursor; cursor++; sprintf(cmdcur,"alert '1.3.6.1.6.3.18.1.4'="); // snmpTrapCommunity OID cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 976-0.8.13-3 FP but replaced strchr(cmdcur,0) if(*cursor!=0x04) return("expecting type 04 (string) for community string"); if(NULL!=(msg=translateValue(&cursor,bufend,&cmdcur,cmdend,""))) return(msg); fprintf(stderr,"translate: checking version specific stuff\n"); fflush(stderr); switch(version){ case 0: /* snmpV1 trap */ if(*cursor!=0xA4) return("expecting 0xA4 for V1 trap"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("buffer length confusion"); if(*cursor!=0x06) return("expecting 0x06 for enterprise OID"); strcpy(cmdcur,",'1.3.6.1.6.3.1.1.4.3'="); cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 4157-0.8.13-3 FP but replaced strchr(cmdcur,0) enterpriseOid=cursor; if(NULL!=(msg=translateValue(&cursor,bufend,&cmdcur,cmdend,""))) return(msg); if(*cursor!=0x40) return("expecting 0x40 for address"); strcpy(cmdcur,",'1.3.6.1.6.3.18.1.3'="); cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 4165-0.8.13-3 FP but replaced strchr(cmdcur,0) if(NULL!=(msg=translateValue(&cursor,bufend,&cmdcur,cmdend,""))) return(msg); if(*cursor!=0x02) return("expecting integer for trap generic type"); cursor++; if(*cursor>1) return("generic trap type length error - expecting 1"); cursor++; generic=*cursor; cursor++; if(*cursor!=0x02) return("expecting integer for trap specific type"); strcpy(cmdcur,",'1.3.6.1.6.3.1.1.4.1.0'="); // snmpTrapOID.0 cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 4162-0.8.13-3 FP but replaced strchr(cmdcur,0) if(generic!=6){ sprintf(cmdcur,"\"1.3.6.1.6.3.1.1.5.%d\"",(*cursor)+1); // see RFC 3584 cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 4156-0.8.13-3 FP but replaced strchr(cmdcur,0) cursor++; objlen=getObjectLength(&cursor,bufend); // step over trap specific type if(objlen<0) return("buffer length confusion"); } else{ trapOIDCur=cmdcur+1; //save location of trapOId if(NULL!=(msg=translateValue(&enterpriseOid,bufend,&cmdcur,cmdend,""))) return(msg); cmdcur--; // back up over ending quote to extend this enterprise oid strcpy(cmdcur,".0."); cmdcur+=3; if(NULL!=(msg=translateValue(&cursor,bufend,&cmdcur,cmdend,""))) return(msg); *trapOID='\''; objlen=cmdcur-trapOIDCur; strncpy(trapOID+1,trapOIDCur,objlen); *(trapOID+objlen+1)='\''; *(trapOID+objlen+2)=0; *cmdcur='"'; cmdcur++; } if(*cursor!=0x43) return("expecting 0x43 for uptime"); strcpy(cmdcur,",'1.3.6.1.2.1.1.3.0'="); cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 966-0.8.13-3 FP but replaced strchr(cmdcur,0) if(NULL!=(msg=translateValue(&cursor,bufend,&cmdcur,cmdend,""))) return(msg); break; case 1: /* snmpV2 trap */ if(*cursor!=0xA7) return("expecting 0xA7 for trap"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("buffer length confusion"); if(*cursor!=0x02) return("expecting 0x02 for variable 1"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("variable 1 length error"); cursor+=objlen; if(*cursor!=0x02 || *(cursor+1)!=0x01 || *(cursor+2)!=0x00 || *(cursor+3)!=0x02 || *(cursor+4)!=0x01 || *(cursor+5)!=0x00) return("V2:expecting 02 01 00 02 01 00 to start trap"); cursor+=6; // Insert the sender's address - don't worry, this will be overridden if sender supplies in variable bindings sprintf(cmdcur,",'1.3.6.1.6.3.18.1.3'=\"%s\"",nbIpGetAddrString(senderAddr,snmptrap->sourceAddr)); cmdcur+=strlen(cmdcur); // 2013-01-14 eat - VID 980-0.8.13-3 FP but replaced strchr(cmdcur,0) break; default: return("unrecognized trap version"); } fprintf(stderr,"translate: checking for handler\n"); fflush(stderr); fprintf(stderr,"translate: processing variable bindings\n"); fflush(stderr); // Variable bindings same for V1 and V2 if(*cursor!=0x30) return("expecting 0x30 for variable binding list"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("variable binding list length error"); while(cursor<bufend){ if(*cursor!=0x30) return("expecting 0x30 for variable binding"); cursor++; objlen=getObjectLength(&cursor,bufend); if(objlen<0) return("variable binding length error"); if(*cursor!=0x06) return("expecting OID on left side of variable binding"); cursor++; *cmdcur=','; cmdcur++; oid=cmdcur; // save location of OID *cmdcur='\''; cmdcur++; if((msg=translateOid(&cursor,bufend,&cmdcur,cmdend))!=NULL) return(msg); *cmdcur='\''; cmdcur++; *cmdcur=0; // look up syntax for value representation if(snmptrap->syntaxContext && (cell=nbTermLocateHere(snmptrap->syntaxContext,oid))!=NULL && (cell=nbTermGetDefinition(snmptrap->syntaxContext,cell))!=NULL && nbCellGetType(snmptrap->syntaxContext,cell)==NB_TYPE_STRING){ syntax=nbCellGetString(snmptrap->syntaxContext,cell); } else syntax=""; // look up attribute name and replace if found if(snmptrap->attributeContext && (cell=nbTermLocateHere(snmptrap->attributeContext,oid))!=NULL && (cell=nbTermGetDefinition(snmptrap->attributeContext,cell))!=NULL && nbCellGetType(snmptrap->attributeContext,cell)==NB_TYPE_STRING){ attribute=nbCellGetString(snmptrap->attributeContext,cell); strcpy(oid,attribute); cmdcur=strchr(oid,0); } *cmdcur='='; cmdcur++; value=cmdcur; if((msg=translateValue(&cursor,bufend,&cmdcur,cmdend,syntax))!=NULL) return(msg); if(strncmp(oid,"'1.3.6.1.6.3.1.1.4.1.0'",23)==0 && *value=='"'){ oid=trapOID; *oid='\''; oid++; value++; while(value<cmdcur && *value!='"') *oid=*value,oid++,value++; *oid='\''; oid++; *oid=0; } } *cmdcur=0; // Check for handler if(snmptrap->handlerContext && (cell=nbTermLocateHere(snmptrap->handlerContext,trapOID))!=NULL && (cell=nbTermGetDefinition(snmptrap->handlerContext,cell))!=NULL && nbCellGetType(snmptrap->handlerContext,cell)==NB_TYPE_STRING){ *handlerNameP=nbCellGetString(snmptrap->handlerContext,cell); //handler=nbCellGetString(snmptrap->handlerContext,cell); //strcpy(cmd,handler); //cmdcur=strchr(cmd,0); //*cmdcur=':'; //cmdcur++; //strcpy(cmdcur,trapOID); //cmdcur=strchr(cmdcur,0); } fprintf(stderr,"translate: returning\n"); fflush(stderr); return(NULL); }
std::unique_ptr<RamValue> translateValue(const AstArgument& arg, const ValueIndex& index = ValueIndex()) { return translateValue(&arg, index); }