Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Addressed issue #182 #186

Open
wants to merge 2 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
18 changes: 16 additions & 2 deletions api/tinyb/BluetoothGattCharacteristic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,26 @@ friend class BluetoothNotificationHandler;
uint16_t offset = 0
);

/** Writes the value of this characteristic.
/** Writes the value of this characteristic with requiring a response.
* @param[in] arg_value The data as vector<uchar>
* to be written packed in a GBytes struct
* @return TRUE if value was written succesfully
*/
bool write_value (const std::vector<unsigned char> &arg_value, uint16_t offset = 0);
bool write_value_request (const std::vector<unsigned char> &arg_value, uint16_t offset = 0);

/** Writes the value of this characteristic without requiring a response.
* @param[in] arg_value The data as vector<uchar>
* to be written packed in a GBytes struct
* @return TRUE if value was written succesfully
*/
bool write_value_command (const std::vector<unsigned char> &arg_value, uint16_t offset = 0);

/** Writes the value of this characteristic. (deprecated, here for backwards compatibility)
*/
inline bool write_value (const std::vector<unsigned char> &arg_value, uint16_t offset = 0)
{
return write_value_request (arg_value, offset);
}

/**
* Enables notifications (including at BLE level) for changes of the
Expand Down
38 changes: 37 additions & 1 deletion src/BluetoothGattCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ std::vector<unsigned char> BluetoothGattCharacteristic::read_value (uint16_t off
return result;
}

bool BluetoothGattCharacteristic::write_value (
bool BluetoothGattCharacteristic::write_value_request (
const std::vector<unsigned char> &arg_value, uint16_t offset)
{
GError *error = NULL;
Expand All @@ -166,6 +166,42 @@ bool BluetoothGattCharacteristic::write_value (
if (offset != 0)
g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset));

g_variant_dict_insert_value(&dict, "type", g_variant_new_string("request"));

GVariant *variant = g_variant_dict_end(&dict);

result = gatt_characteristic1_call_write_value_sync(
object,
arg_value_gbytes,
variant,
NULL,
&error
);

handle_error(error);

/* freeing the GBytes allocated inside from_vector_to_gbytes function */
g_bytes_unref(arg_value_gbytes);

return result;
}

bool BluetoothGattCharacteristic::write_value_command (
const std::vector<unsigned char> &arg_value, uint16_t offset)
{
GError *error = NULL;
bool result = true;

GBytes *arg_value_gbytes = from_vector_to_gbytes(arg_value);

GVariantDict dict;
g_variant_dict_init(&dict, NULL);

if (offset != 0)
g_variant_dict_insert_value(&dict, "offset", g_variant_new_uint16(offset));

g_variant_dict_insert_value(&dict, "type", g_variant_new_string("command"));

GVariant *variant = g_variant_dict_end(&dict);

result = gatt_characteristic1_call_write_value_sync(
Expand Down
18 changes: 9 additions & 9 deletions src/BluetoothUUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BluetoothUUID::BluetoothUUID(const char str[]) {

if (len == 4 || len == 8) {
/* 16bit or 32bit UUID: number + base UUID */
uuid[0] = strtoul(str, NULL, 16) << 32 | 0x00001000ULL;
uuid[0] = strtoull(str, NULL, 16) << 32 | 0x00001000ULL;
uuid[1] = 0x800000805f9b34fbULL;
} else if (len == 36) {
/* 128bit UUID */
Expand All @@ -24,25 +24,25 @@ BluetoothUUID::BluetoothUUID(const char str[]) {

if (u[8] == '-') {
u[8] = ' ';
uuid[0] = strtoul(u + 0, NULL, 16) << 32;
uuid[0] = strtoull(u + 0, NULL, 16) << 32;
} else {
throw std::invalid_argument(err_msg);
}
if (u[13] == '-') {
u[13] = ' ';
uuid[0] = uuid[0] | strtoul(u + 9, NULL, 16) << 16;
uuid[0] = uuid[0] | strtoull(u + 9, NULL, 16) << 16;
} else throw std::invalid_argument(err_msg);
if (u[18] == '-') {
u[18] = ' ';
uuid[0] = uuid[0] | strtoul(u + 14, NULL, 16);
uuid[0] = uuid[0] | strtoull(u + 14, NULL, 16);
} else throw std::invalid_argument(err_msg);

if (u[23] == '-') {
u[23] = ' ';
uuid[1] = strtoul(u + 19, NULL, 16) << 48;
uuid[1] = strtoull(u + 19, NULL, 16) << 48;
} else throw std::invalid_argument(err_msg);

uuid[1] = uuid[1] | strtoul(u + 24, NULL, 16);
uuid[1] = uuid[1] | strtoull(u + 24, NULL, 16);
} else throw std::invalid_argument(err_msg);
}

Expand All @@ -51,7 +51,7 @@ BluetoothUUID::BluetoothUUID(const std::string &str) : BluetoothUUID(str.c_str()
std::string BluetoothUUID::get_string()
{
char u[37];
snprintf(u, 37, "%08lx-%04lx-%04lx-%04lx-%012lx",
snprintf(u, 37, "%08llx-%04llx-%04llx-%04llx-%012llx",
(uuid[0] >> 32),
((uuid[0] >> 16) & 0xFFFFULL),
(uuid[0] & 0xFFFFULL),
Expand All @@ -66,9 +66,9 @@ std::string BluetoothUUID::get_short_string()
if (is_short()) {
uint32_t suuid = get_short();
if (suuid & 0xFFFF == suuid)
snprintf(u, 9, "%04lx", suuid);
snprintf(u, 9, "%04llx", suuid);
else
snprintf(u, 9, "%08lx", suuid);
snprintf(u, 9, "%08llx", suuid);
return std::string(u);
} else {
return get_string();
Expand Down