xref: /haiku/src/servers/bluetooth/LocalDeviceHandler.cpp (revision 6c4a44e36ba846c54467103f884d65dfa13e7fcb)
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 
10 LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd)
11 {
12 	fHCIDelegate = hd;
13 	fProperties = new BMessage();
14 }
15 
16 
17 LocalDeviceHandler::~LocalDeviceHandler()
18 {
19 	delete fHCIDelegate;
20 	delete fProperties;
21 }
22 
23 
24 hci_id
25 LocalDeviceHandler::GetID()
26 {
27 	return fHCIDelegate->Id();
28 }
29 
30 
31 status_t
32 LocalDeviceHandler::Launch(void)
33 {
34 	return fHCIDelegate->Launch();
35 }
36 
37 
38 bool
39 LocalDeviceHandler::Available()
40 {
41 
42 	return true;
43 }
44 
45 
46 void
47 LocalDeviceHandler::Acquire(void)
48 {
49 
50 }
51 
52 
53 bool
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
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
75 LocalDeviceHandler::ClearWantedEvent(BMessage* msg)
76 {
77 	fEventsWanted.Lock();
78 	fEventsWanted.RemoveMessage(msg);
79 	fEventsWanted.Unlock();
80 }
81 
82 
83 void
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@%ld...\n", __FUNCTION__, event, eventIndex);
97 
98 		if (eventFound == event) {
99 
100 			printf("%s:Event matches@%ld", __FUNCTION__, eventIndex);
101 			// there is an opcode specified
102 			if (opcode != 0) {
103 
104 				// The opcode matches
105 				if ((msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK)
106 					&& ((uint16)opcodeFound == opcode)) {
107 
108 					// this should remove only the entry
109 					printf("Removed event %#x and opcode %d from request %p\n",
110 						event, opcode, msg);
111 					(void)msg->RemoveData("eventExpected", eventIndex);
112 					(void)msg->RemoveData("opcodeExpected", eventIndex);
113 					goto finish;
114 				}
115 
116 			} else {
117 				// Event matches so far
118 				printf("Removed event %d from message %p\n", event, msg);
119 				(void)msg->RemoveData("eventExpected", eventIndex);
120 				goto finish;
121 			}
122 
123 		}
124 		eventIndex++;
125 	}
126 	printf("%s:Nothing Found/Removed\n", __FUNCTION__);
127 
128 finish:
129 	fEventsWanted.Unlock();
130 
131 }
132 
133 
134 BMessage*
135 LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound)
136 {
137 	//debug data
138 	int16 eventFound;
139 	int16 opcodeFound;
140 	int32 eventIndex;
141 
142 	fEventsWanted.Lock();
143 	// for each Petition
144 	for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) {
145 		BMessage* msg = fEventsWanted.FindMessage(index);
146 //		printf("%s:Petition %ld ... of %ld msg #%p\n", __FUNCTION__, index,
147 //			fEventsWanted.CountMessages(), msg);
148 //		msg->PrintToStream();
149 		eventIndex = 0;
150 
151 		// for each Event
152 		while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) {
153 			if (eventFound == event) {
154 
155 //				printf("%s:Event %d found@%ld...", __FUNCTION__, event, eventIndex);
156 				// there is an opcode specified..
157 				if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound)
158 					== B_OK) {
159 					// ensure the opcode
160 					if ((uint16)opcodeFound != opcode) {
161 //						printf("%s:opcode does not match %d\n",
162 //							__FUNCTION__, opcode);
163 						eventIndex++;
164 						continue;
165 					}
166 //					printf("Opcode matches %d\n", opcode);
167 				} else {
168 //					printf("No opcode specified\n");
169 				}
170 
171 				fEventsWanted.Unlock();
172 				if (indexFound != NULL)
173 					*indexFound = eventIndex;
174 				return msg;
175 			}
176 			eventIndex++;
177 		}
178 	}
179 //	printf("%s:Event %d not found\n", __FUNCTION__, event);
180 
181 	fEventsWanted.Unlock();
182 	return NULL;
183 
184 }
185