1/*
2 * Copyright (C) 2017 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#include <base/logging.h>
17
18#include "mca_defs.h"
19#include "mcap_test_mdl.h"
20
21namespace SYSTEM_BT_TOOLS_MCAP_TOOL {
22
23/* Test MCAP Channel Configurations */
24const tMCA_CHNL_CFG MCAP_TEST_CHANNEL_CONFIG = {
25    .fcr_opt =
26        {
27            L2CAP_FCR_ERTM_MODE,
28            MCA_FCR_OPT_TX_WINDOW_SIZE, /* Tx window size */
29            /* Maximum transmissions before disconnecting */
30            MCA_FCR_OPT_MAX_TX_B4_DISCNT,
31            MCA_FCR_OPT_RETX_TOUT,    /* retransmission timeout (2 secs) */
32            MCA_FCR_OPT_MONITOR_TOUT, /* Monitor timeout (12 secs) */
33            MCA_FCR_OPT_MPS_SIZE,     /* MPS segment size */
34        },
35    .user_rx_buf_size = BT_DEFAULT_BUFFER_SIZE,
36    .user_tx_buf_size = BT_DEFAULT_BUFFER_SIZE,
37    .fcr_rx_buf_size = BT_DEFAULT_BUFFER_SIZE,
38    .fcr_tx_buf_size = BT_DEFAULT_BUFFER_SIZE,
39    .fcs = MCA_FCS_NONE,
40    .data_mtu = 572 /* L2CAP MTU of the MCAP data channel */
41};
42
43const tMCA_CHNL_CFG* get_test_channel_config() {
44  return &MCAP_TEST_CHANNEL_CONFIG;
45}
46
47McapMdl::McapMdl(btmcap_test_interface_t* mcap_test_interface,
48                 tMCA_CL mcl_handle, tMCA_DEP mdep_handle, uint16_t mdl_id,
49                 uint8_t dep_id, uint8_t cfg) {
50  _mcap_test_interface = mcap_test_interface;
51  _mcl_handle = mcl_handle;
52  _mdep_handle = mdep_handle;
53  _mdl_id = mdl_id;
54  _dep_id = dep_id;
55  _cfg = cfg;
56}
57
58bool McapMdl::UpdateContext(tMCA_DEP mdep_handle, uint8_t dep_id, uint8_t cfg) {
59  if (!_mdl_handle) {
60    LOG(ERROR) << "MDL handle not initialized";
61  }
62  _mdep_handle = mdep_handle;
63  _dep_id = dep_id;
64  _cfg = cfg;
65  tMCA_RESULT ret = _mcap_test_interface->close_mdl_request(_mdl_handle);
66  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
67  if (ret != MCA_SUCCESS) return false;
68  SetHandle(0);
69  SetResponseCode(-1);
70  SetMtu(0);
71  return true;
72}
73
74bool McapMdl::Create(uint16_t data_psm, bool should_connect) {
75  tMCA_RESULT ret = _mcap_test_interface->create_mdl_request(
76      _mcl_handle, _mdep_handle, data_psm, _mdl_id, _dep_id, _cfg,
77      should_connect ? &MCAP_TEST_CHANNEL_CONFIG : nullptr);
78  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
79  return ret == MCA_SUCCESS;
80}
81
82bool McapMdl::Close() {
83  if (!_mdl_handle) {
84    LOG(ERROR) << "MDL handle not initialized";
85    return false;
86  }
87  tMCA_RESULT ret = _mcap_test_interface->close_mdl_request(_mdl_handle);
88  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
89  return ret == MCA_SUCCESS;
90}
91
92bool McapMdl::Reconnect(uint16_t data_psm) {
93  tMCA_RESULT ret = _mcap_test_interface->reconnect_mdl_request(
94      _mcl_handle, _mdep_handle, data_psm, _mdl_id, &MCAP_TEST_CHANNEL_CONFIG);
95  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
96  return ret == MCA_SUCCESS;
97}
98
99bool McapMdl::ReconnectResponse() {
100  tMCA_RESULT ret = _mcap_test_interface->reconnect_mdl_response(
101      _mcl_handle, _mdep_handle, _mdl_id, MCA_RSP_SUCCESS,
102      &MCAP_TEST_CHANNEL_CONFIG);
103  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
104  return ret == MCA_SUCCESS;
105}
106
107bool McapMdl::CreateResponse() {
108  tMCA_RESULT ret = _mcap_test_interface->create_mdl_response(
109      _mcl_handle, _dep_id, _mdl_id, _cfg, MCA_SUCCESS,
110      &MCAP_TEST_CHANNEL_CONFIG);
111  LOG_IF(INFO, ret != MCA_SUCCESS) << "ret=" << (int)ret;
112  return ret == MCA_SUCCESS;
113}
114
115bool McapMdl::IsConnected() { return _mdl_handle > 0; }
116
117uint16_t McapMdl::GetId() { return _mdl_id; }
118
119int32_t McapMdl::GetResponseCode() { return _mdl_rsp_code; }
120
121void McapMdl::SetResponseCode(int32_t rsp_code) { _mdl_rsp_code = rsp_code; }
122
123void McapMdl::SetHandle(tMCA_DL mdl_handle) { _mdl_handle = mdl_handle; }
124
125tMCA_DL McapMdl::GetHandle() { return _mdl_handle; }
126
127void McapMdl::SetMtu(uint16_t mtu) { _data_mtu = mtu; }
128
129uint16_t McapMdl::GetMtu() { return _data_mtu; }
130
131}  // namespace SYSTEM_BT_TOOLS_MCAP_TOOL
132