1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "avctp_br.h"
16 #include "avctp_br_act.h"
17 #include "avctp_dev.h"
18 #include "avctp_st.h"
19 #include "log.h"
20 
21 /*****************************************************************************
22  * Type  Define
23  ****************************************************************************/
24 /* type for state table action functions */
25 typedef uint16_t (*AvctBrAction_t)(AvctCbDev *cbDev, const AvctEvtData *data);
26 
27 /*****************************************************************************
28  * Globle Data  Define
29  ****************************************************************************/
30 const AvctBrAction_t C_AVCT_BR_ACTION[] = {
31     AvctCbBrChConn,         /* AVCT_CHANNEL_CONN */
32     AvctCbBrChBind,         /* AVCT_CHANNEL_BIND */
33     AvctCbBrChBindFail,     /* AVCT_CHANNEL_BIND_FAIL */
34     AvctCbBrChDisconn,      /* AVCT_CHANNEL_DISCONNECT */
35     AvctCbBrChUnbind,       /* AVCT_CHANNEL_UNBIND */
36     AvctCbBrChCheckDisconn, /* AVCT_CHANNEL_CHECK_DISCONNECT */
37     AvctCbBrSendMsg,        /* AVCT_SEND_MSG */
38     AvctCbBrDiscardMsg,     /* AVCT_DISCARD_MSG */
39     AvctCbBrChConnInd,      /* AVCT_CONN_IND */
40     AvctCbBrChConnFail,     /* AVCT_CONN_FAIL */
41     AvctCbBrChCloseInd,     /* AVCT_DISCONN_IND */
42     AvctCbBrChCloseCfm,     /* AVCT_DISCONN_CFM */
43     AvctCbBrRevMsg,         /* AVCT_REV_MSG */
44     AvctCbBrDiscardRevMsg,  /* AVCT_DISCARD_REV_MSG */
45     AvctCbBrChBusyInd,      /* AVCT_BUSY_IND */
46 };
47 /*****************************************************************************
48  * Function
49  ****************************************************************************/
AvctCbBrEvt(AvctCbDev * cbDev,uint8_t event,const AvctEvtData * data)50 uint16_t AvctCbBrEvt(AvctCbDev *cbDev, uint8_t event, const AvctEvtData *data)
51 {
52     LOG_DEBUG("[AVCT] %{public}s: EventID(%hhu) curState(%hhu)", __func__, event, cbDev->cbBr->stState);
53     uint16_t ret = AVCT_SUCCESS;
54     AvctCbCh *cbBr = cbDev->cbBr;
55     int curSt = cbBr->stState;
56     uint8_t nextSt = AvctGetNextStation(curSt, event);
57     uint8_t action = AvctGetEvtAction(curSt, event);
58     /* Set next state */
59     cbBr->stState = nextSt;
60 
61     /* excute actoin */
62     if (action != AVCT_IGNORE) {
63         ret = (*C_AVCT_BR_ACTION[action])(cbDev, data);
64     }
65     return ret;
66 }
67 
68 /*
69  * Function     AvctBrConnectInitiate
70  * Description  Browser Connect for initiate role
71  * Param[out]  *cbConn    connection
72  * Param[in]   *connParam  oint to the info of the connection request.
73  *              Such as the Role、Profile ID、Callback func point.
74  * Param[in]   peerAddr  The peer address to be connected.
75  * Return      AVCT_SUCCESS if successful, otherwise error.
76  */
AvctBrConnectInitiate(AvctCbConn * cbConn)77 uint16_t AvctBrConnectInitiate(AvctCbConn *cbConn)
78 {
79     uint16_t ret = AVCT_SUCCESS;
80     AvctCbCh *cbBr = NULL;
81     /* check whether the ctrl channel has been connected. */
82     if (cbConn->cbDev->cbCtrl == NULL) {
83         LOG_ERROR("[AVCT]The ctrl connection is not open!");
84         ret = AVCT_ERR_CONN_NOT_OPEN;
85     } else if (cbConn->cbDev->chLink & AVCT_ALLOC_BR) {
86         LOG_ERROR("[AVCT]The br connection is already opened!");
87         ret = AVCT_ERR_CONN_BAD;
88     } else {
89         /* Alloc CbBr memory */
90         if (cbConn->cbDev->cbBr == NULL) {
91             cbBr = AvctCbChAlloc();
92             if (cbBr == NULL) {
93                 LOG_ERROR("[AVCT]AvctCbChAlloc Failed!");
94                 ret = AVCT_ERR_NO_RESOURCES;
95             }
96         }
97     }
98     if (ret == AVCT_SUCCESS && cbBr != NULL) {
99         /* create br channel */
100         AvctEvtData evtData;
101         cbConn->cbDev->cbBr = cbBr;
102         cbConn->cbDev->chLink |= AVCT_ALLOC_BR;
103         cbConn->cbDev->cbBr->ia = AVCT_INIT;
104         evtData.cbConn = cbConn;
105         ret = AvctCbBrEvt(cbConn->cbDev, AVCT_BIND_EVT, &evtData);
106     }
107     return ret;
108 }