Skip to content

Arguments parsing #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
l2cap_proxy

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Run the proxy
```
sudo service bluetooth stop
sudo hciconfig hci<X> up pscan
sudo ./l2cap_proxy <master-bdaddr> <dongle-bdaddr> <device-class>
sudo ./l2cap_proxy -h <master-bdaddr> [-d <dongle-bdaddr>] [-c <device-class>]
```
```
<X>: the device number (type hciconfig to list the available adapters)
Expand Down
20 changes: 0 additions & 20 deletions l2cap_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,34 +155,14 @@ int acl_send_data (const char *bdaddr_dst, unsigned short cid, const unsigned ch
return ret;
}

#define L2CAP_MTU 1024

static void l2cap_setsockopt(int fd)
{
struct l2cap_options l2o;
socklen_t len = sizeof(l2o);

int opt = L2CAP_LM_MASTER;
if (setsockopt(fd, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
{
perror("setsockopt L2CAP_LM");
}

memset(&l2o, 0, sizeof(l2o));
if(getsockopt(fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0)
{
perror("getsockopt L2CAP_OPTIONS");
}
else
{
l2o.omtu = L2CAP_MTU;
l2o.imtu = L2CAP_MTU;
if(setsockopt(fd, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0)
{
perror("setsockopt L2CAP_OPTIONS");
}
}

/*if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
{
perror("fcntl O_NONBLOCK");
Expand Down
81 changes: 72 additions & 9 deletions l2cap_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ static void close_fd(struct pollfd* pfd)
pfd->fd = -1;
}

void usage()
{
printf("Usage: l2cap_proxy [options] <host address>\n");
printf("Options:\n");
printf(" -d <addr>, --dongle <addr> Dongle address\n");
printf(" -c <class>, --class <class> Device class\n");
}

int main(int argc, char *argv[])
{
char* master = NULL;
Expand All @@ -120,6 +128,12 @@ int main(int argc, char *argv[])
char slave[sizeof("00:00:00:00:00:00")+1] = {};
unsigned short cid[CID_MAX_INDEX][PSM_MAX_INDEX];

enum {
ARGSTATE_NONE,
ARGSTATE_DONGLEADDR,
ARGSTATE_DEVCLASS
} argstate = ARGSTATE_NONE;

/*
* Set highest priority & scheduler policy.
*/
Expand All @@ -132,18 +146,67 @@ int main(int argc, char *argv[])

(void) signal(SIGINT, terminate);

/* Check args */
if (argc >= 1)
master = argv[1];
/* Parse command line args */
for (int i = 1; i < argc; ++i) {
switch (argstate) {
case ARGSTATE_NONE:
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--dongle"))
{
argstate = ARGSTATE_DONGLEADDR;
}
else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--class"))
{
argstate = ARGSTATE_DEVCLASS;
}
else
{
if (bachk(argv[i]) == -1)
{
fprintf(stderr, "Invalid host address \"%s\"\n", argv[i]);
usage();
return 1;
}
master = argv[i];
}
break;
case ARGSTATE_DONGLEADDR:
if (bachk(argv[i]) == -1)
{
fprintf(stderr, "Invalid dongle address \"%s\"\n", argv[i]);
usage();
return 1;
}
local = argv[i];
argstate = ARGSTATE_NONE;
break;
case ARGSTATE_DEVCLASS:
device_class = strtol(argv[i], NULL, 0);
argstate = ARGSTATE_NONE;
break;
}
}

if (argc >= 2)
local = argv[2];
switch (argstate) {
case ARGSTATE_NONE:
break;
case ARGSTATE_DONGLEADDR:
fprintf(stderr, "Missing argument to -d\n");
break;
case ARGSTATE_DEVCLASS:
fprintf(stderr, "Missing argument to -c\n");
break;
}

if (argc >= 3)
device_class = strtol(argv[3], NULL, 0);
if (argstate != ARGSTATE_NONE)
{
usage();
return 1;
}

if (!master || bachk(master) == -1 || (local && bachk(local) == -1)) {
printf("usage: %s <ps3-mac-address> <dongle-mac-address> <device-class>\n", *argv);
if (!master)
{
fprintf(stderr, "Missing host address\n");
usage();
return 1;
}

Expand Down