summaryrefslogtreecommitdiffstats
path: root/qcom/fmradio/FmRxControls.java
blob: 864e289febf38f5d85ecb53cca01372142b1d909 (plain)
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
/*
 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *        * Redistributions of source code must retain the above copyright
 *            notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *            notice, this list of conditions and the following disclaimer in the
 *            documentation and/or other materials provided with the distribution.
 *        * Neither the name of The Linux Foundation nor
 *            the names of its contributors may be used to endorse or promote
 *            products derived from this software without specific prior written
 *            permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.    IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package qcom.fmradio;
import android.util.Log;


class FmRxControls
{

   private boolean mStateStereo;
   private boolean mStateMute;
   private int mFreq;

   static final int FREQ_MUL = 1000;
   static final int SEEK_FORWARD = 0;
   static final int SEEK_BACKWARD = 1;
   static final int SCAN_FORWARD = 2;
   static final int SCAN_BACKWARD = 3;
   static final int FM_DIGITAL_PATH = 0;
   static final int FM_ANALOG_PATH  = 1;
   private int mSrchMode;
   private int mScanTime;
   private int mSrchDir;
   private int mSrchListMode;
   private int mPrgmType;
   private int mPrgmId;
   private static final String TAG = "FmRxControls";


   /* V4l2 Controls */
   private static final int V4L2_CID_PRIVATE_BASE                          = 0x8000000;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SRCHMODE              = V4L2_CID_PRIVATE_BASE + 1;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SCANDWELL             = V4L2_CID_PRIVATE_BASE + 2;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SRCHON                = V4L2_CID_PRIVATE_BASE + 3;
   private static final int V4L2_CID_PRIVATE_TAVARUA_STATE                 = V4L2_CID_PRIVATE_BASE + 4;
   private static final int V4L2_CID_PRIVATE_TAVARUA_TRANSMIT_MODE         = V4L2_CID_PRIVATE_BASE + 5;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RDSGROUP_MASK         = V4L2_CID_PRIVATE_BASE + 6;
   private static final int V4L2_CID_PRIVATE_TAVARUA_REGION                = V4L2_CID_PRIVATE_BASE + 7;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SIGNAL_TH             = V4L2_CID_PRIVATE_BASE + 8;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SRCH_PTY              = V4L2_CID_PRIVATE_BASE + 9;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SRCH_PI               = V4L2_CID_PRIVATE_BASE + 10;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SRCH_CNT              = V4L2_CID_PRIVATE_BASE + 11;
   private static final int V4L2_CID_PRIVATE_TAVARUA_EMPHASIS              = V4L2_CID_PRIVATE_BASE + 12;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RDS_STD               = V4L2_CID_PRIVATE_BASE + 13;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SPACING               = V4L2_CID_PRIVATE_BASE + 14;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RDSON                 = V4L2_CID_PRIVATE_BASE + 15;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RDSGROUP_PROC         = V4L2_CID_PRIVATE_BASE + 16;
   private static final int V4L2_CID_PRIVATE_TAVARUA_LP_MODE               = V4L2_CID_PRIVATE_BASE + 17;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RDSD_BUF              = V4L2_CID_PRIVATE_BASE + 19;
   private static final int V4L2_CID_PRIVATE_TAVARUA_IOVERC                = V4L2_CID_PRIVATE_BASE + 24;
   private static final int V4L2_CID_PRIVATE_TAVARUA_INTDET                = V4L2_CID_PRIVATE_BASE + 25;
   private static final int V4L2_CID_PRIVATE_TAVARUA_MPX_DCC               = V4L2_CID_PRIVATE_BASE + 26;
   private static final int V4L2_CID_PRIVATE_TAVARUA_AF_JUMP               = V4L2_CID_PRIVATE_BASE + 27;
   private static final int V4L2_CID_PRIVATE_TAVARUA_RSSI_DELTA            = V4L2_CID_PRIVATE_BASE + 28;
   private static final int V4L2_CID_PRIVATE_TAVARUA_HLSI                  = V4L2_CID_PRIVATE_BASE + 29;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SET_AUDIO_PATH        = V4L2_CID_PRIVATE_BASE + 41;
   private static final int V4L2_CID_PRIVATE_SINR                          = V4L2_CID_PRIVATE_BASE + 44;
   private static final int V4L2_CID_PRIVATE_TAVARUA_ON_CHANNEL_THRESHOLD  = V4L2_CID_PRIVATE_BASE + 0x2D;
   private static final int V4L2_CID_PRIVATE_TAVARUA_OFF_CHANNEL_THRESHOLD = V4L2_CID_PRIVATE_BASE + 0x2E;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SINR_THRESHOLD        = V4L2_CID_PRIVATE_BASE + 0x2F;
   private static final int V4L2_CID_PRIVATE_TAVARUA_SINR_SAMPLES          = V4L2_CID_PRIVATE_BASE + 0x30;
   private static final int V4L2_CID_PRIVATE_SPUR_FREQ                     = V4L2_CID_PRIVATE_BASE + 0x31;
   private static final int V4L2_CID_PRIVATE_SPUR_FREQ_RMSSI               = V4L2_CID_PRIVATE_BASE + 0x32;
   private static final int V4L2_CID_PRIVATE_SPUR_SELECTION                = V4L2_CID_PRIVATE_BASE + 0x33;
   private static final int V4L2_CID_PRIVATE_AF_RMSSI_TH                   = V4L2_CID_PRIVATE_BASE + 0x36;
   private static final int V4L2_CID_PRIVATE_AF_RMSSI_SAMPLES              = V4L2_CID_PRIVATE_BASE + 0x37;
   private static final int V4L2_CID_PRIVATE_GOOD_CH_RMSSI_TH              = V4L2_CID_PRIVATE_BASE + 0x38;
   private static final int V4L2_CID_PRIVATE_SRCHALGOTYPE                  = V4L2_CID_PRIVATE_BASE + 0x39;
   private static final int V4L2_CID_PRIVATE_CF0TH12                       = V4L2_CID_PRIVATE_BASE + 0x3A;
   private static final int V4L2_CID_PRIVATE_SINRFIRSTSTAGE                = V4L2_CID_PRIVATE_BASE + 0x3B;
   private static final int V4L2_CID_PRIVATE_RMSSIFIRSTSTAGE               = V4L2_CID_PRIVATE_BASE + 0x3C;
   private static final int V4L2_CID_PRIVATE_RXREPEATCOUNT                 = V4L2_CID_PRIVATE_BASE + 0x3D;
   private static final int V4L2_CID_PRIVATE_RSSI_TH                       = V4L2_CID_PRIVATE_BASE + 0x3E;
   private static final int V4L2_CID_PRIVATE_AF_JUMP_RSSI_TH               = V4L2_CID_PRIVATE_BASE + 0x3F;
   private static final int V4L2_CID_PRIVATE_BLEND_SINRHI                  = V4L2_CID_PRIVATE_BASE + 0x40;
   private static final int V4L2_CID_PRIVATE_BLEND_RMSSIHI                 = V4L2_CID_PRIVATE_BASE + 0x41;

   private static final int V4L2_CTRL_CLASS_USER = 0x980000;
   private static final int V4L2_CID_BASE        = V4L2_CTRL_CLASS_USER | 0x900;
   private static final int V4L2_CID_AUDIO_MUTE  = V4L2_CID_BASE + 9;

   private int sOnData  ;
   private int sOffData ;



   /*
    * Turn on FM Rx/Tx.
    * Rx = 1 and Tx = 2
    */
   public void fmOn(int fd, int device) {
      int re;
      FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_STATE, device );
      setAudioPath(fd, false);
      re = FmReceiverJNI.SetCalibrationNative(fd);
      if (re != 0)
         Log.d(TAG,"Calibration failed");
   }

   /*
    * Turn off FM Rx/Tx
    */
   public void fmOff(int fd){
      FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_STATE, 0 );
   }

   /*
    * set mute control
    */
   public void muteControl(int fd, boolean on) {
      if (on)
      {
         int err = FmReceiverJNI.setControlNative(fd, V4L2_CID_AUDIO_MUTE, 3 );
      } else
      {
         int err = FmReceiverJNI.setControlNative(fd, V4L2_CID_AUDIO_MUTE, 0 );
      }
   }
   /*
    * Get Interference over channel
    */
   public int IovercControl(int fd)
   {
      int ioverc = FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_IOVERC);
      Log.d(TAG, "IOVERC value is : "+ioverc);
      return ioverc;
   }
   /*
    * Get IntDet
    */
   public int IntDet(int fd)
   {
      int intdet =  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_INTDET);
      Log.d(TAG, "IOVERC value is : "+intdet);
      return intdet;
   }

   /*
    * Get MPX_DCC
    */
   public int Mpx_Dcc(int fd)
   {
      int mpx_dcc =  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_MPX_DCC);
      Log.d(TAG, "MPX_DCC value is : " + mpx_dcc);
      return mpx_dcc;
   }

   /*
    * Set Hi-Low injection
    */
   public int setHiLoInj(int fd, int inj)
   {
      int re =  FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_HLSI, inj);
      return re;
   }

   /*
    * Set On channel threshold
    */
   public int setOnChannelThreshold(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_ON_CHANNEL_THRESHOLD, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set On channel threshold data");
      return re;
   }

   /*
    * Set Off channel threshold
    */
   public int setOffChannelThreshold(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_OFF_CHANNEL_THRESHOLD, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set Off channel Threshold data");
      return re;
   }

   /*
    * Get On channel threshold
    */
   public int getOnChannelThreshold(int fd)
   {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_ON_CHANNEL_THRESHOLD);
   }

   /*
    * Get Off channel threshold
    */
   public int getOffChannelThreshold(int fd)
   {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_OFF_CHANNEL_THRESHOLD);
   }

   /*
    * Set sinr threshold
    */
   public int setSINRThreshold(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_SINR_THRESHOLD, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set SINR threshold data");
      return re;
   }

   /*
    * Set rssi threshold
    */
   public int setRssiThreshold(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_RSSI_TH, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set RSSI threshold data");
      return re;
   }

   /*
    * Get Rssi threshold
    */
   public int getRssiThreshold(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_RSSI_TH);
   }

   /*
    * Set AF jump rssi threshold
    */
   public int setAfJumpRssiThreshold(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_AF_JUMP_RSSI_TH, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set AF Jump Rssithreshold data");
      return re;
   }

   /*
    * Get AF jump Rssi threshold
    */
   public int getAfJumpRssiThreshold(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_AF_JUMP_RSSI_TH);
   }

   /*
    * Set RDS FIFO count
    */
   public int setRdsFifoCnt(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_RDSD_BUF, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set RDS fifo count data");
      return re;
   }

   /*
    * Get RDS FIFO count
    */
   public int getRdsFifoCnt(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_RDSD_BUF);
   }

   /*
    * Set number of sinr samples to take in to account for SINR avg calculation
    */
   public int setSINRsamples(int fd, int sBuff)
   {
      int re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_SINR_SAMPLES, sBuff);
      if ( re < 0)
         Log.e(TAG, "Failed to set SINR samples ");
      return re;
   }

   /*
    * Get SINR threshold
    */
   public int getSINRThreshold(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_SINR_THRESHOLD);
   }

   /*
    * Get SINR samples
    */
   public int getSINRsamples(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_SINR_SAMPLES);
   }

   /*
    * Get RMSSI Delta
    */
   public int getRmssiDelta(int fd)
   {
      int rmssiDel =  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_RSSI_DELTA);
      return rmssiDel;
   }

   /*
    * Set RMSSI Delta
    */
   public int setRmssiDel(int fd, int delta)
   {
      int re =  FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_RSSI_DELTA, delta);
      return re;
   }

   /*
    * Set the audio path as analog/digital
    */
   public int setAudioPath(int fd, boolean value)
   {
      int mode;
      if (value)
         mode = FM_ANALOG_PATH;
      else
         mode = FM_DIGITAL_PATH;
      int re =  FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_SET_AUDIO_PATH, mode);
      return re;
   }

   /*
    * Tune FM core to specified freq.
    */
   public int setStation(int fd) {
      Log.d(TAG, "** Tune Using: "+fd);
      int ret = FmReceiverJNI.setFreqNative(fd, mFreq);
      Log.d(TAG, "** Returned: "+ret);
      return ret;
   }

  /*
   * Get currently tuned freq
   */
   public int getTunedFrequency(int fd) {
      int frequency = FmReceiverJNI.getFreqNative(fd);
      Log.d(TAG, "getTunedFrequency: "+frequency);
      return frequency;
   }

   public int getFreq (){
      return mFreq;
   }

   public void setFreq (int f){
      mFreq = f;
   }
    /*
    * Get SINR value
    */
   public int getSINR(int fd)
   {
      return  FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_SINR);
   }


   /*
    * Start search list for auto presets
    */
   public int searchStationList (int fd, int mode, int preset_num,
                                   int dir, int pty )
   {
      int re;


     /* set search mode. */
      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCHMODE, mode);
      if (re != 0) {
         return re;
      }

      /* set number of stations to be returned in the list */
      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCH_CNT, preset_num);
      if (re != 0) {
         return re;
      }

      // RDS search list?
      if (pty > 0 ){
        re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCH_PTY, pty);
      }
      if (re != 0) {
         return re;
      }

      /* This triigers the search and once completed the FM core generates
       * searchListComplete event */
      re = FmReceiverJNI.startSearchNative (fd, dir );
      if (re != 0) {
         return re;
      }
      else {
         return 0;
      }

   }

   /* Read search list from buffer */
   public int[] stationList (int fd)
   {
         int freq = 0;
         int i=0, j = 0;
         int station_num = 0;
         float real_freq = 0;
         int [] stationList;
         byte [] sList = new byte[100];
         int tmpFreqByte1=0;
         int tmpFreqByte2=0;
         float lowBand, highBand;


         lowBand  = (float) (FmReceiverJNI.getLowerBandNative(fd) / 1000.00);
         highBand = (float) (FmReceiverJNI.getUpperBandNative(fd) / 1000.00);

         Log.d(TAG, "lowBand: " + lowBand);
         Log.d(TAG, "highBand: " + highBand);

         FmReceiverJNI.getBufferNative(fd, sList, 0);

         if ((int)sList[0] >0) {
            station_num = (int)sList[0];
         }
         stationList = new int[station_num+1];
         Log.d(TAG, "station_num: " + station_num);

         for (i=0;i<station_num;i++) {
            freq = 0;
            Log.d(TAG, " Byte1 = " + sList[i*2+1]);
            Log.d(TAG, " Byte2 = " + sList[i*2+2]);
            tmpFreqByte1 = sList[i*2+1] & 0xFF;
            tmpFreqByte2 = sList[i*2+2] & 0xFF;
            Log.d(TAG, " tmpFreqByte1 = " + tmpFreqByte1);
            Log.d(TAG, " tmpFreqByte2 = " + tmpFreqByte2);
            freq = (tmpFreqByte1 & 0x03) << 8;
            freq |= tmpFreqByte2;
            Log.d(TAG, " freq: " + freq);
            real_freq  = (float)(freq * 50) + (lowBand * FREQ_MUL);//tuner.rangelow * FREQ_MUL;
            Log.d(TAG, " real_freq: " + real_freq);
            if ( (real_freq < (lowBand * FREQ_MUL)) || (real_freq > (highBand * FREQ_MUL)) ) {
               Log.e(TAG, "Frequency out of band limits");
            }
            else {
               stationList[j] = (int)(real_freq);
               Log.d(TAG, " stationList: " + stationList[j]);
               j++;
            }
         }

        try {
          // mark end of list
           stationList[station_num] = 0;
        }
        catch (ArrayIndexOutOfBoundsException e) {
           Log.d(TAG, "ArrayIndexOutOfBoundsException !!");
        }

        return stationList;

   }


   /* configure various search parameters and start search */
   public int searchStations (int fd, int mode, int dwell,
                               int dir, int pty, int pi){
      int re = 0;


      Log.d(TAG, "Mode is " + mode + " Dwell is " + dwell);
      Log.d(TAG, "dir is "  + dir + " PTY is " + pty);
      Log.d(TAG, "pi is " + pi + " id " +  V4L2_CID_PRIVATE_TAVARUA_SRCHMODE);



      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCHMODE, mode);
      if (re != 0) {
          Log.e(TAG, "setting of search mode failed");
          return re;
      }
      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SCANDWELL, dwell);
      if (re != 0) {
          Log.e(TAG, "setting of scan dwell time failed");
          return re;
      }
      if (pty != 0)
      {
         re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCH_PTY, pty);
         if (re != 0) {
             Log.e(TAG, "setting of PTY failed");
             return re;
         }
      }

      if (pi != 0)
      {
         re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_SRCH_PI, pi);
         if (re != 0) {
             Log.e(TAG, "setting of PI failed");
             return re;
         }
      }

      re = FmReceiverJNI.startSearchNative (fd, dir );
      return re;
   }

   /* force mono/stereo mode */
   public int stereoControl(int fd, boolean stereo) {

     if (stereo){
       return  FmReceiverJNI.setMonoStereoNative (fd, 1);
     }
     else {
       return  FmReceiverJNI.setMonoStereoNative (fd, 0);
     }


   }


   public void searchRdsStations(int mode,int dwelling,
                                 int direction, int RdsSrchPty, int RdsSrchPI){
   }

   /*   public void searchStationList(int listMode,int direction,
                                 int listMax,int pgmType) {
   }
   */

   /* cancel search in progress */
   public void cancelSearch (int fd){
      FmReceiverJNI.cancelSearchNative(fd);
   }

   /* Set LPM. This disables all FM core interrupts */
   public int setLowPwrMode (int fd, boolean lpmOn){

      int re=0;

      if (lpmOn){
        re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_LP_MODE, 1);
      }
      else {
        re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_LP_MODE, 0);
      }

      return re;

   }

   /* get current powermode of the FM core. 1 for LPM and 0 Normal mode */
   public int getPwrMode (int fd) {

      int re=0;

      re = FmReceiverJNI.getControlNative (fd, V4L2_CID_PRIVATE_TAVARUA_LP_MODE);

      return re;

   }

   public int updateSpurTable(int fd, int freq, int rmssi, boolean enable) {

      int re;

      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_SPUR_FREQ, freq);
      if (re < 0) {
        Log.e(TAG, "Failed to program the Spur frequency value");
        return re;
      }

      re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_SPUR_FREQ_RMSSI, rmssi);
      if (re < 0) {
        Log.e(TAG, "Failed to program the RMSSI level of the Spur frequency");
        return re;
      }

      if (enable) {
        re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_SPUR_SELECTION, 1);
      }
      else {
        re = FmReceiverJNI.setControlNative (fd, V4L2_CID_PRIVATE_SPUR_SELECTION, 0);
      }
      if (re < 0) {
        Log.e(TAG, "Failed to program Spur selection");
        return re;
      }

      return re;
   }

   public int configureSpurTable(int fd) {
      return FmReceiverJNI.configureSpurTable(fd);
   }

   public int getAFJumpRmssiTh(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_AF_RMSSI_TH);
   }

   public boolean setAFJumpRmssiTh(int fd, int th) {
      int re;
      re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_AF_RMSSI_TH, th);
      if (re < 0) {
           Log.e(TAG, "Error in setting AF jmp Rmssi Threshold");
           return false;
      } else {
           return true;
      }
   }

   public int getAFJumpRmssiSamples(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_AF_RMSSI_SAMPLES);
   }

   public boolean setAFJumpRmssiSamples(int fd, int samples) {
      int re;
      re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_AF_RMSSI_SAMPLES, samples);
      if (re < 0) {
           Log.e(TAG, "Error in setting AF jmp Rmssi Samples");
           return false;
      } else {
           return true;
      }
   }

   public int getGdChRmssiTh(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_GOOD_CH_RMSSI_TH);
   }

   public boolean setGdChRmssiTh(int fd, int th) {
      int re;
      re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_GOOD_CH_RMSSI_TH, th);
      if (re < 0) {
           Log.e(TAG, "Error in setting Good channel Rmssi Threshold");
           return false;
      } else {
           return true;
      }
   }

   public int getSearchAlgoType(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_SRCHALGOTYPE);
   }

   public boolean setSearchAlgoType(int fd, int saerchType) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_SRCHALGOTYPE, saerchType);
      if(ret < 0) {
         Log.e(TAG, "Error in setting Search Algo type");
         return false;
      }else {
         return true;
      }
   }

   public int getSinrFirstStage(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_SINRFIRSTSTAGE);
   }

   public boolean setSinrFirstStage(int fd, int sinr) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_SINRFIRSTSTAGE, sinr);
      if(ret < 0) {
         Log.e(TAG, "Error in setting Sinr First Stage Threshold");
         return false;
      }else {
         return true;
      }
   }

   public int getRmssiFirstStage(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_RMSSIFIRSTSTAGE);
   }

   public boolean setRmssiFirstStage(int fd, int rmssi) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_RMSSIFIRSTSTAGE, rmssi);
      if(ret < 0) {
         Log.e(TAG, "Error in setting Rmssi First stage Threshold");
         return false;
      }else {
         return true;
      }
   }

   public int getCFOMeanTh(int fd) {
      return FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_CF0TH12);
   }

   public boolean setCFOMeanTh(int fd, int th) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_CF0TH12, th);
      if(ret < 0) {
         Log.e(TAG, "Error in setting Mean CFO Threshold");
         return false;
      }else {
         return true;
      }
   }

   public boolean setPSRxRepeatCount(int fd, int count) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_RXREPEATCOUNT, count);
      if (ret < 0) {
          return false;
      } else {
          return true;
      }
   }

   public byte getBlendSinr(int fd) {
      return (byte)FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_BLEND_SINRHI);
   }

   public boolean setBlendSinr(int fd, byte sinrHi) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_BLEND_SINRHI, sinrHi);
      if(ret < 0) {
         Log.e(TAG, "Error in setting sinrHi ");
         return false;
      }else {
         return true;
      }
   }

   public byte getBlendRmssi(int fd) {
      return (byte)FmReceiverJNI.getControlNative(fd, V4L2_CID_PRIVATE_BLEND_RMSSIHI);
   }

   public boolean setBlendRmssi(int fd, byte rmssiHi) {
      int ret;
      ret = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_BLEND_RMSSIHI, rmssiHi);
      if(ret < 0) {
         Log.e(TAG, "Error in setting RmssiHi ");
         return false;
      }else {
         return true;
      }
   }
}