Add code for simulating a SDP server present in the host

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34330 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Ruiz Dorantes 2009-11-28 19:26:00 +00:00
parent af10de93a4
commit e97e2846af

View File

@ -42,8 +42,8 @@ DispatchEvent(struct hci_event_header* header, int32 code, size_t size)
} }
// fetch the LocalDevice who belongs this event // fetch the LocalDevice who belongs this event
LocalDeviceImpl* lDeviceImplementation = ((BluetoothServer*)be_app)-> LocalDeviceImpl* lDeviceImplementation = ((BluetoothServer*)be_app)->
LocateLocalDeviceImpl(GET_PORTCODE_HID(code)); LocateLocalDeviceImpl(GET_PORTCODE_HID(code));
if (lDeviceImplementation == NULL) { if (lDeviceImplementation == NULL) {
Output::Instance()->Post("LocalDevice could not be fetched", BLACKBOARD_KIT); Output::Instance()->Post("LocalDevice could not be fetched", BLACKBOARD_KIT);
@ -87,13 +87,14 @@ bool BluetoothServer::QuitRequested(void)
!= NULL) != NULL)
delete lDeviceImpl; delete lDeviceImpl;
_RemoveDeskbarIcon(); _RemoveDeskbarIcon();
/* stop the SDP server thread */ /* stop the SDP server thread */
fIsShuttingDown = true; fIsShuttingDown = true;
status_t threadReturnStatus;
wait_for_thread(fSDPThreadID, &threadReturnStatus); status_t threadReturnStatus;
printf("SDP server thread exited with: %s\n", strerror(threadReturnStatus)); wait_for_thread(fSDPThreadID, &threadReturnStatus);
printf("SDP server thread exited with: %s\n", strerror(threadReturnStatus));
printf("Shutting down bluetooth_server.\n"); printf("Shutting down bluetooth_server.\n");
return BApplication::QuitRequested(); return BApplication::QuitRequested();
@ -130,10 +131,13 @@ void BluetoothServer::ReadyToRun(void)
fSDPThreadID = spawn_thread(SDPServerThread, "SDP server thread", fSDPThreadID = spawn_thread(SDPServerThread, "SDP server thread",
B_NORMAL_PRIORITY, this); B_NORMAL_PRIORITY, this);
//#define _USE_SDP_SERVER
#ifdef _USE_SDP_SERVER
if (fSDPThreadID <= 0 || resume_thread(fSDPThreadID) != B_OK) { if (fSDPThreadID <= 0 || resume_thread(fSDPThreadID) != B_OK) {
Output::Instance()->Postf(BLACKBOARD_SDP, Output::Instance()->Postf(BLACKBOARD_SDP,
"Failed launching the SDP server thread: %x\n", fSDPThreadID); "Failed launching the SDP server thread: %x\n", fSDPThreadID);
} }
#endif
} }
@ -204,7 +208,7 @@ void BluetoothServer::MessageReceived(BMessage *message)
/* Handle if the bluetooth preferences is running?? */ /* Handle if the bluetooth preferences is running?? */
case B_SOME_APP_LAUNCHED: case B_SOME_APP_LAUNCHED:
{ {
const char *signature; const char *signature;
// TODO: what's this for? // TODO: what's this for?
if (message->FindString("be:signature", &signature) == B_OK) { if (message->FindString("be:signature", &signature) == B_OK) {
@ -250,9 +254,9 @@ BluetoothServer::LocateDelegateFromMessage(BMessage* message)
/* Try to find out when a ID was specified */ /* Try to find out when a ID was specified */
int index; int index;
for (index = 0; index < fLocalDevicesList.CountItems(); index ++) { for (index = 0; index < fLocalDevicesList.CountItems(); index ++) {
lDeviceImpl = fLocalDevicesList.ItemAt(index); lDeviceImpl = fLocalDevicesList.ItemAt(index);
if (lDeviceImpl->GetID() == hid) if (lDeviceImpl->GetID() == hid)
break; break;
} }
} }
@ -266,7 +270,7 @@ BluetoothServer::LocateLocalDeviceImpl(hci_id hid)
/* Try to find out when a ID was specified */ /* Try to find out when a ID was specified */
int index; int index;
for (index = 0; index < fLocalDevicesList.CountItems(); index ++) { for (index = 0; index < fLocalDevicesList.CountItems(); index++) {
LocalDeviceImpl* lDeviceImpl = fLocalDevicesList.ItemAt(index); LocalDeviceImpl* lDeviceImpl = fLocalDevicesList.ItemAt(index);
if (lDeviceImpl->GetID() == hid) if (lDeviceImpl->GetID() == hid)
return lDeviceImpl; return lDeviceImpl;
@ -299,30 +303,30 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply)
if (message->FindInt32("hci_id", &hid) == B_OK) if (message->FindInt32("hci_id", &hid) == B_OK)
{ {
Output::Instance()->Post("GetLocalDevice requested with id\n", Output::Instance()->Post("GetLocalDevice requested with id\n",
BLACKBOARD_KIT); BLACKBOARD_KIT);
lDeviceImpl = LocateDelegateFromMessage(message); lDeviceImpl = LocateDelegateFromMessage(message);
} else if (message->FindData("bdaddr", B_ANY_TYPE, (const void**)&bdaddr, &size ) } else if (message->FindData("bdaddr", B_ANY_TYPE, (const void**)&bdaddr, &size)
== B_OK) { == B_OK) {
/* Try to find out when the user specified the address */ /* Try to find out when the user specified the address */
Output::Instance()->Post("GetLocalDevice requested with bdaddr\n", Output::Instance()->Post("GetLocalDevice requested with bdaddr\n",
BLACKBOARD_KIT); BLACKBOARD_KIT);
for (lastIndex = 0; lastIndex < fLocalDevicesList.CountItems(); lastIndex ++) { for (lastIndex = 0; lastIndex < fLocalDevicesList.CountItems(); lastIndex ++) {
//TODO: Only possible if the property is available //TODO: Only possible if the property is available
//bdaddr_t local; //bdaddr_t local;
//lDeviceImpl = fLocalDevicesList.ItemAt(lastIndex); //lDeviceImpl = fLocalDevicesList.ItemAt(lastIndex);
//if ((lDeviceImpl->GetAddress(&local, message) == B_OK) //if ((lDeviceImpl->GetAddress(&local, message) == B_OK)
// && bacmp(&local, &bdaddr)) { // && bacmp(&local, &bdaddr)) {
// break; // break;
//} //}
} }
} else { } else {
// Careless, any device not performing operations will be fine // Careless, any device not performing operations will be fine
Output::Instance()->Post("GetLocalDevice plain request\n", BLACKBOARD_KIT); Output::Instance()->Post("GetLocalDevice plain request\n", BLACKBOARD_KIT);
// from last assigned till end // from last assigned till end
for ( int index = lastIndex + 1; index < fLocalDevicesList.CountItems(); index ++) { for (int index = lastIndex + 1; index < fLocalDevicesList.CountItems(); index++) {
lDeviceImpl = fLocalDevicesList.ItemAt(index); lDeviceImpl= fLocalDevicesList.ItemAt(index);
printf("Requesting local device %ld\n", lDeviceImpl->GetID()); printf("Requesting local device %ld\n", lDeviceImpl->GetID());
if (lDeviceImpl != NULL && lDeviceImpl->Available()) if (lDeviceImpl != NULL && lDeviceImpl->Available())
{ {
@ -334,7 +338,7 @@ BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply)
// from starting till last assigned if not yet found // from starting till last assigned if not yet found
if (lDeviceImpl == NULL) { if (lDeviceImpl == NULL) {
for ( int index = 0; index <= lastIndex ; index ++) { for (int index = 0; index <= lastIndex ; index ++) {
lDeviceImpl = fLocalDevicesList.ItemAt(index); lDeviceImpl = fLocalDevicesList.ItemAt(index);
printf("Requesting local device %ld\n", lDeviceImpl->GetID()); printf("Requesting local device %ld\n", lDeviceImpl->GetID());
if (lDeviceImpl != NULL && lDeviceImpl->Available()) if (lDeviceImpl != NULL && lDeviceImpl->Available())
@ -408,30 +412,30 @@ BluetoothServer::HandleGetProperty(BMessage* message, BMessage* reply)
if (message->FindString("property", &propertyRequested) == B_OK) { if (message->FindString("property", &propertyRequested) == B_OK) {
Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Searching %s property...\n", Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Searching %s property...\n",
propertyRequested); propertyRequested);
// Check if the property has been already retrieved // Check if the property has been already retrieved
if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) { if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) {
if (strcmp(propertyRequested, "hci_version") == 0 if (strcmp(propertyRequested, "hci_version") == 0
|| strcmp(propertyRequested, "lmp_version") == 0 || strcmp(propertyRequested, "lmp_version") == 0
|| strcmp(propertyRequested, "sco_mtu") == 0) { || strcmp(propertyRequested, "sco_mtu") == 0) {
uint8 result = lDeviceImpl->GetPropertiesMessage()->FindInt8(propertyRequested); uint8 result = lDeviceImpl->GetPropertiesMessage()->FindInt8(propertyRequested);
reply->AddInt32("result", result); reply->AddInt32("result", result);
} else if (strcmp(propertyRequested, "hci_revision") == 0 } else if (strcmp(propertyRequested, "hci_revision") == 0
|| strcmp(propertyRequested, "lmp_subversion") == 0 || strcmp(propertyRequested, "lmp_subversion") == 0
|| strcmp(propertyRequested, "manufacturer") == 0 || strcmp(propertyRequested, "manufacturer") == 0
|| strcmp(propertyRequested, "acl_mtu") == 0 || strcmp(propertyRequested, "acl_mtu") == 0
|| strcmp(propertyRequested, "acl_max_pkt") == 0 || strcmp(propertyRequested, "acl_max_pkt") == 0
|| strcmp(propertyRequested, "sco_max_pkt") == 0 ) { || strcmp(propertyRequested, "sco_max_pkt") == 0 ) {
uint16 result = lDeviceImpl->GetPropertiesMessage()->FindInt16(propertyRequested); uint16 result = lDeviceImpl->GetPropertiesMessage()->FindInt16(propertyRequested);
reply->AddInt32("result", result); reply->AddInt32("result", result);
} else { } else {
Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Property %s could not be satisfied\n", Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()), "Property %s could not be satisfied\n",
propertyRequested); propertyRequested);
} }
} }
} }
@ -450,26 +454,25 @@ BluetoothServer::SDPServerThread(void* data)
/* Set up the SDP socket. */ /* Set up the SDP socket. */
struct sockaddr_l2cap loc_addr = { 0 }; struct sockaddr_l2cap loc_addr = { 0 };
int socketServer; int socketServer;
int client; int client;
status_t status; status_t status;
char buff[512] = ""; char buffer[512] = "";
Output::Instance()->Postf(BLACKBOARD_SDP, "SDP server thread up...\n"); Output::Instance()->Postf(BLACKBOARD_SDP, "SDP server thread up...\n");
socketServer = socket(PF_BLUETOOTH, SOCK_STREAM /*SOCK_SEQPACKET*/, BLUETOOTH_PROTO_L2CAP); socketServer = socket(PF_BLUETOOTH, SOCK_STREAM /*SOCK_SEQPACKET*/, BLUETOOTH_PROTO_L2CAP);
if (socketServer < 0) { if (socketServer < 0) {
Output::Instance()->Post("Could not create server socket ...\n", BLACKBOARD_SDP); Output::Instance()->Post("Could not create server socket ...\n", BLACKBOARD_SDP);
return B_ERROR; return B_ERROR;
} }
// bind socket to port 0x1001 of the first available // bind socket to port 0x1001 of the first available
// bluetooth adapter // bluetooth adapter
loc_addr.l2cap_family = AF_BLUETOOTH; loc_addr.l2cap_family = AF_BLUETOOTH;
loc_addr.l2cap_bdaddr = *BDADDR_ANY; loc_addr.l2cap_bdaddr = *BDADDR_ANY;
loc_addr.l2cap_psm = B_HOST_TO_LENDIAN_INT16(1); // correct? loc_addr.l2cap_psm = B_HOST_TO_LENDIAN_INT16(1);
loc_addr.l2cap_len = sizeof(struct sockaddr_l2cap); loc_addr.l2cap_len = sizeof(struct sockaddr_l2cap);
status = bind(socketServer, (struct sockaddr *)&loc_addr, sizeof(struct sockaddr_l2cap)); status = bind(socketServer, (struct sockaddr *)&loc_addr, sizeof(struct sockaddr_l2cap));
@ -482,51 +485,51 @@ BluetoothServer::SDPServerThread(void* data)
// setsockopt(sock, SOL_L2CAP, SO_L2CAP_OMTU, &omtu, len ); // setsockopt(sock, SOL_L2CAP, SO_L2CAP_OMTU, &omtu, len );
// getsockopt(sock, SOL_L2CAP, SO_L2CAP_IMTU, &omtu, &len ); // getsockopt(sock, SOL_L2CAP, SO_L2CAP_IMTU, &omtu, &len );
/* Listen for up to 10 connections. */ /* Listen for up to 10 connections. */
status = listen(socketServer, 10); status = listen(socketServer, 10);
if (status != B_OK) { if (status != B_OK) {
Output::Instance()->Postf(BLACKBOARD_SDP, "Could not listen server socket %d ...\n", status); Output::Instance()->Postf(BLACKBOARD_SDP, "Could not listen server socket %d ...\n", status);
return status; return status;
} }
while (!server->fIsShuttingDown) { while (!server->fIsShuttingDown) {
Output::Instance()->Postf(BLACKBOARD_SDP, "Waiting connection for socket %d ...\n", socketServer); Output::Instance()->Postf(BLACKBOARD_SDP, "Waiting connection for socket %d ...\n", socketServer);
uint len = sizeof(struct sockaddr_l2cap); uint len = sizeof(struct sockaddr_l2cap);
client = accept(socketServer, (struct sockaddr *) &loc_addr, &len); client = accept(socketServer, (struct sockaddr *) &loc_addr, &len);
Output::Instance()->Postf(BLACKBOARD_SDP, "Incomming connection... %ld\n", client); Output::Instance()->Postf(BLACKBOARD_SDP, "Incomming connection... %ld\n", client);
ssize_t leng = recv(client, buff, 29 , 0); ssize_t receivedSize;
if (leng == -1) {
Output::Instance()->Post("Error reading client socket\n", BLACKBOARD_SDP);
} do {
receivedSize = recv(client, buffer, 29 , 0);
if (receivedSize < 0)
Output::Instance()->Post("Error reading client socket\n", BLACKBOARD_SDP);
else {
Output::Instance()->Postf(BLACKBOARD_SDP, "Received from SDP client: %ld:\n", receivedSize);
for (int i = 0; i < receivedSize ; i++)
Output::Instance()->Postf(BLACKBOARD_SDP, "%x:", buffer[i]);
Output::Instance()->Postf(BLACKBOARD_SDP, "Received from SDP client: %ld:\n", leng); Output::Instance()->Post("\n", BLACKBOARD_SDP);
for (int i = 0; i < leng ; i++) }
Output::Instance()->Postf(BLACKBOARD_SDP, "%x:", buff[i]); } while (receivedSize >= 0);
Output::Instance()->Post("\n", BLACKBOARD_SDP); /* fd_set fdSet;
/* fd_set fdSet;
FD_ZERO(&fdSet); FD_ZERO(&fdSet);
FD_SET(s, &fdSet); FD_SET(s, &fdSet);
struct timeval timeout; struct timeval timeout;
memset(&timeout, 0, sizeof(timeout)); memset(&timeout, 0, sizeof(timeout));
// TODO initialize timeout! // TODO initialize timeout!
int ret = select(1, &fdSet, NULL, NULL, &timeout); int ret = select(1, &fdSet, NULL, NULL, &timeout);
printf("ready to read descriptors: %d\n", ret); printf("ready to read descriptors: %d\n", ret);
*/ */
snooze(5000000); snooze(5000000);
Output::Instance()->Post("Waiting for next connection...\n", BLACKBOARD_SDP); Output::Instance()->Post("\nWaiting for next connection...\n", BLACKBOARD_SDP);
} }
/* Close the socket */ /* Close the socket */