Class: HardsploitAPI_UART

Inherits:
Object
  • Object
show all
Defined in:
lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb

Instance Method Summary collapse

Constructor Details

#initialize(baud_rate:, word_width:, use_parity_bit:, parity_type:, nb_stop_bits:, idle_line_level:) ⇒ HardsploitAPI_UART

Returns a new instance of HardsploitAPI_UART.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 12

def initialize(baud_rate:,word_width:,use_parity_bit:,parity_type:,nb_stop_bits:,idle_line_level:)
	#to be sure the singleton was initialize
	HardsploitAPI.instance
	self.baud_rate 			 = baud_rate
	self.word_width 		 = word_width
	self.use_parity_bit  = use_parity_bit
	self.parity_type 		 = parity_type
	self.nb_stop_bits 	 = nb_stop_bits
	self.idle_line_level = idle_line_level
	setSettings

	@payload_TX = Array.new
end

Instance Method Details

#baud_rateObject



26
27
28
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 26

def baud_rate
	return 150000000 / @baud_rate
end

#baud_rate=(baud_rate) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 30

def baud_rate=(baud_rate)
	if (baud_rate >= 2400) and (baud_rate <= 1036800) then
		@baud_rate = 150000000 / baud_rate
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#disableMeasureBaudRateObject

disableMeasureBaudRate

*



143
144
145
146
147
148
149
150
151
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 143

def disableMeasureBaudRate
	packet = HardsploitAPI.prepare_packet
	packet.push 0x40 # command
	begin
		tmp = HardsploitAPI.instance.sendAndReceiveDATA(packet,1000)
	rescue
		raise HardsploitAPI::ERROR::USB_ERROR
	end
end

#enableMeasureBaudRateObject

enableMeasureBaudRate

*



130
131
132
133
134
135
136
137
138
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 130

def enableMeasureBaudRate
	packet = HardsploitAPI.prepare_packet
	packet.push 0x41 # command
	begin
		tmp = HardsploitAPI.instance.sendAndReceiveDATA(packet,1000)
	rescue
		raise HardsploitAPI::ERROR::USB_ERROR
	end
end

#idle_line_levelObject



86
87
88
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 86

def idle_line_level
	return @idle_line_level
end

#idle_line_level=(idle_line_level) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 90

def idle_line_level=(idle_line_level)
	if (idle_line_level >= 0) and (idle_line_level <= 1) then
		@idle_line_level = idle_line_level
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#measureBaudRateObject

measureBaudRate

  • Return 32 bits period



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 156

def measureBaudRate
	packet = HardsploitAPI.prepare_packet
	packet.push 0x30 # command

	begin
		tmp = HardsploitAPI.instance.sendAndReceiveDATA(packet,1000)
	rescue
		raise HardsploitAPI::ERROR::USB_ERROR
	end
	#remove header (4 bytes   2 for size 2 for type of command)
	tmp = tmp.drop(4)
	period =  tmp[0] + (tmp[1] << 8 )  + (tmp[2] << 16 ) +  (tmp[3] << 24 )
	period = period * 33.33*(10**-9) #s
	if period > 0 then
		return (1 / period).to_i
	else
		return 0
	end
end

#nb_stop_bitsObject



74
75
76
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 74

def nb_stop_bits
	return @nb_stop_bits
end

#nb_stop_bits=(nb_stop_bits) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 78

def nb_stop_bits=(nb_stop_bits)
	if (nb_stop_bits >= 1) and (nb_stop_bits <= 2) then
		@nb_stop_bits = nb_stop_bits
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#parity_typeObject



62
63
64
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 62

def parity_type
	return @parity_type
end

#parity_type=(parity_type) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 66

def parity_type=(parity_type)
	if (parity_type >= 0) and (parity_type <= 1) then
		@parity_type = parity_type
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#sendAndReceivedObject

First write data if needed and refresh (data are sent and reveived data if needed) and you obtain available data

  • Return nothing



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 111

def sendAndReceived
	packet = HardsploitAPI.prepare_packet
	packet.push 0x20 #Send command
	packet.concat @payload_TX

	begin
		tmp = HardsploitAPI.instance.sendAndReceiveDATA(packet,1000)
	rescue
		raise HardsploitAPI::ERROR::USB_ERROR
	end

	@payload_TX.clear
	#remove header (4 bytes   2 for size 2 for type of command 1 dummy byte)
	return tmp.drop(5)
end

#setSettingsObject

settings

  • Return nothing



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 178

def setSettings
	packet = HardsploitAPI.prepare_packet
	packet.push 0x00 #Settings command
	packet.push ((@parity_type & 0b1) <<  7) ||  ((@use_parity_bit & 0b1) <<  6) || 	((@nb_stop_bits & 0b11) <<  4) || (@word_width & 0b1111)
	packet.push @idle_line_level & 1
	packet.push HardsploitAPI.lowByte(word: @baud_rate)
	packet.push HardsploitAPI.highByte(word: @baud_rate)

	begin
		HardsploitAPI.instance.sendPacket packet
		sleep(1)
	#	tmp= HardsploitAPI.instance.receiveDATA(1000)
		#remove header (4 bytes   2 for size 2 for type of command)
	#	return tmp.bytes.drop(4)
	rescue
		raise HardsploitAPI::ERROR::USB_ERROR
	end
end

#use_parity_bitObject



50
51
52
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 50

def use_parity_bit
	return @use_parity_bit
end

#use_parity_bit=(use_parity_bit) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 54

def use_parity_bit=(use_parity_bit)
	if (use_parity_bit >= 0) and (use_parity_bit <= 1) then
		@use_parity_bit = use_parity_bit
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#word_widthObject



38
39
40
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 38

def word_width
	return @word_width
end

#word_width=(word_width) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 42

def word_width=(word_width)
	if (word_width >= 5) and (word_width <= 8) then
		@word_width = word_width
	else
		raise HardsploitAPI::ERROR::UART_WrongSettings
	end
end

#write(payload:) ⇒ Object

write

  • payload

    Byte array want to send

  • Return nothing



101
102
103
104
105
106
# File 'lib/HardsploitAPI/Modules/UART/HardsploitAPI_UART.rb', line 101

def write(payload:)
	if ( (@payload_TX.size + payload.size)  > 4000) then
		raise HardsploitAPI::ERROR::UART_WrongTxPayloadSize
	end
	@payload_TX.concat payload #Add data
end