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 "support/callbacks.h"
21#include "support/pan.h"
22
23static const int local_role = BTPAN_ROLE_PANU;
24static const int remote_role = BTPAN_ROLE_PANNAP;
25
26bool pan_enable() {
27  int error;
28
29  // PAN is enabled by default, wait for the first control state change
30  // with default parameters set. We don't want to verify the result since
31  // the implementation could have set any parameters.
32  WAIT(pan_control_state_changed);
33
34  // Call enable explicitly and verify that the parameters match what we just set.
35  CALL_AND_WAIT(error = pan_interface->enable(local_role), pan_control_state_changed);
36  TASSERT(error == BT_STATUS_SUCCESS, "Error enabling PAN: %d", error);
37  TASSERT(pan_get_control_state() == BTPAN_STATE_ENABLED, "Control state is disabled.");
38  TASSERT(pan_get_local_role() == local_role, "Unexpected local role: %d", pan_get_local_role());
39  TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error in control callback: %d", pan_get_error());
40
41  return true;
42}
43
44bool pan_connect() {
45  int error;
46
47  if (!pan_enable()) {
48    return false;
49  }
50
51  CALL_AND_WAIT(error = pan_interface->connect(&bt_remote_bdaddr, local_role, remote_role), pan_connection_state_changed);
52  TASSERT(error == BT_STATUS_SUCCESS, "Error connecting to remote host: %d", error);
53  TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error connecting to BT device: %d", pan_get_error());
54  TASSERT(pan_get_connection_state() == BTPAN_STATE_CONNECTING, "Invalid PAN state after connect: %d", pan_get_connection_state());
55  TASSERT(pan_get_local_role() == local_role, "Incorrect local role: %d", pan_get_local_role());
56  TASSERT(pan_get_remote_role() == remote_role, "Incorrect remote role: %d", pan_get_remote_role());
57
58  WAIT(pan_connection_state_changed);
59  TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error connecting to BT device: %d", pan_get_error());
60  TASSERT(pan_get_connection_state() == BTPAN_STATE_CONNECTED, "Invalid PAN state after connect: %d", pan_get_connection_state());
61  TASSERT(pan_get_local_role() == local_role, "Incorrect local role: %d", pan_get_local_role());
62  TASSERT(pan_get_remote_role() == remote_role, "Incorrect remote role: %d", pan_get_remote_role());
63
64  return true;
65}
66
67bool pan_disconnect() {
68  int error;
69
70  if (!pan_connect()) {
71    return false;
72  }
73
74  CALL_AND_WAIT(error = pan_interface->disconnect(&bt_remote_bdaddr), pan_connection_state_changed);
75  TASSERT(error == BT_STATUS_SUCCESS, "Error disconnecting from remote host: %d", error);
76  TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error disconnecting from BT device: %d", pan_get_error());
77  TASSERT(pan_get_connection_state() == BTPAN_STATE_DISCONNECTING, "Invalid PAN state after disconnect: %d", pan_get_connection_state());
78
79  WAIT(pan_connection_state_changed);
80  TASSERT(pan_get_error() == BT_STATUS_SUCCESS, "Error disconnecting from BT device: %d", pan_get_error());
81  TASSERT(pan_get_connection_state() == BTPAN_STATE_DISCONNECTED, "Invalid PAN state after disconnect: %d", pan_get_connection_state());
82
83  return true;
84}
85