Skip to content

Commit

Permalink
Tool working on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zaddach committed Aug 8, 2022
1 parent ca9c154 commit be481ee
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 108 deletions.
15 changes: 12 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ to build the project.

On Windows, you'll need `vcpkg` with the `getopt` package installed. Run
```powershell
cmake -T ClangCL -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" ..
cmake -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" ..
```
to create the build files (You'll need Visual Studio 2019 with the clang build
tools installed).
to create the build files.

Installing
----------
Expand All @@ -98,6 +97,16 @@ Alternatively, if you run Arch Linux, you can download it from the AUR here:

https://aur.archlinux.org/packages/based-connect-git/

Running
-------

On Windows, use
```powershell
Get-PnpDevice -class Bluetooth | Select-Object -Property FriendlyName,
DeviceID
```
to enumerate the Bluetooth device ID.

Dependencies
------------

Expand Down
67 changes: 34 additions & 33 deletions based.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#ifdef _WIN32
#include <winsock2.h>
#define read(sock, buf, size) recv(sock, (void *) buf, (int) size, 0)
#define write(sock, buf, size) send(sock, (char *) buf, size, 0)
#define write(sock, buf, size) send(sock, (char *) buf, (int) size, 0)
#define bswap_16(x) _byteswap_ushort(x)
#else
#include <unistd.h>
#endif

#include "based.h"
#include "bluetooth.h"
#include "malloc.h"

#define ANY 0x00
#define CN_BASE_PACK_LEN 4
Expand Down Expand Up @@ -44,39 +45,39 @@ static int masked_memcmp(const void *ptr1, const void *ptr2, size_t num, const v
return 0;
}

