mqtt-sn-ruby 0.0.6 → 0.0.7

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e34953b26fefce4f5e92f3d3b5ce9761fedac071
4
- data.tar.gz: 97786a47fa00c49c902edc99fc30072aa00fbc03
3
+ metadata.gz: 4ab4b9eba338888a0b00c99ba73f7427f40b826c
4
+ data.tar.gz: f3c2bffc82613335aac13286c1369c3e5200b8be
5
5
  SHA512:
6
- metadata.gz: 4e459aca2a8410edadf97935beec968a7fc59c9a9ee0275b05122a71d2c3ffbbf3e5db328528d1ce43c530d85df1f7b3777ee9051f85fae0fb83d8e52a595180
7
- data.tar.gz: 934ebee4d63c118429b5b257d2698efd36761eb7a6cff7675b4b360bfd8a8b36e3e241b4ceae8b16bf8bb2df87faa8e30ed97d8119d89bf77b2c5adba31fc944
6
+ metadata.gz: 22e80e19b73ae5aabe5b95eec0dba90a0cdbab42120b24f1f63aa4127a2a029be3c23829cae734feac56ebba8c6d00ec1ce0883811e82dac956328ec46a9315a
7
+ data.tar.gz: d955340f857b4a7cd121a922f4a8182327688f92b0be9efd8248d63c21348de3a38f5359d9829484769a16325064d909f794b597c6609bf93a98e4efaef4d56c
@@ -43,6 +43,7 @@ rescue SystemExit, Interrupt
43
43
  puts "\nExiting after Disconnect\n"
44
44
  rescue => e
45
45
  puts "\nError: '#{e}' -- Quit after Disconnect\n"
46
+ pp e.backtrace
46
47
  end
47
48
 
48
49
  puts "MQTT-SN-FORWARDER Done."
data/bin/mqtt-sn-pub.rb CHANGED
@@ -46,6 +46,9 @@ puts "MQTT-SN-PUB: #{options.to_json}"
46
46
  begin
47
47
  sn=MqttSN.new options
48
48
  sn.connect options[:id]
49
+ sn.send :searchgw, expect: :gwinfo do |status,message|
50
+ puts "got gwinfo: #{status}: #{message}"
51
+ end
49
52
  sn.publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
50
53
  puts "Sent ok."
51
54
  rescue SystemExit, Interrupt
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -10,6 +10,8 @@ class MqttSN
10
10
  Nretry = 5 # Max retry
11
11
  Tretry = 10 # Timeout before retry
12
12
 
13
+ SEARCHGW_TYPE =0x01
14
+ GWINFO_TYPE =0x02
13
15
  CONNECT_TYPE =0x04
14
16
  CONNACK_TYPE =0x05
15
17
  WILLTOPICREQ_TYPE=0x06
@@ -66,9 +68,7 @@ class MqttSN
66
68
  end
67
69
  end
68
70
 
69
-
70
-
71
- def hexdump data
71
+ def self.hexdump data
72
72
  raw=""
73
73
  data.each_byte do |b|
74
74
  raw=raw+"," if raw!=""
@@ -77,7 +77,7 @@ class MqttSN
77
77
  raw
78
78
  end
79
79
 
80
- def send_packet m
80
+ def self.build_packet m
81
81
  msg=" "
82
82
  len=1
83
83
  m.each_with_index do |b,i|
@@ -85,16 +85,13 @@ class MqttSN
85
85
  len+=1
86
86
  end
87
87
  msg[0]=len.chr
88
- @s.send(msg, 0, @server, @port)
89
- hexdump msg
88
+ msg
90
89
  end
91
90
 
92
- def raw_send_packet msg
93
- @s.send(msg, 0, @server, @port)
94
- end
91
+
95
92
 
96
93
  def send type,hash={},&block
97
- puts "" if @verbose
94
+ #puts "" if @verbose
98
95
  if @state!=:connected and type!=:connect and type!=:will_topic and type!=:will_msg
99
96
  if type==:disconnect
100
97
  return #already disconnected.. nothing to do
@@ -199,6 +196,8 @@ class MqttSN
199
196
  p=[PUBREL_TYPE,hash[:msg_id] >>8 ,hash[:msg_id] & 0xff]
200
197
  when :ping
201
198
  p=[PINGREQ_TYPE]
199
+ when :searchgw
200
+ p=[SEARCHGW_TYPE,0]
202
201
  when :disconnect
203
202
  if hash[:duration]
