1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of Stesy, the MAGIC Steering System
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 1/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2008
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | ///////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // Ethernet
|
---|
28 | //
|
---|
29 | // Class describing the interface to the Janz card in RawCan mode.
|
---|
30 | //
|
---|
31 | ///////////////////////////////////////////////////////////////////////
|
---|
32 | #include "ethernet.h"
|
---|
33 |
|
---|
34 | #include "MLog.h"
|
---|
35 | #include "MLogManip.h"
|
---|
36 |
|
---|
37 | #include <TSocket.h>
|
---|
38 |
|
---|
39 | ClassImp(Ethernet);
|
---|
40 |
|
---|
41 | using namespace std;
|
---|
42 |
|
---|
43 | // --------------------------------------------------------------------------
|
---|
44 | //
|
---|
45 | // Constructor. Sets logging.
|
---|
46 | // Set the receiving thread to priority -10 and detached.
|
---|
47 | //
|
---|
48 | // Open the device.
|
---|
49 | // reset the device
|
---|
50 | // Enable the fifo buffers
|
---|
51 | // Set the baud rate to the given rate
|
---|
52 | // Disable passthrough of all cobids (all canbus messages)
|
---|
53 | // and switch the can bus communication on
|
---|
54 | //
|
---|
55 | Ethernet::Ethernet(const char *addr, const int tx, const int rx, CanOpen *receiver)
|
---|
56 | : MTcpIpI(rx), Interface(receiver), fTxAddress(addr), fTxPort(tx)
|
---|
57 | {
|
---|
58 | gLog << inf2 << "- Ethernet initialized." << endl;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // --------------------------------------------------------------------------
|
---|
62 | //
|
---|
63 | // Destructor. Stopt the receiver, disables the bus connection and
|
---|
64 | // close the device
|
---|
65 | //
|
---|
66 | Ethernet::~Ethernet()
|
---|
67 | {
|
---|
68 | CancelThread();
|
---|
69 | gLog << inf2 << "- Ethernet stopped." << endl;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // --------------------------------------------------------------------------
|
---|
73 | //
|
---|
74 | void Ethernet::ReadSocket(TSocket &rx)
|
---|
75 | {
|
---|
76 | Int_t pos = -1;
|
---|
77 |
|
---|
78 | Message msg;
|
---|
79 | msg.cmd = M_BCAN_RX_ind;
|
---|
80 | msg.data[0] = 0;
|
---|
81 | msg.data[1] = 0;
|
---|
82 |
|
---|
83 | while (!IsThreadCanceled())
|
---|
84 | {
|
---|
85 | //TThread::CancelPoint();
|
---|
86 |
|
---|
87 | unsigned char c;
|
---|
88 | const Int_t len = rx.RecvRaw(&c, 1);
|
---|
89 |
|
---|
90 | //TThread::CancelPoint();
|
---|
91 |
|
---|
92 | // No data received (non-blocking mode)
|
---|
93 | if (len<0)
|
---|
94 | {
|
---|
95 | usleep(1);
|
---|
96 | continue;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // Data received with zero length!
|
---|
100 | if (len==0)
|
---|
101 | {
|
---|
102 | // THIS MEANS CONNECTIION LOST!!!!
|
---|
103 | cout << "============> LEN==0 (CONNECTION LOST?)" << endl;
|
---|
104 | break; // This break is for TEST PURPOSE FIXME!!!
|
---|
105 | continue;
|
---|
106 | }
|
---|
107 |
|
---|
108 | // Data received
|
---|
109 | if (len>1)
|
---|
110 | {
|
---|
111 | cout << "Data too long!!!" << endl;
|
---|
112 | break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (pos<0)
|
---|
116 | {
|
---|
117 | if (c>=MSGLEN)
|
---|
118 | {
|
---|
119 | cout << "Received data too long (> " << MSGLEN << ")" << endl;
|
---|
120 | break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | msg.len = c;
|
---|
124 | pos = 2;
|
---|
125 | continue;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // if (pos==2 && c==0x0a)
|
---|
129 | // continue;
|
---|
130 |
|
---|
131 | msg.data[pos++] = c;
|
---|
132 | if (pos-2<msg.len)
|
---|
133 | continue;
|
---|
134 |
|
---|
135 | cout << "*** RcvdCanFrame len=" << dec << msg.len << ": ";
|
---|
136 | for (int i=0; i<msg.len; i++)
|
---|
137 | cout << "0x" << setfill('0') << setw(2) << hex << (int)((msg.data+2)[i]) << " ";
|
---|
138 | cout << endl;
|
---|
139 |
|
---|
140 | pos = -1;
|
---|
141 |
|
---|
142 | // String completed
|
---|
143 | HandleMessage(msg);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | // --------------------------------------------------------------------------
|
---|
149 | //
|
---|
150 | // This is IcSendReqBCAN from the Janz software
|
---|
151 | //
|
---|
152 | // /*
|
---|
153 | // * IcSendReqBCAN - Send a CANbus message
|
---|
154 | // *
|
---|
155 | // * Issue request to send a CAN message. <Spec> controls whether to send with
|
---|
156 | // * or without spec/confirmation.
|
---|
157 | // * .CS
|
---|
158 | // * spec action
|
---|
159 | // * 0 send only
|
---|
160 | // * 1 send with confirmation to the host.
|
---|
161 | // * 2 send and echo message to the host.
|
---|
162 | // * 3 send and generate both echo and confirmation.
|
---|
163 | // * .CE
|
---|
164 | // *
|
---|
165 | // * SERVICE: CTXreq, CTXCreq, CTXEreq, CTXCEreq
|
---|
166 | // *
|
---|
167 | // * NOTE:
|
---|
168 | // * Raw ICANOS version of the firmware only.
|
---|
169 | // */
|
---|
170 | //
|
---|
171 | void Ethernet::SendCanFrame(WORD_t cobid, BYTE_t m[8], BYTE_t rtr)
|
---|
172 | {
|
---|
173 | const WORD_t desc = MsgDescr(cobid, 8, rtr);
|
---|
174 |
|
---|
175 | Message msg;
|
---|
176 |
|
---|
177 | // msg.cmd = M_BCAN_TX_req;
|
---|
178 |
|
---|
179 | msg.len = 12;
|
---|
180 | // msg.data[0] = 0;
|
---|
181 | msg.data[1] = msg.len-2;
|
---|
182 | msg.data[2] = word_to_msb(desc);
|
---|
183 | msg.data[3] = word_to_lsb(desc);
|
---|
184 |
|
---|
185 | memcpy(&msg.data[4], m, 8);
|
---|
186 |
|
---|
187 | /*
|
---|
188 | cout << "*** SendCanFrame len=" << dec << msg.len-2 << ": ";
|
---|
189 | for (int i=0; i<msg.len-2; i++)
|
---|
190 | cout << "0x" << setfill('0') << setw(2) << hex << (int)((msg.data+2)[i]) << " ";
|
---|
191 | cout << endl;
|
---|
192 | */
|
---|
193 |
|
---|
194 | // FIXME: MUST BECOME NON-BLOCKING!!!!!
|
---|
195 | cout << "*** Send CanFrame over IP" << endl;
|
---|
196 | // FIXME: MUST BECOME NON-BLOCKING!!!!!
|
---|
197 |
|
---|
198 | MTcpIpO::SendFrame(fTxAddress, fTxPort, (char*)(msg.data+1), msg.len-1);
|
---|
199 |
|
---|
200 | /*
|
---|
201 | const WORD_t desc = MsgDescr(cobid, 8, rtr);
|
---|
202 |
|
---|
203 | Message msg;
|
---|
204 |
|
---|
205 | msg.cmd = M_BCAN_TX_req;
|
---|
206 |
|
---|
207 | msg.len = 12;
|
---|
208 | msg.data[0] = 0;
|
---|
209 | msg.data[1] = 0;
|
---|
210 | msg.data[2] = word_to_msb(desc);
|
---|
211 | msg.data[3] = word_to_lsb(desc);
|
---|
212 |
|
---|
213 | memcpy(&msg.data[4], m, 8);
|
---|
214 |
|
---|
215 | while (!Send(&msg));*/
|
---|
216 | }
|
---|