summaryrefslogtreecommitdiffstats
path: root/qcom/fmradio/FmTransmitter.java
blob: 287da3c709fe8e0894e4ece537b41625666e2169 (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
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
/*
 * Copyright (c) 2009-2013, 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;


/**
 * This class contains all interfaces and types needed to control the FM transmitter.
 * @hide
 */
public class FmTransmitter extends FmTransceiver
{
   private final String TAG = "FmTransmitter";
 /**
   *  An object that contains the PS Features that SoC supports
   *
   *  @see #getPSFeatures
   */
    public class FmPSFeatures
    {
       public int maxPSCharacters;
       public int maxPSStringRepeatCount;
    };


  /**
    *  Command types for the RDS group transmission.
    *  This is used as argument to #transmitRdsGroupControl to
    *  control the RDS group transmission.
    *
    *  @see #transmitRdsGroupControl
    */

   public static final int RDS_GRPS_TX_PAUSE    =  0;           /* Pauses the Group transmission*/

   public static final int RDS_GRPS_TX_RESUME   =  1;          /* Resumes the Group transmission*/

   public static final int RDS_GRPS_TX_STOP     =  2;        /* Stops and clear the Group transmission */

   public static final int FM_TX_MAX_PS_LEN           =  (96+1);
   public static final int FM_TX_MAX_RT_LEN           =  (64-1); /*One space to include NULL*/

   private static final int MAX_PS_CHARS = 97;
   private static final int MAX_PS_REP_COUNT = 15;
   private static final int MAX_RDS_GROUP_BUF_SIZE = 62;

   private FmTransmitterCallbacksAdaptor mTxCallbacks;
   private boolean mPSStarted = false;
   private boolean mRTStarted = false;
   private static final int V4L2_CID_PRIVATE_BASE = 0x8000000;
   private static final int V4L2_CID_PRIVATE_TAVARUA_ANTENNA   = V4L2_CID_PRIVATE_BASE + 18;

   /**
    * Power settings
    *
    * @see #setPowerMode
    * @see #getPowerMode
    */
   public static final int FM_TX_NORMAL_POWER_MODE   =0;
   public static final int FM_TX_LOW_POWER_MODE      =1;

   /**
    * Transmit Power level settings
    *
    * @see #setTxPowerLevel
    */
   public static final int FM_TX_PWR_LEVEL_0   =0;
   public static final int FM_TX_PWR_LEVEL_1   =1;
   public static final int FM_TX_PWR_LEVEL_2   =2;
   public static final int FM_TX_PWR_LEVEL_3   =3;
   public static final int FM_TX_PWR_LEVEL_4   =4;
   public static final int FM_TX_PWR_LEVEL_5   =5;
   public static final int FM_TX_PWR_LEVEL_6   =6;
   public static final int FM_TX_PWR_LEVEL_7   =7;


   /**
    *  Constructor for the transmitter class that takes path to
    *  radio device and event callback adapter
    */
   public FmTransmitter(String path, FmTransmitterCallbacksAdaptor callbacks) throws InstantiationException{

       mTxEvents = new FmTxEventListner();
       mControl = new FmRxControls();
       mTxCallbacks = callbacks;
   }

   /*==============================================================
   FUNCTION:  enable
   ==============================================================*/
   /**
    *  Enables the FM device in Transmit Mode.
    *  <p>
    *  This is a synchronous method used to initialize the FM
    *  device in transmitt mode. If already initialized this function will
    *  intialize the Fm device with default settings. Only after
    *  successfully calling this function can many of the FM device
    *  interfaces be used.
    *  <p>
    *  When enabling the transmitter, the application must also
    *  provide the regional settings in which the transmitter will
    *  operate. These settings (included in argument
    *  configSettings) are typically used for setting up the FM
    *  Transmitter for operating in a particular geographical
    *  region. These settings can be changed after the FM driver
    *  is enabled through the use of the function {@link
    *  #configure}.
    *  <p>
    *  This command can only be issued by the owner of an FM
    *  transmitter.
    *
    *  @param configSettings  the settings to be applied when
    *                           turning on the radio
    *  @return true if Initialization succeeded, false if
    *          Initialization failed.
    *  <p>
    *  @see #enable
    *  @see #registerTransmitClient
    *  @see #disable
    *
    */
   public boolean enable (FmConfig configSettings){
      boolean status = false;

      int state = getFMState();
      if (state == FMState_Tx_Turned_On) {
          Log.d(TAG, "enable: FM Tx already turned On and running");
          return status;
      }else if (state == subPwrLevel_FMTurning_Off) {
          Log.v(TAG, "FM is in the process of turning off.Pls wait for sometime.");
          return status;
      }else if((state == subPwrLevel_FMTx_Starting)
                ||(state == subPwrLevel_FMRx_Starting)) {
          Log.v(TAG, "FM is in the process of turning On.Pls wait for sometime.");
          return status;
      }else if((state == FMState_Srch_InProg)
                ||(state == FMState_Rx_Turned_On)) {
          Log.v(TAG, "FM Rx is turned on");
          return status;
      }
      setFMPowerState(subPwrLevel_FMTx_Starting);
      Log.v(TAG, "enable: CURRENT-STATE : FMOff ---> NEW-STATE : FMTxStarting");
      status = super.enable(configSettings, FmTransceiver.FM_TX);
      if(status == true) {
         registerTransmitClient(mTxCallbacks);
         mRdsData = new FmRxRdsData(sFd);
      } else {
         status = false;
         Log.e(TAG, "enable: failed to turn On FM TX");
         Log.e(TAG, "enable: CURRENT-STATE : FMTxStarting ---> NEW-STATE : FMOff");
         setFMPowerState(FMState_Turned_Off);
      }
      return status;
   }
   /*==============================================================
   FUNCTION:  setRdsOn
   ==============================================================*/
   /**
   *
   *    This function enables RDSCTRL register for SoC.
   *
   *    <p>
   *    This API enables the ability of the FM driver
   *    to send Program Service, RadioText  information.
   *
   *
   *    @return true if the command was placed successfully, false
   *            if command failed.
   *
   */
   public boolean setRdsOn (){

      if (mRdsData == null)
         return false;
      // Enable RDS
      int re = mRdsData.rdsOn(true);

      if (re ==0)
        return true;

      return false;
   }

   /*==============================================================
   FUNCTION:  disable
   ==============================================================*/
   /**
    *  Disables the FM Transmitter Device.
    *  <p>
    *  This is a synchronous command used to disable the FM
    *  device. This function is expected to be used when the
    *  application no longer requires use of the FM device. Once
    *  called, most functionality offered by the FM device will be
    *  disabled until the application re-enables the device again
    *  via {@link #enable}.
    *
    *  <p>
    *  @return true if disabling succeeded, false if disabling
    *          failed.
    *
    *  @see #enable
    *  @see #registerTransmitClient
    */
   public boolean disable(){
      boolean status = false;
      int state;

      state = getFMState();
      switch(state) {
         case FMState_Turned_Off:
              Log.d(TAG, "FM already tuned Off.");
              return true;
         case subPwrLevel_FMTx_Starting:
              /*
               * If, FM is in the process of turning On, then wait for
               * the turn on operation to complete before turning off.
               */
              Log.d(TAG, "disable: FM not yet turned On...");
              try {
                   Thread.sleep(100);
              } catch (InterruptedException e) {
                   e.printStackTrace();
              }
              /* Check for the state of FM device */
              state = getFMState();
              if(state == subPwrLevel_FMTx_Starting) {
                 Log.e(TAG, "disable: FM in bad state");
                 return status;
              }
              break;
         case subPwrLevel_FMTurning_Off:
              /*
               * If, FM is in the process of turning Off, then wait for
               * the turn off operation to complete.
               */
              Log.v(TAG, "disable: FM is getting turned Off.");
              return status;
      }
      setFMPowerState(subPwrLevel_FMTurning_Off);
      Log.v(TAG, "disable: CURRENT-STATE : FMTxOn ---> NEW-STATE : FMTurningOff");
      //Stop all the RDS transmissions if there any
      if(mPSStarted) {
         if(!stopPSInfo()) {
            Log.d(TAG, "FmTrasmitter:stopPSInfo failed\n");
         }
      }
      if(mRTStarted) {
        if(!stopRTInfo()) {
           Log.d(TAG, "FmTrasmitter:stopRTInfo failed\n");
        }
      }
      if(!transmitRdsGroupControl(RDS_GRPS_TX_STOP) ) {
         Log.d(TAG, "FmTrasmitter:transmitRdsGroupControl failed\n");
      }
      super.disable();
      return true;
   }

   /*==============================================================
   FUNCTION:  reset
   ==============================================================*/
   /**
   *    Reset the FM Device.
   *    <p>
   *    This is a synchronous command used to reset the state of FM
   *    device in case of unrecoverable error. This function is
   *    expected to be used when the client receives unexpected
   *    notification of radio disabled. Once called, most
   *    functionality offered by the FM device will be disabled
   *    until the client re-enables the device again via
   *    {@link #enable}.
   *    <p>
   *    @return true if reset succeeded, false if reset failed.
   *    @see #enable
   *    @see #disable
   *    @see #registerTransmitClient
   */
   public boolean reset(){
      boolean status = false;
      int state = getFMState();

      if(state == FMState_Turned_Off) {
         Log.d(TAG, "FM already turned Off.");
         return false;
      }
      setFMPowerState(FMState_Turned_Off);
      Log.v(TAG, "reset: NEW-STATE : FMState_Turned_Off");
      status = unregisterTransmitClient();
      release("/dev/radio0");
      return status;
   }

  /*==============================================================
   FUNCTION:  setStation
   ==============================================================*/
   /**
    *    Tunes the FM device to the specified FM frequency.
    *    <p>
    *    This method tunes the FM device to a station specified by the
    *    provided frequency. Only valid frequencies within the band
    *    set by enable or configure can be tuned by this function.
    *    Attempting to tune to frequencies outside of the set band
    *    will result in an error.
    *    <p>
    *    Once tuning to the specified frequency is completed, the
    *    event callback FmTransmitterCallbacks::onTuneStatusChange will be called.
    *
    *    @param frequencyKHz  Frequency (in kHz) to be tuned
    *                         (Example: 96500 = 96.5Mhz)
    *   @return true if setStation call was placed successfully,
    *           false if setStation failed.
    */
   public boolean setStation (int frequencyKHz) {

      //Stop  If there is any ongoing RDS transmissions
      boolean status = false;
      if( mPSStarted ){
          Log.d(TAG,"FmTransmitter:setStation mPSStarted");
         if( !stopPSInfo() ) return status;
      }
      if( mRTStarted ) {
          Log.d(TAG,"FmTransmitter:setStation mRTStarted");
        if(!stopRTInfo()) return status;
      }
      if(!transmitRdsGroupControl(RDS_GRPS_TX_STOP) )return status;

      Log.d(TAG, "FmTrasmitter:SetStation\n");
      status = super.setStation(frequencyKHz);

      return status;
   }
   /*==============================================================
   FUNCTION:  setPowerMode
   ==============================================================*/
   /**
   *    Puts the driver into or out of low power mode.
   *
   *    <p>
   *    This is an synchronous command which can put the FM
   *    device and driver into and out of low power mode. Low power mode
   *    should be used when the receiver is tuned to a station and only
   *    the FM audio is required. The typical scenario for low power mode
   *    is when the FM application is no longer visible.
   *
   *    <p>
   *    While in low power mode, all normal FM and RDS indications from
   *    the FM driver will be suppressed. By disabling these indications,
   *    low power mode can result in fewer interruptions and this may lead
   *    to a power savings.
   *
   *    <p>
   *    @param powerMode the new driver operating mode.
   *
   *    @return true if setPowerMode succeeded, false if
   *            setPowerMode failed.
   */
   public boolean setPowerMode(int powerMode){

      int re;

      if (powerMode == FM_TX_LOW_POWER_MODE) {
        re = mControl.setLowPwrMode (sFd, true);
      }
      else {
        re = mControl.setLowPwrMode (sFd, false);
      }

      if (re == 0)
         return true;
      return false;
   }


   /*==============================================================
   FUNCTION:  getPSFeatures
   ==============================================================*/
   /**
    *  This function returns the features supported by the FM
    *  driver when using {@link #setPSInfo}.
    *  <p>
    *  This function is used to get the features the FM driver
    *  supports when transmitting Program Service information.
    *  Included in the returned features is the number of Program
    *  Service (PS) characters which can be transmitted using
    *  {@link #setPSInfo}. If the driver supports continuous
    *  transmission of Program Service Information, this function
    *  will return a value greater than 0 for
    *  FmPSFeatures.maxPSCharacters. Although the RDS/RBDS
    *  standard defines each Program Service (PS) string as eight
    *  characters in length, the FM driver may have the ability to
    *  accept a string that is greater than eight character. This
    *  extended string will thenbe broken up into multiple strings
    *  of length eight and transmitted continuously.
    *  <p>
    *  When transmitting more than one string, the application may
    *  want to control the timing of how long each string is
    *  transmitted. Included in the features returned from this
    *  function is the maximum Program Service string repeat count
    *  (FmPSFeatures.maxPSStringRepeatCount). When using the
    *  function {@link #setPSInfo}, the application can specify how
    *  many times each string is repeated before the next string is
    *  transmitted.
    *
    *  @return the Program service maximum characters and repeat
    *          count
    *
    *  @see #setPSInfo
    *
    */
   public FmPSFeatures getPSFeatures(){
      FmPSFeatures psFeatures = new FmPSFeatures();

      psFeatures.maxPSCharacters = MAX_PS_CHARS;
      psFeatures.maxPSStringRepeatCount = MAX_PS_REP_COUNT;
      return psFeatures;
   }

   /*==============================================================
   FUNCTION:  startPSInfo
   ==============================================================*/
   /**
    *  Continuously transmit RDS/RBDS Program Service information
    *  over an already tuned station.
    *  <p>
    *  This is a synchronous function used to continuously transmit Program
    *  Service information over an already tuned station. While
    *  Program Service information can be transmitted using {@link
    *  #transmitRdsGroups} and 0A/0B groups, this function makes
    *  the same output possible with limited input needed from the
    *  application.
    *  <p>
    *  Included in the Program Service information is an RDS/RBDS
    *  program type (PTY), and one or more Program Service
    *  strings. The program type (PTY) is used to describe the
    *  content being transmitted and follows the RDS/RBDS program
    *  types described in the RDS/RBDS specifications.
    *  <p>
    *  Program Service information also includes an eight
    *  character string. This string can be used to display any
    *  information, but is typically used to display information
    *  about the audio being transmitted. Although the RDS/RBDS
    *  standard defines a Program Service (PS) string as eight
    *  characters in length, the FM driver may have the ability to
    *  accept a string that is greater than eight characters. This
    *  extended string will then be broken up into multiple eight
    *  character strings which will be transmitted continuously.
    *  All strings passed to this function must be terminated by a
    *  null character (0x00).
    *  <p>
    *  When transmitting more than one string, the application may
    *  want to control the timing of how long each string is
    *  transmitted. To control this timing and to ensure that the FM
    *  receiver receives each string, the application can specify
    *  how many times each string is repeated before the next string
    *  is transmitted. This command can only be issued by the owner
    *  of an FM transmitter.
    *  <p>
    *  Maximux Programme service string lenght that can be sent is
    *  FM_TX_MAX_PS_LEN. If the application sends PS string longer than
    *  this threshold, string will be truncated to FM_TX_MAX_PS_LEN.
    *
    *  @param psStr the program service strings to transmit
    *  @param pty   the program type to use in the program Service
    *               information.
    *  @param pi    the program type to use in the program Service
    *               information.
    *  @param repeatCount the number of times each 8 char string is
    *                     repeated before next string
    *
    *  @return true if PS information was successfully sent to the
    *             driver, false if PS information could not be sent
    *             to the driver.
    *
    *  @see #getPSFeatures
    *  @see #stopPSInfo
    */
   public boolean startPSInfo(String psStr, int pty, int pi, int repeatCount){

       //Set the PTY
       if((pty < 0) || (pty > 31 )) {
           Log.d(TAG,"pTy is expected from 0 to 31");
           return false;
       }

       int err = FmReceiverJNI.setPTYNative( sFd, pty );
       if( err < 0 ){
           Log.d(TAG,"setPTYNative is failure");
          return false;
       }

       if((pi < 0) || (pi > 65535)) {
           Log.d(TAG,"pi is expected from 0 to 65535");
           return false;
       }

       //Set the PI
       err = FmReceiverJNI.setPINative( sFd, pi );
       if( err < 0 ){
           Log.d(TAG,"setPINative is failure");
          return false;
       }

       if((repeatCount < 0) || (repeatCount > 15)) {
           Log.d(TAG,"repeat count is expected from 0 to 15");
           return false;
       }

       err = FmReceiverJNI.setPSRepeatCountNative( sFd, repeatCount );
       if( err < 0 ){
           Log.d(TAG,"setPSRepeatCountNative is failure");
          return false;
       }

       if( psStr.length() > FM_TX_MAX_PS_LEN ){
          /*truncate the PS string to
          MAX possible length*/
          psStr = psStr.substring( 0, FM_TX_MAX_PS_LEN );

       }

       err = FmReceiverJNI.startPSNative( sFd, psStr , psStr.length() );
       Log.d(TAG,"return for startPS is "+err);

       if( err < 0 ){
           Log.d(TAG, "FmReceiverJNI.startPSNative returned false\n");
           return false;

       }   else {
           Log.d(TAG,"startPSNative is successful");
          mPSStarted = true;
          return true;
       }
   }

   /*==============================================================
   FUNCTION:  stopPSInfo
   ==============================================================*/
   /**
    *  Stops an active Program Service transmission.
    *
    *  <p>
    *  This is a synchrnous function used to stop an active Program Service transmission
    *  started by {@link #startPSInfo}.
    *
    *  @return true if Stop PS information was successfully sent to
    *             the driver, false if Stop PS information could not
    *             be sent to the driver.
    *
    *  @see #getPSFeatures
    *  @see #startPSInfo
    *
    */
   public boolean stopPSInfo(){
       int err =0;
       if( (err =FmReceiverJNI.stopPSNative( sFd )) < 0  ){
           Log.d(TAG,"return for startPS is "+err);
          return false;
       }    else{
           Log.d(TAG,"stopPSNative is successful");
          mPSStarted = false;
          return true;
       }
   }


   /*==============================================================
   FUNCTION:  startRTInfo
   ==============================================================*/
   /**
    *  Continuously transmit RDS/RBDS RadioText information over an
    *  already tuned station.
    *
    *  <p>
    *  This is a synchronous function used to continuously transmit RadioText
    *  information over an already tuned station. While RadioText
    *  information can be transmitted using
    *  {@link #transmitRdsGroups} and 2A/2B groups, this function
    *  makes the same output possible with limited input needed from
    *  the application.
    *  <p>
    *  Included in the RadioText information is an RDS/RBDS program type (PTY),
    *  and a single string of up to 64 characters. The program type (PTY) is used
    *  to describe the content being transmitted and follows the RDS/RBDS program
    *  types described in the RDS/RBDS specifications.
    *  <p>
    *  RadioText information also includes a string that consists of up to 64
    *  characters. This string can be used to display any information, but is
    *  typically used to display information about the audio being transmitted.
    *  This RadioText string is expected to be at 64 characters in length, or less
    *  than 64 characters and terminated by a return carriage (0x0D). All strings
    *  passed to this function must be terminated by a null character (0x00).
    *  <p>
    *  <p>
    *  Maximux Radio Text string length that can be sent is
    *  FM_TX_MAX_RT_LEN. If the application sends RT string longer than
    *  this threshold, string will be truncated to FM_TX_MAX_RT_LEN.
    *
    *  @param rtStr the Radio Text string to transmit
    *  @param pty the program type to use in the Radio text
    *             transmissions.
    *  @param pi the program identifier to use in the Radio text
    *             transmissions.
    *
    *  @return true if RT information String was successfully sent
    *             to the driver, false if RT information string
    *             could not be sent to the driver.
    *
    *  @see #stopRTInfo
    */
   public boolean startRTInfo(String rtStr, int pty, int pi){

       if((pty < 0) || (pty > 31 )) {
           Log.d(TAG,"pTy is expected from 0 to 31");
           return false;
       }
       //Set the PTY
       int err = FmReceiverJNI.setPTYNative( sFd, pty );
       if( err < 0 ){
           Log.d(TAG,"setPTYNative is failure");
          return false;
       }

       if((pi < 0) || (pi > 65535)) {
           Log.d(TAG,"pi is expected from 0 to 65535");
           return false;
       }

       err = FmReceiverJNI.setPINative( sFd, pi );
       if( err < 0 ){
           Log.d(TAG,"setPINative is failure");
          return false;
       }


       if( rtStr.length() > FM_TX_MAX_RT_LEN )
       {
          //truncate it to max length
          rtStr = rtStr.substring( 0, FM_TX_MAX_RT_LEN );
       }

       err = FmReceiverJNI.startRTNative( sFd, rtStr, rtStr.length() );

       if( err < 0 ){
          Log.d(TAG, "FmReceiverJNI.startRTNative returned false\n");
          return false;
       }   else {
           Log.d(TAG,"mRTStarted is true");
          mRTStarted = true;
          return true;
       }
   }

   /*==============================================================
   FUNCTION:  stopRTInfo
   ==============================================================*/
   /**
    *  Stops an active Radio Text information transmission.
    *
    *  <p>
    *  This is a synchrnous function used to stop an active Radio Text
    *  transmission started by {@link #startRTInfo}.
    *
    *  @return true if Stop RT information was successfully sent to
    *             the driver, false if Stop RT information could not
    *             be sent to the driver.
    *
    *  @see #startRTInfo
    *
    */
   public boolean stopRTInfo(){

      if( FmReceiverJNI.stopRTNative( sFd ) < 0  ){
          Log.d(TAG,"stopRTNative is failure");
          return false;
       }    else{
           Log.d(TAG,"mRTStarted is false");
          mRTStarted = false;
          return true;
       }
   }

  /*==============================================================
   FUNCTION:  getRdsGroupBufSize
   ==============================================================*/
   /**
    *  Get the maximum number of RDS/RBDS groups which can be passed
    *  to the FM driver.
    *  <p>
    *  This is a function used to determine the maximum RDS/RBDS
    *  buffer size for use when calling {@link #transmitRdsGroups}
    *
    *  @return the maximum number of RDS/RBDS groups which can be
    *  passed to the FM driver at any one time.
    *
    */
   public int getRdsGroupBufSize(){

   return MAX_RDS_GROUP_BUF_SIZE;
   }


   /*==============================================================
   FUNCTION:  transmitRdsGroups
   ==============================================================*/
   /**
    *  This function will transmit RDS/RBDS groups
    *  over an already tuned station.
    *  This is an asynchronous function used to transmit RDS/RBDS
    *  groups over an already tuned station. This functionality is
    *  is currently unsupported.
    *  <p>
    *  This function accepts a buffer (rdsGroups) containing one or
    *  more RDS groups. When sending this buffer, the application
    *  must also indicate how many groups should be taken from this
    *  buffer (numGroupsToTransmit). It may be possible that the FM
    *  driver can not accept the number of group contained in the
    *  buffer and will indicate how many group were actually
    *  accepted through the return value.
    *
    *  <p>
    *  The FM driver will indicate to the application when it is
    *  ready to accept more data via both the
    *  "onRDSGroupsAvailable()" and "onRDSGroupsComplete()" events
    *  callbacks. The "onRDSGroupsAvailable()" callback will
    *  indicate to the application that the FM driver can accept
    *  additional groups even though all groups may not have been
    *  passed to the FM transmitter. The onRDSGroupsComplete()
    *  callback will indicate when the FM driver has a complete
    *  buffer to transmit RDS data. In many cases all data passed to
    *  the FM driver will be passed to the FM hardware and only a
    *  onRDSGroupsComplete() event will be generated by the
    *  FM driver.
    *  <p> If the application attempts to send more groups than the
    *  FM driver can handle, the application must wait until it
    *  receives a onRDSGroupsAvailable or a onRDSGroupsComplete
    *  event before attempting to transmit more groups. Failure to
    *  do so may result in no group being consumed by the FM driver.
    *  <p> It is important to note that switching between continuous
    *  and non-continuous transmission of RDS groups can only happen
    *  when no RDS/RBDS group transmission is underway. If an
    *  RDS/RBDS group transmission is already underway, the
    *  application must wait for a onRDSGroupsComplete. If the application
    *  wishes to switch from continuous to non-continuous (or
    *  vice-versa) without waiting for the current transmission to
    *  complete, the application can clear all remaining groups
    *  using the {@link #transmitRdsGroupControl} command.
    *  <p>
    *  Once completed, this command will generate a
    *  onRDSGroupsComplete event to all registered applications.
    *
    *  @param rdsGroups The RDS/RBDS groups buffer to transmit.
    *  @param numGroupsToTransmit The number of groups in the buffer
    *                             to transmit.
    *
    *  @return The number of groups the FM driver actually accepted.
    *          A value >0 indicates the command was successfully
    *          accepted and a return value of "-1" indicates error.
    *
    *  @see #transmitRdsGroupControl
    */

   public int transmitRdsGroups(byte[] rdsGroups, long numGroupsToTransmit){
      /*
       * This functionality is currently unsupported
       */

    return -1;
   }
   /*==============================================================
   FUNCTION:  transmitContRdsGroups
   ==============================================================*/
   /**
    *  This function will continuously transmit RDS/RBDS groups over an already tuned station.
    *  <p>
    *  This is an asynchronous function used to continuously
    *  transmit RDS/RBDS groups over an already tuned station.
    *  This functionality is currently unsupported.
    *  <p>
    *  This function accepts a buffer (rdsGroups) containing one or
    *  more RDS groups. When sending this buffer, the application
    *  must also indicate how many groups should be taken from this
    *  buffer (numGroupsToTransmit). It may be possible that the FM
    *  driver can not accept the number of group contained in the
    *  buffer and will indicate how many group were actually
    *  accepted through the return value.
    *
    *  <p>
    *  Application can send a complete RDS group buffer for the transmission.
    *  This data will be sent continuously to the driver. Only single RDS
    *  group can be continuously transmitter at a time. So, application has to
    *  send the complete RDS buffer it intends to transmit. trying to pass the
    *  single buffer in two calls will be interprted as two different RDS/RBDS
    *  groups and hence all the unset groups will be cleared.
    *  <p>
    *  As continuous RDS/RBDS group transmission is done over single buffer,
    *  Application has to wait for the "onContRDSGroupsComplete()" callback
    *  to initiate the further RDS/RBDS group transmissions. Failure to
    *  do so may result in no group being consumed by the FM driver.
    *  <p> It is important to note that switching between continuous
    *  and non-continuous transmission of RDS groups can only happen
    *  when no RDS/RBDS group transmission is underway. If an
    *  RDS/RBDS group transmission is already underway, the
    *  application must wait for a onRDSGroupsComplete or onContRDSGroupsComplete.
    *   If the application wishes to switch from continuous to non-continuous (or
    *  vice-versa) without waiting for the current transmission to
    *  complete, the application can clear all remaining groups
    *  using the {@link #transmitRdsGroupControl} command.
    *  <p>
    *  Once completed, this command will generate a
    *  onRDSContGroupsComplete event to all registered applications.
    *
    *  @param rdsGroups The RDS/RBDS groups buffer to transmit.
    *  @param numGroupsToTransmit The number of groups in the buffer
    *                             to transmit.
    *
    *  @return The number of groups the FM driver actually accepted.
    *          A value >0 indicates the command was successfully
    *          accepted and a return value of "-1" indicates error.
    *
    *  @see #transmitRdsGroupControl
    */

   public int transmitRdsContGroups(byte[] rdsGroups, long numGroupsToTransmit){
      /*
       * This functionality is currently unsupported.
       */
     return -1;
   }

   /*==============================================================
   FUNCTION:  transmitRdsGroupControl
   ==============================================================*/
   /**
    *  Pause/Resume RDS/RBDS group transmission, or stop and clear
    *  all RDS groups.
    *  <p>
    *  This is a function used to pause/resume RDS/RBDS
    *  group transmission, or stop and clear all RDS groups. This
    *  function can be used to control continuous and
    *  non-continuous RDS/RBDS group transmissions. This functionality
    *  is currently unsupported.
    *  <p>
    *  @param ctrlCmd The Tx RDS group control.This should be one of the
    *                 contants RDS_GRPS_TX_PAUSE/RDS_GRPS_TX_RESUME/RDS_GRPS_TX_STOP
    *
    *  @return true if RDS Group Control command was
    *             successfully sent to the driver, false if RDS
    *             Group Control command could not be sent to the
    *             driver.
    *
    *  @see #rdsGroupControlCmdType
    *  @see #transmitRdsGroups
    */
   public boolean transmitRdsGroupControl(int ctrlCmd){
      boolean bStatus = true;
      /*
       * This functionality is currently unsupported.
       */
      int val = 0;
      switch( ctrlCmd ) {
         case RDS_GRPS_TX_PAUSE:break;
         case RDS_GRPS_TX_RESUME:break;
         case RDS_GRPS_TX_STOP:break;
         default:
                /*Shouldn't reach here*/
         bStatus = false;
      }
      return bStatus;
   }

   /*==============================================================
   FUNCTION:  setTxPowerLevel
   ==============================================================*/
   /**
    *  Sets the transmitter power level.
    *
    *  <p>
    *  This is a function used for setting the power level of
    *  Tx device.
    *  <p>
    *  @param powLevel The Tx power level value to be set. The value should be
    *                  in range 0-7.If input is -ve level will be set to 0
    *                  and if it is above 7 level will be set to max i.e.,7.
    *
    *  @return true on success, false on failure.
    *
    */
   public boolean setTxPowerLevel(int powLevel){
      boolean bStatus = true;
       int err = FmReceiverJNI.setTxPowerLevelNative( sFd, powLevel );
       if( err < 0 ){
           Log.d(TAG,"setTxPowerLevel is failure");
          return false;
      }
      return bStatus;
   }

   /*
    * getFMState() returns:
    *     '0' if FM State  is OFF
    *     '1' if FM Rx     is On
    *     '2' if FM Tx     is On
    *     '3' if FM device is Searching
    */
   public int getFMState() {
      /* Current State of FM device */
      int currFMState = FmTransceiver.getFMPowerState();
      return currFMState;
   }
};