Commit 2d588e61 authored by Tzu-Jung Lee's avatar Tzu-Jung Lee
Browse files

merge back x/io/bt into ble/, and add examples/lib/gatt.

parent dec7ee84
Artyom Astafurov <artyom.astafurov@gmail.com>
Ato Araki <ato.araki@gmail.com>
Austin Cherry <chillingwithim@gmail.com>
Homin Lee <homin.lee@suapapa.net>
Josh Bleecher Snyder <josharian@gmail.com> <jbleechersnyder@paypal.com>
Kevin Seidel <git@kseidel.org>
Mark Wolfe <mark@wolfe.id.au>
Mike Voytovich <mikevoyt@gmail.com>
Raffaele Sena <raff367@gmail.com>
Sergey Demyanov <s.demyanov@gmail.com>
Shirou WAKAYAMA <shirou.faw@gmail.com>
Shyam Jayaraman <bjornick@google.com>
Tyler Arnold <tylernol@mac.com>
Tzu-Jung Lee <roylee17@gmail.com> <roylee17@currant.com> <roylee@paypal.com>
bdd <bobbydesimone@gmail.com>
follower <github@rancidbacon.com>
Copyright (c) 2016, Currant, Inc. All rights reserved.
Copyright (c) 2016 Currant Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Currant Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package ble
// AdvHandler ...
type AdvHandler interface {
Handle(a Advertisement)
}
// The AdvHandlerFunc type is an adapter to allow the use of ordinary functions as packet or event handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type AdvHandlerFunc func(a Advertisement)
// Handle handles an advertisement.
func (f AdvHandlerFunc) Handle(a Advertisement) {
f(a)
}
// Advertisement ...
type Advertisement interface {
LocalName() string
ManufacturerData() []byte
ServiceData() []ServiceData
Services() []UUID
OverflowService() []UUID
TxPowerLevel() int
Connectable() bool
SolicitedService() []UUID
RSSI() int
Address() Addr
}
// ServiceData ...
type ServiceData struct {
UUID UUID
Data []byte
}
package ble
// DefaultMTU defines the default MTU of ATT protocol including 3 bytes of ATT header.
const DefaultMTU = 23
// MaxMTU is maximum of ATT_MTU, which is 512 bytes of value length, plus 3 bytes of ATT header.
// The maximum length of an attribute value shall be 512 octets [Vol 3, Part F, 3.2.9]
const MaxMTU = 512 + 3
// UUIDs ...
var (
GAPUUID = UUID16(0x1800) // Generic Access
GATTUUID = UUID16(0x1801) // Generic Attribute
CurrentTimeUUID = UUID16(0x1805) // Current Time Service
DeviceInfoUUID = UUID16(0x180A) // Device Information
BatteryUUID = UUID16(0x180F) // Battery Service
HIDUUID = UUID16(0x1812) // Human Interface Device
PrimaryServiceUUID = UUID16(0x2800)
SecondaryServiceUUID = UUID16(0x2801)
IncludeUUID = UUID16(0x2802)
CharacteristicUUID = UUID16(0x2803)
ClientCharacteristicConfigUUID = UUID16(0x2902)
ServerCharacteristicConfigUUID = UUID16(0x2903)
DeviceNameUUID = UUID16(0x2A00)
AppearanceUUID = UUID16(0x2A01)
PeripheralPrivacyUUID = UUID16(0x2A02)
ReconnectionAddrUUID = UUID16(0x2A03)
PeferredParamsUUID = UUID16(0x2A04)
ServiceChangedUUID = UUID16(0x2A05)
)
......@@ -2,7 +2,7 @@ package darwin
import (
"github.com/currantlabs/ble/darwin/xpc"
"github.com/currantlabs/x/io/bt"
"github.com/currantlabs/ble"
)
type adv struct {
......@@ -18,37 +18,37 @@ func (a *adv) ManufacturerData() []byte {
return a.ad.GetBytes("kCBAdvDataManufacturerData", nil)
}
func (a *adv) ServiceData() []bt.ServiceData {
func (a *adv) ServiceData() []ble.ServiceData {
xSDs, ok := a.ad["kCBAdvDataServiceData"]
if !ok {
return nil
}
xSD := xSDs.(xpc.Array)
var sd []bt.ServiceData
var sd []ble.ServiceData
for i := 0; i < len(xSD); i += 2 {
sd = append(
sd, bt.ServiceData{
UUID: bt.UUID(xSD[i].([]byte)),
sd, ble.ServiceData{
UUID: ble.UUID(xSD[i].([]byte)),
Data: xSD[i+1].([]byte),
})
}
return sd
}
func (a *adv) Services() []bt.UUID {
func (a *adv) Services() []ble.UUID {
xUUIDs, ok := a.ad["kCBAdvDataServiceUUIDs"]
if !ok {
return nil
}
var uuids []bt.UUID
var uuids []ble.UUID
for _, xUUID := range xUUIDs.(xpc.Array) {
uuids = append(uuids, bt.UUID(bt.Reverse(xUUID.([]byte))))
uuids = append(uuids, ble.UUID(ble.Reverse(xUUID.([]byte))))
}
return uuids
}
func (a *adv) OverflowService() []bt.UUID {
func (a *adv) OverflowService() []ble.UUID {
return nil // TODO
}
......@@ -56,7 +56,7 @@ func (a *adv) TxPowerLevel() int {
return a.ad.GetInt("kCBAdvDataTxPowerLevel", 0)
}
func (a *adv) SolicitedService() []bt.UUID {
func (a *adv) SolicitedService() []ble.UUID {
return nil // TODO
}
......@@ -68,6 +68,6 @@ func (a *adv) RSSI() int {
return a.args.GetInt("kCBMsgArgRssi", 0)
}
func (a *adv) Address() bt.Addr {
func (a *adv) Address() ble.Addr {
return xpc.UUID(a.args.MustGetUUID("kCBMsgArgDeviceUUID"))
}
......@@ -2,12 +2,12 @@ package darwin
import (
"github.com/currantlabs/ble/darwin/xpc"
"github.com/currantlabs/x/io/bt"
"github.com/currantlabs/ble"
)
// A Client is a GATT client.
type Client struct {
svcs []*bt.Service
svcs []*ble.Service
name string
id xpc.UUID
......@@ -15,7 +15,7 @@ type Client struct {
}
// NewClient ...
func NewClient(c bt.Conn) (*Client, error) {
func NewClient(c ble.Conn) (*Client, error) {
return &Client{
conn: c.(*conn),
id: xpc.MakeUUID(c.RemoteAddr().String()),
......@@ -23,7 +23,7 @@ func NewClient(c bt.Conn) (*Client, error) {
}
// Address returns UUID of the remote peripheral.
func (cln *Client) Address() bt.Addr {
func (cln *Client) Address() ble.Addr {
return cln.conn.RemoteAddr()
}
......@@ -34,13 +34,13 @@ func (cln *Client) Name() string {
}
// Services returns discovered services.
func (cln *Client) Services() []*bt.Service {
func (cln *Client) Services() []*ble.Service {
return cln.svcs
}
// DiscoverServices finds all the primary services on a server. [Vol 3, Part G, 4.4.1]
// If filter is specified, only filtered services are returned.
func (cln *Client) DiscoverServices(ss []bt.UUID) ([]*bt.Service, error) {
func (cln *Client) DiscoverServices(ss []ble.UUID) ([]*ble.Service, error) {
rsp := cln.conn.sendReq(45, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgUUIDs": uuidSlice(ss),
......@@ -48,11 +48,11 @@ func (cln *Client) DiscoverServices(ss []bt.UUID) ([]*bt.Service, error) {
if rsp.err() != nil {
return nil, rsp.err()
}
svcs := []*bt.Service{}
svcs := []*ble.Service{}
for _, xss := range rsp.services() {
xs := msg(xss.(xpc.Dict))
svcs = append(svcs, &bt.Service{
UUID: bt.MustParse(xs.uuid()),
svcs = append(svcs, &ble.Service{
UUID: ble.MustParse(xs.uuid()),
Handle: uint16(xs.serviceStartHandle()),
EndHandle: uint16(xs.serviceEndHandle()),
})
......@@ -63,7 +63,7 @@ func (cln *Client) DiscoverServices(ss []bt.UUID) ([]*bt.Service, error) {
// DiscoverIncludedServices finds the included services of a service. [Vol 3, Part G, 4.5.1]
// If filter is specified, only filtered services are returned.
func (cln *Client) DiscoverIncludedServices(ss []bt.UUID, s *bt.Service) ([]*bt.Service, error) {
func (cln *Client) DiscoverIncludedServices(ss []ble.UUID, s *ble.Service) ([]*ble.Service, error) {
rsp := cln.conn.sendReq(60, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgServiceStartHandle": s.Handle,
......@@ -73,12 +73,12 @@ func (cln *Client) DiscoverIncludedServices(ss []bt.UUID, s *bt.Service) ([]*bt.
if rsp.err() != nil {
return nil, rsp.err()
}
return nil, bt.ErrNotImplemented
return nil, ble.ErrNotImplemented
}
// DiscoverCharacteristics finds all the characteristics within a service. [Vol 3, Part G, 4.6.1]
// If filter is specified, only filtered characteristics are returned.
func (cln *Client) DiscoverCharacteristics(cs []bt.UUID, s *bt.Service) ([]*bt.Characteristic, error) {
func (cln *Client) DiscoverCharacteristics(cs []ble.UUID, s *ble.Service) ([]*ble.Characteristic, error) {
rsp := cln.conn.sendReq(62, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgServiceStartHandle": s.Handle,
......@@ -90,9 +90,9 @@ func (cln *Client) DiscoverCharacteristics(cs []bt.UUID, s *bt.Service) ([]*bt.C
}
for _, xcs := range rsp.characteristics() {
xc := msg(xcs.(xpc.Dict))
s.Characteristics = append(s.Characteristics, &bt.Characteristic{
UUID: bt.MustParse(xc.uuid()),
Property: bt.Property(xc.characteristicProperties()),
s.Characteristics = append(s.Characteristics, &ble.Characteristic{
UUID: ble.MustParse(xc.uuid()),
Property: ble.Property(xc.characteristicProperties()),
Handle: uint16(xc.characteristicHandle()),
ValueHandle: uint16(xc.characteristicValueHandle()),
})
......@@ -102,7 +102,7 @@ func (cln *Client) DiscoverCharacteristics(cs []bt.UUID, s *bt.Service) ([]*bt.C
// DiscoverDescriptors finds all the descriptors within a characteristic. [Vol 3, Part G, 4.7.1]
// If filter is specified, only filtered descriptors are returned.
func (cln *Client) DiscoverDescriptors(ds []bt.UUID, c *bt.Characteristic) ([]*bt.Descriptor, error) {
func (cln *Client) DiscoverDescriptors(ds []ble.UUID, c *ble.Characteristic) ([]*ble.Descriptor, error) {
rsp := cln.conn.sendReq(70, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgCharacteristicHandle": c.Handle,
......@@ -111,8 +111,8 @@ func (cln *Client) DiscoverDescriptors(ds []bt.UUID, c *bt.Characteristic) ([]*b
})
for _, xds := range rsp.descriptors() {
xd := msg(xds.(xpc.Dict))
c.Descriptors = append(c.Descriptors, &bt.Descriptor{
UUID: bt.MustParse(xd.uuid()),
c.Descriptors = append(c.Descriptors, &ble.Descriptor{
UUID: ble.MustParse(xd.uuid()),
Handle: uint16(xd.descriptorHandle()),
})
}
......@@ -120,7 +120,7 @@ func (cln *Client) DiscoverDescriptors(ds []bt.UUID, c *bt.Characteristic) ([]*b
}
// ReadCharacteristic reads a characteristic value from a server. [Vol 3, Part G, 4.8.1]
func (cln *Client) ReadCharacteristic(c *bt.Characteristic) ([]byte, error) {
func (cln *Client) ReadCharacteristic(c *ble.Characteristic) ([]byte, error) {
rsp := cln.conn.sendReq(65, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgCharacteristicHandle": c.Handle,
......@@ -133,12 +133,12 @@ func (cln *Client) ReadCharacteristic(c *bt.Characteristic) ([]byte, error) {
}
// ReadLongCharacteristic reads a characteristic value which is longer than the MTU. [Vol 3, Part G, 4.8.3]
func (cln *Client) ReadLongCharacteristic(c *bt.Characteristic) ([]byte, error) {
return nil, bt.ErrNotImplemented
func (cln *Client) ReadLongCharacteristic(c *ble.Characteristic) ([]byte, error) {
return nil, ble.ErrNotImplemented
}
// WriteCharacteristic writes a characteristic value to a server. [Vol 3, Part G, 4.9.3]
func (cln *Client) WriteCharacteristic(c *bt.Characteristic, b []byte, noRsp bool) error {
func (cln *Client) WriteCharacteristic(c *ble.Characteristic, b []byte, noRsp bool) error {
args := xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgCharacteristicHandle": c.Handle,
......@@ -154,7 +154,7 @@ func (cln *Client) WriteCharacteristic(c *bt.Characteristic, b []byte, noRsp boo
}
// ReadDescriptor reads a characteristic descriptor from a server. [Vol 3, Part G, 4.12.1]
func (cln *Client) ReadDescriptor(d *bt.Descriptor) ([]byte, error) {
func (cln *Client) ReadDescriptor(d *ble.Descriptor) ([]byte, error) {
rsp := cln.conn.sendReq(77, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgDescriptorHandle": d.Handle,
......@@ -166,7 +166,7 @@ func (cln *Client) ReadDescriptor(d *bt.Descriptor) ([]byte, error) {
}
// WriteDescriptor writes a characteristic descriptor to a server. [Vol 3, Part G, 4.12.3]
func (cln *Client) WriteDescriptor(d *bt.Descriptor, b []byte) error {
func (cln *Client) WriteDescriptor(d *ble.Descriptor, b []byte) error {
rsp := cln.conn.sendReq(78, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgDescriptorHandle": d.Handle,
......@@ -193,7 +193,7 @@ func (cln *Client) ExchangeMTU(mtu int) (int, error) {
// Subscribe subscribes to indication (if ind is set true), or notification of a
// characteristic value. [Vol 3, Part G, 4.10 & 4.11]
func (cln *Client) Subscribe(c *bt.Characteristic, ind bool, fn bt.NotificationHandler) error {
func (cln *Client) Subscribe(c *ble.Characteristic, ind bool, fn ble.NotificationHandler) error {
cln.conn.Lock()
defer cln.conn.Unlock()
cln.conn.subs[c.Handle] = &sub{fn: fn, char: c}
......@@ -212,7 +212,7 @@ func (cln *Client) Subscribe(c *bt.Characteristic, ind bool, fn bt.NotificationH
// Unsubscribe unsubscribes to indication (if ind is set true), or notification
// of a specified characteristic value. [Vol 3, Part G, 4.10 & 4.11]
func (cln *Client) Unsubscribe(c *bt.Characteristic, ind bool) error {
func (cln *Client) Unsubscribe(c *ble.Characteristic, ind bool) error {
rsp := cln.conn.sendReq(68, xpc.Dict{
"kCBMsgArgDeviceUUID": cln.id,
"kCBMsgArgCharacteristicHandle": c.Handle,
......@@ -243,6 +243,6 @@ func (cln *Client) CancelConnection() error {
}
type sub struct {
fn bt.NotificationHandler
char *bt.Characteristic
fn ble.NotificationHandler
char *ble.Characteristic
}
......@@ -5,17 +5,17 @@ import (
"sync"
"github.com/currantlabs/ble/darwin/xpc"
"github.com/currantlabs/x/io/bt"
"github.com/currantlabs/ble"
)
func newConn(d *Device, a bt.Addr) *conn {
func newConn(d *Device, a ble.Addr) *conn {
return &conn{
dev: d,
rxMTU: 23,
txMTU: 23,
addr: a,
notifiers: make(map[uint16]bt.Notifier),
notifiers: make(map[uint16]ble.Notifier),
subs: make(map[uint16]*sub),
rspc: make(chan msg),
......@@ -30,7 +30,7 @@ type conn struct {
ctx context.Context
rxMTU int
txMTU int
addr bt.Addr
addr ble.Addr
rspc chan msg
......@@ -38,7 +38,7 @@ type conn struct {
connLatency int
supervisionTimeout int
notifiers map[uint16]bt.Notifier // central connection only
notifiers map[uint16]ble.Notifier // central connection only
subs map[uint16]*sub
}
......@@ -51,11 +51,11 @@ func (c *conn) SetContext(ctx context.Context) {
c.ctx = ctx
}
func (c *conn) LocalAddr() bt.Addr {
func (c *conn) LocalAddr() ble.Addr {
return c.dev.Addr()
}
func (c *conn) RemoteAddr() bt.Addr {
func (c *conn) RemoteAddr() ble.Addr {
return c.addr
}
......@@ -88,7 +88,7 @@ func (c *conn) Close() error {
}
// server (peripheral)
func (c *conn) subscribed(char *bt.Characteristic) {
func (c *conn) subscribed(char *ble.Characteristic) {
h := char.Handle
if _, found := c.notifiers[h]; found {
return
......@@ -101,14 +101,14 @@ func (c *conn) subscribed(char *bt.Characteristic) {
})
return len(b), nil
}
n := bt.NewNotifier(send)
n := ble.NewNotifier(send)
c.notifiers[h] = n
req := bt.NewRequest(c, nil, 0) // convey *conn to user handler.
req := ble.NewRequest(c, nil, 0) // convey *conn to user handler.
go char.NotifyHandler.ServeNotify(req, n)
}
// server (peripheral)
func (c *conn) unsubscribed(char *bt.Characteristic) {
func (c *conn) unsubscribed(char *ble.Characteristic) {
if n, found := c.notifiers[char.Handle]; found {
n.Close()
delete(c.notifiers, char.Handle)
......
......@@ -8,7 +8,7 @@ import (
"time"
"github.com/currantlabs/ble/darwin/xpc"
"github.com/currantlabs/x/io/bt"
"github.com/currantlabs/ble"
)
const (
......@@ -49,11 +49,11 @@ type Device struct {
conns map[string]*conn
// Only used in client/centralManager implementation
advHandler bt.AdvHandler
advHandler ble.AdvHandler
chConn chan *conn
// Only used in server/peripheralManager implementation
chars map[int]*bt.Characteristic
chars map[int]*ble.Characteristic
base int
}
......@@ -63,7 +63,7 @@ func NewDevice(opts ...Option) (*Device, error) {
rspc: make(chan msg),
conns: make(map[string]*conn),
chConn: make(chan *conn),
chars: make(map[int]*bt.Characteristic),
chars: make(map[int]*ble.Characteristic),
base: 1,
}
if err := d.Option(opts...); err != nil {
......@@ -71,6 +71,7 @@ func NewDevice(opts ...Option) (*Device, error) {
}
d.xpc = xpc.XpcConnect("com.apple.blued", d)
return d, nil
}
......@@ -106,7 +107,7 @@ func (d *Device) AdvertiseMfgData(b []byte) error {
}
// AdvertiseNameAndServices advertises name and specifid service UUIDs.
func (d *Device) AdvertiseNameAndServices(name string, ss ...bt.UUID) error {
func (d *Device) AdvertiseNameAndServices(name string, ss ...ble.UUID) error {
return d.sendReq(8, xpc.Dict{
"kCBAdvDataLocalName": name,
"kCBAdvDataServiceUUIDs": uuidSlice(ss)},
......@@ -127,9 +128,9 @@ func (d *Device) AdvertiseIBeaconData(md []byte) error {
}
// AdvertiseIBeacon advertises iBeacon packet.
func (d *Device) AdvertiseIBeacon(u bt.UUID, major, minor uint16, pwr int8) error {
func (d *Device) AdvertiseIBeacon(u ble.UUID, major, minor uint16, pwr int8) error {
b := make([]byte, 21)
copy(b, bt.Reverse(u)) // Big endian
copy(b, ble.Reverse(u)) // Big endian
binary.BigEndian.PutUint16(b[16:], major) // Big endian
binary.BigEndian.PutUint16(b[18:], minor) // Big endian
b[20] = uint8(pwr) // Measured Tx Power
......@@ -142,7 +143,7 @@ func (d *Device) StopAdvertising() error {
}
// SetAdvHandler ...
func (d *Device) SetAdvHandler(ah bt.AdvHandler) error {
func (d *Device) SetAdvHandler(ah ble.AdvHandler) error {
d.advHandler = ah
return nil
}
......@@ -176,13 +177,13 @@ func (d *Device) RemoveAllServices() error {
// 0x180A (Device Information)
// 0x180F (Battery Service)
// 0x1812 (Human Interface Device)
func (d *Device) AddService(s *bt.Service) error {
if s.UUID.Equal(bt.GAPUUID) ||
s.UUID.Equal(bt.GATTUUID) ||
s.UUID.Equal(bt.CurrentTimeUUID) ||
s.UUID.Equal(bt.DeviceInfoUUID) ||
s.UUID.Equal(bt.BatteryUUID) ||
s.UUID.Equal(bt.HIDUUID) {
func (d *Device) AddService(s *ble.Service) error {
if s.UUID.Equal(ble.GAPUUID) ||
s.UUID.Equal(ble.GATTUUID) ||
s.UUID.Equal(ble.CurrentTimeUUID) ||
s.UUID.Equal(ble.DeviceInfoUUID) ||
s.UUID.Equal(ble.BatteryUUID) ||
s.UUID.Equal(ble.HIDUUID) {
return nil
}
xs := xpc.Dict{
......@@ -190,7 +191,7 @@ func (d *Device) AddService(s *bt.Service) error {
"kCBMsgArgAttributeIDs": []int{},
"kCBMsgArgCharacteristics": nil,
"kCBMsgArgType": 1, // 1 => primary, 0 => excluded
"kCBMsgArgUUID": bt.Reverse(s.UUID),
"kCBMsgArgUUID": ble.Reverse(s.UUID),
}
d.base++
......@@ -198,39 +199,39 @@ func (d *Device) AddService(s *bt.Service) error {
for _, c := range s.Characteristics {
props := 0
perm := 0
if c.Property&bt.CharRead != 0 {
if c.Property&ble.CharRead != 0 {
props |= 0x02
if bt.CharRead&c.Secure != 0 {
if ble.CharRead&c.Secure != 0 {
perm |= 0x04
} else {
perm |= 0x01
}
}
if c.Property&bt.CharWriteNR != 0 {
if c.Property&ble.CharWriteNR != 0 {
props |= 0x04
if c.Secure&bt.CharWriteNR != 0 {
if c.Secure&ble.CharWriteNR != 0 {
perm |= 0x08
} else {
perm |= 0x02
}
}
if c.Property&bt.CharWrite != 0 {
if c.Property&ble.CharWrite != 0 {
props |= 0x08
if c.Secure&bt.CharWrite != 0 {
if c.Secure&ble.CharWrite != 0 {
perm |= 0x08
} else {
perm |= 0x02
}
}
if c.Property&bt.CharNotify != 0 {
if c.Secure&bt.CharNotify != 0 {
if c.Property&ble.CharNotify != 0 {
if c.Secure&ble.CharNotify != 0 {
props |= 0x100
} else {
props |= 0x10
}
}
if c.Property&bt.CharIndicate != 0 {
if c.Secure&bt.CharIndicate != 0 {
if c.Property&ble.CharIndicate != 0 {
if c.Secure&ble.CharIndicate != 0 {
props |= 0x200
} else {
props |= 0x20
......@@ -239,7 +240,7 @@ func (d *Device) AddService(s *bt.Service) error {
xc := xpc.Dict{
"kCBMsgArgAttributeID": d.base,
"kCBMsgArgUUID": bt.Reverse(c.UUID),
"kCBMsgArgUUID": ble.Reverse(c.UUID),
"kCBMsgArgAttributePermissions": perm,
"kCBMsgArgCharacteristicProperties": props,
"kCBMsgArgData": c.Value,
......@@ -250,13 +251,13 @@ func (d *Device) AddService(s *bt.Service) error {
xds := xpc.Array{}
for _, d := range c.Descriptors {
if d.UUID.Equal(bt.ClientCharacteristicConfigUUID) {