#include "lib/mgmt.h"
#include "eir.h"
+static GMainLoop *event_loop = NULL;
+
static bool monitor = false;
static bool discovery = false;
static bool resolve_names = true;
}
if (ev->discovering == 0 && discovery)
- exit(EXIT_SUCCESS);
+ g_main_loop_quit(event_loop);
if (monitor)
printf("hci%u type %u discovering %s\n", index,
fprintf(stderr,
"hci%u PIN Code reply failed with status 0x%02x (%s)\n",
id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ g_main_loop_quit(event_loop);
+ return;
}
printf("hci%u PIN Reply successful\n", id);
fprintf(stderr,
"hci%u PIN Neg reply failed with status 0x%02x (%s)\n",
id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ g_main_loop_quit(event_loop);
+ return;
}
printf("hci%u PIN Negative Reply successful\n", id);
fprintf(stderr,
"hci%u User Confirm reply failed. status 0x%02x (%s)\n",
id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ g_main_loop_quit(event_loop);
+ return;
}
printf("hci%u User Confirm Reply successful\n", id);
fprintf(stderr,
"hci%u Confirm Neg reply failed. status 0x%02x (%s)\n",
id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ g_main_loop_quit(event_loop);
+ return;
}
printf("hci%u User Confirm Negative Reply successful\n", id);
if (status != 0) {
fprintf(stderr, "Reading mgmt version failed with status"
" 0x%02x (%s)\n", status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small version reply (%u bytes)\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
printf("MGMT Version %u, revision %u\n", rp->version,
bt_get_le16(&rp->revision));
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_version(int mgmt_sk, uint16_t index, int argc, char **argv)
if (status != 0) {
fprintf(stderr, "Reading supported commands failed with status"
" 0x%02x (%s)\n", status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small commands reply (%u bytes)\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
num_commands = bt_get_le16(&rp->num_commands);
if (len < expected_len) {
fprintf(stderr, "Too small commands reply (%u != %zu)\n",
len, expected_len);
- exit(EXIT_FAILURE);
+ goto done;
}
opcode = rp->opcodes;
printf("\t%s (0x%04x)\n", mgmt_evstr(ev), ev);
}
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_commands(int mgmt_sk, uint16_t index, int argc, char **argv)
fprintf(stderr,
"Reading hci%u info failed with status 0x%02x (%s)\n",
id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small info reply (%u bytes)\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->bdaddr, addr);
printf("\n\tname %s\n", rp->name);
printf("\tshort name %s\n", rp->short_name);
- if (pending == NULL)
- exit(EXIT_SUCCESS);
+ if (pending)
+ return;
+
+done:
+ g_main_loop_quit(event_loop);
}
static void index_rsp(int mgmt_sk, uint16_t op, uint16_t id, uint8_t status,
fprintf(stderr,
"Reading index list failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small index list reply (%u bytes)\n",
len);
- exit(EXIT_FAILURE);
+ goto done;
}
count = bt_get_le16(&rp->num_controllers);
fprintf(stderr,
"Index count (%u) doesn't match reply length (%u)\n",
count, len);
- exit(EXIT_FAILURE);
+ goto done;
}
if (monitor)
count, count > 1 ? "s" : "");
if (count == 0)
- exit(EXIT_SUCCESS);
+ goto done;
if (monitor && count > 0)
printf("\t");
if (mgmt_send_cmd(mgmt_sk, MGMT_OP_READ_INFO, index, NULL,
0, info_rsp, NULL) < 0) {
fprintf(stderr, "Unable to send read_info cmd\n");
- exit(EXIT_FAILURE);
+ goto done;
}
}
if (monitor && count > 0)
printf("\n");
+
+ return;
+
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_info(int mgmt_sk, uint16_t index, int argc, char **argv)
fprintf(stderr,
"%s for hci%u failed with status 0x%02x (%s)\n",
mgmt_opstr(op), id, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small %s response (%u bytes)\n",
mgmt_opstr(op), len);
- exit(EXIT_FAILURE);
+ goto done;
}
printf("hci%u %s complete, settings: ", id, mgmt_opstr(op));
print_settings(bt_get_le32(rp));
printf("\n");
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_setting(int mgmt_sk, uint16_t index, uint16_t op,
if (len == 0 && status != 0) {
fprintf(stderr, "%s failed, status 0x%02x (%s)\n",
mgmt_opstr(op), status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Unexpected %s len %u\n", mgmt_opstr(op), len);
- exit(EXIT_FAILURE);
+ goto done;
}
printf("%s succeeded. Class 0x%02x%02x%02x\n", mgmt_opstr(op),
rp->class_of_dev[2], rp->class_of_dev[1], rp->class_of_dev[0]);
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_class(int mgmt_sk, uint16_t index, int argc, char **argv)
if (len == 0 && status != 0) {
fprintf(stderr, "Disconnect failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Invalid disconnect response length (%u)\n",
len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->addr.bdaddr, addr);
- if (status == 0) {
+ if (status == 0)
printf("%s disconnected\n", addr);
- exit(EXIT_SUCCESS);
- } else {
+ else
fprintf(stderr,
"Disconnecting %s failed with status 0x%02x (%s)\n",
addr, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
- }
+
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_disconnect(int mgmt_sk, uint16_t index, int argc, char **argv)
if (len < sizeof(*rp)) {
fprintf(stderr, "Too small (%u bytes) get_connections rsp\n",
len);
- exit(EXIT_FAILURE);
+ goto done;
}
count = bt_get_le16(&rp->conn_count);
if (len != sizeof(*rp) + count * sizeof(struct mgmt_addr_info)) {
fprintf(stderr, "Invalid get_connections length "
" (count=%u, len=%u)\n", count, len);
- exit(EXIT_FAILURE);
+ goto done;
}
for (i = 0; i < count; i++) {
printf("%s type %s\n", addr, typestr(rp->addr[i].type));
}
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_con(int mgmt_sk, uint16_t index, int argc, char **argv)
fprintf(stderr,
"Unable to start discovery. status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ g_main_loop_quit(event_loop);
+ return;
}
printf("Discovery started\n");
static void name_rsp(int mgmt_sk, uint16_t op, uint16_t id, uint8_t status,
void *rsp, uint16_t len, void *user_data)
{
- if (status != 0) {
+ if (status != 0)
fprintf(stderr, "Unable to set local name "
"with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
+ g_main_loop_quit(event_loop);
}
static void cmd_name(int mgmt_sk, uint16_t index, int argc, char **argv)
if (len == 0 && status != 0) {
fprintf(stderr, "Pairing failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Unexpected pair_rsp len %u\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->addr.bdaddr, addr);
"Pairing with %s (%s) failed. status 0x%02x (%s)\n",
addr, typestr(rp->addr.type), status,
mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
printf("Paired with %s\n", addr);
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void pair_usage(void)
if (len == 0 && status != 0) {
fprintf(stderr, "Cancel Pairing failed with 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Unexpected cancel_pair_rsp len %u\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->bdaddr, addr);
"Cancel Pairing with %s (%s) failed. 0x%02x (%s)\n",
addr, typestr(rp->type), status,
mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
printf("Pairing Cancelled with %s\n", addr);
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cancel_pair_usage(void)
if (len == 0 && status != 0) {
fprintf(stderr, "Unpair device failed. status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Unexpected unpair_device_rsp len %u\n", len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->addr.bdaddr, addr);
fprintf(stderr,
"Unpairing %s failed. status 0x%02x (%s)\n",
addr, status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
printf("%s unpaired\n", addr);
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void cmd_unpair(int mgmt_sk, uint16_t index, int argc, char **argv)
static void keys_rsp(int mgmt_sk, uint16_t op, uint16_t id, uint8_t status,
void *rsp, uint16_t len, void *user_data)
{
- if (status != 0) {
+ if (status != 0)
fprintf(stderr, "Load keys failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
- }
-
- printf("Keys successfully loaded\n");
+ else
+ printf("Keys successfully loaded\n");
- exit(EXIT_SUCCESS);
+ g_main_loop_quit(event_loop);
}
static void cmd_keys(int mgmt_sk, uint16_t index, int argc, char **argv)
if (len == 0 && status != 0) {
fprintf(stderr, "%s failed, status 0x%02x (%s)\n",
mgmt_opstr(op), status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
if (len != sizeof(*rp)) {
fprintf(stderr, "Unexpected %s len %u\n", mgmt_opstr(op), len);
- exit(EXIT_FAILURE);
+ goto done;
}
ba2str(&rp->bdaddr, addr);
fprintf(stderr, "%s %s (%s) failed. status 0x%02x (%s)\n",
mgmt_opstr(op), addr, typestr(rp->type),
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
+ goto done;
}
printf("%s %s succeeded\n", mgmt_opstr(op), addr);
- exit(EXIT_SUCCESS);
+done:
+ g_main_loop_quit(event_loop);
}
static void block_usage(void)
static void did_rsp(int mgmt_sk, uint16_t op, uint16_t id, uint8_t status,
void *rsp, uint16_t len, void *user_data)
{
- if (status != 0) {
+ if (status != 0)
fprintf(stderr, "Set Device ID failed "
"with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- exit(EXIT_FAILURE);
- }
-
- printf("Device ID successfully set\n");
+ else
+ printf("Device ID successfully set\n");
- exit(EXIT_SUCCESS);
+ g_main_loop_quit(event_loop);
}
static void did_usage(void)
int opt, i, mgmt_sk;
uint16_t index = MGMT_INDEX_NONE;
struct mgmt *mgmt;
- GMainLoop *event_loop;
GIOChannel *mgmt_io;
guint io_id;