void deviceMotionSimulationHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) { if (commandHeader == COMMAND_MOTION_SIMULATION_GET) { // send acknowledge ackCommand(outputStream, MOTION_SIMULATION_DEVICE_HEADER, COMMAND_MOTION_SIMULATION_GET); MotionSimulationParameter* motionSimulationParameter = getMotionSimulationParameter(); appendHex(outputStream, motionSimulationParameter->simulateMotors); appendHex(outputStream, motionSimulationParameter->simulateCoders); appendHex(outputStream, motionSimulationParameter->simulateRobotPosition); } else if (commandHeader == COMMAND_MOTION_SIMULATION_SET) { // send acknowledge ackCommand(outputStream, MOTION_SIMULATION_DEVICE_HEADER, COMMAND_MOTION_SIMULATION_SET); bool simulateMotors = readHex(inputStream); bool simulateCoders = readHex(inputStream); bool simulateRobotPosition = readHex(inputStream); MotionSimulationParameter* motionSimulationParameter = getMotionSimulationParameter(); motionSimulationParameter->simulateMotors = simulateMotors; motionSimulationParameter->simulateCoders = simulateCoders; motionSimulationParameter->simulateRobotPosition = simulateRobotPosition; } }
UUID:: UUID(const char* first, size_t n) { auto it = first, last = first + n; it = readHex(time_low, it, last); it = readHex(time_mid, it, last); it = readHex(time_hi_and_version, it, last); it = readHex(clock_seq, it, last); for (size_t i = 0; i < sizeof(node); ++i) it = readHex(node[i], it, last); }
int main (int ac, char *ag[]) { int len, addr; EIBConnection *con; uchar buf[255]; eibaddr_t dest; char *prog = ag[0]; parseKey (&ac, &ag); if (ac != 5) die ("usage: %s url [-k key] eibaddr addr count", prog); con = EIBSocketURL (ag[1]); if (!con) die ("Open failed"); dest = readaddr (ag[2]); addr = readHex (ag[3]); len = atoi (ag[4]); if (EIB_MC_Connect (con, dest) == -1) die ("Connect failed"); auth (con); len = EIB_MC_Read (con, addr, len, buf); if (len == -1) die ("Read failed"); printHex (len, buf); EIBClose (con); return 0; }
void mainBoardDeviceHandleTrajectoryDeviceNotification(const Device* device, const unsigned char commandHeader, InputStream* notificationInputStream) { // append(getDebugOutputStreamLogger(), device->deviceInterface->deviceHeader); // println(getDebugOutputStreamLogger()); if (device->deviceInterface->deviceHeader == TRAJECTORY_DEVICE_HEADER) { if (commandHeader == NOTIFY_TRAJECTORY_CHANGED) { /* append(getDebugOutputStreamLogger(), commandHeader); println(getDebugOutputStreamLogger()); */ updateNewPositionFromNotification(notificationInputStream); checkIsSeparator(notificationInputStream); enum TrajectoryType trajectoryType = readHex(notificationInputStream); gameStrategyContext->trajectoryType = trajectoryType; } else { writeError(NOTIFICATION_BAD_DEVICE_COMMAND_HANDLER_NOT_HANDLE); appendString(getAlwaysOutputStreamLogger(), "header"); append(getAlwaysOutputStreamLogger(), commandHeader); println(getAlwaysOutputStreamLogger()); } } else { writeError(NOTIFICATION_BAD_DEVICE); } }
static void process(){ rxBuf[rxAdd] = '\0' ; char const *rx = rxBuf ; unsigned long addr ; rx = readHex(rx,&addr); if( rx ){ unsigned long value ; rx = readHex(rx,&value); if( rx ){ showValue(addr); unsigned width ; switch( addr & 3 ){ case 0: width = 4 ; break ; case 1: case 3: width = 1 ; break ; case 2: width = 2 ; break ; } memcpy( (void *)addr, &value, width ); showValue(addr); } else { showValue(addr); } // read request } else if( ('r' == rxBuf[0]) || ('R' == rxBuf[0]) ) reboot(); else if( ('s' == rxBuf[0]) || ('S' == rxBuf[0]) ){ write( DEFUART, "wait 10\r\n" ); delay(10); write( DEFUART, "delay done\r\n" ); } else if( ('x' == rxBuf[0]) || ('X' == rxBuf[0]) ){ unsigned i ; write( DEFUART, "saved context\r\n" ); for( i = 0 ; i < 4 ; i++ ){ writeChar( DEFUART, '[' ); writeHexChar(DEFUART,i); write(DEFUART, "] == 0x" ); writeHex(DEFUART,savedCtx[i]); write( DEFUART, "\r\n" ); } } else if( ('p' == rxBuf[0]) || ('P' == rxBuf[0]) ){ dumpEP(0); } else if( ('u' == rxBuf[0]) || ('U' == rxBuf[0]) ){ usbSpew(); } else if( '.' == rxBuf[0] ){ dumpEP(USBEP_BULKIN); } else tx( "No address (Usage is \"address value\" in hex)\r\n" ); rxAdd = 0 ; }
// Returns 0 for success, 1 for timeout, 2 for CRC failure int getMessage(MessageType *msgType, uint16_t *msgLength, uint16_t *flags, uint8_t *msg) { int c, n; // Take in data until the start of a message do { c = getCharWait(MESSAGE_TIMEOUT); } while (c > -255 && c != '#'); if (c == -255) return 1; MsgHeader header; // Read in the hex-encoded header n = readHex((uint8_t *)&header, sizeof(header)); if (n < sizeof(header)) return 1; // Avoid buffer overflows if (header.length > MAX_MESSAGE_LENGTH) header.length = MAX_MESSAGE_LENGTH; *msgType = (MessageType)header.type; *msgLength = header.length; *flags = header.flags; // Read in the message n = readHex(msg, header.length); if (n < header.length) return 1; // Perform a CRC16 check on the message uint16_t crc = calcCrc16((uint8_t *)&header + 2, sizeof(header) - 2); crc = calcCrc16(msg, header.length, crc); if (crc != header.crc) return 2; if (header.flags & CONFIRMATION_NEEDED_FLAG) confirmMessage(header.id); return 0; }
int main (int ac, char *ag[]) { int addr; int len; int index; CArray result; LowLevelDriverInterface *iface = 0; memset (&arg, 0, sizeof (arg)); argp_parse (&argp, ac, ag, 0, &index, &arg); if (index > ac - 3) die ("more parameter expected"); if (index < ac - 3) die ("unexpected parameter"); signal (SIGPIPE, SIG_IGN); pth_init (); Logs t; t.setTraceLevel (arg.tracelevel); iface = Create (ag[index], &t); if (!iface) die ("initialisation failed"); if (!iface->init ()) die ("initialisation failed"); addr = readHex (ag[index + 1]); len = atoi (ag[index + 2]); int res = readEMIMem (iface, addr, len, result); if (!res) { printf ("Read failed"); } else { for (int i = 0; i < result (); i++) printf ("%02x ", result[i]); printf ("\n"); } delete iface; if (Cleanup) Cleanup (); pth_exit (0); return 0; }
int writeHex(char *nomeArq, int totMem) { int cnt, totP; unsigned char dados[totMem]; unsigned char check[totMem]; if(readHex(nomeArq, dados, totMem) < 0) return 10; chipErase(); totP = writeChip(totMem, dados); readChip(totMem, check); for(cnt=0; cnt < totMem; cnt++) if(dados[cnt] != check[cnt]) { printf("Erro! Endereço %d leu 0x%.2X, mas era para ser 0x%.2X\n", cnt, check[cnt], dados[cnt]); return 0; } return totP; }
void TellStick::downloadFirmware() { QString filename = "TellStick"; int bootloaderStart = 0x3A00; if (type() == 2) { filename = "TellStickDuo"; bootloaderStart = 0x7A00; } QString path; //if (QApplication::arguments().count() > 1) { // path = QApplication::arguments().at(1); //} else { path = QString(":/firmware/%1.hex").arg(filename); //} QByteArray data = readHex(path, bootloaderStart); int bytesLeft = 0, i = 0; char byte; while (i < data.length()) { QApplication::processEvents(); byte = getCh(); QApplication::processEvents(); if (byte == 'b') { bytesLeft = data.length() - i; if (bytesLeft > 0xFF) { bytesLeft = 0xFF; } send(bytesLeft); } else if (byte == 'd') { send(data[i]); --bytesLeft; ++i; this->setUpgradeProgress( (qreal)i/(qreal)data.length()*100.0 ); } } setUpgradeStep(4); QTimer::singleShot(0, this, SLOT(rebootTellStick())); }
short Scanner::getToken( YYSTYPE & lval ) //--------------------------------------- // get the next token from the input stream. // at each call, the next character should be // in _current. { const int MaxBufLen = 512; char buffer[ MaxBufLen ]; int bufPos; // position in buffer char special; gobble(); if( isEOF() ) { return 0; } if( isSpecial() ) { special = (char) _current; get(); // set up for next call return special; // <-------- early return } if( isQuote() ) { readQuotedString( lval ); return T_String; // <-------- early return } if( _current == '0' ) { get(); if( toupper( _current ) == 'X' ) { readHex( lval ); } else { if( isDigit() ) { readDecimal( lval ); } else { lval = 0; } } return T_Number; // <-------- early return } if( isDigit() ) { readDecimal( lval ); return T_Number; // <-------- early return } for( bufPos = 0; bufPos < MaxBufLen; bufPos += 1 ) { buffer[ bufPos ] = (char) _current; get(); if( isEOF() || isSpace() || isSpecial() ) break; } bufPos += 1; assert( bufPos < MaxBufLen ); buffer[ bufPos ] = '\0'; return tokenValue( buffer, lval ); }
int getHex(void) { extern text_t *dport_ptr; return readHex(dport_ptr); }
int main(int argc, char **argv) { FILE *fd; FT_STATUS ftStatus = FT_OK; FT_HANDLE ftHandle; DWORD dwNumberOfDevices = 0; // int vid = 0x0403, pid = 0x6001; // int vid = 0x1781, pid = 0x0C31; int vid = 0x1781, pid = 0x0C30; if (argc < 2) { printf("Usage: %s filename\n", argv[0]); return 1; } FT_SetVIDPID(vid, pid); fd = fopen(argv[1], "r"); if (!fd) { fclose(fd); printf("Could not open file\n"); return 1; } ftStatus = FT_CreateDeviceInfoList(&dwNumberOfDevices); if (ftStatus == FT_OK) { bool found = false; for (int i = 0; i < (int)dwNumberOfDevices; i++) { FT_PROGRAM_DATA pData; char ManufacturerBuf[32]; char ManufacturerIdBuf[16]; char DescriptionBuf[64]; char SerialNumberBuf[16]; pData.Signature1 = 0x00000000; pData.Signature2 = 0xffffffff; pData.Version = 0x00000002; // EEPROM structure with FT232R extensions pData.Manufacturer = ManufacturerBuf; pData.ManufacturerId = ManufacturerIdBuf; pData.Description = DescriptionBuf; pData.SerialNumber = SerialNumberBuf; ftStatus = FT_Open(i, &ftHandle); ftStatus = FT_EE_Read(ftHandle, &pData); if(ftStatus == FT_OK){ if(pData.VendorId == vid && pData.ProductId == pid){ found = true; break; } } FT_Close(ftHandle); } if (!found) { printf("Could not find TellStick (Duo)\n"); return 1; } } if (pid == 0x0C31) { FT_SetBaudRate(ftHandle, 115200); } else { FT_SetBaudRate(ftHandle, 9600); } FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0); std::string data = readHex(fd); FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); printf("Reboot TellStick..."); ftStatus = FT_SetBitMode(ftHandle, 0xff, 0x20); sleep(1); printf("Done\n"); ftStatus = FT_SetBitMode(ftHandle, 0xf0, 0x20); printf("Waiting for TellStick Duo Bootloader.\n"); waitFor(ftHandle, 'g'); send(ftHandle, 'r'); printf("Uploading hex-file\n"); uploadHex(ftHandle, data); printf("Rebooting TellStick\n"); waitFor(ftHandle, 'b'); send(ftHandle, 0x00); printf("Firmware updated!\n"); FT_Close(ftHandle); fclose(fd); }
int dmtcp::Util::readProcMapsLine(int mapsfd, dmtcp::Util::ProcMapsArea *area) { char c, rflag, sflag, wflag, xflag; int i; unsigned int long devmajor, devminor, inodenum; VA startaddr, endaddr; c = readHex (mapsfd, &startaddr); if (c != '-') { if ((c == 0) && (startaddr == 0)) return (0); goto skipeol; } c = readHex (mapsfd, &endaddr); if (c != ' ') goto skipeol; if (endaddr < startaddr) goto skipeol; rflag = c = readChar (mapsfd); if ((c != 'r') && (c != '-')) goto skipeol; wflag = c = readChar (mapsfd); if ((c != 'w') && (c != '-')) goto skipeol; xflag = c = readChar (mapsfd); if ((c != 'x') && (c != '-')) goto skipeol; sflag = c = readChar (mapsfd); if ((c != 's') && (c != 'p')) goto skipeol; c = readChar (mapsfd); if (c != ' ') goto skipeol; c = readHex (mapsfd, (VA *)&devmajor); if (c != ' ') goto skipeol; area -> offset = (off_t)devmajor; c = readHex (mapsfd, (VA *)&devmajor); if (c != ':') goto skipeol; c = readHex (mapsfd, (VA *)&devminor); if (c != ' ') goto skipeol; c = readDec (mapsfd, (VA *)&inodenum); area -> name[0] = '\0'; while (c == ' ') c = readChar (mapsfd); if (c == '/' || c == '[') { /* absolute pathname, or [stack], [vdso], etc. */ i = 0; do { area -> name[i++] = c; if (i == sizeof area -> name) goto skipeol; c = readChar (mapsfd); } while (c != '\n'); area -> name[i] = '\0'; } if (c != '\n') goto skipeol; area -> addr = startaddr; area -> size = endaddr - startaddr; area -> endAddr = endaddr; area -> prot = 0; if (rflag == 'r') area -> prot |= PROT_READ; if (wflag == 'w') area -> prot |= PROT_WRITE; if (xflag == 'x') area -> prot |= PROT_EXEC; area -> flags = MAP_FIXED; if (sflag == 's') area -> flags |= MAP_SHARED; if (sflag == 'p') area -> flags |= MAP_PRIVATE; if (area -> name[0] == '\0') area -> flags |= MAP_ANONYMOUS; return (1); skipeol: JASSERT(false) .Text("Not Reached"); return (0); /* NOTREACHED : stop compiler warning */ }
int main(int argc, char *argv[]) { FILE *fp; char c; time_t start = time(NULL), end; if ((argc < 3) || (argc > 4)) { printf("Usage:\n%s /dev/port /path/to.hex [q]\n", argv[0]); return 1; } // Open HEX File if ((fp = fopen(argv[2], "r")) == NULL) { printf("Could not open file %s\n", argv[2]); return 1; } // Read it's contents if (readHex(fp) != 0) { printf("Could not parse HEX file %s\n", argv[2]); fclose(fp); return 1; } fclose(fp); if (!isValid()) { printf("HEX-File not valid!\n"); freeHex(); return 1; } uint32_t min = minAddress(); uint32_t max = maxAddress(); uint32_t length = dataLength(); printf("Hex File Path : %s\n", argv[2]); printf("Minimum Address : 0x%X\n", min); printf("Maximum Address : 0x%X\n", max); printf("Data payload : %i bytes\n\n", length); uint8_t *d = (uint8_t *)malloc(length * sizeof(uint8_t)); if (d == NULL) { fprintf(stderr, "Not enough memory (%lu bytes)!\n", length * sizeof(uint8_t)); freeHex(); return 1; } parseData(d); freeHex(); // Open serial port if ((fd = serialOpen(argv[1], 38400, 1)) == -1) { printf("Could not open port %s\n", argv[1]); free(d); return 1; } signal(SIGINT, intHandler); signal(SIGQUIT, intHandler); ping: printf("Pinging bootloader... Stop with CTRL+C\n"); if (argc > 3) { c = argv[3][0]; } else { c = 'f'; } serialWriteChar(fd, c); usleep(PINGDELAY * 1000); if ((serialReadRaw(fd, &c, 1) != 1) || (c != OKAY)) { goto ping; } printf("Got response... Acknowledging...\n"); serialWriteChar(fd, CONFIRM); serialReadChar(fd, &c); if (c != ACK) { printf("Invalid acknowledge! Trying again...\n", c, c); goto ping; } printf("Connection established successfully!\n"); printf("Sending target address...\n"); serialWriteChar(fd, (min & 0xFF000000) >> 24); serialWriteChar(fd, (min & 0x00FF0000) >> 16); serialWriteChar(fd, (min & 0x0000FF00) >> 8); serialWriteChar(fd, min & 0x000000FF); serialReadChar(fd, &c); if (c != OKAY) { printf("Invalid acknowledge from YASAB!\n", c, c); free(d); serialClose(fd); return 1; } printf("Sending data length...\n"); serialWriteChar(fd, (length & 0xFF000000) >> 24); serialWriteChar(fd, (length & 0x00FF0000) >> 16); serialWriteChar(fd, (length & 0x0000FF00) >> 8); serialWriteChar(fd, length & 0x000000FF); serialReadChar(fd, &c); if (c != OKAY) { printf("Invalid acknowledge from YASAB (%x)!\n", c, c); free(d); serialClose(fd); return 1; } printf("\n\n"); for (uint32_t i = 0; i < length; i++) { serialWriteChar(fd, d[i]); if (serialHasChar(fd)) { serialReadChar(fd, &c); if (c == OKAY) { printNextPage(); } else if (c == ERROR) { printf("YASAB aborted the connection!\n"); free(d); serialClose(fd); return 1; } else { printf("Unknown answer from YASAB (%x)!\n", c, c); } } printProgress(i + 1, length); } end = time(NULL); printf("\n\nUpload finished after %3.1f seconds.\n", difftime(end, start)); printf("YASAB - Yet another simple AVR Bootloader\n"); printf("By xythobuz - Visit www.xythobuz.org\n"); free(d); serialClose(fd); return 0; }
/* load S format data */ LOCAL W loadSform(void) { W i, c, bcnt, v, v1, dcnt, rtype; UW addr, a_addr; UB cksum, buf[512]; a_addr = s_addr; // real address s_addr = 0xffffffff; // highest load address e_addr = 0; // lowest load address for (;;) { if ((c = (*readFn)()) < 0) return c; if (c != 'S') { if (c == CTLZ) break; // end continue; } if ((c = (*readFn)()) < 0) return c; switch(c) { case '0': // header rtype = 0; break; case '1': // 2 byte address data case '2': // 3 byte address data case '3': // 4 byte address data rtype = c - '0' + 2; break; case '7': // 4 byte address termination case '8': // 3 byte address termination case '9': // 2 byte address termination rtype = -1; break; default: return E_LOADFMT; } for (cksum = bcnt = addr = dcnt = i = 0;; i++) { if ((v1 = readHex()) < 0) return v1; if ((v = readHex()) < 0) return v; cksum += (v += (v1 << 4)); if (i == 0) { // byte counts if ((bcnt = v - 1) < 0) return E_LOAD; addr = 0; continue; } if (i > bcnt) { // checksum if (cksum != 0xff) return E_LOAD; break; } if (rtype <= 0) continue; if (i < rtype) { // load address addr = (addr << 8) + v; } else { // data buf[dcnt++] = (UB)v; } } if (dcnt > 0) { // if we have address specification, then the first address // to be used as the designated address after suitable adjustment. if (a_addr != 0) { offset = a_addr - addr; a_addr = 0; } addr += offset; if (addr < loaddr || addr - 1 > hiaddr - dcnt) return E_RANGE; if (writeMem(addr, buf, dcnt, 1) != dcnt) return E_MACV; if (addr < s_addr) s_addr = addr; if ((addr += dcnt) > e_addr) e_addr = addr; } if (rtype < 0) break; // end } return E_OK; }
void deviceTimerHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) { if (commandHeader == COMMAND_TIMER_LIST) { printTimerList(getInfoOutputStreamLogger(), getTimerList()); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_LIST); } else if (commandHeader == COMMAND_TIMER_COUNT) { ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_COUNT); unsigned timerCount = getTimerCount(); appendHex2(outputStream, timerCount); } else if (commandHeader == COMMAND_TIMER_READ) { ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_READ); unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); appendHex2(outputStream, timerIndex); appendSeparator(outputStream); appendHex2(outputStream, timer->timerCode); appendSeparator(outputStream); appendHex4(outputStream, timer->timeDiviser); appendSeparator(outputStream); appendHex4(outputStream, timer->timeInternalCounter); appendSeparator(outputStream); appendHex6(outputStream, timer->time); appendSeparator(outputStream); appendHex6(outputStream, timer->markTime); appendSeparator(outputStream); appendBool(outputStream, timer->enabled); } // Enable / Tisable else if (commandHeader == COMMAND_TIMER_ENABLE_DISABLE) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); checkIsSeparator(inputStream); unsigned enableAsChar = readHex(inputStream); bool enabled = enableAsChar != 0; timer->enabled = enabled; ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_ENABLE_DISABLE); } // Mark else if (commandHeader == COMMAND_TIMER_MARK) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); unsigned long time = markTimer(timer); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_MARK); appendHex6(outputStream, time); } else if (commandHeader == COMMAND_TIMER_TIME_SINCE_LAST_MARK) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); unsigned long value = getTimeSinceLastMark(timer); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIME_SINCE_LAST_MARK); appendHex6(outputStream, value); } else if (commandHeader == COMMAND_TIMER_TIMEOUT) { unsigned char timerIndex = readHex2(inputStream); checkIsSeparator(inputStream); unsigned long timeToCheck = (unsigned long) readHex6(inputStream); Timer* timer = getTimerByIndex(timerIndex); bool value = timeout(timer, timeToCheck); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIMEOUT); appendHex2(outputStream, timerIndex); appendSeparator(outputStream); appendBool(outputStream, value); } // Demo else if (commandHeader == COMMAND_TIMER_DEMO) { Timer* timer = getTimerByCode(DEMO_TIMER_INDEX); if (timer == NULL) { timer = addTimerDemo(); } // Timer could be null when adding the timerDemo because of limit, we don't want any crash ! if (timer != NULL) { unsigned enableAsChar = readHex(inputStream); bool enabled = enableAsChar != 0; timer->enabled = enabled; } ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_DEMO); } }