hamnet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hamnet.rb +135 -0
  2. metadata +46 -0
data/lib/hamnet.rb ADDED
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # Jeff Francis, N0GQ, jeff@gritch.org
5
+ # http://fldigi.gritch.org
6
+
7
+ require 'zlib'
8
+ require 'base64'
9
+ require 'time'
10
+ require 'thread'
11
+
12
+ FRAME_SIMPLE=0
13
+ FRAME_BASE64=1
14
+ FRAME_COMPRESSED_BASE64=2
15
+ # To do.
16
+ FRAME_PING=3
17
+ FRAME_PING_REPLY=4
18
+ FRAME_TELEMETRY=5
19
+ FRAME_POSITION=6
20
+
21
+ # This defines a basic frame of data for use with fldigi. The frame
22
+ # consists of:
23
+ #
24
+ # Header field. Three bytes, consisting of "<<<"
25
+ # From field. Eight bytes (allowing for "WA0AAA-0")
26
+ # To field. Eight bytes (allowing for "WB0BBB-0")
27
+ # Type field. One byte (numeric) specifying encoding, compression.
28
+ # Data field. Arbitrary number of bytes of payload.
29
+ # CRC field. Last eight bytes, CRC32 checksum of From, To, Type, Data fields.
30
+ # Trailer field. Three bytes, consisting of ">>>"
31
+ class Frame
32
+ attr_accessor :from, :to, :type, :valid
33
+
34
+ def initialize(from, to, type)
35
+ @from=from.downcase
36
+ @to=to.downcase
37
+ @type=type
38
+ @wiredata=nil
39
+ @userdata=nil
40
+ @crc=nil
41
+ @valid=nil
42
+
43
+ while @from.length<8
44
+ @from=@from+":"
45
+ end
46
+ while @to.length<8
47
+ @to=@to+":"
48
+ end
49
+ end
50
+
51
+ def to_s
52
+ return @wiredata
53
+ end
54
+ end
55
+
56
+ # This defines a frame to be transmitted using fldigi. You need to
57
+ # supply the originating call sign (from), the destination call sign
58
+ # (to), the type of frame (see constants above), and the actual data
59
+ # to be transmitted.
60
+ class TxFrame < Frame
61
+ attr_accessor :from, :to, :type, :userdata, :wiredata
62
+
63
+ def initialize(from, to, type, userdata)
64
+ # Create the object.
65
+ super(from, to, type)
66
+ @userdata=userdata
67
+
68
+ # Do the needful with the payload.
69
+ case @type
70
+ when FRAME_SIMPLE
71
+ message=@from+@to+@type.to_s+@userdata
72
+ @crc=Zlib::crc32(message).to_s(16).downcase
73
+ when FRAME_BASE64
74
+ message=@from+@to+@type.to_s+Base64::strict_encode64(@userdata)
75
+ @crc=Zlib::crc32(message).to_s(16).downcase
76
+ when FRAME_COMPRESSED_BASE64
77
+ message=@from+@to+@type.to_s+Base64::strict_encode64(Zlib::Deflate.deflate(@userdata,Zlib::BEST_COMPRESSION))
78
+ @crc=Zlib::crc32(message).to_s(16).downcase
79
+ else
80
+ return false
81
+ end
82
+
83
+ # Set the rest of the fields, and done.
84
+ @wiredata="<<<#{message}#{@crc}>>>"
85
+ @valid=true
86
+ end
87
+ end
88
+
89
+ # This defines a frame received using fldigi. You supply it with the
90
+ # frame (delimited with "<<<" and ">>>", and it validates the frame,
91
+ # then creates and populates an object with the fields of that frame.
92
+ class RxFrame < Frame
93
+ attr_accessor :from, :to, :type, :userdata, :wiredata
94
+
95
+ def initialize(wiredata)
96
+ # First, make sure it's properly delimited.
97
+ if wiredata=~/^<<<.*>>>$/ and wiredata.length>=31
98
+ # Remove the "<<<" and ">>>"
99
+ tmp=wiredata[3,wiredata.length-6]
100
+ # Extract the crc field.
101
+ crc=tmp[tmp.length-8,8]
102
+ tmp=tmp[0,tmp.length-8]
103
+ # Save this for checking CRC later.
104
+ crcstring=tmp
105
+ # Extract and clean the from field.
106
+ from=tmp[0,8].gsub(":","")
107
+ tmp=tmp[8,tmp.length-8]
108
+ # Extract and clean the to field.
109
+ to=tmp[0,8].gsub(":","")
110
+ tmp=tmp[8,tmp.length-8]
111
+ # Extract the frame type.
112
+ type=tmp[0,1].to_i
113
+ tmp=tmp[1,tmp.length-1]
114
+ # Create and populate the object.
115
+ super(from, to, type)
116
+ @wiredata=wiredata
117
+ # Decode the payload.
118
+ case @type
119
+ when FRAME_SIMPLE
120
+ @userdata=tmp
121
+ when FRAME_BASE64
122
+ @userdata=Base64::strict_decode64(tmp)
123
+ when FRAME_COMPRESSED_BASE64
124
+ @userdata=Zlib::Inflate.inflate(Base64::strict_decode64(tmp))
125
+ end
126
+ # Calculate the check the CRC.
127
+ @crc=Zlib::crc32(crcstring).to_s(16).downcase
128
+ if crc==@crc
129
+ @valid=true
130
+ end
131
+ else
132
+ return false
133
+ end
134
+ end
135
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamnet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Francis, N0GQ
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A library for constructing fldigi network packets.
15
+ email: jeff@gritch.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hamnet.rb
21
+ homepage: http://fldigi.gritch.org/
22
+ licenses:
23
+ - MIT
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: A library for constructing fldigi network packets.
46
+ test_files: []