xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/src/ble_hs_atomic.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 
22 int
ble_hs_atomic_conn_delete(uint16_t conn_handle)23 ble_hs_atomic_conn_delete(uint16_t conn_handle)
24 {
25     struct ble_hs_conn *conn;
26 
27     ble_hs_lock();
28     conn = ble_hs_conn_find(conn_handle);
29     if (conn != NULL) {
30         ble_hs_conn_remove(conn);
31         ble_hs_conn_free(conn);
32 
33     }
34     ble_hs_unlock();
35 
36     return conn != NULL ? 0 : BLE_HS_ENOTCONN;
37 }
38 
39 void
ble_hs_atomic_conn_insert(struct ble_hs_conn * conn)40 ble_hs_atomic_conn_insert(struct ble_hs_conn *conn)
41 {
42     ble_hs_lock();
43     ble_hs_conn_insert(conn);
44     ble_hs_unlock();
45 }
46 
47 int
ble_hs_atomic_conn_flags(uint16_t conn_handle,ble_hs_conn_flags_t * out_flags)48 ble_hs_atomic_conn_flags(uint16_t conn_handle, ble_hs_conn_flags_t *out_flags)
49 {
50     struct ble_hs_conn *conn;
51     int rc;
52 
53     ble_hs_lock();
54 
55     conn = ble_hs_conn_find(conn_handle);
56     if (conn == NULL) {
57         rc = BLE_HS_ENOTCONN;
58     } else {
59         rc = 0;
60         if (out_flags != NULL) {
61             *out_flags = conn->bhc_flags;
62         }
63     }
64 
65     ble_hs_unlock();
66 
67     return rc;
68 }
69 
70 int
ble_hs_atomic_conn_set_flags(uint16_t conn_handle,ble_hs_conn_flags_t flags,int on)71 ble_hs_atomic_conn_set_flags(uint16_t conn_handle, ble_hs_conn_flags_t flags,
72                              int on)
73 {
74     struct ble_hs_conn *conn;
75     int rc;
76 
77     ble_hs_lock();
78 
79     conn = ble_hs_conn_find(conn_handle);
80     if (conn == NULL) {
81         rc = BLE_HS_ENOTCONN;
82     } else {
83         rc = 0;
84 
85         if (on) {
86             conn->bhc_flags |= flags;
87         } else {
88             conn->bhc_flags &= ~flags;
89         }
90     }
91 
92     ble_hs_unlock();
93 
94     return rc;
95 }
96 
97 uint16_t
ble_hs_atomic_first_conn_handle(void)98 ble_hs_atomic_first_conn_handle(void)
99 {
100     const struct ble_hs_conn *conn;
101     uint16_t conn_handle;
102 
103     ble_hs_lock();
104 
105     conn = ble_hs_conn_first();
106     if (conn != NULL) {
107         conn_handle = conn->bhc_handle;
108     } else {
109         conn_handle = BLE_HS_CONN_HANDLE_NONE;
110     }
111 
112     ble_hs_unlock();
113 
114     return conn_handle;
115 }
116