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 
16 #include "gap_internal.h"
17 #include "gap_task_internal.h"
18 
19 #include <string.h>
20 
21 #include "allocator.h"
22 #include "log.h"
23 
24 typedef struct {
25     uint16_t aclHandle;
26     uint8_t id;
27     L2capLeConnectionParameter param;
28     void *ctx;
29 } GapReceiveL2capParameterUpdateReqParam;
30 
31 typedef struct {
32     uint16_t aclHandle;
33     uint16_t result;
34     void *ctx;
35 } GapReceiveL2capParameterUpdateRspParam;
36 
GapReceiveL2capParameterUpdateReqTask(void * ctx)37 static void GapReceiveL2capParameterUpdateReqTask(void *ctx)
38 {
39     GapReceiveL2capParameterUpdateReqParam *param = ctx;
40     GapReceiveL2capParameterUpdateReq(param->aclHandle, param->id, &param->param, param->ctx);
41 }
42 
GapRecvL2capParameterUpdateReq(uint16_t aclHandle,uint8_t id,const L2capLeConnectionParameter * param,void * ctx)43 static void GapRecvL2capParameterUpdateReq(
44     uint16_t aclHandle, uint8_t id, const L2capLeConnectionParameter *param, void *ctx)
45 {
46     LOG_INFO("%{public}s: handle:0x%04x", __FUNCTION__, aclHandle);
47     GapReceiveL2capParameterUpdateReqParam *l2capParam =
48         MEM_MALLOC.alloc(sizeof(GapReceiveL2capParameterUpdateReqParam));
49     if (l2capParam == NULL) {
50         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
51         return;
52     }
53 
54     l2capParam->aclHandle = aclHandle;
55     l2capParam->id = id;
56     l2capParam->param = *param;
57     l2capParam->ctx = ctx;
58 
59     int ret = GapRunTaskUnBlockProcess(GapReceiveL2capParameterUpdateReqTask, l2capParam, NULL);
60     if (ret != BT_SUCCESS) {
61         LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
62     }
63 }
64 
GapReceiveL2capParameterUpdateRspTask(void * ctx)65 static void GapReceiveL2capParameterUpdateRspTask(void *ctx)
66 {
67     GapReceiveL2capParameterUpdateRspParam *param = ctx;
68     GapReceiveL2capParameterUpdateRsp(param->aclHandle, param->result, param->ctx);
69 }
70 
GapRecvL2capParameterUpdateRsp(uint16_t aclHandle,uint16_t result,void * ctx)71 static void GapRecvL2capParameterUpdateRsp(uint16_t aclHandle, uint16_t result, void *ctx)
72 {
73     LOG_INFO("%{public}s: handle:0x%04x result:0x%04x", __FUNCTION__, aclHandle, result);
74     GapReceiveL2capParameterUpdateRspParam *l2capParam =
75         MEM_MALLOC.alloc(sizeof(GapReceiveL2capParameterUpdateRspParam));
76     if (l2capParam == NULL) {
77         LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
78         return;
79     }
80 
81     l2capParam->aclHandle = aclHandle;
82     l2capParam->result = result;
83     l2capParam->ctx = ctx;
84 
85     int ret = GapRunTaskUnBlockProcess(GapReceiveL2capParameterUpdateRspTask, l2capParam, NULL);
86     if (ret != BT_SUCCESS) {
87         LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
88     }
89 }
90 
91 static L2capLeConnectionParameterUpdate g_l2capParamUpdate = {
92     .recvLeConnectionParameterUpdateReq = GapRecvL2capParameterUpdateReq,
93     .recvLeConnectionParameterUpdateRsp = GapRecvL2capParameterUpdateRsp,
94 };
95 
GapRegisterL2capCallbacks(void)96 void GapRegisterL2capCallbacks(void)
97 {
98     L2CIF_LeRegisterConnectionParameterUpdate(&g_l2capParamUpdate, NULL);
99 }
100 
GapDeregisterL2capCallbacks(void)101 void GapDeregisterL2capCallbacks(void)
102 {
103     L2CIF_LeDeregisterConnectionParameterUpdate();
104 }
105 
GapLeConnectionParameterUpdateReqResult(uint16_t aclHandle,int result)106 void GapLeConnectionParameterUpdateReqResult(uint16_t aclHandle, int result)
107 {
108     if (result != BT_SUCCESS) {
109         LOG_ERROR("%{public}s: aclHandle:0x%04x result:%{public}d", __FUNCTION__, aclHandle, result);
110     }
111 }
112 
GapLeConnectionParameterUpdateRspResult(uint16_t aclHandle,int result)113 void GapLeConnectionParameterUpdateRspResult(uint16_t aclHandle, int result)
114 {
115     if (result != BT_SUCCESS) {
116         LOG_ERROR("%{public}s: aclHandle:0x%04x result:%{public}d", __FUNCTION__, aclHandle, result);
117     }
118 }
119 
GapLeConnectionParameterUpdateReq(uint16_t aclHandle,const L2capLeConnectionParameter * param)120 void GapLeConnectionParameterUpdateReq(uint16_t aclHandle, const L2capLeConnectionParameter *param)
121 {
122     L2CIF_LeConnectionParameterUpdateReq(aclHandle, param, GapLeConnectionParameterUpdateReqResult);
123 }
124 
GapLeConnectionParameterUpdateRsp(uint16_t aclHandle,uint8_t id,uint16_t result)125 void GapLeConnectionParameterUpdateRsp(uint16_t aclHandle, uint8_t id, uint16_t result)
126 {
127     L2CIF_LeConnectionParameterUpdateRsp(aclHandle, id, result, GapLeConnectionParameterUpdateRspResult);
128 }