JNIEXPORT jint JNICALL Java_com_stericsson_sdk_equipment_io_usb_internal_USBNativeDevice_write(
		JNIEnv *env, jobject src, jbyteArray data, jint offset, jint length,
		jint timeout) {
	try {
		USBDevice *device = getUSBDevice(env, src);

		if (device) {
			jsize dataLength = env->GetArrayLength(data);
			if ((offset < 0) || ((offset + length) > dataLength)) {
				std::stringstream ss;
				ss<<"offset:"<<offset<<"\tlength: "<<length<<"\tdataLength: "<<dataLength;
				Logger::getInstance()->error(LP, ss.str());
				env->ThrowNew(env->FindClass(EX_ARRAY_INDEX_OUT_OF_BOUND),
						"Given offset and length exceeds write buffer space!");
			} else {
				jbyte *buf = (jbyte *) malloc(length * sizeof(jbyte));
				env->GetByteArrayRegion(data, offset, length, buf);

				int ret = device->bulkWrite(env, (char *) buf, length, timeout);
				free(buf);
				return ret;
			}
		} else {
			Logger::getInstance()->error(LP, "Cannot find device!");
		}
	} catch (const char *errorString) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION), errorString);
	} catch (...) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION),
				"Unexpected native call failure in method write!");
	}

	return UNDEF;
}
JNIEXPORT void JNICALL Java_com_stericsson_sdk_equipment_io_usb_internal_USBNativeDevice_open(
		JNIEnv *env, jobject src) {
	try {
		USBDevice *device = getUSBDevice(env, src);

		if (device) {
			device->open();
		} else {
			Logger::getInstance()->error(LP, "Cannot find device!");
		}
	} catch (const char *errorString) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION), errorString);
	} catch (...) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION),
				"Unexpected native call failure in method open!");
	}
}
JNIEXPORT void JNICALL Java_com_stericsson_sdk_equipment_io_usb_internal_USBNativeDevice_close(
		JNIEnv *env, jobject src) {
	try {
		USBDevice *device = getUSBDevice(env, src);

		if (device) {
			device->close(env);
			Logger::getInstance()->debug(LP, "Removing device from internal cache..");
			usbHandler->removeDevice(device->getKey());
			if (usbHandler->getDevice(device->getKey()) != NULL) {
				Logger::getInstance()->error(LP, "Removing from internal cache failed!");
			}
		} else {
			Logger::getInstance()->error(LP, "Cannot find device!");
		}
	} catch (const char *errorString) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION), errorString);
	} catch (...) {
		env->ThrowNew(env->FindClass(EX_NATIVE_EXCEPTION),
				"Unexpected native call failure in method close!");
	}
}
Exemplo n.º 4
0
int iterateDevices(io_iterator_t deviceIterator)
{
io_object_t usbDevice;
int err = -1;
IOReturn ret;
IOUSBDeviceInterface245 **dev=NULL;
SInt32 config = 0;
int exclusiveErrs, attempts;


    for(attempts = 1; attempts < 5; attempts++)
    {
        exclusiveErrs = 0;
        usbDevice = IOIteratorNext(deviceIterator);
        if(usbDevice == IO_OBJECT_NULL)
        {
            fprintf(stderr, "Unable to find first matching USB device\n");
            return(-1);
        }
        
        while(usbDevice != IO_OBJECT_NULL)
        {
            dev = getUSBDevice(usbDevice);
            
            if(dev != nil)
            {
                
                config = openUSBDevice(dev);
                if(config == -2)
                {
                    exclusiveErrs++;
                }
                else if(config >= 0)
                {
                    // Device sucessfully opened
                
                    if(config > 0)
                    {
                        useUSBDevice(dev, config);
                    }
                    else
                    {
                        printf("What use is a device with a zero configuration????\n");
                    }
                
                    ret = (*dev)->USBDeviceClose(dev);
                }
        
                ret = (*dev)->Release(dev);
                // Not worth bothering with errors here
            }
            IOObjectRelease(usbDevice);
            
            if(config >= 0)	// we have sucessfully used device 
            {
                return(0);
            }
            usbDevice = IOIteratorNext(deviceIterator);
        };
        if(exclusiveErrs > 0)
        {
	     	sleep(1);
            IOIteratorReset(deviceIterator);
            printf("Trying open again %d\n", attempts);
        }
        else
        {
            break;
        }
    }
    return(err);
}