mqtt-sn-ruby 0.0.15 → 0.0.16

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: c5ba209ea7715436c359933ae41662a04868ad43
4
- data.tar.gz: c326fc2d3ac00c24bb8df4c4b0ed34cdf44bfe70
3
+ metadata.gz: ae49a1caf7d6b02dd65610dea876ed01b9db5d17
4
+ data.tar.gz: 38b27aa6a3fb222c9a3a04255d20e6c9f9539ed5
5
5
  SHA512:
6
- metadata.gz: fd5d904ad1a174fb9d46d53d8c1281583659efccd23cf8c153ce22579dd3d005de21470bf86b17b077bfd59bc9b2780cad8236685eb1b393399f97dfd7f12b33
7
- data.tar.gz: 01612dd211b3e2b2305433e29454d92c703cffe194b5ff7ed8264a773aab517048e8afbeb55a74d91091dd054fd844b6ea5163a37c2c187fa7cccfcf26efa3c8
6
+ metadata.gz: 4101dfd846691f436618bc3674560505659287080cf23464d13e6fd49af50e9773ee62f1d8ed0bc6cfe413ac2dd69dd56f1df5bdee3023fdd0f39eb1463348e3
7
+ data.tar.gz: 88e4fae98b1be7f2ee5219553ec8ac103e45b83ceda29e1a49bab28bfa6de9b3a87867cba08110bef62e2e7762b8a598657c66871ff5905a005fff21542a04d9
data/bin/mqtt-sn-sub.rb CHANGED
@@ -86,8 +86,10 @@ begin
86
86
  $sn.subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
87
87
  if s==:sub_ack
88
88
  puts "Subscribed Ok! Waiting for Messages!"
89
+ elsif s==:disconnect
90
+ puts "Disconnected -- switch to new gateway"
89
91
  else
90
- puts "Got Message: #{m}"
92
+ puts "Got Message: #{s}: #{m}"
91
93
  end
92
94
  end
93
95
  end
@@ -98,6 +100,7 @@ rescue SystemExit, Interrupt
98
100
  puts "\nExiting after Disconnect\n"
99
101
  rescue => e
100
102
  puts "\nError: '#{e}' -- Quit after Disconnect\n"
103
+ pp e.backtrace
101
104
  end
102
105
  $sn.disconnect if $sn
103
106
 
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -51,12 +51,18 @@ class MqttSN
51
51
  QOS2_FLAG =0x40
52
52
  QOS1_FLAG =0x20
53
53
  QOS0_FLAG =0x00
54
+ TOPIC_SHORT_FLAG =0x02
54
55
 
55
56
 
56
57
  def logger str,*args
57
58
  if @verbose or @debug
58
- @log_q << Time.now.iso8601+" "+sprintf(str,*args)
59
- end
59
+ s=sprintf(str,*args)
60
+ if not @forwarder
61
+ @log_q << sprintf("%s: (%3.3s) | %s",Time.now.iso8601,@active_gw_id,s)
62
+ else
63
+ @log_q << sprintf("%s: [%3.3s] | %s",Time.now.iso8601,@options[:gw_id],s)
64
+ end
65
+ end
60
66
  end
61
67
 
62
68
  def note str,*args
@@ -139,10 +145,10 @@ class MqttSN
139
145
  @will_topic=nil
140
146
  @will_msg=nil
141
147
  @active_gw_id=nil
142
- @id="?"
143
148
 
144
149
  @sem=Mutex.new
145
150
  @gsem=Mutex.new
151
+ @log_q = Queue.new #log queue :)
146
152
  @msg_id=1
147
153
  @clients={}
148
154
  @gateways={}
@@ -159,7 +165,6 @@ class MqttSN
159
165
  @autodiscovery=true
160
166
  end
161
167
 
162
- @log_q = Queue.new #log queue :)
163
168
 
164
169
  @log_t=Thread.new do
165
170
  log_thread
@@ -289,13 +294,14 @@ class MqttSN
289
294
  if type==:disconnect
290
295
  return #already disconnected.. nothing to do
291
296
  else
292
- raise "Error: Cannot #{type} while unconnected, send :connect first!"
297
+ note "Error: Cannot #{type} while unconnected, send :connect first!"
298
+ return nil
293
299
  end
294
300
  end
295
301
  case type
296
302
  when :connect
297
303
  if not hash[:id]
298
- hash[:id]="xxx"
304
+ hash[:id]="mqtt-sn-ruby-#{$$}"
299
305
  end
300
306
  flags=0
