Fix SL4N controller in ACTS

Bug: 37321435
Test: Ran a quick SL4N test
Change-Id: I2cbaf26e5121b96c9c632f9dbe9a5ec5ca72909b
diff --git a/acts/framework/acts/controllers/native.py b/acts/framework/acts/controllers/native.py
index 7c9df2b..aa987b1 100644
--- a/acts/framework/acts/controllers/native.py
+++ b/acts/framework/acts/controllers/native.py
@@ -14,7 +14,7 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-from acts.controllers.android import Android
+from acts.controllers.sl4a_client import Sl4aClient
 import json
 import os
 import socket
@@ -46,13 +46,12 @@
         i += 1
 
 
-class NativeAndroid(Android):
+class NativeAndroid(Sl4aClient):
     COUNTER = IDCounter()
 
     def _rpc(self, method, *args):
-        self.lock.acquire()
-        apiid = next(NativeAndroid.COUNTER)
-        self.lock.release()
+        with self._lock:
+            apiid = next(self._counter)
         data = {'id': apiid, 'method': method, 'params': args}
         request = json.dumps(data)
         self.client.write(request.encode("utf8") + b'\n')
@@ -61,9 +60,8 @@
         if not response:
             raise SL4NProtocolError(SL4NProtocolError.NO_RESPONSE_FROM_SERVER)
         #TODO: (tturney) fix the C side from sending \x00 char over the socket.
-        result = json.loads(str(response,
-                                encoding="utf8").rstrip().replace("\x00", ""))
-
+        result = json.loads(
+            str(response, encoding="utf8").rstrip().replace("\x00", ""))
         if result['error']:
             raise SL4NAPIError(result['error'])
         if result['id'] != apiid:
diff --git a/acts/framework/acts/controllers/native_android_device.py b/acts/framework/acts/controllers/native_android_device.py
index 00662b3..cbe3237 100644
--- a/acts/framework/acts/controllers/native_android_device.py
+++ b/acts/framework/acts/controllers/native_android_device.py
@@ -15,7 +15,7 @@
 #   limitations under the License.
 
 from acts.controllers.android_device import AndroidDevice
-from acts.controllers.utils_lib  import host_utils
+from acts.controllers.utils_lib import host_utils
 import acts.controllers.native as native
 from subprocess import call
 
@@ -29,8 +29,8 @@
 
 
 def create(configs):
-    logger = logging.getLogger()
-    ads = get_instances(configs, logger)
+    logger = logging
+    ads = get_instances(configs)
     for ad in ads:
         try:
             ad.get_droid()
@@ -43,7 +43,7 @@
     pass
 
 
-def get_instances(serials, logger=None):
+def get_instances(serials, ):
     """Create AndroidDevice instances from a list of serials.
 
     Args:
@@ -55,7 +55,7 @@
     """
     results = []
     for s in serials:
-        results.append(NativeAndroidDevice(s, logger=logger))
+        results.append(NativeAndroidDevice(s))
     return results
 
 
@@ -97,12 +97,10 @@
         if not self.h_port or not host_utils.is_port_available(self.h_port):
             self.h_port = host_utils.get_available_host_port()
         self.adb.tcp_forward(self.h_port, self.d_port)
-        pid = self.adb.shell("ps | grep sl4n | awk '{print $2}'").decode(
-            'ascii')
+        pid = self.adb.shell("pidof -s sl4n", ignore_status=True)
         while (pid):
             self.adb.shell("kill {}".format(pid))
-            pid = self.adb.shell("ps | grep sl4n | awk '{print $2}'").decode(
-                'ascii')
+            pid = self.adb.shell("pidof -s sl4n", ignore_status=True)
         call(
             ["adb -s " + self.serial + " shell sh -c \"/system/bin/sl4n\" &"],
             shell=True)
@@ -127,6 +125,7 @@
             existing uid to a new session.
         """
         droid = native.NativeAndroid(port=self.h_port)
+        droid.open()
         if droid.uid in self._droid_sessions:
             raise bt.SL4NException(("SL4N returned an existing uid for a "
                                     "new session. Abort."))
diff --git a/acts/framework/acts/test_utils/bt/native_bt_test_utils.py b/acts/framework/acts/test_utils/bt/native_bt_test_utils.py
index d070f13..9be24f0 100644
--- a/acts/framework/acts/test_utils/bt/native_bt_test_utils.py
+++ b/acts/framework/acts/test_utils/bt/native_bt_test_utils.py
@@ -25,15 +25,14 @@
 def setup_native_bluetooth(native_devices):
     for n in native_devices:
         droid = n.droid
-        pid = n.adb.shell("ps | grep bluetoothtbd | awk '{print $2}'").decode(
-            'ascii')
+        pid = n.adb.shell("pidof -s bluetoothtbd")
         if not pid:
             call(
                 ["adb -s " + n.serial + " shell sh -c \"bluetoothtbd\" &"],
                 shell=True)
-        droid.BluetoothBinderInitInterface()
+        droid.BtBinderInitInterface()
         time.sleep(5)  #temporary sleep statement
-        droid.BluetoothBinderEnable()
+        droid.BtBinderEnable()
         time.sleep(5)  #temporary sleep statement
-        droid.BluetoothBinderRegisterBLE()
+        droid.BtBinderRegisterBLE()
         time.sleep(5)  #temporary sleep statement
diff --git a/acts/tests/google/native/bt/BtNativeTest.py b/acts/tests/google/native/bt/BtNativeTest.py
index 9660a8e..5315e34 100644
--- a/acts/tests/google/native/bt/BtNativeTest.py
+++ b/acts/tests/google/native/bt/BtNativeTest.py
@@ -1,20 +1,4 @@
-#/usr/bin/env python3.4
-#
-# Copyright (C) 2016 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you may not
-# use this file except in compliance with the License. You may obtain a copy of
-# the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations under
-# the License.
-
-import time
+mport time
 from acts.base_test import BaseTestClass
 from acts.controllers import native_android_device
 from acts.test_utils.bt.native_bt_test_utils import setup_native_bluetooth
@@ -26,14 +10,15 @@
 
     def __init__(self, controllers):
         BaseTestClass.__init__(self, controllers)
-        setup_native_bluetooth(self.native_devices)
-        self.droid = self.native_devices[0].droid
-        self.tests = ("test_binder_get_name",
-                      "test_binder_get_name_invalid_parameter",
-                      "test_binder_set_name_get_name",
-                      "test_binder_get_address", )
-        if len(self.native_devices) > 1:
-            self.droid1 = self.native_devices[1].droid
+        setup_native_bluetooth(self.native_android_devices)
+        self.droid = self.native_android_devices[0].droid
+        self.tests = (
+            "test_binder_get_name",
+            "test_binder_get_name_invalid_parameter",
+            "test_binder_set_name_get_name",
+            "test_binder_get_address", )
+        if len(self.native_android_devices) > 1:
+            self.droid1 = self.native_android_devices[1].droid
             self.tests = self.tests + ("test_two_devices_set_get_name", )
 
     def test_binder_get_name(self):
@@ -67,10 +52,11 @@
 
     def test_two_devices_set_get_name(self):
         test_name = generate_id_by_size(4)
-        for n in self.native_devices:
+        for n in self.native_android_devices:
             d = n.droid
             d.BtBinderSetName(test_name)
             name = d.BtBinderGetName()
             if name != test_name:
                 return False
         return True
+