Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/hwbridge/extensions/rftransceiver/rftransceiver.rb
Views: 11705
#1# -*- coding: binary -*-2require 'rex/post/hwbridge/client'34module Rex5module Post6module HWBridge7module Extensions8module RFTransceiver910###11# RF Transceiver extension - set of commands to be executed on transceivers like the TI cc11XX12###1314class RFTransceiver < Extension1516def initialize(client)17super(client, 'rftransceiver')1819# Alias the following things on the client object so that they20# can be directly referenced21client.register_extension_aliases(22[23{24'name' => 'rftransceiver',25'ext' => self26}27])28end2930# Gets supported USB Indexes31# @return [Array] Indexes32def supported_idx33client.send_request("/rftransceiver/supported_idx")34end3536# Sets the frequency37# @param idx [Integer] HW Index38# @param opt [Hash] Optional: "mhz" => 2439# @param freq [Integer] Frequency to set40def set_freq(idx, freq, opt={})41request = "/rftransceiver/#{idx}/set_freq?freq=#{freq}"42request << "&mhz=#{opt['mhz']}" if opt.has_key? 'mhz'43client.send_request(request)44end4546# Retrieves a list of supported Modulations47# @param idx [Integer] HW Index48# @return [Array] of Modulation strings49def get_supported_modulations(idx)50client.send_request("/rftransceiver/#{idx}/get_modulations")51end5253# Sets the mode54# @param idx [Integer] HW Index55# @param mode [String] Either RX, TX or IDLE56def set_mode(idx, mode)57client.send_request("/rftransceiver/#{idx}/set_mode?mode=#{mode}")58end5960# Sets the modulation value61# @param idx [Integer] HW Index62# @param mod [String] Modulation Technique63def set_modulation(idx, mod)64client.send_request("/rftransceiver/#{idx}/set_modulation?mod=#{mod}")65end6667# Sets fixed packet len68# @param idx [Integer] HW Index69# @param len [Integer] Length to set70def make_pkt_flen(idx, len)71client.send_request("/rftransceiver/#{idx}/make_packet_flen?len=#{len}")72end7374# Sets variable packet len75# @param idx [Integer] HW Index76# @param len [Integer] Length to set77def make_pkt_vlen(idx, len)78client.send_request("/rftransceiver/#{idx}/make_packet_vlen?len=#{len}")79end8081# Transmits data82# @param idx [Integer] HW Index83# @param data [String] Data to transmit84# @param opt [Hash] Optional parameters: "repeat" => Integer, "offset" => Integer85def rfxmit(idx, data, opt={})86data = Base64.urlsafe_encode64(data)87request = "/rftransceiver/#{idx}/rfxmit?data=#{data}"88request << "&repeat=#{opt['repeat']}" if opt.has_key? 'repeat'89request << "&offset=#{opt['offset']}" if opt.has_key? 'offset'90client.send_request(request)91end9293# Receives a packet94# @param idx [Integer] HW Index95# @param opt [Hash] Optional parameters: "timeout" => Integer, "blocksize" => Integer96# @return [Hash] "data" => <received data> "timestamp" => When it was received97def rfrecv(idx, opt={})98request = "/rftransceiver/#{idx}/rfrecv"99if opt.size() > 0100first = true101request << '?'102if opt.has_key? 'timeout'103request << "timeout=#{opt['timeout']}"104first = false105end106if opt.has_key? 'blocksize'107request << '&' unless first108request << "blocksize=#{opt['blocksize']}"109end110end111data = client.send_request(request)112# Note the data is initially base64 encoded113if data.size() > 0114data['data'] = Base64.urlsafe_decode64(data['data']) if data.has_key? 'data'115end116data117end118119def enable_packet_crc(idx)120client.send_request("/rftransceiver/#{idx}/enable_packet_crc")121end122123def enable_manchester(idx)124client.send_request("/rftransceiver/#{idx}/enable_machester")125end126127def set_channel(idx, channel)128client.send_request("/rftransceiver/#{idx}/set_channel?channel=#{channel}")129end130131def set_channel_bandwidth(idx, bandwidth, opt={})132request = "/rftransceiver/#{idx}/set_channel_bandwidth?bw=#{bandwidth}"133request << "&mhz=#{opt['mhz']}" if opt.has_key? 'mhz'134client.send_request(request)135end136137def set_channel_spc(idx, opt={})138request = "/rftransceiver/#{idx}/set_channel_spc"139if opt.size > 0140request << '?'141first = true142if opt.has_key? 'chanspc'143request << "chanspc=#{opt['chanspc']}"144first = false145end146if opt.has_key? 'chanspc_m'147request << '&' unless first148request << "chanspc_m=#{opt['chanspc_m']}"149first = false150end151if opt.has_key? 'chanspc_e'152request << '&' unless first153request << "chanspc_e=#{opt['chanspc_e']}"154first = false155end156if opt.has_key? 'mhz'157request << '&' unless first158request << "mhz=#{opt['mhz']}"159end160end161client.send_request(request)162end163164def set_baud_rate(idx, rate, opt={})165request = "/rftransceiver/#{idx}/set_baud_rate?rate=#{rate}"166request << "&mhz=#{opt['mhz']}" if opt.has_key? 'mhz'167client.send_request(request)168end169170def set_deviation(idx, deviat, opt={})171request = "/rftransceiver/#{idx}/set_deviation?deviat=#{deviat}"172request << "&mhz=#{opt['mhz']}" if opt.has_key? 'mhz'173client.send_request(request)174end175176def set_sync_word(idx, word)177client.send_request("/rftransceiver/#{idx}/set_sync_word?word=#{word}")178end179180def set_sync_mode(idx, mode)181client.send_request("/rftransceiver/#{idx}/set_sync_mode?mode=#{mode}")182end183184def set_number_preamble(idx, num)185client.send_request("/rftransceiver/#{idx}/set_number_preamble?num=#{num}")186end187188def set_lowball(idx)189client.send_request("/rftransceiver/#{idx}/set_lowball")190end191192def set_maxpower(idx)193client.send_request("/rftransceiver/#{idx}/set_maxpower")194end195196def set_power(idx, power)197client.send_request("/rftransceiver/#{idx}/set_power?power=#{power}")198end199end200201end202end203end204end205end206207208209