Skip to content

Instantly share code, notes, and snippets.

@yuntan
Created February 5, 2015 13:38
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 yuntan/86f715c0fa6f5aed5fea to your computer and use it in GitHub Desktop.
Save yuntan/86f715c0fa6f5aed5fea to your computer and use it in GitHub Desktop.
cgo: function scope variable and package scope variable
Driver Capability:
Driver: "uvcvideo"
Card: "UCAM-DLN130T series"
Bus: "usb-0000:00:14.0-2"
Capabilities: 84000001
package main
/*
#include <linux/videodev2.h>
*/
import "C"
import (
"fmt"
"os"
"syscall"
"unsafe"
)
func main() {
vd, err := os.OpenFile("/dev/video0", os.O_RDWR, 0660)
if err != nil {
fmt.Println(err)
return
}
defer vd.Close()
if err = printInfo(vd); err != nil {
fmt.Println(err)
return
}
}
func ioctl(fd *os.File, op uintptr, arg uintptr) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd.Fd(), op, arg)
if err != 0 {
return err
}
return nil
}
func printInfo(vd *os.File) error {
// get capability info
var caps C.struct_v4l2_capability
err := ioctl(vd, C.VIDIOC_QUERYCAP, uintptr(unsafe.Pointer(&caps)))
if err != nil {
return err
}
fmt.Printf("Driver Capability:\n"+
" Driver: \"%s\"\n"+
" Card: \"%s\"\n"+
" Bus: \"%s\"\n"+
" Capabilities: %08x\n",
caps.driver,
caps.card,
caps.bus_info,
caps.capabilities)
return nil
}
bad file descriptor
package main
/*
#include <linux/videodev2.h>
*/
import "C"
import (
"fmt"
"os"
"syscall"
"unsafe"
)
var vd *os.File
func main() {
vd, err := os.OpenFile("/dev/video0", os.O_RDWR, 0660)
if err != nil {
fmt.Println(err)
return
}
defer vd.Close()
if err = printInfo(); err != nil {
fmt.Println(err)
return
}
}
func ioctl(fd *os.File, op uintptr, arg uintptr) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd.Fd(), op, arg)
if err != 0 {
return err
}
return nil
}
func printInfo() error {
// get capability info
var caps C.struct_v4l2_capability
err := ioctl(vd, C.VIDIOC_QUERYCAP, uintptr(unsafe.Pointer(&caps)))
if err != nil {
return err
}
fmt.Printf("Driver Capability:\n"+
" Driver: \"%s\"\n"+
" Card: \"%s\"\n"+
" Bus: \"%s\"\n"+
" Capabilities: %08x\n",
caps.driver,
caps.card,
caps.bus_info,
caps.capabilities)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment