2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2008 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
23 Layer7_Broadcast::Layer7_Broadcast (Layer3
* l3
, Trace
* tr
)
26 TRACEPRINTF (t
, 5, this, "L7Broadcast Open");
27 l4
= new T_Broadcast (l3
, tr
, 0);
35 Layer7_Broadcast::~Layer7_Broadcast ()
37 TRACEPRINTF (t
, 5, this, "L7Broadcast Close");
42 bool Layer7_Broadcast::init ()
48 Layer7_Broadcast::A_IndividualAddress_Write (eibaddr_t addr
)
50 A_IndividualAddress_Write_PDU a
;
52 l4
->Send (a
.ToPacket ());
56 Layer7_Broadcast::A_IndividualAddress_Read (unsigned timeout
)
58 Array
< eibaddr_t
> addrs
;
59 A_IndividualAddress_Read_PDU r
;
61 l4
->Send (r
.ToPacket ());
62 pth_event_t t
= pth_event (PTH_EVENT_TIME
, pth_timeout (timeout
, 0));
63 while (pth_event_status (t
) != PTH_STATUS_OCCURRED
)
65 BroadcastComm
*c
= l4
->Get (t
);
68 a
= APDU::fromPacket (c
->data
);
69 if (a
->isResponse (&r
))
71 addrs
.resize (addrs () + 1);
72 addrs
[addrs () - 1] = c
->src
;
78 pth_event_free (t
, PTH_FREE_THIS
);
82 Layer7_Connection::Layer7_Connection (Layer3
* l3
, Trace
* tr
, eibaddr_t d
)
86 l4
= new T_Connection (l3
, tr
, d
);
94 Layer7_Connection::~Layer7_Connection ()
100 bool Layer7_Connection::init ()
106 Layer7_Connection::A_Restart ()
109 l4
->Send (a
.ToPacket ());
113 Layer7_Connection::Request_Response (APDU
* r
)
117 l4
->Send (r
->ToPacket ());
118 pth_event_t t
= pth_event (PTH_EVENT_TIME
, pth_timeout (6, 100));
119 while (pth_event_status (t
) != PTH_STATUS_OCCURRED
)
127 pth_event_free (t
, PTH_FREE_THIS
);
130 a
= APDU::fromPacket (*c
);
132 if (a
->isResponse (r
))
134 pth_event_free (t
, PTH_FREE_THIS
);
138 pth_event_free (t
, PTH_FREE_THIS
);
142 pth_event_free (t
, PTH_FREE_THIS
);
147 Layer7_Connection::A_Property_Read (uchar obj
, uchar propertyid
,
148 uint16_t start
, uchar count
, CArray
& erg
)
150 A_PropertyValue_Read_PDU r
;
153 r
.start
= start
& 0x0fff;
154 r
.count
= count
& 0x0f;
155 APDU
*a
= Request_Response (&r
);
158 A_PropertyValue_Response_PDU
*a1
= (A_PropertyValue_Response_PDU
*) a
;
165 Layer7_Connection::A_Property_Write (uchar obj
, uchar propertyid
,
166 uint16_t start
, uchar count
,
167 const CArray
& data
, CArray
& result
)
169 A_PropertyValue_Write_PDU r
;
172 r
.start
= start
& 0x0fff;
173 r
.count
= count
& 0x0f;
175 APDU
*a
= Request_Response (&r
);
178 A_PropertyValue_Response_PDU
*a1
= (A_PropertyValue_Response_PDU
*) a
;
185 Layer7_Connection::A_Property_Desc (uchar obj
, uchar
& property
,
186 uchar property_index
, uchar
& type
,
187 uint16_t & max_nr_elements
,
190 A_PropertyDescription_Read_PDU r
;
193 r
.property_index
= property_index
;
194 APDU
*a
= Request_Response (&r
);
197 A_PropertyDescription_Response_PDU
*a1
=
198 (A_PropertyDescription_Response_PDU
*) a
;
200 max_nr_elements
= a1
->count
;
208 Layer7_Connection::A_Device_Descriptor_Read (uint16_t & maskver
, uchar type
)
210 A_DeviceDescriptor_Read_PDU r
;
211 r
.type
= type
& 0x3f;
212 APDU
*a
= Request_Response (&r
);
215 A_DeviceDescriptor_Response_PDU
*a1
= (A_DeviceDescriptor_Response_PDU
*) a
;
216 maskver
= a1
->descriptor
;
222 Layer7_Connection::A_ADC_Read (uchar channel
, uchar readcount
,
226 r
.channel
= channel
& 0x3f;
228 APDU
*a
= Request_Response (&r
);
231 A_ADC_Response_PDU
*a1
= (A_ADC_Response_PDU
*) a
;
238 Layer7_Connection::A_Memory_Read (memaddr_t addr
, uchar len
, CArray
& data
)
242 r
.count
= len
& 0x0f;
243 APDU
*a
= Request_Response (&r
);
246 A_Memory_Response_PDU
*a1
= (A_Memory_Response_PDU
*) a
;
253 Layer7_Connection::A_Memory_Write (memaddr_t addr
, const CArray
& data
)
255 A_Memory_Write_PDU r
;
257 r
.count
= data () & 0x0f;
258 r
.data
.set (data
.array (), data () & 0x0f);
259 l4
->Send (r
.ToPacket ());
264 Layer7_Connection::A_Authorize (eibkey_type key
, uchar
& level
)
266 A_Authorize_Request_PDU r
;
268 APDU
*a
= Request_Response (&r
);
271 A_Authorize_Response_PDU
*a1
= (A_Authorize_Response_PDU
*) a
;
278 Layer7_Connection::A_KeyWrite (eibkey_type key
, uchar
& level
)
283 APDU
*a
= Request_Response (&r
);
286 A_Key_Response_PDU
*a1
= (A_Key_Response_PDU
*) a
;
293 Layer7_Connection::X_Property_Write (uchar obj
, uchar propertyid
,
294 uint16_t start
, uchar count
,
298 if (A_Property_Write (obj
, propertyid
, start
, count
, data
, d1
) == -1)
300 if (A_Property_Read (obj
, propertyid
, start
, count
, d1
) == -1)
308 Layer7_Connection::X_Memory_Write (memaddr_t addr
, const CArray
& data
)
311 if (A_Memory_Write (addr
, data
) == -1)
313 if (A_Memory_Read (addr
, data (), d1
) == -1)
321 Layer7_Connection::X_Memory_Write_Block (memaddr_t addr
, const CArray
& data
)
324 int i
, j
, k
, res
= 0;
325 const unsigned blocksize
= 12;
326 if (X_Memory_Read_Block (addr
, data (), prev
) == -1)
328 for (i
= 0; i
< data (); i
++)
330 if (data
[i
] == prev
[i
])
333 while (data
[i
+ j
] != prev
[i
+ j
] && j
< blocksize
&& i
+ j
< data ())
335 k
= X_Memory_Write (addr
+ i
, CArray (data
.array () + i
, j
));
347 Layer7_Connection::X_Memory_Read_Block (memaddr_t addr
, int len
, CArray
& erg
)
349 unsigned blocksize
= 12;
352 for (unsigned i
= 0; i
< len
; i
+= blocksize
)
356 (addr
+ i
, (len
- i
> blocksize
? blocksize
: len
- i
), e
) == -1)
371 Layer7_Connection::A_Memory_Write_Block (memaddr_t addr
, const CArray
& data
)
374 int i
, j
, k
, res
= 0;
375 const unsigned blocksize
= 12;
377 for (i
= 0; i
< data (); i
+= blocksize
)
382 k
= A_Memory_Write (addr
+ i
, CArray (data
.array () + i
, j
));