/** * 本来は複数ブロックやサービスに同時に書き込めますが、この実装は1ブロックだけです。 JIS_X_6319_4 を見ると複数ブロックに書き込む方法がわかります。 * * @param serviceCode 書込むサービスコード * @param addr 何ブロック目に書き込むか。 0~N * @param buff 書きこむブロックデータ. 16バイトである必要があります。 * @return 0: 正常終了, -1: 異常終了 * @throws NfcException */ public int writeWithoutEncryption(ServiceCode serviceCode, int addr, byte[] buff) throws NfcException { if (buff == null || buff.length != 16) { return -1; } byte[] bytes = serviceCode.getBytes(); ByteBuffer b = ByteBuffer.allocate(6 + buff.length); b.put( new byte[] { (byte) 0x01 // Number of Service , (byte) bytes[0] // サービスコード (little endian) , (byte) bytes[1], (byte) 1 // 同時書き込みブロック数 , (byte) 0x80, (byte) addr // ブロックリスト }); b.put(buff); // 書き出すデータ CommandPacket writeWoEncrypt = new CommandPacket(COMMAND_WRITE_WO_ENCRYPTION, idm, b.array()); CommandResponse r = execute(writeWoEncrypt); byte[] retBytes = r.getBytes(); if (retBytes != null && retBytes.length > 10 && retBytes[10] == (byte) 0) { return 0; // normal } else { return -1; // error } }
/** * COMMAND_SEARCH_SERVICECODE を実行します。 参考: * http://wiki.osdev.info/index.php?PaSoRi%2FRC-S320#content_1_25 * * @param idm 問い合わせるシステム領域のIDm * @param index ?番目か * @return Response部分 * @throws NfcException */ public byte[] doSearchServiceCode(IDm idm, int index) throws NfcException { CommandPacket reqServiceCode = new CommandPacket( COMMAND_SEARCH_SERVICECODE, idm, new byte[] {(byte) (index & 0xff), (byte) (index >> 8)}); CommandResponse r = execute(reqServiceCode); byte[] bytes = r.getBytes(); if (bytes[1] != (byte) 0x0b) { // 正常応答かどうか throw new NfcException("ResponseCode is not 0x0b"); } return Arrays.copyOfRange(bytes, 10, bytes.length); }
/** * SystemCodeの一覧を取得します。 * * @return 検出された SystemCodeの一覧を返します。 * @throws NfcException */ public SystemCode[] getSystemCodeList(IDm idm) throws NfcException { // request systemCode CommandPacket reqSystemCode = new CommandPacket(COMMAND_REQUEST_SYSTEMCODE, idm); CommandResponse r = execute(reqSystemCode); byte[] retBytes = r.getBytes(); int num = (int) retBytes[10]; Log.d(TAG, "Num SystemCode: " + num); SystemCode retCodeList[] = new SystemCode[num]; for (int i = 0; i < num; i++) { retCodeList[i] = new SystemCode(Arrays.copyOfRange(retBytes, 11 + i * 2, 13 + i * 2)); } return retCodeList; }
protected UUID postCommand(String command, int version, Object payload) throws IOException { CommandObject cmdObj = new CommandObject(); cmdObj.setCommand(command); cmdObj.setVersion(version); cmdObj.setPayload(payload); String json = connector.toJSON(cmdObj); Map<String, String> params = new HashMap<String, String>(); params.put("payload", json); try { MessageDigest md = MessageDigest.getInstance("SHA-1"); params.put("checksum", Hex.encodeHexString(md.digest(json.getBytes(HttpConnector.UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } CommandResponse response = connector.post("/commands/", params, CommandResponse.class); return response == null ? null : UUID.fromString(response.getUuid()); }
public String runCommand(String shellId, String command, String[] args) { final boolean consoleModeStdin = true; final boolean skipCmdShell = false; String messageId = UUID.randomUUID().toString(); HashMap<String, String> options = new HashMap<>(); options.put("WINRS_CONSOLEMODE_STDIN", consoleModeStdin ? "TRUE" : "FALSE"); options.put("WINRS_SKIP_CMD_SHELL", skipCmdShell ? "TRUE" : "FALSE"); prepareRequest(URI_ACTION_SHELL_COMMAND, shellId, messageId, options); // add SOAP body CommandLine theCommand = new CommandLine(); theCommand.setCommand(command); theCommand.getArguments().addAll(Arrays.asList(args)); // ws call CommandResponse response = wsmanService.command(theCommand); return response.getCommandId(); }
/** * 读取响应 * * @param context 上下文 * @param requestId 请求ID * @param timeOut 超时 * @return 命令响应 */ public CommandResponse read(Context context, int requestId, int timeOut) { ByteBuffer response = null; CommandResponse commandResponse = null; CommandResponse tempResponse = new CommandResponse(); synchronized (context.getReadLock()) { // --直到超时用完,一直读取响应 while (timeOut > 0) { response = context.getReceivedMessageQueue().poll(); // --队列中没有响应时,等待响应 if (response == null) { try { long before = System.currentTimeMillis(); context.getReadLock().wait(timeOut); // 更新超时 timeOut -= (int) (System.currentTimeMillis() - before); } catch (InterruptedException e) { break; } } else { tempResponse.decode(response); // --如果当前响应的ID和请求ID一致,则当前响应就是要读取的响应 if (tempResponse.getId() == requestId) { commandResponse = tempResponse; break; } } } } return commandResponse; }
@Override public void output(CommandResponse response, PrintWriter pw) { try { mapper.writeValue(pw, response.toMap()); } catch (JsonGenerationException e) { LOG.warn("Exception writing command response to JSON:", e); pw.write(ERROR_RESPONSE); } catch (JsonMappingException e) { LOG.warn("Exception writing command response to JSON:", e); pw.write(ERROR_RESPONSE); } catch (IOException e) { LOG.warn("Exception writing command response to JSON:", e); pw.write(ERROR_RESPONSE); } }
public static void main(final String[] args) { try { final Arguments arguments = new Arguments(args); // turn root log wide open, filters will be set to argument levels configureLogging(arguments.getConsoleLogLevel(), arguments.getFileLogLevel()); LOG.info("Version: {}", arguments.getVersion()); LOG.info("Command line args: {}", Joiner.on(", ").join(args)); LOG.info("Console log level: {}", arguments.getConsoleLogLevel().toString()); LOG.info("Log file log level: {}", arguments.getFileLogLevel().toString()); LOG.info(CliCommand.getPlatformInformation()); LOG.info(arguments.getArgumentLog()); if (arguments.isHelp()) { // no need to connect to vend help final Ds3Cli runner = new Ds3Cli(null, arguments, null); final CommandResponse response = runner.getCommandHelp(); System.out.println(response.getMessage()); System.exit(response.getReturnCode()); } final Ds3Client client = createClient(arguments); if (!Utils.isVersionSupported(client)) { System.out.println( String.format( "ERROR: Minimum Black Pearl supported is %s", Utils.MINIMUM_VERSION_SUPPORTED)); System.exit(2); } final Ds3Provider provider = new Ds3ProviderImpl(client, Ds3ClientHelpers.wrap(client)); final FileUtils fileUtils = new FileUtilsImpl(); final Ds3Cli runner = new Ds3Cli(provider, arguments, fileUtils); final CommandResponse response = runner.call(); System.out.println(response.getMessage()); System.exit(response.getReturnCode()); } catch (final FailedRequestException e) { System.out.println("ERROR: " + e.getMessage()); LOG.info("Stack trace: ", e); LOG.info("Printing out the response from the server:"); LOG.info(e.getResponseString()); System.exit(2); } catch (final Exception e) { System.out.println("ERROR: " + e.getMessage()); LOG.info("Stack trace: ", e); System.exit(2); } }
/** * コンストラクタ * * @param response 他のレスポンスをセット */ public CommandResponse(CommandResponse response) { this(response.getBytes()); }