mqtt-sn-ruby 0.1.0 → 0.1.1

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: ccc99a508bb40d3261b690b13a97094bb257210b
4
- data.tar.gz: 04af70b7e0a86a63044631c9e6daa41c273509c5
3
+ metadata.gz: e4f084a51dc0bd607b1900db2538237006d67575
4
+ data.tar.gz: c5e15cf5dbebf2fd20dc952673d2e79c21902d02
5
5
  SHA512:
6
- metadata.gz: 6d1cc0c33d1becf9986eeffe185352436a4e8ebbb4b81c9ddaa0cab5a9433f92f661e890d570962bbbbf44a3f8f145bb81992078bbc38c96d06d53d2dd60e354
7
- data.tar.gz: 5d385953fa575f7537634a5c15b3d1267abac839514191be53a00089b5f113a397ee62959e13f2f55bd30d1fba07bdf5e3859ca01ec57c0c59740e7725010566
6
+ metadata.gz: 511d406d08eafc28272637f9c7418f2296864519f2036d04a76133e58feb00e49609331e0ad855ec1c7ecb6e9781bc00e6dd8661a1dc94fdbb0b3a3c9bc88cca
7
+ data.tar.gz: 90c07411e0b78c7baccc7f68b4a7b4ed735155e7de943d88cf567b3eaa1b22d2f86ff21714468b5e52d570f23bf56f6b540166a74f10568207bd0ceb8d677232
data/bin/mqtt-sn-pub.rb CHANGED
@@ -28,7 +28,7 @@ OptionParser.new do |opts|
28
28
  opts.on("-q", "--qos level", "QoS level (0). When using QoS -1, you must provide either Short Topic (2-char) or Topic_Id") do |v|
29
29
  options[:qos] = v.to_i
30
30
  end
31
- opts.on("-i", "--id id", "This client's id -- free choice (hostname-pid)") do |name|
31
+ opts.on("-i", "--id id", "This client's id -- free choice (mqtt-sn-ruby-pid)") do |name|
32
32
  options[:id] = name
33
33
  end
34
34
  opts.on("-m", "--msg msg", "Message to send (test_value)") do |msg|
@@ -40,8 +40,8 @@ OptionParser.new do |opts|
40
40
 
41
41
  end.parse!
42
42
 
43
- puts "MQTT-SN-PUB: #{options.to_json}"
44
43
  sn=MqttSN.new options
44
+ sn.note "MQTT-SN-PUB: #{options.to_json}"
45
45
  begin
46
46
  sn.pub options
47
47
  rescue SystemExit, Interrupt
data/c/mqtt-sn-pub.c ADDED
@@ -0,0 +1,36 @@
1
+ /* Minimal MQTT-SN Send QoS=-1 */
2
+
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+ #include <stdlib.h>
6
+ #include "mqtt-sn.h"
7
+
8
+ /*
9
+ MQTT-SN minimal c-client-library:
10
+ Copyright (C) 2014 Ari Siitonen
11
+
12
+ This program is free software: you can redistribute it and/or modify
13
+ it under the terms of the GNU General Public License as published by
14
+ the Free Software Foundation, either version 3 of the License, or
15
+ (at your option) any later version.
16
+
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
24
+ */
25
+
26
+ int main(int argc, char**argv)
27
+ {
28
+ if (argc != 5)
29
+ {
30
+ printf("usage: pub Ip Port Topic Msg \n");
31
+ exit(1);
32
+ }
33
+ MqttSN_open_socket(argv[1],atoi(argv[2]));
34
+ MqttSN_pub(argv[3],argv[4]);
35
+ printf("Published '%s' with topic '%s' to mqtt-sn server '%s:%d'\n",argv[4],argv[3],argv[1],atoi(argv[2]));
36
+ }
data/c/mqtt-sn.c ADDED
@@ -0,0 +1,40 @@
1
+ /* Minimal MQTT-SN Send QoS=-1 */
2
+
3
+ #include <sys/socket.h>
4
+ #include <netinet/in.h>
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdlib.h>
8
+
9
+ int MqttSN_sockfd;
10
+ struct sockaddr_in MqttSN_server;
11
+ #define MAX_MSG_LEN 64
12
+
13
+ int MqttSN_open_socket(char *host, int port) {
14
+ MqttSN_sockfd=socket(AF_INET,SOCK_DGRAM,0);
15
+ bzero(&MqttSN_server,sizeof(MqttSN_server));
16
+ MqttSN_server.sin_family = AF_INET;
17
+ MqttSN_server.sin_addr.s_addr=inet_addr(host);
18
+ MqttSN_server.sin_port=htons(port);
19
+ }
20
+
21
+ int MqttSN_pub(char *topic, char *msg) {
22
+ unsigned char buf[MAX_MSG_LEN];
23
+ if (strlen(msg)+7>MAX_MSG_LEN) {
24
+ printf("Error: too long message!\n");
25
+ exit(-1);
26
+ }
27
+ if (strlen(topic)!=2) {
28
+ printf("Error: Topic must be two characters!\n");
29
+ exit(-1);
30
+ }
31
+ buf[0]=strlen(msg)+7; //len
32
+ buf[1]=0x0c; // msg type= publish
33
+ buf[2]=0x62; // flags
34
+ buf[3]=topic[0]; // topic , 2 char
35
+ buf[4]=topic[1];
36
+ buf[5]=0x00;
37
+ buf[6]=0x01;
38
+ strcpy(&buf[7],msg);
39
+ sendto(MqttSN_sockfd,buf,buf[0],0,(struct sockaddr *)&MqttSN_server,sizeof(MqttSN_server));
40
+ }
data/c/mqtt-sn.h ADDED
@@ -0,0 +1,3 @@
1
+
2
+ int MqttSN_open_socket(char *host, int port);
3
+ int MqttSN_pub(char *topic, char *msg);
data/examples/recv.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'mqtt-sn-ruby'
4
4
 
