fpc OS X types / functions lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- josip · 2016-04-11 17:12 I have program written in fpc that is working just fine, executing from command line on Win / Linux / OS X. With header... uses   {$IFDEF WINDOWS}Windows,{$ENDIF}   {$IFDEF UNIX}BaseUnix, Unix, termio,{$ENDIF}   SysUtils, StrUtils, Crt; Now, I need some OS X specific functions like IOServiceMatching, IOServiceGetMatchingServices... and some types like CFMutableDictionaryRef. Google didn't help, so I am asking here is there are something that can be added to uses statement to include this OS X specific things? ---------------------------------------------------------------------- skalogryz · 2016-04-11 17:31 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  uses  ...  {$IFDEF DARWIN}MacOSAll,{$endif} ---------------------------------------------------------------------- josip · 2016-04-11 20:44 Thank you, but something is still missing. IOService* and IORegistry* functions, things related to IOKit. And where is defined io_iterator_t (io_object_t)? ---------------------------------------------------------------------- Phil · 2016-04-11 20:48 MacOSAll provides access to a couple of the older C frameworks, CoreFoundation and Carbon. CocoaAll provides access to AppKit, Foundation and Quartz, which are Objective C frameworks. Other frameworks can be parsed using Ryan's parser: https://github.com/genericptr/ An exception to this, unfortunately, is IOKit, which I believe is a C++ framework. You can access C functions and Objective C classes directly from FPC programs, but not C++. Is it possible there's a command line utility that provides the information you need that you can shell to? Thanks. -Phil Edit: Useful links: https://developer.apple.com/library/mac/documentation/DeviceDrivers/Co nceptual/IOKitFundamentals/Introduction/Introduction.html https://developer.apple.com/library/mac/documentation/MacOSX/Conceptua l/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html#//app le_ref/doc/uid/TP40001067-CH210-BBCBEAJD ---------------------------------------------------------------------- josip · 2016-04-12 10:15 Program need to scan connected (CDC) serial devices on Win / Linux / OS X, and find the right port name (COM4, ttyACM4, cu.usbmodem1141) selected by VID / PID. I made it for Win / Linux, but on OS X this IOKit functions are needed. I found (related to Delphi) this... https://forums.embarcadero.com/thread.jspa?messageID=767167 but for definition like this... const libIOKit = '/System/Library/Frameworks/IOKit.framework/IOKit'; ... function IOServiceGetMatchingService(masterPort: mach_port_t; matching: CFDictionaryRef): io_service_t; cdecl; external libIOKit name 'IOServiceGetMatchingService'; ... something is not right, because I have fpc linker error Free Pascal Compiler version 2.6.4 [2014/02/26] for i386 Copyright (c) 1993-2014 by Florian Klaempfl and others Target OS: Darwin for i386 Compiling fcd.pas Assembling (pipe) fcd.s Linking flash Undefined symbols for architecture i386:   "_IOServiceGetMatchingServices", referenced from:       _P$FCD_CDCPORT$SHORTSTRING$$BOOLEAN in fcd.o   "_IOServiceMatching", referenced from:       _P$FCD_CDCPORT$SHORTSTRING$$BOOLEAN in fcd.o ld: symbol(s) not found for architecture i386 An error occurred while linking ---------------------------------------------------------------------- Thaddy · 2016-04-12 10:34 First and foremost: update your compiler to a supported one, like (stable) fpc 3.0.0. and not 2.6.4. Still doesn't work? report back! ---------------------------------------------------------------------- bee · 2016-04-12 10:39 What's the version of OS X you're using? I suppose all OS X and Mac today are 64 bit. Unless you're using an old Mac and OS X. If it's indeed 64 bit, then you should use the 64 bit version of FPC as well. ---------------------------------------------------------------------- josip · 2016-04-12 14:39 I am running 64-bit kernel. Calling free pascal compiler in OS X terminal by "fpc" is somehow linked to i386 version. Mac: root# ppcx64 fcd.pas Free Pascal Compiler version 2.6.4 [2014/02/26] for x86_64 Copyright (c) 1993-2014 by Florian Klaempfl and others Target OS: Darwin for x86_64 Compiling fcd.pas Assembling (pipe) fcd.s Linking fcd Undefined symbols for architecture x86_64:   "_IOServiceGetMatchingServices", referenced from:       _P$FCD_CDCPORT$SHORTSTRING$$BOOLEAN in fcd.o   "_IOServiceMatching", referenced from:       _P$FCD_CDCPORT$SHORTSTRING$$BOOLEAN in fcd.o ld: symbol(s) not found for architecture x86_64 An error occurred while linking I have installed 2.6.4 that is last stable version before 3.0. I can install 3.0, but don't know how this is related to IOKit (that is older than any fpc version). ---------------------------------------------------------------------- skalogryz · 2016-04-12 16:05 try not to specify libIOKit explicitly. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IOServiceGetMatchingService(masterPort: mach_port_t;   matching: CFDictionaryRef  ): io_service_t; cdecl; external; instead, use linkframe work directive (adding it to the beginning of the unit) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$linkframework IOKit}  However, if the function is exported as C++ function the default generated name will not work. ---------------------------------------------------------------------- josip · 2016-04-13 09:04 Thanks, skalogryz, there is no linking error any more. I need to translate (part of) something like this from C to FPC. https://delog.wordpress.com/2012/04/27/access-usb-device-on-mac-os-x- using-io-kit/ Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...    CFMutableDictionaryRef matchingDictionary = NULL;    SInt32 idVendor = 0x0000; // set vendor id    SInt32 idProduct = 0x0000; // set product id    io_iterator_t iterator = 0;    io_service_t usbRef;...    matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName);    CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID),        CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor));    CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID),       CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct));    IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &iterator);    usbRef = IOIteratorNext(iterator);    if (usbRef == 0) printf("Device not found\n");    IOObjectRelease(iterator);...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$linkframework IOKit} type  io_object_t = mach_port_t;  io_iterator_t = io_object_t; const  kIOMasterPortDefault: mach_port_t = 0;   kIOUSBDeviceClassName = 'IOUSBDevice';  kUSBVendorID = 'idVendor';  kUSBProductID = 'idProduct'; function IOServiceMatching(name: PChar): CFMutableDictionaryRef; cdecl; external; function IOServiceGetMatchingServices(masterPort: mach_port_t; matching: CFDictionaryRef;  existing: io_iterator_t): kern_return_t; cdecl; external; function IOIteratorNext(name: io_iterator_t): io_object_t; cdecl; external: function FindPort: Boolean;var  matchingDictionary: CFMutableDictionaryRef;  iterator: io_iterator_t;begin  matchingDictionary := nil;  iterator := 0;  matchingDictionary := IOServiceMatching(kIOUSBDeviceClassName);  //CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID),  //  CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, PChar('1234')));  //CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID),  //  CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, PChar('5678')));  IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, iterator);...  I guess that link with IOKit is OK, because IOServiceMatching is executed, but something is wrong with IOServiceGetMatchingServices because code is crashed there. An unhandled exception occurred at $9BB2F16E : EAccessViolation : Access violation $9BB2F16E $9BACD294 $00011CF2 ---------------------------------------------------------------------- skalogryz · 2016-04-13 12:03 btw, IOServiceGetMatchingServices should be declared as following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IOServiceGetMatchingServices(masterPort: mach_port_t;   matching: CFDictionaryRef;  var existing: io_iterator_t): kern_return_t; cdecl; external; Note "var" in front of "existing". It might resolve the issue. The function expects a reference to the variable. Without "var" the function accepts zero (nil) as the reference. Which might cause the AV.  OSX APIs rarely do any sanity check, though they probably should. ---------------------------------------------------------------------- josip · 2016-04-14 14:49 Thank you. Eveything is working now, on Lion (10.7.5) and Mavericks (10.9.5), compiled with 32-bit or 64-bit FPC. Here is part of IOKit that was needed for my program. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses MacOSAll {$linkframework IOKit} //IOKit = '/System/Library/Frameworks/IOKit.framework/IOKit'; type    io_name_t = array[0..128] of Char;  io_object_t = mach_port_t;  io_iterator_t = io_object_t;  io_service_t = io_object_t;  io_registry_entry_t = io_object_t;  IOOptionBits = UInt32; const   kIOMasterPortDefault: mach_port_t = 0;  kIOSerialBSDServiceValue = 'IOSerialBSDClient';  kIOSerialBSDTypeKey      = 'IOSerialBSDClientType';  kIOSerialBSDModemType    = 'IOModemSerialStream';  kIOUSBDeviceClassName    = 'IOUSBDevice';  kUSBVendorID             = 'idVendor';  kUSBProductID            = 'idProduct';  kIOTTYDeviceKey        = 'IOTTYDevice';  kIOCalloutDeviceKey      = 'IOCalloutDevice';  kIOServicePlane          = 'IOService';  kUSBInterfaceNumber      = 'bInterfaceNumber'; function IOServiceMatching(name: PChar): CFMutableDictionaryRef; cdecl; external; function IOServiceGetMatchingServices(masterPort: mach_port_t; matching: CFDictionaryRef;  var existing: io_iterator_t): kern_return_t; cdecl; external; function IOIteratorNext(name: io_iterator_t): io_object_t; cdecl; external; function IORegistryEntryCreateCFProperty(entry: io_registry_entry_t; key: CFStringRef;  allocator: CFAllocatorRef; options: IOOptionBits): CFTypeRef; cdecl; external; function IORegistryEntryGetName(entry: io_registry_entry_t; var name: io_name_t):  kern_return_t; cdecl; external; function IORegistryEntryGetParentEntry(entry: io_registry_entry_t; plane: io_name_t;  var parent: io_registry_entry_t): kern_return_t; cdecl; external; function IORegistryEntrySearchCFProperty(entry: io_registry_entry_t; plane: io_name_t;  key: CFStringRef; allocator: CFAllocatorRef; options: IOOptionBits): CFTypeRef; cdecl; external; function IOObjectRelease(object_: io_object_t): kern_return_t; cdecl; external; ---------------------------------------------------------------------- skalogryz · 2016-04-14 15:26 Great job! any plans on publishing IOKit headers? ---------------------------------------------------------------------- Hansaplast · 2018-03-16 13:07 I know this topic is already 2+ years old, so there is a good chance that I'm not getting a reply. I'm struggling with IOKit at the moment as well, and it's not a very well covered topic (either with Mr. Google or in this forum). @Josip; Well done! Would you be able to post some example code on how to use these functions? It would be very much appreciated ---------------------------------------------------------------------- Phil · 2018-03-16 14:45 Quote from: Hansaplast on March 16, 2018, 02:07:26 pmI'm struggling with IOKit at the moment as well, and it's not a very well covered topic (either with Mr. Google or in this forum). You mean other than Apple's docs? https://developer.apple.com/documentation/iokit/iokitlib.h ---------------------------------------------------------------------- Hansaplast · 2018-03-17 12:02 Yeah I did dig through the Apple docs, but I'm definitely struggling with that as well. Or maybe I should say that doing a 1:1 in Lazarus has proven to be challenging for me  I'm trying to list all drives and read/write straight to drives (kind-a like "dd") without using command line options. I've gone the command line route before and noticed that the output of certain statements change per different macOS release (for example the output of diskutil) - so not the most reliable way. So I consider going the "official" route but at times it's a little over my head. ---------------------------------------------------------------------- josip · 2018-04-23 12:43 Quote from: Hansaplast on March 16, 2018, 02:07:26 pmWould you be able to post some example code on how to use these functions? It would be very much appreciated  I use it for connected USB device detection by VID / PID. It is working fine till then, but I forgot coding details. Code: [Select]{$linkframework IOKit} //IOKit = '/System/Library/Frameworks/IOKit.framework/IOKit'; type   io_name_t = array[0..128] of Char;   io_object_t = mach_port_t;   io_iterator_t = io_object_t;   io_service_t = io_object_t;   io_registry_entry_t = io_object_t;   IOOptionBits = UInt32; const   kIOSerialBSDServiceValue = 'IOSerialBSDClient';   kIOSerialBSDTypeKey    = 'IOSerialBSDClientType';   kIOSerialBSDModemType    = 'IOModemSerialStream';   kIOUSBDeviceClassName    = 'IOUSBDevice';   kUSBVendorID             = 'idVendor';   kUSBProductID            = 'idProduct';   kIOTTYDeviceKey    = 'IOTTYDevice';   kIOCalloutDeviceKey    = 'IOCalloutDevice';   kIOServicePlane    = 'IOService';   kUSBInterfaceNumber      = 'bInterfaceNumber'; function IOServiceMatching(name: PChar): CFMutableDictionaryRef; cdecl; external; function IOServiceGetMatchingServices(masterPort: mach_port_t; matching: CFDictionaryRef;   var existing: io_iterator_t): kern_return_t; cdecl; external; function IOIteratorNext(name: io_iterator_t): io_object_t; cdecl; external; function IORegistryEntryCreateCFProperty(entry: io_registry_entry_t; key: CFStringRef;   allocator: CFAllocatorRef; options: IOOptionBits): CFTypeRef; cdecl; external; function IORegistryEntryGetName(entry: io_registry_entry_t; var name: io_name_t): kern_return_t; cdecl; external; function IORegistryEntryGetParentEntry(entry: io_registry_entry_t; plane: io_name_t;   var parent: io_registry_entry_t): kern_return_t; cdecl; external; function IORegistryEntrySearchCFProperty(entry: io_registry_entry_t; plane: io_name_t;   key: CFStringRef; allocator: CFAllocatorRef; options: IOOptionBits): CFTypeRef; cdecl; external; function IOObjectRelease(object_: io_object_t): kern_return_t; cdecl; external; function RegName(FEntry: io_registry_entry_t): string; var   FName: io_name_t; begin   FillChar(FName, SizeOf(FName), 0);   IORegistryEntryGetName(FEntry, FName);   RegName := StrPas(PChar(@FName)); end; function RegStr(FEntry: io_registry_entry_t; FKey: PChar): string; var   FBuf: array[0..255] of Char;   FCFStr: CFTypeRef;   FStr: string; begin   FCFStr := IORegistryEntryCreateCFProperty(FEntry, CFSTR(FKey), kCFAllocatorDefault, 0);   FillChar(FBuf, SizeOf(FBuf), 0);   CFStringGetPascalString(FCFStr, @FBuf[0], 256, CFStringGetSystemEncoding);   FStr := StrPas(PChar(@FBuf));   FStr := RightStr(FStr, Length(FStr) - 1);   RegStr := FStr; end; function RegNum(FEntry: io_registry_entry_t; FKey: PChar; var FRes: Int): Boolean; var   FCFNumber: CFTypeRef;   FNum: SInt32; begin   RegNum := False;   FCFNumber := IORegistryEntrySearchCFProperty(FEntry, kIOServicePlane, CFSTR(FKey), kCFAllocatorDefault, 0);   if FCFNumber <> nil then   begin     CFNumberGetValue(FCFNumber, kCFNumberSInt32Type, @FNum);     FRes := FNum;     RegNum := True;   end; end; function CdcPort: Boolean; var   FMatchDic: CFMutableDictionaryRef;   FIterator: io_iterator_t;   FDevice, FDeviceOld: io_registry_entry_t;   FGetParentRes: kern_return_t;   FVid, FPid, FIntNum: Int;   FPortName, FPortPath: string; begin   CdcPort := False;   FMatchDic := IOServiceMatching(kIOSerialBSDServiceValue);   CFDictionarySetValue(FMatchDic, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDModemType));   //if IOServiceGetMatchingServices(kIOMasterPortDefault, FMatchDic, FIterator) = kIOReturnSuccess then   if IOServiceGetMatchingServices(0, FMatchDic, FIterator) = kIOReturnSuccess then     while True do     begin       FDevice := IOIteratorNext(FIterator);       if FDevice = 0 then break;       FPortName := RegStr(FDevice, kIOTTYDeviceKey);       if LeftStr(FPortName, 8) = 'usbmodem' then       begin         FPortPath := RegStr(FDevice, kIOCalloutDeviceKey);         //WriteLn(FPortName + ':' + FPortPath);         //WriteLn(RegName(FDevice));         FVid := -1;         FGetParentRes := kIOReturnSuccess;         while (FVid < 0) and (FGetParentRes = kIOReturnSuccess) do         begin           //WriteLn(RegName(FDevice));           FDeviceOld := FDevice;           FGetParentRes := IORegistryEntryGetParentEntry(FDevice, kIOServicePlane, FDevice);           if IOObjectRelease(FDeviceOld) = kIOReturnSuccess then ; //WriteLn('*')           if FGetParentRes = kIOReturnSuccess then           begin             RegNum(FDevice, kUSBVendorID, FVid);             RegNum(FDevice, kUSBProductID, FPid);     RegNum(FDevice, kUSBInterfaceNumber, FIntNum);           end;         end;         //WriteLn(IntToHex(FVid, 4) + ' ' + IntToHex(FPid, 4) + ' ' + IntToHex(FIntNum, 4));         if (FVid = $XXXX) and ((FPid = $YYYY) or (FPid = $ZZZZ)) and (FIntNum = 1) then         begin           GPortLen := GPortLen + 1;           GPortName[GPortLen] := FPortName;           GPortPath[GPortLen] := FPortPath;           if AnsiCompareText(GPortName[0], GPortName[GPortLen]) = 0 then CdcPort := True;         end;       end;   end; end; ---------------------------------------------------------------------- Hansaplast · 2018-04-23 14:22 Awesome! Thanks Josip!