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_int.h"
16 #include "avctp_dev.h"
17 #include "avctp_ctrl_l2cap.h"
18 #include "avctp_br_l2cap.h"
19 #include "securec.h"
20 #include "log.h"
21 #include "event.h"
22 #include "btstack.h"
23 #include "btm/btm_thread.h"
24 #include "module.h"
25
26 /*****************************************************************************
27 * Globle Data Define
28 ****************************************************************************/
29 /* Manager block for AVCT */
30 AvctChMng g_avctMng;
31
32 void AvctStartup(void *context);
33 void AvctShutdown(void *context);
34 /*****************************************************************************
35 * Function
36 ****************************************************************************/
37 /*
38 * Function AVCT_Init
39 * Description This function is called to start avctp.
40 * Param[in] void.
41 * Return void.
42 */
AVCT_Init()43 static void AVCT_Init()
44 {
45 LOG_INFO("[AVCT] %{public}s", __func__);
46 return;
47 }
48
49 /*
50 * Function AVCT_Cleanup
51 * Description This function is called to stop avctp.
52 * Param[in] void
53 * Return void
54 */
AVCT_Cleanup()55 static void AVCT_Cleanup()
56 {
57 LOG_INFO("[AVCT] %{public}s", __func__);
58 return;
59 }
60
AvctStartup(void * context)61 void AvctStartup(void *context)
62 {
63 Event *event = (Event *)context;
64 /* Initialize channel manager block */
65 (void)memset_s(&g_avctMng, sizeof(AvctChMng), 0, sizeof(AvctChMng));
66 /* regist psm */
67 if (L2CIF_RegisterService(AVCT_PSM, (L2capService *)&G_AVCT_L2C_SVR, NULL, NULL)) {
68 LOG_ERROR("[AVCT] %{public}s:L2CIF_RegisterService AVCT_PSM failed.", __func__);
69 }
70 /* regist browser psm */
71 if (L2CIF_RegisterService(AVCT_BR_PSM, (L2capService *)&G_AVCT_BR_L2C_SVR, NULL, NULL)) {
72 LOG_ERROR("[AVCT] %{public}s:L2CIF_RegisterService AVCT_BR_PSM failed.", __func__);
73 }
74 EventSet(event);
75 }
76
AVCT_Startup()77 static void AVCT_Startup()
78 {
79 LOG_INFO("[AVCT] %{public}s", __func__);
80 /* Create queue resource */
81 if (BTM_CreateProcessingQueue(PROCESSING_QUEUE_ID_AVCTP, BTM_PROCESSING_QUEUE_SIZE_DEFAULT)) {
82 return;
83 }
84 Event *event = EventCreate(true);
85 if (!AvctAsyncProcess(AvctStartup, event)) {
86 EventWait(event, WAIT_TIME);
87 }
88 EventDelete(event);
89 }
90
AvctShutdown(void * context)91 void AvctShutdown(void *context)
92 {
93 LOG_INFO("[AVCT] %{public}s", __func__);
94 Event *event = (Event *)context;
95 L2CIF_DeregisterService(AVCT_PSM, NULL);
96 L2CIF_DeregisterService(AVCT_BR_PSM, NULL);
97 /* Delete queue resource */
98 BTM_DeleteProcessingQueue(PROCESSING_QUEUE_ID_AVCTP);
99 /* Release device resource */
100 for (uint8_t i = 0; i < AVCT_MAX_DEVICES; i++) {
101 AvctCbDevDealloc(&g_avctMng.cbDev[i]);
102 }
103 EventSet(event);
104 }
105
AVCT_Shutdown()106 static void AVCT_Shutdown()
107 {
108 LOG_INFO("[AVCT] %{public}s", __func__);
109 Event *event = EventCreate(true);
110 if (!AvctAsyncProcess(AvctShutdown, event)) {
111 EventWait(event, WAIT_TIME);
112 }
113 EventDelete(event);
114 }
115
116 static Module g_avctp = {
117 .name = MODULE_NAME_AVCTP,
118 .init = AVCT_Init,
119 .startup = AVCT_Startup,
120 .shutdown = AVCT_Shutdown,
121 .cleanup = AVCT_Cleanup,
122 .dependencies = {MODULE_NAME_L2CAP, MODULE_NAME_GAP},
123 };
124
125 MODULE_DECL(g_avctp)
126