blob: 2c0dc0b12b8eae478874281edaefa52cebcf9b12 [file] [log] [blame]
nxpandroidc7611652015-09-23 16:42:05 +05301/******************************************************************************
2 *
3 * Copyright (C) 2010-2014 Broadcom Corporation
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
nxpandroidc7611652015-09-23 16:42:05 +053019/******************************************************************************
20 *
21 * NFA interface to LLCP
22 *
23 ******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +053024#include <android-base/stringprintf.h>
25#include <base/logging.h>
26
nxpandroidc7611652015-09-23 16:42:05 +053027#include "nfa_p2p_api.h"
28#include "nfa_p2p_int.h"
29
nxf24591c1cbeab2018-02-21 17:32:26 +053030using android::base::StringPrintf;
31
32extern bool nfc_debug_enabled;
33
nxpandroidc7611652015-09-23 16:42:05 +053034/*****************************************************************************
35** Constants
36*****************************************************************************/
37
38/*******************************************************************************
39**
40** Function NFA_P2pRegisterServer
41**
nxpandroid8f6d0532017-07-12 18:25:30 +053042** Description This function is called to listen to a SAP as server on
43** LLCP.
nxpandroidc7611652015-09-23 16:42:05 +053044**
nxpandroid8f6d0532017-07-12 18:25:30 +053045** NFA_P2P_REG_SERVER_EVT will be returned with status and
46** handle.
nxpandroidc7611652015-09-23 16:42:05 +053047**
nxpandroid8f6d0532017-07-12 18:25:30 +053048** If server_sap is set to NFA_P2P_ANY_SAP, then NFA will
49** allocate a SAP between LLCP_LOWER_BOUND_SDP_SAP and
50** LLCP_UPPER_BOUND_SDP_SAP Otherwise, server_sap must be
51** between (LLCP_SDP_SAP + 1) and LLCP_UPPER_BOUND_SDP_SAP
nxpandroidc7611652015-09-23 16:42:05 +053052**
53** link_type : NFA_P2P_LLINK_TYPE and/or NFA_P2P_DLINK_TYPE
54**
nxpandroid8f6d0532017-07-12 18:25:30 +053055** Note: If RF discovery is started,
56** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
57** happen before calling this function
nxpandroidc7611652015-09-23 16:42:05 +053058**
59** Returns NFA_STATUS_OK if successfully initiated
60** NFA_STATUS_FAILED otherwise
61**
62*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053063tNFA_STATUS NFA_P2pRegisterServer(uint8_t server_sap,
64 tNFA_P2P_LINK_TYPE link_type,
65 char* p_service_name,
66 tNFA_P2P_CBACK* p_cback) {
67 tNFA_P2P_API_REG_SERVER* p_msg;
nxpandroidc7611652015-09-23 16:42:05 +053068
nxf24591c1cbeab2018-02-21 17:32:26 +053069 DLOG_IF(INFO, nfc_debug_enabled)
70 << StringPrintf("server_sap:0x%02x, link_type:0x%x, SN:<%s>", server_sap,
71 link_type, p_service_name);
nxpandroidc7611652015-09-23 16:42:05 +053072
nxpandroid8f6d0532017-07-12 18:25:30 +053073 if ((server_sap != NFA_P2P_ANY_SAP) &&
74 ((server_sap <= LLCP_SAP_SDP) ||
75 (server_sap > LLCP_UPPER_BOUND_SDP_SAP))) {
nxf24591c1cbeab2018-02-21 17:32:26 +053076 LOG(ERROR) << StringPrintf("server_sap must be between %d and %d",
77 LLCP_SAP_SDP + 1, LLCP_UPPER_BOUND_SDP_SAP);
nxpandroidc7611652015-09-23 16:42:05 +053078 return (NFA_STATUS_FAILED);
nxpandroid8f6d0532017-07-12 18:25:30 +053079 } else if (((link_type & NFA_P2P_LLINK_TYPE) == 0x00) &&
80 ((link_type & NFA_P2P_DLINK_TYPE) == 0x00)) {
nxf24591c1cbeab2018-02-21 17:32:26 +053081 LOG(ERROR) << StringPrintf("link type (0x%x) must be specified", link_type);
nxpandroid8f6d0532017-07-12 18:25:30 +053082 return (NFA_STATUS_FAILED);
83 }
84
85 if ((p_msg = (tNFA_P2P_API_REG_SERVER*)GKI_getbuf(
86 sizeof(tNFA_P2P_API_REG_SERVER))) != NULL) {
87 p_msg->hdr.event = NFA_P2P_API_REG_SERVER_EVT;
88
89 p_msg->server_sap = server_sap;
90 p_msg->link_type = link_type;
91
92 strncpy(p_msg->service_name, p_service_name, LLCP_MAX_SN_LEN);
93 p_msg->service_name[LLCP_MAX_SN_LEN] = 0;
94
95 p_msg->p_cback = p_cback;
96
97 nfa_sys_sendmsg(p_msg);
98
99 return (NFA_STATUS_OK);
100 }
101
102 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530103}
104
105/*******************************************************************************
106**
107** Function NFA_P2pRegisterClient
108**
nxpandroid8f6d0532017-07-12 18:25:30 +0530109** Description This function is called to register a client service on
110** LLCP.
nxpandroidc7611652015-09-23 16:42:05 +0530111**
nxpandroid8f6d0532017-07-12 18:25:30 +0530112** NFA_P2P_REG_CLIENT_EVT will be returned with status and
113** handle.
nxpandroidc7611652015-09-23 16:42:05 +0530114**
115** link_type : NFA_P2P_LLINK_TYPE and/or NFA_P2P_DLINK_TYPE
116**
117** Returns NFA_STATUS_OK if successfully initiated
118** NFA_STATUS_FAILED otherwise
119**
120*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530121tNFA_STATUS NFA_P2pRegisterClient(tNFA_P2P_LINK_TYPE link_type,
122 tNFA_P2P_CBACK* p_cback) {
123 tNFA_P2P_API_REG_CLIENT* p_msg;
nxpandroidc7611652015-09-23 16:42:05 +0530124
nxf24591c1cbeab2018-02-21 17:32:26 +0530125 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("link_type:0x%x", link_type);
nxpandroidc7611652015-09-23 16:42:05 +0530126
nxpandroid8f6d0532017-07-12 18:25:30 +0530127 if (((link_type & NFA_P2P_LLINK_TYPE) == 0x00) &&
128 ((link_type & NFA_P2P_DLINK_TYPE) == 0x00)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530129 LOG(ERROR) << StringPrintf("link type (0x%x) must be specified", link_type);
nxpandroidc7611652015-09-23 16:42:05 +0530130 return (NFA_STATUS_FAILED);
nxpandroid8f6d0532017-07-12 18:25:30 +0530131 }
132
133 if ((p_msg = (tNFA_P2P_API_REG_CLIENT*)GKI_getbuf(
134 sizeof(tNFA_P2P_API_REG_CLIENT))) != NULL) {
135 p_msg->hdr.event = NFA_P2P_API_REG_CLIENT_EVT;
136
137 p_msg->p_cback = p_cback;
138 p_msg->link_type = link_type;
139
140 nfa_sys_sendmsg(p_msg);
141
142 return (NFA_STATUS_OK);
143 }
144
145 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530146}
147
148/*******************************************************************************
149**
150** Function NFA_P2pDeregister
151**
152** Description This function is called to stop listening to a SAP as server
153** or stop client service on LLCP.
154**
nxpandroid8f6d0532017-07-12 18:25:30 +0530155** Note: If this function is called to de-register a server and RF
156** discovery is started,
157** NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
158** happen before calling this function
nxpandroidc7611652015-09-23 16:42:05 +0530159**
160** Returns NFA_STATUS_OK if successfully initiated
161** NFA_STATUS_BAD_HANDLE if handle is not valid
162** NFA_STATUS_FAILED otherwise
163**
164*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530165tNFA_STATUS NFA_P2pDeregister(tNFA_HANDLE handle) {
166 tNFA_P2P_API_DEREG* p_msg;
167 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530168
nxf24591c1cbeab2018-02-21 17:32:26 +0530169 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%02X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530170
nxpandroid8f6d0532017-07-12 18:25:30 +0530171 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530172
nxpandroid8f6d0532017-07-12 18:25:30 +0530173 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530174 LOG(ERROR) << StringPrintf("Handle is invalid or not registered");
nxpandroid8f6d0532017-07-12 18:25:30 +0530175 return (NFA_STATUS_BAD_HANDLE);
176 }
nxpandroidc7611652015-09-23 16:42:05 +0530177
nxpandroid8f6d0532017-07-12 18:25:30 +0530178 if ((p_msg = (tNFA_P2P_API_DEREG*)GKI_getbuf(sizeof(tNFA_P2P_API_DEREG))) !=
179 NULL) {
180 p_msg->hdr.event = NFA_P2P_API_DEREG_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530181
nxpandroid8f6d0532017-07-12 18:25:30 +0530182 p_msg->handle = handle;
nxpandroidc7611652015-09-23 16:42:05 +0530183
nxpandroid8f6d0532017-07-12 18:25:30 +0530184 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530185
nxpandroid8f6d0532017-07-12 18:25:30 +0530186 return (NFA_STATUS_OK);
187 }
nxpandroidc7611652015-09-23 16:42:05 +0530188
nxpandroid8f6d0532017-07-12 18:25:30 +0530189 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530190}
191
192/*******************************************************************************
193**
194** Function NFA_P2pAcceptConn
195**
196** Description This function is called to accept a request of data link
197** connection to a listening SAP on LLCP after receiving
198** NFA_P2P_CONN_REQ_EVT.
199**
200** Returns NFA_STATUS_OK if successfully initiated
201** NFA_STATUS_BAD_HANDLE if handle is not valid
202** NFA_STATUS_FAILED otherwise
203**
204*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530205tNFA_STATUS NFA_P2pAcceptConn(tNFA_HANDLE handle, uint16_t miu, uint8_t rw) {
206 tNFA_P2P_API_ACCEPT_CONN* p_msg;
207 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530208
nxf24591c1cbeab2018-02-21 17:32:26 +0530209 DLOG_IF(INFO, nfc_debug_enabled)
210 << StringPrintf("handle:0x%02X, MIU:%d, RW:%d", handle, miu, rw);
nxpandroidc7611652015-09-23 16:42:05 +0530211
nxpandroid8f6d0532017-07-12 18:25:30 +0530212 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530213
nxpandroid8f6d0532017-07-12 18:25:30 +0530214 if (!(xx & NFA_P2P_HANDLE_FLAG_CONN)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530215 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530216 return (NFA_STATUS_BAD_HANDLE);
217 } else {
218 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
219 }
nxpandroidc7611652015-09-23 16:42:05 +0530220
nxpandroid8f6d0532017-07-12 18:25:30 +0530221 if ((xx >= LLCP_MAX_DATA_LINK) || (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530222 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530223 return (NFA_STATUS_BAD_HANDLE);
224 }
nxpandroidc7611652015-09-23 16:42:05 +0530225
nxpandroid8f6d0532017-07-12 18:25:30 +0530226 if ((miu < LLCP_DEFAULT_MIU) || (nfa_p2p_cb.local_link_miu < miu)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530227 LOG(ERROR) << StringPrintf("MIU(%d) must be between %d and %d", miu,
228 LLCP_DEFAULT_MIU, nfa_p2p_cb.local_link_miu);
nxpandroid8f6d0532017-07-12 18:25:30 +0530229 } else if ((p_msg = (tNFA_P2P_API_ACCEPT_CONN*)GKI_getbuf(
230 sizeof(tNFA_P2P_API_ACCEPT_CONN))) != NULL) {
231 p_msg->hdr.event = NFA_P2P_API_ACCEPT_CONN_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530232
nxpandroid8f6d0532017-07-12 18:25:30 +0530233 p_msg->conn_handle = handle;
234 p_msg->miu = miu;
235 p_msg->rw = rw;
nxpandroidc7611652015-09-23 16:42:05 +0530236
nxpandroid8f6d0532017-07-12 18:25:30 +0530237 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530238
nxpandroid8f6d0532017-07-12 18:25:30 +0530239 return (NFA_STATUS_OK);
240 }
nxpandroidc7611652015-09-23 16:42:05 +0530241
nxpandroid8f6d0532017-07-12 18:25:30 +0530242 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530243}
244
245/*******************************************************************************
246**
247** Function NFA_P2pRejectConn
248**
249** Description This function is called to reject a request of data link
250** connection to a listening SAP on LLCP after receiving
251** NFA_P2P_CONN_REQ_EVT.
252**
253** Returns NFA_STATUS_OK if successfully initiated
254** NFA_STATUS_BAD_HANDLE if handle is not valid
255** NFA_STATUS_FAILED otherwise
256**
257*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530258tNFA_STATUS NFA_P2pRejectConn(tNFA_HANDLE handle) {
259 tNFA_P2P_API_REJECT_CONN* p_msg;
260 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530261
nxf24591c1cbeab2018-02-21 17:32:26 +0530262 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%02X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530263
nxpandroid8f6d0532017-07-12 18:25:30 +0530264 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530265
nxpandroid8f6d0532017-07-12 18:25:30 +0530266 if (!(xx & NFA_P2P_HANDLE_FLAG_CONN)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530267 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530268 return (NFA_STATUS_BAD_HANDLE);
269 } else {
270 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
271 }
nxpandroidc7611652015-09-23 16:42:05 +0530272
nxpandroid8f6d0532017-07-12 18:25:30 +0530273 if ((xx >= LLCP_MAX_DATA_LINK) || (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530274 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530275 return (NFA_STATUS_BAD_HANDLE);
276 }
nxpandroidc7611652015-09-23 16:42:05 +0530277
nxpandroid8f6d0532017-07-12 18:25:30 +0530278 if ((p_msg = (tNFA_P2P_API_REJECT_CONN*)GKI_getbuf(
279 sizeof(tNFA_P2P_API_REJECT_CONN))) != NULL) {
280 p_msg->hdr.event = NFA_P2P_API_REJECT_CONN_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530281
nxpandroid8f6d0532017-07-12 18:25:30 +0530282 p_msg->conn_handle = handle;
nxpandroidc7611652015-09-23 16:42:05 +0530283
nxpandroid8f6d0532017-07-12 18:25:30 +0530284 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530285
nxpandroid8f6d0532017-07-12 18:25:30 +0530286 return (NFA_STATUS_OK);
287 }
nxpandroidc7611652015-09-23 16:42:05 +0530288
nxpandroid8f6d0532017-07-12 18:25:30 +0530289 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530290}
291
292/*******************************************************************************
293**
294** Function NFA_P2pDisconnect
295**
296** Description This function is called to disconnect an existing or
297** connecting data link connection.
298**
nxpandroid8f6d0532017-07-12 18:25:30 +0530299** discard any pending data on data link connection if flush is
nxf24591c1cbeab2018-02-21 17:32:26 +0530300** set to TRUE
nxpandroidc7611652015-09-23 16:42:05 +0530301**
nxpandroid8f6d0532017-07-12 18:25:30 +0530302** NFA_P2P_DISC_EVT will be returned after data link connection
303** is disconnected
nxpandroidc7611652015-09-23 16:42:05 +0530304**
305** Returns NFA_STATUS_OK if successfully initiated
306** NFA_STATUS_BAD_HANDLE if handle is not valid
307** NFA_STATUS_FAILED otherwise
308**
309*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530310tNFA_STATUS NFA_P2pDisconnect(tNFA_HANDLE handle, bool flush) {
311 tNFA_P2P_API_DISCONNECT* p_msg;
312 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530313
nxf24591c1cbeab2018-02-21 17:32:26 +0530314 DLOG_IF(INFO, nfc_debug_enabled)
315 << StringPrintf("handle:0x%02X, flush=%d", handle, flush);
nxpandroidc7611652015-09-23 16:42:05 +0530316
nxpandroid8f6d0532017-07-12 18:25:30 +0530317 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530318
nxpandroid8f6d0532017-07-12 18:25:30 +0530319 if (xx & NFA_P2P_HANDLE_FLAG_CONN) {
320 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
nxpandroidc7611652015-09-23 16:42:05 +0530321
nxpandroid8f6d0532017-07-12 18:25:30 +0530322 if ((xx >= LLCP_MAX_DATA_LINK) || (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530323 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530324 return (NFA_STATUS_BAD_HANDLE);
nxpandroidc7611652015-09-23 16:42:05 +0530325 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530326 } else {
nxf24591c1cbeab2018-02-21 17:32:26 +0530327 LOG(ERROR) << StringPrintf("Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530328 return (NFA_STATUS_BAD_HANDLE);
329 }
nxpandroidc7611652015-09-23 16:42:05 +0530330
nxpandroid8f6d0532017-07-12 18:25:30 +0530331 if ((p_msg = (tNFA_P2P_API_DISCONNECT*)GKI_getbuf(
332 sizeof(tNFA_P2P_API_DISCONNECT))) != NULL) {
333 p_msg->hdr.event = NFA_P2P_API_DISCONNECT_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530334
nxpandroid8f6d0532017-07-12 18:25:30 +0530335 p_msg->conn_handle = handle;
336 p_msg->flush = flush;
nxpandroidc7611652015-09-23 16:42:05 +0530337
nxpandroid8f6d0532017-07-12 18:25:30 +0530338 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530339
nxpandroid8f6d0532017-07-12 18:25:30 +0530340 return (NFA_STATUS_OK);
341 }
nxpandroidc7611652015-09-23 16:42:05 +0530342
nxpandroid8f6d0532017-07-12 18:25:30 +0530343 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530344}
345
346/*******************************************************************************
347**
348** Function NFA_P2pConnectByName
349**
nxpandroid8f6d0532017-07-12 18:25:30 +0530350** Description This function is called to create a connection-oriented
351** transport by a service name.
nxpandroidc7611652015-09-23 16:42:05 +0530352** NFA_P2P_CONNECTED_EVT if success
353** NFA_P2P_DISC_EVT if failed
354**
355** Returns NFA_STATUS_OK if successfully initiated
356** NFA_STATUS_BAD_HANDLE if client is not registered
357** NFA_STATUS_FAILED otherwise
358**
359*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530360tNFA_STATUS NFA_P2pConnectByName(tNFA_HANDLE client_handle,
361 char* p_service_name, uint16_t miu,
362 uint8_t rw) {
363 tNFA_P2P_API_CONNECT* p_msg;
364 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530365
nxf24591c1cbeab2018-02-21 17:32:26 +0530366 DLOG_IF(INFO, nfc_debug_enabled)
367 << StringPrintf("client_handle:0x%x, SN:<%s>, MIU:%d, RW:%d",
368 client_handle, p_service_name, miu, rw);
nxpandroidc7611652015-09-23 16:42:05 +0530369
nxpandroid8f6d0532017-07-12 18:25:30 +0530370 xx = client_handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530371
nxpandroid8f6d0532017-07-12 18:25:30 +0530372 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530373 LOG(ERROR) << StringPrintf("Client Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530374 return (NFA_STATUS_BAD_HANDLE);
375 }
nxpandroidc7611652015-09-23 16:42:05 +0530376
nxpandroid8f6d0532017-07-12 18:25:30 +0530377 if ((miu < LLCP_DEFAULT_MIU) ||
378 (nfa_p2p_cb.llcp_state != NFA_P2P_LLCP_STATE_ACTIVATED) ||
379 (nfa_p2p_cb.local_link_miu < miu)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530380 LOG(ERROR) << StringPrintf(
381 "MIU(%d) must be between %d and %d or LLCP "
nxpandroid8f6d0532017-07-12 18:25:30 +0530382 "link is not activated",
383 miu, LLCP_DEFAULT_MIU, nfa_p2p_cb.local_link_miu);
384 } else if ((p_msg = (tNFA_P2P_API_CONNECT*)GKI_getbuf(
385 sizeof(tNFA_P2P_API_CONNECT))) != NULL) {
386 p_msg->hdr.event = NFA_P2P_API_CONNECT_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530387
nxpandroid8f6d0532017-07-12 18:25:30 +0530388 strncpy(p_msg->service_name, p_service_name, LLCP_MAX_SN_LEN);
389 p_msg->service_name[LLCP_MAX_SN_LEN] = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530390
nxpandroid8f6d0532017-07-12 18:25:30 +0530391 p_msg->dsap = LLCP_INVALID_SAP;
392 p_msg->miu = miu;
393 p_msg->rw = rw;
394 p_msg->client_handle = client_handle;
nxpandroidc7611652015-09-23 16:42:05 +0530395
nxpandroid8f6d0532017-07-12 18:25:30 +0530396 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530397
nxpandroid8f6d0532017-07-12 18:25:30 +0530398 return (NFA_STATUS_OK);
399 }
nxpandroidc7611652015-09-23 16:42:05 +0530400
nxpandroid8f6d0532017-07-12 18:25:30 +0530401 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530402}
403
404/*******************************************************************************
405**
406** Function NFA_P2pConnectBySap
407**
nxpandroid8f6d0532017-07-12 18:25:30 +0530408** Description This function is called to create a connection-oriented
409** transport by a SAP.
nxpandroidc7611652015-09-23 16:42:05 +0530410** NFA_P2P_CONNECTED_EVT if success
411** NFA_P2P_DISC_EVT if failed
412**
413** Returns NFA_STATUS_OK if successfully initiated
414** NFA_STATUS_BAD_HANDLE if client is not registered
415** NFA_STATUS_FAILED otherwise
416**
417*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530418tNFA_STATUS NFA_P2pConnectBySap(tNFA_HANDLE client_handle, uint8_t dsap,
419 uint16_t miu, uint8_t rw) {
420 tNFA_P2P_API_CONNECT* p_msg;
421 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530422
nxf24591c1cbeab2018-02-21 17:32:26 +0530423 DLOG_IF(INFO, nfc_debug_enabled)
424 << StringPrintf("client_handle:0x%x, DSAP:0x%02X, MIU:%d, RW:%d",
425 client_handle, dsap, miu, rw);
nxpandroidc7611652015-09-23 16:42:05 +0530426
nxpandroid8f6d0532017-07-12 18:25:30 +0530427 xx = client_handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530428
nxpandroid8f6d0532017-07-12 18:25:30 +0530429 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530430 LOG(ERROR) << StringPrintf("Client Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530431 return (NFA_STATUS_BAD_HANDLE);
432 }
nxpandroidc7611652015-09-23 16:42:05 +0530433
nxpandroid8f6d0532017-07-12 18:25:30 +0530434 if ((miu < LLCP_DEFAULT_MIU) ||
435 (nfa_p2p_cb.llcp_state != NFA_P2P_LLCP_STATE_ACTIVATED) ||
436 (nfa_p2p_cb.local_link_miu < miu)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530437 LOG(ERROR) << StringPrintf(
438 "MIU(%d) must be between %d and %d, or LLCP "
nxpandroid8f6d0532017-07-12 18:25:30 +0530439 "link is not activated",
440 miu, LLCP_DEFAULT_MIU, nfa_p2p_cb.local_link_miu);
441 } else if ((p_msg = (tNFA_P2P_API_CONNECT*)GKI_getbuf(
442 sizeof(tNFA_P2P_API_CONNECT))) != NULL) {
443 p_msg->hdr.event = NFA_P2P_API_CONNECT_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530444
nxpandroid8f6d0532017-07-12 18:25:30 +0530445 p_msg->service_name[LLCP_MAX_SN_LEN] = 0;
nxpandroidc7611652015-09-23 16:42:05 +0530446
nxpandroid8f6d0532017-07-12 18:25:30 +0530447 p_msg->dsap = dsap;
448 p_msg->miu = miu;
449 p_msg->rw = rw;
450 p_msg->client_handle = client_handle;
nxpandroidc7611652015-09-23 16:42:05 +0530451
nxpandroid8f6d0532017-07-12 18:25:30 +0530452 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530453
nxpandroid8f6d0532017-07-12 18:25:30 +0530454 return (NFA_STATUS_OK);
455 }
nxpandroidc7611652015-09-23 16:42:05 +0530456
nxpandroid8f6d0532017-07-12 18:25:30 +0530457 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530458}
459
460/*******************************************************************************
461**
462** Function NFA_P2pSendUI
463**
464** Description This function is called to send data on connectionless
465** transport.
466**
467** Returns NFA_STATUS_OK if successfully initiated
468** NFA_STATUS_BAD_HANDLE if handle is not valid
nxpandroid8f6d0532017-07-12 18:25:30 +0530469** NFA_STATUS_BAD_LENGTH if data length is more than remote
470** link MIU
nxpandroidc7611652015-09-23 16:42:05 +0530471** NFA_STATUS_CONGESTED if congested
472** NFA_STATUS_FAILED otherwise
473**
474*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530475tNFA_STATUS NFA_P2pSendUI(tNFA_HANDLE handle, uint8_t dsap, uint16_t length,
476 uint8_t* p_data) {
477 tNFA_P2P_API_SEND_UI* p_msg;
478 tNFA_STATUS ret_status = NFA_STATUS_FAILED;
479 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530480
nxf24591c1cbeab2018-02-21 17:32:26 +0530481 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
482 "handle:0x%X, DSAP:0x%02X, length:%d", handle, dsap, length);
nxpandroidc7611652015-09-23 16:42:05 +0530483
nxpandroid8f6d0532017-07-12 18:25:30 +0530484 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530485
nxpandroid8f6d0532017-07-12 18:25:30 +0530486 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530487
nxpandroid8f6d0532017-07-12 18:25:30 +0530488 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530489 LOG(ERROR) << StringPrintf("Handle (0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530490 ret_status = NFA_STATUS_BAD_HANDLE;
491 } else if (length > nfa_p2p_cb.remote_link_miu) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530492 LOG(ERROR) << StringPrintf(
493 "handle:0x%X, length(%d) must be less than remote "
nxpandroid8f6d0532017-07-12 18:25:30 +0530494 "link MIU(%d)",
495 handle, length, nfa_p2p_cb.remote_link_miu);
496 ret_status = NFA_STATUS_BAD_LENGTH;
497 } else if (nfa_p2p_cb.sap_cb[xx].flags & NFA_P2P_SAP_FLAG_LLINK_CONGESTED) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530498 LOG(WARNING) << StringPrintf(
499 "handle:0x%X, logical data link is already congested", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530500 ret_status = NFA_STATUS_CONGESTED;
501 } else if (LLCP_IsLogicalLinkCongested(
502 (uint8_t)xx, nfa_p2p_cb.sap_cb[xx].num_pending_ui_pdu,
503 nfa_p2p_cb.total_pending_ui_pdu,
504 nfa_p2p_cb.total_pending_i_pdu)) {
505 nfa_p2p_cb.sap_cb[xx].flags |= NFA_P2P_SAP_FLAG_LLINK_CONGESTED;
506
nxf24591c1cbeab2018-02-21 17:32:26 +0530507 LOG(WARNING) << StringPrintf("handle:0x%X, logical data link is congested",
508 handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530509 ret_status = NFA_STATUS_CONGESTED;
510 } else if ((p_msg = (tNFA_P2P_API_SEND_UI*)GKI_getbuf(
511 sizeof(tNFA_P2P_API_SEND_UI))) != NULL) {
512 p_msg->hdr.event = NFA_P2P_API_SEND_UI_EVT;
513
514 p_msg->handle = handle;
515 p_msg->dsap = dsap;
516
517 p_msg->p_msg = (NFC_HDR*)GKI_getpoolbuf(LLCP_POOL_ID);
518 if (p_msg->p_msg != NULL) {
519 p_msg->p_msg->len = length;
520 p_msg->p_msg->offset = LLCP_MIN_OFFSET;
521 memcpy(((uint8_t*)(p_msg->p_msg + 1) + p_msg->p_msg->offset), p_data,
522 length);
523
524 /* increase number of tx UI PDU which is not processed by NFA for
525 * congestion control */
526 nfa_p2p_cb.sap_cb[xx].num_pending_ui_pdu++;
527 nfa_p2p_cb.total_pending_ui_pdu++;
528 nfa_sys_sendmsg(p_msg);
529
530 ret_status = NFA_STATUS_OK;
531 } else {
532 GKI_freebuf(p_msg);
533
534 nfa_p2p_cb.sap_cb[xx].flags |= NFA_P2P_SAP_FLAG_LLINK_CONGESTED;
535 ret_status = NFA_STATUS_CONGESTED;
nxpandroidc7611652015-09-23 16:42:05 +0530536 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530537 }
nxpandroidc7611652015-09-23 16:42:05 +0530538
nxpandroid8f6d0532017-07-12 18:25:30 +0530539 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530540
nxpandroid8f6d0532017-07-12 18:25:30 +0530541 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530542}
543
544/*******************************************************************************
545**
546** Function NFA_P2pReadUI
547**
548** Description This function is called to read data on connectionless
nxpandroid8f6d0532017-07-12 18:25:30 +0530549** transport when receiving NFA_P2P_DATA_EVT with
550** NFA_P2P_LLINK_TYPE.
nxpandroidc7611652015-09-23 16:42:05 +0530551**
552** - Remote SAP who sent UI PDU is returned.
nxpandroid8f6d0532017-07-12 18:25:30 +0530553** - Information of UI PDU up to max_data_len is copied into
554** p_data.
555** - If more information of UI PDU or more UI PDU in queue then
nxf24591c1cbeab2018-02-21 17:32:26 +0530556** more is returned to TRUE.
nxpandroidc7611652015-09-23 16:42:05 +0530557** - Information of next UI PDU is not concatenated.
558**
559** Returns NFA_STATUS_OK if successfully initiated
560** NFA_STATUS_BAD_HANDLE if handle is not valid
561**
562*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530563tNFA_STATUS NFA_P2pReadUI(tNFA_HANDLE handle, uint32_t max_data_len,
564 uint8_t* p_remote_sap, uint32_t* p_data_len,
565 uint8_t* p_data, bool* p_more) {
566 tNFA_STATUS ret_status;
567 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530568
nxf24591c1cbeab2018-02-21 17:32:26 +0530569 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530570
nxpandroid8f6d0532017-07-12 18:25:30 +0530571 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530572
nxpandroid8f6d0532017-07-12 18:25:30 +0530573 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530574
nxpandroid8f6d0532017-07-12 18:25:30 +0530575 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530576 LOG(ERROR) << StringPrintf("Handle (0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530577 ret_status = NFA_STATUS_BAD_HANDLE;
578 } else {
579 *p_more = LLCP_ReadLogicalLinkData((uint8_t)xx, max_data_len, p_remote_sap,
580 p_data_len, p_data);
581 ret_status = NFA_STATUS_OK;
582 }
nxpandroidc7611652015-09-23 16:42:05 +0530583
nxpandroid8f6d0532017-07-12 18:25:30 +0530584 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530585
nxpandroid8f6d0532017-07-12 18:25:30 +0530586 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530587}
588
589/*******************************************************************************
590**
591** Function NFA_P2pFlushUI
592**
593** Description This function is called to flush data on connectionless
594** transport.
595**
596** Returns NFA_STATUS_OK if successfully initiated
597** NFA_STATUS_BAD_HANDLE if handle is not valid
598**
599*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530600tNFA_STATUS NFA_P2pFlushUI(tNFA_HANDLE handle, uint32_t* p_length) {
601 tNFA_STATUS ret_status;
602 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530603
nxf24591c1cbeab2018-02-21 17:32:26 +0530604 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530605
nxpandroid8f6d0532017-07-12 18:25:30 +0530606 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530607
nxpandroid8f6d0532017-07-12 18:25:30 +0530608 xx = handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530609
nxpandroid8f6d0532017-07-12 18:25:30 +0530610 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530611 LOG(ERROR) << StringPrintf("Handle (0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530612 ret_status = NFA_STATUS_BAD_HANDLE;
613 *p_length = 0;
614 } else {
615 *p_length = LLCP_FlushLogicalLinkRxData((uint8_t)xx);
616 ret_status = NFA_STATUS_OK;
617 }
nxpandroidc7611652015-09-23 16:42:05 +0530618
nxpandroid8f6d0532017-07-12 18:25:30 +0530619 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530620
nxpandroid8f6d0532017-07-12 18:25:30 +0530621 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530622}
623
624/*******************************************************************************
625**
626** Function NFA_P2pSendData
627**
628** Description This function is called to send data on connection-oriented
629** transport.
630**
631** Returns NFA_STATUS_OK if successfully initiated
632** NFA_STATUS_BAD_HANDLE if handle is not valid
633** NFA_STATUS_BAD_LENGTH if data length is more than remote MIU
634** NFA_STATUS_CONGESTED if congested
635** NFA_STATUS_FAILED otherwise
636**
637*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530638tNFA_STATUS NFA_P2pSendData(tNFA_HANDLE handle, uint16_t length,
639 uint8_t* p_data) {
640 tNFA_P2P_API_SEND_DATA* p_msg;
641 tNFA_STATUS ret_status = NFA_STATUS_FAILED;
642 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530643
nxf24591c1cbeab2018-02-21 17:32:26 +0530644 DLOG_IF(INFO, nfc_debug_enabled)
645 << StringPrintf("handle:0x%X, length:%d", handle, length);
nxpandroidc7611652015-09-23 16:42:05 +0530646
nxpandroid8f6d0532017-07-12 18:25:30 +0530647 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530648
nxpandroid8f6d0532017-07-12 18:25:30 +0530649 xx = handle & NFA_HANDLE_MASK;
650 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
nxpandroidc7611652015-09-23 16:42:05 +0530651
nxpandroid8f6d0532017-07-12 18:25:30 +0530652 if ((!(handle & NFA_P2P_HANDLE_FLAG_CONN)) || (xx >= LLCP_MAX_DATA_LINK) ||
653 (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530654 LOG(ERROR) << StringPrintf("Handle(0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530655 ret_status = NFA_STATUS_BAD_HANDLE;
656 } else if (nfa_p2p_cb.conn_cb[xx].flags & NFA_P2P_CONN_FLAG_REMOTE_RW_ZERO) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530657 LOG(ERROR) << StringPrintf("handle:0x%X, Remote set RW to 0 (flow off)",
658 handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530659 ret_status = NFA_STATUS_FAILED;
660 } else if (nfa_p2p_cb.conn_cb[xx].remote_miu < length) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530661 LOG(ERROR) << StringPrintf("handle:0x%X, Data more than remote MIU(%d)",
662 handle, nfa_p2p_cb.conn_cb[xx].remote_miu);
nxpandroid8f6d0532017-07-12 18:25:30 +0530663 ret_status = NFA_STATUS_BAD_LENGTH;
664 } else if (nfa_p2p_cb.conn_cb[xx].flags & NFA_P2P_CONN_FLAG_CONGESTED) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530665 LOG(WARNING) << StringPrintf(
666 "handle:0x%X, data link connection is already "
nxpandroid8f6d0532017-07-12 18:25:30 +0530667 "congested",
668 handle);
669 ret_status = NFA_STATUS_CONGESTED;
670 } else if (LLCP_IsDataLinkCongested(nfa_p2p_cb.conn_cb[xx].local_sap,
671 nfa_p2p_cb.conn_cb[xx].remote_sap,
672 nfa_p2p_cb.conn_cb[xx].num_pending_i_pdu,
673 nfa_p2p_cb.total_pending_ui_pdu,
674 nfa_p2p_cb.total_pending_i_pdu)) {
675 nfa_p2p_cb.conn_cb[xx].flags |= NFA_P2P_CONN_FLAG_CONGESTED;
676
nxf24591c1cbeab2018-02-21 17:32:26 +0530677 LOG(WARNING) << StringPrintf(
678 "handle:0x%X, data link connection is congested", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530679 ret_status = NFA_STATUS_CONGESTED;
680 } else if ((p_msg = (tNFA_P2P_API_SEND_DATA*)GKI_getbuf(
681 sizeof(tNFA_P2P_API_SEND_DATA))) != NULL) {
682 p_msg->hdr.event = NFA_P2P_API_SEND_DATA_EVT;
683
684 p_msg->conn_handle = handle;
685
686 p_msg->p_msg = (NFC_HDR*)GKI_getpoolbuf(LLCP_POOL_ID);
687 if (p_msg->p_msg != NULL) {
688 p_msg->p_msg->len = length;
689 p_msg->p_msg->offset = LLCP_MIN_OFFSET;
690 memcpy(((uint8_t*)(p_msg->p_msg + 1) + p_msg->p_msg->offset), p_data,
691 length);
692
693 /* increase number of tx I PDU which is not processed by NFA for
694 * congestion control */
695 nfa_p2p_cb.conn_cb[xx].num_pending_i_pdu++;
696 nfa_p2p_cb.total_pending_i_pdu++;
697 nfa_sys_sendmsg(p_msg);
698
699 ret_status = NFA_STATUS_OK;
700 } else {
701 GKI_freebuf(p_msg);
702 nfa_p2p_cb.conn_cb[xx].flags |= NFA_P2P_CONN_FLAG_CONGESTED;
703 ret_status = NFA_STATUS_CONGESTED;
nxpandroidc7611652015-09-23 16:42:05 +0530704 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530705 }
nxpandroidc7611652015-09-23 16:42:05 +0530706
nxpandroid8f6d0532017-07-12 18:25:30 +0530707 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530708
nxpandroid8f6d0532017-07-12 18:25:30 +0530709 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530710}
711
712/*******************************************************************************
713**
714** Function NFA_P2pReadData
715**
716** Description This function is called to read data on connection-oriented
nxpandroid8f6d0532017-07-12 18:25:30 +0530717** transport when receiving NFA_P2P_DATA_EVT with
718** NFA_P2P_DLINK_TYPE.
nxpandroidc7611652015-09-23 16:42:05 +0530719**
nxpandroid8f6d0532017-07-12 18:25:30 +0530720** - Information of I PDU is copied into p_data up to
721** max_data_len.
722** - If more information of I PDU or more I PDU in queue, then
nxf24591c1cbeab2018-02-21 17:32:26 +0530723** more is returned to TRUE.
nxpandroidc7611652015-09-23 16:42:05 +0530724** - Information of next I PDU is not concatenated.
725**
726** Returns NFA_STATUS_OK if successfully initiated
727** NFA_STATUS_BAD_HANDLE if handle is not valid
728**
729*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530730tNFA_STATUS NFA_P2pReadData(tNFA_HANDLE handle, uint32_t max_data_len,
731 uint32_t* p_data_len, uint8_t* p_data,
732 bool* p_more) {
733 tNFA_STATUS ret_status;
734 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530735
nxf24591c1cbeab2018-02-21 17:32:26 +0530736 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530737
nxpandroid8f6d0532017-07-12 18:25:30 +0530738 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530739
nxpandroid8f6d0532017-07-12 18:25:30 +0530740 xx = handle & NFA_HANDLE_MASK;
741 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
nxpandroidc7611652015-09-23 16:42:05 +0530742
nxpandroid8f6d0532017-07-12 18:25:30 +0530743 if ((!(handle & NFA_P2P_HANDLE_FLAG_CONN)) || (xx >= LLCP_MAX_DATA_LINK) ||
744 (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530745 LOG(ERROR) << StringPrintf("Handle(0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530746 ret_status = NFA_STATUS_BAD_HANDLE;
747 } else {
748 *p_more = LLCP_ReadDataLinkData(nfa_p2p_cb.conn_cb[xx].local_sap,
749 nfa_p2p_cb.conn_cb[xx].remote_sap,
750 max_data_len, p_data_len, p_data);
751 ret_status = NFA_STATUS_OK;
752 }
nxpandroidc7611652015-09-23 16:42:05 +0530753
nxpandroid8f6d0532017-07-12 18:25:30 +0530754 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530755
nxpandroid8f6d0532017-07-12 18:25:30 +0530756 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530757}
758
759/*******************************************************************************
760**
761** Function NFA_P2pFlushData
762**
763** Description This function is called to flush data on connection-oriented
764** transport.
765**
766** Returns NFA_STATUS_OK if successfully initiated
767** NFA_STATUS_BAD_HANDLE if handle is not valid
768**
769*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530770tNFA_STATUS NFA_P2pFlushData(tNFA_HANDLE handle, uint32_t* p_length) {
771 tNFA_STATUS ret_status;
772 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530773
nxf24591c1cbeab2018-02-21 17:32:26 +0530774 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%X", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530775
nxpandroid8f6d0532017-07-12 18:25:30 +0530776 GKI_sched_lock();
nxpandroidc7611652015-09-23 16:42:05 +0530777
nxpandroid8f6d0532017-07-12 18:25:30 +0530778 xx = handle & NFA_HANDLE_MASK;
779 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
nxpandroidc7611652015-09-23 16:42:05 +0530780
nxpandroid8f6d0532017-07-12 18:25:30 +0530781 if ((!(handle & NFA_P2P_HANDLE_FLAG_CONN)) || (xx >= LLCP_MAX_DATA_LINK) ||
782 (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530783 LOG(ERROR) << StringPrintf("Handle(0x%X) is not valid", handle);
nxpandroid8f6d0532017-07-12 18:25:30 +0530784 ret_status = NFA_STATUS_BAD_HANDLE;
785 } else {
786 *p_length = LLCP_FlushDataLinkRxData(nfa_p2p_cb.conn_cb[xx].local_sap,
787 nfa_p2p_cb.conn_cb[xx].remote_sap);
788 ret_status = NFA_STATUS_OK;
789 }
nxpandroidc7611652015-09-23 16:42:05 +0530790
nxpandroid8f6d0532017-07-12 18:25:30 +0530791 GKI_sched_unlock();
nxpandroidc7611652015-09-23 16:42:05 +0530792
nxpandroid8f6d0532017-07-12 18:25:30 +0530793 return (ret_status);
nxpandroidc7611652015-09-23 16:42:05 +0530794}
795
796/*******************************************************************************
797**
798** Function NFA_P2pSetLocalBusy
799**
800** Description This function is called to stop or resume incoming data on
801** connection-oriented transport.
802**
803** Returns NFA_STATUS_OK if successfully initiated
804** NFA_STATUS_BAD_HANDLE if handle is not valid
805** NFA_STATUS_FAILED otherwise
806**
807*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530808tNFA_STATUS NFA_P2pSetLocalBusy(tNFA_HANDLE conn_handle, bool is_busy) {
809 tNFA_P2P_API_SET_LOCAL_BUSY* p_msg;
810 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530811
nxf24591c1cbeab2018-02-21 17:32:26 +0530812 DLOG_IF(INFO, nfc_debug_enabled)
813 << StringPrintf("conn_handle:0x%02X, is_busy:%d", conn_handle, is_busy);
nxpandroidc7611652015-09-23 16:42:05 +0530814
nxpandroid8f6d0532017-07-12 18:25:30 +0530815 xx = conn_handle & NFA_HANDLE_MASK;
nxpandroidc7611652015-09-23 16:42:05 +0530816
nxpandroid8f6d0532017-07-12 18:25:30 +0530817 if (!(xx & NFA_P2P_HANDLE_FLAG_CONN)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530818 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530819 return (NFA_STATUS_BAD_HANDLE);
820 } else {
821 xx &= ~NFA_P2P_HANDLE_FLAG_CONN;
822 }
nxpandroidc7611652015-09-23 16:42:05 +0530823
nxpandroid8f6d0532017-07-12 18:25:30 +0530824 if ((xx >= LLCP_MAX_DATA_LINK) || (nfa_p2p_cb.conn_cb[xx].flags == 0)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530825 LOG(ERROR) << StringPrintf("Connection Handle is not valid");
nxpandroid8f6d0532017-07-12 18:25:30 +0530826 return (NFA_STATUS_BAD_HANDLE);
827 }
nxpandroidc7611652015-09-23 16:42:05 +0530828
nxpandroid8f6d0532017-07-12 18:25:30 +0530829 if ((p_msg = (tNFA_P2P_API_SET_LOCAL_BUSY*)GKI_getbuf(
830 sizeof(tNFA_P2P_API_SET_LOCAL_BUSY))) != NULL) {
831 p_msg->hdr.event = NFA_P2P_API_SET_LOCAL_BUSY_EVT;
nxpandroidc7611652015-09-23 16:42:05 +0530832
nxpandroid8f6d0532017-07-12 18:25:30 +0530833 p_msg->conn_handle = conn_handle;
834 p_msg->is_busy = is_busy;
nxpandroidc7611652015-09-23 16:42:05 +0530835
nxpandroid8f6d0532017-07-12 18:25:30 +0530836 nfa_sys_sendmsg(p_msg);
nxpandroidc7611652015-09-23 16:42:05 +0530837
nxpandroid8f6d0532017-07-12 18:25:30 +0530838 return (NFA_STATUS_OK);
839 }
nxpandroidc7611652015-09-23 16:42:05 +0530840
nxpandroid8f6d0532017-07-12 18:25:30 +0530841 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530842}
843
844/*******************************************************************************
845**
846** Function NFA_P2pGetLinkInfo
847**
848** Description This function is called to get local/remote link MIU and
nxpandroid8f6d0532017-07-12 18:25:30 +0530849** Well-Known Service list encoded as a 16-bit field of
850** connected LLCP. NFA_P2P_LINK_INFO_EVT will be returned.
nxpandroidc7611652015-09-23 16:42:05 +0530851**
852** Returns NFA_STATUS_OK if successfully initiated
853** NFA_STATUS_BAD_HANDLE if server or client is not registered
854** NFA_STATUS_FAILED otherwise
855**
856*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530857tNFA_STATUS NFA_P2pGetLinkInfo(tNFA_HANDLE handle) {
858 tNFA_P2P_API_GET_LINK_INFO* p_msg;
859 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530860
nxf24591c1cbeab2018-02-21 17:32:26 +0530861 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("handle:0x%x", handle);
nxpandroidc7611652015-09-23 16:42:05 +0530862
nxpandroid8f6d0532017-07-12 18:25:30 +0530863 if (nfa_p2p_cb.llcp_state != NFA_P2P_LLCP_STATE_ACTIVATED) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530864 LOG(ERROR) << StringPrintf("LLCP link is not activated");
nxpandroidc7611652015-09-23 16:42:05 +0530865 return (NFA_STATUS_FAILED);
nxpandroid8f6d0532017-07-12 18:25:30 +0530866 }
867
868 xx = handle & NFA_HANDLE_MASK;
869
870 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530871 LOG(ERROR) << StringPrintf("Handle is invalid or not registered");
nxpandroid8f6d0532017-07-12 18:25:30 +0530872 return (NFA_STATUS_BAD_HANDLE);
873 }
874
875 if ((p_msg = (tNFA_P2P_API_GET_LINK_INFO*)GKI_getbuf(
876 sizeof(tNFA_P2P_API_GET_LINK_INFO))) != NULL) {
877 p_msg->hdr.event = NFA_P2P_API_GET_LINK_INFO_EVT;
878
879 p_msg->handle = handle;
880
881 nfa_sys_sendmsg(p_msg);
882
883 return (NFA_STATUS_OK);
884 }
885
886 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530887}
888
889/*******************************************************************************
890**
891** Function NFA_P2pGetRemoteSap
892**
nxpandroid8f6d0532017-07-12 18:25:30 +0530893** Description This function is called to get SAP associated by service
894** name on connected remote LLCP.
nxpandroidc7611652015-09-23 16:42:05 +0530895** NFA_P2P_SDP_EVT will be returned.
896**
897** Returns NFA_STATUS_OK if successfully initiated
898** NFA_STATUS_BAD_HANDLE if server or client is not registered
899** NFA_STATUS_FAILED otherwise
900**
901*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530902tNFA_STATUS NFA_P2pGetRemoteSap(tNFA_HANDLE handle, char* p_service_name) {
903 tNFA_P2P_API_GET_REMOTE_SAP* p_msg;
904 tNFA_HANDLE xx;
nxpandroidc7611652015-09-23 16:42:05 +0530905
nxf24591c1cbeab2018-02-21 17:32:26 +0530906 DLOG_IF(INFO, nfc_debug_enabled)
907 << StringPrintf("handle:0x%x, SN:<%s>", handle, p_service_name);
nxpandroidc7611652015-09-23 16:42:05 +0530908
nxpandroid8f6d0532017-07-12 18:25:30 +0530909 if (nfa_p2p_cb.llcp_state != NFA_P2P_LLCP_STATE_ACTIVATED) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530910 LOG(ERROR) << StringPrintf("LLCP link is not activated");
nxpandroidc7611652015-09-23 16:42:05 +0530911 return (NFA_STATUS_FAILED);
nxpandroid8f6d0532017-07-12 18:25:30 +0530912 }
913
914 xx = handle & NFA_HANDLE_MASK;
915
916 if ((xx >= NFA_P2P_NUM_SAP) || (nfa_p2p_cb.sap_cb[xx].p_cback == NULL)) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530917 LOG(ERROR) << StringPrintf("Handle is invalid or not registered");
nxpandroid8f6d0532017-07-12 18:25:30 +0530918 return (NFA_STATUS_BAD_HANDLE);
919 }
920
921 if ((p_msg = (tNFA_P2P_API_GET_REMOTE_SAP*)GKI_getbuf(
922 sizeof(tNFA_P2P_API_GET_REMOTE_SAP))) != NULL) {
923 p_msg->hdr.event = NFA_P2P_API_GET_REMOTE_SAP_EVT;
924
925 p_msg->handle = handle;
926
927 strncpy(p_msg->service_name, p_service_name, LLCP_MAX_SN_LEN);
928 p_msg->service_name[LLCP_MAX_SN_LEN] = 0;
929
930 nfa_sys_sendmsg(p_msg);
931
932 return (NFA_STATUS_OK);
933 }
934
935 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +0530936}
937
938/*******************************************************************************
939**
940** Function NFA_P2pSetLLCPConfig
941**
942** Description This function is called to change LLCP config parameters.
943** Application must call while LLCP is not activated.
944**
945** Parameters descriptions (default value)
946** - Local Link MIU (LLCP_MIU)
947** - Option parameter (LLCP_OPT_VALUE)
948** - Response Waiting Time Index (LLCP_WAITING_TIME)
949** - Local Link Timeout (LLCP_LTO_VALUE)
nxpandroid8f6d0532017-07-12 18:25:30 +0530950** - Inactivity Timeout as initiator role
951** (LLCP_INIT_INACTIVITY_TIMEOUT)
952** - Inactivity Timeout as target role
953** (LLCP_TARGET_INACTIVITY_TIMEOUT)
nxpandroidc7611652015-09-23 16:42:05 +0530954** - Delay SYMM response (LLCP_DELAY_RESP_TIME)
nxpandroid8f6d0532017-07-12 18:25:30 +0530955** - Data link connection timeout
956** (LLCP_DATA_LINK_CONNECTION_TOUT)
957** - Delay timeout to send first PDU as initiator
958** (LLCP_DELAY_TIME_TO_SEND_FIRST_PDU)
nxpandroidc7611652015-09-23 16:42:05 +0530959**
960** Returns NFA_STATUS_OK if successfully initiated
961** NFA_STATUS_FAILED otherwise
962**
963*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530964tNFA_STATUS NFA_P2pSetLLCPConfig(uint16_t link_miu, uint8_t opt, uint8_t wt,
965 uint16_t link_timeout,
966 uint16_t inact_timeout_init,
967 uint16_t inact_timeout_target,
968 uint16_t symm_delay,
969 uint16_t data_link_timeout,
970 uint16_t delay_first_pdu_timeout) {
971 tNFA_P2P_API_SET_LLCP_CFG* p_msg;
nxpandroidc7611652015-09-23 16:42:05 +0530972
nxf24591c1cbeab2018-02-21 17:32:26 +0530973 DLOG_IF(INFO, nfc_debug_enabled)
974 << StringPrintf("link_miu:%d, opt:0x%02X, wt:%d, link_timeout:%d",
975 link_miu, opt, wt, link_timeout);
976 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
nxpandroid8f6d0532017-07-12 18:25:30 +0530977 " inact_timeout(init:%d, target:%d), "
978 "symm_delay:%d, data_link_timeout:%d",
979 inact_timeout_init, inact_timeout_target, symm_delay, data_link_timeout);
nxf24591c1cbeab2018-02-21 17:32:26 +0530980 DLOG_IF(INFO, nfc_debug_enabled)
981 << StringPrintf(" delay_first_pdu_timeout:%d",
982 delay_first_pdu_timeout);
nxpandroidc7611652015-09-23 16:42:05 +0530983
nxpandroid8f6d0532017-07-12 18:25:30 +0530984 if (nfa_p2p_cb.llcp_state == NFA_P2P_LLCP_STATE_ACTIVATED) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530985 LOG(ERROR) << StringPrintf("LLCP link is activated");
nxpandroidc7611652015-09-23 16:42:05 +0530986 return (NFA_STATUS_FAILED);
nxpandroid8f6d0532017-07-12 18:25:30 +0530987 }
988
989 if ((p_msg = (tNFA_P2P_API_SET_LLCP_CFG*)GKI_getbuf(
990 sizeof(tNFA_P2P_API_SET_LLCP_CFG))) != NULL) {
991 p_msg->hdr.event = NFA_P2P_API_SET_LLCP_CFG_EVT;
992
993 p_msg->link_miu = link_miu;
994 p_msg->opt = opt;
995 p_msg->wt = wt;
996 p_msg->link_timeout = link_timeout;
997 p_msg->inact_timeout_init = inact_timeout_init;
998 p_msg->inact_timeout_target = inact_timeout_target;
999 p_msg->symm_delay = symm_delay;
1000 p_msg->data_link_timeout = data_link_timeout;
1001 p_msg->delay_first_pdu_timeout = delay_first_pdu_timeout;
1002
1003 nfa_sys_sendmsg(p_msg);
1004
1005 return (NFA_STATUS_OK);
1006 }
1007
1008 return (NFA_STATUS_FAILED);
nxpandroidc7611652015-09-23 16:42:05 +05301009}
1010
1011/*******************************************************************************
1012**
1013** Function NFA_P2pGetLLCPConfig
1014**
1015** Description This function is called to read LLCP config parameters.
1016**
1017** Parameters descriptions
1018** - Local Link MIU
1019** - Option parameter
1020** - Response Waiting Time Index
1021** - Local Link Timeout
1022** - Inactivity Timeout as initiator role
1023** - Inactivity Timeout as target role
1024** - Delay SYMM response
1025** - Data link connection timeout
1026** - Delay timeout to send first PDU as initiator
1027**
1028** Returns None
1029**
1030*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +05301031void NFA_P2pGetLLCPConfig(uint16_t* p_link_miu, uint8_t* p_opt, uint8_t* p_wt,
1032 uint16_t* p_link_timeout,
1033 uint16_t* p_inact_timeout_init,
1034 uint16_t* p_inact_timeout_target,
1035 uint16_t* p_symm_delay, uint16_t* p_data_link_timeout,
1036 uint16_t* p_delay_first_pdu_timeout) {
1037 LLCP_GetConfig(p_link_miu, p_opt, p_wt, p_link_timeout, p_inact_timeout_init,
1038 p_inact_timeout_target, p_symm_delay, p_data_link_timeout,
1039 p_delay_first_pdu_timeout);
nxpandroidc7611652015-09-23 16:42:05 +05301040
nxf24591c1cbeab2018-02-21 17:32:26 +05301041 DLOG_IF(INFO, nfc_debug_enabled)
1042 << StringPrintf("link_miu:%d, opt:0x%02X, wt:%d, link_timeout:%d",
1043 *p_link_miu, *p_opt, *p_wt, *p_link_timeout);
1044 DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(
nxpandroid8f6d0532017-07-12 18:25:30 +05301045 " inact_timeout(init:%d, target:%d), "
1046 "symm_delay:%d, data_link_timeout:%d",
1047 *p_inact_timeout_init, *p_inact_timeout_target, *p_symm_delay,
1048 *p_data_link_timeout);
nxf24591c1cbeab2018-02-21 17:32:26 +05301049 DLOG_IF(INFO, nfc_debug_enabled)
1050 << StringPrintf(" delay_first_pdu_timeout:%d",
1051 *p_delay_first_pdu_timeout);
nxpandroidc7611652015-09-23 16:42:05 +05301052}