vfs: check userland buffers before reading them.
[haiku.git] / src / servers / bluetooth / LocalDeviceHandler.cpp
blob7d3936a6fe8445e6264cb78dff6c807784f8c533
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 */
7 #include "LocalDeviceHandler.h"
10 LocalDeviceHandler::LocalDeviceHandler(HCIDelegate* hd)
12 fHCIDelegate = hd;
13 fProperties = new BMessage();
17 LocalDeviceHandler::~LocalDeviceHandler()
19 delete fHCIDelegate;
20 delete fProperties;
24 hci_id
25 LocalDeviceHandler::GetID()
27 return fHCIDelegate->Id();
31 status_t
32 LocalDeviceHandler::Launch(void)
34 return fHCIDelegate->Launch();
38 bool
39 LocalDeviceHandler::Available()
42 return true;
46 void
47 LocalDeviceHandler::Acquire(void)
53 bool
54 LocalDeviceHandler::IsPropertyAvailable(const char* property)
56 type_code typeFound;
57 int32 countFound;
59 return (fProperties->GetInfo(property, &typeFound, &countFound) == B_OK );
63 void
64 LocalDeviceHandler::AddWantedEvent(BMessage* msg)
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();
74 void
75 LocalDeviceHandler::ClearWantedEvent(BMessage* msg)
77 fEventsWanted.Lock();
78 fEventsWanted.RemoveMessage(msg);
79 fEventsWanted.Unlock();
83 void
84 LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode)
86 // Remove the whole petition from queue
87 fEventsWanted.Lock();
89 int16 eventFound;
90 int16 opcodeFound;
91 int32 eventIndex = 0;
93 // for each Event
94 while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK) {
96 printf("%s:Event expected %d@%" B_PRId32 "...\n", __FUNCTION__, event,
97 eventIndex);
99 if (eventFound == event) {
101 printf("%s:Event matches@%" B_PRId32 "\n", __FUNCTION__, eventIndex);
102 // there is an opcode specified
103 if (opcode != 0) {
105 // The opcode matches
106 if ((msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK)
107 && ((uint16)opcodeFound == opcode)) {
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;
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;
125 eventIndex++;
127 printf("%s:Nothing Found/Removed\n", __FUNCTION__);
129 finish:
130 fEventsWanted.Unlock();
135 BMessage*
136 LocalDeviceHandler::FindPetition(uint16 event, uint16 opcode, int32* indexFound)
138 //debug data
139 int16 eventFound;
140 int16 opcodeFound;
141 int32 eventIndex;
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;
152 // for each Event
153 while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) {
154 if (eventFound == event) {
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;
167 // printf("Opcode matches %d\n", opcode);
168 } else {
169 // printf("No opcode specified\n");
172 fEventsWanted.Unlock();
173 if (indexFound != NULL)
174 *indexFound = eventIndex;
175 return msg;
177 eventIndex++;
180 // printf("%s:Event %d not found\n", __FUNCTION__, event);
182 fEventsWanted.Unlock();
183 return NULL;