mirror of https://github.com/labapart/gattlib
examples/read_write_memory: Added read/write for demonstrating memory management
parent
967b46bb99
commit
53bb794a7d
|
@ -97,6 +97,7 @@ if(GATTLIB_BUILD_EXAMPLES)
|
|||
add_subdirectory(examples/ble_scan)
|
||||
add_subdirectory(examples/discover)
|
||||
add_subdirectory(examples/read_write)
|
||||
add_subdirectory(examples/read_write_memory)
|
||||
add_subdirectory(examples/notification)
|
||||
add_subdirectory(examples/nordic_uart)
|
||||
|
||||
|
|
|
@ -116,7 +116,11 @@ Examples
|
|||
|
||||
./examples/nordic_uart/nordic_uart
|
||||
|
||||
**Note:** `examples/gatttool` has been partially ported to gattlib. There are two reasons: the laziness
|
||||
**Note 1:** [The example 'read/write mem'](/examples/read_write_mem/read_write.c) is similar to
|
||||
[the example 'read/write'](/examples/read_write/read_write.c) except a GLib loop is used to allows
|
||||
the memory to be freed by Glib. Without this loop, some memory could be locked.
|
||||
|
||||
**Note 2:** `examples/gatttool` has been partially ported to gattlib. There are two reasons: the laziness
|
||||
(some of the GATT functions could be replaced by their gattlib equivalent) and the completeness (there
|
||||
are still some missing functions in gattlib).
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
#
|
||||
#
|
||||
# GattLib - GATT Library
|
||||
#
|
||||
# Copyright (C) 2016-2017 Olivier Martin <olivier@labapart.org>
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
pkg_search_module(GATTLIB REQUIRED gattlib)
|
||||
pkg_search_module(GLIB REQUIRED glib-2.0)
|
||||
pkg_search_module(PCRE REQUIRED libpcre)
|
||||
|
||||
include_directories(${GLIB_INCLUDE_DIRS})
|
||||
set(read_write_memory_SRCS read_write_memory.c)
|
||||
|
||||
add_executable(read_write_memory ${read_write_memory_SRCS})
|
||||
target_link_libraries(read_write_memory ${GATTLIB_LIBRARIES} ${GATTLIB_LDFLAGS} ${GLIB_LDFLAGS} ${PCRE_LIBRARIES} pthread)
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
*
|
||||
* GattLib - GATT Library
|
||||
*
|
||||
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <glib.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gattlib.h"
|
||||
|
||||
static uuid_t m_uuid;
|
||||
static GMainLoop *m_main_loop;
|
||||
|
||||
static void usage(char *argv[]) {
|
||||
printf("%s <device_address> <read|write> <uuid> [<hex-value-to-write>]\n", argv[0]);
|
||||
}
|
||||
|
||||
struct connect_ble_params {
|
||||
const char *mac_address;
|
||||
enum { READ, WRITE} operation;
|
||||
long int value_data;
|
||||
};
|
||||
|
||||
void *connect_ble(void *arg) {
|
||||
struct connect_ble_params *params = arg;
|
||||
gatt_connection_t* connection;
|
||||
int ret, i;
|
||||
size_t len;
|
||||
|
||||
connection = gattlib_connect(NULL, params->mac_address, GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
|
||||
if (connection == NULL) {
|
||||
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (params->operation == READ) {
|
||||
for (int j = 0; j < 40; j++) {
|
||||
uint8_t *buffer = NULL;
|
||||
|
||||
ret = gattlib_read_char_by_uuid(connection, &m_uuid, (void **)&buffer, &len);
|
||||
if (ret != GATTLIB_SUCCESS) {
|
||||
char uuid_str[MAX_LEN_UUID_STR + 1];
|
||||
|
||||
gattlib_uuid_to_string(&m_uuid, uuid_str, sizeof(uuid_str));
|
||||
|
||||
if (ret == GATTLIB_NOT_FOUND) {
|
||||
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
|
||||
"You might call the program with '--gatt-discovery'.\n", uuid_str);
|
||||
} else {
|
||||
fprintf(stderr, "Error while reading GATT Characteristic with UUID %s (ret:%d)\n", uuid_str, ret);
|
||||
}
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
printf("Read UUID completed: ");
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x ", buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
} else {
|
||||
ret = gattlib_write_char_by_uuid(connection, &m_uuid, ¶ms->value_data, sizeof(params->value_data));
|
||||
if (ret != GATTLIB_SUCCESS) {
|
||||
char uuid_str[MAX_LEN_UUID_STR + 1];
|
||||
|
||||
gattlib_uuid_to_string(&m_uuid, uuid_str, sizeof(uuid_str));
|
||||
|
||||
if (ret == GATTLIB_NOT_FOUND) {
|
||||
fprintf(stderr, "Could not find GATT Characteristic with UUID %s. "
|
||||
"You might call the program with '--gatt-discovery'.\n", uuid_str);
|
||||
} else {
|
||||
fprintf(stderr, "Error while writing GATT Characteristic with UUID %s (ret:%d)\n",
|
||||
uuid_str, ret);
|
||||
}
|
||||
goto EXIT;
|
||||
}
|
||||
}
|
||||
|
||||
EXIT:
|
||||
gattlib_disconnect(connection);
|
||||
g_main_loop_quit(m_main_loop);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
pthread_t tid;
|
||||
|
||||
if ((argc != 4) && (argc != 5)) {
|
||||
usage(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct connect_ble_params params = {
|
||||
.mac_address = argv[1],
|
||||
};
|
||||
|
||||
if (strcmp(argv[2], "read") == 0) {
|
||||
params.operation = READ;
|
||||
} else if ((strcmp(argv[2], "write") == 0) && (argc == 5)) {
|
||||
params.operation = WRITE;
|
||||
|
||||
if ((strlen(argv[4]) >= 2) && (argv[4][0] == '0') && ((argv[4][1] == 'x') || (argv[4][1] == 'X'))) {
|
||||
params.value_data = strtol(argv[4], NULL, 16);
|
||||
} else {
|
||||
params.value_data = strtol(argv[4], NULL, 0);
|
||||
}
|
||||
printf("Value to write: 0x%lx\n", params.value_data);
|
||||
} else {
|
||||
usage(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (gattlib_string_to_uuid(argv[3], strlen(argv[3]) + 1, &m_uuid) < 0) {
|
||||
usage(argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
m_main_loop = g_main_loop_new(NULL, 0);
|
||||
|
||||
pthread_create(&tid, NULL, connect_ble, ¶ms);
|
||||
|
||||
g_main_loop_run(m_main_loop);
|
||||
g_main_loop_unref(m_main_loop);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue