Merge "FM: Handle the error responses"
diff --git a/jni/android_hardware_fm.cpp b/jni/android_hardware_fm.cpp
index 8d2e610..82fdb97 100644
--- a/jni/android_hardware_fm.cpp
+++ b/jni/android_hardware_fm.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2015, 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:
@@ -449,9 +449,10 @@
 {
     int err;
     jboolean isCopy;
-    jbyte *byte_buffer;
+    jbyte *byte_buffer = NULL;
 
     if ((fd >= 0) && (index >= 0)) {
+        ALOGE("index: %d\n", index);
         byte_buffer = env->GetByteArrayElements(buff, &isCopy);
         err = FmIoctlsInterface :: get_buffer(fd,
                                                (char *)byte_buffer,
@@ -460,7 +461,11 @@
         if (err < 0) {
             err = FM_JNI_FAILURE;
         }
-        env->ReleaseByteArrayElements(buff, byte_buffer, 0);
+        if (buff != NULL) {
+            ALOGE("Free the buffer\n");
+            env->ReleaseByteArrayElements(buff, byte_buffer, 0);
+            byte_buffer =  NULL;
+        }
     } else {
         err = FM_JNI_FAILURE;
     }
diff --git a/qcom/fmradio/FmRxControls.java b/qcom/fmradio/FmRxControls.java
index 864e289..767d56f 100644
--- a/qcom/fmradio/FmRxControls.java
+++ b/qcom/fmradio/FmRxControls.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2015, 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:
@@ -114,13 +114,18 @@
     * Turn on FM Rx/Tx.
     * Rx = 1 and Tx = 2
     */
-   public void fmOn(int fd, int device) {
+   public int fmOn(int fd, int device) {
       int re;
-      FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_STATE, device );
+      re = FmReceiverJNI.setControlNative(fd, V4L2_CID_PRIVATE_TAVARUA_STATE, device );
+      if (re < 0) {
+         Log.d(TAG,"setControlNative faile" + V4L2_CID_PRIVATE_TAVARUA_STATE);
+         return re;
+      }
       setAudioPath(fd, false);
       re = FmReceiverJNI.SetCalibrationNative(fd);
       if (re != 0)
          Log.d(TAG,"Calibration failed");
+      return re;
    }
 
    /*
diff --git a/qcom/fmradio/FmTransceiver.java b/qcom/fmradio/FmTransceiver.java
index fa2378e..b39fd7a 100644
--- a/qcom/fmradio/FmTransceiver.java
+++ b/qcom/fmradio/FmTransceiver.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2013, 2015, 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:
@@ -403,6 +403,7 @@
    public boolean enable (FmConfig configSettings, int device){
 
       boolean status;
+      int ret;
       //Acquire the deviceon Enable
       if( !acquire("/dev/radio0")){
          return false;
@@ -414,7 +415,13 @@
           Log.d(TAG, "No existing file to do spur configuration");
       }
       Log.d(TAG, "turning on " + device);
-      mControl.fmOn(sFd, device);
+      ret = mControl.fmOn(sFd, device);
+      if (ret < 0) {
+          Log.d(TAG, "turning on failed");
+          FmReceiverJNI.closeFdNative(sFd);
+          sFd = 0;
+          return false;
+      }
 
       Log.d(TAG, "Calling fmConfigure");
       status = FmConfig.fmConfigure (sFd, configSettings);