/* * Something that breaks my device. * Copyright (C) Geoff Oakham, 2004; */ #include #include #include #include #include "ifp.h" #define BUILDDIR "./" #define TESTDATA "../testdata" #define REMOTE_TESTDIR "\\debug" int problem_code(struct ifp_device * dev) { int i = 0; const char * data = "hello world\n"; //const char * data = "masioub, qkb\n"; int ndata = strlen(data); const char * remote = REMOTE_TESTDIR "\\corruption_test.dat"; uint8_t buf[IFP_TUNER_PRESET_DATA]; //Get the presets i = ifp_get_tuner_presets(dev, buf, sizeof(buf)); ifp_err_jump(i, err, "can't get presets"); //upload "hello world\n" i = ifp_delete(dev, remote); if (i == -ENOENT) { //nothing } else if (i == 0) { printf("removed ifp:\\%s from previous test\n", remote); } i = ifp_write_open(dev, remote, ndata); ifp_err_jump(i, err, "couldn't create file"); i = ifp_write_data(dev, (void*)data, ndata); ifp_err_jump(i, err, "couldn't upload file data"); i = ifp_write_close(dev); ifp_err_jump(i, err, "couldn't close file"); #if 1 //cleanup i = ifp_delete(dev, remote); ifp_err_jump(i, err, "couldn't delete file"); #endif i = 0; err: return i; } int do_something(struct ifp_device * dev) { int i = 0, e=0; int made_remote; char buf[255]; i = ifp_device_info(dev, buf, sizeof(buf)); if (i) { printf("device info failed, i=%d.\n", i); return i; } printf("Detected: %s\n", buf); if (ifp_is_dir(dev, REMOTE_TESTDIR)) { made_remote = 0; } else { made_remote = 1; i = ifp_mkdir(dev, REMOTE_TESTDIR); if (i) { ifp_err_i(i, "mkdir ifp:\\%s failed", REMOTE_TESTDIR); return i; } //printf("I need the directory ifp:\\" REMOTE_TESTDIR " on the device to run these tests. (But I don't want to do it without your permission.) Please create it and try again.\n"); //return -1; } i = problem_code(dev); if (i) { printf("test failed\n"); } else { printf("test ok\n"); } e = e ? e : i; if (made_remote) { i = ifp_rmdir(dev, REMOTE_TESTDIR); if (i) { ifp_err_i(i, "remote cleanup of ifp:\\%s failed", REMOTE_TESTDIR); e = e ? e : i; } } return e; } int main(int argc, char **argv) { struct usb_device *dev = NULL; usb_dev_handle *dh; struct ifp_device ifpdev; int i=0, e=0; usb_init(); dh = ifp_find_device(); if (dh == NULL) { fprintf(stderr, "A suitable iRiver iFP device couldn't be found; " "perhaps it's unplugged or turned off.\n"); i = 1; goto out_0; } dev = usb_device(dh); /* "must be called" written in the libusb documentation */ if (usb_claim_interface(dh, dev->config->interface->altsetting-> bInterfaceNumber)) { fprintf(stderr, "Device is busy. (I was unable to claim its" " interface.)\n"); i = 1; goto out_1; } i = ifp_init(&ifpdev, dh); if (i) { printf("Device failed to initialize:\n%s\n (err=%d)\n", ifp_error_message(i), i); goto out_2; } i = do_something(&ifpdev); if (i) { printf("do_something failed, i=%d.\n%s\n", i, ifp_error_message(i)); goto out_3; } out_3: e = ifp_finalize(&ifpdev); if (e) { fprintf(stderr, "warning: finalize failed, i=%d\n",e); i = i?i:e; } out_2: usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber); out_1: e = ifp_release_device(dh); if (e) { fprintf(stderr, "warning: release_device failed, i=%d\n",e); i=i?i:e; } out_0: return i; }