5
5
  begin
6
- sn=MqttSN.new server_uri: "udp://mqtt.fi:1882"
6
+ sn=MqttSN.new server_uri: "udp://mqtt.fi:1882", verbose: true
7
7
  sn.sub do |status,msg|
8
8
  sn.note "Got Message '#{msg[:msg]}' with Topic '#{msg[:topic]}'"
9
9
  end
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -128,11 +128,9 @@ class MqttSN
128
128
  s
129
129
  end
130
130
 
131
-
132
131
  attr_accessor :clients
133
132
  attr_accessor :gateways
134
133
 
135
-
136
134
  def initialize(hash={})
137
135
  @options=hash #save these
138
136
  @server_uri=hash[:server_uri]
@@ -156,14 +154,14 @@ class MqttSN
156
154
  @broadcast_uri=hash[:broadcast_uri]
157
155
 
158
156
  if @server_uri
159
- puts "Using Default Gateway: #{@server_uri}"
157
+ note "Using Default Gateway: #{@server_uri}"
160
158
  @gateways[0]={stamp: Time.now.to_i,uri: @server_uri, duration: 0, source: 'default', status: :ok}
161
159
  pick_new_gateway
162
160
  elsif @broadcast_uri
163
- puts "Autodiscovery Active, using #{@broadcast_uri}"
161
+ note "Autodiscovery Active, using #{@broadcast_uri}"
164
162
  @autodiscovery=true
165
163
  else
166
- puts "No autodiscovery and no Default Gateway -- cannot proceed"
164
+ note "No autodiscovery and no Default Gateway -- cannot proceed"
167
165
  exit -1
168
166
  end
169
167
 
@@ -186,7 +184,7 @@ class MqttSN
186
184
  end
187
185
  if @forwarder
188
186
  @s,@server,@port = MqttSN::open_port @server_uri
189
- puts "Open port to Gateway: #{@server_uri}: #{@server},#{@port} -- Listening local port: #{@local_port}"
187
+ note "Open port to Gateway: #{@server_uri}: #{@server},#{@port} -- Listening local port: #{@local_port}"
190
188
  @local_port=hash[:local_port]||1883
191
189
  @s.bind("0.0.0.0",@local_port)
192
190
  @bcast_period=60
@@ -311,9 +309,10 @@ class MqttSN
311
309
  end
312
310
  case type
313
311
  when :connect
314
- if not hash[:id]
312
+ if not hash[:id] or hash[:id]=""
315
313
  hash[:id]="mqtt-sn-ruby-#{$$}"
316
314
  end
315
+ note "Connecting as '#{hash[:id]}'"
317
316
  flags=0
318
317
  flags+=CLEAN_FLAG if hash[:clean]
319
318
  flags+=RETAIN_FLAG if hash[:retain]
@@ -1048,7 +1047,6 @@ class MqttSN
1048
1047
 
1049
1048
  #toplevel funcs:
1050
1049
  def pub options={}
1051
- pp options
1052
1050
  sent=false
1053
1051
  if options[:qos]==-1
1054
1052
  publish options[:topic]||"XX", options[:msg]||"test_value", qos: options[:qos]
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen
@@ -23,6 +23,9 @@ files:
23
23
  - bin/mqtt-sn-forwarder.rb
24
24
  - bin/mqtt-sn-pub.rb
25
25
  - bin/mqtt-sn-sub.rb
26
+ - c/mqtt-sn-pub.c
27
+ - c/mqtt-sn.c
28
+ - c/mqtt-sn.h
26
29
  - examples/recv.rb
27
30
  - examples/send.rb
28
31
  - lib/mqtt-sn-ruby.rb