Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2016 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4861aba2280251fe57c6 to your computer and use it in GitHub Desktop.
Save anonymous/4861aba2280251fe57c6 to your computer and use it in GitHub Desktop.
Compile and link ioctl on Linux from Swift
import Glibc
import CLinuxDVB
@_silgen_name("ioctl") func ioctl(fildes: CInt, request: UInt64, result: UnsafePointer<CInt>) -> Int
print("Compiled against DVB API version \(DVB_API_VERSION).\(DVB_API_VERSION)")
class DVBFrontEnd {
var fileReference: CInt? = .None
init?(adapter: Int, number: Int) {
let path: String = "/dev/dvb/adapter\(adapter)/frontend\(number)"
fileReference = open(path, O_RDWR | O_NONBLOCK)
guard fileReference > 0 else {
fileReference = .None
return nil
}
}
deinit {
if let fileReference = self.fileReference {
close(fileReference)
}
}
}
class DVBDVR {
/// The requested DVR buffer size. The integer represents the number of bytes.
static let DVR_BUFFER_SIZE: CInt = 40 * 188 * 1024
var fileReference: CInt? = .None
init?(adapter: Int, number: Int) {
let path: String = "/dev/dvb/adapter\(adapter)/dvr\(number)"
fileReference = open(path, O_RDONLY | O_NONBLOCK)
guard fileReference > 0 else {
fileReference = .None
return nil
}
let DMX_SET_BUFFER_SIZE: CUnsignedInt = 28461
var value = DVBDVR.DVR_BUFFER_SIZE
let result = ioctl(fileReference!, request: UInt64(DMX_SET_BUFFER_SIZE), result: &value)
print(result)
}
deinit {
if let fileReference = self.fileReference {
close(fileReference)
}
}
}
print("Would like to set the DVR buffer size to: \(DVBDVR.DVR_BUFFER_SIZE) bytes.")
if let frontend = DVBFrontEnd(adapter: 0, number: 0), let dvr = DVBDVR(adapter: 0, number: 0) {
print("Connected to the DVB adapters.")
} else {
print("Could not connect to the DVB adapters.")
}
import PackageDescription
let package = Package(
name: "CastableLive",
dependencies: [
.Package(url: "https://github.com/RLovelett/CLinuxDVB.swift.git", Version(1, 0, 0))
]
)
@RLovelett
Copy link

$ swift build
Linking Executable:  .build/debug/CastableLive
/home/ryan/Source/castable-live/.build/debug/CastableLive.o/main.swift.o: In function `_TFC12CastableLive6DVBDVRcfT7adapterSi6numberSi_GSqS0__':
/home/ryan/Source/castable-live/main.swift:40: undefined reference to `ioctl'
/usr/bin/ld: /home/ryan/Source/castable-live/.build/debug/CastableLive: hidden symbol `ioctl' isn't defined
/usr/bin/ld: final link failed: Bad value
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
error: exit(1): ["/usr/bin/swift-build-tool", "-f", "/home/ryan/Source/castable-live/.build/debug/CastableLive.o/llbuild.yaml"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment