Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optional_parameter support for submit_sm

  • Loading branch information...
commit 69dd17b525fdee8b4482227b44efafe4adfc41bf 1 parent cc6f8b1
@raykrueger authored
View
1  .gitignore
@@ -17,6 +17,7 @@ tmtags
coverage
rdoc
pkg
+tags
## PROJECT::SPECIFIC
*.log
View
7 lib/smpp/base.rb
@@ -165,7 +165,10 @@ def read_pdu(data)
end
def hex_debug(data, prefix = "")
- return unless @config[:hex_debug]
+ Base.hex_debug(data, prefix)
+ end
+
+ def Base.hex_debug(data, prefix = "")
logger.debug do
message = "Hex dump follows:\n"
hexdump(data).each_line do |line|
@@ -175,7 +178,7 @@ def hex_debug(data, prefix = "")
end
end
- def hexdump(target)
+ def Base.hexdump(target)
width=16
group=2
View
23 lib/smpp/pdu/base.rb
@@ -98,11 +98,32 @@ def to_human
end
# return int as binary string of 4 octets
- def fixed_int(value)
+ def Base.fixed_int(value)
arr = [value >> 24, value >> 16, value >> 8, value & 0xff]
arr.pack("cccc")
end
+ def fixed_int(value)
+ Base.fixed_int(value)
+ end
+
+ #expects a hash like {tag => {:tag => tag, :length => length, :value => value}}
+ def Base.optional_parameters_to_buffer(optionals)
+ output = ""
+ optionals.each do |tag, tlv|
+ length = tlv[:value].length
+ buffer = []
+ buffer += [tag >> 8, tag & 0xff]
+ buffer += [length >> 8, length & 0xff]
+ output << buffer.pack('cccc') << tlv[:value]
+ end
+ output
+ end
+
+ def optional_parameters_to_buffer(optionals)
+ Base.optional_parameters_to_buffer(optionals)
+ end
+
def next_sequence_number
Base.next_sequence_number
end
View
18 lib/smpp/pdu/submit_sm.rb
@@ -4,7 +4,7 @@ class Smpp::Pdu::SubmitSm < Smpp::Pdu::Base
attr_reader :service_type, :source_addr_ton, :source_addr_npi, :source_addr, :dest_addr_ton, :dest_addr_npi,
:destination_addr, :esm_class, :protocol_id, :priority_flag, :schedule_delivery_time,
:validity_period, :registered_delivery, :replace_if_present_flag, :data_coding,
- :sm_default_msg_id, :sm_length, :udh, :short_message
+ :sm_default_msg_id, :sm_length, :udh, :short_message, :optional_parameters
# Note: short_message (the SMS body) must be in iso-8859-1 format
@@ -33,11 +33,17 @@ def initialize(source_addr, destination_addr, short_message, options={}, seq = n
payload = @udh ? @udh + @short_message : @short_message
@sm_length = payload.length
+ @optional_parameters = options[:optional_parameters]
+
# craft the string/byte buffer
pdu_body = sprintf("%s\0%c%c%s\0%c%c%s\0%c%c%c%s\0%s\0%c%c%c%c%c%s", @service_type, @source_addr_ton, @source_addr_npi, @source_addr,
@dest_addr_ton, @dest_addr_npi, @destination_addr, @esm_class, @protocol_id, @priority_flag, @schedule_delivery_time, @validity_period,
@registered_delivery, @replace_if_present_flag, @data_coding, @sm_default_msg_id, @sm_length, payload)
+ if @optional_parameters
+ pdu_body << optional_parameters_to_buffer(@optional_parameters)
+ end
+
seq ||= next_sequence_number
super(SUBMIT_SM, 0, seq, pdu_body)
@@ -70,8 +76,14 @@ def self.from_wire_data(seq, status, body)
options[:data_coding],
options[:sm_default_msg_id],
options[:sm_length],
- short_message = body.unpack('Z*CCZ*CCZ*CCCZ*Z*CCCCCa*')
- Smpp::Base.logger.debug "DeliverSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}"
+ remaining_bytes = body.unpack('Z*CCZ*CCZ*CCCZ*Z*CCCCCa*')
+
+ short_message = remaining_bytes.slice!(0...options[:sm_length])
+
+ #everything left in remaining_bytes is 3.4 optional parameters
+ options[:optional_parameters] = optional_parameters(remaining_bytes)
+
+ Smpp::Base.logger.debug "SubmitSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}"
new(source_addr, destination_addr, short_message, options, seq)
end
View
45 test/submit_sm_test.rb
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'test/unit'
+require 'smpp'
+
+class SubmitSmTest < Test::Unit::TestCase
+
+ def setup
+ Smpp::Base.logger = Logger.new($stdout)
+ end
+
+ def test_fixnum_optional_parameter
+ value = [383 >> 8, 383 & 0xff]
+ optionals = {0x2150 => {:tag => 0x2150, :value => value.pack('cc')}}
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
+ Smpp::Base.hex_debug(pdu.data)
+
+ end
+
+ def test_fixnum_optional_parameter
+ value = [383 >> 8, 383 & 0xff]
+ optionals = {0x2150 => {:tag => 0x2150, :value => value.pack('cc')}}
+
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
+ pdu_from_wire = Smpp::Pdu::Base.create(pdu.data)
+
+ assert optional = pdu_from_wire.optional_parameters[0x2150]
+ assert_equal 2, optional[:length]
+
+ optional_value = optional[:value].unpack('n')[0]
+ assert_equal 383, optional_value
+ end
+
+ def test_string_optional_parameter
+ optionals = {0x2150 => {:tag => 0x2150, :length => 5, :value => "boosh"}}
+
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
+ pdu_from_wire = Smpp::Pdu::Base.create(pdu.data)
+
+ assert optional = pdu_from_wire.optional_parameters[0x2150]
+ assert_equal 5, optional[:length]
+
+ optional_value = optional[:value].unpack("A*")[0]
+ assert_equal 'boosh', optional_value
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.