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); } }
int yk_write_ndef(YK_KEY *yk, YK_NDEF *ndef) { /* just wrap yk_write_ndef2() with confnum 1 */ return yk_write_ndef2(yk, ndef, 1); }