71 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.4 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.") {
 | 
						|
			fields := strings.Fields(scanner.Text())
 | 
						|
			busID := fields[1]
 | 
						|
			devID := fields[3][0:3]
 | 
						|
			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)
 | 
						|
	}
 | 
						|
}
 |