Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / usrp / host / lib / inband / usrp_server.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <usrp_server.h>
26 #include <iostream>
27 #include <usrp_inband_usb_packet.h>
28 #include <mb_class_registry.h>
29 #include <vector>
30
31 typedef usrp_inband_usb_packet transport_pkt;   // makes conversion to gigabit easy
32
33 // FIXME We should machine generate these by a simple preprocessor run over this file
34 //
35 // These are all the messages that we expect to receive.
36 //
37 // We "intern" these here (make them into symbols) so that our
38 // comparisions below are effectively pointer comparisons.
39
40 static pmt_t s_cmd_allocate_channel = pmt_intern("cmd-allocate-channel");
41 static pmt_t s_cmd_close = pmt_intern("cmd-close");
42 static pmt_t s_cmd_deallocate_channel = pmt_intern("cmd-deallocate-channel");
43 static pmt_t s_cmd_open = pmt_intern("cmd-open");
44 static pmt_t s_cmd_start_recv_raw_samples = pmt_intern("cmd-start-recv-raw-samples");
45 static pmt_t s_cmd_stop_recv_raw_samples = pmt_intern("cmd-stop-recv-raw-samples");
46 static pmt_t s_cmd_to_control_channel = pmt_intern("cmd-to-control-channel");
47 static pmt_t s_cmd_xmit_raw_frame  = pmt_intern("cmd-xmit-raw-frame");
48 static pmt_t s_cmd_max_capacity  = pmt_intern("cmd-max-capacity");
49 static pmt_t s_cmd_ntx_chan  = pmt_intern("cmd-ntx-chan");
50 static pmt_t s_cmd_nrx_chan  = pmt_intern("cmd-nrx-chan");
51 static pmt_t s_cmd_current_capacity_allocation  = pmt_intern("cmd-current-capacity-allocation");
52 static pmt_t s_response_allocate_channel = pmt_intern("response-allocate-channel");
53 static pmt_t s_response_close = pmt_intern("response-close");
54 static pmt_t s_response_deallocate_channel = pmt_intern("response-deallocate-channel");
55 static pmt_t s_response_from_control_channel = pmt_intern("response-from-control-channel");
56 static pmt_t s_response_open = pmt_intern("response-open");
57 static pmt_t s_response_recv_raw_samples = pmt_intern("response-recv-raw-samples");
58 static pmt_t s_response_xmit_raw_frame = pmt_intern("response-xmit-raw-frame");
59 static pmt_t s_response_max_capacity = pmt_intern("response-max-capacity");
60 static pmt_t s_response_ntx_chan = pmt_intern("response-ntx-chan");
61 static pmt_t s_response_nrx_chan = pmt_intern("response-nrx-chan");
62 static pmt_t s_response_current_capacity_allocation  = pmt_intern("response-current-capacity-allocation");
63
64 static std::string
65 str(long x)
66 {
67   std::ostringstream s;
68   s << x;
69   return s.str();
70 }
71
72 usrp_server::usrp_server(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
73   : mb_mblock(rt, instance_name, user_arg)
74 {
75   // define our ports
76
77   // control & status port
78   d_cs = define_port("cs", "usrp-server-cs", true, mb_port::EXTERNAL);  
79
80   // ports
81   //
82   // (if/when we do replicated ports, these will be replaced by a
83   //  single replicated port)
84   for(int port=0; port < N_PORTS; port++) {
85     d_tx.push_back(define_port("tx"+str(port), "usrp-tx", true, mb_port::EXTERNAL));
86     d_rx.push_back(define_port("rx"+str(port), "usrp-rx", true, mb_port::EXTERNAL));
87   }
88
89   // FIXME ... initializing to 2 channels on each for now, eventually we should
90   // query the FPGA to get these values
91   d_ntx_chan = 2;
92   d_nrx_chan = 2;
93
94   // Initialize capacity on each channel to 0 and to no owner
95   for(int chan=0; chan < d_ntx_chan; chan++) {
96     d_chaninfo_tx[chan].assigned_capacity = 0;
97     d_chaninfo_tx[chan].owner = PMT_NIL;
98   }
99   for(int chan=0; chan < d_nrx_chan; chan++) {
100     d_chaninfo_rx[chan].assigned_capacity = 0;
101     d_chaninfo_rx[chan].owner = PMT_NIL;
102   }
103 }
104
105 usrp_server::~usrp_server()
106 {
107 }
108
109
110 void
111 usrp_server::initial_transition()
112 {
113   // the initial transition
114 }
115
116 void
117 usrp_server::handle_message(mb_message_sptr msg)
118 {
119   pmt_t event = msg->signal();          // the "name" of the message
120   pmt_t port_id = msg->port_id();       // which port it came in on
121   pmt_t data = msg->data();
122   pmt_t metadata = msg->metadata();
123   pmt_t invocation_handle;
124   pmt_t reply_data;
125   pmt_t status;
126
127   if (1){
128     std::cout << "[USRP_SERVER] event: " << event << std::endl;
129     std::cout << "[USRP_SERVER] port_id: " << port_id << std::endl;
130   }
131
132   // It would be nice if this were all table driven, and we could
133   // compute our state transition as f(current_state, port_id, signal)
134
135   if (pmt_eq(port_id, d_cs->port_symbol())){    // message came in on our control/status port
136
137     if (pmt_eq(event, s_cmd_open)){
138       // extract args from data
139       invocation_handle = pmt_nth(0, data);
140       long which_usrp = pmt_to_long(pmt_nth(1, data));  // integer usrp id, usually 0
141       
142       // Do the right thing....
143       // build a reply
144       (void) which_usrp;        // avoid unused warning
145
146       // if everything OK
147       status = PMT_T;
148       reply_data = pmt_list2(invocation_handle, status);
149
150       //  ...and send it
151       d_cs->send(s_response_open, reply_data);
152       return;
153     }
154     else if (pmt_eq(event, s_cmd_close)){
155       // ...
156     }
157     else if (pmt_eq(event, s_cmd_max_capacity)) {
158       invocation_handle = pmt_nth(0, data);
159       reply_data = pmt_list2(invocation_handle, pmt_from_long(max_capacity()));
160       d_cs->send(s_response_max_capacity, reply_data);
161       return;
162     }
163     else if (pmt_eq(event, s_cmd_ntx_chan)) {
164       invocation_handle = pmt_nth(0, data);
165       reply_data = pmt_list2(invocation_handle, pmt_from_long(d_ntx_chan));
166       d_cs->send(s_response_ntx_chan, reply_data);
167     }
168     else if (pmt_eq(event, s_cmd_nrx_chan)) {
169       invocation_handle = pmt_nth(0, data);
170       reply_data = pmt_list2(invocation_handle, pmt_from_long(d_nrx_chan));
171       d_cs->send(s_response_nrx_chan, reply_data);
172     }
173     else if (pmt_eq(event, s_cmd_current_capacity_allocation)) {
174       invocation_handle = pmt_nth(0, data);
175       reply_data = pmt_list2(invocation_handle, pmt_from_long(current_capacity_allocation()));
176       d_cs->send(s_response_current_capacity_allocation, reply_data);
177     }
178     goto unhandled;
179   }
180
181   if (pmt_eq(event, s_cmd_allocate_channel)){
182     handle_cmd_allocate_channel(port_id, data);
183     return;
184   }
185
186   if (pmt_eq(event, s_cmd_deallocate_channel)) {
187     handle_cmd_deallocate_channel(port_id, data);
188     return;
189   }
190     
191   if (pmt_eq(event, s_cmd_xmit_raw_frame)){
192     handle_cmd_xmit_raw_frame(data);
193     return;
194   }
195
196  unhandled:
197   std::cout << "[USRP_SERVER] unhandled msg: " << msg << std::endl;
198 }
199
200 // Return -1 if it is not an RX port, or an index
201 int usrp_server::tx_port_index(pmt_t port_id) {
202
203   for(int i=0; i < (int) d_tx.size(); i++) 
204     if(pmt_eq(d_tx[i]->port_symbol(), port_id))
205       return i;
206
207   return -1;
208 }
209
210 // Return -1 if it is not an RX port, or an index
211 int usrp_server::rx_port_index(pmt_t port_id) {
212   
213   for(int i=0; i < (int) d_rx.size(); i++) 
214     if(pmt_eq(d_rx[i]->port_symbol(), port_id))
215       return i;
216
217   return -1;
218 }
219
220 // Go through all TX and RX channels, sum up the assigned capacity
221 // and return it
222 long usrp_server::current_capacity_allocation() {
223   long capacity = 0;
224
225   for(int chan=0; chan < d_ntx_chan; chan++) 
226     capacity += d_chaninfo_tx[chan].assigned_capacity;
227
228   for(int chan=0; chan < d_nrx_chan; chan++)
229     capacity += d_chaninfo_rx[chan].assigned_capacity;
230
231   return capacity;
232 }
233     
234 void usrp_server::handle_cmd_allocate_channel(pmt_t port_id, pmt_t data) {
235
236   pmt_t invocation_handle = pmt_nth(0, data);
237   long rqstd_capacity = pmt_to_long(pmt_nth(1, data));
238   long chan, port;
239   pmt_t reply_data;
240
241   // If it's a TX port, allocate on a free channel, else check if it's a RX port
242   // and allocate.
243   if((port = tx_port_index(port_id)) != -1) {
244
245     // Check capacity exists
246     if((D_USB_CAPACITY - current_capacity_allocation()) < rqstd_capacity) {
247       reply_data = pmt_list3(invocation_handle, pmt_from_long(RQSTD_CAPACITY_UNAVAIL), PMT_NIL);  // no capacity available
248       d_tx[port]->send(s_response_allocate_channel, reply_data);
249       return;
250     }
251
252     // Find a free channel, assign the capacity and respond
253     for(chan=0; chan < d_ntx_chan; chan++) {
254       if(d_chaninfo_tx[chan].owner == PMT_NIL) {
255         d_chaninfo_tx[chan].owner = port_id;
256         d_chaninfo_tx[chan].assigned_capacity = rqstd_capacity;
257         reply_data = pmt_list3(invocation_handle, PMT_T, pmt_from_long(chan));
258         d_tx[port]->send(s_response_allocate_channel, reply_data);
259         return;
260       }
261     }
262
263     std::cout << "[USRP_SERVER] Couldnt find a TX chan\n";
264
265     reply_data = pmt_list3(invocation_handle, pmt_from_long(CHANNEL_UNAVAIL), PMT_NIL);  // no free TX chan found
266     d_tx[port]->send(s_response_allocate_channel, reply_data);
267     return;
268   }
269   
270   // Repeat the same process on the RX side if the port was not determined to be TX
271   if((port = rx_port_index(port_id)) != -1) {
272     
273     if((D_USB_CAPACITY - current_capacity_allocation()) < rqstd_capacity) {
274       reply_data = pmt_list3(invocation_handle, pmt_from_long(RQSTD_CAPACITY_UNAVAIL), PMT_NIL);  // no capacity available
275       d_rx[port]->send(s_response_allocate_channel, reply_data);
276       return;
277     }
278
279     for(chan=0; chan < d_nrx_chan; chan++) {
280       if(d_chaninfo_rx[chan].owner == PMT_NIL) {
281         d_chaninfo_rx[chan].owner = port_id;
282         d_chaninfo_rx[chan].assigned_capacity = rqstd_capacity;
283         reply_data = pmt_list3(invocation_handle, PMT_T, pmt_from_long(chan));
284         d_rx[port]->send(s_response_allocate_channel, reply_data);
285         return;
286       }
287     }
288
289     std::cout << "[USRP_SERVER] Couldnt find a RX chan\n";
290     reply_data = pmt_list3(invocation_handle, pmt_from_long(CHANNEL_UNAVAIL), PMT_NIL);  // no free RX chan found
291     d_rx[port]->send(s_response_allocate_channel, reply_data);
292     return;
293   }
294 }
295
296 // Check the port type and deallocate assigned capacity based on this, ensuring
297 // that the owner of the method invocation is the owner of the port and that
298 // the channel number is valid.
299 void usrp_server::handle_cmd_deallocate_channel(pmt_t port_id, pmt_t data) {
300
301   pmt_t invocation_handle = pmt_nth(0, data); 
302   long channel = pmt_to_long(pmt_nth(1, data));
303   long port;
304   pmt_t reply_data;
305   
306   // Check that the channel number is valid, and that the calling port is the owner
307   // of the channel, and if so remove the assigned capacity.
308   if((port = tx_port_index(port_id)) != -1) {
309   
310     if(channel >= d_ntx_chan) {
311       reply_data = pmt_list2(invocation_handle, pmt_from_long(CHANNEL_INVALID));   // not a legit channel number
312       d_tx[port]->send(s_response_deallocate_channel, reply_data);
313       return;
314     }
315
316     if(d_chaninfo_tx[channel].owner != port_id) {
317       reply_data = pmt_list2(invocation_handle, pmt_from_long(PERMISSION_DENIED));   // not the owner of the port
318       d_tx[port]->send(s_response_deallocate_channel, reply_data);
319       return;
320     }
321
322     d_chaninfo_tx[channel].assigned_capacity = 0;
323     d_chaninfo_tx[channel].owner = PMT_NIL;
324
325     reply_data = pmt_list2(invocation_handle, PMT_T);
326     d_tx[port]->send(s_response_deallocate_channel, reply_data);
327     return;
328   }
329
330   // Repeated process on the RX side
331   if((port = rx_port_index(port_id)) != -1) {
332   
333     if(channel >= d_nrx_chan) {
334       reply_data = pmt_list2(invocation_handle, pmt_from_long(CHANNEL_INVALID));   // not a legit channel number
335       d_rx[port]->send(s_response_deallocate_channel, reply_data);
336       return;
337     }
338
339     if(d_chaninfo_rx[channel].owner != port_id) {
340       reply_data = pmt_list2(invocation_handle, pmt_from_long(PERMISSION_DENIED));   // not the owner of the port
341       d_rx[port]->send(s_response_deallocate_channel, reply_data);
342       return;
343     }
344
345     d_chaninfo_rx[channel].assigned_capacity = 0;
346     d_chaninfo_rx[channel].owner = PMT_NIL;
347
348     reply_data = pmt_list2(invocation_handle, PMT_T);
349     d_rx[port]->send(s_response_deallocate_channel, reply_data);
350     return;
351   }
352
353 }
354
355 void usrp_server::handle_cmd_xmit_raw_frame(pmt_t data) {
356
357   size_t n_bytes, psize;
358   long max_payload_len = transport_pkt::max_payload();
359
360   pmt_t invocation_handle = pmt_nth(0, data);
361   long channel = pmt_to_long(pmt_nth(1, data));
362   const void *samples = pmt_uniform_vector_elements(pmt_nth(2, data), n_bytes);
363   long timestamp = pmt_to_long(pmt_nth(3, data));
364
365   // Determine the number of packets to allocate contiguous memory for bursting over the
366   // USB and get a pointer to the memory to be used in building the packets
367   long n_packets = static_cast<long>(std::ceil(n_bytes / (double)max_payload_len));
368   pmt_t v_packets = pmt_make_u8vector(sizeof(transport_pkt) * n_packets, 0);
369
370   transport_pkt *pkts =
371     (transport_pkt *) pmt_u8vector_writeable_elements(v_packets, psize);
372
373   for(int n=0; n < n_packets; n++) {
374
375     long payload_len = std::min((long)(n_bytes-(n*max_payload_len)), (long)max_payload_len);
376   
377     if(n == 0) { // first packet gets start of burst flag and timestamp
378       pkts[n].set_header(pkts[n].FL_START_OF_BURST, channel, 0, payload_len);
379       pkts[n].set_timestamp(timestamp);
380     } else {
381       pkts[n].set_header(0, channel, 0, payload_len);
382       pkts[n].set_timestamp(0xffffffff);
383     }
384
385     memcpy(pkts[n].payload(), (uint8_t *)samples+(max_payload_len * n), payload_len);
386   }
387
388   pkts[n_packets-1].set_end_of_burst();   // set the last packet's end of burst
389
390   // interface with the USRP to send the USB packet, since the memory is
391   // contiguous, this should be a serious of memory copies to the bus, each being
392   // USB_PKT_SIZE * MAX_PACKET_BURST bytes worth of data (given a full burst)
393 }
394
395 REGISTER_MBLOCK_CLASS(usrp_server);