Example #1
0
void snepServerThread :: run()
{
    /** wait  */
    while(!flag);
    qDebug() << "snepClientThread running.";

    if(mac_link == NULL || llc_link == NULL || con == NULL) {
        qDebug() << "Device is not initialed.";
        return;
    }

    if (mac_link_activate_as_target(mac_link) < 0) {
        qDebug() << "Cannot activate MAC link";
        return;
    }

    void *err;
    mac_link_wait(mac_link, &err);
}
Example #2
0
int
main(int argc, char *argv[])
{
  int ret = 0;
  int ch;
  int testno;
  const int testcount = sizeof(tests) / sizeof(*tests);
  char junk;

  nfc_init(NULL);

  if (llcp_init() < 0)
    errx(EXIT_FAILURE, "llcp_init()");

  signal(SIGINT, stop_mac_link);

  while ((ch = getopt_long(argc, argv, "qd:f:l:D:m:Q:ht:T", longopts, NULL)) != -1) {
    switch (ch) {
      case 'q':
      case 'd':
      case 'f':
        warnx("ignored option -- %c (hint: edit log4crc)", ch);
        break;
      case 'l':
        if (1 != sscanf(optarg, "%d%c", &options.link_miu, &junk)) {
          errx(EXIT_FAILURE, "“%s” is not a valid link MIU value", optarg);
        }
        break;
      case 'D':
        options.device = optarg;
        break;
      case 'm':
        if (0 == strcasecmp("initiator", optarg))
          options.mode = M_INITIATOR;
        else if (0 == strcasecmp("target", optarg))
          options.mode = M_TARGET;
        else
          errx(EXIT_FAILURE, "“%s” is not a supported mode", optarg);
        break;
      case 'Q':
        if (0 == strcasecmp("android", optarg))
          options.quirks = Q_ANDROID;
        else
          errx(EXIT_FAILURE, "“%s” is not a support quirks mode", optarg);
        break;
      case 't':

        if (1 != sscanf(optarg, "%d%c", &testno, &junk)) {
          errx(EXIT_FAILURE, "“%s” is not a valid test number", optarg);
        }
        if ((testno < 1) || (testno > testcount)) {
          errx(EXIT_FAILURE, "Test number %d is not in the [1..%d] range", testno, (int) testcount);
        }
        tests[testno - 1].enabled = 1;
        break;
      case 'T':
        fprintf(stderr, "Available tests:\n");
        for (size_t i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
          fprintf(stderr, "%3d - %s\n", (int) i + 1, tests[i].description);
        }
        exit(EXIT_SUCCESS);
        break;
      case 'h':
      default:
        usage(basename(argv[0]));
        exit(EXIT_FAILURE);
        break;

    }
  }
  argc -= optind;
  argv += optind;

  nfc_connstring device_connstring[1];

  int res;
  res = nfc_list_devices(NULL, device_connstring, 1);

  if (res < 1)
    errx(EXIT_FAILURE, "No NFC device found");

  nfc_device *device;
  if (!(device = nfc_open(NULL, device_connstring[0]))) {
    errx(EXIT_FAILURE, "Cannot open NFC device");
  }

  struct llc_link *llc_link = llc_link_new();
  if (!llc_link) {
    errx(EXIT_FAILURE, "Cannot allocate LLC link data structures");
  }

  mac_link = mac_link_new(device, llc_link);
  if (!mac_link)
    errx(EXIT_FAILURE, "Cannot create MAC link");

  for (int i = 0; i < testcount; i++) {
    if (tests[i].enabled) {
      ret += tests[i].fn(llc_link);
    }
  }

  void *err;
  mac_link_wait(mac_link, &err);

  switch (llc_link->role & 0x01) {
    case LLC_INITIATOR:
      printf("I was the Initiator\n");
      break;
    case LLC_TARGET:
      printf("I was the Target\n");
      break;
  }

  mac_link_free(mac_link);
  llc_link_free(llc_link);

  nfc_close(device);

  llcp_fini();
  nfc_exit(NULL);
  exit(ret);
}
Example #3
0
int main (int argc, char *argv[])
{
	nfc_init(&context);
	  if (context == NULL) {
		printf("Unable to init libnfc (malloc)");
		exit(EXIT_FAILURE);
	  }


	if (llcp_init () < 0)
	errx (EXIT_FAILURE, "llcp_init()");

	llcp_log_log("[nfc-p2p-demo.c]", LLC_PRIORITY_WARN, "This is a test warning message");
	llcp_log_log("[nfc-p2p-demo.c]", LLC_PRIORITY_FATAL, "This is a test fatal message");
	llcp_log_log("[nfc-p2p-demo.c]", LLC_PRIORITY_TRACE, "This is a test trace message");
	llcp_log_log("[nfc-p2p-demo.c]", LLC_PRIORITY_DEBUG, "This is a test debug message");
	llcp_log_log("[nfc-p2p-demo.c]", LLC_PRIORITY_ERROR, "This is a test error message");


	int res;

	//Auto probe for nfc device
	/*
	nfc_connstring device_connstring[1];

	res = nfc_list_devices (NULL, device_connstring, 1);
	if (res < 1) errx (EXIT_FAILURE, "Sorry, no NFC device found");

	//Create nfc_device
	nfc_device *device;

	if (!(device = nfc_open (NULL, device_connstring[0]))) {
		errx (EXIT_FAILURE, "Cannot connect to NFC device");
	}
	*/


	//Create nfc_device
	nfc_device *device;

	//if (!(device = nfc_open (NULL, device_connstring[0]))) {
	//if (!(device = nfc_open (NULL, "pn532_uart:/dev/ttyUSB0:115200"))) {
	if (!(device = nfc_open (context, NULL))) {
			errx (EXIT_FAILURE, "Cannot connect to NFC device");
	}

	//Create llc_link
	struct llc_link *my_llc_link = llc_link_new ();

	struct llc_service *snep_service;


	//Create receiving service!
	if (!(snep_service = llc_service_new_with_uri (NULL, snep_service_thread, LLCP_SNEP_URI, NULL))) errx (EXIT_FAILURE, "Cannot create snep service");
	//Bind llc_service to llc_link
	if (llc_link_service_bind (my_llc_link, snep_service, LLCP_SNEP_SAP) < 0) {
		errx (EXIT_FAILURE, "llc_service_new_with_uri()");
	}

	//Create mac_link
	struct mac_link *my_mac_link = mac_link_new (device, my_llc_link);

	if (!my_mac_link) errx (EXIT_FAILURE, "Cannot establish MAC link");

	//Active mac_link as initiator
	res = mac_link_activate_as_initiator(my_mac_link);
	if (res <= 0) errx (EXIT_FAILURE, "Cannot activate link");


	//Wait for mac_link to finish
	void *status;
		mac_link_wait (my_mac_link, &status);

		printf ("STATUS = %p\n", status);

	mac_link_free (my_mac_link);
	llc_link_free (my_llc_link);

	nfc_close (device);

	llcp_fini ();
	nfc_exit(context);
	exit(EXIT_SUCCESS);

}