Venkateshwarlu Domakonda | aa25363 | 2013-03-25 16:53:03 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011,2012, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer in the |
| 10 | * documentation and/or other materials provided with the distribution. |
| 11 | * * Neither the name of The Linux Foundation nor |
| 12 | * the names of its contributors may be used to endorse or promote |
| 13 | * products derived from this software without specific prior written |
| 14 | * permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | package qcom.fmradio; |
| 30 | |
| 31 | import android.util.Log; |
| 32 | import qcom.fmradio.FmTransceiver; |
| 33 | |
| 34 | class FmTxEventListner { |
| 35 | |
| 36 | |
| 37 | private final int EVENT_LISTEN = 1; |
| 38 | |
| 39 | private final int TUNE_EVENT = 1; /*Tune completion event*/ |
| 40 | private final int TXRDSDAT_EVENT = 16; /*RDS Group available event*/ |
| 41 | private final int TXRDSDONE_EVENT = 17; /*RDS group complete event */ |
| 42 | private final int RADIO_DISABLED = 18; |
| 43 | private final int READY_EVENT = 0; |
| 44 | private byte []buff = new byte[128]; |
| 45 | private Thread mThread; |
| 46 | private static final String TAG = "FMTxEventListner"; |
| 47 | |
| 48 | public void startListner (final int fd, final FmTransmitterCallbacks cb) { |
| 49 | /* start a thread and listen for messages */ |
| 50 | mThread = new Thread(){ |
| 51 | public void run(){ |
| 52 | |
| 53 | Log.d(TAG, "Starting Tx Event listener " + fd); |
| 54 | |
| 55 | while((!Thread.currentThread().isInterrupted())) { |
| 56 | try { |
| 57 | int index = 0; |
| 58 | int freq = 0; |
| 59 | Log.d(TAG, "getBufferNative called"); |
| 60 | int eventCount = FmReceiverJNI.getBufferNative |
| 61 | (fd, buff, EVENT_LISTEN); |
| 62 | Log.d(TAG, "Received event. Count: " + eventCount); |
| 63 | |
| 64 | for(index = 0; index < eventCount; index++) { |
| 65 | Log.d(TAG, "Received <" +buff[index]+ ">" ); |
| 66 | switch(buff[index]){ |
| 67 | case READY_EVENT: |
| 68 | Log.d(TAG, "Got RADIO_ENABLED"); |
| 69 | if(FmTransceiver.getFMPowerState() == |
| 70 | FmTransceiver.subPwrLevel_FMTx_Starting) { |
| 71 | /*Set the state as FMRxOn */ |
| 72 | FmTransceiver.setFMPowerState |
| 73 | (FmTransceiver.FMState_Tx_Turned_On); |
| 74 | Log.v(TAG, "TxEvtList: CURRENT-STATE:" + |
| 75 | "FMTxStarting ---> NEW-STATE : FMTxOn"); |
| 76 | cb.FmTxEvRadioEnabled(); |
| 77 | } |
| 78 | break; |
| 79 | case TUNE_EVENT: |
| 80 | Log.d(TAG, "Got TUNE_EVENT"); |
| 81 | freq = FmReceiverJNI.getFreqNative(fd); |
| 82 | if (freq > 0) |
| 83 | cb.FmTxEvTuneStatusChange(freq); |
| 84 | else |
| 85 | Log.e(TAG, "get frqency cmd failed"); |
| 86 | break; |
| 87 | case TXRDSDAT_EVENT: |
| 88 | Log.d(TAG, "Got TXRDSDAT_EVENT"); |
| 89 | cb.FmTxEvRDSGroupsAvailable(); |
| 90 | break; |
| 91 | case TXRDSDONE_EVENT: |
| 92 | Log.d(TAG, "Got TXRDSDONE_EVENT"); |
| 93 | cb.FmTxEvContRDSGroupsComplete(); |
| 94 | break; |
| 95 | case RADIO_DISABLED: |
| 96 | Log.d(TAG, "Got RADIO_DISABLED"); |
| 97 | if(FmTransceiver.getFMPowerState() == |
| 98 | FmTransceiver.subPwrLevel_FMTurning_Off) { |
| 99 | /*Set the state as FMOff */ |
| 100 | FmTransceiver.setFMPowerState |
| 101 | (FmTransceiver.FMState_Turned_Off); |
| 102 | Log.v(TAG, "TxEvtList:CURRENT-STATE :" + |
| 103 | "FMTurningOff ---> NEW-STATE: FMOff"); |
| 104 | FmTransceiver.release("/dev/radio0"); |
| 105 | cb.FmTxEvRadioDisabled(); |
| 106 | Thread.currentThread().interrupt(); |
| 107 | } else { |
| 108 | Log.d(TAG, "Unexpected RADIO_DISABLED recvd"); |
| 109 | cb.FmTxEvRadioReset(); |
| 110 | } |
| 111 | break; |
| 112 | default: |
| 113 | Log.d(TAG, "Unknown event"); |
| 114 | break; |
| 115 | }//switch |
| 116 | }//for |
| 117 | }catch (Exception ex) { |
| 118 | Log.d( TAG, "RunningThread InterruptedException"); |
| 119 | Thread.currentThread().interrupt(); |
| 120 | }//try |
| 121 | }//while |
| 122 | Log.d(TAG, "Came out of the while loop"); |
| 123 | } |
| 124 | }; |
| 125 | mThread.start(); |
| 126 | } |
| 127 | |
| 128 | public void stopListener(){ |
| 129 | Log.d(TAG, "Thread Stopped\n"); |
| 130 | //Thread stop is deprecate API |
| 131 | //Interrupt the thread and check for the thread status |
| 132 | // and return from the run() method to stop the thread |
| 133 | //properly |
| 134 | Log.d(TAG, "stopping the Listener\n"); |
| 135 | |
| 136 | if(mThread != null) { |
| 137 | mThread.interrupt(); |
| 138 | } |
| 139 | Log.d(TAG, "Thread Stopped\n"); |
| 140 | } |
| 141 | } |