204
203
  p=[DISCONNECT_TYPE,hash[:duration] >>8 ,hash[:duration] & 0xff]
@@ -221,7 +220,7 @@ class MqttSN
221
220
  end
222
221
  raw=send_packet p
223
222
  hash[:debug]=raw if @debug
224
- puts "send:#{@id} #{type},#{hash.to_json}" if @verbose
223
+ #puts "send:#{@id} #{type},#{hash.to_json}" if @verbose
225
224
  timeout=hash[:timeout]||Tretry
226
225
  retries=0
227
226
  if hash[:expect]
@@ -256,6 +255,41 @@ class MqttSN
256
255
 
257
256
  end
258
257
 
258
+ def self.send_raw_packet msg,socket,server,port
259
+ socket.send(msg, 0, server, port)
260
+ MqttSN::hexdump msg
261
+ end
262
+
263
+ def self.send_packet m,socket,server,port
264
+ msg=MqttSN::build_packet m
265
+ MqttSN::send_raw_packet msg,socket,server,port
266
+ dest="#{server}:#{port}"
267
+ _,port,_,_ = socket.addr
268
+ src=":#{port}"
269
+ printf "< %-18.18s <- %-18.18s | %s\n",dest,src,parse_message(msg).to_json
270
+
271
+ end
272
+
273
+
274
+ def send_packet m
275
+ MqttSN::send_packet m,@s,@server,@port
276
+ end
277
+
278
+ def self.poll_packet socket
279
+ #decide how to get data -- UDP-socket or FM-radio
280
+ begin
281
+ r,stuff=socket.recvfrom_nonblock(200) #get_packet --high level func!
282
+ client_ip=stuff[2]
283
+ client_port=stuff[1]
284
+ return [r,client_ip,client_port]
285
+ rescue IO::WaitReadable
286
+ rescue => e
287
+ puts "Error: receive thread died: #{e}"
288
+ pp e.backtrace
289
+ end
290
+ return nil
291
+ end
292
+
259
293
  def self.forwarder hash={}
260
294
  @options=hash #save these
261
295
  @server=hash[:server]||"127.0.0.1"
@@ -268,35 +302,46 @@ class MqttSN
268
302
  clients={}
269
303
  begin
270
304
  while true
271
- begin
272
- r,stuff=socket.recvfrom_nonblock(200)
273
- puts "got packet:"
274
- client_ip=stuff[2]
275
- client_port=stuff[1]
305
+ #periodically broadcast :advertize
306
+ if pac=poll_packet(socket)
307
+ r,client_ip,client_port=pac
276
308
  key="#{client_ip}:#{client_port}"
277
309
  if not clients[key]
278
310
  clients[key]={ip:client_ip, port:client_port, socket: UDPSocket.new }
311
+ dest="#{client_ip}:#{client_port}"
312
+ printf "+ %s\n",dest
279
313
  end
280
- clients[key][:socket].send(r, 0, @server, @port)
281
314
  m=MqttSN::parse_message r
282
- puts ">#{m.to_json}"
283
- rescue IO::WaitReadable
284
- rescue => e
285
- puts "Error: receive thread died: #{e}"
286
- pp e.backtrace
315
+ done=false
316
+ case m[:type]
317
+ when :searchgw
318
+ #do it here! and reply with :gwinfo
319
+ dest="#{client_ip}:#{client_port}"
320
+ printf "C %-18.18s -> %-18.18s | %s\n",key,dest,m.to_json
321
+ MqttSN::send_packet [GWINFO_TYPE,0xAB],socket,client_ip,client_port
322
+ done=true
323
+ end
324
+ if not done # not done locally -> forward it
325
+ sbytes=clients[key][:socket].send(r, 0, @server, @port) # to rsmb -- ok as is
326
+ _,port,_,_ = clients[key][:socket].addr
327
+ dest="#{@server}:#{port}"
328
+ printf "C %-18.18s -> %-18.18s | %s\n",key,dest,m.to_json
329
+ end
287
330
  end
288
331
  clients.each do |key,c|
289
- begin
290
- r,stuff=c[:socket].recvfrom_nonblock(200)
291
- puts "got packet from server to client #{key}:"
292
- puts "sending to #{c[:ip]}:#{c[:port]}"
293
- socket.send(r, 0, c[:ip], c[:port])
332
+ if pac=poll_packet(c[:socket])
333
+ r,client_ip,client_port=pac
334
+ #puts "got packet #{pac} from server to client #{key}:"
335
+ #puts "sending to #{c[:ip]}:#{c[:port]}"
336
+ socket.send(r, 0, c[:ip], c[:port]) # send_packet
294
337
  m=MqttSN::parse_message r
