Update l2cap client testcase

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37473 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Ruiz Dorantes 2010-07-11 17:15:58 +00:00
parent ef36d96496
commit aa5fca06bf

View File

@ -1,84 +1,100 @@
#include <stddef.h> /*
#include <stdio.h> * Copyright 2010 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
#include <unistd.h> * All rights reserved. Distributed under the terms of the MIT License.
#include <errno.h> *
#include <stdlib.h> */
#include <sys/socket.h> #include <stddef.h>
#include <stdio.h>
#include <bluetooth/bluetooth.h> #include <unistd.h>
#include <bluetooth/bdaddrUtils.h> #include <errno.h>
#include <bluetooth/L2CAP/btL2CAP.h> #include <stdlib.h>
#include <sys/socket.h>
struct sockaddr_l2cap l2sa;
#include <bluetooth/bluetooth.h>
int #include <bluetooth/bdaddrUtils.h>
make_named_socket (const bdaddr_t* bdaddr) #include <bluetooth/L2CAP/btL2CAP.h>
{
int
int sock; CreateL2capSocket(const bdaddr_t* bdaddr, struct sockaddr_l2cap& l2sa, uint16 psm)
size_t size; {
status_t error; int sock;
size_t size;
/* Create the socket. */ status_t error;
//printf("Creating socket for %s\n",pbatostr(bdaddr));
/* Create the socket. */
sock = socket (PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_L2CAP); // BLUETOOTH_PROTO_L2CAP printf("Creating socket ...\n");
if (sock < 0)
{ sock = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_L2CAP);
perror ("socket"); if (sock < 0) {
exit (EXIT_FAILURE); perror ("socket");
} exit (EXIT_FAILURE);
}
/* Bind a name to the socket. */
l2sa.l2cap_psm = 1; /* Bind a name to the socket. */
l2sa.l2cap_family = PF_BLUETOOTH; printf("Binding socket ...\n");
memcpy(&l2sa.l2cap_bdaddr, bdaddr, sizeof(bdaddr_t));
size = sizeof(struct sockaddr_l2cap);
size = sizeof(struct sockaddr_l2cap);
l2sa.l2cap_len = size; l2sa.l2cap_family = PF_BLUETOOTH;
l2sa.l2cap_len = size;
if (bind (sock, (struct sockaddr *) &l2sa, size) < 0) memcpy(&l2sa.l2cap_bdaddr, bdaddr, sizeof(bdaddr_t));
{ l2sa.l2cap_psm = psm;
perror ("bind");
exit (EXIT_FAILURE); if (bind(sock, (struct sockaddr *)&l2sa, size) < 0) {
} perror ("bind");
exit (EXIT_FAILURE);
printf("Connecting socket for %s\n", bdaddrUtils::ToString(*bdaddr)); }
if ( (error = connect(sock , (struct sockaddr *) &l2sa, sizeof(l2sa))) < 0) {
perror ("connect"); printf("Connecting socket for %s\n", bdaddrUtils::ToString(*bdaddr));
exit (EXIT_FAILURE); if ((error = connect(sock, (struct sockaddr *)&l2sa, sizeof(l2sa))) < 0) {
} perror ("connect");
printf("Return status of the connection is %ld \n", error ); exit (EXIT_FAILURE);
}
return sock;
} printf("Return status of the connection is %ld \n", error );
int main(int argc, char **argv) return sock;
{ }
int sock;
char string[512]; int main(int argc, char **argv)
size_t len; {
struct sockaddr_l2cap l2sa;
if (argc != 2 ) { char string[512];
printf("i need a bdaddr! (%d)\n", argc); size_t len;
return 0; uint16 psm = 1;
}
if (argc < 2 ) {
const bdaddr_t bdaddr = bdaddrUtils::FromString(argv[1]); printf("I need a bdaddr!\nUsage:\n\t%s bluetooth_address [psm]\n",
argv[0]);
sock = make_named_socket(&bdaddr); return 0;
}
while (strcmp(string,"bye")!= 0 ) {
if (argc > 2) {
fscanf(stdin, "%s", string ); psm = atoi(argv[2]);
// len = send(sock, string, strlen(string), 0); printf("PSM requested %d\n", psm);
len = sendto(sock, string, strlen(string) + 1 /*\0*/, 0, (struct sockaddr*) &l2sa, sizeof(struct sockaddr_l2cap) ); }
printf("Sent %ld bytes\n",len); if ((psm & 1) == 0)
//recvfrom(sock, buff, 4096-1, 0, (struct sockaddr *) &l2, &len); printf("WARNING: PSM requested is not pair\n");
}
const bdaddr_t bdaddr = bdaddrUtils::FromString(argv[1]);
printf("Transmission done ...\n");
getchar(); int sock = CreateL2capSocket(&bdaddr, l2sa, psm);
close(sock);
} while (strcmp(string,"bye") != 0) {
fscanf(stdin, "%s", string);
len = sendto(sock, string, strlen(string) + 1 /*\0*/, 0,
(struct sockaddr*) &l2sa, sizeof(struct sockaddr_l2cap));
printf("Sent %ld bytes\n", len);
// len = send(sock, string + 1 /*\0*/, strlen(string), 0);
// recvfrom(sock, buff, 4096-1, 0, (struct sockaddr *) &l2, &len);
}
printf("Transmission done ... (press key to close socket)\n");
getchar();
close(sock);
}