void * target_thread(void *arg) { intptr_t res = 0; struct test_thread_data *thread_data = (struct test_thread_data *) arg; /* uint8_t id[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 };*/ cut_set_current_test_context(thread_data->context); printf("Activating target...\n"); struct llc_link *llc_link = llc_link_new(); cut_assert_not_null(llc_link, cut_message("llc_link_new() failed")); struct mac_link *link = mac_link_new(thread_data->device, llc_link); cut_assert_not_null(link, cut_message("mac_link_new() failed")); res = mac_link_activate_as_target(link); cut_assert_equal_int(1, res, cut_message("mac_link_activate_as_target() failed")); sleep(3); printf("===== DEACTIVATE =====\n"); mac_link_deactivate(link, MAC_DEACTIVATE_ON_REQUEST); mac_link_free(link); llc_link_free(llc_link); return (void *) res; }
void test_llc_link_encode_parameters(void) { struct llc_link *link; link = llc_link_new(); cut_assert_not_null(link, cut_message("llc_link_new()")); uint8_t buffer[1024]; int res = llc_link_encode_parameters(link, buffer, sizeof(buffer)); cut_assert_not_equal_int(-1, res, cut_message("llc_link_encode_parameters()")); res = llc_link_configure(link, buffer, res); cut_assert_equal_int(0, res, cut_message("llc_link_configure()")); }
int snepClientThread :: init(nfc_device **device) { pnd = *device; if (llcp_init() < 0){ qDebug() << "llcp_init() failed."; return -1; } llc_link = llc_link_new(); if (!llc_link) { qDebug() << "Cannot allocate LLC link data structures"; } mac_link = mac_link_new(pnd, llc_link); if (!mac_link){ qDebug() << "Cannot create MAC link"; return -1; } struct llc_service *com_android_npp; if (!(com_android_npp = llc_service_new(NULL, snepService, NULL))){ qDebug() << "Cannot create com.android.npp service"; return -1; } llc_service_set_miu(com_android_npp, 512); llc_service_set_rw(com_android_npp, 2); int sap; if ((sap = llc_link_service_bind(llc_link, com_android_npp, 0x20)) < 0){ qDebug() << "Cannot create com.android.npp service"; return -1; } con = llc_outgoing_data_link_connection_new(llc_link, sap, LLCP_SNEP_SAP); if (!con){ qDebug() << "Connection created failed."; return -1; } qDebug() << "snep: flag set true"; flag = true; return 0; }
void test_llc_link_activate_as_target(void) { struct llc_link *link; link = llc_link_new(); cut_assert_not_null(link, cut_message("llc_link_new()")); int res = llc_link_activate(link, LLC_TARGET, NULL, 0); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); cut_assert_equal_int(LLCP_VERSION_MAJOR, link->version.major, cut_message("Wrong major version")); cut_assert_equal_int(LLCP_VERSION_MINOR, link->version.minor, cut_message("Wrong minor version")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->local_miu, cut_message("Wrong local MIU")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->remote_miu, cut_message("Wrong remote MIU")); cut_assert_equal_int(0x0001, link->remote_wks, cut_message("Wrong remote WKS")); cut_assert_equal_int(0, link->remote_lto.tv_sec, cut_message("Wrong remote LTO sec")); cut_assert_equal_int(100000, link->remote_lto.tv_usec, cut_message("Wrong remote LTO nsec")); cut_assert_equal_int(3, link->remote_lsc, cut_message("Wrong remote LSC")); llc_link_deactivate(link); llc_link_free(link); }
int snepServerThread :: init(nfc_device **device) { pnd = *device; if (llcp_init() < 0) { qDebug() << "llcp_init() failed."; return -1; } llc_link = llc_link_new(); if (!llc_link) { qDebug() << "Cannot allocate LLC link data structures"; return -1; } /** initial LLCP service struct */ struct llc_service *com_android_snep; if (!(com_android_snep = llc_service_new_with_uri(NULL, snepService, (char *)"urn:nfc:sn:snep", NULL))) { qDebug() << "Cannot create com.android.snep service"; } llc_service_set_miu(com_android_snep, 512); llc_service_set_rw(com_android_snep, 2); if (llc_link_service_bind(llc_link, com_android_snep, LLCP_SNEP_SAP) < 0) { qDebug() << "Cannot bind service"; } mac_link = mac_link_new(pnd, llc_link); if (!mac_link) { qDebug() << "Cannot create MAC link"; return -1; } qDebug() << "snep: flag set true"; flag = true; return 0; }
void test_llc_link_find_sap_by_uri(void) { struct llc_link *link; int res; link = llc_link_new(); cut_assert_not_null(link, cut_message("llc_link_new()")); struct llc_service *service = llc_service_new_with_uri(NULL, void_service, "urn:nfc:xsn:foo", NULL); res = llc_link_service_bind(link, service, -1); cut_assert_not_equal_int(-1, res, cut_message("llc_link_service_bind()")); uint8_t sap = llc_link_find_sap_by_uri(link, "urn:nfc:xsn:foo"); cut_assert_not_equal_int(0, sap, cut_message("llc_link_find_sap_by_uri()")); cut_assert_equal_int(res, sap, cut_message("Wrong SAP")); llc_link_service_unbind(link, service->sap); sap = llc_link_find_sap_by_uri(link, "urn:nfc:xsn:foo"); cut_assert_equal_int(0, sap, cut_message("llc_link_find_sap_by_uri()")); llc_service_free(service); llc_link_free(link); }
void * initiator_thread(void *arg) { intptr_t res = 0; struct test_thread_data *thread_data = (struct test_thread_data *) arg; cut_set_current_test_context(thread_data->context); struct llc_link *llc_link = llc_link_new(); cut_assert_not_null(llc_link, cut_message("llc_link_new() failed")); struct mac_link *link = mac_link_new(thread_data->device, llc_link); cut_assert_not_null(link, cut_message("mac_link_new() failed")); printf("Activating initiator...\n"); res = mac_link_activate_as_initiator(link); cut_assert_equal_int(1, res, cut_message("mac_link_activate_as_initiator() failed")); printf("===== DEACTIVATE =====\n"); mac_link_deactivate(link, MAC_DEACTIVATE_ON_REQUEST); mac_link_free(link); llc_link_free(llc_link); return (void *) res; }
int main(int argc, char *argv[]) { (void)argc; (void)argv; nfc_context *context; nfc_init(&context); if (llcp_init() < 0) errx(EXIT_FAILURE, "llcp_init()"); signal(SIGINT, stop_mac_link); atexit(bye); if (!(device = nfc_open(context, NULL))) { errx(EXIT_FAILURE, "Cannot connect to 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"); } struct llc_service *com_android_npp; if (!(com_android_npp = llc_service_new(NULL, com_android_snep_service, NULL))){ errx(EXIT_FAILURE, "Cannot create com.android.npp service"); } llc_service_set_miu(com_android_npp, 512); llc_service_set_rw(com_android_npp, 2); int sap; if ((sap = llc_link_service_bind(llc_link, com_android_npp, 0x20)) < 0) errx(EXIT_FAILURE, "Cannot bind service"); // struct llc_connection *con = llc_outgoing_data_link_connection_new_by_uri(llc_link, sap, "urn:nfc:sn:snep"); struct llc_connection *con = llc_outgoing_data_link_connection_new(llc_link, sap, LLCP_SNEP_SAP); if (!con){ errx(EXIT_FAILURE, "Cannot create llc_connection"); } if (mac_link_activate_as_initiator(mac_link) < 0) { errx(EXIT_FAILURE, "Cannot activate MAC link"); } if (llc_connection_connect(con) < 0) errx(EXIT_FAILURE, "Cannot connect llc_connection"); llc_connection_wait(con, NULL); llc_link_deactivate(llc_link); mac_link_free(mac_link); llc_link_free(llc_link); nfc_close(device); device = NULL; llcp_fini(); nfc_exit(context); exit(EXIT_SUCCESS); }
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); }
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); }
void dummy_mac_transport(struct llc_link *initiator, struct llc_link *target) { int n; char buffer[1024]; for (;;) { struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000, }; n = mq_timedreceive(initiator->llc_down, buffer, sizeof(buffer), NULL, &ts); if (n < 0) { if (errno == ETIMEDOUT) { n = 2; buffer[0] = buffer[1] = 0x00; } else break; } pthread_testcancel(); n = mq_send(target->llc_up, buffer, n, 0); if (n < 0) break; pthread_testcancel(); n = mq_timedreceive(target->llc_down, buffer, sizeof(buffer), NULL, &ts); if (n < 0) { if (errno == ETIMEDOUT) { n = 2; buffer[0] = buffer[1] = 0x00; } else break; } pthread_testcancel(); n = mq_send(initiator->llc_up, buffer, n, 0); if (n < 0) break; pthread_testcancel(); } } void * dummy_mac_transport_thread(void *arg) { struct dummy_mac_transport_endpoints *eps = (struct dummy_mac_transport_endpoints *)arg; dummy_mac_transport(eps->initiator, eps->target); return NULL; } void test_dummy_mac_link(void) { int res; struct llc_link *initiator, *target; struct llc_service *service; struct mac_link mac_initiator, mac_target; initiator = llc_link_new(); cut_assert_not_null(initiator, cut_message("llc_link_new()")); target = llc_link_new(); cut_assert_not_null(target, cut_message("llc_link_new()")); initiator->cut_test_context = cut_get_current_test_context(); service = llc_service_new(NULL, echo_service, NULL); cut_assert_not_null(service, cut_message("llc_service_new()")); res = llc_link_service_bind(initiator, service, ECHO_SAP); cut_assert_equal_int(ECHO_SAP, res, cut_message("llc_link_service_bind()")); res = llc_link_activate(initiator, LLC_INITIATOR | LLC_PAX_PDU_PROHIBITED, NULL, 0); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); res = llc_link_activate(target, LLC_TARGET | LLC_PAX_PDU_PROHIBITED, NULL, 0); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); char buffer[1024]; pthread_t transport; struct dummy_mac_transport_endpoints eps = { .initiator = initiator, .target = target, }; pthread_create(&transport, NULL, dummy_mac_transport_thread, &eps); mac_initiator.exchange_pdus_thread = &transport; mac_target.exchange_pdus_thread = &transport; //initiator->mac_link = &mac_initiator; //target->mac_link = &mac_target; buffer[0] = ECHO_SAP << 2; buffer[1] = '\xC0'; buffer[2] = 'H'; buffer[3] = 'e'; buffer[4] = 'l'; buffer[5] = 'l'; buffer[6] = 'o'; res = mq_send(initiator->llc_up, buffer, 7, 0); cut_assert_equal_int(0, res, cut_message("mq_send")); struct timespec ts = { .tv_sec = time(NULL) + 2, .tv_nsec = 0, }; int old_cancelstate; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); res = sem_timedwait(sem_cutter, &ts); cut_assert_equal_int(0, res, cut_message("Message not received")); pthread_setcancelstate(old_cancelstate, NULL); pthread_cancel(transport); //pthread_kill (transport, SIGUSR1); pthread_join(transport, NULL); llc_link_deactivate(initiator); llc_link_deactivate(target); llc_link_free(initiator); llc_link_free(target); }
void test_llc_link_activate_as_initiator(void) { struct llc_link *link; link = llc_link_new(); cut_assert_not_null(link, cut_message("llc_link_new()")); int res = llc_link_activate(link, LLC_INITIATOR, NULL, 0); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); cut_assert_equal_int(LLCP_VERSION_MAJOR, link->version.major, cut_message("Wrong major version")); cut_assert_equal_int(LLCP_VERSION_MINOR, link->version.minor, cut_message("Wrong minor version")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->local_miu, cut_message("Wrong local MIU")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->remote_miu, cut_message("Wrong remote MIU")); cut_assert_equal_int(0x0001, link->remote_wks, cut_message("Wrong remote WKS")); cut_assert_equal_int(0, link->remote_lto.tv_sec, cut_message("Wrong remote LTO sec")); cut_assert_equal_int(100000, link->remote_lto.tv_usec, cut_message("Wrong remote LTO nsec")); cut_assert_equal_int(3, link->remote_lsc, cut_message("Wrong remote LSC")); llc_link_deactivate(link); uint8_t parameters[] = { 0x01, 0x01, 0x23 }; res = llc_link_activate(link, LLC_INITIATOR, parameters, sizeof(parameters)); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); cut_assert_equal_int(LLCP_VERSION_MAJOR, link->version.major, cut_message("Wrong major version")); cut_assert_equal_int(LLCP_VERSION_MINOR, link->version.minor, cut_message("Wrong minor version")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->local_miu, cut_message("Wrong local MIU")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->remote_miu, cut_message("Wrong remote MIU")); cut_assert_equal_int(0x0001, link->remote_wks, cut_message("Wrong remote WKS")); cut_assert_equal_int(0, link->remote_lto.tv_sec, cut_message("Wrong remote LTO sec")); cut_assert_equal_int(100000, link->remote_lto.tv_usec, cut_message("Wrong remote LTO nsec")); cut_assert_equal_int(3, link->remote_lsc, cut_message("Wrong remote LSC")); llc_link_deactivate(link); uint8_t parameters2[] = { 0x02, 0x02, 0x01, 0x23 }; res = llc_link_activate(link, LLC_INITIATOR, parameters2, sizeof(parameters2)); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); cut_assert_equal_int(LLCP_VERSION_MAJOR, link->version.major, cut_message("Wrong major version")); cut_assert_equal_int(LLCP_VERSION_MINOR, link->version.minor, cut_message("Wrong minor version")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->local_miu, cut_message("Wrong local MIU")); cut_assert_equal_int(419, link->remote_miu, cut_message("Wrong remote MIU")); cut_assert_equal_int(0x0001, link->remote_wks, cut_message("Wrong remote WKS")); cut_assert_equal_int(0, link->remote_lto.tv_sec, cut_message("Wrong remote LTO sec")); cut_assert_equal_int(100000, link->remote_lto.tv_usec, cut_message("Wrong remote LTO nsec")); cut_assert_equal_int(3, link->remote_lsc, cut_message("Wrong remote LSC")); llc_link_deactivate(link); uint8_t parameters3[] = { 0x01, 0x01, 0x10, 0x42, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x01, 0x23, 0x03, 0x02, 0x12, 0x34, 0x04, 0x01, 0x64, 0x07, 0x01, 0x02 }; res = llc_link_activate(link, LLC_INITIATOR, parameters3, sizeof(parameters3)); cut_assert_equal_int(0, res, cut_message("llc_link_activate()")); cut_assert_equal_int(1, link->version.major, cut_message("Wrong major version")); cut_assert_equal_int(0, link->version.minor, cut_message("Wrong minor version")); cut_assert_equal_int(LLCP_DEFAULT_MIU, link->local_miu, cut_message("Wrong local MIU")); cut_assert_equal_int(419, link->remote_miu, cut_message("Wrong remote MIU")); cut_assert_equal_int(0x1235, link->remote_wks, cut_message("Wrong remote WKS")); cut_assert_equal_int(1, link->remote_lto.tv_sec, cut_message("Wrong remote LTO sec")); cut_assert_equal_int(0, link->remote_lto.tv_usec, cut_message("Wrong remote LTO nsec")); cut_assert_equal_int(2, link->remote_lsc, cut_message("Wrong remote LSC")); llc_link_deactivate(link); uint8_t parameters4[] = { 0x01, 0x01, 0x09 }; res = llc_link_activate(link, LLC_INITIATOR, parameters4, sizeof(parameters4)); cut_assert_equal_int(-1, res, cut_message("llc_link_activate()")); llc_link_free(link); }