Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #207 from jcmurray/master
Browse files Browse the repository at this point in the history
Fix button enable/disable logic with HCE in 10.3
  • Loading branch information
John Murray committed Sep 29, 2014
2 parents 3870f1c + d787f5e commit 0a56258
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 26 deletions.
1 change: 1 addition & 0 deletions hostcardemulation/README.md
Expand Up @@ -17,6 +17,7 @@ the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0.html).

* **V1.0.0** - Initial release
* **V1.0.1** - Correct the AID registration code for 10.3
* **V1.0.2** - Fix logic around button enabling/disabling woth HCE in 10.3

**Known Issues**

Expand Down
6 changes: 3 additions & 3 deletions hostcardemulation/assets/main.qml
Expand Up @@ -146,7 +146,7 @@ Page {
Button {
id: registerAidButton
text: "HCE Register AID"
enabled: mainPage.nfcSupported && mainPage.hceSupported && !mainPage.aidRegistered
enabled: mainPage.nfcSupported && mainPage.hceSupported && !mainPage.aidRegistered && !mainPage.emulationStarted
visible: mainPage.featureSetSupported
horizontalAlignment: HorizontalAlignment.Center
layoutProperties: StackLayoutProperties {
Expand Down Expand Up @@ -177,7 +177,7 @@ Page {
Button {
id: startEmulationButton
text: "Start HCE"
enabled: mainPage.nfcSupported && !mainPage.emulationStarted && (mainPage.aidRegistered || !mainPage.hceSupported)
enabled: mainPage.nfcSupported && !mainPage.emulationStarted && (!mainPage.aidRegistered || !mainPage.hceSupported)
horizontalAlignment: HorizontalAlignment.Center
layoutProperties: StackLayoutProperties {
spaceQuota: 50
Expand All @@ -189,7 +189,7 @@ Page {
Button {
id: stopEmulationButton
text: "Stop HCE"
enabled: mainPage.nfcSupported && mainPage.emulationStarted && (mainPage.aidRegistered || !mainPage.hceSupported)
enabled: mainPage.nfcSupported && mainPage.emulationStarted && (!mainPage.aidRegistered || !mainPage.hceSupported)
horizontalAlignment: HorizontalAlignment.Center
layoutProperties: StackLayoutProperties {
spaceQuota: 50
Expand Down
2 changes: 1 addition & 1 deletion hostcardemulation/bar-descriptor.xml
Expand Up @@ -51,7 +51,7 @@
<!-- A string value of the format <0-999>.<0-999>.<0-999> that represents application version which can be used to check for application upgrade.
Values can also be 1-part or 2-part. It is not necessary to have a 3-part value.
An updated version of application must have a versionNumber value higher than the previous version. Required. -->
<versionNumber>1.0.1</versionNumber>
<versionNumber>1.0.2</versionNumber>

<!-- Fourth digit segment of the package version. First three segments are taken from the
<versionNumber> element. Must be an integer from 0 to 2^16-1 -->
Expand Down
2 changes: 2 additions & 0 deletions hostcardemulation/scripts/readBBY.py
Expand Up @@ -9,6 +9,7 @@
from smartcard.util import toASCIIString

COMMAND_HELLO_POS = [0xa0, 0x37, 0x00, 0x00, 0x01, 0x99, 0x00]
SELECT_F00012345210 = [0x00, 0xa4, 0x00, 0x00, 0x06, 0xF0, 0x00, 0x12, 0x34, 0x52, 0x10, 0x00]

cardtype = AnyCardType()

Expand All @@ -21,6 +22,7 @@
cardservice.connection.connect()

response, sw1, sw2 = cardservice.connection.transmit(COMMAND_HELLO_POS)
#response, sw1, sw2 = cardservice.connection.transmit(SELECT_F00012345210)
if sw1 == 0x90 and sw2 == 0x00:
print toASCIIString(response)
else:
Expand Down
1 change: 1 addition & 0 deletions hostcardemulation/scripts/sendApduToDevice.txt
Expand Up @@ -3,5 +3,6 @@ enable_trace
enable_timer
card_connect
send_apdu -APDU A0370000019900
#send_apdu -APDU 00A4000006F0001234521000
card_disconnect
release_context
88 changes: 66 additions & 22 deletions hostcardemulation/src/NfcListener.cpp
Expand Up @@ -108,6 +108,13 @@ void NfcListener::initialise()
_initialised = true;
}

#if BBNDK_VERSION_AT_LEAST(10,3,0)
if (isAidRegistered()) {
emit aidRegistered(_aid);
emit message("AID: " + _aid + " was already registered");
}
#endif

qDebug() << "XXXX NfcListener::initialise() ends..." << endl;
}

Expand Down Expand Up @@ -373,7 +380,7 @@ void NfcListener::onStartEmulating()
emit message("Emulating applet");
emit emulationStarted();
} else {
emit message(QString("Error starting emulation %1").arg(rc));
emit message(QString("Error starting emulation: rc=%1, %2").arg(rc).arg(strerror(errno)));
}
}

Expand All @@ -394,19 +401,20 @@ void NfcListener::onRegisterAid()
bool isRegistered = false;
int rc = 0;
QByteArray aidBytes;
if (_aid != "*") // wildcard?
{
// sanity check on AID: it must have even length
if (_aid.size() % 2 != 0) {
qDebug() << "XXXX NfcListener::onRegisterAid: nfc_hce_register_aid() : Rejected AID with odd length" << endl;
return;
}
aidBytes = QByteArray::fromHex(_aid.toAscii()); // convert the hexstring to bytes
}

if (_aid != "*") // wildcard?
{
// sanity check on AID: it must have even length
if (_aid.size() % 2 != 0) {
qDebug() << "XXXX NfcListener::onRegisterAid: Rejected AID with odd length" << endl;
return;
}
aidBytes = QByteArray::fromHex(_aid.toAscii()); // convert the hexstring to bytes
}

const uint8_t* aidPtr = reinterpret_cast<const uint8_t*>(aidBytes.constData());

if (_featureSet == 2) {
if (_featureSet == 2) {

rc = nfc_hce_is_aid_registered(aidPtr, aidBytes.size(), &isRegistered);

Expand Down Expand Up @@ -459,15 +467,15 @@ void NfcListener::onUnregisterAid()
bool isRegistered = false;
int rc = 0;
QByteArray aidBytes;
if (_aid != "*") // wildcard?
{
// sanity check on AID: it must have even length
if (_aid.size() % 2 != 0) {
qDebug() << "XXXX NfcListener::onRegisterAid: nfc_hce_register_aid() : Rejected AID with odd length" << endl;
return;
}
aidBytes = QByteArray::fromHex(_aid.toAscii()); // convert the hexstring to bytes
}
if (_aid != "*") // wildcard?
{
// sanity check on AID: it must have even length
if (_aid.size() % 2 != 0) {
qDebug() << "XXXX NfcListener::onRegisterAid: Rejected AID with odd length" << endl;
return;
}
aidBytes = QByteArray::fromHex(_aid.toAscii()); // convert the hexstring to bytes
}
const uint8_t* aidPtr = reinterpret_cast<const uint8_t*>(aidBytes.constData());
if (_featureSet == 2) {

Expand Down Expand Up @@ -512,6 +520,44 @@ void NfcListener::onUnregisterAid()
return;
}

bool NfcListener::isAidRegistered()
{
bool isRegistered = false;

#if BBNDK_VERSION_AT_LEAST(10,3,0)
qDebug() << "XXXX NfcListener::isAidRegistered: " << endl;

int rc = 0;
QByteArray aidBytes;

if (_aid != "*") // wildcard?
{
// sanity check on AID: it must have even length
if (_aid.size() % 2 != 0) {
qDebug() << "XXXX NfcListener::isAidRegistered: Rejected AID with odd length" << endl;
return isRegistered;
}
aidBytes = QByteArray::fromHex(_aid.toAscii()); // convert the hexstring to bytes
}

const uint8_t* aidPtr = reinterpret_cast<const uint8_t*>(aidBytes.constData());

if (_featureSet == 2) {
rc = nfc_hce_is_aid_registered(aidPtr, aidBytes.size(), &isRegistered);
if ((rc == NFC_RESULT_SUCCESS) && isRegistered) {
qDebug() << "XXXX NfcListener::isAidRegistered: nfc_hce_is_aid_registered() : REGISTERED" << endl;
isRegistered = true;
} else {
qDebug() << "XXXX NfcListener::isAidRegistered: AID wasn't registered:" << strerror(errno) << endl;
}
} else {
qDebug() << "XXXX NfcListener::isAidRegistered: Feature set doesn't support HCE AID registration"<< endl;
}
#endif

return isRegistered;
}

bool NfcListener::initialised()
{
return !_initialised;
Expand All @@ -524,8 +570,6 @@ void NfcListener::onInvoked(const bb::system::InvokeRequest &request)
#if BBNDK_VERSION_AT_LEAST(10,3,0)
if (request.action().compare(HCE_INVOKE_AID_SELECTED) == 0) {
qDebug() << "XXXX AID has been selected" << endl;
onStartEmulating();

} else {
qWarning() << "XXXX received invocation request we don't handle:" << request.action() << endl;
}
Expand Down
2 changes: 2 additions & 0 deletions hostcardemulation/src/NfcListener.hpp
Expand Up @@ -89,6 +89,8 @@ private slots:
void terminate();
void event(bps_event_t *event);
void processIso144434CommandEvent(nfc_target_t *target);
bool isAidRegistered();

QString _aid;
bb::system::InvokeManager *_invokeManager;
int _featureSet;
Expand Down

0 comments on commit 0a56258

Please sign in to comment.