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