-
Notifications
You must be signed in to change notification settings - Fork 45
/
protocol1.c
1786 lines (1621 loc) · 51.1 KB
/
protocol1.c
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
/* Copyright (C)
* 2018 - John Melton, G0ORX/N6LYT
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <wdsp.h>
#include "band.h"
#include "channel.h"
#include "discovered.h"
#include "bpsk.h"
#include "mode.h"
#include "filter.h"
#include "receiver.h"
#include "transmitter.h"
#include "wideband.h"
#include "adc.h"
#include "dac.h"
#include "radio.h"
#include "main.h"
#include "protocol1.h"
#include "audio.h"
#include "signal.h"
#include "vfo.h"
#include "transmitter.h"
//#include "vox.h"
#include "ext.h"
#include "error_handler.h"
#ifdef CWDAEMON
#include "cwdaemon.h"
#endif
#define min(x,y) (x<y?x:y)
#define SYNC0 0
#define SYNC1 1
#define SYNC2 2
#define C0 3
#define C1 4
#define C2 5
#define C3 6
#define C4 7
#define DATA_PORT 1024
#define SYNC 0x7F
#define OZY_BUFFER_SIZE 512
//#define OUTPUT_BUFFER_SIZE 1024
// ozy command and control
#define MOX_DISABLED 0x00
#define MOX_ENABLED 0x01
#define MIC_SOURCE_JANUS 0x00
#define MIC_SOURCE_PENELOPE 0x80
#define CONFIG_NONE 0x00
#define CONFIG_PENELOPE 0x20
#define CONFIG_MERCURY 0x40
#define CONFIG_BOTH 0x60
#define PENELOPE_122_88MHZ_SOURCE 0x00
#define MERCURY_122_88MHZ_SOURCE 0x10
#define ATLAS_10MHZ_SOURCE 0x00
#define PENELOPE_10MHZ_SOURCE 0x04
#define MERCURY_10MHZ_SOURCE 0x08
#define SPEED_48K 0x00
#define SPEED_96K 0x01
#define SPEED_192K 0x02
#define SPEED_384K 0x03
#define MODE_CLASS_E 0x01
#define MODE_OTHERS 0x00
#define ALEX_ATTENUATION_0DB 0x00
#define ALEX_ATTENUATION_10DB 0x01
#define ALEX_ATTENUATION_20DB 0x02
#define ALEX_ATTENUATION_30DB 0x03
#define LT2208_GAIN_OFF 0x00
#define LT2208_GAIN_ON 0x04
#define LT2208_DITHER_OFF 0x00
#define LT2208_DITHER_ON 0x08
#define LT2208_RANDOM_OFF 0x00
#define LT2208_RANDOM_ON 0x10
static int data_socket;
static struct sockaddr_in data_addr;
static int data_addr_length;
static int output_buffer_size;
static unsigned char control_in[5]={0x00,0x00,0x00,0x00,0x00};
static gboolean running;
static long ep4_sequence;
static int current_rx=0;
static int mic_samples=0;
static int mic_sample_divisor=1;
static unsigned char output_buffer[OZY_BUFFER_SIZE];
static int output_buffer_index=8;
static int tx_output_buffer_index=8;
static int command=1;
enum {
SYNC_0=0,
SYNC_1,
SYNC_2,
CONTROL_0,
CONTROL_1,
CONTROL_2,
CONTROL_3,
CONTROL_4,
LEFT_SAMPLE_HI,
LEFT_SAMPLE_MID,
LEFT_SAMPLE_LOW,
RIGHT_SAMPLE_HI,
RIGHT_SAMPLE_MID,
RIGHT_SAMPLE_LOW,
MIC_SAMPLE_HI,
MIC_SAMPLE_LOW,
SKIP
};
static int state=SYNC_0;
static GThread *receive_thread_id;
static void start_protocol1_thread();
static gpointer receive_thread(gpointer arg);
static void process_ozy_input_buffer(unsigned char *buffer);
static void process_wideband_buffer(unsigned char *buffer);
void ozy_send_buffer();
static unsigned char metis_buffer[1032];
static long send_sequence=-1;
static int metis_offset=8;
static int metis_write(unsigned char ep,unsigned char* buffer,int length);
static void metis_start_stop(int command);
static void metis_send_buffer(unsigned char* buffer,int length);
static void metis_restart();
#define COMMON_MERCURY_FREQUENCY 0x80
#define PENELOPE_MIC 0x80
#ifdef USBOZY
//
// additional defines if we include USB Ozy support
//
#include "ozyio.h"
static GThread *ozy_EP4_rx_thread_id;
static GThread *ozy_EP6_rx_thread_id;
static gpointer ozy_ep4_rx_thread(gpointer arg);
static gpointer ozy_ep6_rx_thread(gpointer arg);
static void start_usb_receive_threads();
static int ozyusb_write(char* buffer,int length);
#define EP6_IN_ID 0x86 // end point = 6, direction toward PC
#define EP2_OUT_ID 0x02 // end point = 2, direction from PC
#define EP6_BUFFER_SIZE 2048
static unsigned char usb_output_buffer[EP6_BUFFER_SIZE];
static unsigned char ep6_inbuffer[EP6_BUFFER_SIZE];
static unsigned char usb_buffer_block = 0;
#endif
void protocol1_stop() {
metis_start_stop(0);
running=FALSE;
}
void protocol1_run() {
fprintf(stderr,"protocol1_run\n");
start_protocol1_thread();
for(int i=8;i<OZY_BUFFER_SIZE;i++) {
output_buffer[i]=0;
}
metis_restart();
}
void protocol1_set_mic_sample_rate(int rate) {
mic_sample_divisor=rate/48000;
}
void protocol1_init(RADIO *r) {
QueueInit();
fprintf(stderr,"protocol1_init\n");
protocol1_set_mic_sample_rate(r->sample_rate);
if(radio->local_microphone) {
if(audio_open_input(r)!=0) {
radio->local_microphone=FALSE;
}
}
#ifdef USBOZY
//
// if we have a USB interfaced Ozy device:
//
if (radio->discovered->device == DEVICE_OZY) {
fprintf(stderr,"protocol1_init: initialise ozy on USB\n");
ozy_initialise();
start_usb_receive_threads();
}
else
#endif
//start_protocol1_thread();
}
#ifdef USBOZY
//
// starts the threads for USB receive
// EP4 is the wideband endpoint
// EP6 is the "normal" USB frame endpoint
//
static void start_usb_receive_threads()
{
int rc;
fprintf(stderr,"protocol1 starting USB receive thread: buffer_size=%d\n",radio->buffer_size);
ozy_EP6_rx_thread_id = g_thread_new( "OZY EP6 RX", ozy_ep6_rx_thread, NULL);
if( ! ozy_EP6_rx_thread_id )
{
fprintf(stderr,"g_thread_new failed for ozy_ep6_rx_thread\n");
exit( -1 );
}
}
//
// receive threat for USB EP4 (wideband) not currently used.
//
static gpointer ozy_ep4_rx_thread(gpointer arg)
{
}
//
// receive threat for USB EP6 (512 byte USB Ozy frames)
// this function loops reading 4 frames at a time through USB
// then processes them one at a time.
//
static gpointer ozy_ep6_rx_thread(gpointer arg) {
int bytes;
unsigned char buffer[2048];
fprintf(stderr, "protocol1: USB EP6 receive_thread\n");
running=TRUE;
while (running)
{
bytes = ozy_read(EP6_IN_ID,ep6_inbuffer,EP6_BUFFER_SIZE); // read a 2K buffer at a time
if (bytes == 0)
{
fprintf(stderr,"protocol1_ep6_read: ozy_read returned 0 bytes... retrying\n");
continue;
}
else if (bytes != EP6_BUFFER_SIZE)
{
fprintf(stderr,"protocol1_ep6_read: OzyBulkRead failed %d bytes\n",bytes);
perror("ozy_read(EP6 read failed");
//exit(1);
}
else
// process the received data normally
{
process_ozy_input_buffer(&ep6_inbuffer[0]);
process_ozy_input_buffer(&ep6_inbuffer[512]);
process_ozy_input_buffer(&ep6_inbuffer[1024]);
process_ozy_input_buffer(&ep6_inbuffer[1024+512]);
}
}
// terminate
//_exit(0);
}
#endif
static void start_protocol1_thread() {
fprintf(stderr,"protocol1 starting receive thread: buffer_size=%d output_buffer_size=%d\n",radio->buffer_size,output_buffer_size);
switch(radio->discovered->device) {
#ifdef USBOZY
case DEVICE_OZY:
break;
#endif
default:
data_socket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(data_socket<0) {
perror("protocol1: create socket failed for data_socket\n");
exit(-1);
}
int optval = 1;
if(setsockopt(data_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))<0) {
perror("data_socket: SO_REUSEADDR");
}
if(setsockopt(data_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval))<0) {
perror("data_socket: SO_REUSEPORT");
}
// bind to the interface
if(bind(data_socket,(struct sockaddr*)&radio->discovered->info.network.interface_address,radio->discovered->info.network.interface_length)<0) {
perror("protocol1: bind socket failed for data_socket\n");
exit(-1);
}
memcpy(&data_addr,&radio->discovered->info.network.address,radio->discovered->info.network.address_length);
data_addr_length=radio->discovered->info.network.address_length;
data_addr.sin_port=htons(DATA_PORT);
break;
}
receive_thread_id = g_thread_new( "protocol1", receive_thread, NULL);
if( ! receive_thread_id )
{
fprintf(stderr,"g_thread_new failed on receive_thread\n");
exit( -1 );
}
fprintf(stderr, "receive_thread: id=%p\n",receive_thread_id);
}
static gpointer receive_thread(gpointer arg) {
struct sockaddr_in addr;
socklen_t length;
unsigned char buffer[2048];
int bytes_read;
int ep;
long sequence;
fprintf(stderr, "protocol1: receive_thread\n");
running=TRUE;
length=sizeof(addr);
while(running) {
switch(radio->discovered->device) {
#ifdef USBOZY
case DEVICE_OZY:
// should not happen
break;
#endif
default:
bytes_read=recvfrom(data_socket,buffer,sizeof(buffer),0,(struct sockaddr*)&addr,&length);
if(bytes_read<0) {
if(errno==EAGAIN) {
error_handler("protocol1: receiver_thread: recvfrom socket failed","Radio not sending data");
} else {
error_handler("protocol1: receiver_thread: recvfrom socket failed",strerror(errno));
}
//running=FALSE;
continue;
}
if(buffer[0]==0xEF && buffer[1]==0xFE) {
switch(buffer[2]) {
case 1:
// get the end point
ep=buffer[3]&0xFF;
// get the sequence number
sequence=((buffer[4]&0xFF)<<24)+((buffer[5]&0xFF)<<16)+((buffer[6]&0xFF)<<8)+(buffer[7]&0xFF);
switch(ep) {
case 6: // EP6
// process the data
process_ozy_input_buffer(&buffer[8]);
process_ozy_input_buffer(&buffer[520]);
full_tx_buffer(radio->transmitter);
break;
case 4: // EP4
ep4_sequence++;
if(sequence!=ep4_sequence) {
ep4_sequence=sequence;
} else {
//int seq=(int)(sequence%32L);
if((sequence%32L)==0L) {
reset_wideband_buffer_index(radio->wideband);
}
process_wideband_buffer(&buffer[8]);
process_wideband_buffer(&buffer[520]);
}
break;
default:
fprintf(stderr,"unexpected EP %d length=%d\n",ep,bytes_read);
break;
}
break;
case 2: // response to a discovery packet
fprintf(stderr,"unexepected discovery response when not in discovery mode\n");
break;
default:
fprintf(stderr,"unexpected packet type: 0x%02X\n",buffer[2]);
break;
}
} else {
fprintf(stderr,"received bad header bytes on data port %02X,%02X\n",buffer[0],buffer[1]);
}
break;
}
}
fprintf(stderr,"EXIT: protocol1: receive_thread\n");
return NULL;
}
static void process_control_bytes() {
gboolean previous_ptt;
// Unused - commented in case used in future
//gboolean previous_dot;
//gboolean previous_dash;
gint tx_mode=USB;
RECEIVER *tx_receiver=radio->transmitter->rx;
if(tx_receiver!=NULL) {
#ifdef USE_VFO_B_MODE_AND_FILTER
if(radio->transmitter->rx->split) {
tx_mode=tx_receiver->mode_b;
} else {
#endif
tx_mode=tx_receiver->mode_a;
#ifdef USE_VFO_B_MODE_AND_FILTER
}
#endif
}
previous_ptt=radio->local_ptt;
//previous_dot=radio->dot;
//previous_dash=radio->dash;
radio->ptt=(control_in[0]&0x01)==0x01;
//radio->dash=(control_in[0]&0x02)==0x02;
//radio->dot=(control_in[0]&0x04)==0x04;
radio->local_ptt=radio->ptt;
if(tx_mode==CWL || tx_mode==CWU) {
radio->local_ptt=radio->ptt|radio->dot|radio->dash;
}
if(previous_ptt!=radio->local_ptt) {
g_print("process_control_bytes: ppt=%d dot=%d dash=%d\n",radio->ptt,radio->dot,radio->dash);
g_idle_add(ext_ptt_changed,(gpointer)radio);
}
switch((control_in[0]>>3)&0x1F) {
case 0:
radio->adc_overload=(control_in[1]&0x01)==0x01;
radio->IO1=(control_in[1]&0x02)==0x02;
radio->IO2=(control_in[1]&0x04)==0x04;
radio->IO3=(control_in[1]&0x08)==0x08;
//HL2 Buffer over/underflow
#ifdef CWDAEMON
if ((radio->ptt) || keytx) {
int recov = (control_in[3]&0x40) == 0x40;
int msb = (control_in[3]&0x80) == 0x80;
/*
if (msb == 1) {
g_print("Buffer recovery %d %d\n", recov, msb);
}
*/
}
#endif
//}
if(radio->mercury_software_version!=control_in[2]) {
radio->mercury_software_version=control_in[2];
fprintf(stderr," Mercury Software version: %d (0x%0X)\n",radio->mercury_software_version,radio->mercury_software_version);
}
if(radio->penelope_software_version!=control_in[3]) {
radio->penelope_software_version=control_in[3];
if(radio->discovered->device!=DEVICE_HERMES_LITE2) {
fprintf(stderr," Penelope Software version: %d (0x%0X)\n",radio->penelope_software_version,radio->penelope_software_version);
}
}
if(radio->ozy_software_version!=control_in[4]) {
radio->ozy_software_version=control_in[4];
fprintf(stderr,"FPGA firmware version: %d.%d\n",radio->ozy_software_version/10,radio->ozy_software_version%10);
}
break;
case 1:
radio->transmitter->exciter_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Penelope or Hermes
int adc = ((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF);
double this_temperature = (3.26 * ((double)adc/4096.0) - 0.5) / 0.01;
// Exponential moving average filter
double alpha = 0.7;
radio->transmitter->temperature = (alpha * this_temperature) + (1 - alpha) * radio->transmitter->temperature;
radio->transmitter->alex_forward_power=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // from Alex or Apollo
break;
case 2:
radio->transmitter->alex_reverse_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Alex or Apollo
radio->AIN3=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
break;
case 3:
radio->AIN4=(control_in[1]<<8)+control_in[2]; // from Pennelope or Hermes
radio->AIN6=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
break;
}
}
static int nreceiver;
static int left_sample;
static int right_sample;
static short mic_sample;
static double left_sample_double;
static double right_sample_double;
static int nsamples;
static int iq_samples;
static void process_ozy_byte(int b) {
int i,j;
switch(state) {
case SYNC_0:
if(b==SYNC) {
state++;
}
break;
case SYNC_1:
if(b==SYNC) {
state++;
}
break;
case SYNC_2:
if(b==SYNC) {
state++;
}
break;
case CONTROL_0:
control_in[0]=b;
state++;
break;
case CONTROL_1:
control_in[1]=b;
state++;
break;
case CONTROL_2:
control_in[2]=b;
state++;
break;
case CONTROL_3:
control_in[3]=b;
state++;
break;
case CONTROL_4:
control_in[4]=b;
process_control_bytes();
nreceiver=0;
iq_samples=(512-8)/((radio->receivers*6)+2);
nsamples=0;
state++;
break;
case LEFT_SAMPLE_HI:
left_sample=(int)((signed char)b<<16);
state++;
break;
case LEFT_SAMPLE_MID:
left_sample|=(int)((((unsigned char)b)<<8)&0xFF00);
state++;
break;
case LEFT_SAMPLE_LOW:
left_sample|=(int)((unsigned char)b&0xFF);
left_sample_double=(double)left_sample/8388607.0; // 24 bit sample 2^23-1
state++;
break;
case RIGHT_SAMPLE_HI:
right_sample=(int)((signed char)b<<16);
state++;
break;
case RIGHT_SAMPLE_MID:
right_sample|=(int)((((unsigned char)b)<<8)&0xFF00);
state++;
break;
case RIGHT_SAMPLE_LOW:
right_sample|=(int)((unsigned char)b&0xFF);
right_sample_double=(double)right_sample/8388607.0; // 24 bit sample 2^23-1
//find receiver
i=-1;
for(j=0;j<radio->discovered->supported_receivers;j++) {
if(radio->receiver[j]!=NULL) {
i++;
if(i==nreceiver) break;
}
}
if(radio->receiver[j]!=NULL) {
add_iq_samples(radio->receiver[j], left_sample_double,right_sample_double);
}
nreceiver++;
if(nreceiver==radio->receivers) {
state++;
} else {
state=LEFT_SAMPLE_HI;
}
break;
case MIC_SAMPLE_HI:
mic_sample=(short)(b<<8);
state++;
break;
case MIC_SAMPLE_LOW:
mic_sample|=(short)(b&0xFF);
if(!radio->local_microphone) {
mic_samples++;
if(mic_samples>=mic_sample_divisor) { // reduce to 48000
add_mic_sample(radio->transmitter,(float)mic_sample/32768.0);
mic_samples=0;
}
}
nsamples++;
if(nsamples==iq_samples) {
state=SYNC_0;
} else {
nreceiver=0;
state=LEFT_SAMPLE_HI;
}
break;
}
}
static void process_ozy_input_buffer(unsigned char *buffer) {
int i;
if(radio->receivers>0) {
for(i=0;i<512;i++) {
process_ozy_byte(buffer[i]&0xFF);
}
}
}
#ifdef OLD_PROCESS
static void process_ozy_input_buffer(char *buffer) {
int i,j;
int r;
int count;
int b=0;
unsigned char ozy_samples[8*8];
int bytes;
gboolean previous_ptt;
gboolean previous_dot;
gboolean previous_dash;
int left_sample;
int right_sample;
short mic_sample;
double left_sample_double;
double right_sample_double;
double mic_sample_double;
double gain=pow(10.0, radio->transmitter->mic_gain / 20.0);
int left_sample_1;
int right_sample_1;
double left_sample_double_rx;
double right_sample_double_rx;
double left_sample_double_tx;
double right_sample_double_tx;
int nreceivers;
gint tx_mode=USB;
RECEIVER *tx_receiver=radio->transmitter->rx;
if(tx_receiver!=NULL) {
#ifdef USE_VFO_B_MODE_AND_FILTER
if(radio->transmitter->rx->split) {
tx_mode=tx_receiver->mode_b;
} else {
#endif
tx_mode=tx_receiver->mode_a;
#ifdef USE_VFO_B_MODE_AND_FILTER
}
#endif
}
if(buffer[b++]==SYNC && buffer[b++]==SYNC && buffer[b++]==SYNC) {
// extract control bytes
control_in[0]=buffer[b++];
control_in[1]=buffer[b++];
control_in[2]=buffer[b++];
control_in[3]=buffer[b++];
control_in[4]=buffer[b++];
previous_ptt=radio->local_ptt;
previous_dot=radio->dot;
previous_dash=radio->dash;
radio->ptt=(control_in[0]&0x01)==0x01;
radio->dash=(control_in[0]&0x02)==0x02;
radio->dot=(control_in[0]&0x04)==0x04;
radio->local_ptt=radio->ptt;
if(tx_mode==CWL || tx_mode==CWU) {
radio->local_ptt=radio->ptt|radio->dot|radio->dash;
}
if(previous_ptt!=radio->local_ptt) {
//g_idle_add(ext_ptt_update,(gpointer)(long)(radio->local_ptt));
}
switch((control_in[0]>>3)&0x1F) {
case 0:
radio->adc_overload=control_in[1]&0x01==0x01;
radio->IO1=control_in[1]&0x02==0x02;
radio->IO2=control_in[1]&0x04==0x04;
radio->IO3=control_in[1]&0x08==0x08;
if(radio->mercury_software_version!=control_in[2]) {
radio->mercury_software_version=control_in[2];
fprintf(stderr," Mercury Software version: %d (0x%0X)\n",radio->mercury_software_version,radio->mercury_software_version);
}
if(radio->penelope_software_version!=control_in[3]) {
radio->penelope_software_version=control_in[3];
fprintf(stderr," Penelope Software version: %d (0x%0X)\n",radio->penelope_software_version,radio->penelope_software_version);
}
if(radio->ozy_software_version!=control_in[4]) {
radio->ozy_software_version=control_in[4];
fprintf(stderr,"FPGA firmware version: %d.%d\n",radio->ozy_software_version/10,radio->ozy_software_version%10);
}
break;
case 1:
radio->transmitter->exciter_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Penelope or Hermes
radio->transmitter->alex_forward_power=((control_in[3]&0xFF)<<8)|(control_in[4]&0xFF); // from Alex or Apollo
break;
case 2:
radio->transmitter->alex_reverse_power=((control_in[1]&0xFF)<<8)|(control_in[2]&0xFF); // from Alex or Apollo
radio->AIN3=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
break;
case 3:
radio->AIN4=(control_in[1]<<8)+control_in[2]; // from Pennelope or Hermes
radio->AIN6=(control_in[3]<<8)+control_in[4]; // from Pennelope or Hermes
break;
}
#ifdef PURESIGNAL
nreceivers=(RECEIVERS*2)+1;
#else
nreceivers=radio->receivers;
#endif
int iq_samples=(512-8)/((nreceivers*6)+2);
for(i=0;i<iq_samples;i++) {
for(r=0;r<nreceivers;r++) {
//find receiver
count=-1;
for(j=0;j<radio->discovered->supported_receivers;j++) {
if(radio->receiver[j]!=NULL) {
count++;
if(count==r) break;
}
}
left_sample = (int)((signed char) buffer[b++])<<16;
left_sample |= (int)((((unsigned char)buffer[b++])<<8)&0xFF00);
left_sample |= (int)((unsigned char)buffer[b++]&0xFF);
right_sample = (int)((signed char)buffer[b++]) << 16;
right_sample |= (int)((((unsigned char)buffer[b++])<<8)&0xFF00);
right_sample |= (int)((unsigned char)buffer[b++]&0xFF);
left_sample_double=(double)left_sample/8388607.0; // 24 bit sample 2^23-1
right_sample_double=(double)right_sample/8388607.0; // 24 bit sample 2^23-1
#ifdef PURESIGNAL
if(!isTransmitting(radio) || (isTransmitting(radio) && !radio->transmitter->puresignal)) {
switch(r) {
case 0:
add_iq_samples(receiver[0], left_sample_double,right_sample_double);
break;
case 1:
break;
case 2:
add_iq_samples(receiver[1], left_sample_double,right_sample_double);
break;
case 3:
break;
case 4:
break;
}
} else {
switch(r) {
case 0:
if(radio->discovered->device==DEVICE_METIS) {
left_sample_double_rx=left_sample_double;
right_sample_double_rx=right_sample_double;
}
break;
case 1:
if(radio->discovered->device==DEVICE_METIS) {
left_sample_double_tx=left_sample_double;
right_sample_double_tx=right_sample_double;
add_ps_iq_samples(radio->transmitter, left_sample_double_rx,right_sample_double_rx,left_sample_double_tx,right_sample_double_tx);
}
break;
case 2:
if(radio->discovered->device==DEVICE_HERMES) {
left_sample_double_rx=left_sample_double;
right_sample_double_rx=right_sample_double;
}
break;
case 3:
if(radio->discovered->device==DEVICE_METIS) {
left_sample_double_tx=left_sample_double;
right_sample_double_tx=right_sample_double;
add_ps_iq_samples(radio->transmitter, left_sample_double_tx,right_sample_double_tx,left_sample_double_rx,right_sample_double_rx);
} else if(radio->discovered->device==DEVICE_ANGELIA || radio->discovered->device==DEVICE_ORION || radio->discovered->device==DEVICE_ORION2) {
left_sample_double_rx=left_sample_double;
right_sample_double_rx=right_sample_double;
}
break;
case 4:
if(radio->discovered->device==DEVICE_ANGELIA || radio->discovered->device==DEVICE_ORION || radio->discovered->device==DEVICE_ORION2) {
left_sample_double_tx=left_sample_double;
right_sample_double_tx=right_sample_double;
add_ps_iq_samples(radio->transmitter, left_sample_double_tx,right_sample_double_tx,left_sample_double_rx,right_sample_double_rx);
}
break;
}
}
#else
if(radio->receiver[j]!=NULL) {
add_iq_samples(radio->receiver[j], left_sample_double,right_sample_double);
} else {
}
#endif
}
mic_sample = (short)(buffer[b++]<<8);
mic_sample |= (short)(buffer[b++]&0xFF);
if(!radio->local_microphone) {
mic_samples++;
if(mic_samples>=mic_sample_divisor) { // reduce to 48000
add_mic_sample(radio->transmitter,(float)mic_sample/32768);
mic_samples=0;
}
}
}
} else {
time_t t;
struct tm* gmt;
time(&t);
gmt=gmtime(&t);
g_print("%s: process_ozy_input_buffer: did not find sync: restarting\n",
asctime(gmt));
b=0;
while(b<510) {
if(buffer[b]==SYNC && buffer[b+1]==SYNC && buffer[b+2]==SYNC) {
g_print("found sync at %d\n",b);
}
b++;
}
metis_start_stop(0);
metis_restart();
}
}
#endif
// Send rx audio back to radio
void protocol1_audio_samples(RECEIVER *rx,short left_audio_sample,short right_audio_sample) {
if(!isTransmitting(radio)) {
// if(rx->mixed_audio==0) {
rx->mixed_left_audio=left_audio_sample;
rx->mixed_right_audio=right_audio_sample;
// } else {
// rx->mixed_left_audio+=left_audio_sample;
// rx->mixed_right_audio+=right_audio_sample;
// }
// rx->mixed_audio++;
// if(rx->mixed_audio>=radio->receivers) {
output_buffer[output_buffer_index++]=rx->mixed_left_audio>>8;
output_buffer[output_buffer_index++]=rx->mixed_left_audio;
output_buffer[output_buffer_index++]=rx->mixed_right_audio>>8;
output_buffer[output_buffer_index++]=rx->mixed_right_audio;
output_buffer[output_buffer_index++]=0;
output_buffer[output_buffer_index++]=0;
output_buffer[output_buffer_index++]=0;
output_buffer[output_buffer_index++]=0;
if(output_buffer_index>=OZY_BUFFER_SIZE) {
ozy_send_buffer();
output_buffer_index=8;
}
// rx->mixed_audio=0;
// }
}
}
void protocol1_iq_samples(int isample,int qsample) {
output_buffer[tx_output_buffer_index++]=0;
output_buffer[tx_output_buffer_index++]=0;
output_buffer[tx_output_buffer_index++]=0;
output_buffer[tx_output_buffer_index++]=0;
#ifdef CWDAEMON
gint tx_mode=USB;
RECEIVER *tx_receiver=radio->transmitter->rx;
if(tx_receiver!=NULL) {
#ifdef USE_VFO_B_MODE_AND_FILTER
if(radio->transmitter->rx->split) {
tx_mode=tx_receiver->mode_b;
} else {
#endif
tx_mode=tx_receiver->mode_a;
#ifdef USE_VFO_B_MODE_AND_FILTER
}
#endif
}
// I[0] of IQ stream is CWX keydown
if ((radio->cwdaemon) && (tx_mode==CWL || tx_mode==CWU)) {
g_mutex_lock(&cwdaemon_mutex);
if(keytx) {
output_buffer[tx_output_buffer_index++]=0x00;
output_buffer[tx_output_buffer_index++]=0x01;
}
else {
output_buffer[tx_output_buffer_index++]=0x00;
output_buffer[tx_output_buffer_index++]=0x00;
}
g_mutex_unlock(&cwdaemon_mutex);
}
else {
output_buffer[tx_output_buffer_index++]=isample>>8;
output_buffer[tx_output_buffer_index++]=isample;
}
#else
output_buffer[tx_output_buffer_index++]=isample>>8;
output_buffer[tx_output_buffer_index++]=isample;
#endif
output_buffer[tx_output_buffer_index++]=qsample>>8;
output_buffer[tx_output_buffer_index++]=qsample;
if(tx_output_buffer_index>=OZY_BUFFER_SIZE) {
tx_output_buffer_index=8;
ozy_send_buffer();
}
//}
}
void protocol1_eer_iq_samples(int isample,int qsample,int lasample,int rasample) {
if(isTransmitting(radio)) {
output_buffer[output_buffer_index++]=lasample>>8;
output_buffer[output_buffer_index++]=lasample;
output_buffer[output_buffer_index++]=rasample>>8;
output_buffer[output_buffer_index++]=rasample;
output_buffer[output_buffer_index++]=isample>>8;
output_buffer[output_buffer_index++]=isample;
output_buffer[output_buffer_index++]=qsample>>8;
output_buffer[output_buffer_index++]=qsample;
if(output_buffer_index>=OZY_BUFFER_SIZE) {
ozy_send_buffer();
output_buffer_index=8;
}
}
}
// Microphone buffer dump called from audio.c
void protocol1_process_local_mic(RADIO *r) {
int i;
for(i=0;i<r->local_microphone_buffer_size;i++) {
add_mic_sample(r->transmitter,r->local_microphone_buffer[i]);
}
}