Skip to content

Various fixes #4

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

Merged
merged 3 commits into from
Jul 5, 2017
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ List bluetooth devices
----------------------

```
var btim = require('btim')
var interfaces = JSON.parse('[' + btim.list() + ']');
var btim = require('btim');
var interfaces = btim.list();
console.log(interfaces);
```

Expand Down
139 changes: 78 additions & 61 deletions hci_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@

#include <nan.h>

#define INTERFACE_DESCRIPTION_SIZE 1024
#define HEADER_DESCRIPTION_SIZE 512

static struct hci_dev_info device_info;
STAILQ_HEAD(, interface) interfaces;

struct interface
{
char description[INTERFACE_DESCRIPTION_SIZE];
v8::Local<v8::Object> description;
STAILQ_ENTRY(interface) next;
};

Expand All @@ -29,75 +26,95 @@ struct interface
* Params:
* - devices_info: info about a HCI device.
*/
static char *devices_header(struct hci_dev_info *device_info)
static void interface_infos(v8::Isolate *isolate, struct hci_dev_info *device_info)
{
static int header = -1;
char mac_address[18];
char header_info[HEADER_DESCRIPTION_SIZE];
char buffer[101]; // Let be large
struct hci_dev_stats *device_stats = &device_info->stat;
char *device_status;

if (header == device_info->dev_id)
return NULL;
ba2str(&device_info->bdaddr, mac_address);

header = device_info->dev_id;
device_status = hci_dflagstostr(device_info->flags);

ba2str(&device_info->bdaddr, mac_address);
/*
* Fill an object with all infos
*/

// Fill the header
snprintf(header_info, HEADER_DESCRIPTION_SIZE,
"{\"%s\" : {\"Type\": \"%s\", \"Bus\": \"%s\", \"BD Address\": \"%s\", \"ACL MTU\": \"%d:%d\", \"SCO MTU\": \"%d:%d\"",
device_info->name,
hci_typetostr((device_info->type & 0x30) >> 4),
hci_bustostr(device_info->type & 0x0f),
mac_address,
device_info->acl_mtu,
device_info->acl_pkts,
device_info->sco_mtu,
device_info->sco_pkts);

return strdup(header_info);
}
// RX
v8::Local<v8::Object> obj_rx = v8::Object::New(isolate);

