static GMainLoop *main_loop = NULL;
+static int nfctool_poll_cb(guint8 cmd, guint32 idx, gpointer data);
+
+static void nfctool_quit(gboolean force);
+
static gchar *nfctool_poll_mode_str(int mode)
{
if (mode == POLLING_MODE_TARGET)
return -ENODEV;
}
+ nl_add_event_handler(NFC_EVENT_TARGETS_FOUND, nfctool_poll_cb);
+ nl_add_event_handler(NFC_EVENT_TM_ACTIVATED, nfctool_poll_cb);
+
err = nl_start_poll(adapter, opts.poll_mode);
if (err == 0) {
return err;
}
-static int nfctool_tm_activated(void)
-{
- printf("Target mode activated\n");
-
- if (!opts.sniff)
- g_main_loop_quit(main_loop);
-
- return 0;
-}
-
static void nfctool_send_dep_link_up(guint32 target_idx, guint32 adapter_idx)
{
nl_send_dep_link_up(adapter_idx, target_idx);
}
exit:
- if (!opts.sniff)
- g_main_loop_quit(main_loop);
-
return err;
}
-static int nfc_event_cb(guint8 cmd, guint32 idx)
+static int nfctool_poll_cb(guint8 cmd, guint32 idx, gpointer data)
{
int err = 0;
+ DBG("cmd: %d, idx: %d", cmd, idx);
+
switch (cmd) {
case NFC_EVENT_TARGETS_FOUND:
- DBG("Targets found");
err = nfctool_targets_found(idx);
break;
case NFC_EVENT_TM_ACTIVATED:
- DBG("Target mode activated");
- err = nfctool_tm_activated();
+ printf("Target mode activated\n");
break;
}
+ nfctool_quit(FALSE);
+
return err;
}
DBG("Terminating");
- g_main_loop_quit(main_loop);
+ nfctool_quit(TRUE);
}
struct nfctool_options opts = {
g_main_loop_unref(main_loop);
}
+static void nfctool_quit(gboolean force)
+{
+ if (force || !opts.sniff)
+ g_main_loop_quit(main_loop);
+}
+
int main(int argc, char **argv)
{
int err;
adapter_init();
if (opts.need_netlink) {
- err = nl_init(nfc_event_cb);
+ err = nl_init();
if (err)
goto exit_err;