301
307
  flags+=CLEAN_FLAG if hash[:clean]
@@ -370,6 +376,7 @@ class MqttSN
370
376
  @msg_id+=1
371
377
 
372
378
  when :publish
379
+ puts "topic id: #{hash[:topic_id]}"
373
380
  raise "Need :topic_id to Publish!" if not hash[:topic_id]
374
381
  qos=hash[:qos]||0
375
382
  flags=0
@@ -379,6 +386,9 @@ class MqttSN
379
386
  else
380
387
  flags+=QOS1_FLAG*qos
381
388
  end
389
+ if hash[:topic_type]==:short
390
+ flags+=TOPIC_SHORT_FLAG
391
+ end
382
392
  p=[PUBLISH_TYPE,flags,hash[:topic_id] >>8 ,hash[:topic_id] & 0xff,@msg_id >>8 ,@msg_id & 0xff]
383
393
  hash[:msg].each_byte do |b|
384
394
  p<<b
@@ -409,7 +419,6 @@ class MqttSN
409
419
  else
410
420
  raw=send_packet_gw p
411
421
  end
412
- hash[:debug]=raw if @debug
413
422
  return
414
423
  end
415
424
  @sem.synchronize do #one command at a time --
@@ -453,6 +462,11 @@ class MqttSN
453
462
  #need to set DUP flag !
454
463
  end
455
464
  end
465
+ if status==:timeout
466
+ note "Warn: ack timeouted, assume disconnected"
467
+ @state=:disconnect
468
+ gateway_close :timeout
469
+ end
456
470
  end
457
471
  end #sem
458
472
  if block
@@ -491,11 +505,26 @@ class MqttSN
491
505
  def send_packet_gw m
492
506
  msg=MqttSN::build_packet m
493
507
  waits=0
494
- while not @active_gw_id or not @gateways[@active_gw_id] or not @gateways[@active_gw_id][:socket]
495
- sleep 0.1
496
- waits+=1
497
- break if waits>30
498
- end
508
+ debug={}
509
+ debug[:debug]=MqttSN::hexdump(msg) if @debug
510
+
511
+ if not @active_gw_id or not @gateways[@active_gw_id] or not @gateways[@active_gw_id][:socket]
512
+ print "No active gw, wait ."
513
+ while not @active_gw_id or not @gateways[@active_gw_id] or not @gateways[@active_gw_id][:socket]
514
+ ret="-"
515
+ if not ret=pick_new_gateway
516
+ sleep 0.5
517
+ print "."
518
+ end
519
+ waits+=1
520
+ if waits>30
521
+ puts "\nNone Found -- not sending"
522
+ return
523
+ end
524
+ end
525
+ puts " Ok!"
526
+ end
527
+
499
528
  if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket]
500
529
  #get server & port from uri!
501
530
  uri=URI.parse(@gateways[@active_gw_id][:uri])
@@ -503,7 +532,7 @@ class MqttSN
503
532
  MqttSN::send_raw_packet msg,@gateways[@active_gw_id][:socket],uri.host,uri.port
504
533
  _,port,_,_ = @gateways[@active_gw_id][:socket].addr
505
534
  src="udp://0.0.0.0:#{port}"
506
- logger "od %-24.24s <- %-24.24s | %s",@gateways[@active_gw_id][:uri],src,MqttSN::parse_message(msg).to_json
535
+ logger "od %-24.24s <- %-24.24s | %s",@gateways[@active_gw_id][:uri],src,MqttSN::parse_message(msg).merge(debug).to_json
507
536
  else
508
537
  puts "no gw to send.."
509
538
  sleep 1
@@ -596,7 +625,9 @@ class MqttSN
596
625
  send :subscribe, topic: topic, qos: hash[:qos],expect: :sub_ack do |s,m|
597
626
  if s==:ok
598
627
  if m[:topic_id] and m[:topic_id]>0 #when subs topic has no wild cards, we get topic id here:
599
- @topics[topic]=m[:topic_id]
628
+ if m[:topic_type]==:long
629
+ @topics[topic]=m[:topic_id]
630
+ end
600
631
  end
601
632
  end
602
633
  if block
@@ -609,6 +640,7 @@ class MqttSN
609
640
  sleep 0.1
610
641
  if @state!=:connected
611
642
  block.call :disconnect,{}
643
+ #gateway_close :subscribe_disconnected
612
644
  break
613
645
  end
614
646
  end
@@ -643,17 +675,24 @@ class MqttSN
643
675
  end
644
676
 
645
677
  def publish topic,msg,hash={}
