void _test_to_long_uri()
{
	YK_NDEF *ndef = ykp_alloc_ndef();
	char uri[] = "https://example.example.example.example.com/foo/kaka/bar/blahonga";
	int rc = ykp_construct_ndef_uri(ndef, uri);
	assert(rc == 0);
	assert(ndef->type == 0);
	assert(ndef->len == 0);
	ykp_free_ndef(ndef);
}
void _test_exact_uri()
{
	YK_NDEF *ndef = ykp_alloc_ndef();
	char uri[] = "https://www.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	int rc = ykp_construct_ndef_uri(ndef, uri);
	char text[256];
	assert(rc == 1);
	assert(ndef->type == 'U');
	assert(ndef->data[0] == 0x02);
	assert(strncmp(ndef->data + 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", NDEF_DATA_SIZE -1) == 0);
	assert(ndef->len == NDEF_DATA_SIZE);
	rc = ykp_ndef_as_text(ndef, text, 256);
	assert(rc == 1);
	assert(strncmp(uri, text, strlen(uri)) == 0);
	ykp_free_ndef(ndef);
}
void _test_https_uri()
{
	YK_NDEF *ndef = ykp_alloc_ndef();
	char uri[] = "https://example.com/foo";
	int rc = ykp_construct_ndef_uri(ndef, uri);
	char text[256];
	assert(rc == 1);
	assert(ndef->type == 'U');
	assert(ndef->data[0] == 0x04);
	assert(strncmp(ndef->data + 1, "example.com/foo", 15) == 0);
	assert(ndef->len == 16);
	rc = ykp_ndef_as_text(ndef, text, 256);
	assert(rc == 1);
	assert(strncmp(uri, text, strlen(uri)) == 0);
	ykp_free_ndef(ndef);
}
void _test_exact_text()
{
	YK_NDEF *ndef = ykp_alloc_ndef();
	char text[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	int rc = ykp_construct_ndef_text(ndef, text, "en", false);
	char text2[256];
	assert(rc == 1);
	assert(ndef->type == 'T');
	assert(ndef->data[0] == 2);
	assert(strncmp(ndef->data + 1, "en", 2) == 0);
	assert(strncmp(ndef->data + 3, text, NDEF_DATA_SIZE - 3) == 0);
	assert(ndef->len == NDEF_DATA_SIZE);
	rc = ykp_ndef_as_text(ndef, text2, 256);
	assert(rc == 1);
	assert(strncmp(text, text2, strlen(text)) == 0);
	ykp_free_ndef(ndef);
}
void _test_other_lang_text()
{
	YK_NDEF *ndef = ykp_alloc_ndef();
	char text[] = "aaaaaaaaaaaaaaa";
	size_t text_len = strlen(text);
	int rc = ykp_construct_ndef_text(ndef, text, "sv-SE", true);
	char text2[256];
	assert(rc == 1);
	assert(ndef->type == 'T');
	assert(ndef->data[0] == (0x80 & 5));
	assert(strncmp(ndef->data + 1, "sv-SE", 5) == 0);
	assert(strncmp(ndef->data + 6, text, text_len) == 0);
	assert(ndef->len == text_len + 6);
	rc = ykp_ndef_as_text(ndef, text2, 256);
	assert(rc == 1);
	assert(strncmp(text, text2, strlen(text)));
	ykp_free_ndef(ndef);
}
void YubiKeyWriter::writeNdef(bool uri, const QString language,
    const QString payload, const QString accCode, int slot) {
    YubiKeyFinder::getInstance()->stop();

    YK_KEY *yk = 0;
    YK_NDEF *ndef = ykp_alloc_ndef();

    bool error = false;

    qDebug() << "Writing ndef " << payload << " of type " << uri;

    try {

        if(accCode.length() > 0) {
            unsigned char accessCode[MAX_SIZE];
            size_t accessCodeLen = 0;
            int rc = encodeAccessCode(accCode, accessCode, &accessCodeLen);
            if (rc <= 0) {
                qDebug() << "Invalid access code: " << accCode;
                throw 0;
            }

            ykp_set_ndef_access_code(ndef, accessCode);
        }

        QByteArray payload_array = payload.toUtf8();
        const char *ndef_payload = payload_array.constData();
        qDebug() << "payload: " << ndef_payload;
        if (!(yk = yk_open_first_key())) {
            throw 0;
        }

        if(uri) {
            if(!ykp_construct_ndef_uri(ndef, ndef_payload)) {
                throw 0;
            }
        } else {
            QByteArray lang_array = language.toUtf8();
            const char *lang = lang_array.constData();
            if(!ykp_construct_ndef_text(ndef, ndef_payload, lang, false)) {
                throw 0;
            }
        }

        qDebug() << "writing the ndef.";
        if(! yk_write_ndef2(yk, ndef, slot)) {
            throw 0;
        }
        emit configWritten(true, NULL);
        emit diagnostics(QString("Wrote NDEF for slot %1").arg(slot));
    } catch(...) {
        error = true;
    }

    if(ndef && !ykp_free_ndef(ndef)) {
        error = true;
    }

    if (yk && !yk_close_key(yk)) {
        error = true;
    }

    YubiKeyFinder::getInstance()->start();


    if(error) {
        qDebug() << "Writing NDEF failed.";
        QString errMsg = reportError();
        emit configWritten(false, errMsg);
    }
}