btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / add-ons / kernel / bluetooth / btCoreData / ConnectionInterface.cpp
blobe25b537a00c8f5f411ff134d93a2be2293b5e7a1
1 /*
2 * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <util/DoublyLinkedList.h>
8 #include <KernelExport.h>
10 #include <bluetooth/bluetooth.h>
11 #include <bluetooth/bdaddrUtils.h>
13 #include <btDebug.h>
15 #include <l2cap.h>
17 #include "ConnectionInterface.h"
20 void PurgeChannels(HciConnection* conn);
23 HciConnection::HciConnection()
25 mutex_init(&fLock, "conn outgoing");
26 mutex_init(&fLockExpected, "frame expected");
30 HciConnection::~HciConnection()
32 mutex_destroy(&fLock);
33 mutex_destroy(&fLockExpected);
37 HciConnection*
38 AddConnection(uint16 handle, int type, const bdaddr_t& dst, hci_id hid)
40 // Create connection descriptor
42 HciConnection* conn = ConnectionByHandle(handle, hid);
43 if (conn != NULL)
44 goto update;
46 conn = new (std::nothrow) HciConnection;
47 if (conn == NULL)
48 goto bail;
50 // memset(conn, 0, sizeof(HciConnection));
52 conn->currentRxPacket = NULL;
53 conn->currentRxExpectedLength = 0;
54 update:
55 // fill values
56 bdaddrUtils::Copy(conn->destination, dst);
57 conn->type = type;
58 conn->handle = handle;
59 conn->Hid = hid;
60 conn->status = HCI_CONN_OPEN;
61 conn->mtu = L2CAP_MTU_MINIMUM; // TODO: give the mtu to the connection
62 conn->lastCid = L2CAP_FIRST_CID;
63 conn->lastIdent = L2CAP_FIRST_IDENT;
65 sConnectionList.Add(conn);
67 bail:
68 return conn;
72 status_t
73 RemoveConnection(const bdaddr_t& destination, hci_id hid)
75 HciConnection* conn;
77 DoublyLinkedList<HciConnection>::Iterator iterator
78 = sConnectionList.GetIterator();
80 while (iterator.HasNext()) {
82 conn = iterator.Next();
83 if (conn->Hid == hid
84 && bdaddrUtils::Compare(conn->destination, destination)) {
86 // if the device is still part of the list, remove it
87 if (conn->GetDoublyLinkedListLink()->next != NULL
88 || conn->GetDoublyLinkedListLink()->previous != NULL
89 || conn == sConnectionList.Head()) {
90 sConnectionList.Remove(conn);
92 delete conn;
93 return B_OK;
97 return B_ERROR;
101 status_t
102 RemoveConnection(uint16 handle, hci_id hid)
104 HciConnection* conn;
106 DoublyLinkedList<HciConnection>::Iterator iterator
107 = sConnectionList.GetIterator();
108 while (iterator.HasNext()) {
110 conn = iterator.Next();
111 if (conn->Hid == hid && conn->handle == handle) {
113 // if the device is still part of the list, remove it
114 if (conn->GetDoublyLinkedListLink()->next != NULL
115 || conn->GetDoublyLinkedListLink()->previous != NULL
116 || conn == sConnectionList.Head()) {
117 sConnectionList.Remove(conn);
119 PurgeChannels(conn);
120 delete conn;
121 return B_OK;
125 return B_ERROR;
129 hci_id
130 RouteConnection(const bdaddr_t& destination) {
132 HciConnection* conn;
134 DoublyLinkedList<HciConnection>::Iterator iterator
135 = sConnectionList.GetIterator();
136 while (iterator.HasNext()) {
138 conn = iterator.Next();
139 if (bdaddrUtils::Compare(conn->destination, destination)) {
140 return conn->Hid;
144 return -1;
148 HciConnection*
149 ConnectionByHandle(uint16 handle, hci_id hid)
151 HciConnection* conn;
153 DoublyLinkedList<HciConnection>::Iterator iterator
154 = sConnectionList.GetIterator();
155 while (iterator.HasNext()) {
157 conn = iterator.Next();
158 if (conn->Hid == hid && conn->handle == handle) {
159 return conn;
163 return NULL;
167 HciConnection*
168 ConnectionByDestination(const bdaddr_t& destination, hci_id hid)
170 HciConnection* conn;
172 DoublyLinkedList<HciConnection>::Iterator iterator
173 = sConnectionList.GetIterator();
174 while (iterator.HasNext()) {
176 conn = iterator.Next();
177 if (conn->Hid == hid
178 && bdaddrUtils::Compare(conn->destination, destination)) {
179 return conn;
183 return NULL;
187 #if 0
188 #pragma mark - ACL helper funcs
189 #endif
191 void
192 SetAclBuffer(HciConnection* conn, net_buffer* nbuf)
194 conn->currentRxPacket = nbuf;
198 void
199 SetAclExpectedSize(HciConnection* conn, size_t size)
201 conn->currentRxExpectedLength = size;
205 void
206 AclPutting(HciConnection* conn, size_t size)
208 conn->currentRxExpectedLength -= size;
212 bool
213 AclComplete(HciConnection* conn)
215 return conn->currentRxExpectedLength == 0;
219 bool
220 AclOverFlowed(HciConnection* conn)
222 return conn->currentRxExpectedLength < 0;
226 #if 0
227 #pragma mark - private funcs
228 #endif
230 void
231 PurgeChannels(HciConnection* conn)