/*
* Get device infos
* Params:
* - devices_info: info about a HCI device.
*/
static void devices_info(struct hci_dev_info *device_info)
{
struct hci_dev_stats *device_stats = &device_info->stat;
char *device_status, *header;
char buffer[INTERFACE_DESCRIPTION_SIZE];
obj_rx->Set(v8::String::NewFromUtf8(isolate, "bytes"),
v8::Number::New(isolate, device_stats->byte_rx));

header = devices_header(device_info);
obj_rx->Set(v8::String::NewFromUtf8(isolate, "acl"),
v8::Number::New(isolate, device_stats->acl_rx));

if (!header)
{
printf("Error while filling with device header infos\n");
return;
}
obj_rx->Set(v8::String::NewFromUtf8(isolate, "sco"),
v8::Number::New(isolate, device_stats->sco_rx));

device_status = hci_dflagstostr(device_info->flags);
obj_rx->Set(v8::String::NewFromUtf8(isolate, "events"),
v8::Number::New(isolate, device_stats->evt_rx));

obj_rx->Set(v8::String::NewFromUtf8(isolate, "errors"),
v8::Number::New(isolate, device_stats->err_rx));

// TX
v8::Local<v8::Object> obj_tx = v8::Object::New(isolate);

obj_tx->Set(v8::String::NewFromUtf8(isolate, "bytes"),
v8::Number::New(isolate, device_stats->byte_tx));

obj_tx->Set(v8::String::NewFromUtf8(isolate, "acl"),
v8::Number::New(isolate, device_stats->acl_tx));

obj_tx->Set(v8::String::NewFromUtf8(isolate, "sco"),
v8::Number::New(isolate, device_stats->sco_tx));

obj_tx->Set(v8::String::NewFromUtf8(isolate, "events"),
v8::Number::New(isolate, device_stats->cmd_tx));

obj_tx->Set(v8::String::NewFromUtf8(isolate, "errors"),
v8::Number::New(isolate, device_stats->err_tx));

// Other info
v8::Local<v8::Object> obj_info = v8::Object::New(isolate);

obj_info->Set(v8::String::NewFromUtf8(isolate, "type"),
v8::String::NewFromUtf8(isolate,
hci_typetostr((device_info->type & 0x30) >> 4)));

obj_info->Set(v8::String::NewFromUtf8(isolate, "bus"),
v8::String::NewFromUtf8(isolate,
hci_bustostr(device_info->type & 0x0f)));

obj_info->Set(v8::String::NewFromUtf8(isolate, "address"),
v8::String::NewFromUtf8(isolate, mac_address));

snprintf(buffer, 100, "%d:%d", device_info->acl_mtu, device_info->acl_pkts);
obj_info->Set(v8::String::NewFromUtf8(isolate, "acl_mtu"),
v8::String::NewFromUtf8(isolate, buffer));

snprintf(buffer, 100, "%d:%d", device_info->sco_mtu, device_info->sco_pkts);
obj_info->Set(v8::String::NewFromUtf8(isolate, "sco_mtu"),
v8::String::NewFromUtf8(isolate, buffer));

obj_info->Set(v8::String::NewFromUtf8(isolate, "status"),
v8::String::NewFromUtf8(isolate, device_status));

// Construct the object
obj_info->Set(v8::String::NewFromUtf8(isolate, "rx"), obj_rx);
obj_info->Set(v8::String::NewFromUtf8(isolate, "tx"), obj_tx);

// Fill the buffer
snprintf(buffer, INTERFACE_DESCRIPTION_SIZE,
"%s, \"status\": \"%s\",\
\"RX\": {\"bytes\": %d, \"ACL\": %d, \"SCO\": %d, \"events\": %d, \"errors\": %d},\
\"TX\": {\"bytes\": %d, \"ACL\": %d, \"SCO\": %d, \"events\": %d, \"errors\": %d}}}",
header, device_status,
device_stats->byte_rx, device_stats->acl_rx,
device_stats->sco_rx, device_stats->evt_rx,
device_stats->err_rx,
device_stats->byte_tx, device_stats->acl_tx,
device_stats->sco_tx, device_stats->cmd_tx,
device_stats->err_tx);
v8::Local<v8::Object> obj = v8::Object::New(isolate);
obj->Set(v8::String::NewFromUtf8(isolate, device_info->name), obj_info);

// Add to the list
struct interface *bluetooth_interface
= (struct interface *)malloc(sizeof(struct interface));
strncpy(bluetooth_interface->description, buffer, 1024);
bluetooth_interface->description = obj;
STAILQ_INSERT_TAIL(&interfaces, bluetooth_interface, next);

free(header);
bt_free(device_status);
}

Expand All @@ -107,7 +124,7 @@ static void devices_info(struct hci_dev_info *device_info)
* - EXIT_FAILURE: on failure.
* - EXIT_SUCCESS: on success.
*/
static void list_devices(void)
static void list_devices(v8::Isolate *isolate)
{
struct hci_dev_list_req *devices_list;
struct hci_dev_req *dr;
Expand All @@ -119,7 +136,7 @@ static void list_devices(void)
return EXIT_FAILURE;
}

if (!(devices_list = (hci_dev_list_req *)malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req))))
if ((devices_list = (hci_dev_list_req *)malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req))) == NULL)
{
perror("Can't allocate memory");
return EXIT_FAILURE;
Expand Down Expand Up @@ -149,7 +166,7 @@ static void list_devices(void)
hci_close_dev(device);
}

devices_info(&device_info);
interface_infos(isolate, &device_info);
}

free(devices_list);
Expand All @@ -173,12 +190,12 @@ void HCI_list(const Nan::FunctionCallbackInfo<v8::Value>& info)
STAILQ_INIT(&interfaces);

// Get bluetooth interfaces
list_devices();
list_devices(isolate);

// Populate the array
STAILQ_FOREACH(bluetooth_interface, &interfaces, next)
{
array->Set(idx_item, v8::String::NewFromUtf8(isolate, bluetooth_interface->description));
array->Set(idx_item, bluetooth_interface->description);
idx_item++;
}

Expand Down
8 changes: 4 additions & 4 deletions hci_spoof_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,14 @@ int hci_spoof_mac(int device_id, char const *new_mac_address)
{
if (vendor[i].reset_device(descriptor) < 0)
{
// The device should be reset manually.
status = ECANCELED;
// The device should be reset manually.
status = ECANCELED;
}
else
{
// Reset the device
// Reset the device
ioctl(descriptor, HCIDEVRESET, device_id);
status = EXIT_SUCCESS;
status = EXIT_SUCCESS;
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btim",
"version": "1.0.1",
"version": "1.0.2",
"description": "Bindings to HCI functions to list bluetooth device interfaces, spoof a MAC address and bring an interface \"up\" or \"down\".",
"main": "index.js",
"dependencies": {
Expand Down