Skip to content

Commit

Permalink
Add empty select after checking card info (#98)
Browse files Browse the repository at this point in the history
* add empty select before starting delivery

* rearrange terms

* add comment

* Add select in ws deliveries too
  • Loading branch information
diegomallada1 authored Aug 23, 2024
1 parent e2bdd1a commit 6f490dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
9 changes: 9 additions & 0 deletions library/src/main/java/com/fidesmo/fdsm/FidesmoCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static Optional<FidesmoCard> detectOnline(BIBO channel, FidesmoApiClient
static final HexBytes getDataCIN = HexBytes.b(new CommandAPDU(0x80, 0xCA, 0x00, 0x45, 0x00).getBytes());
static final HexBytes selectFidesmoPlatform = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, FIDESMO_PLATFORM_AID.getBytes()).getBytes());
static final HexBytes selectFidesmoBatch = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, FIDESMO_BATCH_AID.getBytes()).getBytes());
static final HexBytes selectEmpty = HexBytes.b(new CommandAPDU(0x00, 0xA4, 0x00, 0x00, 0x00).getBytes());

public static Map<HexBytes, byte[]> probe(BIBO channel) {
// preserve order, just for fun
Expand Down Expand Up @@ -331,6 +332,14 @@ public static boolean deliverRecipes(BIBO bibo, FidesmoCard card, AuthenticatedF
return true;
}

public byte[] selectEmpty(APDUBIBO channel) {
CommandAPDU select = new CommandAPDU(selectEmpty.value());
ResponseAPDU response;

response = channel.transmit(select);
return response.getData();
}

public byte[] getCIN() {
return cin.clone();
}
Expand Down
20 changes: 12 additions & 8 deletions library/src/main/java/com/fidesmo/fdsm/ServiceDeliverySession.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.fidesmo.fdsm;

import apdu4j.core.APDUBIBO;
import apdu4j.core.BIBO;
import apdu4j.core.BIBOException;
import apdu4j.core.HexUtils;
Expand Down Expand Up @@ -103,6 +104,9 @@ public DeliveryResult call() throws FDSMException {
}

public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws IOException, UnsupportedCallbackException {
APDUBIBO apduBibo = new APDUBIBO(bibo);
//Reset after checking card info to avoid leaving FPA selected, which prevents some services to be run.
card.selectEmpty(apduBibo);
// Address #4
JsonNode deviceInfo = client.rpc(client.getURI(FidesmoApiClient.DEVICES_URL, HexUtils.bin2hex(card.getCIN()), card.getBatchId()));
byte[] iin = HexUtils.decodeHexString_imp(deviceInfo.get("iin").asText());
Expand Down Expand Up @@ -136,17 +140,17 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws
}

// Construct Delivery Request
ObjectNode deliveryrequest = JsonNodeFactory.instance.objectNode();
ObjectNode deliveryRequest = JsonNodeFactory.instance.objectNode();

deliveryrequest.put("appId", appId);
deliveryrequest.put("serviceId", serviceId);
deliveryRequest.put("appId", appId);
deliveryRequest.put("serviceId", serviceId);

// cardId
ObjectNode cardId = JsonNodeFactory.instance.objectNode();
cardId.put("iin", HexUtils.bin2hex(iin));
cardId.put("cin", HexUtils.bin2hex(card.getCIN()));
cardId.put("platformVersion", platformVersion);
deliveryrequest.set("cardId", cardId);
deliveryRequest.set("cardId", cardId);

// User input fields
ArrayList<Field> fields = new ArrayList<>(fieldsFromNode(description.get("fieldsRequired")));
Expand All @@ -158,13 +162,13 @@ public DeliveryResult deliver(BIBO bibo, String appId, String serviceId) throws

Map<String, Field> userInput = formHandler.processForm(fields);
if (description.has("emailRequired"))
deliveryrequest.put("email", userInput.remove("email").getValue());
deliveryRequest.put("email", userInput.remove("email").getValue());
if (description.has("msisdnRequired"))
deliveryrequest.put("msisdn", userInput.remove("msisdn").getValue());
deliveryRequest.put("msisdn", userInput.remove("msisdn").getValue());

deliveryrequest.set("fields", mapToJsonNode(userInput));
deliveryRequest.set("fields", mapToJsonNode(userInput));

JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryrequest);
JsonNode delivery = client.rpc(client.getURI(FidesmoApiClient.SERVICE_DELIVER_URL), deliveryRequest);
String sessionId = delivery.get("sessionId").asText();

logger.info("Delivering: {}", FidesmoApiClient.lamei18n(description.get("title")));
Expand Down
2 changes: 2 additions & 0 deletions tool/src/main/java/com/fidesmo/fdsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ public static void main(String[] argv) {
DeliveryUrl delivery = DeliveryUrl.parse(args.valueOf(OPT_RUN));

if (delivery.isWebSocket()) {
FidesmoCard fidesmoCard = fidesmoMetadata.get();
fidesmoCard.selectEmpty(bibo);
boolean success = WsClient.execute(new URI(delivery.getService()), bibo, auth).join().isSuccess();
if (!success) {
fail("Fail to run a script");
Expand Down

0 comments on commit 6f490dc

Please sign in to comment.