1/******************************************************************************
2 *
3 *  Copyright (C) 2014 Google, Inc.
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19#include "base.h"
20#include "cases/cases.h"
21#include "support/callbacks.h"
22#include "support/hal.h"
23#include "support/pan.h"
24
25#define GRAY  "\x1b[0;37m"
26#define GREEN "\x1b[0;32m"
27#define RED   "\x1b[0;31m"
28
29const bt_interface_t *bt_interface;
30bt_bdaddr_t bt_remote_bdaddr;
31
32static bool parse_bdaddr(const char *str, bt_bdaddr_t *addr) {
33  if (!addr) {
34    return false;
35  }
36
37  int v[6];
38  if (sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]) != 6) {
39    return false;
40  }
41
42  for (int i = 0; i < 6; ++i) {
43    addr->address[i] = (uint8_t)v[i];
44  }
45
46  return true;
47}
48
49int main(int argc, char **argv) {
50  if (argc < 2 || !parse_bdaddr(argv[1], &bt_remote_bdaddr)) {
51    printf("Usage: %s <bdaddr>\n", argv[0]);
52    return -1;
53  }
54
55  if (!hal_open(callbacks_get_adapter_struct())) {
56    printf("Unable to open Bluetooth HAL.\n");
57    return 1;
58  }
59
60  if (!pan_init()) {
61    printf("Unable to initialize PAN.\n");
62    return 2;
63  }
64
65  int pass = 0;
66  int fail = 0;
67  int case_num = 0;
68
69  // Run through the sanity suite.
70  for (size_t i = 0; i < sanity_suite_size; ++i) {
71    callbacks_init();
72    if (sanity_suite[i].function()) {
73      printf("[%4d] %-64s [%sPASS%s]\n", ++case_num, sanity_suite[i].function_name, GREEN, GRAY);
74      ++pass;
75    } else {
76      printf("[%4d] %-64s [%sFAIL%s]\n", ++case_num, sanity_suite[i].function_name, RED, GRAY);
77      ++fail;
78    }
79    callbacks_cleanup();
80  }
81
82  // If there was a failure in the sanity suite, don't bother running the rest of the tests.
83  if (fail) {
84    printf("\n%sSanity suite failed with %d errors.%s\n", RED, fail, GRAY);
85    hal_close();
86    return 0;
87  }
88
89  // Run the full test suite.
90  for (size_t i = 0; i < test_suite_size; ++i) {
91    callbacks_init();
92    CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
93    if (test_suite[i].function()) {
94      printf("[%4d] %-64s [%sPASS%s]\n", ++case_num, test_suite[i].function_name, GREEN, GRAY);
95      ++pass;
96    } else {
97      printf("[%4d] %-64s [%sFAIL%s]\n", ++case_num, test_suite[i].function_name, RED, GRAY);
98      ++fail;
99    }
100    CALL_AND_WAIT(bt_interface->disable(), adapter_state_changed);
101    callbacks_cleanup();
102  }
103
104  printf("\n");
105
106  if (fail) {
107    printf("%d/%d tests failed. See above for failed test cases.\n", fail, test_suite_size);
108  } else {
109    printf("All tests passed!\n");
110  }
111
112  hal_close();
113  return 0;
114}
115