cwmp 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjZmMzY4NjIxNGFmMGMxYjA0ZTMyNzE5YTQ4ZjgwYjRkZTc2NGYzNQ==
4
+ ZjYyNWVmZDgwODQ1OTA1MGUyMjhlZjRkOGJmODdiNjVmZDc0OTZmYQ==
5
5
  data.tar.gz: !binary |-
6
- N2Y1ZjYwMjJmODE2ZDk2OTMxNjEwM2ZhMzU2MzIzNTVlYmE0MmY5Mg==
6
+ YTJhMTlmNjU3M2EzZmVmMmNiY2Q4ZGM4MTEwOTIyZjkzMzRhYWFmOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjBmYjE3ZDg4NDVhODJiZDIyNDIwM2U1NzEyNzBiOTE3Y2UwNTNjOWFiMjAz
10
- ZjgwYWI4MjliYzFiYjYxNzlkYTNjMjJhZjRlZDhkMmExNDczZDEzMTZlYzk2
11
- ZWU0MjIwOGM5ZGIzMmI0ZjY3NmMxNTYxZmE5NDRjOGM1MDhiZDI=
9
+ NjU0Y2RiY2NmNzhkZTFmYmM2MGYzYThhYzNjYTA5ZDA3MjBkY2QzOTE1MDU5
10
+ ZDQ1MmI1MTFhMTQxMmIyNzNiMjQ3NDYyMWI2ODI0ZmI0NDdjZmIwZGMxNzg0
11
+ OGMxNzdiY2QxMzMwMmY2YWFhY2U4ZjRlYjUzYWQ3NTNkNGMzNDg=
12
12
  data.tar.gz: !binary |-
13
- MjVhZjZlZGNmM2M2ZmNlYzAwYzkxYWI3NTk3ZjYyMTlmNGE5NTVmMDVhZmIy
14
- Njc0MGQyMWIzMjMyNmNmOGJhOTgzM2NjOGY1Y2I5YzYwOTU0MDBmYWVjMDgz
15
- NmMyMzEzMWVhNzQ1NjBjNjI2YTdlY2U0ZGRhMTYzMTlkYjRhMDQ=
13
+ MzgyZDhiYWM5ZmQ4YmFhZWY4YWEzYWI0NzgyNzIxMmVkODU3NjFjMWIwMmQx
14
+ MmEzZGI0ZjMxZTQ2YzkyYjM3ODM3MTEzZTZiMzRhNmQ0NTBjMjE5ZTFlNDlj
15
+ ZDI5MGY4YjIyNDJmZDBlZGU1NGYyNjhjNzJkY2VkZmUxMWRlNTc=
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Ruby CWMP
2
2
 
3
- A ruby library to parse and generate CWMP messages. Includes an ACS server and a CPE simulator.
3
+ A ruby library for parsing and crafting CWMP messages. Includes an ACS server and a CPE simulator.
4
4
 
5
- ## Using the library to build CWMP messages
5
+ ## Crafting CWMP messages
6
6
 
