/* $Id: plugin_pop3.c,v 1.10 2006/02/25 13:36:33 geronet Exp $
*
* Plugin to check POP3 mail accounts
*
* Copyright (C) 2004 Javi Garcia Dominguez (aka Stolz) <javi@gsmlandia.com>
* Based on code from pop3check (C) 1999 http://sourceforge.net/projects/pop3check
* Simon Liddington <squidly@users.sourceforge.net> is the pop3check current maintainer.
* The pop3check original author is Steven Radack <steve@lconn.net>.
*
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
*
* This file is a pluging for LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* $Log: plugin_pop3.c,v $
* Revision 1.10 2006/02/25 13:36:33 geronet
* updated indent.sh, applied coding style
*
* Revision 1.9 2005/05/08 04:32:44 reinelt
* CodingStyle added and applied
*
* Revision 1.8 2005/04/01 05:16:04 reinelt
* moved plugin init stuff to a seperate function called on first use
*
* Revision 1.7 2005/01/18 06:30:23 reinelt
* added (C) to all copyright statements
*
* Revision 1.6 2004/06/26 12:05:00 reinelt
*
* uh-oh... the last CVS log message messed up things a lot...
*
* Revision 1.5 2004/06/26 09:27:21 reinelt
*
* added '-W' to CFLAGS
* changed all C++ comments to C ones
* cleaned up a lot of signed/unsigned mistakes
*
* Revision 1.4 2004/05/31 21:05:13 reinelt
*
* fixed lots of bugs in the Cwlinux driver
* do not emit EAGAIN error on the first retry
* made plugin_i2c_sensors a bit less 'chatty'
* moved init and exit functions to the bottom of plugin_pop3
*
* Revision 1.3 2004/05/20 07:14:46 reinelt
* made all local functions stati/* $Id$
* $URL$
*
* GLCD2USB driver for LCD4Linux
* (see http://www.harbaum.org/till/glcd2usb for hardware)
*
* Copyright (C) 2007 Till Harbaum <till@harbaum.org>
*
* This file is part of LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
*
* exported fuctions:
*
* struct DRIVER drv_GLCD2USB
*
*/
/*
* Options:
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/time.h>
#include <usb.h>
#include "debug.h"
#include "cfg.h"
#include "timer.h"
#include "qprintf.h"
#include "plugin.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "widget_keypad.h"
#include "drv.h"
#include "drv_generic_graphic.h"
#include "drv_generic_keypad.h"
/* Numeric constants for 'reportType' parameters */
#define USB_HID_REPORT_TYPE_INPUT 1
#define USB_HID_REPORT_TYPE_OUTPUT 2
#define USB_HID_REPORT_TYPE_FEATURE 3
/* These are the error codes which can be returned by functions of this
* module.
*/
#define USB_ERROR_NONE 0
#define USB_ERROR_ACCESS 1
#define USB_ERROR_NOTFOUND 2
#define USB_ERROR_BUSY 16
#define USB_ERROR_IO 5
/* ------------------------------------------------------------------------ */
#include "glcd2usb.h"
/* ------------------------------------------------------------------------- */
#define USBRQ_HID_GET_REPORT 0x01
#define USBRQ_HID_SET_REPORT 0x09
usb_dev_handle *dev = NULL;
/* USB message buffer */
static union {
unsigned char bytes[132];
display_info_t display_info;
} buffer;
/* ------------------------------------------------------------------------- */
#define IDENT_VENDOR_NUM 0x1c40
#define IDENT_VENDOR_STRING "www.harbaum.org/till/glcd2usb"
#define IDENT_PRODUCT_NUM 0x0525
#define IDENT_PRODUCT_STRING "GLCD2USB"
/* early versions used the ftdi vendor id */
#define IDENT_VENDOR_NUM_OLD 0x0403
#define IDENT_PRODUCT_NUM_OLD 0xc634
static char Name[] = IDENT_PRODUCT_STRING;
/* ------------------------------------------------------------------------- */
static int usbGetString(usb_dev_handle * dev, int index, char *buf, int buflen)
{
char buffer[256];
int rval, i;
if ((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
(USB_DT_STRING << 8) + index, 0x0409, buffer, sizeof(buffer), 1000)) < 0)
return rval;
/* not a string */
if (buffer[1] != USB_DT_STRING)
return 0;
/* string would have been longer than buffer is */
if ((unsigned char) buffer[0] < rval)
rval = (unsigned char) buffer[0];
/* 16 bit unicode -> 8 bit ascii */
rval /= 2;
/* lossy conversion to ISO Latin1 */
for (i = 1; i < rval; i++) {
if (i > buflen) /* destination buffer overflow */
break;
buf[i - 1] = buffer[2 * i];
if (buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
buf[i - 1] = '?';
}
/* terminate string */
buf[i - 1] = 0;
return i - 1;
}
/* ------------------------------------------------------------------------- */
int usbOpenDevice(usb_dev_handle ** device, int vendor, char *vendorName, int product, char *productName)
{
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *handle = NULL;
int errorCode = USB_ERROR_NOTFOUND;
static int didUsbInit = 0;
if (!didUsbInit) {
usb_init();
didUsbInit = 1;
}
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
char string[256];
int len;
handle = usb_open(dev); /* we need to open the device in order to query strings */
if (!handle) {
errorCode = USB_ERROR_ACCESS;
error("%s Warning: cannot open USB device: %s", Name, usb_strerror());
continue;
}
if (vendorName == NULL && productName == NULL) { /* name does not matter */
break;
}
/* now check whether the names match: */
len = usbGetString(handle, dev->descriptor.iManufacturer, string, sizeof(string));
if (len < 0) {
errorCode = USB_ERROR_IO;
error("%s: Cannot query manufacturer for device: %s", Name, usb_strerror());
} else {
errorCode = USB_ERROR_NOTFOUND;
if (strcmp(string, vendorName) == 0) {
len = usbGetString(handle, dev->descriptor.iProduct, string, sizeof(string));
if (len < 0) {
errorCode = USB_ERROR_IO;
fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
} else {
errorCode = USB_ERROR_NOTFOUND;
if (strcmp(string, productName) == 0)
break;
}
}
}
usb_close(handle);
handle = NULL;
}
}
if (handle)
break;
}
if (handle != NULL) {
int rval, retries = 3;
if (usb_set_configuration(handle, 1)) {
fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror());
}
/* now try to claim the interface and detach the kernel HID driver on
* linux and other operating systems which support the call.
*/
while ((rval = usb_claim_interface(handle, 0)) != 0 && retries-- > 0) {
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
if (usb_detach_kernel_driver_np(handle, 0) < 0) {
fprintf(stderr, "Warning: could not detach kernel HID driver: %s\n", usb_strerror());
}
#endif
}
#ifndef __APPLE__
if (rval != 0)
fprintf(stderr, "Warning: could not claim interface\n");
#endif
/* Continue anyway, even if we could not claim the interface. Control transfers
* should still work.
*/
errorCode = 0;
*device = handle;
}
return errorCode;
}
/* ------------------------------------------------------------------------- */
void usbCloseDevice(usb_dev_handle * device)
{
if (device != NULL)
usb_close(device);
}
/* ------------------------------------------------------------------------- */
int usbSetReport(usb_dev_handle * device, int reportType, unsigned char *buffer, int len)
{
int bytesSent;
/* the write command needs some tweaking regarding allowed report lengths */
if (buffer[0] == GLCD2USB_RID_WRITE) {
int i = 0, allowed_lengths[] = { 4 + 4, 8 + 4, 16 + 4, 32 + 4, 64 + 4, 128 + 4 };
if (len > 128 + 4)
error("%s: %d bytes usb report is too long \n", Name, len);
while (allowed_lengths[i] != (128 + 4) && allowed_lengths[i] < len)
i++;
len = allowed_lengths[i];
buffer[0] = GLCD2USB_RID_WRITE + i;
}
bytesSent = usb_control_msg(device, USB_TYPE_CLASS | USB_RECIP_INTERFACE |
USB_ENDPOINT_OUT, USBRQ_HID_SET_REPORT,
reportType << 8 | buffer[0], 0, (char *) buffer, len, 1000);
if (bytesSent != len) {
if (bytesSent < 0)
error("%s: Error sending message: %s", Name, usb_strerror());
return USB_ERROR_IO;
}
return 0;
}
/* ------------------------------------------------------------------------- */
int usbGetReport(usb_dev_handle * device, int reportType, int reportNumber, unsigned char *buffer, int *len)
{
*len = usb_control_msg(device, USB_TYPE_CLASS | USB_RECIP_INTERFACE |
USB_ENDPOINT_IN, USBRQ_HID_GET_REPORT,
reportType << 8 | reportNumber, 0, (char *) buffer, *len, 1000);
if (*len < 0) {
error("%s: Error sending message: %s", Name, usb_strerror());
return USB_ERROR_IO;
}
return 0;
}
char *usbErrorMessage(int errCode)
{
static char buffer[80];
switch (errCode) {
case USB_ERROR_ACCESS:
return "Access to device denied";
case USB_ERROR_NOTFOUND:
return "The specified device was not found";
case USB_ERROR_BUSY:
return "The device is used by another application";
case USB_ERROR_IO:
return "Communication error with device";
default:
sprintf(buffer, "Unknown USB error %d", errCode);
return buffer;
}
return NULL; /* not reached */
}
static char *video_buffer = NULL;
static char *dirty_buffer = NULL;
static void drv_GLCD2USB_blit(const int row, const int col, const int height, const int width)
{
int r, c, err, i, j;
/* update offscreen buffer */
for (r = row; r < row + height; r++) {
for (c = col; c < col + width; c++) {
int x, y, bit;
/* these assignments are display layout dependent */
x = c;
y = r / 8;
bit = r % 8;
i = video_buffer[x + DCOLS * y];
if (drv_generic_graphic_black(r, c))
video_buffer[x + DCOLS * y] |= 1 << bit;
else
video_buffer[x + DCOLS * y] &= ~(1 << bit);
if (video_buffer[x + DCOLS * y] != i)
dirty_buffer[x + DCOLS * y] |= 1 << bit;
}
}
#if 0
/* display what's in the buffer (for debugging) */
for (r = 0; r < DROWS; r++) {
for (c = 0; c < DCOLS; c++) {
if (video_buffer[c + DCOLS * (r / 8)] & (1 << (r % 8)))
putchar('#');