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 fProperties; 20 } 21 22 23 hci_id 24 LocalDeviceHandler::GetID() 25 { 26 return fHCIDelegate->GetID(); 27 } 28 29 30 status_t 31 LocalDeviceHandler::Launch(void) 32 { 33 return fHCIDelegate->Launch(); 34 } 35 36 37 bool 38 LocalDeviceHandler::Available() 39 { 40 41 return true; 42 } 43 44 45 void 46 LocalDeviceHandler::Acquire(void) 47 { 48 49 } 50 51 52 bool 53 LocalDeviceHandler::IsPropertyAvailable(const char* property) 54 { 55 type_code typeFound; 56 int32 countFound; 57 58 return (fProperties->GetInfo(property, &typeFound, &countFound) == B_OK ); 59 } 60 61 62 void 63 LocalDeviceHandler::AddWantedEvent(BMessage* msg) 64 { 65 fEventsWanted.Lock(); 66 // TODO: review why it is needed to replicate the msg 67 printf("Adding request... %p\n", msg); 68 fEventsWanted.AddMessage(msg); 69 fEventsWanted.Unlock(); 70 } 71 72 73 void 74 LocalDeviceHandler::ClearWantedEvent(BMessage* msg) 75 { 76 fEventsWanted.Lock(); 77 fEventsWanted.RemoveMessage(msg); 78 fEventsWanted.Unlock(); 79 } 80 81 82 void 83 LocalDeviceHandler::ClearWantedEvent(BMessage* msg, uint16 event, uint16 opcode) 84 { 85 // Remove the whole petition from queue 86 fEventsWanted.Lock(); 87 88 int16 eventFound; 89 int16 opcodeFound; 90 int32 eventIndex = 0; 91 92 // for each Event 93 while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK) { 94 95 printf("%s:Event expected@%ld...\n", __FUNCTION__, eventIndex); 96 97 if (eventFound == event) { 98 99 printf("%s:Event matches@%ld", __FUNCTION__, eventIndex); 100 // there is an opcode specified 101 if (opcode != 0) { 102 103 // The opcode matches 104 if ( (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) == B_OK) 105 && ((uint16)opcodeFound == opcode) ) { 106 107 // this should remove only the entry 108 printf("Removed event %#x and opcode %d from request %p\n", 109 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 printf("%s:Petition %ld ... of %ld msg #%p\n", __FUNCTION__, index, 145 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) 156 == B_OK) { 157 // ensure the opcode 158 if ((uint16)opcodeFound != opcode) { 159 printf("%s:opcode does not match %d\n", 160 __FUNCTION__, opcode); 161 break; 162 } 163 printf("Opcode matches %d\n", opcode); 164 } else { 165 printf("No opcode specified\n"); 166 } 167 168 fEventsWanted.Unlock(); 169 if (indexFound != NULL) 170 *indexFound = eventIndex; 171 return msg; 172 } 173 eventIndex++; 174 } 175 } 176 printf("%s:Nothing found\n", __FUNCTION__); 177 178 fEventsWanted.Unlock(); 179 return NULL; 180 181 } 182