Some cleanup in chatpad.?pp

This commit is contained in:
Ingo Ruhnke 2011-05-07 19:04:05 +02:00
parent 1a05c597b0
commit 428cf31cb5
3 changed files with 51 additions and 24 deletions

View file

@ -33,6 +33,14 @@ struct USBControlMsg
uint16_t wLength;
};
/*
Chatpad Interface:
==================
bInterfaceNumber 2
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 93
bInterfaceProtocol 2
*/
Chatpad::Chatpad(libusb_device_handle* handle, uint16_t bcdDevice,
bool no_init, bool debug) :
m_init_state(kStateInit1),
@ -105,12 +113,14 @@ Chatpad::Chatpad(libusb_device_handle* handle, uint16_t bcdDevice,
init_uinput();
if (!no_init)
if (no_init)
{
send_command();
m_init_state = kStateKeepAlive_1e;
}
usb_submit_read(6, 5);
send_command();
usb_submit_read(6, 32);
}
Chatpad::~Chatpad()
@ -120,7 +130,6 @@ Chatpad::~Chatpad()
void
Chatpad::init_uinput()
{
log_trace();
struct input_id usbid;
usbid.bustype = 0;
@ -143,7 +152,6 @@ Chatpad::init_uinput()
void
Chatpad::usb_submit_read(int endpoint, int len)
{
log_trace();
assert(!m_read_transfer);
m_read_transfer = libusb_alloc_transfer(0);
@ -166,8 +174,6 @@ Chatpad::usb_submit_read(int endpoint, int len)
void
Chatpad::on_read_data(libusb_transfer* transfer)
{
log_trace();
assert(transfer);
if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
@ -176,7 +182,8 @@ Chatpad::on_read_data(libusb_transfer* transfer)
}
else
{
log_error("chatpad data: " << raw2str(transfer->buffer, transfer->actual_length));
log_tmp("chatpad data: " << usb_transfer_strerror(transfer->status) << " "
<< raw2str(transfer->buffer, transfer->actual_length));
if (transfer->actual_length == 5 && transfer->buffer[0] == 0x00)
{
@ -198,7 +205,6 @@ Chatpad::on_read_data(libusb_transfer* transfer)
void
Chatpad::send_timeout(int msec)
{
log_trace();
// FIMXE: must keep track of sources and destroy them in ~Chatpad()
//assert(m_timeout_id == -1);
//m_timeout_id =
@ -208,11 +214,17 @@ Chatpad::send_timeout(int msec)
void
Chatpad::send_command()
{
log_trace();
log_error("send_command: " << m_init_state);
//log_tmp("send_command: " << m_init_state);
// default init code for m_bcdDevice == 0x0110
uint8_t code[2] = { 0x01, 0x02 };
if (m_bcdDevice == 0x0114)
{
code[0] = 0x09;
code[1] = 0x00;
}
switch(m_init_state)
{
case kStateInit1:
@ -275,7 +287,12 @@ Chatpad::send_command()
void
Chatpad::on_control(libusb_transfer* transfer)
{
log_trace();
log_tmp(m_init_state << " " << usb_transfer_strerror(transfer->status) << " len: " << transfer->actual_length);
if (transfer->actual_length != 0)
{
log_tmp(" PAYLOAD: " << raw2str(transfer->buffer, transfer->actual_length));
}
switch(m_init_state)
{
case kStateInit1:
@ -318,7 +335,6 @@ Chatpad::on_control(libusb_transfer* transfer)
bool
Chatpad::on_timeout()
{
log_trace();
//m_timeout_id = -1;
switch(m_init_state)
{
@ -345,7 +361,6 @@ Chatpad::send_ctrl(uint8_t request_type, uint8_t request, uint16_t value, uint16
uint8_t* data_in, uint16_t length,
libusb_transfer_cb_fn callback, void* userdata)
{
log_trace();
libusb_transfer* transfer = libusb_alloc_transfer(0);
transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER;
transfer->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;

View file

@ -72,9 +72,10 @@ Logger::append_unchecked(LogLevel level, const std::string& str)
switch(level)
{
case kError: std::cout << "[ERROR] "; break;
case kWarning: std::cout << "[WARN] "; break;
case kInfo: std::cout << "[INFO] "; break;
case kWarning: std::cout << "[WARN] "; break;
case kInfo: std::cout << "[INFO] "; break;
case kDebug: std::cout << "[DEBUG] "; break;
case kTmp: std::cout << "[TEMP] "; break;
}
std::cout << str << std::endl;

View file

@ -42,7 +42,10 @@ public:
kInfo,
/** extra verbose debugging messages */
kDebug
kDebug,
/** temporary extra verbose debugging messages */
kTmp
};
private:
@ -94,15 +97,23 @@ public:
} \
} while(false)
/** Write an empty debug level message, thus class and function name
are visible */
#define log_trace() do { \
if (g_logger.get_log_level() >= Logger::kDebug) \
{ \
std::ostringstream x6ac1c382; \
/** Write an empty debug message, thus only class and function
name are visible, log level is ignored, messages are always
printed. Use for temporary messages in development that should not
be part of final release. */
#define log_tmp_trace() do { \
std::ostringstream x6ac1c382; \
x6ac1c382 << log_pretty_print(__PRETTY_FUNCTION__); \
g_logger.append_unchecked(Logger::kDebug, x6ac1c382.str()); \
} \
} while(false)
/** Write an debug message, while ignoring the log level. Use for
temporary messages in development that should not be part of final
release. */
#define log_tmp(text) do { \
std::ostringstream x6ac1c382; \
x6ac1c382 << log_pretty_print(__PRETTY_FUNCTION__) << ": " << text; \
g_logger.append_unchecked(Logger::kDebug, x6ac1c382.str()); \
} while(false)
extern Logger g_logger;