static int read_check(int sock, void *recieve, size_t recieve_n, const void *ack,
static int read_check(socktype_t sock, void *receive, size_t receive_n, const void *ack,
const void *mask) {
int status = read(sock, recieve, recieve_n);
if (status != recieve_n) {
int status = read(sock, receive, receive_n);
if (status != receive_n) {
return status ? status : 1;
}

return abs(mask
? masked_memcmp(ack, recieve, recieve_n, mask)
: memcmp(ack, recieve, recieve_n));
? masked_memcmp(ack, receive, receive_n, mask)
: memcmp(ack, receive, receive_n));
}

static int write_check(int sock, const void *send_b, size_t send_n,
static int write_check(socktype_t sock, const void *send_b, size_t send_n,
const void *ack, size_t ack_n) {
uint8_t buffer[ack_n];
uint8_t *buffer = alloca(ack_n);

int status = write(sock, send_b, send_n);
if (status != send_n) {
return status ? status : 1;
}
return read_check(sock, buffer, sizeof(buffer), ack, NULL);
return read_check(sock, buffer, ack_n, ack, NULL);
}

int send_packet(int sock, const void *send_b, size_t send_n, uint8_t recieved[MAX_BT_PACK_LEN]) {
int send_packet(socktype_t sock, const void *send_b, size_t send_n, uint8_t received[MAX_BT_PACK_LEN]) {
int status = write(sock, send_b, send_n);
if (status != send_n) {
return status ? status : 1;
}

return read(sock, recieved, MAX_BT_PACK_LEN);
return read(sock, received, MAX_BT_PACK_LEN);
}

int init_connection(int sock) {
int init_connection(socktype_t sock) {
static const uint8_t send_buf[] = { 0x00, 0x01, 0x01, 0x00 };
static const uint8_t ack[] = { 0x00, 0x01, 0x03, 0x05 };

Expand All @@ -96,7 +97,7 @@ int init_connection(int sock) {
return 0;
}

int get_device_id(int sock, unsigned int *device_id, unsigned int *index) {
int get_device_id(socktype_t sock, unsigned int *device_id, unsigned int *index) {
static const uint8_t send_buf[] = { 0x00, 0x03, 0x01, 0x00 };
static const uint8_t ack[] = { 0x00, 0x03, 0x03, 0x03 };

Expand All @@ -123,7 +124,7 @@ int get_device_id(int sock, unsigned int *device_id, unsigned int *index) {
return 0;
}

static int get_name(int sock, char name[MAX_NAME_LEN + 1]) {
static int get_name(socktype_t sock, char name[MAX_NAME_LEN + 1]) {
static const uint8_t ack[] = { 0x01, 0x02, 0x03, ANY, 0x00 };
static const uint8_t mask[] = { 0xff, 0xff, 0xff, 0x00, 0xff };
uint8_t buffer[sizeof(ack)];
Expand All @@ -143,11 +144,11 @@ static int get_name(int sock, char name[MAX_NAME_LEN + 1]) {
return 0;
}

int set_name(int sock, const char *name) {
int set_name(socktype_t sock, const char *name) {
static uint8_t send_buf[CN_BASE_PACK_LEN + MAX_NAME_LEN] = { 0x01, 0x02, 0x02, ANY };
size_t length = strlen(name);

send_buf[3] = length;
send_buf[3] = (uint8_t) length;
strncpy_s((char *) &send_buf[CN_BASE_PACK_LEN], sizeof(send_buf) - CN_BASE_PACK_LEN, name, MAX_NAME_LEN);

size_t send_size = CN_BASE_PACK_LEN + length;
Expand All @@ -165,7 +166,7 @@ int set_name(int sock, const char *name) {
return abs(strcmp(name, got_name));
}

static int get_prompt_language(int sock, enum PromptLanguage *language) {
static int get_prompt_language(socktype_t sock, enum PromptLanguage *language) {
// TODO: ensure that this value is correct
// TODO: figure out what bytes 6 and 7 are for
static const uint8_t ack[] = { 0x01, 0x03, 0x03, 0x05, ANY, 0x00, ANY, ANY, 0xde };
Expand All @@ -181,7 +182,7 @@ static int get_prompt_language(int sock, enum PromptLanguage *language) {
return 0;
}

int set_prompt_language(int sock, enum PromptLanguage language) {
int set_prompt_language(socktype_t sock, enum PromptLanguage language) {
static uint8_t send_buf[] = { 0x01, 0x03, 0x02, 0x01, ANY };
send_buf[4] = language;

Expand All @@ -199,7 +200,7 @@ int set_prompt_language(int sock, enum PromptLanguage language) {
return abs(language - got_language);
}

int set_voice_prompts(int sock, int on) {
int set_voice_prompts(socktype_t sock, int on) {
char name[MAX_NAME_LEN + 1];
enum PromptLanguage pl;
enum AutoOff ao;
Expand All @@ -219,7 +220,7 @@ int set_voice_prompts(int sock, int on) {
return set_prompt_language(sock, pl);
}

static int get_auto_off(int sock, enum AutoOff *minutes) {
static int get_auto_off(socktype_t sock, enum AutoOff *minutes) {
static const uint8_t ack[] = { 0x01, 0x04, 0x03, 0x01, ANY };
static const uint8_t mask[] = { 0xff, 0xff, 0xff, 0xff, 0x00 };
uint8_t buffer[sizeof(ack)];
Expand All @@ -233,7 +234,7 @@ static int get_auto_off(int sock, enum AutoOff *minutes) {
return 0;
}

int set_auto_off(int sock, enum AutoOff minutes) {
int set_auto_off(socktype_t sock, enum AutoOff minutes) {
static uint8_t send_buf[] = { 0x01, 0x04, 0x02, 0x01, ANY };
send_buf[4] = minutes;

Expand All @@ -251,7 +252,7 @@ int set_auto_off(int sock, enum AutoOff minutes) {
return abs(minutes - got_minutes);
}

static int get_noise_cancelling(int sock, enum NoiseCancelling *level) {
static int get_noise_cancelling(socktype_t sock, enum NoiseCancelling *level) {
static const uint8_t ack[] = { 0x01, 0x06, 0x03, 0x02, ANY, 0x0b };
static const uint8_t mask[] = { 0xff, 0xff, 0xff, 0xff, 0x00, 0xff };
uint8_t buffer[sizeof(ack)];
Expand All @@ -265,7 +266,7 @@ static int get_noise_cancelling(int sock, enum NoiseCancelling *level) {
return 0;
}

int set_noise_cancelling(int sock, enum NoiseCancelling level) {
int set_noise_cancelling(socktype_t sock, enum NoiseCancelling level) {
static uint8_t send_buf[] = { 0x01, 0x06, 0x02, 0x01, ANY };
send_buf[4] = level;

Expand All @@ -283,7 +284,7 @@ int set_noise_cancelling(int sock, enum NoiseCancelling level) {
return abs(level - got_level);
}

int get_device_status(int sock, char name[MAX_NAME_LEN + 1], enum PromptLanguage *language,
int get_device_status(socktype_t sock, char name[MAX_NAME_LEN + 1], enum PromptLanguage *language,
enum AutoOff *minutes, enum NoiseCancelling *level) {
unsigned int device_id;
unsigned int index;
Expand Down Expand Up @@ -335,15 +336,15 @@ int get_device_status(int sock, char name[MAX_NAME_LEN + 1], enum PromptLanguage
return read_check(sock, buffer2, sizeof(buffer2), ack2, NULL);
}

int set_pairing(int sock, enum Pairing pairing) {
int set_pairing(socktype_t sock, enum Pairing pairing) {
static uint8_t send_buf[] = { 0x04, 0x08, 0x05, 0x01, ANY };
static uint8_t ack[] = { 0x04, 0x08, 0x06, 0x01, ANY };
send_buf[4] = pairing;
ack[4] = pairing;
return write_check(sock, send_buf, sizeof(send_buf), ack, sizeof(ack));
}

int get_firmware_version(int sock, char version[VER_STR_LEN]) {
int get_firmware_version(socktype_t sock, char version[VER_STR_LEN]) {
static const uint8_t send_buf[] = { 0x00, 0x05, 0x01, 0x00 };
static const uint8_t ack[] = { 0x00, 0x05, 0x03, 0x05 };

Expand All @@ -361,7 +362,7 @@ int get_firmware_version(int sock, char version[VER_STR_LEN]) {
return 0;
}

int get_serial_number(int sock, char serial[0x100]) {
int get_serial_number(socktype_t sock, char serial[0x100]) {
static const uint8_t send_buf[] = { 0x00, 0x07, 0x01, 0x00 };
static const uint8_t ack[] = { 0x00, 0x07, 0x03 };

Expand All @@ -385,7 +386,7 @@ int get_serial_number(int sock, char serial[0x100]) {
return 0;
}

int get_battery_level(int sock, unsigned int *level) {
int get_battery_level(socktype_t sock, unsigned int *level) {
static const uint8_t send_buf[] = { 0x02, 0x02, 0x01, 0x00 };
static const uint8_t ack[] = { 0x02, 0x02, 0x03, 0x01 };

Expand All @@ -400,7 +401,7 @@ int get_battery_level(int sock, unsigned int *level) {
return 0;
}

int get_device_info(int sock, bdaddr_t address, struct Device *device) {
int get_device_info(socktype_t sock, bdaddr_t address, struct Device *device) {
static uint8_t send_buf[10] = { 0x04, 0x05, 0x01, BT_ADDR_LEN };
static const uint8_t ack[] = { 0x04, 0x05, 0x03 };

Expand Down Expand Up @@ -454,7 +455,7 @@ int get_device_info(int sock, bdaddr_t address, struct Device *device) {
return 0;
}

int get_paired_devices(int sock, bdaddr_t addresses[MAX_NUM_DEVICES], size_t *num_devices,
int get_paired_devices(socktype_t sock, bdaddr_t addresses[MAX_NUM_DEVICES], size_t *num_devices,
enum DevicesConnected *connected) {
static const uint8_t send_buf[] = { 0x04, 0x04, 0x01, 0x00 };
static const uint8_t ack[] = { 0x04, 0x04, 0x03 };
Expand Down Expand Up @@ -494,23 +495,23 @@ int get_paired_devices(int sock, bdaddr_t addresses[MAX_NUM_DEVICES], size_t *nu
return 0;
}

int connect_device(int sock, bdaddr_t address) {
int connect_device(socktype_t sock, bdaddr_t address) {
static uint8_t send_buf[11] = { 0x04, 0x01, 0x05, BT_ADDR_LEN + 1, 0x00 };
static uint8_t ack[10] = { 0x04, 0x01, 0x07, BT_ADDR_LEN };
memcpy(&send_buf[5], &BDADDR_BYTES(address), BT_ADDR_LEN);
memcpy(&ack[4], &BDADDR_BYTES(address), BT_ADDR_LEN);
return write_check(sock, send_buf, sizeof(send_buf), ack, sizeof(ack));
}

int disconnect_device(int sock, bdaddr_t address) {
int disconnect_device(socktype_t sock, bdaddr_t address) {
static uint8_t send_buf[10] = { 0x04, 0x02, 0x05, BT_ADDR_LEN };
static uint8_t ack[10] = { 0x04, 0x02, 0x07, BT_ADDR_LEN };
memcpy(&send_buf[4], &BDADDR_BYTES(address), BT_ADDR_LEN);
memcpy(&ack[4], &BDADDR_BYTES(address), BT_ADDR_LEN);
return write_check(sock, send_buf, sizeof(send_buf), ack, sizeof(ack));
}

int remove_device(int sock, bdaddr_t address) {
int remove_device(socktype_t sock, bdaddr_t address) {
static uint8_t send_buf[10] = { 0x04, 0x03, 0x05, BT_ADDR_LEN };
static uint8_t ack[10] = { 0x04, 0x03, 0x06, BT_ADDR_LEN };
memcpy(&send_buf[4], &BDADDR_BYTES(address), BT_ADDR_LEN);
Expand Down
43 changes: 19 additions & 24 deletions based.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#ifndef BASED_H
#define BASED_H

#ifdef _WIN32
#include <winsock2.h>
#include <stdint.h>
#else
#include <bluetooth/bluetooth.h>
#endif
#include "socket.h"

#include <stddef.h>

Expand Down Expand Up @@ -72,25 +67,25 @@ struct Device {
};

int has_noise_cancelling(unsigned int device_id);
int init_connection(int sock);
int send_packet(int sock, const void *send, size_t send_n, uint8_t recieved[MAX_BT_PACK_LEN]);
int get_device_id(int sock, unsigned int *device_id, unsigned int *index);
int set_name(int sock, const char *name);
int set_prompt_language(int sock, enum PromptLanguage language);
int set_voice_prompts(int sock, int on);
int set_auto_off(int sock, enum AutoOff minutes);
int set_noise_cancelling(int sock, enum NoiseCancelling level);
int get_device_status(int sock, char name[MAX_NAME_LEN + 1], enum PromptLanguage *language,
int init_connection(socktype_t sock);
int send_packet(socktype_t sock, const void *send, size_t send_n, uint8_t recieved[MAX_BT_PACK_LEN]);
int get_device_id(socktype_t sock, unsigned int *device_id, unsigned int *index);
int set_name(socktype_t sock, const char *name);
int set_prompt_language(socktype_t sock, enum PromptLanguage language);
int set_voice_prompts(socktype_t sock, int on);
int set_auto_off(socktype_t sock, enum AutoOff minutes);
int set_noise_cancelling(socktype_t sock, enum NoiseCancelling level);
int get_device_status(socktype_t sock, char name[MAX_NAME_LEN + 1], enum PromptLanguage *language,
enum AutoOff *minutes, enum NoiseCancelling *level);
int set_pairing(int sock, enum Pairing pairing);
int get_firmware_version(int sock, char version[VER_STR_LEN]);
int get_serial_number(int sock, char serial[0x100]);
int get_battery_level(int sock, unsigned int *level);
int get_device_info(int sock, bdaddr_t address, struct Device *device);
int get_paired_devices(int sock, bdaddr_t addresses[MAX_NUM_DEVICES], size_t *num_devices,
int set_pairing(socktype_t sock, enum Pairing pairing);
int get_firmware_version(socktype_t sock, char version[VER_STR_LEN]);
int get_serial_number(socktype_t sock, char serial[0x100]);
int get_battery_level(socktype_t sock, unsigned int *level);
int get_device_info(socktype_t sock, bdaddr_t address, struct Device *device);
int get_paired_devices(socktype_t sock, bdaddr_t addresses[MAX_NUM_DEVICES], size_t *num_devices,
enum DevicesConnected *connected);
int connect_device(int sock, bdaddr_t address);
int disconnect_device(int sock, bdaddr_t address);
int remove_device(int sock, bdaddr_t address);
int connect_device(socktype_t sock, bdaddr_t address);
int disconnect_device(socktype_t sock, bdaddr_t address);
int remove_device(socktype_t sock, bdaddr_t address);

#endif
6 changes: 3 additions & 3 deletions bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Based on code taken from the BlueZ library.
*/

int reverse_ba2str(const bdaddr_t *ba, char *str) {
return sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
int reverse_ba2str(const bdaddr_t *ba, char *str, size_t str_len) {
return sprintf_s(str, str_len, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
BDADDR_BYTES(*ba)[0], BDADDR_BYTES(*ba)[1], BDADDR_BYTES(*ba)[2], BDADDR_BYTES(*ba)[3], BDADDR_BYTES(*ba)[4], BDADDR_BYTES(*ba)[5]);
}

Expand All @@ -28,7 +28,7 @@ int reverse_str2ba(const char *str, bdaddr_t *ba) {
}

for (i = 0; i < 6; i++, str += 3)
BDADDR_BYTES(*ba)[i] = strtol(str, NULL, 16);
BDADDR_BYTES(*ba)[i] = (uint8_t) strtol(str, NULL, 16);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define BT_ADDR_LEN 6

int reverse_ba2str(const bdaddr_t *ba, char *str);
int reverse_ba2str(const bdaddr_t *ba, char *str, size_t str_len);
int reverse_str2ba(const char *str, bdaddr_t *ba);
#ifdef _WIN32
int bachk(const char *str);
Expand Down
Loading

0 comments on commit be481ee

Please sign in to comment.