OSDN Git Service

Set preferred PHY and read PHY implementation (1/3)
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 23 Mar 2017 02:06:09 +0000 (19:06 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Thu, 23 Mar 2017 20:48:34 +0000 (20:48 +0000)
Test: manual
Bug: 30622771
Change-Id: I43b7b036e50188f76d8ed0609f5142b2a4ad07ee

jni/com_android_bluetooth_gatt.cpp
src/com/android/bluetooth/gatt/GattService.java

index 9322b87..90c3835 100644 (file)
@@ -161,6 +161,8 @@ static jmethodID method_onTrackAdvFoundLost;
 static jmethodID method_onScanParamSetupCompleted;
 static jmethodID method_getSampleGattDbElement;
 static jmethodID method_onGetGattDb;
+static jmethodID method_onClientPhyUpdate;
+static jmethodID method_onClientPhyRead;
 
 /**
  * Server callback methods
@@ -179,6 +181,8 @@ static jmethodID method_onExecuteWrite;
 static jmethodID method_onNotificationSent;
 static jmethodID method_onServerCongestion;
 static jmethodID method_onServerMtuChanged;
+static jmethodID method_onServerPhyUpdate;
+static jmethodID method_onServerPhyRead;
 
 /**
  * Advertiser callback methods
@@ -514,6 +518,16 @@ void btgattc_get_gatt_db_cb(int conn_id, btgatt_db_element_t* db, int count) {
                                array.get());
 }
 
+void btgattc_phy_updated_cb(int conn_id, uint8_t tx_phy, uint8_t rx_phy,
+                            uint8_t status) {
+  CallbackEnv sCallbackEnv(__func__);
+  if (!sCallbackEnv.valid()) return;
+
+  info("ASDFASDFADSFDSAFDS");
+  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onClientPhyUpdate, conn_id,
+                               tx_phy, rx_phy, status);
+}
+
 static const btgatt_scanner_callbacks_t sGattScannerCallbacks = {
     btgattc_scan_result_cb,
     btgattc_batchscan_reports_cb,
@@ -538,8 +552,8 @@ static const btgatt_client_callbacks_t sGattClientCallbacks = {
     btgattc_congestion_cb,
     btgattc_get_gatt_db_cb,
     NULL, /* services_removed_cb */
-    NULL  /* services_added_cb */
-};
+    NULL, /* services_added_cb */
+    btgattc_phy_updated_cb};
 
 /**
  * BTA server callbacks
@@ -703,6 +717,15 @@ void btgatts_mtu_changed_cb(int conn_id, int mtu) {
                                conn_id, mtu);
 }
 
+void btgatts_phy_updated_cb(int conn_id, uint8_t tx_phy, uint8_t rx_phy,
+                            uint8_t status) {
+  CallbackEnv sCallbackEnv(__func__);
+  if (!sCallbackEnv.valid()) return;
+
+  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onServerPhyUpdate, conn_id,
+                               tx_phy, rx_phy, status);
+}
+
 static const btgatt_server_callbacks_t sGattServerCallbacks = {
     btgatts_register_app_cb,
     btgatts_connection_cb,
@@ -717,7 +740,8 @@ static const btgatt_server_callbacks_t sGattServerCallbacks = {
     btgatts_response_confirmation_cb,
     btgatts_indication_sent_cb,
     btgatts_congestion_cb,
-    btgatts_mtu_changed_cb};
+    btgatts_mtu_changed_cb,
+    btgatts_phy_updated_cb};
 
 /**
  * GATT callbacks
@@ -793,6 +817,10 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
                        "()Lcom/android/bluetooth/gatt/GattDbElement;");
   method_onGetGattDb =
       env->GetMethodID(clazz, "onGetGattDb", "(ILjava/util/ArrayList;)V");
+  method_onClientPhyRead =
+      env->GetMethodID(clazz, "onClientPhyRead", "(IIII)V");
+  method_onClientPhyUpdate =
+      env->GetMethodID(clazz, "onClientPhyUpdate", "(IIII)V");
 
   // Server callbacks
 
@@ -823,6 +851,10 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
   method_onServerCongestion =
       env->GetMethodID(clazz, "onServerCongestion", "(IZ)V");
   method_onServerMtuChanged = env->GetMethodID(clazz, "onMtuChanged", "(II)V");
+  method_onServerPhyRead =
+      env->GetMethodID(clazz, "onServerPhyRead", "(IIII)V");
+  method_onServerPhyUpdate =
+      env->GetMethodID(clazz, "onServerPhyUpdate", "(IIII)V");
 
   info("classInitNative: Success!");
 }
@@ -959,6 +991,29 @@ static void gattClientDisconnectNative(JNIEnv* env, jobject object,
   sGattIf->client->disconnect(clientIf, &bda, conn_id);
 }
 
+static void gattClientSetPreferredPhyNative(JNIEnv* env, jobject object,
+                                            jint clientIf, jint conn_id,
+                                            jint tx_phy, jint rx_phy,
+                                            jint phy_options) {
+  if (!sGattIf) return;
+  sGattIf->client->set_preferred_phy(conn_id, tx_phy, rx_phy, phy_options);
+}
+
+static void readClientPhyCb(int conn_id, uint8_t tx_phy, uint8_t rx_phy,
+                            uint8_t status) {
+  CallbackEnv sCallbackEnv(__func__);
+  if (!sCallbackEnv.valid()) return;
+
+  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onClientPhyRead, conn_id,
+                               tx_phy, rx_phy, status);
+}
+
+static void gattClientReadPhyNative(JNIEnv* env, jobject object, jint clientIf,
+                                    jint conn_id) {
+  if (!sGattIf) return;
+  sGattIf->client->read_phy(conn_id, base::Bind(readClientPhyCb, conn_id));
+}
+
 static void gattClientRefreshNative(JNIEnv* env, jobject object, jint clientIf,
                                     jstring address) {
   if (!sGattIf) return;
@@ -1422,6 +1477,29 @@ static void gattServerDisconnectNative(JNIEnv* env, jobject object,
   sGattIf->server->disconnect(serverIf, &bda, conn_id);
 }
 
+static void gattServerSetPreferredPhyNative(JNIEnv* env, jobject object,
+                                            jint serverIf, jint conn_id,
+                                            jint tx_phy, jint rx_phy,
+                                            jint phy_options) {
+  if (!sGattIf) return;
+  sGattIf->server->set_preferred_phy(conn_id, tx_phy, rx_phy, phy_options);
+}
+
+static void readServerPhyCb(int conn_id, uint8_t tx_phy, uint8_t rx_phy,
+                            uint8_t status) {
+  CallbackEnv sCallbackEnv(__func__);
+  if (!sCallbackEnv.valid()) return;
+
+  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onServerPhyRead, conn_id,
+                               tx_phy, rx_phy, status);
+}
+
+static void gattServerReadPhyNative(JNIEnv* env, jobject object, jint serverIf,
+                                    jint conn_id) {
+  if (!sGattIf) return;
+  sGattIf->server->read_phy(conn_id, base::Bind(readServerPhyCb, conn_id));
+}
+
 static void gattServerAddServiceNative(JNIEnv* env, jobject object,
                                        jint server_if,
                                        jobject gatt_db_elements) {
@@ -1936,6 +2014,9 @@ static JNINativeMethod sMethods[] = {
      (void*)gattClientConnectNative},
     {"gattClientDisconnectNative", "(ILjava/lang/String;I)V",
      (void*)gattClientDisconnectNative},
+    {"gattClientSetPreferredPhyNative", "(IIIII)V",
+     (void*)gattClientSetPreferredPhyNative},
+    {"gattClientReadPhyNative", "(II)V", (void*)gattClientReadPhyNative},
     {"gattClientRefreshNative", "(ILjava/lang/String;)V",
      (void*)gattClientRefreshNative},
     {"gattClientSearchServiceNative", "(IZJJ)V",
@@ -1967,6 +2048,9 @@ static JNINativeMethod sMethods[] = {
      (void*)gattServerConnectNative},
     {"gattServerDisconnectNative", "(ILjava/lang/String;I)V",
      (void*)gattServerDisconnectNative},
+    {"gattServerSetPreferredPhyNative", "(IIIII)V",
+     (void*)gattServerSetPreferredPhyNative},
+    {"gattServerReadPhyNative", "(II)V", (void*)gattServerReadPhyNative},
     {"gattServerAddServiceNative", "(ILjava/util/List;)V",
      (void*)gattServerAddServiceNative},
     {"gattServerStopServiceNative", "(II)V",
index 03cf7ed..701d9d4 100644 (file)
@@ -372,6 +372,7 @@ public class GattService extends ProfileService {
             service.flushPendingBatchResults(scannerId);
         }
 
+        @Override
         public void clientConnect(
                 int clientIf, String address, boolean isDirect, int transport, int phy) {
             GattService service = getService();
@@ -379,23 +380,26 @@ public class GattService extends ProfileService {
             service.clientConnect(clientIf, address, isDirect, transport, phy);
         }
 
+        @Override
         public void clientDisconnect(int clientIf, String address) {
             GattService service = getService();
             if (service == null) return;
             service.clientDisconnect(clientIf, address);
         }
 
+        @Override
         public void clientSetPreferredPhy(
                 int clientIf, String address, int txPhy, int rxPhy, int phyOptions) {
             GattService service = getService();
             if (service == null) return;
-            // TODO(jpawlowski): implement
+            service.clientSetPreferredPhy(clientIf, address, txPhy, rxPhy, phyOptions);
         }
 
+        @Override
         public void clientReadPhy(int clientIf, String address) {
             GattService service = getService();
             if (service == null) return;
-            // TODO(jpawlowski): implement
+            service.clientReadPhy(clientIf, address);
         }
 
         public void refreshDevice(int clientIf, String address) {
@@ -501,13 +505,13 @@ public class GattService extends ProfileService {
                 int serverIf, String address, int txPhy, int rxPhy, int phyOptions) {
             GattService service = getService();
             if (service == null) return;
-            // TODO(jpawlowski): implement
+            service.serverSetPreferredPhy(serverIf, address, txPhy, rxPhy, phyOptions);
         }
 
         public void serverReadPhy(int clientIf, String address) {
             GattService service = getService();
             if (service == null) return;
-            // TODO(jpawlowski): implement
+            service.serverReadPhy(clientIf, address);
         }
 
         public void addService(int serverIf, BluetoothGattService svc) {
@@ -805,6 +809,54 @@ public class GattService extends ProfileService {
         }
     }
 
+    void onClientPhyUpdate(int connId, int txPhy, int rxPhy, int status) throws RemoteException {
+        if (DBG) Log.d(TAG, "onClientPhyUpdate() - connId=" + connId + ", status=" + status);
+
+        String address = mClientMap.addressByConnId(connId);
+        if (address == null) return;
+
+        ClientMap.App app = mClientMap.getByConnId(connId);
+        if (app == null) return;
+
+        app.callback.onPhyUpdate(address, txPhy, rxPhy, status);
+    }
+
+    void onClientPhyRead(int connId, int txPhy, int rxPhy, int status) throws RemoteException {
+        if (DBG) Log.d(TAG, "onClientPhyRead() - connId=" + connId + ", status=" + status);
+
+        String address = mClientMap.addressByConnId(connId);
+        if (address == null) return;
+
+        ClientMap.App app = mClientMap.getByConnId(connId);
+        if (app == null) return;
+
+        app.callback.onPhyRead(address, txPhy, rxPhy, status);
+    }
+
+    void onServerPhyUpdate(int connId, int txPhy, int rxPhy, int status) throws RemoteException {
+        if (DBG) Log.d(TAG, "onServerPhyUpdate() - connId=" + connId + ", status=" + status);
+
+        String address = mServerMap.addressByConnId(connId);
+        if (address == null) return;
+
+        ServerMap.App app = mServerMap.getByConnId(connId);
+        if (app == null) return;
+
+        app.callback.onPhyUpdate(address, txPhy, rxPhy, status);
+    }
+
+    void onServerPhyRead(int connId, int txPhy, int rxPhy, int status) throws RemoteException {
+        if (DBG) Log.d(TAG, "onServerPhyRead() - connId=" + connId + ", status=" + status);
+
+        String address = mServerMap.addressByConnId(connId);
+        if (address == null) return;
+
+        ServerMap.App app = mServerMap.getByConnId(connId);
+        if (app == null) return;
+
+        app.callback.onPhyRead(address, txPhy, rxPhy, status);
+    }
+
     void onSearchCompleted(int connId, int status) throws RemoteException {
         if (DBG) Log.d(TAG, "onSearchCompleted() - connId=" + connId+ ", status=" + status);
         // Gatt DB is ready!
@@ -1506,6 +1558,32 @@ public class GattService extends ProfileService {
         gattClientDisconnectNative(clientIf, address, connId != null ? connId : 0);
     }
 
+    void clientSetPreferredPhy(int clientIf, String address, int txPhy, int rxPhy, int phyOptions) {
+        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
+
+        Integer connId = mClientMap.connIdByAddress(clientIf, address);
+        if (connId == null) {
+            Log.d(TAG, "clientSetPreferredPhy() - no connection to " + address);
+            return;
+        }
+
+        if (DBG) Log.d(TAG, "clientSetPreferredPhy() - address=" + address + ", connId=" + connId);
+        gattClientSetPreferredPhyNative(clientIf, connId, txPhy, rxPhy, phyOptions);
+    }
+
+    void clientReadPhy(int clientIf, String address) {
+        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
+
+        Integer connId = mClientMap.connIdByAddress(clientIf, address);
+        if (connId == null) {
+            Log.d(TAG, "clientReadPhy() - no connection to " + address);
+            return;
+        }
+
+        if (DBG) Log.d(TAG, "clientReadPhy() - address=" + address + ", connId=" + connId);
+        gattClientReadPhyNative(clientIf, connId);
+    }
+
     int numHwTrackFiltersAvailable() {
         return (AdapterService.getAdapterService().getTotalNumOfTrackableAdvertisements()
                     - mScanManager.getCurrentUsedTrackingAdvertisement());
@@ -1999,6 +2077,32 @@ public class GattService extends ProfileService {
         gattServerDisconnectNative(serverIf, address, connId != null ? connId : 0);
     }
 
+    void serverSetPreferredPhy(int serverIf, String address, int txPhy, int rxPhy, int phyOptions) {
+        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
+
+        Integer connId = mServerMap.connIdByAddress(serverIf, address);
+        if (connId == null) {
+            Log.d(TAG, "serverSetPreferredPhy() - no connection to " + address);
+            return;
+        }
+
+        if (DBG) Log.d(TAG, "serverSetPreferredPhy() - address=" + address + ", connId=" + connId);
+        gattServerSetPreferredPhyNative(serverIf, connId, txPhy, rxPhy, phyOptions);
+    }
+
+    void serverReadPhy(int serverIf, String address) {
+        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
+
+        Integer connId = mServerMap.connIdByAddress(serverIf, address);
+        if (connId == null) {
+            Log.d(TAG, "serverReadPhy() - no connection to " + address);
+            return;
+        }
+
+        if (DBG) Log.d(TAG, "serverReadPhy() - address=" + address + ", connId=" + connId);
+        gattServerReadPhyNative(serverIf, connId);
+    }
+
     void addService(int serverIf, BluetoothGattService service) {
         enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
 
@@ -2303,6 +2407,11 @@ public class GattService extends ProfileService {
     private native void gattClientDisconnectNative(int clientIf, String address,
             int conn_id);
 
+    private native void gattClientSetPreferredPhyNative(
+            int clientIf, int conn_id, int tx_phy, int rx_phy, int phy_options);
+
+    private native void gattClientReadPhyNative(int clientIf, int conn_id);
+
     private native void gattClientRefreshNative(int clientIf, String address);
 
     private native void gattClientSearchServiceNative(int conn_id,
@@ -2344,6 +2453,11 @@ public class GattService extends ProfileService {
     private native void gattServerDisconnectNative(int serverIf, String address,
                                               int conn_id);
 
+    private native void gattServerSetPreferredPhyNative(
+            int clientIf, int conn_id, int tx_phy, int rx_phy, int phy_options);
+
+    private native void gattServerReadPhyNative(int clientIf, int conn_id);
+
     private native void gattServerAddServiceNative(int server_if, List<GattDbElement> service);
 
     private native void gattServerStopServiceNative (int server_if,