-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from fidesmo/feature-send-apdu
Add basic console implementation using jline
- Loading branch information
Showing
7 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.fidesmo.gradle.plugin; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.regex.Pattern; | ||
|
||
import jline.Terminal; | ||
import jline.TerminalFactory; | ||
import jline.console.ConsoleReader; | ||
import jline.console.UserInterruptException; | ||
import com.fidesmo.sec.utils.Hex; | ||
import nordpol.IsoCard; | ||
|
||
/** Simple command line to communicate with card. | ||
* | ||
*/ | ||
public class Console { | ||
|
||
public static void run() throws IOException { | ||
Terminal terminal = TerminalFactory.create(); | ||
terminal.setEchoEnabled(true); | ||
ConsoleReader reader = new ConsoleReader("Fidesmo console", | ||
System.in, | ||
System.out, | ||
terminal); | ||
reader.setHandleUserInterrupt(true); | ||
PrintWriter out = new PrintWriter(reader.getOutput()); | ||
IsoCard card = (new SmartcardioTransceiver(out)) | ||
.getCard() | ||
.toBlocking() | ||
.first(); | ||
|
||
String line; | ||
try { | ||
while((line = reader.readLine("> ")) != null) { | ||
String[] tokens = line.split(" "); | ||
if (tokens[0].equals("send")) { | ||
if (tokens.length < 2) { | ||
out.println("To few arguments for send command"); | ||
} | ||
|
||
byte[] apdu = Hex.decodeHex(tokens[1].toUpperCase()); | ||
card.transceive(apdu); // output displayed through log | ||
} else if (line.equals("exit")) { | ||
card.close(); | ||
break; | ||
} else if (line.equals("help")) { | ||
out.println("send <hex string> -- send apdu to card"); | ||
out.println("help -- display this message"); | ||
out.println("exit -- exit this shell"); | ||
} else { | ||
out.println("Unknown command. Type 'exit' to leave."); | ||
} | ||
} | ||
} catch(UserInterruptException uie) { | ||
try { | ||
card.close(); | ||
} catch(IOException ioe) { | ||
/* Ignore */ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<appender name="STDOUT-COMPACT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<logger name="com.fidesmo.gradle.plugin.LoggingCard" level="INFO" additivity="false"> | ||
<appender-ref ref="STDOUT-COMPACT" /> | ||
</logger> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |