1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.settings.util.bluetooth; 18 19 import java.nio.ByteBuffer; 20 import java.util.UUID; 21 22 /** 23 * Class that contains helper functions and constants related to the communication protocol 24 * used between the controller and hub over bluetooth. 25 */ 26 public class PairingUtils { 27 28 public static final UUID[] UUIDs = new UUID[] { 29 UUID.fromString("578f077f-2bb0-474b-8473-c257d910a09b"), 30 UUID.fromString("c9e53035-20e2-4b09-822f-8fe4055063a6"), 31 UUID.fromString("ef845772-2a89-420e-9042-181f6134d6ef"), 32 UUID.fromString("67debdcf-ffd2-4f1e-a851-424b2a7f9178"), 33 UUID.fromString("a3330af0-4b14-46f1-8398-d074e7863e29") 34 }; 35 36 // Messages sent from the controller to the tungsten. 37 // ================================================================================ 38 39 // -->> (none) 40 // <<-- version 41 public static final int MSG_GET_PROTOCOL_VERSION = 110; 42 43 // -->> (n/a) 44 // <<-- Error message (length prefix string) 45 public static final int MSG_ERROR = 120; 46 47 // -->> Controller protocol version 48 // -->> Timezone (length prefix string) 49 // -->> Hub name 50 // <<-- Status bits 51 // <<-- Build fingerprint of the device (added in v7) 52 public static final int MSG_START_SETUP = 310; 53 54 // -->> (none) 55 // <<-- Error code 56 // <<-- Ethernet status (NETWORK_ constants) 57 // <<-- Wifi status (NETWORK_ constants) 58 // <<-- Wifi registration attempts (int) 59 // <<-- Wifi SSID (length prefixed string) 60 // <<-- IP address (length prefixed string) 61 // <<-- Wifi MAC address (length prefixed string) (add in v4) 62 public static final int MSG_GET_NETWORK_STATE = 320; 63 64 // -->> SSID (length prefix string) 65 // -->> Password (length prefix string) 66 // -->> Auth algorithm (encoded WifiConfiguration.AuthAlgorithm) 67 // -->> Key management policy (encoded WifiConfiguration.KeyMgmt) 68 // -->> The number of enterprise fields (int) (added in v9) 69 // -->> Series of enterprise field names and values (length prefixed strings) (added in v9) 70 // <<-- (same data as MSG_GET_NETWORK_STATE what=MSG_SET_WIFI_INFO) 71 public static final int MSG_SET_WIFI_INFO = 330; 72 73 // -->> Last ethernet status (NETWORK_ constants) 74 // -->> Last wifi status (NETWORK_ constants) 75 // -->> Last wifi attempt count 76 // -->> Wifi MAC address, if using wifi (added in v4) 77 // <<-- (same data as MSG_GET_NETWORK_STATE what=MSG_SET_WIFI_INFO) 78 // Waits until the wifi and / or ethernet status is not the same the input 79 // values, then returns. 80 public static final int MSG_WAIT_FOR_NET_CHANGE = 340; 81 82 // added in v7 83 // -->> Test to run (TEST_ constants) 84 // <<-- If the test is ready to run (STATE_TEST_ constants) 85 public static final int MSG_RUN_NETWORK_TEST = 343; 86 87 // added in v5 88 // -->> (none) 89 // <<-- placeId (length prefix string) 90 // <<-- hub serial (length prefix string) 91 // <<-- IP address of the hub 92 public static final int MSG_GET_HUB_IDENTIFIERS = 345; 93 94 // -->> Account ID (robot account, not user GAIA) 95 // -->> OAuth token to use 96 // -->> Service token 97 // -->> HubName 98 // <<-- 1 or 0 99 public static final int MSG_JOIN_CLOUDBRIDGE = 350; 100 101 // -->> Place ID 102 // -->> Name (length prefix string) 103 // -->> Owner cert (length prefix string, bas64 encoded ???) 104 // <<-- EndpointInfo with master address and port 105 public static final int MSG_CREATE_PLACE = 360; 106 107 // -->> (none) 108 // <<-- Tungsten's certificate (length prefixed) 109 // - Will wait for broker to finish 110 public static final int MSG_GET_CERTIFICATE = 370; 111 112 // -->> (none) 113 // <<-- OTA required (1 or 0) 114 // - Will wait for first checkin to happen 115 public static final int MSG_CHECK_FOR_OTA = 390; 116 117 // -->> (none) 118 // <<-- State (See OtaObserver for valid values) 119 // <<-- Progress (-1 - 100, or OtaObserver#NOT_SET) 120 // - Sent when waiting for OTA progress 121 public static final int MSG_OTA_PROGRESS = 392; 122 123 // -->> (none) 124 // <<-- 1 or 0 if the controller should wait for a reboot 125 // <<-- Serial number of the device 126 // <<-- the IP address of the device 127 public static final int MSG_WAIT_FOR_FIRST_RUN = 400; 128 129 // added in v10 130 // -->> One of TEST_MODE_* constants indicating the role of the hub (int) 131 // -->> IP address of controller, if the hub is running in client mode, otherwise omitted 132 // (length prefixed string) 133 // <<-- (none) 134 public static final int MSG_RUN_UNICAST_CONNECTION_TEST = 800; 135 136 // added in v10 137 // -->> One of TEST_MODE_* constants indicating the role of the hub (int) 138 // -->> IP address of controller, if the hub is running in client mode, otherwise omitted 139 // (length prefixed string) 140 // <<-- (none) 141 public static final int MSG_RUN_BANDWIDTH_TEST = 801; 142 143 // added in v10 144 // -->> One of TEST_MODE_* constants indicating the role of the hub (int) 145 // -->> IP address of controller, if the hub is running in client mode, otherwise omitted 146 // (length prefixed string) 147 // <<-- (none) 148 public static final int MSG_RUN_PACKET_LOSS_TEST = 802; 149 150 // The most recent protocol version. 151 // ================================================================================ 152 public static final int PROTOCOL_VERSION = 12; 153 154 // Errors that the networking can return 155 public static final int ERROR_NONE = 0; 156 public static final int ERROR_UNKNOWN = 1; 157 public static final int ERROR_ADDING_NETWORK = 2; 158 public static final int ERROR_AUTH_FAILED = 3; 159 public static final int ERROR_SETUP_RESET = 4; 160 public static final int ERROR_JOIN_CLOUDBRIDGE = 5; 161 public static final int ERROR_NETWORK_DISABLED = 6; 162 163 // Wifi status codes (from android.net.NetworkInfo.DetailedState) 164 // ================================================================================ 165 public static final int NETWORK_AUTHENTICATING = 1; 166 public static final int NETWORK_BLOCKED = 2; 167 public static final int NETWORK_CONNECTED = 3; 168 public static final int NETWORK_CONNECTING = 4; 169 public static final int NETWORK_DISCONNECTED = 5; 170 public static final int NETWORK_DISCONNECTING = 6; 171 public static final int NETWORK_FAILED = 7; 172 public static final int NETWORK_IDLE = 8; 173 public static final int NETWORK_OBTAINING_IPADDR = 9; 174 public static final int NETWORK_SCANNING = 10; 175 public static final int NETWORK_SUSPENDED = 11; 176 // the network is connected, but we have no ip address 177 public static final int NETWORK_NO_IP = 12; 178 179 // Test codes 180 // ================================================================================ 181 public static final int TEST_UNICAST = 1; 182 public static final int TEST_LAN_BANDWIDTH = 2; 183 public static final int TEST_PACKET_LOSS = 3; 184 185 public static final int STATE_TEST_STARTUP_FAILED = 0; 186 public static final int STATE_TEST_READY = 1; 187 188 // send a heartbeat every 10 seconds 189 public static final int TIMEOUT_IP_WRITE = 10 * 1000; 190 // set the read timeout with plenty of buffer in case the write is delayed for some reason 191 public static final int TIMEOUT_IP_READ = TIMEOUT_IP_WRITE * 25 / 10; 192 193 public static String getString(ByteBuffer msg) { 194 final int len = msg.getInt(); 195 final byte[] buf = msg.array(); 196 final int pos = msg.position(); 197 msg.position(pos + len); 198 return new String(buf, pos, len); 199 } 200 201 /** 202 * Append the length of the given string as an integer into the ByteBuffer followed by the 203 * string itself. 204 * @param buffer ByteBuffer to add this string to 205 * @param toAdd The string to add 206 * @return The same buffer that was passed in, just returned for convenience. 207 */ 208 public static ByteBuffer putString(ByteBuffer buffer, String toAdd) { 209 buffer.putInt(toAdd.length()); 210 buffer.put(toAdd.getBytes()); 211 return buffer; 212 } 213 214 public static byte[] getByteArray(ByteBuffer msg) { 215 final int len = msg.getInt(); 216 final byte[] result = new byte[len]; 217 System.arraycopy(msg.array(), msg.position(), result, 0, len); 218 final int pos = msg.position(); 219 msg.position(pos + len); 220 return result; 221 } 222 223 // Constants encoding the setup needs of the device 224 // bit mask to do place setup 225 public static final int TASK_SETUP_PLACE = 1; 226 227 // bit mask to do network setup 228 public static final int TASK_SETUP_NETWORK = 2; 229 230 // bit mask to do network setup (if needed), run network tests, and terminate. Generally 231 // speaking it should always be possible to run tests 232 public static final int TASK_TEST_NETWORK = 4; 233 234 // The hub should run as the server for this test, the meaning of "server" is context sensitive 235 public static final int TEST_MODE_SERVER = 0; 236 237 // The hub should run as the client for this test, the meaning of "client" is context sensitive 238 public static final int TEST_MODE_CLIENT = 1; 239 240 } 241