Class: ScanBeacon::BeaconParser

Inherits:
Object
  • Object
show all
Defined in:
lib/scan_beacon/beacon_parser.rb

Constant Summary collapse

DEFAULT_LAYOUTS =
{
  altbeacon: "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25",
  eddystone_uid: "s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19;d:20-21"
}
AD_TYPE_MFG =
0xff
AD_TYPE_SERVICE =
0x03
BT_EIR_SERVICE_DATA =
"\x16".force_encoding("ASCII-8BIT")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beacon_type, layout) ⇒ BeaconParser



16
17
18
19
20
21
22
23
24
25
# File 'lib/scan_beacon/beacon_parser.rb', line 16

def initialize(beacon_type, layout)
  @beacon_type = beacon_type
  if layout.include?("s")
    @ad_type = AD_TYPE_SERVICE
  else
    @ad_type = AD_TYPE_MFG
  end
  @layout = layout
  parse_layout
end

Instance Attribute Details

#beacon_typeObject

Returns the value of attribute beacon_type.



10
11
12
# File 'lib/scan_beacon/beacon_parser.rb', line 10

def beacon_type
  @beacon_type
end

Class Method Details

.default_parsersObject



12
13
14
# File 'lib/scan_beacon/beacon_parser.rb', line 12

def self.default_parsers
  DEFAULT_LAYOUTS.map {|name, layout| BeaconParser.new name, layout }
end

Instance Method Details

#generate_ad(beacon) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/scan_beacon/beacon_parser.rb', line 100

def generate_ad(beacon)
  length = [@matchers, @ids, @power, @data_fields].flatten.map {|elem| elem[:end] }.max + 1
  ad = ("\x00" * length).force_encoding("ASCII-8BIT")
  @matchers.each do |matcher|
    ad[matcher[:start]..matcher[:end]] = matcher[:expected]
  end
  @ids.each_with_index do |id, index|
    id_bytes = Beacon::Field.field_with_length(beacon.ids[index], id[:length]).bytes
    ad[id[:start]..id[:end]] = id_bytes
  end
  @data_fields.each_with_index do |field, index|
    unless beacon.data[index].nil?
      field_bytes = Beacon::Field.field_with_length(beacon.data[index], field[:length]).bytes
      ad[field[:start]..field[:end]] = field_bytes
    end
  end
  ad[@power[:start]..@power[:end]] = [beacon.power].pack('c')
  if @ad_type == AD_TYPE_SERVICE
    "\x03\x03".force_encoding("ASCII-8BIT") + [beacon.service_uuid].pack("S<") + [length+1].pack('C') + BT_EIR_SERVICE_DATA + ad
  elsif @ad_type == AD_TYPE_MFG
    ad[0..1] = [beacon.mfg_id].pack("S<")
    [length+1].pack('C') + [AD_TYPE_MFG].pack('C') +  ad
  end
end

#inspectObject



125
126
127
# File 'lib/scan_beacon/beacon_parser.rb', line 125

def inspect
  "<BeaconParser type=\"#{@beacon_type}\", layout=\"#{@layout}\">"
end

#matches?(data) ⇒ Boolean



58
59
60
61
62
63
# File 'lib/scan_beacon/beacon_parser.rb', line 58

def matches?(data)
  @matchers.each do |matcher|
    return false unless data[matcher[:start]..matcher[:end]] == matcher[:expected]
  end
  return true
end

#parse(data, ad_type = AD_TYPE_MFG) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/scan_beacon/beacon_parser.rb', line 65

def parse(data, ad_type = AD_TYPE_MFG)
  return nil if ad_type != @ad_type || !matches?(data)
  if @ad_type == AD_TYPE_MFG
    Beacon.new(ids: parse_ids(data), power: parse_power(data), beacon_type: @beacon_type,
      data: parse_data_fields(data), mfg_id: parse_mfg_or_service_id(data))
  else
    Beacon.new(ids: parse_ids(data), power: parse_power(data), beacon_type: @beacon_type,
      data: parse_data_fields(data), service_uuid: parse_mfg_or_service_id(data))
  end
end

#parse_data_fields(data) ⇒ Object



80
81
82
# File 'lib/scan_beacon/beacon_parser.rb', line 80

def parse_data_fields(data)
  parse_elems(@data_fields, data)
end

#parse_elems(elems, data) ⇒ Object



84
85
86
87
88
89
# File 'lib/scan_beacon/beacon_parser.rb', line 84

def parse_elems(elems, data)
  elems.map {|elem|
    elem_str = data[elem[:start]..elem[:end]]
    Beacon::Field.new(bytes: elem_str)
  }
end

#parse_ids(data) ⇒ Object



76
77
78
# File 'lib/scan_beacon/beacon_parser.rb', line 76

def parse_ids(data)
  parse_elems(@ids, data)
end

#parse_layoutObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scan_beacon/beacon_parser.rb', line 27

def parse_layout
  @matchers = []
  @ids = []
  @data_fields = []
  @power = nil
  @layout.split(",").each do |field|
    field_type, range_start, range_end, expected = field.split(/:|=|-/)
    field_params = {
      start: range_start.to_i,
      end: range_end.to_i,
      length: range_end.to_i - range_start.to_i + 1,
    }
    field_params[:expected] = [expected].pack("H*") unless expected.nil?
    case field_type
    when 'm'
      @matchers << field_params
    when 's'
      # swap byte order of service uuid
      expected = field_params[:expected]
      field_params[:expected] = expected[1] + expected[0]
      @matchers << field_params
    when 'i'
      @ids << field_params
    when 'd'
      @data_fields << field_params
    when 'p'
      @power = field_params
    end
  end
end

#parse_mfg_or_service_id(data) ⇒ Object



91
92
93
# File 'lib/scan_beacon/beacon_parser.rb', line 91

def parse_mfg_or_service_id(data)
  data[0..1].unpack('S<')[0]
end

#parse_power(data) ⇒ Object



95
96
97
98
# File 'lib/scan_beacon/beacon_parser.rb', line 95

def parse_power(data)
  return nil if @power.nil?
  data[@power[:start]..@power[:end]].unpack('c')[0]
end