Home | History | Annotate | Download | only in gatt
      1 /*
      2  * Copyright (C) 2013 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.bluetooth.gatt;
     18 
     19 import android.content.Intent;
     20 import android.os.Bundle;
     21 import android.util.Log;
     22 import java.util.UUID;
     23 
     24 /**
     25  * Helper class containing useful tools for GATT service debugging.
     26  */
     27 /*package*/ class GattDebugUtils {
     28     private static final String TAG = GattServiceConfig.TAG_PREFIX + "DebugUtils";
     29     private static final boolean DEBUG_ADMIN = GattServiceConfig.DEBUG_ADMIN;
     30 
     31     private static final String ACTION_DEBUG_DUMP_CLIENTMAP =
     32                                 "android.bluetooth.action.DEBUG_DUMP_CLIENTMAP";
     33     private static final String ACTION_DEBUG_DUMP_SERVERMAP =
     34                                 "android.bluetooth.action.DEBUG_DUMP_SERVERMAP";
     35     private static final String ACTION_DEBUG_DUMP_HANDLEMAP =
     36                                 "android.bluetooth.action.DEBUG_DUMP_HANDLEMAP";
     37 
     38     private static final String ACTION_GATT_PAIRING_CONFIG =
     39                                 "android.bluetooth.action.GATT_PAIRING_CONFIG";
     40 
     41     private static final String ACTION_GATT_TEST_USAGE =
     42                                 "android.bluetooth.action.GATT_TEST_USAGE";
     43     private static final String ACTION_GATT_TEST_ENABLE =
     44                                 "android.bluetooth.action.GATT_TEST_ENABLE";
     45     private static final String ACTION_GATT_TEST_CONNECT =
     46                                 "android.bluetooth.action.GATT_TEST_CONNECT";
     47     private static final String ACTION_GATT_TEST_DISCONNECT =
     48                                 "android.bluetooth.action.GATT_TEST_DISCONNECT";
     49     private static final String ACTION_GATT_TEST_DISCOVER =
     50                                 "android.bluetooth.action.GATT_TEST_DISCOVER";
     51 
     52     private static final String EXTRA_ENABLE = "enable";
     53     private static final String EXTRA_ADDRESS = "address";
     54     private static final String EXTRA_UUID = "uuid";
     55     private static final String EXTRA_TYPE = "type";
     56     private static final String EXTRA_SHANDLE = "start";
     57     private static final String EXTRA_EHANDLE = "end";
     58     private static final String EXTRA_AUTH_REQ = "auth_req";
     59     private static final String EXTRA_IO_CAP = "io_cap";
     60     private static final String EXTRA_INIT_KEY = "init_key";
     61     private static final String EXTRA_RESP_KEY = "resp_key";
     62     private static final String EXTRA_MAX_KEY = "max_key";
     63 
     64     /**
     65      * Handles intents passed in via GattService.onStartCommand().
     66      * This allows sending debug actions to the running service.
     67      * To trigger a debug action, invoke the following shell command:
     68      *
     69      *   adb shell am startservice -a <action> <component>
     70      *
     71      * Where <action> represents one of the ACTION_* constants defines above
     72      * and  <component> identifies the GATT service.
     73      *
     74      * For example:
     75      *   import com.android.bluetooth.gatt.GattService;
     76      */
     77     static boolean handleDebugAction(GattService svc, Intent intent) {
     78         if (!DEBUG_ADMIN) return false;
     79 
     80         String action = intent.getAction();
     81         Log.d(TAG, "handleDebugAction() action=" + action);
     82 
     83         /*
     84          * Debug log functinos
     85          */
     86 
     87         if (ACTION_DEBUG_DUMP_CLIENTMAP.equals(action)) {
     88             svc.mClientMap.dump();
     89 
     90         } else if (ACTION_DEBUG_DUMP_SERVERMAP.equals(action)) {
     91             svc.mServerMap.dump();
     92 
     93         } else if (ACTION_DEBUG_DUMP_HANDLEMAP.equals(action)) {
     94             svc.mHandleMap.dump();
     95 
     96         /*
     97          * PTS test commands
     98          */
     99 
    100         } else if (ACTION_GATT_TEST_USAGE.equals(action)) {
    101             logUsageInfo();
    102 
    103         } else if (ACTION_GATT_TEST_ENABLE.equals(action)) {
    104             boolean bEnable = intent.getBooleanExtra(EXTRA_ENABLE, true);
    105             svc.gattTestCommand( 0x01, null,null, bEnable ? 1 : 0, 0,0,0,0);
    106 
    107         } else if (ACTION_GATT_TEST_CONNECT.equals(action)) {
    108             String address = intent.getStringExtra(EXTRA_ADDRESS);
    109             int type = intent.getIntExtra(EXTRA_TYPE, 2 /* LE device */);
    110             svc.gattTestCommand( 0x02, null, address, type, 0,0,0,0);
    111 
    112         } else if (ACTION_GATT_TEST_DISCONNECT.equals(action)) {
    113             svc.gattTestCommand( 0x03, null, null, 0,0,0,0,0);
    114 
    115         } else if (ACTION_GATT_TEST_DISCOVER.equals(action)) {
    116             UUID uuid = getUuidExtra(intent);
    117             int type = intent.getIntExtra(EXTRA_TYPE, 1 /* All services */);
    118             int shdl = getHandleExtra(intent, EXTRA_SHANDLE, 1);
    119             int ehdl = getHandleExtra(intent, EXTRA_EHANDLE, 0xFFFF);
    120             svc.gattTestCommand( 0x04, uuid, null, type, shdl, ehdl, 0,0);
    121 
    122         } else if (ACTION_GATT_PAIRING_CONFIG.equals(action)) {
    123             int auth_req = intent.getIntExtra(EXTRA_AUTH_REQ, 5);
    124             int io_cap = intent.getIntExtra(EXTRA_IO_CAP, 4);
    125             int init_key = intent.getIntExtra(EXTRA_INIT_KEY, 7);
    126             int resp_key = intent.getIntExtra(EXTRA_RESP_KEY, 7);
    127             int max_key = intent.getIntExtra(EXTRA_MAX_KEY, 16);
    128             svc.gattTestCommand( 0xF0, null, null, auth_req, io_cap, init_key,
    129                                  resp_key, max_key);
    130 
    131         } else {
    132             return false;
    133         }
    134 
    135         return true;
    136     }
    137 
    138     /**
    139      * Attempts to retrieve an extra from an intent first as hex value,
    140      * then as an ineger.
    141      * @hide
    142      */
    143     static private int getHandleExtra(Intent intent, String extra, int default_value) {
    144         Bundle extras = intent.getExtras();
    145         Object uuid = extras != null ? extras.get(extra) : null;
    146         if (uuid != null && uuid.getClass().getName().equals("java.lang.String")) {
    147             try
    148             {
    149                 return Integer.parseInt(extras.getString(extra), 16);
    150             } catch (NumberFormatException e) {
    151                 return default_value;
    152             }
    153         }
    154 
    155         return intent.getIntExtra(extra, default_value);
    156     }
    157 
    158     /**
    159      * Retrieves the EXTRA_UUID parameter.
    160      * If a string of length 4 is detected, a 16-bit hex UUID is assumed and
    161      * the default Bluetooth UUID is appended.
    162      * @hide
    163      */
    164     static private UUID getUuidExtra(Intent intent) {
    165         String uuidStr = intent.getStringExtra(EXTRA_UUID);
    166         if (uuidStr != null && uuidStr.length() == 4) {
    167             uuidStr = String.format("0000%s-0000-1000-8000-00805f9b34fb", uuidStr);
    168         }
    169         return (uuidStr != null) ? UUID.fromString(uuidStr) : null;
    170     }
    171 
    172     /**
    173      * Log usage information.
    174      * @hide
    175      */
    176     static private void logUsageInfo() {
    177         StringBuilder b = new StringBuilder();
    178         b.append(  "------------ GATT TEST ACTIONS  ----------------");
    179         b.append("\nGATT_TEST_ENABLE");
    180         b.append("\n  [--ez enable <bool>] Enable or disable,");
    181         b.append("\n                       defaults to true (enable).\n");
    182         b.append("\nGATT_TEST_CONNECT");
    183         b.append("\n   --es address <bda>");
    184         b.append("\n  [--ei type <type>]   Default is 2 (LE Only)\n");
    185         b.append("\nGATT_TEST_DISCONNECT\n");
    186         b.append("\nGATT_TEST_DISCOVER");
    187         b.append("\n  [--ei type <type>]   Possible values:");
    188         b.append("\n                         1 = Discover all services (default)");
    189         b.append("\n                         2 = Discover services by UUID");
    190         b.append("\n                         3 = Discover included services");
    191         b.append("\n                         4 = Discover characteristics");
    192         b.append("\n                         5 = Discover descriptors\n");
    193         b.append("\n  [--es uuid <uuid>]   Optional; Can be either full 128-bit");
    194         b.append("\n                       UUID hex string, or 4 hex characters");
    195         b.append("\n                       for 16-bit UUIDs.\n");
    196         b.append("\n  [--ei start <hdl>]   Start of handle range (default 1)");
    197         b.append("\n  [--ei end <hdl>]     End of handle range (default 65355)");
    198         b.append("\n    or");
    199         b.append("\n  [--es start <hdl>]   Start of handle range (hex format)");
    200         b.append("\n  [--es end <hdl>]     End of handle range (hex format)\n");
    201         b.append("\nGATT_PAIRING_CONFIG");
    202         b.append("\n  [--ei auth_req]      Authentication flag (default 5)");
    203         b.append("\n  [--ei io_cap]        IO capabilities (default 4)");
    204         b.append("\n  [--ei init_key]      Initial key size (default 7)");
    205         b.append("\n  [--ei resp_key]      Response key size (default 7)");
    206         b.append("\n  [--ei max_key]       Maximum key size (default 16)");
    207         b.append("\n------------------------------------------------");
    208         Log.i(TAG, b.toString());
    209     }
    210 }
    211