xref: /haiku/src/servers/bluetooth/LocalDeviceHandler.cpp (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
1 /*
2  * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3  *
4  * All rights reserved. Distributed under the terms of the MIT License.
5  *
6  */
7 
8 
9 #include "LocalDeviceHandler.h"
10 
11 
12 
13 LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd)
14 {
15 	fHCIDelegate = hd;
16 	fProperties = new BMessage();
17 
18 }
19 
20 LocalDeviceHandler::~LocalDeviceHandler()
21 {
22 
23 }
24 
25 
26 hci_id
27 LocalDeviceHandler::GetID()
28 {
29     return fHCIDelegate->GetID();
30 }
31 
32 status_t
33 LocalDeviceHandler::Launch(void)
34 {
35     return fHCIDelegate->Launch();
36 }
37 
38 
39 bool
40 LocalDeviceHandler::Available() {
41 
42 	return true;
43 }
44 
45 
46 void
47 LocalDeviceHandler::Acquire(void) {
48 
49 }
50 
51 
52 bool
53 LocalDeviceHandler::IsPropertyAvailable(const BString& property) {
54 
55 	type_code typeFound;
56 	int32     countFound;
57 
58 	return (fProperties->GetInfo(property.String(), &typeFound, &countFound) == B_OK );
59 
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 void
74 LocalDeviceHandler::ClearWantedEvent(BMessage* msg)
75 {
76     fEventsWanted.Lock();
77     fEventsWanted.RemoveMessage(msg);
78     fEventsWanted.Unlock();
79 
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@%ld...\n", __FUNCTION__, 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", event, opcode, msg);
110 					(void)msg->RemoveData("eventExpected", eventIndex);
111 					(void)msg->RemoveData("opcodeExpected", eventIndex);
112 					goto finish;
113 				}
114 
115 			}  else {
116 				// Event matches so far
117 				printf("Removed event %d from message %p\n", event, msg);
118 				(void)msg->RemoveData("eventExpected", eventIndex);
119 				goto finish;
120 			}
121 
122 		}
123 		eventIndex++;
124 	}
125 	printf("%s:Nothing Found/Removed\n",__FUNCTION__);
126 
127 finish:
128 	fEventsWanted.Unlock();
129 
130 }
131 
132 
133 BMessage*
134 LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound)
135 {
136     int16 eventFound;
137     int16 opcodeFound;
138 	int32 eventIndex;
139 
140     fEventsWanted.Lock();
141     // for each Petition
142     for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) {
143         BMessage* msg = fEventsWanted.FindMessage(index);
144 
145 		printf("%s:Petition %ld ... of %ld msg #%p\n", __FUNCTION__, index, fEventsWanted.CountMessages(), msg);
146 		//msg->PrintToStream();
147 		eventIndex = 0;
148 
149         // for each Event
150         while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) {
151             if (eventFound == event) {
152 
153         		printf("%s:Event found@%ld...", __FUNCTION__, eventIndex);
154                 // there is an opcode specified..
155                 if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) {
156 
157                 	// ensure the opcode
158                 	if ((uint16)opcodeFound != opcode) {
159 		        		printf("%s:opcode does not match %d\n",__FUNCTION__, opcode);
160     	                break;
161     	            }
162 					printf("opcode match %d\n", opcode);
163                 } else {
164                 	printf("no opcode specified\n");
165                 }
166 
167 	            fEventsWanted.Unlock();
168 				if (indexFound != NULL)
169 					*indexFound = eventIndex;
170     	        return msg;
171 
172 
173             }
174             eventIndex++;
175         }
176     }
177   	printf("%s:Nothing found\n",__FUNCTION__);
178 
179     fEventsWanted.Unlock();
180     return NULL;
181 }
182