7
7
  ```ruby
8
8
  require 'cwmp'
data/bin/acs CHANGED
@@ -11,7 +11,5 @@ o.on('-v', '--version', 'Version') { puts "acs v#{Cwmp::VERSION}"; exit }
11
11
  o.parse!
12
12
 
13
13
  port = options['port'] || 9321
14
-
15
- puts "Starting ACS Daemon on http://localhost:#{port}"
16
14
  Cwmp::Acs.new(port).start
17
15
 
@@ -3,9 +3,9 @@ require './lib/cwmp/version'
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'cwmp'
5
5
  spec.version = Cwmp::VERSION
6
- spec.date = '2014-07-14'
6
+ spec.date = Date.today.to_s
7
7
  spec.summary = "A CWMP library"
8
- spec.description = "A ruby library to parse and generate CWMP messages. Includes an ACS server and a CPE simulator."
8
+ spec.description = "A ruby library for parsing and crafting CWMP messages. Includes an ACS server and a CPE simulator."
9
9
  spec.authors = ["Luca Cervasio"]
10
10
  spec.email = 'luca.cervasio@gmail.com'
11
11
  spec.files = `git ls-files`.split($/)
@@ -4,11 +4,31 @@ require 'thin'
4
4
  require 'http_router'
5
5
  require 'nokogiri'
6
6
  require 'readline'
7
+ require 'digest'
7
8
 
9
+ Thread::abort_on_exception = true
8
10
 
9
11
  module Cwmp
10
12
 
13
+ class AcsCpe
14
+ attr_accessor :serial, :conn_req, :queue, :session_cookie
15
+
16
+ def initialize serial
17
+ @serial = serial
18
+ @queue = Queue.new
19
+ end
20
+
21
+ def do_connection_request
22
+ c = HTTPClient.new
23
+ c.get @conn_req
24
+ end
25
+
26
+ end
27
+
11
28
  class Handler
29
+ def initialize obj
30
+ @acs = obj
31
+ end
12
32
 
13
33
  def call(env)
14
34
  req = Rack::Request.new(env)
@@ -17,8 +37,11 @@ module Cwmp
17
37
  if len == 0
18
38
  message_type = ''
19
39
  else
20
- doc = Nokogiri::XML(req.body.read)
21
- message_type = doc.css("soap|Body").children.map(&:name)[1]
40
+ body = req.body.read
41
+ doc = Nokogiri::XML(body)
42
+ body =~ /(\w+):Envelope/
43
+ soap_ns = $1
44
+ message_type = doc.css("#{soap_ns}|Body")[0].element_children[0].name
22
45
  end
23
46
 
24
47
  if message_type == "Inform"
@@ -27,25 +50,54 @@ module Cwmp
27
50
  serial_number = doc.css("SerialNumber").text
28
51
  event_codes = doc.css("EventCode").map { |n| n.text }
29
52
  parameters = {}
30
- doc.css("ParameterValueStruct").map { |it| parameters[it.children[1].text] = it.children[3].text }
53
+ doc.css("ParameterValueStruct").map { |it| parameters[it.element_children[0].text] = it.element_children[1].text }
54
+
55
+ ck = Digest::MD5.hexdigest("#{serial_number}#{Time.new.to_i}")
31
56
 
57
+ if !@acs.cpes.has_key? serial_number
58
+ cpe = AcsCpe.new serial_number
59
+ cpe.conn_req = parameters['InternetGatewayDevice.ManagementServer.ConnectionRequestURL']
60
+ cpe.session_cookie = ck
61
+ @acs.cpes[serial_number] = cpe
62
+ end
32
63
  puts "got Inform from #{req.ip}:#{req.port} [sn #{serial_number}] with eventcodes #{event_codes.join(", ")}"
33
64
 
34
65
  inform_response = Cwmp::Message::inform_response
35
66
  response = Rack::Response.new inform_response, 200, {'Connection' => 'Keep-Alive', 'Server' => 'ruby-cwmp'}
36
- response.set_cookie("sessiontrack", {:value => "294823094lskdfsfsdf", :path => "/", :expires => Time.now+24*60*60})
67
+ response.set_cookie("sessiontrack", {:value => ck, :path => "/", :expires => Time.now+24*60*60})
37
68
  response.finish
38
69
  elsif message_type == "TransferComplete"
39
70
  puts "got TransferComplete"
40
71
  else
41
- if message_type.include? "Response"
42
- puts "got #{message_type}"
43
- elsif len == 0
72
+ if len == 0
44
73
  puts "got Empty Post"
74
+ else
75
+ puts "got #{message_type}"
76
+ case message_type
77
+ when "GetParameterValuesResponse"
78
+ doc.css("ParameterValueStruct").each do |node|
79
+ puts "#{node.element_children[0].text}: #{node.element_children[1].text}"
80
+ end
81
+ when "Fault"
82
+ puts "#{doc.css("faultstring").text}: #{doc.css("FaultString").text}"
83
+ end
84
+ end
85
+
86
+ cookie = req.cookies['sessiontrack']
87
+ @acs.cpes.each do |k,c|
88
+ cpe = c if c.session_cookie = cookie
45
89
  end
46
90
 
47
91
  # Got Empty Post or a Response. Now check for any event to send, otherwise 204
48
- [204, {"Connection" => "Close", 'Server' => 'ruby-cwmp'}, ""]
92
+ if cpe.queue.size > 0
93
+ m = cpe.queue.pop
94
+ response = Rack::Response.new m, 200, {'Connection' => 'Keep-Alive', 'Server' => 'ruby-cwmp'}
95
+ response.finish
96
+ else
97
+ puts "sending 204"
98
+ [204, {"Connection" => "Close", 'Server' => 'ruby-cwmp'}, ""]
99
+ end
100
+
49
101
  end
50
102
  end
51
103
 
@@ -53,16 +105,20 @@ module Cwmp
53
105
 
54
106
 
55
107
  class Acs
108
+ attr_accessor :cpes
109
+
56
110
  def initialize (port)
111
+ ac = self
57
112
  @port = port
113
+ # @handler = Handler.new
58
114
  @app = HttpRouter.new do
59
115
  # add('/api').to(SocketApp.new)
60
- add('/acs').to(Handler.new)
116
+ add('/acs').to(Handler.new(ac))
61
117
  end
118
+ @cpes = {}
62
119
  end
63
120
 
64
121
  def start_cli
65
-
66
122
  list = [
67
123
  'GetParameterValues', 'SetParameterValues', 'Reboot', 'FactoryReset', 'Download', 'AddObject', 'DeleteObject',
68
124
  'help', 'quit', "waitMessage"
@@ -81,34 +137,29 @@ module Cwmp
81
137
  when "help"
82
138
  help
83
139
  when "list"
84
- puts "list"
140
+ p @cpes
141
+ when /^get (\w+) (.+)/
142
+ cpe = @cpes[$1]
143
+ cpe.queue << Cwmp::Message::get_parameter_values($2)
144
+ cpe.do_connection_request
145
+ when /reboot (\w+)/
146
+ cpe = @cpes[$1]
147
+ cpe.queue << Cwmp::Message::reboot
148
+ cpe.do_connection_request
85
149
  end
86
150
  end
87
151
  end
88
152
 
89
153
  def start
154
+ puts "ACS #{Cwmp::VERSION} by Luca Cervasio <luca.cervasio@gmail.com>"
155
+ puts "Daemon running on http://localhost:#{@port}/acs"
90
156
  @web = Thread.new do
91
157
  Thin::Logging.silent = true
92
158
  Rack::Handler::Thin.run @app, :Port => @port
93
159
  end
94
160
 
95
- Thread.new do
96
- while true do
97
- sleep 1
98
- Readline.clear_rl
99
- puts "i"
100
- Readline.restore
101
- end
102
- end
103
161
  start_cli
104
162
  end
105
163
  end
106
164
 
107
-
108
-
109
-
110
- end
111
-
112
-
113
-
114
-
165
+ end
@@ -10,8 +10,8 @@ module Cwmp
10
10
  @acs_url = acs_url
11
11
  @serial = h[:serial] || "23434ds"
12
12
  @oui = h[:oui] || "006754"
13
- @software_version = h[:software_version] || "0.1.1"
14
- @manufacturer = h[:manufacturer] || "Moonar"
13
+ @software_version = h[:software_version] || Cwmp::VERSION
14
+ @manufacturer = h[:manufacturer] || "ruby-cwmp"
15
15
  @state = 'off'
16
16
  @periodic = Thread.new { periodic periodic } if periodic > 0
17
17
  @conn_req = Thread.new { connection_request }
@@ -86,12 +86,14 @@ module Cwmp
86
86
  puts "sending Inform with event #{event}"
87
87
  resp = c.post @acs_url, Cwmp::Message::inform(@manufacturer, @oui, @serial, event, @software_version), {'User-Agent' => "ruby-cwmp #{Cwmp::VERSION}", "Content-Type" => 'text/xml; charset="utf-8"'}
88
88
  doc = Nokogiri::XML(resp.body)
89
- message_type = doc.css("soapenv|Body").children.map(&:name)[1]
89
+ resp.body =~ /(\w+):Envelope/
90
+ soap_ns = $1
91
+ message_type = doc.css("#{soap_ns}|Body").children.map(&:name)[1]
90
92
  puts "got #{message_type} message"
91
93
  resp = c.post @acs_url, "", {'User-Agent' => "ruby-cwmp #{Cwmp::VERSION}", "Content-Type" => 'text/xml; charset="utf-8"'}
92
94
  while resp.status != 204
93
95
  doc = Nokogiri::XML(resp.body)
94
- message_type = doc.css("soapenv|Body").children.map(&:name)[1]
96
+ message_type = doc.css("#{soap_ns}|Body").children.map(&:name)[1]
95
97
  puts "got #{message_type}"
96
98
  case message_type
97
99
  when "GetParameterValues"
@@ -76,6 +76,19 @@ module Cwmp
76
76
  return b.to_xml
77
77
  end
78
78
 
79
+ def self.reboot
80
+ b = Nokogiri::XML::Builder.new
81
+
82
+ b[:soap].Envelope(NAMESPACES) {
83
+ b[:soap].Header {}
84
+ b[:soap].Body {
85
+ b[:cwmp].Reboot() {}
86
+ }
87
+ }
88
+
89
+ return b.to_xml
90
+ end
91
+
79
92
  def self.get_parameter_values (leaves)
80
93
  b = Nokogiri::XML::Builder.new
81
94
 
@@ -1,3 +1,3 @@
1
1
  module Cwmp
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cwmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Cervasio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-14 00:00:00.000000000 Z
11
+ date: 2014-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -110,8 +110,8 @@ dependencies:
110
110
  - - ! '>='
111
111
  - !ruby/object:Gem::Version
112
112
  version: 2.4.0
113
- description: A ruby library to parse and generate CWMP messages. Includes an ACS server
114
- and a CPE simulator.
113
+ description: A ruby library for parsing and crafting CWMP messages. Includes an ACS
114
+ server and a CPE simulator.
115
115
  email: luca.cervasio@gmail.com
116
116
  executables:
117
117
  - acs