pdu_sms 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ module PduSms
2
+ class UserDataLength
3
+
4
+ def initialize(type, ud)
5
+ if type == :encode_ms
6
+ raise ArgumentError, 'Parameter "ud" must be an instance of UserData' unless UserData == ud.class
7
+ @udl = _count_message(ud)
8
+ elsif type == :decode_ms
9
+ @udl = ud
10
+ elsif type == :encode_sc
11
+ raise ArgumentError, 'Parameter "ud" must be an instance of UserData' unless UserData == ud.class
12
+ @udl = _count_message(ud)
13
+ elsif type == :decode_sc
14
+ @udl = ud
15
+ else
16
+ raise ArgumentError, 'The "type" is incorrect'
17
+ end
18
+ end
19
+
20
+ def UserDataLength.encode_ms(ud)
21
+ new(:encode_ms, ud).freeze
22
+ end
23
+
24
+ def UserDataLength.decode_ms(pdu_str)
25
+ pdu = UserDataLength.cut_off_pdu(pdu_str, :current, :ms)
26
+ new(:decode_ms, pdu).freeze
27
+ end
28
+
29
+ def UserDataLength.encode_sc(ud)
30
+ new(:encode_sc, ud).freeze
31
+ end
32
+
33
+ def UserDataLength.decode_sc(pdu_str)
34
+ pdu = UserDataLength.cut_off_pdu(pdu_str, :current, :sc)
35
+ new(:decode_sc, pdu).freeze
36
+ end
37
+
38
+ def get_hex
39
+ @udl
40
+ end
41
+
42
+ def UserDataLength.cut_off_pdu(pdu, part=:all, type=:ms) # tail current
43
+ if type == :ms
44
+ part_pdu = ValidityPeriod.cut_off_pdu(pdu, :tail, :ms)
45
+ elsif type == :sc
46
+ part_pdu = ServiceCenterTimeStamp.cut_off_pdu(pdu, :tail, :sc)
47
+ else
48
+ raise ArgumentError, 'The "pdu" is incorrect'
49
+ end
50
+ raise ArgumentError, 'The "pdu" is incorrect' if part_pdu.length < 2
51
+ current = part_pdu[0..1]
52
+ tail = part_pdu[2..-1]
53
+ case part
54
+ when :current then current
55
+ when :tail then tail
56
+ else [current,tail]
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def _count_message(ud)
63
+ case ud.get_coding
64
+ when ALPHABET_7BIT
65
+ count_sms = ud.get_message.length
66
+ count_sms += 8 if ud.is_udh?
67
+ when ALPHABET_8BIT
68
+ count_sms = ud.get_message.length
69
+ count_sms += 6 if ud.is_udh?
70
+ when ALPHABET_16BIT
71
+ count_sms = ud.get_message.length * 2
72
+ count_sms += 6 if ud.is_udh?
73
+ else
74
+ raise ArgumentError, 'The "ud" is incorrect'
75
+ end
76
+ '%02x' % count_sms
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,164 @@
1
+ require 'date'
2
+
3
+ module PduSms
4
+ class ValidityPeriod
5
+
6
+ def initialize(type, times, type_time=false)
7
+ @type_time = type_time
8
+ return @vp = '' unless times
9
+ if type == :encode_ms
10
+ @type_time = (0..635040).include?(times) ? VALIDITY_PERIOD_FORMAT_10 : VALIDITY_PERIOD_FORMAT_11 unless @type_time
11
+ if @type_time == VALIDITY_PERIOD_FORMAT_10
12
+ @vp = _relative_seconds times
13
+ elsif @type_time == VALIDITY_PERIOD_FORMAT_11
14
+ @vp = _absolute_timestamp times
15
+ else
16
+ raise ArgumentError, 'The "type_time" is incorrect'
17
+ end
18
+ elsif type == :decode_ms
19
+ if times.length == 2
20
+ @type_time = VALIDITY_PERIOD_FORMAT_10
21
+ @vp = times
22
+ elsif times.length == 14
23
+ @type_time = VALIDITY_PERIOD_FORMAT_11
24
+ @vp = times
25
+ else
26
+ raise ArgumentError, 'The "times" is incorrect'
27
+ end
28
+ else
29
+ raise ArgumentError, 'The "type" is incorrect'
30
+ end
31
+ end
32
+
33
+ def ValidityPeriod.encode_ms(times, type_time=false)
34
+ new(:encode_ms, times, type_time).freeze
35
+ end
36
+
37
+ def ValidityPeriod.decode_ms(pdu_str)
38
+ vp = ValidityPeriod.cut_off_pdu(pdu_str, part=:current, :ms)
39
+ new(:decode_ms, vp).freeze
40
+ end
41
+
42
+ def ValidityPeriod.cut_off_pdu(pdu, part=:all, type=:ms)
43
+ part_pdu = DataCodingScheme.cut_off_pdu(pdu, :tail, :ms)
44
+ pdu_type = PDUType.decode_ms(pdu)
45
+ if pdu_type.validity_period_format_relative?
46
+ raise ArgumentError, 'The "pdu" is incorrect' if part_pdu.length < 2
47
+ current = part_pdu[0..1]
48
+ tail = part_pdu[2..-1]
49
+ elsif pdu_type.validity_period_format_absolute?
50
+ raise ArgumentError, 'The "pdu" is incorrect' if part_pdu.length < 14
51
+ current = part_pdu[0..13]
52
+ tail = part_pdu[14..-1]
53
+ else
54
+ current = false
55
+ tail = part_pdu
56
+ end
57
+ case part
58
+ when :current then current
59
+ when :tail then tail
60
+ else [current,tail]
61
+ end
62
+ end
63
+
64
+ def get_type_time
65
+ @type_time
66
+ end
67
+
68
+ def get_time
69
+ if @type_time == VALIDITY_PERIOD_FORMAT_10
70
+ _relative_pdu @vp
71
+ elsif @type_time == VALIDITY_PERIOD_FORMAT_11
72
+ _absolute_pdu @vp
73
+ else
74
+ ''
75
+ end
76
+ end
77
+
78
+ def get_hex
79
+ @vp
80
+ end
81
+
82
+ def is_setup
83
+ if @vp.empty?
84
+ VALIDITY_PERIOD_FORMAT_00
85
+ elsif @vp.length == 2
86
+ VALIDITY_PERIOD_FORMAT_10
87
+ else
88
+ VALIDITY_PERIOD_FORMAT_11
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def _relative_seconds(seseconds)
95
+ minutes = (seseconds.abs / 60).floor
96
+ case minutes
97
+ when 0..4
98
+ @vp = 0x0
99
+ when 5..720
100
+ @vp = minutes / 5 - 1
101
+ when 721..749
102
+ @vp = 0x8f
103
+ when 750..1440
104
+ @vp = (minutes - 720) / 30 + 143
105
+ when 1441..2879
106
+ @vp = 0xa7
107
+ when 2880..43200
108
+ @vp = minutes / 1440 + 166
109
+ when 43201..50399
110
+ @vp = 0xc4
111
+ when 50400..635040
112
+ @vp = minutes / 10080 + 192
113
+ else
114
+ @vp = 0xff
115
+ end
116
+ @vp = '%02x' % @vp
117
+ end
118
+
119
+ def _relative_pdu(vp)
120
+ vp_integer_16 = vp.to_i(16)
121
+ case vp_integer_16
122
+ when 0..143
123
+ minutes = (vp_integer_16 + 1) * 5
124
+ when 144..167
125
+ minutes = 720 + (vp_integer_16 - 143) * 30
126
+ when 168..196
127
+ minutes = (vp_integer_16 - 166) * 1440
128
+ when 197..255
129
+ minutes = (vp_integer_16 - 192) * 10080
130
+ else
131
+ raise ArgumentError, 'The "vp" is incorrect'
132
+ end
133
+ minutes * 60
134
+ end
135
+
136
+ def _absolute_timestamp(timestamp)
137
+ time = Time.at(timestamp).to_datetime
138
+ date_time = time.strftime('%y%m%d%H%M%S')
139
+ if time.strftime('%z').to_i >= 0
140
+ date_time += ('%02x' % (4 * time.strftime('%z')[0..2].to_i + time.strftime('%z')[3..4].to_i / 15).to_s.to_i(16))
141
+ else
142
+ tz = '%08b' % ((4 * time.strftime('%z')[0..2].to_i.abs + time.strftime('%z')[3..4].to_i / 15).to_s.to_i(16))
143
+ tz[0] = ?1
144
+ date_time += '%02x' % tz.to_i(2)
145
+ end
146
+ @vp = encode_bcd(date_time)
147
+ end
148
+
149
+ def _absolute_pdu(vp)
150
+ ss = decode_bcd(vp)
151
+ year, month, day, hours, minutes, seconds, zone_quarter = Time.now.year.to_s[0..1] + ss[0..1], ss[2..3], ss[4..5], ss[6..7], ss[8..9], ss[10..11], ss[12..13]
152
+ tz = '%08b' % zone_quarter
153
+ if tz[0] == '1'
154
+ tz[0] = ?0
155
+ zone = '-'
156
+ else
157
+ zone = '+'
158
+ end
159
+ zone += '%02i:%02i' % [(tz.to_i(2) / 4), ((tz.to_i(2) % 4) * 15)]
160
+ Time.new(year, month, day, hours, minutes, seconds, zone).to_i
161
+ end
162
+
163
+ end
164
+ end
@@ -0,0 +1,3 @@
1
+ module PduSms
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pdu_sms/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pdu_sms"
8
+ spec.version = PduSms::VERSION
9
+ spec.authors = ["Mikhail Stolyarov"]
10
+ spec.email = ["schnack.desu@gmail.com"]
11
+ spec.license = "MTI"
12
+ spec.summary = %q{PDU SMS coding and decoding library}
13
+ spec.description = %q{Look README.md for details}
14
+ spec.homepage = "https://github.com/schnack/pdu_sms"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdu_sms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Stolyarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ description: Look README.md for details
56
+ email:
57
+ - schnack.desu@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/pdu_sms.rb
71
+ - lib/pdu_sms/data_coding_scheme.rb
72
+ - lib/pdu_sms/destination_address.rb
73
+ - lib/pdu_sms/helpers.rb
74
+ - lib/pdu_sms/message_reference.rb
75
+ - lib/pdu_sms/originating_address.rb
76
+ - lib/pdu_sms/packet_data_unit.rb
77
+ - lib/pdu_sms/packet_data_unit_error.rb
78
+ - lib/pdu_sms/pdu_type.rb
79
+ - lib/pdu_sms/phone.rb
80
+ - lib/pdu_sms/protocol_identifier.rb
81
+ - lib/pdu_sms/service_center_address.rb
82
+ - lib/pdu_sms/service_center_time_stamp.rb
83
+ - lib/pdu_sms/user_data.rb
84
+ - lib/pdu_sms/user_data_length.rb
85
+ - lib/pdu_sms/validity_period.rb
86
+ - lib/pdu_sms/version.rb
87
+ - pdu_sms.gemspec
88
+ homepage: https://github.com/schnack/pdu_sms
89
+ licenses:
90
+ - MTI
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: PDU SMS coding and decoding library
112
+ test_files: []