- Remove keeping list of connections per device

- Start to move some common code from the driver layer (not yet used)
- Style



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34332 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Ruiz Dorantes
2009-11-28 19:50:09 +00:00
parent b973d02fde
commit cf3d8a746f
@@ -15,6 +15,7 @@
#include <util/DoublyLinkedList.h> #include <util/DoublyLinkedList.h>
#include <KernelExport.h> #include <KernelExport.h>
#include <SupportDefs.h>
#include <errno.h> #include <errno.h>
@@ -32,17 +33,22 @@
#include <btDebug.h> #include <btDebug.h>
#include <btCoreData.h> #include <btCoreData.h>
#include <btModules.h> #include <btModules.h>
#include <CodeHandler.h>
#include <bluetooth/HCI/btHCI_sco.h>
#include <bluetooth/HCI/btHCI_acl.h> #include <bluetooth/HCI/btHCI_acl.h>
#include <bluetooth/HCI/btHCI_event.h>
#include <bluetooth/HCI/btHCI_command.h>
#include "acl.h" #include "acl.h"
struct bluetooth_device : net_device, DoublyLinkedListLinkImpl<bluetooth_device> { struct bluetooth_device : net_device, DoublyLinkedListLinkImpl<bluetooth_device> {
DoublyLinkedList<HciConnection> sConnectionList; net_buffer* fBuffersRx[HCI_NUM_PACKET_TYPES];
size_t fExpectedPacketSize[HCI_NUM_PACKET_TYPES];
int fd; int fd;
uint16 frame_size;
uint16 mtu; uint16 mtu;
}; };
@@ -56,6 +62,9 @@ static mutex sListLock;
static DoublyLinkedList<bluetooth_device> sDeviceList; static DoublyLinkedList<bluetooth_device> sDeviceList;
static sem_id sLinkChangeSemaphore; static sem_id sLinkChangeSemaphore;
// forward declarations
status_t bluetooth_receive_data(net_device* _device, net_buffer** _buffer);
bluetooth_device* bluetooth_device*
FindDeviceByID(hci_id hid) FindDeviceByID(hci_id hid)
@@ -73,32 +82,124 @@ FindDeviceByID(hci_id hid)
return NULL; return NULL;
} }
// TODO: keeping this list might not be strictily needed
void
RegisterConnection(hci_id hid, uint16 handle)
{
bluetooth_device* device = FindDeviceByID(hid);
HciConnection* conn = btCoreData->ConnectionByHandle(handle, hid);
if (device != NULL && conn != NULL) { status_t
conn->ndevice = (struct net_device*)device; Assemble(net_device* netDevice, bt_packet_t type, void* data, size_t count)
device->sConnectionList.Add(conn); {
bluetooth_device* bluetoothDevice = (bluetooth_device*)netDevice;
net_buffer* nbuf = bluetoothDevice->fBuffersRx[type];
size_t currentPacketLen = 0;
while (count) {
if (nbuf == NULL) {
/* new buffer incoming */
switch (type) {
case BT_EVENT:
if (count >= HCI_EVENT_HDR_SIZE) {
struct hci_event_header* headerPkt = (struct hci_event_header*)data;
bluetoothDevice->fExpectedPacketSize[type] = HCI_EVENT_HDR_SIZE + headerPkt->elen;
if (count > bluetoothDevice->fExpectedPacketSize[type]) {
// the whole packet is here so it can be already posted.
btCoreData->PostEvent(bluetoothDevice, data, bluetoothDevice->fExpectedPacketSize[type]);
} else { } else {
panic("problem registering connection"); bluetoothDevice->fBuffersRx[type] = nbuf =
gBufferModule->create(bluetoothDevice->fExpectedPacketSize[type]);
nbuf->protocol = type;
} }
} else {
flowf("EVENT frame corrupted\n");
return EILSEQ;
}
break;
case BT_ACL:
if (count >= HCI_ACL_HDR_SIZE) {
struct hci_acl_header* headerPkt = (struct hci_acl_header*)data;
bluetoothDevice->fExpectedPacketSize[type] = HCI_ACL_HDR_SIZE
+ B_LENDIAN_TO_HOST_INT16(headerPkt->alen);
// Create the buffer -> TODO: this allocation can fail
bluetoothDevice->fBuffersRx[type] = nbuf = gBufferModule->create(bluetoothDevice->fExpectedPacketSize[type]);
nbuf->protocol = type;
} else {
flowf("ACL frame corrupted\n");
return EILSEQ;
}
break;
case BT_SCO:
break;
default:
panic("unknown packet type in assembly");
break;
}
currentPacketLen = bluetoothDevice->fExpectedPacketSize[type];
} else {
// Continuation of a packet
currentPacketLen = bluetoothDevice->fExpectedPacketSize[type] - nbuf->size;
}
if (nbuf != NULL) {
currentPacketLen = min_c(currentPacketLen, count);
gBufferModule->append(nbuf, data, currentPacketLen);
if ((bluetoothDevice->fExpectedPacketSize[type] - nbuf->size) == 0 ) {
switch(nbuf->protocol) {
case BT_EVENT:
btCoreData->PostEvent(netDevice, data, bluetoothDevice->fExpectedPacketSize[type]);
break;
case BT_ACL:
bluetooth_receive_data(netDevice, &nbuf);
break;
default:
break;
}
bluetoothDevice->fBuffersRx[type] = nbuf = NULL;
bluetoothDevice->fExpectedPacketSize[type] = 0;
} else {
#if DEBUG_ACL
if (type == BT_ACL)
debugf("ACL Packet not filled size=%ld expected=%ld\n",
nbuf->size, bluetoothDevice->fExpectedPacketSize[type]);
#endif
}
}
/* in case in the pipe there is info about the next buffer ... */
count -= currentPacketLen;
data = (void*)((uint8*)data + currentPacketLen);
}
return B_OK;
} }
void status_t
unRegisterConnection(hci_id hid, uint16 handle) HciPacketHandler(void* data, int32 code, size_t size)
{ {
bluetooth_device* bluetoothDevice =
FindDeviceByID(Bluetooth::CodeHandler::Device(code));
bluetooth_device* device; if (bluetoothDevice != NULL)
device = FindDeviceByID(hid); return Assemble(bluetoothDevice, Bluetooth::CodeHandler::Protocol(code),
data, size);
if (device != NULL) { return B_ERROR;
device->sConnectionList.Remove(btCoreData->ConnectionByHandle(handle, hid));
}
} }
// #pragma mark - // #pragma mark -
@@ -217,7 +318,7 @@ bluetooth_send_data(net_device *_device, net_buffer* buffer)
bluetooth_device* device = (bluetooth_device*)_device; bluetooth_device* device = (bluetooth_device*)_device;
net_buffer* curr_frame = NULL; net_buffer* curr_frame = NULL;
net_buffer* next_frame = buffer; net_buffer* next_frame = buffer;
uint16 handle = buffer->type; //TODO: net_routes?? uint16 handle = buffer->type; //TODO: CodeHandler?
uint8 flag = HCI_ACL_PACKET_START; uint8 flag = HCI_ACL_PACKET_START;
debugf("index %ld try to send bt packet of %lu bytes (flags %ld):\n",device->index, buffer->size, buffer->flags); debugf("index %ld try to send bt packet of %lu bytes (flags %ld):\n",device->index, buffer->size, buffer->flags);
@@ -348,11 +449,6 @@ dump_bluetooth_devices(int argc, char** argv)
device = iterator.Next(); device = iterator.Next();
kprintf("\tname=%s index=%#lx @%p\n",device->name, device->index, device); kprintf("\tname=%s index=%#lx @%p\n",device->name, device->index, device);
DoublyLinkedList<HciConnection>::Iterator connIterator = device->sConnectionList.GetIterator();
while (connIterator.HasNext()) {
HciConnection* conn = connIterator.Next();
kprintf("\t\t handle=%#x destination=%s @%p\n", conn->handle, bdaddrUtils::ToString(conn->destination), conn);
}
} }
return 0; return 0;