-
Notifications
You must be signed in to change notification settings - Fork 2
/
daq_device_srs.cc
212 lines (161 loc) · 4.42 KB
/
daq_device_srs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "daq_device_srs.h"
#include "srs_utils.h"
#include <unistd.h>
#include <string.h>
#define PORT 6006
// max 16 hybrids + 1 end marker + 1 reserve
#define MAX_PACKETS 18
using namespace std;
daq_device_srs::daq_device_srs(const int eventtype
, const int subeventid
, const char *ipaddr
, const int trigger)
{
m_eventType = eventtype;
m_subeventid = subeventid;
strcpy ( _ip, ipaddr);
_s = 0;
_th = 0;
_broken = 0;
if ((_s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
{
_broken =1;
cout << __LINE__ << " " << __FILE__ << " broken 1" << endl;
perror("srs");
}
memset((char *) &_si_me, 0, sizeof(_si_me));
_si_me.sin_family = AF_INET;
_si_me.sin_port = htons(PORT);
_si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(_s, (sockaddr *)&_si_me, sizeof(_si_me))==-1)
{
_broken =1;
cout << __LINE__ << " " << __FILE__ << " broken 1" << endl;
perror("bind");
}
FD_ZERO(&read_flag);
FD_SET(_s, &read_flag);
if ( trigger)
{
// cout << __LINE__ << " " << __FILE__ << " registering triggerhandler " << endl;
_th = new srsTriggerHandler ( &read_flag, _s);
registerTriggerHandler ( _th);
}
}
daq_device_srs::~daq_device_srs()
{
if ( _th)
{
clearTriggerHandler();
delete _th;
_th = 0;
}
close( _s );
}
// the put_data function
int daq_device_srs::put_data(const int etype, int * adr, const int length )
{
// cout << __LINE__ << " " << __FILE__ << " etype= " << etype << " adr= " << adr << " length= " << length << endl;
if ( _broken )
{
cout << __LINE__ << " " << __FILE__ << " broken ";
identify();
return 0; // we had a catastrophic failure
}
int len = 0;
if (etype != m_eventType ) // not our id
{
return 0;
}
sevt = (subevtdata_ptr) adr;
// set the initial subevent length
sevt->sub_length = SEVTHEADERLENGTH;
// update id's etc
sevt->sub_id = m_subeventid;
sevt->sub_type=4;
sevt->sub_decoding = 70;
sevt->reserved[0] = 0;
sevt->reserved[1] = 0;
if ( length < max_length(etype) )
{
cout << __LINE__ << " " << __FILE__ << " length " << length <<endl;
return 0;
}
unsigned int data;
char *d = (char *) &sevt->data;
struct timeval tv;
struct sockaddr_in si_src;
socklen_t src_len;
int ia;
int received_len;
for ( ia = 0; ia < MAX_PACKETS; ia++) //receive
{
tv.tv_sec = 1;
tv.tv_usec = 0;
int retval = select(_s+1, &read_flag, NULL, NULL, &tv);
if ( retval > 0)
{
src_len = sizeof (si_src);
if ( ( received_len = recvfrom(_s, d, 9000, 0, (sockaddr *) &si_src, &src_len) ) ==-1)
{
perror ( "receive ");
cout << __LINE__ << " " << __FILE__ << " error " << received_len <<endl;
return 0;
}
// cout << __LINE__ << " " << __FILE__ << " got " << received_len << " bytes " << _s << endl;
len += received_len; // we are still counting bytes here
d+= received_len; // and here - update the write pointer
if (received_len == 4)
{
len /= 4; // now we have ints
sevt->sub_padding = len%2;
len = len + (len%2);
sevt->sub_length += len;
// cout << __LINE__ << " " << __FILE__ << " returning " << sevt->sub_length << endl;
return sevt->sub_length;
}
const unsigned int eop_marker = 0xf000f000;
memcpy ( d, &eop_marker, 4); // add the EOP
d+=4;
len+=4;
}
else
{
cout << __LINE__ << " " << __FILE__ << " " << retval << " timeout in ";
identify();
return 0;
}
}
return 0;
}
void daq_device_srs::identify(std::ostream& os) const
{
os << "SRS Device Event Type: " << m_eventType << " Subevent id: " << m_subeventid
<< " IP: " << _ip;
if ( _th) os << " trigger ";
if ( _broken) os << " ** not functional ** ";
os << endl;
}
int daq_device_srs::max_length(const int etype) const
{
if (etype != m_eventType) return 0;
return (MAX_PACKETS*2255 + SEVTHEADERLENGTH);
}
int daq_device_srs::init()
{
//execute the "RO_ENABLE" function, reg 15 on port 6039 ->1
int i = set_value( _ip, 0xF, 0, 6039, 1);
return 0;
}
int daq_device_srs::endrun()
{
//execute the "RO_DISABLE" function, reg 15 on port 6039 ->0
int i = set_value( _ip, 0xF, 0, 6039, 0);
return 0;
}
// the rearm() function
int daq_device_srs::rearm(const int etype)
{
if (etype != m_eventType) return 0;
return 0;
}