1/*
2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
6
7
8#include "h2util.h"
9
10#include <malloc.h>
11
12#include <bluetooth/HCI/btHCI_acl.h>
13#include <bluetooth/HCI/btHCI_command.h>
14#include <bluetooth/HCI/btHCI_event.h>
15#include <bluetooth/HCI/btHCI_sco.h>
16
17#include "h2debug.h"
18#include "h2upper.h"
19#include "h2transactions.h"
20
21
22void*
23nb_get_whole_buffer(net_buffer* nbuf)
24{
25	void* conPointer;
26	status_t err;
27#if 0
28	// the job could be already done
29	// !!! it could be trash from other upper protocols...
30	if (nbuf->COOKIEFIELD != NULL)
31		return (void*)nbuf->COOKIEFIELD;
32#endif
33	err = nb->direct_access(nbuf, 0, nbuf->size, &conPointer);
34
35	if (err != B_OK) {
36		panic("expected to be contiguous");
37		#if 0
38		// We are gonna need a realocation
39		nbuf->COOKIEFIELD = (uint32) malloc(nbuf->size);
40		if (nbuf->COOKIEFIELD == NULL)
41			goto fail;
42
43		err = nb->write(nbuf, 0, (void*) nbuf->COOKIEFIELD, nbuf->size);
44		if (err != B_OK)
45			goto free;
46
47		conPointer = (void*)nbuf->COOKIEFIELD;
48		#endif
49	}
50
51	return conPointer;
52#if 0
53free:
54	free((void*) nbuf->COOKIEFIELD);
55fail:
56	return NULL;
57#endif
58}
59
60
61void
62nb_destroy(net_buffer* nbuf)
63{
64	if (nbuf == NULL)
65		return;
66#if 0
67	// Free possible allocated
68	if (nbuf->COOKIEFIELD != NULL)
69		free((void*)nbuf->COOKIEFIELD);
70#endif
71	// TODO check for survivers...
72	if (nb != NULL)
73		nb->free(nbuf);
74
75}
76
77
78// Extract the expected size of the packet
79// TODO: This might be inefficient as at the moment of the creation
80// of the net_buffer this information is known and it could be stored
81#if 0
82ssize_t
83get_expected_size(net_buffer* nbuf)
84{
85
86	if (nbuf == NULL)
87		panic("Analizing NULL packet");
88
89	switch (nbuf->protocol) {
90
91		case BT_COMMAND: {
92			struct hci_command_header* header = nb_get_whole_buffer(nbuf);
93			return header->clen + sizeof(struct hci_command_header);
94		}
95
96		case BT_EVENT: {
97			struct hci_event_header* header = nb_get_whole_buffer(nbuf);
98			return header->elen + sizeof(struct hci_event_header);
99		}
100
101		case BT_ACL: {
102			struct hci_acl_header* header = nb_get_whole_buffer(nbuf);
103			return header->alen + sizeof(struct hci_acl_header);
104		}
105
106		case BT_SCO: {
107			struct hci_sco_header* header = nb_get_whole_buffer(nbuf);
108			return header->slen + sizeof(struct hci_sco_header);
109		}
110
111		default:
112			panic(BLUETOOTH_DEVICE_DEVFS_NAME ":no protocol specified for ");
113		break;
114	}
115
116	return -1;
117}
118#endif
119
120
121#if 0
122#pragma mark - room util -
123#endif
124
125
126void
127init_room(struct list* l)
128{
129	list_init(l);
130}
131
132
133void*
134alloc_room(struct list* l, size_t size)
135{
136
137	void* item = list_get_first_item(l);
138
139	if (item == NULL)
140		item = (void*) malloc(size);
141
142	return item;
143
144}
145
146
147void
148reuse_room(struct list* l, void* room)
149{
150	list_add_item(l, room);
151}
152
153
154void
155purge_room(struct list* l)
156{
157	void* item;
158
159	while ((item = list_remove_head_item(l)) != NULL) {
160		free(item);
161	}
162}
163