Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
*
* Copyright (c) 2014 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the 'Software'), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in al
l
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Link with libusb >= 1.0.0 (which uses the newer style api)
*
* gcc -Wall button.c -lusb-1.0.0 -I /usr/include/libusb-1.0/
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <libusb.h>
static bool debug = false;
#define DEBUG(_fmt, ...) if (debug) fprintf(stdout, "brb: " _fmt "\n", ## __VA_A
RGS__)
#define INFO(_fmt, ...) fprintf(stdout, "brb: "_fmt "\n", ## __VA_ARGS__)
#define ERROR(_fmt, ...) fprintf(stderr, "brb: " _fmt "\n", ## __VA_ARGS__)
typedef enum button_state {
BUTTON_STATE_ERROR = -1, //!< Error occurred getting button state
BUTTON_STATE_UNKNOWN = 0,
BUTTON_STATE_LID_CLOSED = 0x15, //!< Button lid is closed
BUTTON_STATE_PRESSED = 0x16, //!< Button is currently depressed (and
lid is open)
BUTTON_STATE_LID_OPEN = 0x17 //!< Button lid is open
} button_states_t;
#define BRB_VID 0x1d34 //!< Big Red Button Vendor ID
#define BRB_PID 0x000d //!< Big Red Button Product ID
#define BRB_POLL_INTERVAL 20000 //!< How long we wait in between polling
the button
static char const *progname; //!< What the binary's called
static bool should_exit = false; //!< If true, break out of the poll loop
.
static bool kernel_was_attached;
/** Find the device, and detach the kernel driver
*
*/
static struct libusb_device_handle *get_button_handle(void){
struct libusb_device_handle *handle = NULL;
int ret;
DEBUG("Attempting to open device (vendor 0x%04x, device 0x%04x)", BRB_VI
D, BRB_PID);
handle = libusb_open_device_with_vid_pid(NULL, BRB_VID, BRB_PID);
if (!handle) {
ERROR("Failed opening device descriptor (you may need to be root
)...");
return NULL;
}
/* If the kernel driver is active, we need to detach it */
if (libusb_kernel_driver_active(handle, 0)) {
DEBUG("Kernel driver active, attempting to detach...");
ret = libusb_detach_kernel_driver(handle, 0);
if (ret < 0 ){
ERROR("Can't detach kernel driver");
return NULL;
}
kernel_was_attached = true;
}
return handle;
}