// Add a prefix to the front of the error by reallocating & copying // DLLEXPORT(void) errPrefix(const char **error, const char *prefix) { if ( !prefix || !error ) { return; } if ( *error ) { // There's already something there - prefix it char *newError, *p; const size_t pLen = strlen(prefix); // ": " and null terminator const size_t len = pLen + strlen(*error) + 3; // ": " and null terminator p = newError = (char*)malloc(len); if ( newError == NULL ) { errFree(*error); *error = NULL; return; } strcpy(p, prefix); p += pLen; *p++ = ':'; *p++ = ' '; strcpy(p, *error); errFree(*error); *error = newError; } else { // Nothing is already there, so just copy char *newError = (char*)malloc(strlen(prefix) + 1); strcpy(newError, prefix); *error = newError; } }
int main(int argc, const char *argv[]) { int retVal = 0; struct USBDevice *device = NULL; const char *error = NULL; const char *vp; USBStatus uStatus; if ( argc != 2 ) { fprintf(stderr, "Synopsis: %s <VID:PID>\n", argv[0]); FAIL(1, cleanup); } vp = argv[1]; uStatus = usbInitialise(0, &error); CHECK_STATUS(uStatus, 2, cleanup); uStatus = usbOpenDevice(vp, 1, 0, 0, &device, &error); CHECK_STATUS(uStatus, 3, cleanup); uStatus = usbPrintConfiguration(device, stdout, &error); CHECK_STATUS(uStatus, 4, cleanup); cleanup: if ( device ) { usbCloseDevice(device, 0); } if ( error ) { fprintf(stderr, "%s: %s\n", argv[0], error); errFree(error); } return retVal; }
// Add a prefix to the front of the error by reallocating & copying // DLLEXPORT(void) errPrefix(const char **error, const char *prefix) { if ( error && prefix ) { char *newError, *p; const int pLen = strlen(prefix); // ": " and null terminator const int len = pLen + strlen(*error) + 3; // ": " and null terminator p = newError = (char*)malloc(len); if ( newError == NULL ) { errFree(*error); *error = NULL; return; } strcpy(p, prefix); p += pLen; *p++ = ':'; *p++ = ' '; strcpy(p, *error); errFree(*error); *error = newError; } }
int main(int argc, const char *argv[]) { int retVal; // Init library wiringPiSetup(); // Validate command-line args if ( argc != 2 ) { fprintf(stderr, "Synopsis: %s <svf-file>\n", argv[0]); FAIL(GJ_USAGE, exit); } // Setup pins pinMode(TCK, OUTPUT); pinMode(TMS, OUTPUT); pinMode(TDI, OUTPUT); pinMode(TDO, INPUT); digitalWrite(TCK, LOW); digitalWrite(TMS, LOW); digitalWrite(TDI, LOW); // Parse .svf file const char *error = NULL; const struct ParserCallbacks cb = {setTCK, setTMS, setTDI, getTDO}; ParserStatus pStatus = parse(argv[1], &cb, &error); CHECK_STATUS(pStatus, pStatus, cleanup); retVal = 0; cleanup: if ( error ) { printf("%s\n", error); errFree(error); } exit: pinMode(TCK, INPUT); pinMode(TMS, INPUT); pinMode(TDI, INPUT); pinMode(TDO, INPUT); return retVal; }
// Convenience function to avoid having to include liberror.h. // DLLEXPORT(void) flFreeError(const char *err) { errFree(err); }
int main(int argc, const char *argv[]) { int retVal = 0; struct Buffer data = {0,}; struct Buffer mask = {0,}; struct Buffer i2c = {0,}; BufferStatus bStatus; I2CStatus iStatus; int dStatus; uint8 configByte; const char *error = NULL; if ( argc != 4 ) { usage(argv[0]); FAIL(1, cleanup); } if ( strstr(argv[2], "WithBoot") ) { // Boot firmware explicitly connects configByte = CONFIG_BYTE_400KHZ | CONFIG_BYTE_DISCON; } else { // NonBoot firmwares are connected automatically configByte = CONFIG_BYTE_400KHZ; } bStatus = bufInitialise(&data, 0x4000, 0x00, &error); CHECK_STATUS(bStatus, 2, cleanup); bStatus = bufInitialise(&mask, 0x4000, 0x00, &error); CHECK_STATUS(bStatus, 3, cleanup); bStatus = bufReadFromIntelHexFile(&data, &mask, argv[1], &error); CHECK_STATUS(bStatus, 4, cleanup); if ( !strcmp("iic", argv[3]) ) { // Get i2c records bStatus = bufInitialise(&i2c, 0x4000, 0x00, &error); CHECK_STATUS(bStatus, 5, cleanup); i2cInitialise(&i2c, 0x0000, 0x0000, 0x0000, configByte); iStatus = i2cWritePromRecords(&i2c, &data, &mask, &error); CHECK_STATUS(iStatus, 6, cleanup); iStatus = i2cFinalise(&i2c, &error); CHECK_STATUS(iStatus, 7, cleanup); // Dump the code dStatus = dumpCode(argv[0], argv[2], &i2c); CHECK_STATUS(dStatus, dStatus, cleanup); } else if ( !strcmp("bix", argv[3]) ) { // Dump the code dStatus = dumpCode(argv[0], argv[2], &data); CHECK_STATUS(dStatus, dStatus, cleanup); } else { usage(argv[0]); FAIL(8, cleanup); } cleanup: if ( error ) { fprintf(stderr, "%s: %s\n", argv[0], error); errFree(error); } bufDestroy(&i2c); bufDestroy(&mask); bufDestroy(&data); return retVal; }
int main(int argc, const char *argv[]) { int returnCode = 0; struct Buffer data1 = {0,}; struct Buffer mask1 = {0,}; struct Buffer data2 = {0,}; struct Buffer mask2 = {0,}; struct Buffer i2c1 = {0,}; struct Buffer i2c2 = {0,}; BufferStatus bStatus; I2CStatus iStatus; int dStatus; uint8 configByte; const char *error = NULL; if ( argc != 5 ) { //&& argc != 6 ) { usage(argv[0]); FAIL(1); } if ( strstr(argv[3], "WithBoot") ) { // Boot firmware explicitly connects configByte = CONFIG_BYTE_400KHZ | CONFIG_BYTE_DISCON; } else { // NonBoot firmwares are connected automatically configByte = CONFIG_BYTE_400KHZ; } bStatus = bufInitialise(&data1, 0x4000, 0x00, &error); CHECK(bStatus, 2); bStatus = bufInitialise(&mask1, 0x4000, 0x00, &error); CHECK(bStatus, 3); bStatus = bufReadFromIntelHexFile(&data1, &mask1, argv[1], &error); CHECK(bStatus, 4); bStatus = bufInitialise(&data2, 0x4000, 0x00, &error); CHECK(bStatus, 5); bStatus = bufInitialise(&mask2, 0x4000, 0x00, &error); CHECK(bStatus, 6); bStatus = bufReadFromIntelHexFile(&data2, &mask2, argv[2], &error); CHECK(bStatus, 7); if ( !strcmp("iic", argv[4]) ) { // Get i2c records from first build bStatus = bufInitialise(&i2c1, 0x4000, 0x00, &error); CHECK(bStatus, 8); i2cInitialise(&i2c1, 0x0000, 0x0000, 0x0000, configByte); iStatus = i2cWritePromRecords(&i2c1, &data1, &mask1, &error); CHECK(iStatus, 9); iStatus = i2cFinalise(&i2c1, &error); CHECK(iStatus, 10); // Get i2c records from second build bStatus = bufInitialise(&i2c2, 0x4000, 0x00, &error); CHECK(bStatus, 11); i2cInitialise(&i2c2, 0x0000, 0x0000, 0x0000, configByte); iStatus = i2cWritePromRecords(&i2c2, &data2, &mask2, &error); CHECK(iStatus, 12); iStatus = i2cFinalise(&i2c2, &error); CHECK(iStatus, 13); // Dump the code dStatus = dumpCode(argv[0], argv[3], &i2c1, &i2c2); CHECK(dStatus, dStatus); } else if ( !strcmp("bix", argv[4]) ) { // Dump the code dStatus = dumpCode(argv[0], argv[3], &data1, &data2); CHECK(dStatus, dStatus); } else { usage(argv[0]); FAIL(14); } cleanup: if ( error ) { fprintf(stderr, "%s: %s\n", argv[0], error); errFree(error); } bufDestroy(&i2c2); bufDestroy(&i2c1); bufDestroy(&mask2); bufDestroy(&data2); bufDestroy(&mask1); bufDestroy(&data1); return returnCode; }