2016-04-28 01:22:00 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* GattLib - GATT Library
|
|
|
|
*
|
2019-06-27 14:39:40 +02:00
|
|
|
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
|
2016-04-28 01:22:00 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
2017-01-30 11:52:54 +01:00
|
|
|
#include <stdlib.h>
|
2016-04-28 01:22:00 +02:00
|
|
|
|
|
|
|
#include "gattlib.h"
|
|
|
|
|
|
|
|
typedef enum { READ, WRITE} operation_t;
|
|
|
|
operation_t g_operation;
|
|
|
|
|
2017-02-06 16:13:17 +01:00
|
|
|
static uuid_t g_uuid;
|
2016-04-28 01:22:00 +02:00
|
|
|
long int value_data;
|
|
|
|
|
|
|
|
static void usage(char *argv[]) {
|
|
|
|
printf("%s <device_address> <read|write> <uuid> [<hex-value-to-write>]\n", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2017-03-15 18:01:58 +01:00
|
|
|
int i, ret;
|
|
|
|
size_t len;
|
2016-04-28 01:22:00 +02:00
|
|
|
gatt_connection_t* connection;
|
|
|
|
|
|
|
|
if ((argc != 4) && (argc != 5)) {
|
|
|
|
usage(argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[2], "read") == 0) {
|
|
|
|
g_operation = READ;
|
|
|
|
} else if ((strcmp(argv[2], "write") == 0) && (argc == 5)) {
|
|
|
|
g_operation = WRITE;
|
|
|
|
|
2019-03-05 13:26:44 +01:00
|
|
|
if ((strlen(argv[4]) >= 2) && (argv[4][0] == '0') && ((argv[4][1] == 'x') || (argv[4][1] == 'X'))) {
|
2016-04-28 01:22:00 +02:00
|
|
|
value_data = strtol(argv[4], NULL, 16);
|
2019-03-05 13:26:44 +01:00
|
|
|
} else {
|
|
|
|
value_data = strtol(argv[4], NULL, 0);
|
2016-04-28 01:22:00 +02:00
|
|
|
}
|
|
|
|
printf("Value to write: 0x%lx\n", value_data);
|
|
|
|
} else {
|
|
|
|
usage(argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-06 16:13:17 +01:00
|
|
|
if (gattlib_string_to_uuid(argv[3], strlen(argv[3]) + 1, &g_uuid) < 0) {
|
2016-04-28 01:22:00 +02:00
|
|
|
usage(argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:53:19 +02:00
|
|
|
connection = gattlib_connect(NULL, argv[1], GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT);
|
2016-04-28 01:22:00 +02:00
|
|
|
if (connection == NULL) {
|
|
|
|
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_operation == READ) {
|
2019-06-26 17:34:57 +02:00
|
|
|
uint8_t *buffer = NULL;
|
2019-05-27 17:21:59 +02:00
|
|
|
|
|
|
|
ret = gattlib_read_char_by_uuid(connection, &g_uuid, (void **)&buffer, &len);
|
2019-06-26 17:34:57 +02:00
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
2019-03-05 17:14:45 +01:00
|
|
|
char uuid_str[MAX_LEN_UUID_STR + 1];
|
|
|
|
|
|
|
|
gattlib_uuid_to_string(&g_uuid, uuid_str, sizeof(uuid_str));
|
|
|
|
|
2019-06-27 14:39:40 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-03-05 17:14:45 +01:00
|
|
|
goto EXIT;
|
|
|
|
}
|
2016-04-28 01:22:00 +02:00
|
|
|
|
|
|
|
printf("Read UUID completed: ");
|
2019-03-05 17:14:45 +01:00
|
|
|
for (i = 0; i < len; i++) {
|
2016-04-28 01:22:00 +02:00
|
|
|
printf("%02x ", buffer[i]);
|
2019-03-05 17:14:45 +01:00
|
|
|
}
|
2016-04-28 01:22:00 +02:00
|
|
|
printf("\n");
|
2019-05-27 17:21:59 +02:00
|
|
|
|
|
|
|
free(buffer);
|
2016-04-28 01:22:00 +02:00
|
|
|
} else {
|
2019-05-27 17:21:59 +02:00
|
|
|
ret = gattlib_write_char_by_uuid(connection, &g_uuid, &value_data, sizeof(value_data));
|
2019-06-26 17:34:57 +02:00
|
|
|
if (ret != GATTLIB_SUCCESS) {
|
2019-03-05 17:14:45 +01:00
|
|
|
char uuid_str[MAX_LEN_UUID_STR + 1];
|
|
|
|
|
|
|
|
gattlib_uuid_to_string(&g_uuid, uuid_str, sizeof(uuid_str));
|
|
|
|
|
2019-06-27 14:39:40 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-03-05 17:14:45 +01:00
|
|
|
goto EXIT;
|
|
|
|
}
|
2016-04-28 01:22:00 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 17:14:45 +01:00
|
|
|
EXIT:
|
2016-04-28 01:22:00 +02:00
|
|
|
gattlib_disconnect(connection);
|
2019-03-05 17:14:45 +01:00
|
|
|
return ret;
|
2016-04-28 01:22:00 +02:00
|
|
|
}
|