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 */
5
6#include "FrameInterface.h"
7
8#include <btDebug.h>
9
10#include <lock.h>
11
12
13L2capFrame*
14SignalByIdent(HciConnection* conn, uint8 ident)
15{
16	L2capFrame*	frame;
17
18	mutex_lock(&conn->fLockExpected);
19	DoublyLinkedList<L2capFrame>::Iterator iterator
20		= conn->ExpectedResponses.GetIterator();
21
22	while (iterator.HasNext()) {
23
24		frame = iterator.Next();
25		if (frame->type == L2CAP_C_FRAME && frame->ident == ident) {
26			mutex_unlock(&frame->conn->fLockExpected);
27			return frame;
28		}
29	}
30
31	mutex_unlock(&conn->fLockExpected);
32
33	return NULL;
34}
35
36
37status_t
38TimeoutSignal(L2capFrame* frame, uint32 timeo)
39{
40	if (frame != NULL)
41		return B_OK;
42
43	return B_ERROR;
44}
45
46
47status_t
48unTimeoutSignal(L2capFrame* frame)
49{
50	if (frame != NULL)
51		return B_OK;
52
53	return B_ERROR;
54}
55
56
57L2capFrame*
58SpawmFrame(HciConnection* conn, L2capChannel* channel, net_buffer* buffer,
59	frame_type type)
60{
61	if (buffer == NULL)
62		panic("Null Buffer to outgoing queue");
63
64	L2capFrame* frame = new (std::nothrow) L2capFrame;
65
66	frame->conn = conn;
67	frame->channel = channel; // TODO: maybe only scid needed
68
69	frame->buffer = buffer;
70	frame->type = type;
71
72	mutex_lock(&conn->fLock);
73
74	conn->OutGoingFrames.Add(frame, true);
75
76	mutex_unlock(&conn->fLock);
77
78	return frame;
79}
80
81
82L2capFrame*
83SpawmSignal(HciConnection* conn, L2capChannel* channel, net_buffer* buffer,
84	uint8 ident, uint8 code)
85{
86	if (buffer == NULL)
87		panic("Null Buffer to outgoing queue");
88
89	L2capFrame* frame = new (std::nothrow) L2capFrame;
90
91	frame->conn = conn;
92	frame->channel = channel; // TODO: not specific descriptor should be required
93
94	frame->buffer = buffer;
95	frame->type = L2CAP_C_FRAME;
96	frame->ident = ident;
97	frame->code = code;
98
99	mutex_lock(&conn->fLock);
100
101	conn->OutGoingFrames.Add(frame, true);
102
103	mutex_unlock(&conn->fLock);
104
105	return frame;
106}
107
108
109status_t
110AcknowledgeSignal(L2capFrame* frame)
111{
112
113	if (frame != NULL) {
114
115		if (frame->type == L2CAP_C_FRAME) {
116			HciConnection* connection = frame->conn;
117
118			unTimeoutSignal(frame);
119			mutex_lock(&connection->fLockExpected);
120			connection->ExpectedResponses.Remove(frame);
121			mutex_unlock(&connection->fLockExpected);
122		}
123
124		// NO! This will be deleted by lower layers while being sent!
125		// gBufferModule->free(frame->buffer);
126		delete frame;
127
128		return B_OK;
129	}
130
131	return B_ERROR;
132}
133
134
135status_t
136QueueSignal(L2capFrame* frame)
137{
138	if (frame != NULL) {
139
140		if (frame->type == L2CAP_C_FRAME) {
141			mutex_lock(&frame->conn->fLockExpected);
142			frame->conn->ExpectedResponses.Add(frame);
143			mutex_unlock(&frame->conn->fLockExpected);
144			return B_OK;
145		}
146	}
147
148	return B_ERROR;
149}
150