70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
//The below C code is compiled and executed using CGo.
|
||
|
|
||
|
//#include <stdio.h>
|
||
|
//#include <unistd.h>
|
||
|
//#include <fcntl.h>
|
||
|
//#include <errno.h>
|
||
|
//#include <sys/ioctl.h>
|
||
|
//
|
||
|
//#include <linux/usbdevice_fs.h>
|
||
|
//
|
||
|
//
|
||
|
//int resetUSB(char *filearg)
|
||
|
//{
|
||
|
// const char *filename;
|
||
|
// int fd;
|
||
|
// int rc;
|
||
|
//
|
||
|
// filename = filearg;
|
||
|
//
|
||
|
// fd = open(filename, O_WRONLY);
|
||
|
// if (fd < 0) {
|
||
|
// perror("Error opening output file");
|
||
|
// return 1;
|
||
|
// }
|
||
|
//
|
||
|
// printf("Resetting USB device %s\n", filename);
|
||
|
// rc = ioctl(fd, USBDEVFS_RESET, 0);
|
||
|
// if (rc < 0) {
|
||
|
// perror("Error in ioctl");
|
||
|
// return 1;
|
||
|
// }
|
||
|
// printf("Reset successful\n");
|
||
|
//
|
||
|
// close(fd);
|
||
|
// return 0;
|
||
|
//}
|
||
|
import "C"
|
||
|
import (
|
||
|
"bufio"
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
lsusbCmd := exec.Command("lsusb")
|
||
|
lsusbOut, err := lsusbCmd.Output()
|
||
|
if err != nil {
|
||
|
log.Fatalf("could not get output of lsusb: %v", err)
|
||
|
}
|
||
|
|
||
|
scanner := bufio.NewScanner(bytes.NewReader(lsusbOut))
|
||
|
for scanner.Scan() {
|
||
|
if strings.Contains(scanner.Text(), "Bose Corp.") {
|
||
|
busID := "002"
|
||
|
devID := "005"
|
||
|
usbFilename := C.CString(fmt.Sprintf("/dev/bus/usb/%s/%s", busID, devID))
|
||
|
C.resetUSB(usbFilename)
|
||
|
}
|
||
|
}
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
fmt.Fprintln(os.Stderr, "reading standard input:", err)
|
||
|
}
|
||
|
}
|