Snippets

Gerad Munsch Reset Bluetooth controller on Lenovo X131e Chromebook

Created by Gerad Munsch
#!/usr/bin/env bash
#
# Resets the bluetooth controller with usbreset
#


##
## determine bluetooth device usb path
##
BT_DEVICE_PATH="/dev/bus/usb/$(lsusb | egrep 'Bluetooth' | sed 's/^Bus \(...\) Device \(...\).*$/\1\/\2/')"


##
## provide user output
##
echo -e "\n\t+--------------------------------+"
echo -e "\t|\033[33;1m RESETTING \033[34mBLUETOOTH\033[33m CONTROLLER\033[0m |"
echo -e "\t+--------------------------------+\n"
echo -e "\033[1mDevice Path:\033[0m\t${BT_DEVICE_PATH}\n\n"


##
## reset USB device
##
usbreset "${BT_DEVICE_PATH}"
/**
 * \file usbreset.c
 * Send a USB port reset to a USB device
 *
 * Compile using:
 * \code{.sh}
 * gcc -o usbreset usbreset.c
 * chmod a+x ./usbreset
 * cp ./usbreset /usr/local/bin/usbreset
 * \endcode
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>


int main(int argc, char **argv) {
	const char *filename;
	int fd;
	int rc;

    /* usbreset takes exactly 1 argument: the USB device filename */
	if (argc != 2) {
		fprintf(stderr, "Usage: usbreset device-filename\n");
		return 1;
	}

    /* Our (only) argument is the device filename */
	filename = argv[1];

    /* Open the file descriptor.. */
	fd = open(filename, O_WRONLY);

    /* ...and make sure it was sucessful */
	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;
}

Comments (0)