From d3a0d509c2a49a766acc6056ee8f9192d7e21057 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 9 Mar 2018 17:33:10 +0200 Subject: [PATCH] progress with serial device monitor --- CHANGELOG.md | 1 + README.md | 5 +- .../project.pbxproj | 335 ++++++++ .../SerialDeviceSwift/AppDelegate.swift | 30 + .../AppIcon.appiconset/Contents.json | 58 ++ .../Base.lproj/Main.storyboard | 784 ++++++++++++++++++ .../SerialDeviceSwift/Info.plist | 32 + .../SerialDeviceSwift.entitlements | 12 + .../SerialDeviceSwift/ViewController.swift | 27 + Sources/HIDDeviceMonitor.swift | 2 +- Sources/SerialDevice.swift | 24 + Sources/SerialDeviceMonitor.swift | 79 ++ USBDeviceSwift.podspec | 4 +- USBDeviceSwift.xcodeproj/project.pbxproj | 8 + .../contents.xcworkspacedata | 3 + 15 files changed, 1400 insertions(+), 4 deletions(-) create mode 100644 SerialDeviceSwift/SerialDeviceSwift.xcodeproj/project.pbxproj create mode 100644 SerialDeviceSwift/SerialDeviceSwift/AppDelegate.swift create mode 100644 SerialDeviceSwift/SerialDeviceSwift/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 SerialDeviceSwift/SerialDeviceSwift/Base.lproj/Main.storyboard create mode 100644 SerialDeviceSwift/SerialDeviceSwift/Info.plist create mode 100644 SerialDeviceSwift/SerialDeviceSwift/SerialDeviceSwift.entitlements create mode 100644 SerialDeviceSwift/SerialDeviceSwift/ViewController.swift create mode 100644 Sources/SerialDevice.swift create mode 100644 Sources/SerialDeviceMonitor.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a49e33..6c0d4a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Updated all projects to Switf 4 - Created Wiki documentation [here](https://github.com/Arti3DPlayer/USBDeviceSwift/wiki) +- Added Serial device support ## 1.0.2 - Explicitly set Swift version project-wide diff --git a/README.md b/README.md index 97ad328..0812e28 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # USBDeviceSwift -**USBDeviceSwift** - is a wrapper for `IOKit.usb` and `IOKit.hid` written on pure Swift that allows you convenient work with USB devices. +**USBDeviceSwift** - is a wrapper for `IOKit.usb` and `IOKit.hid` and `IOKit.serial` written on pure Swift that allows you convenient work with USB devices. @@ -13,6 +13,9 @@
+Working with `IOKit.usb` and `IOKit.hid` and `IOKit.serial` on Swift is a pain. A lot of not converted C code, pointers make your life harder. +This library provides basic connect/disconnect events, converted functions to send and receive requests and examples. + ## Getting Started ### Requirements diff --git a/SerialDeviceSwift/SerialDeviceSwift.xcodeproj/project.pbxproj b/SerialDeviceSwift/SerialDeviceSwift.xcodeproj/project.pbxproj new file mode 100644 index 0000000..f070aee --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift.xcodeproj/project.pbxproj @@ -0,0 +1,335 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + 3F8A57A1205287F500E5BD99 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8A57A0205287F500E5BD99 /* AppDelegate.swift */; }; + 3F8A57A3205287F500E5BD99 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8A57A2205287F500E5BD99 /* ViewController.swift */; }; + 3F8A57A5205287F500E5BD99 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3F8A57A4205287F500E5BD99 /* Assets.xcassets */; }; + 3F8A57A8205287F500E5BD99 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3F8A57A6205287F500E5BD99 /* Main.storyboard */; }; + 3F8A57B120528AF300E5BD99 /* USBDeviceSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3F8A57B220528AF300E5BD99 /* USBDeviceSwift.framework */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 3F8A579D205287F500E5BD99 /* SerialDeviceSwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SerialDeviceSwift.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 3F8A57A0205287F500E5BD99 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + 3F8A57A2205287F500E5BD99 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 3F8A57A4205287F500E5BD99 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 3F8A57A7205287F500E5BD99 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + 3F8A57A9205287F500E5BD99 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 3F8A57AA205287F500E5BD99 /* SerialDeviceSwift.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SerialDeviceSwift.entitlements; sourceTree = ""; }; + 3F8A57B220528AF300E5BD99 /* USBDeviceSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = USBDeviceSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 3F8A579A205287F500E5BD99 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 3F8A57B120528AF300E5BD99 /* USBDeviceSwift.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 3F8A5794205287F500E5BD99 = { + isa = PBXGroup; + children = ( + 3F8A579F205287F500E5BD99 /* SerialDeviceSwift */, + 3F8A579E205287F500E5BD99 /* Products */, + 3F8A57B020528AF300E5BD99 /* Frameworks */, + ); + sourceTree = ""; + }; + 3F8A579E205287F500E5BD99 /* Products */ = { + isa = PBXGroup; + children = ( + 3F8A579D205287F500E5BD99 /* SerialDeviceSwift.app */, + ); + name = Products; + sourceTree = ""; + }; + 3F8A579F205287F500E5BD99 /* SerialDeviceSwift */ = { + isa = PBXGroup; + children = ( + 3F8A57A0205287F500E5BD99 /* AppDelegate.swift */, + 3F8A57A2205287F500E5BD99 /* ViewController.swift */, + 3F8A57A4205287F500E5BD99 /* Assets.xcassets */, + 3F8A57A6205287F500E5BD99 /* Main.storyboard */, + 3F8A57A9205287F500E5BD99 /* Info.plist */, + 3F8A57AA205287F500E5BD99 /* SerialDeviceSwift.entitlements */, + ); + path = SerialDeviceSwift; + sourceTree = ""; + }; + 3F8A57B020528AF300E5BD99 /* Frameworks */ = { + isa = PBXGroup; + children = ( + 3F8A57B220528AF300E5BD99 /* USBDeviceSwift.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 3F8A579C205287F500E5BD99 /* SerialDeviceSwift */ = { + isa = PBXNativeTarget; + buildConfigurationList = 3F8A57AD205287F500E5BD99 /* Build configuration list for PBXNativeTarget "SerialDeviceSwift" */; + buildPhases = ( + 3F8A5799205287F500E5BD99 /* Sources */, + 3F8A579A205287F500E5BD99 /* Frameworks */, + 3F8A579B205287F500E5BD99 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SerialDeviceSwift; + productName = SerialDeviceSwift; + productReference = 3F8A579D205287F500E5BD99 /* SerialDeviceSwift.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 3F8A5795205287F500E5BD99 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0920; + LastUpgradeCheck = 0920; + ORGANIZATIONNAME = "Artem Hruzd"; + TargetAttributes = { + 3F8A579C205287F500E5BD99 = { + CreatedOnToolsVersion = 9.2; + ProvisioningStyle = Automatic; + SystemCapabilities = { + com.apple.Sandbox = { + enabled = 1; + }; + }; + }; + }; + }; + buildConfigurationList = 3F8A5798205287F500E5BD99 /* Build configuration list for PBXProject "SerialDeviceSwift" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 3F8A5794205287F500E5BD99; + productRefGroup = 3F8A579E205287F500E5BD99 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 3F8A579C205287F500E5BD99 /* SerialDeviceSwift */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 3F8A579B205287F500E5BD99 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 3F8A57A5205287F500E5BD99 /* Assets.xcassets in Resources */, + 3F8A57A8205287F500E5BD99 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 3F8A5799205287F500E5BD99 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 3F8A57A3205287F500E5BD99 /* ViewController.swift in Sources */, + 3F8A57A1205287F500E5BD99 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 3F8A57A6205287F500E5BD99 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 3F8A57A7205287F500E5BD99 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 3F8A57AB205287F500E5BD99 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "Mac Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 3F8A57AC205287F500E5BD99 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "Mac Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + }; + name = Release; + }; + 3F8A57AE205287F500E5BD99 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = SerialDeviceSwift/SerialDeviceSwift.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = LGTF372X5G; + INFOPLIST_FILE = SerialDeviceSwift/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = ah.com.SerialDeviceSwift; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Debug; + }; + 3F8A57AF205287F500E5BD99 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = SerialDeviceSwift/SerialDeviceSwift.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = LGTF372X5G; + INFOPLIST_FILE = SerialDeviceSwift/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = ah.com.SerialDeviceSwift; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 3F8A5798205287F500E5BD99 /* Build configuration list for PBXProject "SerialDeviceSwift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 3F8A57AB205287F500E5BD99 /* Debug */, + 3F8A57AC205287F500E5BD99 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 3F8A57AD205287F500E5BD99 /* Build configuration list for PBXNativeTarget "SerialDeviceSwift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 3F8A57AE205287F500E5BD99 /* Debug */, + 3F8A57AF205287F500E5BD99 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 3F8A5795205287F500E5BD99 /* Project object */; +} diff --git a/SerialDeviceSwift/SerialDeviceSwift/AppDelegate.swift b/SerialDeviceSwift/SerialDeviceSwift/AppDelegate.swift new file mode 100644 index 0000000..245be4f --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/AppDelegate.swift @@ -0,0 +1,30 @@ +// +// AppDelegate.swift +// SerialDeviceSwift +// +// Created by Artem Hruzd on 3/9/18. +// Copyright © 2018 Artem Hruzd. All rights reserved. +// + +import Cocoa +import USBDeviceSwift + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + //make sure that cfDeviceMonitor always exist + let cfDeviceMonitor = SerialDeviceMonitor() + + func applicationDidFinishLaunching(_ aNotification: Notification) { + // Insert code here to initialize your application + + let cfDeviceDaemon = Thread(target: self.cfDeviceMonitor, selector:#selector(self.cfDeviceMonitor.start), object: nil) + cfDeviceDaemon.start() + } + + func applicationWillTerminate(_ aNotification: Notification) { + // Insert code here to tear down your application + } + + +} + diff --git a/SerialDeviceSwift/SerialDeviceSwift/Assets.xcassets/AppIcon.appiconset/Contents.json b/SerialDeviceSwift/SerialDeviceSwift/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SerialDeviceSwift/SerialDeviceSwift/Base.lproj/Main.storyboard b/SerialDeviceSwift/SerialDeviceSwift/Base.lproj/Main.storyboard new file mode 100644 index 0000000..648aad9 --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/Base.lproj/Main.storyboard @@ -0,0 +1,784 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SerialDeviceSwift/SerialDeviceSwift/Info.plist b/SerialDeviceSwift/SerialDeviceSwift/Info.plist new file mode 100644 index 0000000..a9f1289 --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/Info.plist @@ -0,0 +1,32 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2018 Artem Hruzd. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/SerialDeviceSwift/SerialDeviceSwift/SerialDeviceSwift.entitlements b/SerialDeviceSwift/SerialDeviceSwift/SerialDeviceSwift.entitlements new file mode 100644 index 0000000..9c8ecca --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/SerialDeviceSwift.entitlements @@ -0,0 +1,12 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.device.usb + + com.apple.security.files.user-selected.read-only + + + diff --git a/SerialDeviceSwift/SerialDeviceSwift/ViewController.swift b/SerialDeviceSwift/SerialDeviceSwift/ViewController.swift new file mode 100644 index 0000000..495cbd9 --- /dev/null +++ b/SerialDeviceSwift/SerialDeviceSwift/ViewController.swift @@ -0,0 +1,27 @@ +// +// ViewController.swift +// SerialDeviceSwift +// +// Created by Artem Hruzd on 3/9/18. +// Copyright © 2018 Artem Hruzd. All rights reserved. +// + +import Cocoa + +class ViewController: NSViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override var representedObject: Any? { + didSet { + // Update the view, if already loaded. + } + } + + +} + diff --git a/Sources/HIDDeviceMonitor.swift b/Sources/HIDDeviceMonitor.swift index f4f3591..e921846 100644 --- a/Sources/HIDDeviceMonitor.swift +++ b/Sources/HIDDeviceMonitor.swift @@ -43,7 +43,7 @@ open class HIDDeviceMonitor { IOHIDManagerRegisterDeviceRemovalCallback(managerRef, removalCallback, unsafeBitCast(self, to: UnsafeMutableRawPointer.self)) - RunLoop.current.run(); + RunLoop.current.run() } open func read(_ inResult: IOReturn, inSender: UnsafeMutableRawPointer, type: IOHIDReportType, reportId: UInt32, report: UnsafeMutablePointer, reportLength: CFIndex) { diff --git a/Sources/SerialDevice.swift b/Sources/SerialDevice.swift new file mode 100644 index 0000000..397aca3 --- /dev/null +++ b/Sources/SerialDevice.swift @@ -0,0 +1,24 @@ +// +// SerialDevice.swift +// USBDeviceSwift +// +// Created by Artem Hruzd on 3/9/18. +// Copyright © 2018 Artem Hruzd. All rights reserved. +// + +import Foundation +import IOKit.serial + + +public struct SerialDevice { + let path:String + var name:String? // USB Product Name + var vendorName:String? //USB Vendor Name + var serialNumber:String? //USB Serial Number + var vendorId:Int? //USB Serial Number + var productId:Int? //USB Serial Number + + init(path:String) { + self.path = path + } +} diff --git a/Sources/SerialDeviceMonitor.swift b/Sources/SerialDeviceMonitor.swift new file mode 100644 index 0000000..c0743d5 --- /dev/null +++ b/Sources/SerialDeviceMonitor.swift @@ -0,0 +1,79 @@ +// +// SerialDeviceMonitor.swift +// USBDeviceSwift +// +// Created by Artem Hruzd on 3/9/18. +// Copyright © 2018 Artem Hruzd. All rights reserved. +// + +import Cocoa +import Foundation +import IOKit +import IOKit.serial + + +open class SerialDeviceMonitor { + var serialDevices:[SerialDevice] = [] + + public init() { + } + + private func getParentProperty(device:io_object_t, key:String) -> AnyObject? { + return IORegistryEntrySearchCFProperty(device, kIOServicePlane, key as CFString, kCFAllocatorDefault, IOOptionBits(kIORegistryIterateRecursively | kIORegistryIterateParents)) + } + + func getDeviceClass(device:io_object_t) -> String { + var nameCString:[CChar] = [CChar](repeating: 0, count: 128) + let kern_result = IOObjectGetClass(device, &nameCString) + + if kern_result == KERN_SUCCESS { + return String(cString: nameCString) + } + + return "" + } + + func getDeviceProperty(device:io_object_t, key:String) -> AnyObject? { + let cfKey = key as CFString + let propValue = IORegistryEntryCreateCFProperty(device, cfKey, kCFAllocatorDefault, 0) + + return propValue?.takeUnretainedValue() + } + + func getSerialDevices(iterator: io_iterator_t) { + var newSerialDevices:[SerialDevice] = [] + while case let serialPort = IOIteratorNext(iterator), serialPort != 0 { + guard let calloutDevice = getDeviceProperty(device: serialPort, key: kIOCalloutDeviceKey) as? String else { + continue + } + print("\(kIOCalloutDeviceKey): \(calloutDevice)") + + var sd = SerialDevice(path: calloutDevice) + sd.name = getParentProperty(device: serialPort, key: "USB Product Name") as? String + sd.vendorName = getParentProperty(device: serialPort, key: "USB Vendor Name") as? String + sd.serialNumber = getParentProperty(device: serialPort, key: "USB Serial Number") as? String + sd.vendorId = getParentProperty(device: serialPort, key: "idVendor") as? Int + sd.productId = getParentProperty(device: serialPort, key: "idProduct") as? Int + + newSerialDevices.append(sd) + IOObjectRelease(serialPort) + } + IOObjectRelease(iterator) + + print(newSerialDevices) + } + + @objc open func start() { + //while true { + var portIterator: io_iterator_t = 0 + var result: kern_return_t = KERN_FAILURE + let classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue) as NSMutableDictionary + classesToMatch[kIOSerialBSDTypeKey] = kIOSerialBSDAllTypes + result = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &portIterator) + if result == KERN_SUCCESS { + getSerialDevices(iterator: portIterator) + } + usleep(1000000) + //} + } +} diff --git a/USBDeviceSwift.podspec b/USBDeviceSwift.podspec index 21f81a7..c406edf 100644 --- a/USBDeviceSwift.podspec +++ b/USBDeviceSwift.podspec @@ -17,7 +17,7 @@ Pod::Spec.new do |s| s.name = "USBDeviceSwift" s.version = "1.0.3" - s.summary = "USBDeviceSwift - is a wrapper for `IOKit.usb` and `IOKit.hid` written on pure Swift that allows you convenient work with USB devices." + s.summary = "USBDeviceSwift - is a wrapper for `IOKit.usb` and `IOKit.hid` and `IOKit.serial` written on pure Swift that allows you convenient work with USB devices." # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? @@ -26,7 +26,7 @@ Pod::Spec.new do |s| # * Finally, don't worry about the indent, CocoaPods strips it! s.description = <<-DESC - Working with `IOKit.usb` and `IOKit.hid` on Swift is a pain. A lot of not converted C code, pointers make your life harder. + Working with `IOKit.usb` and `IOKit.hid` and `IOKit.serial` on Swift is a pain. A lot of not converted C code, pointers make your life harder. This library provides basic connect/disconnect events, converted functions to send and receive requests and examples. DESC diff --git a/USBDeviceSwift.xcodeproj/project.pbxproj b/USBDeviceSwift.xcodeproj/project.pbxproj index 07adc04..1d6de9f 100644 --- a/USBDeviceSwift.xcodeproj/project.pbxproj +++ b/USBDeviceSwift.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 3F6C3D881EF134BE0069B6CC /* Supporting Files in Resources */ = {isa = PBXBuildFile; fileRef = 3F6C3D871EF134BE0069B6CC /* Supporting Files */; }; 3F6C3D8A1EF14D4C0069B6CC /* HIDDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C3D891EF14D4C0069B6CC /* HIDDevice.swift */; }; 3F6C3D8C1EF14D740069B6CC /* HIDDeviceMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F6C3D8B1EF14D740069B6CC /* HIDDeviceMonitor.swift */; }; + 3F8A574D2052851400E5BD99 /* SerialDeviceMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8A574C2052851400E5BD99 /* SerialDeviceMonitor.swift */; }; + 3F8A574F2052853000E5BD99 /* SerialDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F8A574E2052853000E5BD99 /* SerialDevice.swift */; }; 3F9E47E91EED5B59001D4FDD /* USBDeviceSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F9E47E71EED5B59001D4FDD /* USBDeviceSwift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3F9E47F01EED5ECF001D4FDD /* USBDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F9E47EF1EED5ECF001D4FDD /* USBDevice.swift */; }; /* End PBXBuildFile section */ @@ -20,6 +22,8 @@ 3F6C3D871EF134BE0069B6CC /* Supporting Files */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "Supporting Files"; sourceTree = ""; }; 3F6C3D891EF14D4C0069B6CC /* HIDDevice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HIDDevice.swift; sourceTree = ""; }; 3F6C3D8B1EF14D740069B6CC /* HIDDeviceMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HIDDeviceMonitor.swift; sourceTree = ""; }; + 3F8A574C2052851400E5BD99 /* SerialDeviceMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SerialDeviceMonitor.swift; sourceTree = ""; }; + 3F8A574E2052853000E5BD99 /* SerialDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SerialDevice.swift; sourceTree = ""; }; 3F9E47E41EED5B59001D4FDD /* USBDeviceSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = USBDeviceSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 3F9E47E71EED5B59001D4FDD /* USBDeviceSwift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = USBDeviceSwift.h; sourceTree = ""; }; 3F9E47EF1EED5ECF001D4FDD /* USBDevice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = USBDevice.swift; sourceTree = ""; }; @@ -69,6 +73,8 @@ 3F4799671EEE923E0095CBFF /* USBDeviceMonitor.swift */, 3F6C3D891EF14D4C0069B6CC /* HIDDevice.swift */, 3F6C3D8B1EF14D740069B6CC /* HIDDeviceMonitor.swift */, + 3F8A574E2052853000E5BD99 /* SerialDevice.swift */, + 3F8A574C2052851400E5BD99 /* SerialDeviceMonitor.swift */, ); path = Sources; sourceTree = ""; @@ -156,6 +162,8 @@ buildActionMask = 2147483647; files = ( 3F9E47F01EED5ECF001D4FDD /* USBDevice.swift in Sources */, + 3F8A574F2052853000E5BD99 /* SerialDevice.swift in Sources */, + 3F8A574D2052851400E5BD99 /* SerialDeviceMonitor.swift in Sources */, 3F4799681EEE923E0095CBFF /* USBDeviceMonitor.swift in Sources */, 3F6C3D8C1EF14D740069B6CC /* HIDDeviceMonitor.swift in Sources */, 3F6C3D8A1EF14D4C0069B6CC /* HIDDevice.swift in Sources */, diff --git a/USBDeviceSwift.xcworkspace/contents.xcworkspacedata b/USBDeviceSwift.xcworkspace/contents.xcworkspacedata index 281003e..a0020ff 100644 --- a/USBDeviceSwift.xcworkspace/contents.xcworkspacedata +++ b/USBDeviceSwift.xcworkspace/contents.xcworkspacedata @@ -1,6 +1,9 @@ + +