Home | History | Annotate | Download | only in bluetooth
      1 /*
      2  * Copyright 2018 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.cts.verifier.bluetooth;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 import com.android.cts.verifier.PassFailButtons;
     23 import com.android.cts.verifier.R;
     24 
     25 import android.app.AlertDialog;
     26 import android.app.Dialog;
     27 import android.content.BroadcastReceiver;
     28 import android.content.Context;
     29 import android.content.DialogInterface;
     30 import android.content.Intent;
     31 import android.content.IntentFilter;
     32 import android.os.Bundle;
     33 import android.util.Log;
     34 import android.widget.ListView;
     35 import android.widget.Toast;
     36 
     37 public class BleCocServerTestBaseActivity extends PassFailButtons.Activity {
     38 
     39     public static final boolean DEBUG = true;
     40     public static final String TAG = "BleCocServerTestBaseActivity";
     41 
     42     private final int TEST_BLE_LE_CONNECTED = 0;
     43     private final int TEST_BLE_LISTENER_CREATED = 1;
     44     private final int TEST_BLE_PSM_READ = 2;
     45     private final int TEST_BLE_COC_CONNECTED = 3;
     46     private final int TEST_BLE_CONNECTION_TYPE_CHECKED = 4;
     47     private final int TEST_BLE_DATA_8BYTES_READ = 5;
     48     private final int TEST_BLE_DATA_8BYTES_SENT = 6;
     49     private final int TEST_BLE_DATA_EXCHANGED = 7;
     50     private final int TEST_BLE_SERVER_DISCONNECTED = 8;
     51     private static final int PASS_FLAG_ALL = 0x01FF;
     52 
     53     private TestAdapter mTestAdapter;
     54     private long mPassed;
     55 
     56     @Override
     57     public void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59         setContentView(R.layout.ble_server_start);
     60         setPassFailButtonClickListeners();
     61         setInfoResources(R.string.ble_coc_server_start_name,
     62                          R.string.ble_server_start_info, -1);
     63         getPassButton().setEnabled(false);
     64 
     65         mTestAdapter = new TestAdapter(this, setupTestList());
     66         ListView listView = (ListView) findViewById(R.id.ble_server_tests);
     67         listView.setAdapter(mTestAdapter);
     68 
     69         mPassed = 0;
     70     }
     71 
     72     @Override
     73     public void onResume() {
     74         super.onResume();
     75 
     76         IntentFilter filter = new IntentFilter();
     77 
     78         filter.addAction(BleCocServerService.BLE_LE_CONNECTED);
     79         filter.addAction(BleCocServerService.BLE_COC_LISTENER_CREATED);
     80         filter.addAction(BleCocServerService.BLE_PSM_READ);
     81         filter.addAction(BleCocServerService.BLE_COC_CONNECTED);
     82         filter.addAction(BleCocServerService.BLE_CONNECTION_TYPE_CHECKED);
     83         filter.addAction(BleCocServerService.BLE_DATA_8BYTES_READ);
     84         filter.addAction(BleCocServerService.BLE_DATA_8BYTES_SENT);
     85         filter.addAction(BleCocServerService.BLE_DATA_LARGEBUF_READ);
     86 
     87         filter.addAction(BleCocServerService.BLE_BLUETOOTH_MISMATCH_SECURE);
     88         filter.addAction(BleCocServerService.BLE_BLUETOOTH_MISMATCH_INSECURE);
     89         filter.addAction(BleCocServerService.BLE_SERVER_DISCONNECTED);
     90 
     91         filter.addAction(BleCocServerService.BLE_BLUETOOTH_DISABLED);
     92         filter.addAction(BleCocServerService.BLE_OPEN_FAIL);
     93         filter.addAction(BleCocServerService.BLE_ADVERTISE_UNSUPPORTED);
     94         filter.addAction(BleCocServerService.BLE_ADD_SERVICE_FAIL);
     95 
     96         registerReceiver(mBroadcast, filter);
     97     }
     98 
     99     @Override
    100     public void onPause() {
    101         super.onPause();
    102         unregisterReceiver(mBroadcast);
    103     }
    104 
    105     @Override
    106     public void onDestroy() {
    107         super.onDestroy();
    108     }
    109 
    110     private List<Integer> setupTestList() {
    111         ArrayList<Integer> testList = new ArrayList<Integer>();
    112         testList.add(R.string.ble_coc_server_le_connect);
    113         testList.add(R.string.ble_coc_server_create_listener);
    114         testList.add(R.string.ble_coc_server_psm_read);
    115         testList.add(R.string.ble_coc_server_connection);
    116         testList.add(R.string.ble_coc_server_check_connection_type);
    117         testList.add(R.string.ble_coc_server_receive_data_8bytes);
    118         testList.add(R.string.ble_coc_server_send_data_8bytes);
    119         testList.add(R.string.ble_coc_server_data_exchange);
    120         testList.add(R.string.ble_server_receiving_disconnect);
    121         return testList;
    122     }
    123 
    124     private void showErrorDialog(int titleId, int messageId, boolean finish) {
    125         AlertDialog.Builder builder = new AlertDialog.Builder(this)
    126                 .setTitle(titleId)
    127                 .setMessage(messageId);
    128         if (finish) {
    129             builder.setOnCancelListener(new Dialog.OnCancelListener() {
    130                 @Override
    131                 public void onCancel(DialogInterface dialog) {
    132                     finish();
    133                 }
    134             });
    135         }
    136         builder.create().show();
    137     }
    138 
    139     private BroadcastReceiver mBroadcast = new BroadcastReceiver() {
    140         @Override
    141         public void onReceive(Context context, Intent intent) {
    142             String action = intent.getAction();
    143             if (DEBUG) {
    144                 Log.d(TAG, "BroadcastReceiver.onReceive: action=" + action);
    145             }
    146             String newAction = null;
    147             final Intent startIntent = new Intent(BleCocServerTestBaseActivity.this, BleCocServerService.class);
    148 
    149             switch (action) {
    150             case BleCocServerService.BLE_BLUETOOTH_DISABLED:
    151                 showErrorDialog(R.string.ble_bluetooth_disable_title, R.string.ble_bluetooth_disable_message, true);
    152                 break;
    153             case BleCocServerService.BLE_LE_CONNECTED:
    154                 mTestAdapter.setTestPass(TEST_BLE_LE_CONNECTED);
    155                 mPassed |= (1 << TEST_BLE_LE_CONNECTED);
    156                 break;
    157             case BleCocServerService.BLE_COC_LISTENER_CREATED:
    158                 mTestAdapter.setTestPass(TEST_BLE_LISTENER_CREATED);
    159                 mPassed |= (1 << TEST_BLE_LISTENER_CREATED);
    160                 break;
    161             case BleCocServerService.BLE_PSM_READ:
    162                 mTestAdapter.setTestPass(TEST_BLE_PSM_READ);
    163                 mPassed |= (1 << TEST_BLE_PSM_READ);
    164                 break;
    165             case BleCocServerService.BLE_COC_CONNECTED:
    166                 mTestAdapter.setTestPass(TEST_BLE_COC_CONNECTED);
    167                 mPassed |= (1 << TEST_BLE_COC_CONNECTED);
    168                 break;
    169             case BleCocServerService.BLE_CONNECTION_TYPE_CHECKED:
    170                 mTestAdapter.setTestPass(TEST_BLE_CONNECTION_TYPE_CHECKED);
    171                 mPassed |= (1 << TEST_BLE_CONNECTION_TYPE_CHECKED);
    172                 break;
    173             case BleCocServerService.BLE_DATA_8BYTES_READ:
    174                 mTestAdapter.setTestPass(TEST_BLE_DATA_8BYTES_READ);
    175                 mPassed |= (1 << TEST_BLE_DATA_8BYTES_READ);
    176                 // send the next action to send 8 bytes
    177                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_SEND_DATA_8BYTES;
    178                 break;
    179             case BleCocServerService.BLE_DATA_8BYTES_SENT:
    180                 mTestAdapter.setTestPass(TEST_BLE_DATA_8BYTES_SENT);
    181                 mPassed |= (1 << TEST_BLE_DATA_8BYTES_SENT);
    182                 // send the next action to send 8 bytes
    183                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_EXCHANGE_DATA;
    184                 break;
    185             case BleCocServerService.BLE_DATA_LARGEBUF_READ:
    186                 mTestAdapter.setTestPass(TEST_BLE_DATA_EXCHANGED);
    187                 mPassed |= (1 << TEST_BLE_DATA_EXCHANGED);
    188                 // Disconnect
    189                 newAction = BleCocServerService.BLE_COC_SERVER_ACTION_DISCONNECT;
    190                 break;
    191             case BleCocServerService.BLE_SERVER_DISCONNECTED:
    192                 mTestAdapter.setTestPass(TEST_BLE_SERVER_DISCONNECTED);
    193                 mPassed |= (1 << TEST_BLE_SERVER_DISCONNECTED);
    194                 // all tests done
    195                 break;
    196             case BleCocServerService.BLE_BLUETOOTH_MISMATCH_SECURE:
    197                 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_secure_message, true);
    198                 break;
    199             case BleCocServerService.BLE_BLUETOOTH_MISMATCH_INSECURE:
    200                 showErrorDialog(R.string.ble_bluetooth_mismatch_title, R.string.ble_bluetooth_mismatch_insecure_message, true);
    201                 break;
    202             case BleCocServerService.BLE_ADVERTISE_UNSUPPORTED:
    203                 showErrorDialog(R.string.bt_advertise_unsupported_title, R.string.bt_advertise_unsupported_message, true);
    204                 break;
    205             case BleCocServerService.BLE_OPEN_FAIL:
    206                 setTestResultAndFinish(false);
    207                 runOnUiThread(new Runnable() {
    208                     @Override
    209                     public void run() {
    210                         Toast.makeText(BleCocServerTestBaseActivity.this, R.string.bt_open_failed_message, Toast.LENGTH_SHORT).show();
    211                     }
    212                 });
    213                 break;
    214             case BleCocServerService.BLE_ADD_SERVICE_FAIL:
    215                 showErrorDialog(R.string.bt_add_service_failed_title, R.string.bt_add_service_failed_message, true);
    216                 break;
    217             default:
    218                 if (DEBUG) {
    219                     Log.d(TAG, "Note: BroadcastReceiver.onReceive: unhandled action=" + action);
    220                 }
    221             }
    222 
    223             mTestAdapter.notifyDataSetChanged();
    224 
    225             if (newAction != null) {
    226                 Log.d(TAG, "Starting " + newAction);
    227                 startIntent.setAction(newAction);
    228 
    229                 startService(startIntent);
    230             }
    231 
    232             if (mPassed == PASS_FLAG_ALL) {
    233                 Log.d(TAG, "All Tests Passed.");
    234                 getPassButton().setEnabled(true);
    235             }
    236         }
    237     };
    238 }
    239