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@%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", 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 int16 eventFound; 138 int16 opcodeFound; 139 int32 eventIndex; 140 141 fEventsWanted.Lock(); 142 // for each Petition 143 for (int32 index = 0 ; index < fEventsWanted.CountMessages() ; index++) { 144 BMessage* msg = fEventsWanted.FindMessage(index); 145 printf("%s:Petition %ld ... of %ld msg #%p\n", __FUNCTION__, index, 146 fEventsWanted.CountMessages(), msg); 147 //msg->PrintToStream(); 148 eventIndex = 0; 149 150 // for each Event 151 while (msg->FindInt16("eventExpected", eventIndex, &eventFound) == B_OK ) { 152 if (eventFound == event) { 153 154 printf("%s:Event found@%ld...", __FUNCTION__, eventIndex); 155 // there is an opcode specified.. 156 if (msg->FindInt16("opcodeExpected", eventIndex, &opcodeFound) 157 == B_OK) { 158 // ensure the opcode 159 if ((uint16)opcodeFound != opcode) { 160 printf("%s:opcode does not match %d\n", 161 __FUNCTION__, opcode); 162 break; 163 } 164 printf("Opcode matches %d\n", opcode); 165 } else { 166 printf("No opcode specified\n"); 167 } 168 169 fEventsWanted.Unlock(); 170 if (indexFound != NULL) 171 *indexFound = eventIndex; 172 return msg; 173 } 174 eventIndex++; 175 } 176 } 177 printf("%s:Nothing found\n", __FUNCTION__); 178 179 fEventsWanted.Unlock(); 180 return NULL; 181 182 } 183