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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
|
/* -*- c++ -*- */
/*
* Copyright 2007 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <usrp_server.h>
#include <iostream>
#include <usrp_inband_usb_packet.h>
#include <mb_class_registry.h>
#include <vector>
#include <usrp_usb_interface.h>
#include <symbols_usrp_server_cs.h>
#include <symbols_usrp_channel.h>
#include <symbols_usrp_tx.h>
#include <symbols_usrp_rx.h>
#include <symbols_usrp_low_level_cs.h>
#include <symbols_usrp_interface_cs.h>
static pmt_t s_shutdown = pmt_intern("%shutdown");
typedef usrp_inband_usb_packet transport_pkt; // makes conversion to gigabit easy
const static bool verbose = false;
static std::string
str(long x)
{
std::ostringstream s;
s << x;
return s.str();
}
usrp_server::usrp_server(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(rt, instance_name, user_arg),
d_fake_rx(false)
{
if(verbose)
std::cout << "[USRP_SERVER] Initializing...\n";
// Dictionary for arguments to all of the components
pmt_t usrp_dict = user_arg;
// control & status port
d_cs = define_port("cs", "usrp-server-cs", true, mb_port::EXTERNAL);
d_cs_usrp = define_port("cs_usrp", "usrp-interface-cs", false, mb_port::INTERNAL);
// ports
//
// (if/when we do replicated ports, these will be replaced by a
// single replicated port)
for(int port=0; port < N_PORTS; port++) {
d_tx.push_back(define_port("tx"+str(port),
"usrp-tx",
true,
mb_port::EXTERNAL));
d_rx.push_back(define_port("rx"+str(port),
"usrp-rx",
true,
mb_port::EXTERNAL));
}
define_component("usrp", "usrp_usb_interface", usrp_dict);
connect("self", "cs_usrp", "usrp", "cs");
d_defer=false;
d_opened=false;
// FIXME: needs to be returned from open, if we want to use this
d_nrx_chan = 2;
d_ntx_chan = 2;
// Initialize capacity on each channel to 0 and to no owner
// Also initialize the USRP standard tx/rx pointers to NULL
for(int chan=0; chan < d_ntx_chan; chan++)
d_chaninfo_tx.push_back(channel_info());
for(int chan=0; chan < d_nrx_chan; chan++)
d_chaninfo_rx.push_back(channel_info());
d_rx_chan_mask = 0;
for(int i=0; i < D_MAX_RID; i++)
d_rids.push_back(rid_info());
//d_fake_rx=true;
}
void
usrp_server::reset_channels()
{
for(int chan=0; chan < d_ntx_chan; chan++) {
d_chaninfo_tx[chan].assigned_capacity = 0;
d_chaninfo_tx[chan].owner = PMT_NIL;
}
for(int chan=0; chan < d_nrx_chan; chan++) {
d_chaninfo_rx[chan].assigned_capacity = 0;
d_chaninfo_rx[chan].owner = PMT_NIL;
}
d_rx_chan_mask = 0;
}
usrp_server::~usrp_server()
{
}
void
usrp_server::initial_transition()
{
// the initial transition
}
void
usrp_server::handle_message(mb_message_sptr msg)
{
pmt_t event = msg->signal(); // the "name" of the message
pmt_t port_id = msg->port_id(); // which port it came in on
pmt_t data = msg->data();
pmt_t invocation_handle;
pmt_t metadata = msg->metadata();
pmt_t status;
long port;
if (pmt_eq(event, s_shutdown)) // ignore (for now)
return;
invocation_handle = pmt_nth(0, data);
if (0){
std::cout << "[USRP_SERVER] event: " << event << std::endl;
std::cout << "[USRP_SERVER] port_id: " << port_id << std::endl;
}
// It would be nice if this were all table driven, and we could compute our
// state transition as f(current_state, port_id, signal)
// A message from the USRP CS, which should *only* be responses
//
// It is important that this set come before checking messages of any other
// components. This is since we always want to listen to the low level USRP
// server, even if we aren't initialized we are waiting for responses to
// become initialized. Likewise, after the usrp_server is "closed", we still
// want to pass responses back from the low level.
//---------------- USRP RESPONSE ---------------//
if (pmt_eq(port_id, d_cs_usrp->port_symbol())) {
//-------------- USRP OPEN ------------------//
if(pmt_eq(event, s_response_usrp_open)) {
// pass the response back over the regular CS port
pmt_t status = pmt_nth(1, data);
d_cs->send(s_response_open, pmt_list2(invocation_handle, status));
if(pmt_eqv(status,PMT_T)) {
d_opened = true;
d_defer = false;
recall_defer_queue();
}
return;
}
//------------- USRP CLOSE -------------------//
else if (pmt_eq(event, s_response_usrp_close)) {
pmt_t status = pmt_nth(1, data);
d_cs->send(s_response_close, pmt_list2(invocation_handle, status));
if(pmt_eqv(status,PMT_T)) {
d_opened = false;
d_defer = false;
reset_channels();
recall_defer_queue();
}
return;
}
//--------------- USRP WRITE --------------//
else if (pmt_eq(event, s_response_usrp_write)) {
pmt_t status = pmt_nth(1, data);
long channel = pmt_to_long(pmt_nth(2, data));
long port;
// Do not report back responses if they were generated from a
// command packet
if(channel == 0x1f)
return;
// Find the port through the owner of the channel
if((port = tx_port_index(d_chaninfo_tx[channel].owner)) !=-1 )
d_tx[port]->send(s_response_xmit_raw_frame,
pmt_list2(invocation_handle, status));
return;
}
//--------------- USRP READ ---------------//
else if (pmt_eq(event, s_response_usrp_read)) {
pmt_t status = pmt_nth(1, data);
if(!pmt_eqv(status, PMT_T)) {
std::cerr << "[USRP_SERVER] Error receiving packet\n";
return;
}
else {
handle_response_usrp_read(data);
return;
}
}
goto unhandled;
}
// Checking for defer on all other messages
if(d_defer) {
if (verbose)
std::cout << "[USRP_SERVER] Received msg while deferring ("
<< msg->signal() << ")\n";
d_defer_queue.push(msg);
return;
}
//--------- CONTROL / STATUS ------------//
if (pmt_eq(port_id, d_cs->port_symbol())){
//----------- OPEN -----------//
if (pmt_eq(event, s_cmd_open)){
// Reject if already open
if(d_opened) {
d_cs->send(s_response_open, pmt_list2(invocation_handle, s_err_usrp_already_opened));
return;
}
// the parameters are the same to the low level interface, so we just pass 'data' along
d_cs_usrp->send(s_cmd_usrp_open, data);
d_defer = true;
return;
}
//---------- CLOSE -----------//
else if (pmt_eq(event, s_cmd_close)){
if(!d_opened) {
d_cs->send(s_response_close, pmt_list2(invocation_handle, s_err_usrp_already_closed));
return;
}
d_defer = true;
d_cs_usrp->send(s_cmd_usrp_close, pmt_list1(invocation_handle));
return;
}
//---------- MAX CAPACITY ----------//
else if (pmt_eq(event, s_cmd_max_capacity)) {
if(!d_opened) {
d_cs->send(s_response_max_capacity,
pmt_list3(invocation_handle, s_err_usrp_not_opened, pmt_from_long(0)));
return;
}
d_cs->send(s_response_max_capacity,
pmt_list3(invocation_handle,
PMT_T,
pmt_from_long(max_capacity())));
return;
}
//---------- NTX CHAN --------------//
else if (pmt_eq(event, s_cmd_ntx_chan)) {
if(!d_opened) {
d_cs->send(s_response_ntx_chan,
pmt_list3(invocation_handle, s_err_usrp_not_opened, pmt_from_long(0)));
return;
}
d_cs->send(s_response_ntx_chan,
pmt_list3(invocation_handle,
PMT_T,
pmt_from_long(d_ntx_chan)));
return;
}
//---------- NRX CHAN -----------//
else if (pmt_eq(event, s_cmd_nrx_chan)) {
if(!d_opened) {
d_cs->send(s_response_nrx_chan,
pmt_list3(invocation_handle, s_err_usrp_not_opened, pmt_from_long(0)));
return;
}
d_cs->send(s_response_nrx_chan,
pmt_list3(invocation_handle,
PMT_T,
pmt_from_long(d_nrx_chan)));
return;
}
//--------- ALLOCATION? -----------//
else if (pmt_eq(event, s_cmd_current_capacity_allocation)) {
if(!d_opened) {
d_cs->send(s_response_current_capacity_allocation,
pmt_list3(invocation_handle,
s_err_usrp_not_opened,
pmt_from_long(0)));
return;
}
d_cs->send(s_response_current_capacity_allocation,
pmt_list3(invocation_handle,
PMT_T,
pmt_from_long(current_capacity_allocation())));
return;
}
goto unhandled;
}
//-------------- TX ---------------//
if ((port = tx_port_index(port_id)) != -1) {
//------------ ALLOCATE (TX) ----------------//
if (pmt_eq(event, s_cmd_allocate_channel)){
if(!d_opened) {
d_tx[port]->send(s_response_allocate_channel,
pmt_list3(invocation_handle,
s_err_usrp_not_opened,
pmt_from_long(0)));
return;
}
handle_cmd_allocate_channel(d_tx[port], d_chaninfo_tx, data);
return;
}
//----------- DEALLOCATE (TX) ---------------//
if (pmt_eq(event, s_cmd_deallocate_channel)) {
if(!d_opened) {
d_tx[port]->send(s_response_deallocate_channel,
pmt_list3(invocation_handle,
s_err_usrp_not_opened,
pmt_from_long(0)));
return;
}
handle_cmd_deallocate_channel(d_tx[port], d_chaninfo_tx, data);
return;
}
//-------------- XMIT RAW FRAME -----------------/
if (pmt_eq(event, s_cmd_xmit_raw_frame)){
if(!d_opened) {
d_tx[port]->send(s_response_xmit_raw_frame,
pmt_list2(invocation_handle, s_err_usrp_not_opened));
return;
}
handle_cmd_xmit_raw_frame(d_tx[port], d_chaninfo_tx, data);
return;
}
//-------------- CONTROL PACKET -----------------/
if (pmt_eq(event, s_cmd_to_control_channel)) {
if(!d_opened) {
d_tx[port]->send(s_response_xmit_raw_frame,
pmt_list2(invocation_handle, s_err_usrp_not_opened));
return;
}
handle_cmd_to_control_channel(d_tx[port], d_chaninfo_tx, data);
return;
}
goto unhandled;
}
//-------------- RX ---------------//
if ((port = rx_port_index(port_id)) != -1) {
//------------ ALLOCATE (RX) ----------------//
if (pmt_eq(event, s_cmd_allocate_channel)) {
if(!d_opened) {
d_rx[port]->send(s_response_allocate_channel,
pmt_list3(invocation_handle,
s_err_usrp_not_opened,
pmt_from_long(0)));
return;
}
handle_cmd_allocate_channel(d_rx[port], d_chaninfo_rx, data);
return;
}
//----------- DEALLOCATE (RX) ---------------//
if (pmt_eq(event, s_cmd_deallocate_channel)) {
if(!d_opened) {
d_rx[port]->send(s_response_deallocate_channel,
pmt_list3(invocation_handle,
s_err_usrp_not_opened,
pmt_from_long(0)));
return;
}
handle_cmd_deallocate_channel(d_rx[port], d_chaninfo_rx, data);
return;
}
//-------------- START RECV ----------------//
if (pmt_eq(event, s_cmd_start_recv_raw_samples)) {
if(!d_opened) {
d_rx[port]->send(s_response_recv_raw_samples,
pmt_list2(invocation_handle, s_err_usrp_not_opened));
return;
}
handle_cmd_start_recv_raw_samples(d_rx[port], d_chaninfo_rx, data);
return;
}
//-------------- STOP RECV ----------------//
if (pmt_eq(event, s_cmd_stop_recv_raw_samples)) {
if(!d_opened)
return;
// FIX ME : no response for stopping? even if error? (permissions)
handle_cmd_stop_recv_raw_samples(d_rx[port], d_chaninfo_rx, data);
return;
}
goto unhandled;
}
unhandled:
std::cout << "[USRP_SERVER] unhandled msg: " << msg << std::endl;
}
// Return -1 if it is not an RX port, or an index
int usrp_server::tx_port_index(pmt_t port_id) {
for(int i=0; i < (int) d_tx.size(); i++)
if(pmt_eq(d_tx[i]->port_symbol(), port_id))
return i;
return -1;
}
// Return -1 if it is not an RX port, or an index
int usrp_server::rx_port_index(pmt_t port_id) {
for(int i=0; i < (int) d_rx.size(); i++)
if(pmt_eq(d_rx[i]->port_symbol(), port_id))
return i;
return -1;
}
// Go through all TX and RX channels, sum up the assigned capacity
// and return it
long usrp_server::current_capacity_allocation() {
long capacity = 0;
for(int chan=0; chan < d_ntx_chan; chan++)
capacity += d_chaninfo_tx[chan].assigned_capacity;
for(int chan=0; chan < d_nrx_chan; chan++)
capacity += d_chaninfo_rx[chan].assigned_capacity;
return capacity;
}
void
usrp_server::handle_cmd_allocate_channel(
mb_port_sptr port,
std::vector<struct channel_info> &chan_info,
pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
long rqstd_capacity = pmt_to_long(pmt_nth(1, data));
long chan;
// Check capacity exists
if((D_USB_CAPACITY - current_capacity_allocation()) < rqstd_capacity) {
// no capacity available
port->send(s_response_allocate_channel,
pmt_list3(invocation_handle,
s_err_requested_capacity_unavailable,
PMT_NIL));
return;
}
// Find a free channel, assign the capacity and respond
for(chan=0; chan < (long)chan_info.size(); chan++) {
if(verbose)
std::cout << "[USRP_SERVER] Checking chan: " << chan
<< " owner " << chan_info[chan].owner
<< " size " << chan_info.size()
<< std::endl;
if(chan_info[chan].owner == PMT_NIL) {
chan_info[chan].owner = port->port_symbol();
chan_info[chan].assigned_capacity = rqstd_capacity;
port->send(s_response_allocate_channel,
pmt_list3(invocation_handle,
PMT_T,
pmt_from_long(chan)));
if(verbose)
std::cout << "[USRP_SERVER] Assigning channel: " << chan
<< " to " << chan_info[chan].owner
<< std::endl;
return;
}
}
if (verbose)
std::cout << "[USRP_SERVER] Couldnt find a TX chan\n";
// no free TX chan found
port->send(s_response_allocate_channel,
pmt_list3(invocation_handle,
s_err_channel_unavailable,
PMT_NIL));
return;
}
// Check the port type and deallocate assigned capacity based on this, ensuring
// that the owner of the method invocation is the owner of the port and that the
// channel number is valid.
void
usrp_server::handle_cmd_deallocate_channel(
mb_port_sptr port,
std::vector<struct channel_info> &chan_info,
pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
long channel = pmt_to_long(pmt_nth(1, data));
// Ensure the channel is valid and the caller owns the port
if(!check_valid(port, channel, chan_info,
pmt_list2(s_response_deallocate_channel, invocation_handle)))
return;
chan_info[channel].assigned_capacity = 0;
chan_info[channel].owner = PMT_NIL;
port->send(s_response_deallocate_channel,
pmt_list2(invocation_handle,
PMT_T));
return;
}
void usrp_server::handle_cmd_xmit_raw_frame(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data) {
size_t n_bytes, psize;
long max_payload_len = transport_pkt::max_payload();
pmt_t invocation_handle = pmt_nth(0, data);
long channel = pmt_to_long(pmt_nth(1, data));
const void *samples = pmt_uniform_vector_elements(pmt_nth(2, data), n_bytes);
long timestamp = pmt_to_long(pmt_nth(3, data));
pmt_t properties = pmt_nth(4, data);
// Ensure the channel is valid and the caller owns the port
if(!check_valid(port, channel, chan_info,
pmt_list2(s_response_xmit_raw_frame, invocation_handle)))
return;
// Read information from the properties of the packet
bool carrier_sense = false;
if(pmt_is_dict(properties)) {
// Check if carrier sense is enabled for the frame
if(pmt_t p_carrier_sense = pmt_dict_ref(properties,
pmt_intern("carrier-sense"),
PMT_NIL)) {
if(pmt_eqv(p_carrier_sense, PMT_T))
carrier_sense = true;
}
}
// Determine the number of packets to allocate contiguous memory for
// bursting over the USB and get a pointer to the memory to be used in
// building the packets
long n_packets =
static_cast<long>(std::ceil(n_bytes / (double)max_payload_len));
pmt_t v_packets = pmt_make_u8vector(sizeof(transport_pkt) * n_packets, 0);
transport_pkt *pkts =
(transport_pkt *) pmt_u8vector_writeable_elements(v_packets, psize);
for(int n=0; n < n_packets; n++) {
long payload_len =
std::min((long)(n_bytes-(n*max_payload_len)), (long)max_payload_len);
if(n == 0) { // first packet gets start of burst flag and timestamp
if(carrier_sense)
pkts[n].set_header(pkts[n].FL_START_OF_BURST
| pkts[n].FL_CARRIER_SENSE,
channel, 0, payload_len);
else
pkts[n].set_header(pkts[n].FL_START_OF_BURST, channel, 0, payload_len);
pkts[n].set_timestamp(timestamp);
} else {
pkts[n].set_header(0, channel, 0, payload_len);
pkts[n].set_timestamp(0xffffffff);
}
memcpy(pkts[n].payload(),
(uint8_t *)samples+(max_payload_len * n),
payload_len);
}
pkts[n_packets-1].set_end_of_burst(); // set the last packet's end of burst
if (verbose && 0)
std::cout << "[USRP_SERVER] Received raw frame invocation: "
<< invocation_handle << std::endl;
// The actual response to the write will be generated by a
// s_response_usrp_write
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packets));
return;
}
void usrp_server::handle_cmd_to_control_channel(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
pmt_t subpackets = pmt_nth(1, data);
long n_subpkts = pmt_length(subpackets);
long curr_subpkt = 0;
size_t psize;
long payload_len = 0;
long channel = 0x1f;
// The design of the following code is optimized for simplicity, not
// performance. To performance optimize this code, the total size in bytes
// needed for all of the CS packets is needed to allocate contiguous memory
// which contains the USB packets for bursting over the bus. However to do
// this the packets subpackets would need to be parsed twice and their sizes
// would need to be determined.
//
// The approach taken is to keep parsing the subpackets and putting them in to
// USB packets. Once the USB packet is full, a write is sent for it and
// another packet is created.
//
// The subpacket creation methods will return false if the subpacket will not
// fit in to the current USB packet. In these cases a new USB packet is
// created and the old is sent.
new_packet:
// This code needs to become "smart" and only make a new packet when full
pmt_t v_packet = pmt_make_u8vector(sizeof(transport_pkt), 0);
transport_pkt *pkt = (transport_pkt *) pmt_u8vector_writeable_elements(v_packet, psize);
payload_len = 0;
pkt->set_header(0, channel, 0, payload_len);
pkt->set_timestamp(0xffffffff);
while(curr_subpkt < n_subpkts) {
pmt_t subp = pmt_nth(curr_subpkt, subpackets);
pmt_t subp_cmd = pmt_nth(0, subp);
pmt_t subp_data = pmt_nth(1, subp);
//--------- PING FIXED --------------//
if(pmt_eq(subp_cmd, s_op_ping_fixed)) {
long urid = pmt_to_long(pmt_nth(0, subp_data));
long pingval = pmt_to_long(pmt_nth(1, subp_data));
// USRP server sets request ID's to keep track of which application gets
// what response back. To allow a full 6-bits for an RID to the user, we
// keep a mapping and replace the RID's as the packets go in and out. If
// there are no RID's available, the command is thrown away silently.
long srid;
if((srid = next_rid()) == -1)
goto subpkt_bail;
// We use a vector to store the owner of the ping request and will use it
// to send the request on any RX port they own.
d_rids[srid].owner = port->port_symbol();
d_rids[srid].user_rid = urid;
// Adds a ping after the previous command in the pkt
if(!pkt->cs_ping(srid, pingval))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
// Return the RID
d_rids[srid].owner = PMT_NIL;
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received ping command request"
<< " assigning RID " << srid << std::endl;
}
//----------- WRITE REG ---------------//
if(pmt_eq(subp_cmd, s_op_write_reg)) {
long reg_num = pmt_to_long(pmt_nth(0, subp_data));
long val = pmt_to_long(pmt_nth(1, subp_data));
if(!pkt->cs_write_reg(reg_num, val))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received write register request "
<< "("
<< "Reg: " << reg_num << ", "
<< "Val: " << val
<< ")\n";
}
//------- WRITE REG MASKED ----------//
if(pmt_eq(subp_cmd, s_op_write_reg_masked)) {
long reg_num = pmt_to_long(pmt_nth(0, subp_data));
long val = pmt_to_long(pmt_nth(1, subp_data));
long mask = pmt_to_long(pmt_nth(2, subp_data));
if(!pkt->cs_write_reg_masked(reg_num, val, mask))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received write register masked request\n";
}
//------------ READ REG --------------//
if(pmt_eq(subp_cmd, s_op_read_reg)) {
long urid = pmt_to_long(pmt_nth(0, subp_data));
long reg_num = pmt_to_long(pmt_nth(1, subp_data));
long srid;
if((srid = next_rid()) == -1)
goto subpkt_bail;
d_rids[srid].owner = port->port_symbol();
d_rids[srid].user_rid = urid;
if(!pkt->cs_read_reg(srid, reg_num))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
// Return the rid
d_rids[srid].owner = PMT_NIL;
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received read register request"
<< " assigning RID " << srid << std::endl;
}
//------------ DELAY --------------//
if(pmt_eq(subp_cmd, s_op_delay)) {
long ticks = pmt_to_long(pmt_nth(0, subp_data));
if(!pkt->cs_delay(ticks))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received delay request of "
<< ticks << " ticks\n";
}
//--------- I2C WRITE -----------//
// FIXME: could check that byte count does not exceed 2^8 which
// is the max length in the subpacket for # of bytes to read.
if(pmt_eq(subp_cmd, s_op_i2c_write)) {
long i2c_addr = pmt_to_long(pmt_nth(0, subp_data));
pmt_t data = pmt_nth(1, subp_data);
// Get a readable address to the data which also gives us the length
size_t data_len;
uint8_t *i2c_data = (uint8_t *) pmt_u8vector_writeable_elements(data, data_len);
// Make the USB packet
if(!pkt->cs_i2c_write(i2c_addr, i2c_data, data_len))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received I2C write\n";
}
//----------- I2C Read -------------//
if(pmt_eq(subp_cmd, s_op_i2c_read)) {
long urid = pmt_to_long(pmt_nth(0, subp_data));
long i2c_addr = pmt_to_long(pmt_nth(1, subp_data));
long i2c_bytes = pmt_to_long(pmt_nth(2, subp_data));
long srid;
if((srid = next_rid()) == -1)
goto subpkt_bail;
d_rids[srid].owner = port->port_symbol();
d_rids[srid].user_rid = urid;
if(!pkt->cs_i2c_read(srid, i2c_addr, i2c_bytes))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
d_rids[srid].owner = PMT_NIL;
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received I2C read\n";
}
//--------- SPI WRITE -----------//
if(pmt_eq(subp_cmd, s_op_spi_write)) {
long enables = pmt_to_long(pmt_nth(0, subp_data));
long format = pmt_to_long(pmt_nth(1, subp_data));
long opt = pmt_to_long(pmt_nth(2, subp_data));
pmt_t data = pmt_nth(3, subp_data);
// Get a readable address to the data which also gives us the length
size_t data_len;
uint8_t *spi_data = (uint8_t *) pmt_u8vector_writeable_elements(data, data_len);
// Make the USB packet
if(!pkt->cs_spi_write(enables, format, opt, spi_data, data_len))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received SPI write\n";
}
//--------- SPI READ -----------//
if(pmt_eq(subp_cmd, s_op_spi_read)) {
long urid = pmt_to_long(pmt_nth(0, subp_data));
long enables = pmt_to_long(pmt_nth(1, subp_data));
long format = pmt_to_long(pmt_nth(2, subp_data));
long opt = pmt_to_long(pmt_nth(3, subp_data));
long n_bytes = pmt_to_long(pmt_nth(4, subp_data));
long srid;
if((srid = next_rid()) == -1)
goto subpkt_bail;
d_rids[srid].owner = port->port_symbol();
d_rids[srid].user_rid = urid;
// Make the USB packet
if(!pkt->cs_spi_read(srid, enables, format, opt, n_bytes))
{
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
// Return the rid
d_rids[srid].owner = PMT_NIL;
goto new_packet;
}
if(verbose)
std::cout << "[USRP_SERVER] Received SPI read\n";
}
subpkt_bail:
curr_subpkt++;
}
// If the current packets length is > 0, we know there are subpackets that
// need to be sent out still.
if(pkt->payload_len() > 0)
d_cs_usrp->send(s_cmd_usrp_write,
pmt_list3(invocation_handle,
pmt_from_long(channel),
v_packet));
return;
}
void
usrp_server::handle_cmd_start_recv_raw_samples(mb_port_sptr port, std::vector<struct channel_info> &chan_info, pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
long channel = pmt_to_long(pmt_nth(1, data));
// Ensure the channel is valid and the caller owns the port
if(!check_valid(port, channel, chan_info,
pmt_list2(s_response_xmit_raw_frame, invocation_handle)))
return;
// Already started receiving samples? (another start before a stop)
// Check the RX channel bitmask.
if(d_rx_chan_mask & (1 << channel)) {
port->send(s_response_recv_raw_samples,
pmt_list5(invocation_handle,
s_err_already_receiving,
PMT_NIL,
PMT_NIL,
PMT_NIL));
return;
}
// We only need to generate a 'start reading' command down to the
// low level interface if no other channel is already reading
//
// We carry this over the CS interface because the lower level
// interface does not care about the channel, we only demux it
// at the usrp_server on responses.
if(d_rx_chan_mask == 0) {
if(verbose)
std::cout << "[USRP_SERVER] Sending read request down to start recv\n";
d_cs_usrp->send(s_cmd_usrp_start_reading, pmt_list1(invocation_handle));
}
d_rx_chan_mask |= 1<<channel;
return;
}
void
usrp_server::handle_cmd_stop_recv_raw_samples(
mb_port_sptr port,
std::vector<struct channel_info> &chan_info,
pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
long channel = pmt_to_long(pmt_nth(1, data));
// FIX ME : we have no responses to send an error...
// Ensure the channel is valid and the caller owns the port
//if(!check_valid(port, channel, chan_info,
// pmt_list2(s_response_xmit_raw_frame, invocation_handle)))
// return;
// Remove this hosts bit from the receiver mask
d_rx_chan_mask &= ~(1<<channel);
// We only need to generate a 'start reading' command down to the
// low level interface if no other channel is already reading
//
// We carry this over the CS interface because the lower level
// interface does not care about the channel, we only demux it
// at the usrp_server on responses.
if(d_rx_chan_mask == 0) {
if(verbose)
std::cout << "[USRP_SERVER] Sending stop reading request down\n";
d_cs_usrp->send(s_cmd_usrp_stop_reading, pmt_list1(invocation_handle));
}
return;
}
// Read the packet header, determine the port by the channel owner
void
usrp_server::handle_response_usrp_read(pmt_t data)
{
pmt_t invocation_handle = pmt_nth(0, data);
pmt_t status = pmt_nth(1, data);
pmt_t v_pkt = pmt_nth(2, data);
size_t n_bytes;
size_t ignore;
if (d_fake_rx) {
pmt_t pkt = pmt_nth(2, data);
d_rx[0]->send(s_response_recv_raw_samples,
pmt_list5(PMT_F,
PMT_T,
pkt,
pmt_from_long(0xffff),
PMT_NIL));
return;
}
// Extract the packet and return appropriately
transport_pkt *pkt = (transport_pkt *) pmt_u8vector_writeable_elements(v_pkt, n_bytes);
// The channel is used to find the port to pass the samples on
long channel = pkt->chan();
long payload_len = pkt->payload_len();
long port;
// Ignore packets which seem to have incorrect size or size 0
if(payload_len > pkt->max_payload() || payload_len == 0)
return;
// If the packet is a C/S packet, parse it separately
if(channel == 0x1f) {
parse_control_pkt(invocation_handle, pkt);
return;
}
if((port = rx_port_index(d_chaninfo_rx[channel].owner)) == -1)
return; // Don't know where to send the sample... possibility on abrupt close
pmt_t v_samples = pmt_make_u8vector(payload_len, 0);
uint8_t *samples = pmt_u8vector_writeable_elements(v_samples, ignore);
memcpy(samples, pkt->payload(), payload_len);
// Build a properties dictionary to store things such as the RSSI
pmt_t properties = pmt_make_dict();
pmt_dict_set(properties,
pmt_intern("rssi"),
pmt_from_long(pkt->rssi()));
if(pkt->overrun())
pmt_dict_set(properties,
pmt_intern("overrun"),
PMT_T);
if(pkt->underrun())
pmt_dict_set(properties,
pmt_intern("underrun"),
PMT_T);
d_rx[port]->send(s_response_recv_raw_samples,
pmt_list5(invocation_handle,
status,
v_samples,
pmt_from_long(pkt->timestamp()),
properties));
return;
}
void
usrp_server::parse_control_pkt(pmt_t invocation_handle, transport_pkt *pkt)
{
long payload_len = pkt->payload_len();
long curr_payload = 0;
long port;
// We dispatch based on the control packet type, however we can extract the
// opcode and the length immediately which is consistent in all responses.
//
// Since each control packet can have multiple responses, we keep reading the
// lengths of each subpacket until we reach the payload length.
while(curr_payload < payload_len) {
pmt_t sub_packet = pkt->read_subpacket(curr_payload);
pmt_t op_symbol = pmt_nth(0, sub_packet);
int len = pkt->cs_len(curr_payload);
if(verbose)
std::cout << "[USRP_SERVER] Parsing subpacket "
<< op_symbol << " ... length " << len << std::endl;
//----------------- PING RESPONSE ------------------//
if(pmt_eq(op_symbol, s_op_ping_fixed_reply)) {
long srid = pmt_to_long(pmt_nth(1, sub_packet));
pmt_t pingval = pmt_nth(2, sub_packet);
long urid = d_rids[srid].user_rid;
if(verbose)
std::cout << "[USRP_SERVER] Found ping response "
<< "("
<< "URID: " << urid << ", "
<< "SRID: " << srid << ", "
<< "VAL: " << pingval
<< ")\n";
// Do some bounds checking incase of bogus/corrupt responses
if(srid > D_MAX_RID)
return;
pmt_t owner = d_rids[srid].owner;
// FIXME: should be 1 response for all subpackets here ?
if((port = tx_port_index(owner)) != -1)
d_tx[port]->send(s_response_from_control_channel,
pmt_list4(invocation_handle,
PMT_T,
pmt_list2(s_op_ping_fixed_reply, // subp
pmt_list2(pmt_from_long(urid),
pingval)),
pmt_from_long(pkt->timestamp())));
}
//----------------- READ REG RESPONSE ------------------//
else if(pmt_eq(op_symbol, s_op_read_reg_reply)) {
long srid = pmt_to_long(pmt_nth(1, sub_packet));
pmt_t reg_num = pmt_nth(2, sub_packet);
pmt_t reg_val = pmt_nth(3, sub_packet);
long urid = d_rids[srid].user_rid;
if(verbose)
std::cout << "[USRP_SERVER] Found read register response "
<< "("
<< "URID: " << urid << ", "
<< "SRID: " << srid << ", "
<< "REG: " << reg_num << ", "
<< "VAL: " << reg_val
<< ")\n";
// Do some bounds checking to avoid seg faults
if(srid > D_MAX_RID)
return;
pmt_t owner = d_rids[srid].owner;
// FIXME: should be 1 response for all subpackets here ?
if((port = tx_port_index(owner)) != -1)
d_tx[port]->send(s_response_from_control_channel,
pmt_list4(invocation_handle,
PMT_T,
pmt_list2(s_op_read_reg_reply, // subp
pmt_list3(pmt_from_long(urid),
reg_num,
reg_val)),
pmt_from_long(pkt->timestamp())));
}
//------------------ I2C READ REPLY -------------------//
else if(pmt_eq(op_symbol, s_op_i2c_read_reply)) {
long srid = pmt_to_long(pmt_nth(1, sub_packet));
pmt_t i2c_addr = pmt_nth(2, sub_packet);
pmt_t i2c_data = pmt_nth(3, sub_packet);
long urid = d_rids[srid].user_rid;
if(verbose)
std::cout << "[USRP_SERVER] Found i2c read reply "
<< "("
<< "URID: " << urid << ", "
<< "SRID: " << srid << ", "
<< "Addr: " << i2c_addr << ", "
<< "Data: " << i2c_data
<< ")\n";
// Do some bounds checking to avoid seg faults
if(srid > D_MAX_RID)
return;
pmt_t owner = d_rids[srid].owner;
if((port = tx_port_index(owner)) != -1)
d_tx[port]->send(s_response_from_control_channel,
pmt_list4(invocation_handle,
PMT_T,
pmt_list2(s_op_i2c_read_reply,
pmt_list3(pmt_from_long(urid),
i2c_addr,
i2c_data)),
pmt_from_long(pkt->timestamp())));
}
//------------------ SPI READ REPLY -------------------//
else if(pmt_eq(op_symbol, s_op_spi_read_reply)) {
long srid = pmt_to_long(pmt_nth(1, sub_packet));
pmt_t spi_data = pmt_nth(2, sub_packet);
long urid = d_rids[srid].user_rid;
if(verbose)
std::cout << "[USRP_SERVER] Found SPI read reply "
<< "("
<< "URID: " << urid << ", "
<< "SRID: " << srid << ", "
<< "Data: " << spi_data
<< ")\n";
// Bounds check the RID
if(srid > D_MAX_RID)
return;
pmt_t owner = d_rids[srid].owner;
if((port = tx_port_index(owner)) != -1)
d_tx[port]->send(s_response_from_control_channel,
pmt_list4(invocation_handle,
PMT_T,
pmt_list2(s_op_spi_read_reply,
pmt_list2(pmt_from_long(urid),
spi_data)),
pmt_from_long(pkt->timestamp())));
}
// Each subpacket has an unaccounted for 2 bytes which is the opcode
// and the length field
curr_payload += len + 2;
// All subpackets are 32-bit aligned
int align_offset = 4 - (curr_payload % 4);
if(align_offset != 4)
curr_payload += align_offset;
}
}
void
usrp_server::recall_defer_queue()
{
std::vector<mb_message_sptr> recall;
while(!d_defer_queue.empty()) {
recall.push_back(d_defer_queue.front());
d_defer_queue.pop();
}
// Parse the messages that were queued while waiting for an open response
for(int i=0; i < (int)recall.size(); i++)
handle_message(recall[i]);
return;
}
bool
usrp_server::check_valid(mb_port_sptr port,
long channel,
std::vector<struct channel_info> &chan_info,
pmt_t signal_info)
{
pmt_t response_signal = pmt_nth(0, signal_info);
pmt_t invocation_handle = pmt_nth(1, signal_info);
// not a valid channel number?
if(channel >= (long)chan_info.size() && channel != 0x1f) {
port->send(response_signal,
pmt_list2(invocation_handle,
s_err_channel_invalid));
if(verbose)
std::cout << "[USRP_SERVER] Invalid channel number for event "
<< response_signal << std::endl;
return false;
}
// not the owner of the port?
if(chan_info[channel].owner != port->port_symbol()) {
port->send(response_signal,
pmt_list2(invocation_handle,
s_err_channel_permission_denied));
if(verbose)
std::cout << "[USRP_SERVER] Invalid permissions"
<< " for " << response_signal
<< " from " << port->port_symbol()
<< " proper owner is " << chan_info[channel].owner
<< " on channel " << channel
<< " invocation " << invocation_handle
<< std::endl;
return false;
}
return true;
}
// Goes through the vector of RIDs and retreieves an
// available one for use
long
usrp_server::next_rid()
{
for(int i = 0; i < D_MAX_RID; i++)
if(pmt_eqv(d_rids[i].owner, PMT_NIL))
return i;
return -1;
}
REGISTER_MBLOCK_CLASS(usrp_server);
|