646
- if not @topics[topic]
647
- register_topic topic
678
+ topic_type=:long
679
+ if topic.size!=2
680
+ if not @topics[topic]
681
+ register_topic topic
682
+ end
683
+ topic_id=@topics[topic]
684
+ else
685
+ topic_id=((topic[0].ord&0xff)<<8)+(topic[1].ord&0xff)
686
+ topic_type=:short
648
687
  end
649
688
  case hash[:qos]
650
689
  when 1
651
- send :publish,msg: msg, retain: hash[:retain], topic_id: @topics[topic], qos: 1, expect: [:publish_ack] do |s,m|
690
+ send :publish,msg: msg, retain: hash[:retain], topic_id: topic_id, topic_type: topic_type, qos: 1, expect: [:publish_ack] do |s,m|
652
691
  if s==:ok
653
692
  end
654
693
  end
655
694
  when 2
656
- send :publish,msg: msg, retain: hash[:retain], topic_id: @topics[topic], qos: 2, expect: [:pubrec] do |s,m|
695
+ send :publish,msg: msg, retain: hash[:retain], topic_id: topic_id, topic_type: topic_type, qos: 2, expect: [:pubrec] do |s,m|
657
696
  if s==:ok
658
697
  if m[:type]==:pubrec
659
698
  send :pubrel,msg_id: m[:msg_id], expect: :pubcomp do |s,m|
@@ -662,7 +701,7 @@ class MqttSN
662
701
  end
663
702
  end
664
703
  else
665
- send :publish,msg: msg, retain: hash[:retain],topic_id: @topics[topic], qos: 0
704
+ send :publish,msg: msg, retain: hash[:retain],topic_id: topic_id, topic_type: topic_type, qos: hash[:qos]||0
666
705
  end
667
706
  end
668
707
 
@@ -696,7 +735,7 @@ class MqttSN
696
735
  msg_id=(r[5].ord<<8)+r[6].ord
697
736
  m={type: :sub_ack, topic_id: topic_id, msg_id: msg_id, status: status}
698
737
  when SUBSCRIBE_TYPE
699
- msg_id=(r[5].ord<<8)+r[6].ord
738
+ msg_id=(r[3].ord<<8)+r[4].ord
700
739
  topic=r[5,len-5]
701
740
  m={type: :subscribe, flags: r[2].ord, topic: topic, msg_id: msg_id, status: :ok}
702
741
  when UNSUBACK_TYPE
@@ -707,8 +746,15 @@ class MqttSN
707
746
  msg_id=(r[5].ord<<8)+r[6].ord
708
747
  msg=r[7,len-7]
709
748
  flags=r[2].ord
749
+ topic_type=:long
750
+ topic=""
751
+ if flags&0x03==TOPIC_SHORT_FLAG
752
+ topic_type=:short
753
+ topic=r[3].chr+r[4].chr
754
+ end
710
755
  qos=(flags>>5)&0x03
711
- m={type: :publish, qos: qos, topic_id: topic_id,msg_id: msg_id, msg: msg,status: :ok}
756
+ qos=-1 if qos==3
757
+ m={type: :publish, qos: qos, topic_id: topic_id, topic_type:topic_type, topic: topic, msg_id: msg_id, msg: msg,status: :ok}
712
758
  when PUBREL_TYPE
713
759
  msg_id=(r[2].ord<<8)+r[3].ord
714
760
  m={type: :pub_rel, msg_id: msg_id, status: :ok}
@@ -779,8 +825,10 @@ class MqttSN
779
825
  done=true
780
826
  end
781
827
  when :publish
782
- m[:topic]=@topics.key(m[:topic_id])
783
-
828
+ if m[:topic_type]==:long
829
+ puts "on pitkä #{m[:topic_id]} ->#{@topics.key(m[:topic_id])}"
830
+ m[:topic]=@topics.key(m[:topic_id])
831
+ end
784
832
  if not @transfer
785
833
  @dataq<<m
786
834
  if m[:qos]==1
@@ -928,6 +976,7 @@ class MqttSN
928
976
  puts "Error: receive thread died: #{e}"
929
977
  pp e.backtrace
930
978
  end
979
+ return @active_gw_id
931
980
  end
932
981
 
933
982
  def roam_thread socket
@@ -955,7 +1004,7 @@ class MqttSN
955
1004
  while true do
956
1005
  if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket]
957
1006
  else # not so ok -- pick one!
958
- pick_new_gateway
1007
+ #pick_new_gateway
959
1008
  end
960
1009
  sleep 0.01
961
1010
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sn-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen