Python3 Migration: fix bytes vs strings issues

Some bytes vs strings issues were only showing up at run time
since data were passed using XMLRPC methods. This patch fixed
such issues.

BUG=b:169724328
TEST=Follow the steps to make and install with python 3
(cr) make PY_VERSION=python3
(cr) make remote-install "${CHAMELEON_HOST}" PY_VERSION=python3
(cr) Run bluetooth_AdapterQuickHealth.AVL.
     All tests should pass.

Change-Id: I643967e46f4503b32c76e703d77e806de71514b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/chameleon/+/2573720
Commit-Queue: Shyh-In Hwang <josephsih@chromium.org>
Tested-by: Shyh-In Hwang <josephsih@chromium.org>
Reviewed-by: Yu-Hsuan Hsu <yuhsuan@chromium.org>
diff --git a/chameleond/devices/raspi_bluetooth_flow.py b/chameleond/devices/raspi_bluetooth_flow.py
index c61ce63..5ba5755 100644
--- a/chameleond/devices/raspi_bluetooth_flow.py
+++ b/chameleond/devices/raspi_bluetooth_flow.py
@@ -132,7 +132,7 @@
     for scan_code in input_scan_codes:
       time.sleep(.1)
 
-      self.SendHIDReport(scan_code)
+      self.SendHIDReport(bytearray(scan_code))
 
 
   def KeyboardSendString(self, string_to_send):
diff --git a/chameleond/utils/bluetooth_raspi.py b/chameleond/utils/bluetooth_raspi.py
index 956d7b9..51724b3 100644
--- a/chameleond/utils/bluetooth_raspi.py
+++ b/chameleond/utils/bluetooth_raspi.py
@@ -1048,17 +1048,17 @@
             self._CheckValidScanCodes(keys)):
       return None
 
-    real_scan_codes = [chr(key) for key in keys]
-    padding_0s = (chr(0) * (RAW_REPORT_FORMAT_KEYBOARD_LEN_SCAN_CODES -
-                            len(real_scan_codes)))
+    real_scan_codes = [key for key in keys]
+    padding_0s = (0) * (RAW_REPORT_FORMAT_KEYBOARD_LEN_SCAN_CODES -
+                        len(real_scan_codes))
 
-    return (chr(UART_INPUT_RAW_MODE) +
-            chr(RAW_REPORT_FORMAT_KEYBOARD_LENGTH) +
-            chr(RAW_REPORT_FORMAT_KEYBOARD_DESCRIPTOR) +
-            chr(sum(modifiers)) +
-            chr(0x0) +
-            ''.join(real_scan_codes) +
-            padding_0s)
+    return bytearray((UART_INPUT_RAW_MODE,
+                      RAW_REPORT_FORMAT_KEYBOARD_LENGTH,
+                      RAW_REPORT_FORMAT_KEYBOARD_DESCRIPTOR,
+                      sum(modifiers),
+                      0x0) +
+                     tuple(keys) +
+                     padding_0s)
 
 
   def _MouseButtonsRawHidValues(self):
@@ -1174,12 +1174,12 @@
         return value + 256
       return value
 
-    return (chr(RAW_REPORT_START) +
-            chr(RAW_REPORT_FORMAT_MOUSE_DESCRIPTOR) +
-            chr(SignedChar(buttons)) +
-            chr(SignedChar(x_stop)) +
-            chr(SignedChar(y_stop)) +
-            chr(SignedChar(wheel)))
+    return bytearray((RAW_REPORT_START,
+                      RAW_REPORT_FORMAT_MOUSE_DESCRIPTOR,
+                      SignedChar(buttons),
+                      SignedChar(x_stop),
+                      SignedChar(y_stop),
+                      SignedChar(wheel)))
 
 
   def PressShorthandCodes(self, modifiers=None, keys=None):
diff --git a/chameleond/utils/bluetooth_sdp_socket.py b/chameleond/utils/bluetooth_sdp_socket.py
index 59007fa..63bef9d 100644
--- a/chameleond/utils/bluetooth_sdp_socket.py
+++ b/chameleond/utils/bluetooth_sdp_socket.py
@@ -1,5 +1,5 @@
 # Lint as: python2, python3
-# Copyright (c) 2020 The Chromium OS Authors. All rights reserved.
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
@@ -240,7 +240,7 @@
                                           'Supported values are: 16, 32, 128'
                                           % preferred_size)
 
-        res = ''
+        res = b''
         for uuid in uuids:
             # Fall back to 128 bits if the UUID doesn't fit into preferred_size.
             if uuid >= (1 << preferred_size) or preferred_size == 128:
@@ -319,7 +319,7 @@
 
         pattern = self._pack_uuids(uuids, preferred_size) + struct.pack(
                   '>H', max_rec_cnt)
-        cont_state = '\0'
+        cont_state = b'\0'
         handles = []
 
         while True:
@@ -341,7 +341,7 @@
 
             cur_list, cont_state = self._unpack_uuids(response)
             handles.extend(cur_list)
-            if cont_state == '\0':
+            if cont_state == b'\0':
                 break
 
         return handles
@@ -358,7 +358,7 @@
 
         """
         attr_ids.sort()
-        res = ''
+        res = b''
         for attr_id in attr_ids:
             # Each element could be either a single Attribute ID or
             # a range of IDs.
@@ -386,7 +386,7 @@
         """
         res = 0
         for i in range(cnt):
-            res = (res << 8) | ord(data[i])
+            res = (res << 8) | data[i]
         return res
 
 
@@ -435,7 +435,7 @@
 
             res = data[int_size : int_size + str_size]
             scanned += int_size + str_size
-            return res, scanned
+            return res.decode(), scanned
         elif data_type == 6 or data_type == 7:
             if data_size < 5 or data_size > 7:
                 raise BluetoothSDPSocketError('Invalid size descriptor')
@@ -486,11 +486,11 @@
         pattern = (struct.pack('>I', handle) +
                    struct.pack('>H', max_attr_byte_count) +
                    self._pack_attr_ids(attr_ids))
-        cont_state = '\0'
-        complete_response = ''
+        cont_state = b'\0'
+        complete_response = b''
 
         while True:
-            request = (invalid_request if invalid_request
+            request = (invalid_request.encode() if invalid_request
                        else pattern + cont_state)
 
             code, response = self.send_request_and_wait(
@@ -511,7 +511,7 @@
             complete_response += response[:response_byte_count]
             cont_state = response[response_byte_count:]
 
-            if cont_state == '\0':
+            if cont_state == b'\0':
                 break
 
         id_values_list = self._unpack_sdp_data_element(complete_response)[0]
@@ -554,13 +554,13 @@
         pattern = (self._pack_uuids(uuids, preferred_size) +
                    struct.pack('>H', max_attr_byte_count) +
                    self._pack_attr_ids(attr_ids))
-        cont_state = '\0'
-        complete_response = ''
+        cont_state = b'\0'
+        complete_response = b''
 
         while True:
             request = pattern + cont_state
             if invalid_request:
-                request = invalid_request + request
+                request = invalid_request.encode() + request
 
             code, response = self.send_request_and_wait(
                     SDP_SVC_SEARCH_ATTR_REQ, request, forced_pdu_size)
@@ -580,7 +580,7 @@
             complete_response += response[:response_byte_count]
             cont_state = response[response_byte_count:]
 
-            if cont_state == '\0':
+            if cont_state == b'\0':
                 break
 
         id_values_list = self._unpack_sdp_data_element(complete_response)[0]
diff --git a/chameleond/utils/raspi_bluez_service.py b/chameleond/utils/raspi_bluez_service.py
index 5cf973b..fe3120c 100644
--- a/chameleond/utils/raspi_bluez_service.py
+++ b/chameleond/utils/raspi_bluez_service.py
@@ -277,7 +277,7 @@
     """Sends HID report across socket"""
 
     # Convert from dbus to native type for socket send
-    native_report = ''.join([chr(b) for b in report])
+    native_report = bytes(report)
     try:
       self._cinterrupt.send(native_report)
     except IOError as e: