Use Makefile variables for Python paths
[bcusdk.git] / eibd / backend / bcu1.cpp
bloba51fef49fa422c4acf625037f3c31d2a9b1ec962
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2007 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include "bcu1.h"
25 BCU1DriverLowLevelDriver::BCU1DriverLowLevelDriver (const char *dev,
26 Trace * tr)
28 t = tr;
29 TRACEPRINTF (t, 1, this, "Open");
30 fd = open (dev, O_RDWR);
31 if (fd == -1)
32 throw Exception (DEV_OPEN_FAIL);
34 pth_sem_init (&in_signal);
35 pth_sem_init (&out_signal);
36 pth_sem_init (&send_empty);
37 pth_sem_set_value (&send_empty, 1);
38 getwait = pth_event (PTH_EVENT_SEM, &out_signal);
40 pth_event_t timeout = pth_event (PTH_EVENT_TIME, pth_timeout (0, 1));
41 send_done = pth_event (PTH_EVENT_FD | PTH_UNTIL_FD_EXCEPTION, fd);
42 pth_event_concat (send_done, timeout, NULL);
44 pth_wait (send_done);
46 pth_event_isolate (send_done);
47 pth_event_free (timeout, PTH_FREE_THIS);
49 if (pth_event_status (send_done) != PTH_STATUS_OCCURRED)
51 TRACEPRINTF (t, 1, this, "Driver select extension missing");
52 pth_event_free (send_done, PTH_FREE_THIS);
53 send_done = 0;
56 Start ();
57 TRACEPRINTF (t, 1, this, "Opened");
60 BCU1DriverLowLevelDriver::~BCU1DriverLowLevelDriver ()
62 TRACEPRINTF (t, 1, this, "Close");
63 Stop ();
64 if (send_done)
65 pth_event_free (send_done, PTH_FREE_THIS);
67 pth_event_free (getwait, PTH_FREE_THIS);
69 if (fd != -1)
70 close (fd);
73 bool BCU1DriverLowLevelDriver::Connection_Lost ()
75 return 0;
78 void
79 BCU1DriverLowLevelDriver::Send_Packet (CArray l)
81 CArray pdu;
82 t->TracePacket (1, this, "Send", l);
83 inqueue.put (l);
84 pth_sem_set_value (&send_empty, 0);
85 pth_sem_inc (&in_signal, TRUE);
88 void
89 BCU1DriverLowLevelDriver::SendReset ()
93 bool BCU1DriverLowLevelDriver::Send_Queue_Empty ()
95 return inqueue.isempty ();
98 pth_sem_t *
99 BCU1DriverLowLevelDriver::Send_Queue_Empty_Cond ()
101 return &send_empty;
104 CArray *
105 BCU1DriverLowLevelDriver::Get_Packet (pth_event_t stop)
107 if (stop != NULL)
108 pth_event_concat (getwait, stop, NULL);
110 pth_wait (getwait);
112 if (stop)
113 pth_event_isolate (getwait);
115 if (pth_event_status (getwait) == PTH_STATUS_OCCURRED)
117 pth_sem_dec (&out_signal);
118 CArray *c = outqueue.get ();
119 t->TracePacket (1, this, "Recv", *c);
120 return c;
122 else
123 return 0;
126 void
127 BCU1DriverLowLevelDriver::Run (pth_sem_t * stop1)
129 int i;
130 uchar buf[255];
131 pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1);
132 pth_event_t input = pth_event (PTH_EVENT_SEM, &in_signal);
133 while (pth_event_status (stop) != PTH_STATUS_OCCURRED)
135 pth_event_concat (stop, input, NULL);
136 i = pth_read_ev (fd, buf, sizeof (buf), stop);
137 if (i > 0)
139 t->TracePacket (0, this, "Recv", i, buf);
140 outqueue.put (new CArray (buf, i));
141 pth_sem_inc (&out_signal, 1);
143 pth_event_isolate (stop);
144 if (!inqueue.isempty ())
146 const CArray & c = inqueue.top ();
147 t->TracePacket (0, this, "Send", c);
148 i = pth_write_ev (fd, c.array (), c (), stop);
149 if (i == c ())
151 if (send_done)
152 pth_wait (send_done);
154 pth_sem_dec (&in_signal);
155 inqueue.get ();
156 if (inqueue.isempty ())
157 pth_sem_set_value (&send_empty, 1);
161 pth_event_free (stop, PTH_FREE_THIS);
162 pth_event_free (input, PTH_FREE_THIS);
165 LowLevelDriverInterface::EMIVer BCU1DriverLowLevelDriver::getEMIVer ()
167 return vEMI1;