#!/usr/bin/env ruby # frozen_string_literal: false require 'pwn' require 'optparse' require 'json' opts = {} OptionParser.new do |options| options.banner = "USAGE: #{$PROGRAM_NAME} [opts] " options.on('-dDEV', '--block-dev=DEV', '') do |d| opts[:block_dev] = d end options.on('-bBAUD', '--baud=BAUD', '') do |b| opts[:baud] = b end options.on('-DDATABITS', '--data-bits=DATABITS', '') do |d| opts[:data_bits] = d end options.on('-sSTOPBITS', '--stop-bits=STOPBITS', '') do |s| opts[:stop_bits] = s end options.on('-pPARITY', '--parity=PARITY', '') do |p| opts[:parity] = p end options.on('-fFLOWCTRL', '--flow-control=FLOWCTRL', '') do |f| opts[:flow_control] = f end end.parse! if opts.empty? puts `#{$PROGRAM_NAME} --help` exit 1 end begin block_dev = opts[:block_dev] if File.exist?(opts[:block_dev]) baud = opts[:baud] data_bits = opts[:data_bits] stop_bits = opts[:stop_bits] parity = opts[:parity] flow_control = opts[:flow_control] son_micro_rfid_obj = PWN::Plugins::SonMicroRFID.connect( block_dev: block_dev, baud: baud, data_bits: data_bits, stop_bits: stop_bits, parity: parity, flow_control: flow_control ) puts "- Welcome to #{File.basename($PROGRAM_NAME)} -" puts "Connected via: #{block_dev} @ #{son_micro_rfid_obj[:serial_conn].modem_params}" puts "Flow Control: #{son_micro_rfid_obj[:serial_conn].flow_control}" puts "Signals: #{son_micro_rfid_obj[:serial_conn].signals}" exec_resp = PWN::Plugins::SonMicroRFID.exec( son_micro_rfid_obj: son_micro_rfid_obj, cmd: :firmware ) puts "Firmware Version: #{exec_resp.first[:decoded_resp]}" # Main Menu menu_msg = '' loop do puts "\n>> MAIN MENU OPTIONS:" puts '[(R)ead Card]' puts '[(B)ackup Card]' puts '[(C)opy Card]' puts '[(L)oad Card from File]' puts '[(W)arm Reset]' puts '[(Q)uit]' puts menu_msg print 'MAIN MENU OPTION >>> ' menu_msg = '' option = gets.scrub.chomp.strip.upcase.to_sym case option when :R menu_msg = 'READ CARD' rfid_data = PWN::Plugins::SonMicroRFID.read_card( son_micro_rfid_obj: son_micro_rfid_obj ) when :B menu_msg = 'BACKUP CARD TO FILE' rfid_data = PWN::Plugins::SonMicroRFID.backup_card( son_micro_rfid_obj: son_micro_rfid_obj ) when :C menu_msg = 'COPY CARD' rfid_data = PWN::Plugins::SonMicroRFID.copy_card( son_micro_rfid_obj: son_micro_rfid_obj ) when :L menu_msg = 'LOAD FROM FILE' rfid_data = PWN::Plugins::SonMicroRFID.load_card_from_file( son_micro_rfid_obj: son_micro_rfid_obj ) when :W menu_msg = 'WARM RESET' exec_resp = PWN::Plugins::SonMicroRFID.exec( son_micro_rfid_obj: son_micro_rfid_obj, cmd: :reset ) when :Q exit else menu_msg = '****** ERROR: Invalid Menu Option Selected ******' end end rescue StandardError => e raise e rescue SystemExit, Interrupt puts "\nGoodbye." ensure son_micro_rfid_obj = PWN::Plugins::SonMicroRFID.disconnect(son_micro_rfid_obj: son_micro_rfid_obj) if son_micro_rfid_obj end