1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "ble_hs_priv.h"
21
ble_hs_atomic_conn_delete(uint16_t conn_handle)22 int ble_hs_atomic_conn_delete(uint16_t conn_handle)
23 {
24 struct ble_hs_conn *conn;
25 ble_hs_lock();
26 conn = ble_hs_conn_find(conn_handle);
27 if (conn != NULL) {
28 ble_hs_conn_remove(conn);
29 #if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER)
30
31 if (conn->psync) {
32 ble_hs_periodic_sync_free(conn->psync);
33 }
34
35 #endif
36 ble_hs_conn_free(conn);
37 }
38
39 ble_hs_unlock();
40 return conn != NULL ? 0 : BLE_HS_ENOTCONN;
41 }
42
ble_hs_atomic_conn_insert(struct ble_hs_conn * conn)43 void ble_hs_atomic_conn_insert(struct ble_hs_conn *conn)
44 {
45 ble_hs_lock();
46 ble_hs_conn_insert(conn);
47 ble_hs_unlock();
48 }
49
ble_hs_atomic_conn_flags(uint16_t conn_handle,ble_hs_conn_flags_t * out_flags)50 int ble_hs_atomic_conn_flags(uint16_t conn_handle, ble_hs_conn_flags_t *out_flags)
51 {
52 struct ble_hs_conn *conn;
53 int rc;
54 ble_hs_lock();
55 conn = ble_hs_conn_find(conn_handle);
56 if (conn == NULL) {
57 rc = BLE_HS_ENOTCONN;
58 } else {
59 rc = 0;
60
61 if (out_flags != NULL) {
62 *out_flags = conn->bhc_flags;
63 }
64 }
65
66 ble_hs_unlock();
67 return rc;
68 }
69
ble_hs_atomic_conn_set_flags(uint16_t conn_handle,ble_hs_conn_flags_t flags,int on)70 int ble_hs_atomic_conn_set_flags(uint16_t conn_handle, ble_hs_conn_flags_t flags, int on)
71 {
72 struct ble_hs_conn *conn;
73 int rc;
74 ble_hs_lock();
75 conn = ble_hs_conn_find(conn_handle);
76 if (conn == NULL) {
77 rc = BLE_HS_ENOTCONN;
78 } else {
79 rc = 0;
80
81 if (on) {
82 conn->bhc_flags |= flags;
83 } else {
84 conn->bhc_flags &= ~flags;
85 }
86 }
87
88 ble_hs_unlock();
89 return rc;
90 }
91
ble_hs_atomic_first_conn_handle(void)92 uint16_t ble_hs_atomic_first_conn_handle(void)
93 {
94 const struct ble_hs_conn *conn;
95 uint16_t conn_handle;
96 ble_hs_lock();
97 conn = ble_hs_conn_first();
98 if (conn != NULL) {
99 conn_handle = conn->bhc_handle;
100 } else {
101 conn_handle = BLE_HS_CONN_HANDLE_NONE;
102 }
103
104 ble_hs_unlock();
105 return conn_handle;
106 }