#include <string.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
STATE_CONNECTED
} conn_state;
+#define error(fmt, arg...) \
+ rl_printf(COLOR_RED "Error: " COLOR_OFF fmt, ## arg)
+
+#define failed(fmt, arg...) \
+ rl_printf(COLOR_RED "Command Failed: " COLOR_OFF fmt, ## arg)
+
static char *get_prompt(void)
{
if (conn_state == STATE_CONNECTED)
handle);
break;
default:
- rl_printf("Invalid opcode\n");
+ error("Invalid opcode\n");
return;
}
{
if (err) {
set_state(STATE_DISCONNECTED);
- rl_printf("%s\n", err->message);
+ error("%s\n", err->message);
return;
}
GSList *l;
if (status) {
- rl_printf("Discover all primary services failed: %s\n",
- att_ecode2str(status));
+ error("Discover all primary services failed: %s\n",
+ att_ecode2str(status));
return;
}
if (services == NULL) {
- rl_printf("No primary service found\n");
+ error("No primary service found\n");
return;
}
GSList *l;
if (status) {
- rl_printf("Discover primary services by UUID failed: %s\n",
+ error("Discover primary services by UUID failed: %s\n",
att_ecode2str(status));
return;
}
if (ranges == NULL) {
- rl_printf("No service UUID found\n");
+ error("No service UUID found\n");
return;
}
GSList *l;
if (status) {
- rl_printf("Find included services failed: %s\n",
+ error("Find included services failed: %s\n",
att_ecode2str(status));
return;
}
GSList *l;
if (status) {
- rl_printf("Discover all characteristics failed: %s\n",
+ error("Discover all characteristics failed: %s\n",
att_ecode2str(status));
return;
}
GString *s;
if (status != 0) {
- rl_printf("Characteristic value/descriptor read failed: %s\n",
+ error("Characteristic value/descriptor read failed: %s\n",
att_ecode2str(status));
return;
}
vlen = dec_read_resp(pdu, plen, value, sizeof(value));
if (vlen < 0) {
- rl_printf("Protocol error\n");
+ error("Protocol error\n");
return;
}
goto done;
if (status != 0) {
- rl_printf("Read characteristics by UUID failed: %s\n",
+ error("Read characteristics by UUID failed: %s\n",
att_ecode2str(status));
goto done;
}
}
if (opt_dst == NULL) {
- rl_printf("Remote Bluetooth address required\n");
+ error("Remote Bluetooth address required\n");
return;
}
opt_psm, opt_mtu, connect_cb, &gerr);
if (iochannel == NULL) {
set_state(STATE_DISCONNECTED);
- rl_printf("%s\n", gerr->message);
+ error("%s\n", gerr->message);
g_error_free(gerr);
} else
g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL);
bt_uuid_t uuid;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
}
if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
- rl_printf("Invalid UUID\n");
+ error("Invalid UUID\n");
return;
}
int end = 0xffff;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
if (argcp > 1) {
start = strtohandle(argvp[1]);
if (start < 0) {
- rl_printf("Invalid start handle: %s\n", argvp[1]);
+ error("Invalid start handle: %s\n", argvp[1]);
return;
}
end = start;
if (argcp > 2) {
end = strtohandle(argvp[2]);
if (end < 0) {
- rl_printf("Invalid end handle: %s\n", argvp[2]);
+ error("Invalid end handle: %s\n", argvp[2]);
return;
}
}
int end = 0xffff;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
if (argcp > 1) {
start = strtohandle(argvp[1]);
if (start < 0) {
- rl_printf("Invalid start handle: %s\n", argvp[1]);
+ error("Invalid start handle: %s\n", argvp[1]);
return;
}
}
if (argcp > 2) {
end = strtohandle(argvp[2]);
if (end < 0) {
- rl_printf("Invalid end handle: %s\n", argvp[2]);
+ error("Invalid end handle: %s\n", argvp[2]);
return;
}
}
bt_uuid_t uuid;
if (bt_string_to_uuid(&uuid, argvp[3]) < 0) {
- rl_printf("Invalid UUID\n");
+ error("Invalid UUID\n");
return;
}
static void cmd_char_desc(int argcp, char **argvp)
{
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
if (argcp > 1) {
start = strtohandle(argvp[1]);
if (start < 0) {
- rl_printf("Invalid start handle: %s\n", argvp[1]);
+ error("Invalid start handle: %s\n", argvp[1]);
return;
}
} else
if (argcp > 2) {
end = strtohandle(argvp[2]);
if (end < 0) {
- rl_printf("Invalid end handle: %s\n", argvp[2]);
+ error("Invalid end handle: %s\n", argvp[2]);
return;
}
} else
int handle;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
if (argcp < 2) {
- rl_printf("Missing argument: handle\n");
+ error("Missing argument: handle\n");
return;
}
handle = strtohandle(argvp[1]);
if (handle < 0) {
- rl_printf("Invalid handle: %s\n", argvp[1]);
+ error("Invalid handle: %s\n", argvp[1]);
return;
}
bt_uuid_t uuid;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
if (argcp < 2) {
- rl_printf("Missing argument: UUID\n");
+ error("Missing argument: UUID\n");
return;
}
if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
- rl_printf("Invalid UUID\n");
+ error("Invalid UUID\n");
return;
}
if (argcp > 2) {
start = strtohandle(argvp[2]);
if (start < 0) {
- rl_printf("Invalid start handle: %s\n", argvp[1]);
+ error("Invalid start handle: %s\n", argvp[1]);
return;
}
}
if (argcp > 3) {
end = strtohandle(argvp[3]);
if (end < 0) {
- rl_printf("Invalid end handle: %s\n", argvp[2]);
+ error("Invalid end handle: %s\n", argvp[2]);
return;
}
}
gpointer user_data)
{
if (status != 0) {
- rl_printf("Characteristic Write Request failed: "
+ error("Characteristic Write Request failed: "
"%s\n", att_ecode2str(status));
return;
}
if (!dec_write_resp(pdu, plen) && !dec_exec_write_resp(pdu, plen)) {
- rl_printf("Protocol error\n");
+ error("Protocol error\n");
return;
}
int handle;
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: disconnected\n");
+ failed("Disconnected\n");
return;
}
handle = strtohandle(argvp[1]);
if (handle <= 0) {
- rl_printf("A valid handle is required\n");
+ error("A valid handle is required\n");
return;
}
plen = gatt_attr_data_from_string(argvp[2], &value);
if (plen == 0) {
- rl_printf("Invalid value\n");
+ error("Invalid value\n");
return;
}
BT_IO_OPT_SEC_LEVEL, sec_level,
BT_IO_OPT_INVALID);
if (gerr) {
- rl_printf("Error: %s\n", gerr->message);
+ error("%s\n", gerr->message);
g_error_free(gerr);
}
}
uint16_t mtu;
if (status != 0) {
- rl_printf("Exchange MTU Request failed: %s\n",
- att_ecode2str(status));
+ error("Exchange MTU Request failed: %s\n",
+ att_ecode2str(status));
return;
}
if (!dec_mtu_resp(pdu, plen, &mtu)) {
- rl_printf("Protocol error\n");
+ error("Protocol error\n");
return;
}
if (g_attrib_set_mtu(attrib, mtu))
rl_printf("MTU was exchanged successfully: %d\n", mtu);
else
- rl_printf("Error exchanging MTU\n");
+ error("Error exchanging MTU\n");
}
static void cmd_mtu(int argcp, char **argvp)
{
if (conn_state != STATE_CONNECTED) {
- rl_printf("Command failed: not connected.\n");
+ failed("Disconnected\n");
return;
}
if (opt_psm) {
- rl_printf("Command failed: operation is only available for LE"
- " transport.\n");
+ failed("Operation is only available for LE transport.\n");
return;
}
}
if (opt_mtu) {
- rl_printf("Command failed: MTU exchange can only occur once per"
- " connection.\n");
+ failed("MTU exchange can only occur once per connection.\n");
return;
}
errno = 0;
opt_mtu = strtoll(argvp[1], NULL, 0);
if (errno != 0 || opt_mtu < ATT_DEFAULT_LE_MTU) {
- rl_printf("Invalid value. Minimum MTU size is %d\n",
+ error("Invalid value. Minimum MTU size is %d\n",
ATT_DEFAULT_LE_MTU);
return;
}
if (commands[i].cmd)
commands[i].func(argcp, argvp);
else
- rl_printf("%s: command not found\n", argvp[0]);
+ error("%s: command not found\n", argvp[0]);
g_strfreev(argvp);