how to get EmbeddedFiles from FileAttachment Annotation in pdf file - iphone

I am working on PDF annotation app i added PDF file annotation in iPhone it working fine it annotation also visible any reader but facing one issue How to get EmbeddedFiles from File Attachment Annotation in PDF file whose is created annotation from desktop how can do for this issue
i am using this code for get contents and location of annotation from File Attachment Annotation it working fine
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pPage);
// NSLog(#"%#",(NSDictionary*)pageDictionary);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
[pdfAnnots release];
return nil;
}
CGPDFArrayRef rectArray;
if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) {
break;
}
int arrayCount = CGPDFArrayGetCount( rectArray );
CGPDFReal coords[4];
for( int k = 0; k < arrayCount; ++k ) {
CGPDFObjectRef rectObj;
if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) {
break;
}
CGPDFReal coord;
if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) {
break;
}
coords[k] = coord;
}
CGRect rect = CGRectMake(coords[0],coords[1],coords[2],coords[3]);
NSLog(#"%#",NSStringFromCGRect(rect));
CGPDFDictionaryRef aDict;
if(CGPDFDictionaryGetDictionary(annotDict, "AP", &aDict))
{
CGPDFStreamRef textStringRef33;
if(CGPDFDictionaryGetStream(aDict, "N", &textStringRef33)) {
CGPDFDataFormat *format = NULL;
CFDataRef contdata = CGPDFStreamCopyData( textStringRef33, format );
NSData *data=(NSData*)contdata;
}
please help me
Thank you in advance

PDF embeds is not the same as annotations. Embedded files stored in document section instead of page section.
Annonation can contains embedded file, but mechanism of it's extraction is different.
First, you should see internal structure of annotation object. You can use function like this:
static void op_Applier(const char *key, CGPDFObjectRef value, void *info) {
if (*(int *)info > 6) {
return;
}
const char *valuestr = NULL;
CGPDFObjectType type = CGPDFObjectGetType(value);
int i = 0;
for (i = 0; i < *(int *)info; i++) {
printf("\t");
}
printf("%s", key);
switch (type) {
case kCGPDFObjectTypeBoolean: {
bool b;
CGPDFObjectGetValue(value, type, &b);
printf(" (boolean) : %s\n", (b)?"true":"false");
}
break;
case kCGPDFObjectTypeInteger: {
int64_t i;
CGPDFObjectGetValue(value, type, &i);
printf(" (integer) : %lld\n", i);
}
break;
case kCGPDFObjectTypeReal: {
CGPDFReal f;
CGPDFObjectGetValue(value, type, &f);
printf(" (real) : %f\n", f);
}
break;
case kCGPDFObjectTypeName:{
CGPDFObjectGetValue(value, type, (void *)&valuestr);
printf(" (name) : %s\n", valuestr);
}
break;
case kCGPDFObjectTypeString: {
CGPDFStringRef str;
CGPDFObjectGetValue(value, type, &str);
printf(" (string) : %s\n", CGPDFStringGetBytePtr(str));
}
break;
case kCGPDFObjectTypeArray: {
printf(" (array)\n");
CGPDFArrayRef arr;
CGPDFObjectGetValue(value, type, &arr);
int t = *(int *)info + 1;
int j;
size_t size = CGPDFArrayGetCount(arr);
for (j = 0; j < size; j ++) {
CGPDFObjectRef obj;
CGPDFArrayGetObject(arr, j, &obj);
char buf[4] = {0};
sprintf(buf, "%d", j);
op_Applier(buf, obj, &t);
}
}
break;
case kCGPDFObjectTypeDictionary: {
printf(" (dictionary)\n");
CGPDFDictionaryRef dict;
CGPDFObjectGetValue(value, type, &dict);
int t = *(int *)info + 1;
CGPDFDictionaryApplyFunction(dict, &op_Applier, &t);
}
break;
case kCGPDFObjectTypeStream:
printf(" (stream)\n");
break;
default:
printf(" (NULL)\n");
break;
}
}
int t = 0; op_Applier("MyDictionary", yourAnnotationDictionary, &t);
Second, you should find path to your file (it will be stream object), and move to this object. This can be done with CGPDFDictionary methods.
Third, get NSData from stream, and save it
CGPDFDataFormat format;
CFDataRef data = CGPDFStreamCopyData(stream, &format);
[((__bridge NSData *)data) writeToURL:resultUrl atomically:NO];
CFRelease(data);
P.S. PostScript syntax of PDF annotation can be different. Different libraries use different syntax, and you should implement all possibilities to make app, that works on almost all PDF files. Sample can be found in this file in vfr Reader

Related

How to display a PBFrame in iOS

I am currently working on an app which uses a live stream from an IP camera(PnP IP/Network camera manufactured by V Star). When connected using the Api provided I get the frame o/p as PBFrames. I am having trouble displaying them. anybody who have been through it please help. Thanks in advance
void *thread_ReceiveVideo(void *arg)
{
NSLog(#"[thread_ReceiveVideo] Starting...");
int avIndex = *(int *)arg;
char *buf = malloc(VIDEO_BUF_SIZE);
unsigned int frmNo;
int ret;
FRAMEINFO_t frameInfo;
while (1)
{
ret = avRecvFrameData(avIndex, buf, VIDEO_BUF_SIZE, (char *)&frameInfo, sizeof(FRAMEINFO_t), &frmNo);
if(ret == AV_ER_DATA_NOREADY)
{
usleep(30000);
continue;
}
else if(ret == AV_ER_LOSED_THIS_FRAME)
{
NSLog(#"Lost video frame NO[%d]", frmNo);
continue;
}
else if(ret == AV_ER_INCOMPLETE_FRAME)
{
NSLog(#"Incomplete video frame NO[%d]", frmNo);
NSLog(#" the biffer is : %lu",sizeof(buf));
continue;
}
else if(ret == AV_ER_SESSION_CLOSE_BY_REMOTE)
{
NSLog(#"[thread_ReceiveVideo] AV_ER_SESSION_CLOSE_BY_REMOTE");
break;
}
else if(ret == AV_ER_REMOTE_TIMEOUT_DISCONNECT)
{
NSLog(#"[thread_ReceiveVideo] AV_ER_REMOTE_TIMEOUT_DISCONNECT");
break;
}
else if(ret == IOTC_ER_INVALID_SID)
{
NSLog(#"[thread_ReceiveVideo] Session cant be used anymore");
break;
}
NSLog(#"FRAM INFO FLAG :%hhu ",frameInfo.flags);
if(frameInfo.flags == IPC_FRAME_FLAG_PBFRAME)
{
// got an PBFrame, draw it.
NSLog(#"got a video iframe to display");
// NSString *iFramestring = [NSString stringWithUTF8String:buf];
printf("correclty recieved frame is %s",buf);
//
}
}
free(buf);
NSLog(#"[thread_ReceiveVideo] thread exit");
return 0;
}

iOS - receiving binary data from server only works in debug mode

I have a question concerning my last post
But I think at first I should provide some code:
This is the function to handle my created object
- (void)handleObject:(NSMutableData *)object {
NSLog(#"[object length] = %d", [object length]);
Byte *byteArray;
byteArray = (Byte *)[object bytes];
switch (byteArray[5]) {
case 0: NSLog(#"Login OK");
break;
case 1: NSLog(#"Exit. Server disconnect");
break;
case 2: NSLog(#"Ping erhalten");
break;
case 4: NSLog(#"Points received");
break;
case 6: NSLog(#"transfer of POLYLINE vector objects");
break;
case 8: NSLog(#"transfer of POLYGON vector objects");
break;
case 9: NSLog(#"transfer of Render Rule");
break;
case 10: {
NSLog(#"end of response for RequestVectorObjects");
isLoading = FALSE;
}
break;
case 11: NSLog(#"Background Colorcode: %d", (byteArray[6] & 0xFF));
break;
default: NSLog(#"From server: default value");
break;
}
}
And I here I receive the data from a stream
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
isLoading = YES;
while (isLoading) {
uint8_t bufSize[4];
int packetLength = 0;
[(NSInputStream *)stream read:bufSize maxLength:4];
packetLength = [self bytesToInt:bufSize withOffset:0];
NSLog(#"packetLength: %d", packetLength);
[tempStore appendBytes:bufSize length:4];
if (packetLength == 25600) {
loggedIn = YES;
uint8_t buf[4];
[(NSInputStream *)stream read:buf maxLength:4];
[tempStore appendBytes:buf length:4];
[self handleObject:tempStore];
}
else {
uint8_t buf[packetLength];
[(NSInputStream *)stream read:buf maxLength:packetLength];
[tempStore appendBytes:buf length:packetLength];
[self handleObject:tempStore];
}
[tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
[tempStore setLength:0];
}
NSLog(#"Finished loading objects");
} break;
}
It all works just fine with the simulator or if I debug the application on the iPhone, but if try to run it on the device I always get BAD_EXCESS, because the value of packetLength is wrong and so I try to read too much byte into my buffer.
Now to my question: How can it be that all values are right in debug mode, but get totally messed up in run mode? Is there anything I could to avoid that problem or I should be aware of?
Your help is much appreciated
Here is my BytesToInt Function, but I think it works as it should, if the received values are correct
- (int)bytesToInt:(Byte *)b withOffset:(int)offset {
int ret = 0;
for (int i = 0; i < 4; i++) {
ret |= (b[offset + i] & 0xFF) << (3-i)*8;
}
return ret;
}
I finally did it by looping the received data until I reached my maxLength value. It wasn't too hard to fix, but I wasn't acting quite smart...
Well, here is how I did it:
isLoading = YES;
while (isLoading) {
uint8_t bufSize[4];
int packetLength , maxLength, len;
maxLength = 4;
len = [(NSInputStream *)stream read:bufSize maxLength:maxLength];
NSLog(#"len = %d", len);
packetLength = [self bytesToInt:bufSize withOffset:0];
NSLog(#"packetLength: %d", packetLength);
[tempStore appendBytes:bufSize length:maxLength];
if (packetLength == 25600) {
loggedIn = YES;
maxLength = 4;
uint8_t buf[maxLength];
len = [(NSInputStream *)stream read:buf maxLength:maxLength];
NSLog(#"len = %d", len);
[tempStore appendBytes:buf length:maxLength];
}
else {
maxLength = packetLength;
BOOL allDataReceived = NO;
while (!allDataReceived) {
uint8_t buf[maxLength];
len = [(NSInputStream *)stream read:buf maxLength:maxLength];
NSLog(#"len = %d", len);
if (len < maxLength) {
maxLength -= len;
[tempStore appendBytes:buf length:len];
}
else {
allDataReceived = YES;
[tempStore appendBytes:buf length:len];
}
}
}
[self handleObject:tempStore];
[tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
[tempStore setLength:0];
}
Check the return value of your [NSInputStream read]. Specifying maxLength does not mean that actually that many bytes will be really read. It can be less, or even 0. If the stream reads only 2 bytes, I guess you will have to issue a second read with a pointer that points behind the bytes already read in your buffer, and with maxLength of the remaining bytes in the buffer.

Noise in sound in ios 4.3.x and 5.0 after recording

I've got an app where I use Core Audio for sound recording. Sound is made from some parts and then must be saved to the device. It was working fine but in some new ios versions I have a noise, something like distortion, in output files. What is the possible reason? ExtAudioFileCreateWithURL is used to create output file and ExtAudioFileSetProperty to set its properties.
Any help will be appreciated.
This code was created by another programmer who is currently unavailable, so I don't have any idea why such hack was implemented.
The way sound buffer is created for different versions of ios:
static BOOL shouldFixData = NO;
static int checkOnce = 1;
if (checkOnce) {
checkOnce = 0;
if (inNumberFrames * 8 == ioData->mBuffers[0].mDataByteSize) {
shouldFixData = YES;
}
}
if (shouldFixData) {
AudioBufferList cutData = {0};
cutData.mNumberBuffers = 1;
cutData.mBuffers[0].mNumberChannels = ioData->mBuffers[0].mNumberChannels;
cutData.mBuffers[0].mDataByteSize = ioData->mBuffers[0].mDataByteSize / 2;
cutData.mBuffers[0].mData = malloc(cutData.mBuffers[0].mDataByteSize);
SInt32* oldData = (SInt32*)ioData->mBuffers[0].mData;
SInt32* newData = (SInt32*)cutData.mBuffers[0].mData;
int count = cutData.mBuffers[0].mDataByteSize/4;
for (int i = 0; i < count; ++i) {
newData[i] = oldData[i*2];
}
ExtAudioFileWriteAsync(userData->outputFile, inNumberFrames, &cutData);
free(cutData.mBuffers[0].mData);
} else {
ExtAudioFileWriteAsync(userData->outputFile, inNumberFrames, ioData);
}
}
Saving the record:
CAStreamBasicDescription dstFormat;
dstFormat.mSampleRate = mOutputFormat.mSampleRate;
dstFormat.mFormatID = kAudioFormatLinearPCM;
dstFormat.mChannelsPerFrame = 2;
dstFormat.mBitsPerChannel = 16;
dstFormat.mBytesPerPacket = 2 * dstFormat.mChannelsPerFrame;
dstFormat.mBytesPerFrame = 2 * dstFormat.mChannelsPerFrame;
dstFormat.mFramesPerPacket = 1;
dstFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
//recordInfo.output file is ExtAudioFileRef
err = ExtAudioFileCreateWithURL((CFURLRef)recordFileURL, kAudioFileWAVEType, &dstFormat, NULL, kAudioFileFlags_EraseFile, &recordInfo.outputFile);
if (err) { printf("ExtAudioFileCreateWithURL result %ld %08X %4.4s\n", err, (unsigned int)err, (char*)&err); return; }
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
NSComparisonResult versionCompareRes = [currSysVer compare:#"4.3" options:NSNumericSearch];
if (versionCompareRes == NSOrderedSame || versionCompareRes == NSOrderedDescending) {
//for new versions
err = ExtAudioFileSetProperty(recordInfo.outputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(mOutputFormat), &mOutputFormat);
if (err) { printf("ExtAudioFileSetProperty result %ld %08X %4.4s\n", err, (unsigned int)err, (char*)&err); return; }
} else {
//for old versions
err = ExtAudioFileSetProperty(recordInfo.outputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(dstFormat), &dstFormat);
if (err) { printf("ExtAudioFileSetProperty result %ld %08X %4.4s\n", err, (unsigned int)err, (char*)&err); return; }
}
sample output file:
output.mp3
You could use SPEEX to do denoising, include speex_preprocess.h first:
SpeexPreprocessState *_spt = speex_preprocess_state_init(NN, 16000);/NN is 320 for sampleRate 16000.
int i=1;
speex_preprocess_ctl(_spt, SPEEX_PREPROCESS_SET_DENOISE, &i);
-(BOOL)doConvert:(void *)data SampleLength:(SInt64)length
{
AudioBufferList *dataBuffer = (AudioBufferList*)data;
AudioSampleType *samples = (AudioSampleType*)dataBuffer->mBuffers[0].mData;
SInt64 count = length / NN;
//short sample_ground[count][NN];
short **sample_group;
sample_group = (short**)malloc(sizeof(short*)*count);
for (int i=0; i<count; i++)
sample_group[i] = (short*)malloc(sizeof(short)*NN);
for (int i = 0; i < count; i++) {
for (int j = 0 ; j < NN; j++) {
short value = samples[i*NN+j];
sample_group[i][j] = value;
}
}
for (int i = 0; i < count; i++) {
speex_preprocess_run(_spt, sample_group[i]);
}
for (int i = 0; i < count; i++) {
for (int j = 0 ; j < NN; j++) {
samples[i*NN+j] = sample_group[i][j];
}
}
for (int i=0; i<count; i++)
free(sample_group[i]);
free(sample_group);
return YES;
}
oops.. should have been feedback. Sorry..
The main difference in the code you posted is the format setup
sizeof(mOutputFormat), &mOutputFormat
where mOutputFormat not specified how its set.
compared to the dst format for older version which is set.
Setting "wrong" format usually gives bad sound :-)

How can I monitor my iPhone's charging status while plugged into my PC?

I'm working on a program that monitors your iPhone's charging statues while plugged into your PC. How can I do this in either VB, BAT or C++?
You can set the following on a timer (in ObjC):
-(NSString *)getBatteryPercent
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource = NULL;
const void *psValue;
int i;
int curCapacity = 0;
int maxCapacity = 0;
int percent = 0;
int numOfSources = CFArrayGetCount(sources);
//if (numOfSources == 0) return 1;
for (i = 0 ; i < numOfSources ; i++)
{
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
percent = (int)((double)curCapacity/(double)maxCapacity * 100);
}
return [NSString stringWithFormat:#"%d",percent];
}
To get the state of the battery:
-(NSString*)batteryStateStatus:(UIDeviceBatteryState)state
{
switch ( state )
{
case UIDeviceBatteryStateUnknown:
return #"Unknown";
break;
case UIDeviceBatteryStateUnplugged:
return #"Unplugged";
break;
case UIDeviceBatteryStateCharging:
return #"Charging";
break;
case UIDeviceBatteryStateFull:
return #"Charged";
break;
}
return nil;
}
EDIT:
Requires the IOKit.framework
#include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>

Trying to find USB device on iphone with IOKit.framework

I'm working on a project were I need the USB port to communicate with an external device. I have been looking for examples on the net (Apple and /developer/IOKit/usb exemple) and trying some others, but I can't even find the device.
In my code, I'm blocking at the place where the function looks for a next iterator (pointer in fact) with the function getNextIterator; but it never returns a good value, so the code is blocking. By the way, I am using toolchain and added IOKit.framework in my project. All I want right now is to communicate or do like a ping to someone on the USB bus! I'm blocking in FindDevice... I can't manage to enter in the while loop because the variable usbDevice is always = to 0... I have tested my code in a small mac program and it works...
Here is my code :
IOReturn ConfigureDevice(IOUSBDeviceInterface **dev) {
UInt8 numConfig;
IOReturn result;
IOUSBConfigurationDescriptorPtr configDesc;
//Get the number of configurations
result = (*dev)->GetNumberOfConfigurations(dev, &numConfig);
if (!numConfig) {
return -1;
}
// Get the configuration descriptor
result = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &configDesc);
if (result) {
NSLog(#"Couldn't get configuration descriptior for index %d (err=%08x)\n", 0, result);
return -1;
}
#ifdef OSX_DEBUG
NSLog(#"Number of Configurations: %d\n", numConfig);
#endif
// Configure the device
result = (*dev)->SetConfiguration(dev, configDesc->bConfigurationValue);
if (result)
{
NSLog(#"Unable to set configuration to value %d (err=%08x)\n", 0, result);
return -1;
}
return kIOReturnSuccess;
}
IOReturn FindInterfaces(IOUSBDeviceInterface **dev, IOUSBInterfaceInterface ***itf) {
IOReturn kr;
IOUSBFindInterfaceRequest request;
io_iterator_t iterator;
io_service_t usbInterface;
IOUSBInterfaceInterface **intf = NULL;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT res;
SInt32 score;
UInt8 intfClass;
UInt8 intfSubClass;
UInt8 intfNumEndpoints;
int pipeRef;
CFRunLoopSourceRef runLoopSource;
NSLog(#"Debut FindInterfaces \n");
request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
usbInterface = IOIteratorNext(iterator);
IOObjectRelease(iterator);
NSLog(#"Interface found.\n");
kr = IOCreatePlugInInterfaceForService(usbInterface, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
kr = IOObjectRelease(usbInterface); // done with the usbInterface object now that I have the plugin
if ((kIOReturnSuccess != kr) || !plugInInterface)
{
NSLog(#"unable to create a plugin (%08x)\n", kr);
return -1;
}
// I have the interface plugin. I need the interface interface
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*) &intf);
(*plugInInterface)->Release(plugInInterface); // done with this
if (res || !intf)
{
NSLog(#"couldn't create an IOUSBInterfaceInterface (%08x)\n", (int) res);
return -1;
}
// Now open the interface. This will cause the pipes to be instantiated that are
// associated with the endpoints defined in the interface descriptor.
kr = (*intf)->USBInterfaceOpen(intf);
if (kIOReturnSuccess != kr)
{
NSLog(#"unable to open interface (%08x)\n", kr);
(void) (*intf)->Release(intf);
return -1;
}
kr = (*intf)->CreateInterfaceAsyncEventSource(intf, &runLoopSource);
if (kIOReturnSuccess != kr)
{
NSLog(#"unable to create async event source (%08x)\n", kr);
(void) (*intf)->USBInterfaceClose(intf);
(void) (*intf)->Release(intf);
return -1;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
if (!intf)
{
NSLog(#"Interface is NULL!\n");
} else
{
*itf = intf;
}
NSLog(#"End of FindInterface \n \n");
return kr;
}
unsigned int FindDevice(void *refCon, io_iterator_t iterator) {
kern_return_t kr;
io_service_t usbDevice;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT result;
SInt32 score;
UInt16 vendor;
UInt16 product;
UInt16 release;
unsigned int count = 0;
NSLog(#"Searching Device....\n");
while (usbDevice = IOIteratorNext(iterator))
{
// create intermediate plug-in
NSLog(#"Found a device!\n");
kr = IOCreatePlugInInterfaceForService(usbDevice,
kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface, &score);
kr = IOObjectRelease(usbDevice);
if ((kIOReturnSuccess != kr) || !plugInInterface) {
NSLog(#"Unable to create a plug-in (%08x)\n", kr);
continue;
}
// Now create the device interface
result = (*plugInInterface)->QueryInterface(plugInInterface,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(LPVOID)&dev);
// Don't need intermediate Plug-In Interface
(*plugInInterface)->Release(plugInInterface);
if (result || !dev) {
NSLog(#"Couldn't create a device interface (%08x)\n",
(int)result);
continue;
}
// check these values for confirmation
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
//kr = (*dev)->GetDeviceReleaseNumber(dev, &release);
//if ((vendor != LegoUSBVendorID) || (product != LegoUSBProductID) || (release != LegoUSBRelease)) {
if ((vendor != LegoUSBVendorID) || (product != LegoUSBProductID))
{
NSLog(#"Found unwanted device (vendor = %d != %d, product = %d != %d, release = %d)\n",
vendor, kUSBVendorID, product, LegoUSBProductID, release);
(void) (*dev)->Release(dev);
continue;
}
// Open the device to change its state
kr = (*dev)->USBDeviceOpen(dev);
if (kr == kIOReturnSuccess) {
count++;
} else {
NSLog(#"Unable to open device: %08x\n", kr);
(void) (*dev)->Release(dev);
continue;
}
// Configure device
kr = ConfigureDevice(dev);
if (kr != kIOReturnSuccess) {
NSLog(#"Unable to configure device: %08x\n", kr);
(void) (*dev)->USBDeviceClose(dev);
(void) (*dev)->Release(dev);
continue;
}
break;
}
return count;
}
// USB rcx Init
IOUSBInterfaceInterface** osx_usb_rcx_init (void)
{
CFMutableDictionaryRef matchingDict;
kern_return_t result;
IOUSBInterfaceInterface **intf = NULL;
unsigned int device_count = 0;
// Create master handler
result = IOMasterPort(MACH_PORT_NULL, &gMasterPort);
if (result || !gMasterPort)
{
NSLog(#"ERR: Couldn't create master I/O Kit port(%08x)\n", result);
return NULL;
}
else {
NSLog(#"Created Master Port.\n");
NSLog(#"Master port 0x:08X \n \n", gMasterPort);
}
// Set up the matching dictionary for class IOUSBDevice and its subclasses
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict) {
NSLog(#"Couldn't create a USB matching dictionary \n");
mach_port_deallocate(mach_task_self(), gMasterPort);
return NULL;
}
else {
NSLog(#"USB matching dictionary : %08X \n", matchingDict);
}
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &LegoUSBVendorID));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &LegoUSBProductID));
result = IOServiceGetMatchingServices(gMasterPort, matchingDict, &gRawAddedIter);
matchingDict = 0; // this was consumed by the above call
// Iterate over matching devices to access already present devices
NSLog(#"RawAddedIter : 0x:%08X \n", &gRawAddedIter);
device_count = FindDevice(NULL, gRawAddedIter);
if (device_count == 1)
{
result = FindInterfaces(dev, &intf);
if (kIOReturnSuccess != result)
{
NSLog(#"unable to find interfaces on device: %08x\n", result);
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return NULL;
}
// osx_usb_rcx_wakeup(intf);
return intf;
}
else if (device_count > 1)
{
NSLog(#"too many matching devices (%d) !\n", device_count);
}
else
{
NSLog(#"no matching devices found\n");
}
return NULL;
}
int main(int argc, char *argv[])
{
int returnCode;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(#"Debut du programme \n \n");
osx_usb_rcx_init();
NSLog(#"Fin du programme \n \n");
return 0;
// returnCode = UIApplicationMain(argc, argv, #"Untitled1App", #"Untitled1App");
// [pool release];
// return returnCode;
}
IOKit is not available for iPhone applications. If you need to connect with external devices from the iPhone you need to sign up for the MFi Program which will provide you with the needed API's and documentation.
besides the appstore rules i dont think u can even touch iokit on iOS without violating the sdk's agreement.