xref: /haiku/src/servers/bluetooth/LocalDeviceHandler.cpp (revision b8ded2f89783a220c7b3019d48266a682cc79158)
1 /*
2  * Copyright 2007-2009 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 #include "LocalDeviceHandler.h"
8 
9 
LocalDeviceHandler(HCIDelegate * hd)10 LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd)
11 {
12 	fHCIDelegate = hd;
13 	fProperties = new BMessage();
14 }
15 
16 
~LocalDeviceHandler()17 LocalDeviceHandler::~LocalDeviceHandler()
18 {
19 	delete fHCIDelegate;
20 	delete fProperties;
21 }
22 
23 
24 hci_id
GetID()25 LocalDeviceHandler::GetID()
26 {
27 	return fHCIDelegate->Id();
28 }
29 
30 
31 status_t
Launch(void)32 LocalDeviceHandler::Launch(void)
33 {
34 	return fHCIDelegate->Launch();
35 }
36 
37 
38 bool
Available()39 LocalDeviceHandler::Available()
40 {
41 
42 	return true;
43 }
44 
45 
46 void
Acquire(void)47 LocalDeviceHandler::Acquire(void)
48 {
49 
50 }
51 
52 
53 bool
IsPropertyAvailable(const char * property)54 LocalDeviceHandler::IsPropertyAvailable(const char* property)
55 {
56 	type_code typeFound;
57 	int32 countFound;
58 
59 	return (fProperties->GetInfo(property, &typeFound, &countFound) == B_OK );
60 }
61 
62 
63 void
AddWantedEvent(BMessage * msg)64 LocalDeviceHandler::AddWantedEvent(BMessage* msg)
65 {
66 	fEventsWanted.Lock();
67 	// TODO: review why it is needed to replicate the msg
68 //	printf("Adding request... %p\n", msg);
69 	fEventsWanted.AddMessage(msg);
70 	fEventsWanted.Unlock();
71 }
72 
73 
74 void
ClearWantedEvent(BMessage * msg)75 LocalDeviceHandler::ClearWantedEvent(BMessage* msg)
76 {
77 	fEventsWanted.Lock();
78 	fEventsWanted.RemoveMessage(msg);
79 	fEventsWanted.Unlock();
80 }
81 
82 
83 void
ClearWantedEvent(BMessage * msg,uint16 event,uint16 opcode)84 LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode)
85 {
86 	// Remove the whole petition from queue
87 	fEventsWanted.Lock();
88 
89 	int16 eventFound;
90 	int16 opcodeFound;
91 	int32 eventIndex = 0;
92 
93 	// for each Event
94 	while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK) {
95 
96 		printf("%s:Event expected %d@%" B_PRId32 "...\n", __FUNCTION__, event,
97 			eventIndex);
98 
99 		if (eventFound == event) {
100 
101 			printf("%s:Event matches@%" B_PRId32 "\n", __FUNCTION__, eventIndex);
102 			// there is an opcode specified
103 			if (opcode != 0) {
104 
105 				// The opcode matches
106 				if ((msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK)
107 					&& ((uint16)opcodeFound == opcode)) {
108 
109 					// this should remove only the entry
110 					printf("Removed event %#x and opcode %d from request %p\n",
111 						event, opcode, msg);
112 					(void)msg->RemoveData("eventExpected", eventIndex);
113 					(void)msg->RemoveData("opcodeExpected", eventIndex);
114 					goto finish;
115 				}
116 
117 			} else {
118 				// Event matches so far
119 				printf("Removed event %d from message %p\n", event, msg);
120 				(void)msg->RemoveData("eventExpected", eventIndex);
121 				goto finish;
122 			}
123 
124 		}
125 		eventIndex++;
126 	}
127 	printf("%s:Nothing Found/Removed\n", __FUNCTION__);
128 
129 finish:
130 	fEventsWanted.Unlock();
131 
132 }
133 
134 
135 BMessage*
FindPetition(uint16 event,uint16 opcode,int32 * indexFound)136 LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound)
137 {
138 	//debug data
139 	int16 eventFound;
140 	int16 opcodeFound;
141 	int32 eventIndex;
142 
143 	fEventsWanted.Lock();
144 	// for each Petition
145 	for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) {
146 		BMessage* msg = fEventsWanted.FindMessage(index);
147 //		printf("%s:Petition %ld ... of %ld msg #%p\n", __FUNCTION__, index,
148 //			fEventsWanted.CountMessages(), msg);
149 //		msg->PrintToStream();
150 		eventIndex = 0;
151 
152 		// for each Event
153 		while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) {
154 			if (eventFound == event) {
155 
156 //				printf("%s:Event %d found@%ld...", __FUNCTION__, event, eventIndex);
157 				// there is an opcode specified..
158 				if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound)
159 					== B_OK) {
160 					// ensure the opcode
161 					if ((uint16)opcodeFound != opcode) {
162 //						printf("%s:opcode does not match %d\n",
163 //							__FUNCTION__, opcode);
164 						eventIndex++;
165 						continue;
166 					}
167 //					printf("Opcode matches %d\n", opcode);
168 				} else {
169 //					printf("No opcode specified\n");
170 				}
171 
172 				fEventsWanted.Unlock();
173 				if (indexFound != NULL)
174 					*indexFound = eventIndex;
175 				return msg;
176 			}
177 			eventIndex++;
178 		}
179 	}
180 //	printf("%s:Event %d not found\n", __FUNCTION__, event);
181 
182 	fEventsWanted.Unlock();
183 	return NULL;
184 
185 }
186