#include <bluetooth/sdp_lib.h>
#include <glib.h>
+#include "glib-helper.h"
+#include "monitor/mainloop.h"
+#include "src/shared/util.h"
#include "src/shared/mgmt.h"
-#include "glib-helper.h"
#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) {
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
fprintf(stderr,
"PIN Code reply failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
fprintf(stderr,
"PIN Neg reply failed with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
fprintf(stderr,
"User Confirm reply failed. status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
fprintf(stderr,
"Confirm Neg reply failed. status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
bt_get_le16(&rp->revision));
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_version(struct mgmt *mgmt, uint16_t index, int argc,
}
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_commands(struct mgmt *mgmt, uint16_t index, int argc,
void *user_data)
{
const struct mgmt_rp_read_info *rp = param;
- int id = GPOINTER_TO_INT(user_data);
+ uint16_t index = PTR_TO_UINT(user_data);
char addr[18];
pending--;
if (status != 0) {
fprintf(stderr,
"Reading hci%u info failed with status 0x%02x (%s)\n",
- id, status, mgmt_errstr(status));
+ index, status, mgmt_errstr(status));
goto done;
}
ba2str(&rp->bdaddr, addr);
printf("hci%u:\taddr %s version %u manufacturer %u"
- " class 0x%02x%02x%02x\n",
- id, addr, rp->version, bt_get_le16(&rp->manufacturer),
+ " class 0x%02x%02x%02x\n", index,
+ addr, rp->version, bt_get_le16(&rp->manufacturer),
rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
printf("\tsupported settings: ");
return;
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void index_rsp(uint8_t status, uint16_t len, const void *param,
if (monitor)
printf("hci%u ", index);
- data = GINT_TO_POINTER((int) index);
+ data = UINT_TO_PTR(index);
if (mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
info_rsp, data, NULL) == 0) {
return;
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_info(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
return;
}
- data = GINT_TO_POINTER((int) index);
+ data = UINT_TO_PTR(index);
if (mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL, info_rsp,
data, NULL) == 0) {
struct command_data *data;
unsigned int send_id;
- data = g_new0(struct command_data, 1);
+ data = new0(struct command_data, 1);
+ if (!data)
+ return 0;
+
data->id = id;
data->op = op;
data->callback = cb;
- send_id = mgmt_send(mgmt, op, id, len, param, cmd_rsp, data, g_free);
+ send_id = mgmt_send(mgmt, op, id, len, param, cmd_rsp, data, free);
if (send_id == 0)
- g_free(data);
+ free(data);
return send_id;
}
printf("\n");
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_setting(struct mgmt *mgmt, uint16_t index, uint16_t op,
rp->class_of_dev[2], rp->class_of_dev[1], rp->class_of_dev[0]);
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_class(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
addr, status, mgmt_errstr(status));
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_disconnect(struct mgmt *mgmt, uint16_t index, int argc,
}
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_con(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
fprintf(stderr,
"Unable to start discovery. status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
return;
}
"with status 0x%02x (%s)\n",
status, mgmt_errstr(status));
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_name(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
printf("Paired with %s\n", addr);
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void pair_usage(void)
printf("Pairing Cancelled with %s\n", addr);
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cancel_pair_usage(void)
printf("%s unpaired\n", addr);
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_unpair(struct mgmt *mgmt, uint16_t index, int argc,
else
printf("Keys successfully loaded\n");
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_keys(struct mgmt *mgmt, uint16_t index, int argc, char **argv)
printf("%s %s succeeded\n", mgmt_opstr(op), addr);
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void block_usage(void)
printf("\n");
done:
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void cmd_local_oob(struct mgmt *mgmt, uint16_t index,
else
printf("Device ID successfully set\n");
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void did_usage(void)
else
printf("Static address successfully set\n");
- g_main_loop_quit(event_loop);
+ mainloop_quit();
}
static void static_addr_usage(void)
int opt, i;
uint16_t index = MGMT_INDEX_NONE;
struct mgmt *mgmt;
+ int exit_status;
while ((opt = getopt_long(argc, argv, "+hvi:",
main_options, NULL)) != -1) {
return 0;
}
- event_loop = g_main_loop_new(NULL, FALSE);
+ mainloop_init();
mgmt = mgmt_new_default();
if (!mgmt) {
fprintf(stderr, "Unable to open mgmt_socket\n");
- g_main_loop_unref(event_loop);
- return -1;
+ return EXIT_FAILURE;
}
for (i = 0; command[i].cmd; i++) {
if (command[i].cmd == NULL) {
fprintf(stderr, "Unknown command: %s\n", argv[0]);
mgmt_unref(mgmt);
- g_main_loop_unref(event_loop);
- return -1;
+ return EXIT_FAILURE;
}
mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error,
mgmt_register(mgmt, MGMT_EV_USER_CONFIRM_REQUEST, index, user_confirm,
mgmt, NULL);
- g_main_loop_run(event_loop);
+ exit_status = mainloop_run();
mgmt_cancel_all(mgmt);
mgmt_unregister_all(mgmt);
mgmt_unref(mgmt);
- g_main_loop_unref(event_loop);
-
- return 0;
+ return exit_status;
}