From b43cbdf0e04b6aa93de593bd7c429dcb3376a015 Mon Sep 17 00:00:00 2001 From: Martin Koegler Date: Wed, 2 Apr 2008 19:11:22 +0200 Subject: [PATCH] LowLevelDriverInterface: don't use exceptions Signed-off-by: Martin Koegler --- eibd/backend/bcu1.cpp | 16 +++++++++++----- eibd/backend/bcu1.h | 1 + eibd/backend/bcu1serial.cpp | 44 +++++++++++++++++++++++++++++++++++--------- eibd/backend/bcu1serial.h | 1 + eibd/backend/ft12.cpp | 38 +++++++++++++++++++++++++++++--------- eibd/backend/ft12.h | 1 + eibd/backend/usbif.cpp | 37 +++++++++++++++++++++++++------------ eibd/backend/usbif.h | 2 ++ eibd/bcu/bcuaddrtab.cpp | 2 ++ eibd/bcu/bcuread.cpp | 2 ++ eibd/libserver/eibusb.cpp | 19 +++++++++++++++++-- eibd/libserver/eibusb.h | 1 + eibd/libserver/emi1.cpp | 2 ++ eibd/libserver/emi2.cpp | 2 ++ eibd/libserver/lowlevel.h | 1 + 15 files changed, 132 insertions(+), 37 deletions(-) diff --git a/eibd/backend/bcu1.cpp b/eibd/backend/bcu1.cpp index 062fbce..9181e97 100644 --- a/eibd/backend/bcu1.cpp +++ b/eibd/backend/bcu1.cpp @@ -26,16 +26,17 @@ BCU1DriverLowLevelDriver::BCU1DriverLowLevelDriver (const char *dev, Trace * tr) { t = tr; - TRACEPRINTF (t, 1, this, "Open"); - fd = open (dev, O_RDWR); - if (fd == -1) - throw Exception (DEV_OPEN_FAIL); - pth_sem_init (&in_signal); pth_sem_init (&out_signal); pth_sem_init (&send_empty); pth_sem_set_value (&send_empty, 1); getwait = pth_event (PTH_EVENT_SEM, &out_signal); + send_done = 0; + + TRACEPRINTF (t, 1, this, "Open"); + fd = open (dev, O_RDWR); + if (fd == -1) + return; pth_event_t timeout = pth_event (PTH_EVENT_TIME, pth_timeout (0, 1)); send_done = pth_event (PTH_EVENT_FD | PTH_UNTIL_FD_EXCEPTION, fd); @@ -70,6 +71,11 @@ BCU1DriverLowLevelDriver::~BCU1DriverLowLevelDriver () close (fd); } +bool BCU1DriverLowLevelDriver::init () +{ + return fd != -1; +} + bool BCU1DriverLowLevelDriver::Connection_Lost () { return 0; diff --git a/eibd/backend/bcu1.h b/eibd/backend/bcu1.h index 4056f49..8d22a19 100644 --- a/eibd/backend/bcu1.h +++ b/eibd/backend/bcu1.h @@ -48,6 +48,7 @@ class BCU1DriverLowLevelDriver:public LowLevelDriverInterface, private Thread public: BCU1DriverLowLevelDriver (const char *device, Trace * tr); ~BCU1DriverLowLevelDriver (); + bool init (); void Send_Packet (CArray l); bool Send_Queue_Empty (); diff --git a/eibd/backend/bcu1serial.cpp b/eibd/backend/bcu1serial.cpp index 0d91eba..b8889e3 100644 --- a/eibd/backend/bcu1serial.cpp +++ b/eibd/backend/bcu1serial.cpp @@ -85,19 +85,38 @@ BCU1SerialLowLevelDriver::BCU1SerialLowLevelDriver (const char *dev, struct termios ti; t = tr; + pth_sem_init (&in_signal); + pth_sem_init (&out_signal); + pth_sem_init (&send_empty); + pth_sem_set_value (&send_empty, 1); + getwait = pth_event (PTH_EVENT_SEM, &out_signal); + TRACEPRINTF (t, 1, this, "Open"); fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC); if (fd == -1) - throw Exception (DEV_OPEN_FAIL); + return; set_low_latency (fd, &sold); close (fd); fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC); if (fd == -1) - throw Exception (DEV_OPEN_FAIL); + return; - tcgetattr (fd, &told); + if (tcgetattr (fd, &told)) + { + restore_low_latency (fd, &sold); + close (fd); + fd = -1; + return; + } + if (tcgetattr (fd, &ti)) + { + restore_low_latency (fd, &sold); + close (fd); + fd = -1; + return; + } ti.c_cflag = CS8 | CLOCAL | CREAD; ti.c_iflag = IGNBRK | INPCK | ISIG; ti.c_oflag = 0; @@ -107,16 +126,17 @@ BCU1SerialLowLevelDriver::BCU1SerialLowLevelDriver (const char *dev, cfsetospeed (&ti, B9600); cfsetispeed (&ti, 0); - tcsetattr (fd, TCSAFLUSH, &ti); + if (tcsetattr (fd, TCSAFLUSH, &ti)) + { + restore_low_latency (fd, &sold); + close (fd); + fd = -1; + return; + } setstat (getstat () & ~(TIOCM_RTS | TIOCM_CTS)); while ((getstat () & TIOCM_CTS)); - pth_sem_init (&in_signal); - pth_sem_init (&out_signal); - pth_sem_init (&send_empty); - pth_sem_set_value (&send_empty, 1); - getwait = pth_event (PTH_EVENT_SEM, &out_signal); Start (); TRACEPRINTF (t, 1, this, "Opened"); } @@ -135,6 +155,12 @@ BCU1SerialLowLevelDriver::~BCU1SerialLowLevelDriver () } } +bool +BCU1SerialLowLevelDriver::init () +{ + return fd != -1; +} + int BCU1SerialLowLevelDriver::getstat () { diff --git a/eibd/backend/bcu1serial.h b/eibd/backend/bcu1serial.h index 715fe29..2ebe754 100644 --- a/eibd/backend/bcu1serial.h +++ b/eibd/backend/bcu1serial.h @@ -62,6 +62,7 @@ class BCU1SerialLowLevelDriver:public LowLevelDriverInterface, private Thread public: BCU1SerialLowLevelDriver (const char *device, Trace * tr); ~BCU1SerialLowLevelDriver (); + bool init (); void Send_Packet (CArray l); bool Send_Queue_Empty (); diff --git a/eibd/backend/ft12.cpp b/eibd/backend/ft12.cpp index 822b029..c0feaad 100644 --- a/eibd/backend/ft12.cpp +++ b/eibd/backend/ft12.cpp @@ -27,14 +27,29 @@ FT12LowLevelDriver::FT12LowLevelDriver (const char *dev, Trace * tr) { struct termios t1; t = tr; + pth_sem_init (&in_signal); + pth_sem_init (&out_signal); + pth_sem_init (&send_empty); + pth_sem_set_value (&send_empty, 1); + getwait = pth_event (PTH_EVENT_SEM, &out_signal); TRACEPRINTF (t, 1, this, "Open"); fd = open (dev, O_RDWR | O_NOCTTY); if (fd == -1) - throw Exception (DEV_OPEN_FAIL); - tcgetattr (fd, &old); + return; + if (tcgetattr (fd, &old)) + { + close (fd); + fd = -1; + return; + } - tcgetattr (fd, &t1); + if (tcgetattr (fd, &t1)) + { + close (fd); + fd = -1; + return; + } t1.c_cflag = CS8 | PARENB | CLOCAL | CREAD; t1.c_iflag = IGNBRK | INPCK | ISIG; t1.c_oflag = 0; @@ -44,16 +59,16 @@ FT12LowLevelDriver::FT12LowLevelDriver (const char *dev, Trace * tr) cfsetospeed (&t1, B19200); cfsetispeed (&t1, 0); - tcsetattr (fd, TCSAFLUSH, &t1); + if (tcsetattr (fd, TCSAFLUSH, &t1)) + { + close (fd); + fd = -1; + return; + } sendflag = 0; recvflag = 0; repeatcount = 0; mode = 0; - pth_sem_init (&in_signal); - pth_sem_init (&out_signal); - pth_sem_init (&send_empty); - pth_sem_set_value (&send_empty, 1); - getwait = pth_event (PTH_EVENT_SEM, &out_signal); Start (); TRACEPRINTF (t, 1, this, "Opened"); } @@ -74,6 +89,11 @@ FT12LowLevelDriver::~FT12LowLevelDriver () } } +bool FT12LowLevelDriver::init () +{ + return fd != -1; +} + void FT12LowLevelDriver::Send_Packet (CArray l) { diff --git a/eibd/backend/ft12.h b/eibd/backend/ft12.h index db30a7f..1cf940d 100644 --- a/eibd/backend/ft12.h +++ b/eibd/backend/ft12.h @@ -61,6 +61,7 @@ class FT12LowLevelDriver:public LowLevelDriverInterface, private Thread public: FT12LowLevelDriver (const char *device, Trace * tr); ~FT12LowLevelDriver (); + bool init (); void Send_Packet (CArray l); bool Send_Queue_Empty (); diff --git a/eibd/backend/usbif.cpp b/eibd/backend/usbif.cpp index f2c4bfc..3c8ec14 100644 --- a/eibd/backend/usbif.cpp +++ b/eibd/backend/usbif.cpp @@ -203,31 +203,35 @@ USBLowLevelDriver::USBLowLevelDriver (const char *Dev, Trace * tr) unsigned char devnum; t = tr; + pth_sem_init (&in_signal); + pth_sem_init (&out_signal); + pth_sem_init (&send_empty); + pth_sem_set_value (&send_empty, 1); + getwait = pth_event (PTH_EVENT_SEM, &out_signal); + TRACEPRINTF (t, 1, this, "Detect"); USBEndpoint e = parseUSBEndpoint (Dev); d = detectUSBEndpoint (e); + state = 0; if (d.dev == -1) - throw Exception (DEV_OPEN_FAIL); + return; libusb_get_devnum (d.dev, &devnum); libusb_get_bus_id (d.dev, &bus); TRACEPRINTF (t, 1, this, "Using %d (%d:%d:%d:%d) (%d:%d)", d.dev, libusb_get_busnum (bus), devnum, d.config, d.interface, d.sendep, d.recvep); if (libusb_open (d.dev, &dev) < 0) - throw Exception (DEV_OPEN_FAIL); + return; + state = 1; TRACEPRINTF (t, 1, this, "Open"); libusb_detach_kernel_driver_np (dev, d.interface); if (libusb_set_configuration (dev, d.config) < 0) - throw Exception (DEV_OPEN_FAIL); + return; if (libusb_claim_interface (dev, d.interface) < 0) - throw Exception (DEV_OPEN_FAIL); + return; TRACEPRINTF (t, 1, this, "Claimed"); + state = 2; - pth_sem_init (&in_signal); - pth_sem_init (&out_signal); - pth_sem_init (&send_empty); - pth_sem_set_value (&send_empty, 1); - getwait = pth_event (PTH_EVENT_SEM, &out_signal); Start (); TRACEPRINTF (t, 1, this, "Opened"); } @@ -239,12 +243,21 @@ USBLowLevelDriver::~USBLowLevelDriver () pth_event_free (getwait, PTH_FREE_THIS); TRACEPRINTF (t, 1, this, "Release"); - libusb_release_interface (dev, d.interface); - libusb_attach_kernel_driver_np (dev, d.interface); + if (state > 0) + { + libusb_release_interface (dev, d.interface); + libusb_attach_kernel_driver_np (dev, d.interface); + } TRACEPRINTF (t, 1, this, "Close"); - libusb_close (dev); + if (state > 0) + libusb_close (dev); } +bool +USBLowLevelDriver::init () +{ + return state == 2; +} bool USBLowLevelDriver::Connection_Lost () diff --git a/eibd/backend/usbif.h b/eibd/backend/usbif.h index c79bf2a..e238c99 100644 --- a/eibd/backend/usbif.h +++ b/eibd/backend/usbif.h @@ -61,12 +61,14 @@ class USBLowLevelDriver:public LowLevelDriverInterface, private Thread pth_event_t getwait; /** semaphore to signal empty sendqueue */ pth_sem_t send_empty; + int state; void Run (pth_sem_t * stop); public: USBLowLevelDriver (const char *device, Trace * tr); ~USBLowLevelDriver (); + bool init (); void Send_Packet (CArray l); bool Send_Queue_Empty (); diff --git a/eibd/bcu/bcuaddrtab.cpp b/eibd/bcu/bcuaddrtab.cpp index b1b9a07..f4d2149 100644 --- a/eibd/bcu/bcuaddrtab.cpp +++ b/eibd/bcu/bcuaddrtab.cpp @@ -177,6 +177,8 @@ main (int ac, char *ag[]) try { iface = Create (ag[index], &t); + if (!iface->init ()) + throw Exception (DEV_OPEN_FAIL); } catch (Exception e) { diff --git a/eibd/bcu/bcuread.cpp b/eibd/bcu/bcuread.cpp index e6b5bbf..e0f7729 100644 --- a/eibd/bcu/bcuread.cpp +++ b/eibd/bcu/bcuread.cpp @@ -165,6 +165,8 @@ main (int ac, char *ag[]) try { iface = Create (ag[index], &t); + if (!iface->init ()) + throw Exception (DEV_OPEN_FAIL); } catch (Exception e) { diff --git a/eibd/libserver/eibusb.cpp b/eibd/libserver/eibusb.cpp index 0e820b8..446ece4 100644 --- a/eibd/libserver/eibusb.cpp +++ b/eibd/libserver/eibusb.cpp @@ -35,6 +35,12 @@ initUSBDriver (LowLevelDriverInterface * i, Trace * tr) 0x01 }; + if (!i->init ()) + { + delete i; + return 0; + } + i->Send_Packet (CArray (ask, sizeof (ask))); do { @@ -103,8 +109,8 @@ initUSBDriver (LowLevelDriverInterface * i, Trace * tr) break; default: TRACEPRINTF (tr, 1, i, "Unsupported EMI %02x %02x", r1[12], r1[13]); - throw Exception (DEV_OPEN_FAIL); - break; + delete i; + return 0; } return iface; } @@ -136,6 +142,11 @@ USBConverterInterface::~USBConverterInterface () delete i; } +bool USBConverterInterface::init () +{ + return i->init (); +} + void USBConverterInterface::Send_Packet (CArray l) { @@ -260,6 +271,9 @@ USBLayer2Interface::USBLayer2Interface (LowLevelDriverInterface * i, { emi = 0; LowLevelDriverInterface *iface = initUSBDriver (i, tr); + if (!iface) + throw Exception (DEV_OPEN_FAIL); + switch (iface->getEMIVer ()) { case LowLevelDriverInterface::vEMI1: @@ -270,6 +284,7 @@ USBLayer2Interface::USBLayer2Interface (LowLevelDriverInterface * i, break; default: TRACEPRINTF (tr, 2, this, "Unsupported EMI"); + delete iface; throw Exception (DEV_OPEN_FAIL); } } diff --git a/eibd/libserver/eibusb.h b/eibd/libserver/eibusb.h index e6bf985..22fadb9 100644 --- a/eibd/libserver/eibusb.h +++ b/eibd/libserver/eibusb.h @@ -33,6 +33,7 @@ public: USBConverterInterface (LowLevelDriverInterface * iface, Trace * tr, EMIVer ver); virtual ~ USBConverterInterface (); + bool init (); void Send_Packet (CArray l); bool Send_Queue_Empty (); diff --git a/eibd/libserver/emi1.cpp b/eibd/libserver/emi1.cpp index a03be06..f9af53e 100644 --- a/eibd/libserver/emi1.cpp +++ b/eibd/libserver/emi1.cpp @@ -80,6 +80,8 @@ EMI1Layer2Interface::EMI1Layer2Interface (LowLevelDriverInterface * i, vmode = 0; pth_sem_init (&out_signal); getwait = pth_event (PTH_EVENT_SEM, &out_signal); + if (!iface->init ()) + throw Exception (DEV_OPEN_FAIL); Start (); TRACEPRINTF (tr, 2, this, "Opened"); } diff --git a/eibd/libserver/emi2.cpp b/eibd/libserver/emi2.cpp index 4dd4d47..29bdf3b 100644 --- a/eibd/libserver/emi2.cpp +++ b/eibd/libserver/emi2.cpp @@ -80,6 +80,8 @@ EMI2Layer2Interface::EMI2Layer2Interface (LowLevelDriverInterface * i, vmode = 0; pth_sem_init (&out_signal); getwait = pth_event (PTH_EVENT_SEM, &out_signal); + if (!iface->init ()) + throw Exception (DEV_OPEN_FAIL); Start (); TRACEPRINTF (tr, 2, this, "Opened"); } diff --git a/eibd/libserver/lowlevel.h b/eibd/libserver/lowlevel.h index 1d84956..6918291 100644 --- a/eibd/libserver/lowlevel.h +++ b/eibd/libserver/lowlevel.h @@ -32,6 +32,7 @@ public: virtual ~ LowLevelDriverInterface () { } + virtual bool init () = 0; /** sends a EMI frame asynchronous */ virtual void Send_Packet (CArray l) = 0; -- 2.11.4.GIT