295
- puts "<#{m.to_json}"
296
- rescue IO::WaitReadable
297
- rescue => e
298
- puts "Error: receive thread died: #{e}"
299
- pp e.backtrace
338
+ _,port,_,_ = clients[key][:socket].addr
339
+ dest="#{@server}:#{port}"
340
+ printf "S %-18.18s <- %-18.18s | %s\n",key,dest,m.to_json
341
+ case m[:type]
342
+ when :disconnect
343
+ puts "disco -- kill me!"
344
+ end
300
345
  end
301
346
  end
302
347
  sleep 0.01
@@ -448,12 +493,20 @@ class MqttSN
448
493
  type_byte=r[1].ord
449
494
  done=false
450
495
  case type_byte
496
+ when CONNECT_TYPE
497
+ duration=(r[4].ord<<8)+r[5].ord
498
+ id=r[6,len-6]
499
+ m={type: :connect, flags: r[2].ord, duration: duration, client_id: id, status: :ok}
451
500
  when CONNACK_TYPE
452
501
  m={type: :connect_ack,status: status}
453
502
  when SUBACK_TYPE
454
503
  topic_id=(r[3].ord<<8)+r[4].ord
455
504
  msg_id=(r[5].ord<<8)+r[6].ord
456
505
  m={type: :sub_ack, topic_id: topic_id, msg_id: msg_id, status: status}
506
+ when SUBSCRIBE_TYPE
507
+ msg_id=(r[5].ord<<8)+r[6].ord
508
+ topic=r[5,len-5]
509
+ m={type: :subscribe, flags: r[2].ord, topic: topic, msg_id: msg_id, status: :ok}
457
510
  when UNSUBACK_TYPE
458
511
  msg_id=(r[2].ord<<8)+r[3].ord
459
512
  m={type: :unsub_ack, msg_id: msg_id, status: :ok}
@@ -498,6 +551,11 @@ class MqttSN
498
551
  when WILLMSGRESP_TYPE
499
552
  m={type: :will_msg_resp, status: :ok}
500
553
 
554
+ when SEARCHGW_TYPE
555
+ m={type: :searchgw, radius: r[2].ord, status: :ok}
556
+ when GWINFO_TYPE
557
+ m={type: :gwinfo, gw_id: r[2].ord, status: :ok}
558
+
501
559
  when PINGRESP_TYPE
502
560
  m={type: :pong, status: :ok}
503
561
  else
@@ -506,11 +564,7 @@ class MqttSN
506
564
  m
507
565
  end
508
566
 
509
- def process_message r
510
- m=MqttSN::parse_message r
511
- if @debug and m
512
- m[:debug]=hexdump r
513
- end
567
+ def process_message m
514
568
  done=false
515
569
  case m[:type]
516
570
  when :register
@@ -541,9 +595,8 @@ class MqttSN
541
595
  when :connect_ack
542
596
  @state=:connected
543
597
  end
544
- puts "got :#{@id} #{m.to_json}" if @verbose
598
+ # puts "got :#{@id} #{m.to_json}" if @verbose
545
599
  if not done
546
- puts "pushed to q"
547
600
  @iq<<m if m
548
601
  end
549
602
  m
@@ -551,12 +604,20 @@ class MqttSN
551
604
 
552
605
  def recv_thread
553
606
  while true do
554
- begin
555
- r,stuff=@s.recvfrom(200) #_nonblock(200)
556
- process_message r
557
- rescue IO::WaitReadable
558
- IO.select([@s])
559
- retry
607
+ begin
608
+ if pac=MqttSN::poll_packet(@s)
609
+ r,client_ip,client_port=pac
610
+ m=MqttSN::parse_message r
611
+ if @debug and m
612
+ m[:debug]=MqttSN::hexdump r
613
+ end
614
+ dest="#{client_ip}:#{client_port}"
615
+ _,port,_,_ = @s.addr
616
+ src=port
617
+ printf "> %-18.18s <- %-18.18s | %s\n",dest,":#{port}",m.to_json
618
+ process_message m
619
+
620
+ end
560
621
  rescue => e
561
622
  puts "Error: receive thread died: #{e}"
562
623
  pp e.backtrace
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sn-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby toolkit for MQTT-SN, compatible with RSMB, command line tools and
14
14
  API