ble 0.0.1 → 0.0.2

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.
@@ -0,0 +1,128 @@
1
+ module BLE
2
+ # Build information about {https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx Bluetooth Services}
3
+ #
4
+ # To add a new service description:
5
+ # BLE::Service.add '63a83b9c-0fa0-4d04-8ef9-23be4ed36231',
6
+ # name: 'World domination',
7
+ # type: 'net.cortex-minus.service.world_domination'
8
+ #
9
+ # Returned service description will be a hash:
10
+ # {
11
+ # name: "Bluetooth service name",
12
+ # type: "org.bluetooth.service.name",
13
+ # uuid: "128bit-uuid-string"
14
+ # }
15
+ #
16
+ module Service
17
+ # Notify of service not found
18
+ class NotFound < BLE::NotFound
19
+ end
20
+
21
+ private
22
+ DB_UUID = {}
23
+ DB_TYPE = {}
24
+ DB_NICKNAME = {}
25
+
26
+ public
27
+ # Get a service description from it's id.
28
+ # The id could be either a uuid, a type, or a nickname
29
+ #
30
+ # @param id [Symbol,String] uuid, type or nickname
31
+ # @return [Hash] service description
32
+ def self.[](id)
33
+ case id
34
+ when Symbol then DB_NICKNAME[id]
35
+ when UUID::REGEX then DB_UUID[id]
36
+ when String then DB_TYPE[id]
37
+ else raise ArgumentError, "invalid type for service id"
38
+ end
39
+ end
40
+
41
+ # Get service description from nickname.
42
+ #
43
+ # @param id [Symbol] nickname
44
+ # @return [Hash] service description
45
+ def self.by_nickname(id)
46
+ DB_NICKNAME[id]
47
+ end
48
+
49
+ # Get service description from uuid.
50
+ #
51
+ # @param id [String] uuid
52
+ # @return [Hash] service description
53
+ def self.by_uuid(id)
54
+ DB_UUID[id]
55
+ end
56
+
57
+ # Get service description from type
58
+ #
59
+ # @param id [Strig] type
60
+ # @return [Hash] service description
61
+ def self.by_type(id)
62
+ DB_TYPE[id]
63
+ end
64
+
65
+ # Add a service description.
66
+ # @example Add a service description with a 16-bit uuid
67
+ # module Service
68
+ # add 0x1800,
69
+ # name: 'Generic Access',
70
+ # type: 'org.bluetooth.service.generic_access'
71
+ # end
72
+ #
73
+ # @example Add a service description with a 128-bit uuid
74
+ # module Service
75
+ # add '63a83b9c-0fa0-4d04-8ef9-23be4ed36231',
76
+ # name: 'World domination',
77
+ # type: 'net.cortex-minus.service.world_domination'
78
+ # end
79
+ #
80
+ # @param uuid [Integer, String] 16-bit, 32-bit or 128-bit uuid
81
+ # @param name [String]
82
+ # @param type [String]
83
+ # @return [Hash] service description
84
+ def self.add(uuid, name:, type:, **opts)
85
+ if opts.first
86
+ raise ArgumentError, "unknown keyword: #{opts.first[0]}"
87
+ end
88
+
89
+ uuid = case uuid
90
+ when Integer
91
+ if !(0..4294967296).include?(uuid)
92
+ raise ArgumentError, "not a 16bit or 32bit uuid"
93
+ end
94
+ ([uuid].pack("L>").unpack('H*').first +
95
+ "-0000-1000-8000-00805F9B34FB")
96
+
97
+ when String
98
+ if uuid !~ UUID::REGEX
99
+ raise ArgumentError, "not a 128bit uuid string"
100
+ end
101
+ uuid
102
+ else raise ArgumentError, "invalid uuid type"
103
+ end
104
+ uuid = uuid.downcase
105
+ type = type.downcase
106
+
107
+ desc = DB_TYPE[type] = DB_UUID[uuid] = {
108
+ name: name,
109
+ type: type,
110
+ uuid: uuid,
111
+ }
112
+
113
+ stype = type.split('.')
114
+ key = stype.pop.to_sym
115
+ prefix = stype.join('.')
116
+ case prefix
117
+ when 'org.bluetooth.service'
118
+ if DB_NICKNAME.include?(key)
119
+ raise ArgumentError,
120
+ "nickname '#{key}' already registered (type: #{type})"
121
+ end
122
+ DB_NICKNAME[key] = desc
123
+ end
124
+
125
+ desc
126
+ end
127
+ end
128
+ end
@@ -1,3 +1,4 @@
1
1
  module BLE
2
- VERSION = "0.0.1"
2
+ # Library version
3
+ VERSION = "0.0.2"
3
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephane D'Alu
@@ -52,7 +52,35 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Allow access to BLE device from ruby
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: github-markup
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Allow access to Bluetooth Low Energy device from ruby
56
84
  email:
57
85
  - stephane.dalu@gmail.com
58
86
  executables: []
@@ -63,8 +91,13 @@ files:
63
91
  - LICENSE
64
92
  - ble.gemspec
65
93
  - lib/ble.rb
94
+ - lib/ble/adapter.rb
95
+ - lib/ble/agent.rb
96
+ - lib/ble/characteristic.rb
66
97
  - lib/ble/db_characteristic.rb
67
98
  - lib/ble/db_service.rb
99
+ - lib/ble/device.rb
100
+ - lib/ble/service.rb
68
101
  - lib/ble/version.rb
69
102
  homepage: http://github.com/sdalu/ruby-ble
70
103
  licenses:
@@ -89,6 +122,6 @@ rubyforge_project:
89
122
  rubygems_version: 2.2.2
90
123
  signing_key:
91
124
  specification_version: 4
92
- summary: Bluetooth Low Energy API
125
+ summary: Bluetooth Low Energy (BLE) API
93
126
  test_files: []
94
127
  has_rdoc: yard