all: use 'debug' variable protected by build tags for debug logging

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2024-01-16 17:18:32 +01:00 committed by Ron Evans
parent dc7d1b4d4c
commit 564b0ba58f
9 changed files with 99 additions and 102 deletions

View file

@ -1,8 +1,5 @@
package bluetooth package bluetooth
// Set this to true to print debug messages, for example for unknown events.
const debug = false
// SetConnectHandler sets a handler function to be called whenever the adaptor connects // SetConnectHandler sets a handler function to be called whenever the adaptor connects
// or disconnects. You must call this before you call adaptor.Connect() for centrals // or disconnects. You must call this before you call adaptor.Connect() for centrals
// or adaptor.Start() for peripherals in order for it to work. // or adaptor.Start() for peripherals in order for it to work.

View file

@ -155,7 +155,7 @@ func (a *Adapter) startNotifications() {
return return
} }
if _debug { if debug {
println("starting notifications...") println("starting notifications...")
} }
@ -166,7 +166,7 @@ func (a *Adapter) startNotifications() {
for { for {
if err := a.att.poll(); err != nil { if err := a.att.poll(); err != nil {
// TODO: handle error // TODO: handle error
if _debug { if debug {
println("error polling for notifications:", err.Error()) println("error polling for notifications:", err.Error())
} }
} }
@ -180,13 +180,13 @@ func (a *Adapter) startNotifications() {
for { for {
select { select {
case not := <-a.att.notifications: case not := <-a.att.notifications:
if _debug { if debug {
println("notification received", not.connectionHandle, not.handle, not.data) println("notification received", not.connectionHandle, not.handle, not.data)
} }
d := a.findDevice(not.connectionHandle) d := a.findDevice(not.connectionHandle)
if d.deviceInternal == nil { if d.deviceInternal == nil {
if _debug { if debug {
println("no device found for handle", not.connectionHandle) println("no device found for handle", not.connectionHandle)
} }
continue continue
@ -194,7 +194,7 @@ func (a *Adapter) startNotifications() {
n := d.findNotificationRegistration(not.handle) n := d.findNotificationRegistration(not.handle)
if n == nil { if n == nil {
if _debug { if debug {
println("no notification registered for handle", not.handle) println("no notification registered for handle", not.handle)
} }
continue continue
@ -215,7 +215,7 @@ func (a *Adapter) startNotifications() {
func (a *Adapter) findDevice(handle uint16) Device { func (a *Adapter) findDevice(handle uint16) Device {
for _, d := range a.connectedDevices { for _, d := range a.connectedDevices {
if d.handle == handle { if d.handle == handle {
if _debug { if debug {
println("found device", handle, d.Address.String(), "with notifications registered", len(d.notificationRegistrations)) println("found device", handle, d.Address.String(), "with notifications registered", len(d.notificationRegistrations))
} }

View file

@ -288,7 +288,7 @@ func newATT(hci *hci) *att {
} }
func (a *att) readByGroupReq(connectionHandle, startHandle, endHandle uint16, uuid shortUUID) error { func (a *att) readByGroupReq(connectionHandle, startHandle, endHandle uint16, uuid shortUUID) error {
if _debug { if debug {
println("att.readByGroupReq:", connectionHandle, startHandle, endHandle, uuid) println("att.readByGroupReq:", connectionHandle, startHandle, endHandle, uuid)
} }
@ -309,7 +309,7 @@ func (a *att) readByGroupReq(connectionHandle, startHandle, endHandle uint16, uu
} }
func (a *att) readByTypeReq(connectionHandle, startHandle, endHandle uint16, typ uint16) error { func (a *att) readByTypeReq(connectionHandle, startHandle, endHandle uint16, typ uint16) error {
if _debug { if debug {
println("att.readByTypeReq:", connectionHandle, startHandle, endHandle, typ) println("att.readByTypeReq:", connectionHandle, startHandle, endHandle, typ)
} }
@ -330,7 +330,7 @@ func (a *att) readByTypeReq(connectionHandle, startHandle, endHandle uint16, typ
} }
func (a *att) findInfoReq(connectionHandle, startHandle, endHandle uint16) error { func (a *att) findInfoReq(connectionHandle, startHandle, endHandle uint16) error {
if _debug { if debug {
println("att.findInfoReq:", connectionHandle, startHandle, endHandle) println("att.findInfoReq:", connectionHandle, startHandle, endHandle)
} }
@ -350,7 +350,7 @@ func (a *att) findInfoReq(connectionHandle, startHandle, endHandle uint16) error
} }
func (a *att) readReq(connectionHandle, valueHandle uint16) error { func (a *att) readReq(connectionHandle, valueHandle uint16) error {
if _debug { if debug {
println("att.readReq:", connectionHandle, valueHandle) println("att.readReq:", connectionHandle, valueHandle)
} }
@ -369,7 +369,7 @@ func (a *att) readReq(connectionHandle, valueHandle uint16) error {
} }
func (a *att) writeCmd(connectionHandle, valueHandle uint16, data []byte) error { func (a *att) writeCmd(connectionHandle, valueHandle uint16, data []byte) error {
if _debug { if debug {
println("att.writeCmd:", connectionHandle, valueHandle, hex.EncodeToString(data)) println("att.writeCmd:", connectionHandle, valueHandle, hex.EncodeToString(data))
} }
@ -388,7 +388,7 @@ func (a *att) writeCmd(connectionHandle, valueHandle uint16, data []byte) error
} }
func (a *att) writeReq(connectionHandle, valueHandle uint16, data []byte) error { func (a *att) writeReq(connectionHandle, valueHandle uint16, data []byte) error {
if _debug { if debug {
println("att.writeReq:", connectionHandle, valueHandle, hex.EncodeToString(data)) println("att.writeReq:", connectionHandle, valueHandle, hex.EncodeToString(data))
} }
@ -407,7 +407,7 @@ func (a *att) writeReq(connectionHandle, valueHandle uint16, data []byte) error
} }
func (a *att) mtuReq(connectionHandle, mtu uint16) error { func (a *att) mtuReq(connectionHandle, mtu uint16) error {
if _debug { if debug {
println("att.mtuReq:", connectionHandle) println("att.mtuReq:", connectionHandle)
} }
@ -428,7 +428,7 @@ func (a *att) mtuReq(connectionHandle, mtu uint16) error {
func (a *att) sendReq(handle uint16, data []byte) error { func (a *att) sendReq(handle uint16, data []byte) error {
a.clearResponse() a.clearResponse()
if _debug { if debug {
println("att.sendReq:", handle, "data:", hex.EncodeToString(data)) println("att.sendReq:", handle, "data:", hex.EncodeToString(data))
} }
@ -440,7 +440,7 @@ func (a *att) sendReq(handle uint16, data []byte) error {
} }
func (a *att) sendNotification(handle uint16, data []byte) error { func (a *att) sendNotification(handle uint16, data []byte) error {
if _debug { if debug {
println("att.sendNotifications:", handle, "data:", hex.EncodeToString(data)) println("att.sendNotifications:", handle, "data:", hex.EncodeToString(data))
} }
@ -452,7 +452,7 @@ func (a *att) sendNotification(handle uint16, data []byte) error {
binary.LittleEndian.PutUint16(b[1:], handle) binary.LittleEndian.PutUint16(b[1:], handle)
for connection := range a.connections { for connection := range a.connections {
if _debug { if debug {
println("att.sendNotifications: sending to", connection) println("att.sendNotifications: sending to", connection)
} }
@ -467,7 +467,7 @@ func (a *att) sendNotification(handle uint16, data []byte) error {
func (a *att) sendError(handle uint16, opcode uint8, hdl uint16, code uint8) error { func (a *att) sendError(handle uint16, opcode uint8, hdl uint16, code uint8) error {
a.clearResponse() a.clearResponse()
if _debug { if debug {
println("att.sendError:", handle, "data:", opcode, hdl, code) println("att.sendError:", handle, "data:", opcode, hdl, code)
} }
@ -485,7 +485,7 @@ func (a *att) sendError(handle uint16, opcode uint8, hdl uint16, code uint8) err
} }
func (a *att) handleData(handle uint16, buf []byte) error { func (a *att) handleData(handle uint16, buf []byte) error {
if _debug { if debug {
println("att.handleData:", handle, "data:", hex.EncodeToString(buf)) println("att.handleData:", handle, "data:", hex.EncodeToString(buf))
} }
@ -496,14 +496,14 @@ func (a *att) handleData(handle uint16, buf []byte) error {
a.lastErrorHandle = binary.LittleEndian.Uint16(buf[2:]) a.lastErrorHandle = binary.LittleEndian.Uint16(buf[2:])
a.lastErrorCode = buf[4] a.lastErrorCode = buf[4]
if _debug { if debug {
println("att.handleData: attOpERROR", a.lastErrorOpcode, a.lastErrorCode) println("att.handleData: attOpERROR", a.lastErrorOpcode, a.lastErrorCode)
} }
return ErrATTOp return ErrATTOp
case attOpMTUReq: case attOpMTUReq:
if _debug { if debug {
println("att.handleData: attOpMTUReq") println("att.handleData: attOpMTUReq")
} }
a.mtu = binary.LittleEndian.Uint16(buf[1:]) a.mtu = binary.LittleEndian.Uint16(buf[1:])
@ -513,14 +513,14 @@ func (a *att) handleData(handle uint16, buf []byte) error {
} }
case attOpMTUResponse: case attOpMTUResponse:
if _debug { if debug {
println("att.handleData: attOpMTUResponse") println("att.handleData: attOpMTUResponse")
} }
a.responded = true a.responded = true
a.mtu = binary.LittleEndian.Uint16(buf[1:]) a.mtu = binary.LittleEndian.Uint16(buf[1:])
case attOpFindInfoReq: case attOpFindInfoReq:
if _debug { if debug {
println("att.handleData: attOpFindInfoReq") println("att.handleData: attOpFindInfoReq")
} }
@ -530,7 +530,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return a.handleFindInfoReq(handle, startHandle, endHandle) return a.handleFindInfoReq(handle, startHandle, endHandle)
case attOpFindInfoResponse: case attOpFindInfoResponse:
if _debug { if debug {
println("att.handleData: attOpFindInfoResponse") println("att.handleData: attOpFindInfoResponse")
} }
a.responded = true a.responded = true
@ -541,7 +541,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
d := rawDescriptor{} d := rawDescriptor{}
d.Write(buf[i : i+lengthPerDescriptor]) d.Write(buf[i : i+lengthPerDescriptor])
if _debug { if debug {
println("att.handleData: descriptor", d.handle, hex.EncodeToString(d.data)) println("att.handleData: descriptor", d.handle, hex.EncodeToString(d.data))
} }
@ -549,12 +549,12 @@ func (a *att) handleData(handle uint16, buf []byte) error {
} }
case attOpFindByTypeReq: case attOpFindByTypeReq:
if _debug { if debug {
println("att.handleData: attOpFindByTypeReq") println("att.handleData: attOpFindByTypeReq")
} }
case attOpReadByTypeReq: case attOpReadByTypeReq:
if _debug { if debug {
println("att.handleData: attOpReadByTypeReq") println("att.handleData: attOpReadByTypeReq")
} }
@ -565,7 +565,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return a.handleReadByTypeReq(handle, startHandle, endHandle, uuid) return a.handleReadByTypeReq(handle, startHandle, endHandle, uuid)
case attOpReadByTypeResponse: case attOpReadByTypeResponse:
if _debug { if debug {
println("att.handleData: attOpReadByTypeResponse") println("att.handleData: attOpReadByTypeResponse")
} }
a.responded = true a.responded = true
@ -576,7 +576,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
c := rawCharacteristic{} c := rawCharacteristic{}
c.Write(buf[i : i+lengthPerCharacteristic]) c.Write(buf[i : i+lengthPerCharacteristic])
if _debug { if debug {
println("att.handleData: characteristic", c.startHandle, c.properties, c.valueHandle, c.uuid.String()) println("att.handleData: characteristic", c.startHandle, c.properties, c.valueHandle, c.uuid.String())
} }
@ -586,7 +586,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return nil return nil
case attOpReadByGroupReq: case attOpReadByGroupReq:
if _debug { if debug {
println("att.handleData: attOpReadByGroupReq") println("att.handleData: attOpReadByGroupReq")
} }
@ -597,7 +597,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return a.handleReadByGroupReq(handle, startHandle, endHandle, uuid) return a.handleReadByGroupReq(handle, startHandle, endHandle, uuid)
case attOpReadByGroupResponse: case attOpReadByGroupResponse:
if _debug { if debug {
println("att.handleData: attOpReadByGroupResponse") println("att.handleData: attOpReadByGroupResponse")
} }
a.responded = true a.responded = true
@ -608,7 +608,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
service := rawService{} service := rawService{}
service.Write(buf[i : i+lengthPerService]) service.Write(buf[i : i+lengthPerService])
if _debug { if debug {
println("att.handleData: service", service.startHandle, service.endHandle, service.uuid.String()) println("att.handleData: service", service.startHandle, service.endHandle, service.uuid.String())
} }
@ -618,7 +618,7 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return nil return nil
case attOpReadReq: case attOpReadReq:
if _debug { if debug {
println("att.handleData: attOpReadReq") println("att.handleData: attOpReadReq")
} }
@ -626,19 +626,19 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return a.handleReadReq(handle, attrHandle) return a.handleReadReq(handle, attrHandle)
case attOpReadBlobReq: case attOpReadBlobReq:
if _debug { if debug {
println("att.handleData: attOpReadBlobReq") println("att.handleData: attOpReadBlobReq")
} }
case attOpReadResponse: case attOpReadResponse:
if _debug { if debug {
println("att.handleData: attOpReadResponse") println("att.handleData: attOpReadResponse")
} }
a.responded = true a.responded = true
a.value = append(a.value, buf[1:]...) a.value = append(a.value, buf[1:]...)
case attOpWriteReq: case attOpWriteReq:
if _debug { if debug {
println("att.handleData: attOpWriteReq") println("att.handleData: attOpWriteReq")
} }
@ -646,28 +646,28 @@ func (a *att) handleData(handle uint16, buf []byte) error {
return a.handleWriteReq(handle, attrHandle, buf[3:]) return a.handleWriteReq(handle, attrHandle, buf[3:])
case attOpWriteCmd: case attOpWriteCmd:
if _debug { if debug {
println("att.handleData: attOpWriteCmd") println("att.handleData: attOpWriteCmd")
} }
case attOpWriteResponse: case attOpWriteResponse:
if _debug { if debug {
println("att.handleData: attOpWriteResponse") println("att.handleData: attOpWriteResponse")
} }
a.responded = true a.responded = true
case attOpPrepWriteReq: case attOpPrepWriteReq:
if _debug { if debug {
println("att.handleData: attOpPrepWriteReq") println("att.handleData: attOpPrepWriteReq")
} }
case attOpExecWriteReq: case attOpExecWriteReq:
if _debug { if debug {
println("att.handleData: attOpExecWriteReq") println("att.handleData: attOpExecWriteReq")
} }
case attOpHandleNotify: case attOpHandleNotify:
if _debug { if debug {
println("att.handleData: attOpHandleNotify") println("att.handleData: attOpHandleNotify")
} }
@ -685,27 +685,27 @@ func (a *att) handleData(handle uint16, buf []byte) error {
} }
case attOpHandleInd: case attOpHandleInd:
if _debug { if debug {
println("att.handleData: attOpHandleInd") println("att.handleData: attOpHandleInd")
} }
case attOpHandleCNF: case attOpHandleCNF:
if _debug { if debug {
println("att.handleData: attOpHandleCNF") println("att.handleData: attOpHandleCNF")
} }
case attOpReadMultiReq: case attOpReadMultiReq:
if _debug { if debug {
println("att.handleData: attOpReadMultiReq") println("att.handleData: attOpReadMultiReq")
} }
case attOpSignedWriteCmd: case attOpSignedWriteCmd:
if _debug { if debug {
println("att.handleData: attOpSignedWriteCmd") println("att.handleData: attOpSignedWriteCmd")
} }
default: default:
if _debug { if debug {
println("att.handleData: unknown") println("att.handleData: unknown")
} }
} }
@ -723,7 +723,7 @@ func (a *att) handleReadByGroupReq(handle, start, end uint16, uuid shortUUID) er
case shortUUID(gattServiceUUID): case shortUUID(gattServiceUUID):
for _, s := range a.localServices { for _, s := range a.localServices {
if s.startHandle >= start && s.endHandle <= end { if s.startHandle >= start && s.endHandle <= end {
if _debug { if debug {
println("attOpReadByGroupReq: replying with service", s.startHandle, s.endHandle, s.uuid.String()) println("attOpReadByGroupReq: replying with service", s.startHandle, s.endHandle, s.uuid.String())
} }
@ -762,7 +762,7 @@ func (a *att) handleReadByGroupReq(handle, start, end uint16, uuid shortUUID) er
return nil return nil
default: default:
if _debug { if debug {
println("handleReadByGroupReq: unknown uuid", New16BitUUID(uint16(uuid)).String()) println("handleReadByGroupReq: unknown uuid", New16BitUUID(uint16(uuid)).String())
} }
if err := a.sendError(handle, attOpReadByGroupReq, start, attErrorAttrNotFound); err != nil { if err := a.sendError(handle, attOpReadByGroupReq, start, attErrorAttrNotFound); err != nil {
@ -784,12 +784,12 @@ func (a *att) handleReadByTypeReq(handle, start, end uint16, uuid shortUUID) err
response[1] = 0 response[1] = 0
for _, c := range a.characteristics { for _, c := range a.characteristics {
if _debug { if debug {
println("handleReadByTypeReq: looking at characteristic", c.startHandle, c.uuid.String()) println("handleReadByTypeReq: looking at characteristic", c.startHandle, c.uuid.String())
} }
if c.startHandle >= start && c.valueHandle <= end { if c.startHandle >= start && c.valueHandle <= end {
if _debug { if debug {
println("handleReadByTypeReq: replying with characteristic", c.startHandle, c.uuid.String()) println("handleReadByTypeReq: replying with characteristic", c.startHandle, c.uuid.String())
} }
@ -827,7 +827,7 @@ func (a *att) handleReadByTypeReq(handle, start, end uint16, uuid shortUUID) err
return nil return nil
default: default:
if _debug { if debug {
println("handleReadByTypeReq: unknown uuid", New16BitUUID(uint16(uuid)).String()) println("handleReadByTypeReq: unknown uuid", New16BitUUID(uint16(uuid)).String())
} }
if err := a.sendError(handle, attOpReadByTypeReq, start, attErrorAttrNotFound); err != nil { if err := a.sendError(handle, attOpReadByTypeReq, start, attErrorAttrNotFound); err != nil {
@ -848,12 +848,12 @@ func (a *att) handleFindInfoReq(handle, start, end uint16) error {
response[1] = 0 response[1] = 0
for _, attr := range a.attributes { for _, attr := range a.attributes {
if _debug { if debug {
println("handleFindInfoReq: looking at attribute") println("handleFindInfoReq: looking at attribute")
} }
if attr.handle >= start && attr.handle <= end { if attr.handle >= start && attr.handle <= end {
if _debug { if debug {
println("handleFindInfoReq: replying with attribute", attr.handle, attr.uuid.String(), attr.typ) println("handleFindInfoReq: replying with attribute", attr.handle, attr.uuid.String(), attr.typ)
} }
@ -896,7 +896,7 @@ func (a *att) handleFindInfoReq(handle, start, end uint16) error {
func (a *att) handleReadReq(handle, attrHandle uint16) error { func (a *att) handleReadReq(handle, attrHandle uint16) error {
attr := a.findAttribute(attrHandle) attr := a.findAttribute(attrHandle)
if attr == nil { if attr == nil {
if _debug { if debug {
println("att.handleReadReq: attribute not found", attrHandle) println("att.handleReadReq: attribute not found", attrHandle)
} }
return a.sendError(handle, attOpReadReq, attrHandle, attErrorAttrNotFound) return a.sendError(handle, attOpReadReq, attrHandle, attErrorAttrNotFound)
@ -908,7 +908,7 @@ func (a *att) handleReadReq(handle, attrHandle uint16) error {
switch attr.typ { switch attr.typ {
case attributeTypeCharacteristicValue: case attributeTypeCharacteristicValue:
if _debug { if debug {
println("att.handleReadReq: reading characteristic value", attrHandle) println("att.handleReadReq: reading characteristic value", attrHandle)
} }
@ -928,7 +928,7 @@ func (a *att) handleReadReq(handle, attrHandle uint16) error {
} }
case attributeTypeDescriptor: case attributeTypeDescriptor:
if _debug { if debug {
println("att.handleReadReq: reading descriptor", attrHandle) println("att.handleReadReq: reading descriptor", attrHandle)
} }
@ -954,7 +954,7 @@ func (a *att) handleReadReq(handle, attrHandle uint16) error {
func (a *att) handleWriteReq(handle, attrHandle uint16, data []byte) error { func (a *att) handleWriteReq(handle, attrHandle uint16, data []byte) error {
attr := a.findAttribute(attrHandle) attr := a.findAttribute(attrHandle)
if attr == nil { if attr == nil {
if _debug { if debug {
println("att.handleWriteReq: attribute not found", attrHandle) println("att.handleWriteReq: attribute not found", attrHandle)
} }
return a.sendError(handle, attOpWriteReq, attrHandle, attErrorAttrNotFound) return a.sendError(handle, attOpWriteReq, attrHandle, attErrorAttrNotFound)
@ -962,7 +962,7 @@ func (a *att) handleWriteReq(handle, attrHandle uint16, data []byte) error {
switch attr.typ { switch attr.typ {
case attributeTypeCharacteristicValue: case attributeTypeCharacteristicValue:
if _debug { if debug {
println("att.handleWriteReq: writing characteristic value", attrHandle, hex.EncodeToString(data)) println("att.handleWriteReq: writing characteristic value", attrHandle, hex.EncodeToString(data))
} }
@ -980,7 +980,7 @@ func (a *att) handleWriteReq(handle, attrHandle uint16, data []byte) error {
} }
case attributeTypeDescriptor: case attributeTypeDescriptor:
if _debug { if debug {
println("att.handleWriteReq: writing descriptor", attrHandle, hex.EncodeToString(data)) println("att.handleWriteReq: writing descriptor", attrHandle, hex.EncodeToString(data))
} }

View file

@ -2,4 +2,4 @@
package bluetooth package bluetooth
var _debug = true var debug = true

View file

@ -48,7 +48,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
case a.hci.advData.reported: case a.hci.advData.reported:
adf := AdvertisementFields{} adf := AdvertisementFields{}
if a.hci.advData.eirLength > 31 { if a.hci.advData.eirLength > 31 {
if _debug { if debug {
println("eirLength too long") println("eirLength too long")
} }
@ -68,7 +68,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
case 0x06, 0x07: case 0x06, 0x07:
// 128-bit Service Class UUID // 128-bit Service Class UUID
case 0x08, 0x09: case 0x08, 0x09:
if _debug { if debug {
println("local name", string(a.hci.advData.eirData[i+2:i+1+l])) println("local name", string(a.hci.advData.eirData[i+2:i+1+l]))
} }
@ -103,7 +103,7 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) error {
return nil return nil
} }
if _debug && (time.Now().UnixNano()-lastUpdate)/int64(time.Second) > 1 { if debug && (time.Now().UnixNano()-lastUpdate)/int64(time.Second) > 1 {
println("still scanning...") println("still scanning...")
lastUpdate = time.Now().UnixNano() lastUpdate = time.Now().UnixNano()
} }
@ -136,7 +136,7 @@ type Address struct {
// Connect starts a connection attempt to the given peripheral device address. // Connect starts a connection attempt to the given peripheral device address.
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) { func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
if _debug { if debug {
println("Connect") println("Connect")
} }
@ -221,7 +221,7 @@ type deviceInternal struct {
// Disconnect from the BLE device. // Disconnect from the BLE device.
func (d Device) Disconnect() error { func (d Device) Disconnect() error {
if _debug { if debug {
println("Disconnect") println("Disconnect")
} }
if err := d.adapter.hci.disconnect(d.handle); err != nil { if err := d.adapter.hci.disconnect(d.handle); err != nil {
@ -381,7 +381,7 @@ func (a *Advertisement) Start() error {
for { for {
if err := a.adapter.att.poll(); err != nil { if err := a.adapter.att.poll(); err != nil {
// TODO: handle error // TODO: handle error
if _debug { if debug {
println("error polling while advertising:", err.Error()) println("error polling while advertising:", err.Error())
} }
} }

View file

@ -52,7 +52,7 @@ func (s DeviceService) UUID() UUID {
// Passing a nil slice of UUIDs will return a complete list of // Passing a nil slice of UUIDs will return a complete list of
// services. // services.
func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) { func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
if _debug { if debug {
println("DiscoverServices") println("DiscoverServices")
} }
@ -67,7 +67,7 @@ func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
return nil, err return nil, err
} }
if _debug { if debug {
println("found d.adapter.att.services", len(d.adapter.att.services)) println("found d.adapter.att.services", len(d.adapter.att.services))
} }
@ -143,7 +143,7 @@ func (c DeviceCharacteristic) UUID() UUID {
// Passing a nil slice of UUIDs will return a complete // Passing a nil slice of UUIDs will return a complete
// list of characteristics. // list of characteristics.
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) { func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
if _debug { if debug {
println("DiscoverCharacteristics") println("DiscoverCharacteristics")
} }
@ -165,7 +165,7 @@ func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteri
return nil, err return nil, err
} }
if _debug { if debug {
println("found s.device.adapter.att.characteristics", len(s.device.adapter.att.characteristics)) println("found s.device.adapter.att.characteristics", len(s.device.adapter.att.characteristics))
} }
@ -244,7 +244,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
switch { switch {
case callback == nil: case callback == nil:
// disable notifications // disable notifications
if _debug { if debug {
println("disabling notifications") println("disabling notifications")
} }
@ -254,7 +254,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
} }
default: default:
// enable notifications // enable notifications
if _debug { if debug {
println("enabling notifications") println("enabling notifications")
} }

View file

@ -49,14 +49,14 @@ func (a *Adapter) AddService(service *Service) error {
service.Characteristics[i].Handle.value = service.Characteristics[i].Value service.Characteristics[i].Handle.value = service.Characteristics[i].Value
} }
if _debug { if debug {
println("added characteristic", charHandle, valueHandle, service.Characteristics[i].UUID.String()) println("added characteristic", charHandle, valueHandle, service.Characteristics[i].UUID.String())
} }
a.att.addLocalCharacteristic(charHandle, service.Characteristics[i].Flags, valueHandle, service.Characteristics[i].UUID, service.Characteristics[i].Handle) a.att.addLocalCharacteristic(charHandle, service.Characteristics[i].Flags, valueHandle, service.Characteristics[i].UUID, service.Characteristics[i].Handle)
} }
if _debug { if debug {
println("added service", serviceHandle, endHandle, service.UUID.String()) println("added service", serviceHandle, endHandle, service.UUID.String())
} }

View file

@ -176,7 +176,7 @@ func (h *hci) poll() error {
done, err := h.processPacket(i) done, err := h.processPacket(i)
switch { switch {
case err == ErrHCIUnknown || err == ErrHCIInvalidPacket || err == ErrHCIUnknownEvent: case err == ErrHCIUnknown || err == ErrHCIInvalidPacket || err == ErrHCIUnknownEvent:
if _debug { if debug {
println("hci error:", err.Error()) println("hci error:", err.Error())
} }
i = 0 i = 0
@ -203,7 +203,7 @@ func (h *hci) processPacket(i int) (bool, error) {
case pktlen > len(h.buf): case pktlen > len(h.buf):
return true, ErrHCIInvalidPacket return true, ErrHCIInvalidPacket
case i >= (hciACLLenPos + pktlen): case i >= (hciACLLenPos + pktlen):
if _debug { if debug {
println("hci acl data:", i, hex.EncodeToString(h.buf[:1+hciACLLenPos+pktlen])) println("hci acl data:", i, hex.EncodeToString(h.buf[:1+hciACLLenPos+pktlen]))
} }
return true, h.handleACLData(h.buf[1 : 1+hciACLLenPos+pktlen]) return true, h.handleACLData(h.buf[1 : 1+hciACLLenPos+pktlen])
@ -218,7 +218,7 @@ func (h *hci) processPacket(i int) (bool, error) {
case pktlen > len(h.buf): case pktlen > len(h.buf):
return true, ErrHCIInvalidPacket return true, ErrHCIInvalidPacket
case i >= (hciEvtLenPos + pktlen): case i >= (hciEvtLenPos + pktlen):
if _debug { if debug {
println("hci event data:", i, hex.EncodeToString(h.buf[:1+hciEvtLenPos+pktlen])) println("hci event data:", i, hex.EncodeToString(h.buf[:1+hciEvtLenPos+pktlen]))
} }
return true, h.handleEventData(h.buf[1 : 1+hciEvtLenPos+pktlen]) return true, h.handleEventData(h.buf[1 : 1+hciEvtLenPos+pktlen])
@ -226,7 +226,7 @@ func (h *hci) processPacket(i int) (bool, error) {
} }
default: default:
if _debug { if debug {
println("unknown packet data:", h.buf[0]) println("unknown packet data:", h.buf[0])
} }
return true, ErrHCIUnknown return true, ErrHCIUnknown
@ -364,7 +364,7 @@ func (h *hci) sendCommand(opcode uint16) error {
} }
func (h *hci) sendCommandWithParams(opcode uint16, params []byte) error { func (h *hci) sendCommandWithParams(opcode uint16, params []byte) error {
if _debug { if debug {
println("hci send command", opcode, hex.EncodeToString(params)) println("hci send command", opcode, hex.EncodeToString(params))
} }
@ -395,7 +395,7 @@ func (h *hci) sendCommandWithParams(opcode uint16, params []byte) error {
} }
func (h *hci) sendWithoutResponse(opcode uint16, params []byte) error { func (h *hci) sendWithoutResponse(opcode uint16, params []byte) error {
if _debug { if debug {
println("hci send without response command", opcode, hex.EncodeToString(params)) println("hci send without response command", opcode, hex.EncodeToString(params))
} }
@ -423,7 +423,7 @@ func (h *hci) sendAclPkt(handle uint16, cid uint8, data []byte) error {
copy(h.buf[9:], data) copy(h.buf[9:], data)
if _debug { if debug {
println("hci send acl data", handle, cid, hex.EncodeToString(h.buf[:9+len(data)])) println("hci send acl data", handle, cid, hex.EncodeToString(h.buf[:9+len(data)]))
} }
@ -479,7 +479,7 @@ func (h *hci) handleACLData(buf []byte) error {
case attCID: case attCID:
if aclFlags == 0x01 { if aclFlags == 0x01 {
// TODO: use buffered packet // TODO: use buffered packet
if _debug { if debug {
println("WARNING: att.handleACLData needs buffered packet") println("WARNING: att.handleACLData needs buffered packet")
} }
return h.att.handleData(aclHdr.handle&0x0fff, buf[8:aclHdr.len+8]) return h.att.handleData(aclHdr.handle&0x0fff, buf[8:aclHdr.len+8])
@ -487,7 +487,7 @@ func (h *hci) handleACLData(buf []byte) error {
return h.att.handleData(aclHdr.handle&0x0fff, buf[8:aclHdr.len+8]) return h.att.handleData(aclHdr.handle&0x0fff, buf[8:aclHdr.len+8])
} }
default: default:
if _debug { if debug {
println("unknown acl data cid", aclHdr.cid) println("unknown acl data cid", aclHdr.cid)
} }
} }
@ -501,7 +501,7 @@ func (h *hci) handleEventData(buf []byte) error {
switch evt { switch evt {
case evtDisconnComplete: case evtDisconnComplete:
if _debug { if debug {
println("evtDisconnComplete") println("evtDisconnComplete")
} }
@ -511,7 +511,7 @@ func (h *hci) handleEventData(buf []byte) error {
return h.leSetAdvertiseEnable(true) return h.leSetAdvertiseEnable(true)
case evtEncryptionChange: case evtEncryptionChange:
if _debug { if debug {
println("evtEncryptionChange") println("evtEncryptionChange")
} }
@ -524,7 +524,7 @@ func (h *hci) handleEventData(buf []byte) error {
h.cmdResponse = buf[:0] h.cmdResponse = buf[:0]
} }
if _debug { if debug {
println("evtCmdComplete", h.cmdCompleteOpcode, h.cmdCompleteStatus) println("evtCmdComplete", h.cmdCompleteOpcode, h.cmdCompleteStatus)
} }
@ -533,7 +533,7 @@ func (h *hci) handleEventData(buf []byte) error {
case evtCmdStatus: case evtCmdStatus:
h.cmdCompleteStatus = buf[2] h.cmdCompleteStatus = buf[2]
h.cmdCompleteOpcode = binary.LittleEndian.Uint16(buf[4:]) h.cmdCompleteOpcode = binary.LittleEndian.Uint16(buf[4:])
if _debug { if debug {
println("evtCmdStatus", h.cmdCompleteOpcode, h.cmdCompleteOpcode, h.cmdCompleteStatus) println("evtCmdStatus", h.cmdCompleteOpcode, h.cmdCompleteOpcode, h.cmdCompleteStatus)
} }
@ -542,17 +542,17 @@ func (h *hci) handleEventData(buf []byte) error {
return nil return nil
case evtNumCompPkts: case evtNumCompPkts:
if _debug { if debug {
println("evtNumCompPkts") println("evtNumCompPkts")
} }
case evtLEMetaEvent: case evtLEMetaEvent:
if _debug { if debug {
println("evtLEMetaEvent") println("evtLEMetaEvent")
} }
switch buf[2] { switch buf[2] {
case leMetaEventConnComplete, leMetaEventEnhancedConnectionComplete: case leMetaEventConnComplete, leMetaEventEnhancedConnectionComplete:
if _debug { if debug {
println("leMetaEventConnComplete") println("leMetaEventConnComplete")
} }
@ -575,13 +575,13 @@ func (h *hci) handleEventData(buf []byte) error {
copy(h.advData.peerBdaddr[0:], buf[6:]) copy(h.advData.peerBdaddr[0:], buf[6:])
h.advData.eirLength = buf[12] h.advData.eirLength = buf[12]
h.advData.rssi = 0 h.advData.rssi = 0
if _debug { if debug {
println("leMetaEventAdvertisingReport", plen, h.advData.numReports, println("leMetaEventAdvertisingReport", plen, h.advData.numReports,
h.advData.typ, h.advData.peerBdaddrType, h.advData.eirLength) h.advData.typ, h.advData.peerBdaddrType, h.advData.eirLength)
} }
if int(13+h.advData.eirLength+1) > len(buf) || h.advData.eirLength > 31 { if int(13+h.advData.eirLength+1) > len(buf) || h.advData.eirLength > 31 {
if _debug { if debug {
println("invalid packet length", h.advData.eirLength, len(buf)) println("invalid packet length", h.advData.eirLength, len(buf))
} }
return ErrHCIInvalidPacket return ErrHCIInvalidPacket
@ -596,12 +596,12 @@ func (h *hci) handleEventData(buf []byte) error {
return nil return nil
case leMetaEventLongTermKeyRequest: case leMetaEventLongTermKeyRequest:
if _debug { if debug {
println("leMetaEventLongTermKeyRequest") println("leMetaEventLongTermKeyRequest")
} }
case leMetaEventRemoteConnParamReq: case leMetaEventRemoteConnParamReq:
if _debug { if debug {
println("leMetaEventRemoteConnParamReq") println("leMetaEventRemoteConnParamReq")
} }
@ -623,27 +623,27 @@ func (h *hci) handleEventData(buf []byte) error {
return h.sendWithoutResponse(ogfLECtrl<<10|ocfLEParamRequestReply, b[:]) return h.sendWithoutResponse(ogfLECtrl<<10|ocfLEParamRequestReply, b[:])
case leMetaEventConnectionUpdateComplete: case leMetaEventConnectionUpdateComplete:
if _debug { if debug {
println("leMetaEventConnectionUpdateComplete") println("leMetaEventConnectionUpdateComplete")
} }
case leMetaEventReadLocalP256Complete: case leMetaEventReadLocalP256Complete:
if _debug { if debug {
println("leMetaEventReadLocalP256Complete") println("leMetaEventReadLocalP256Complete")
} }
case leMetaEventGenerateDHKeyComplete: case leMetaEventGenerateDHKeyComplete:
if _debug { if debug {
println("leMetaEventGenerateDHKeyComplete") println("leMetaEventGenerateDHKeyComplete")
} }
case leMetaEventDataLengthChange: case leMetaEventDataLengthChange:
if _debug { if debug {
println("leMetaEventDataLengthChange") println("leMetaEventDataLengthChange")
} }
default: default:
if _debug { if debug {
println("unknown metaevent", buf[2], buf[3], buf[4], buf[5]) println("unknown metaevent", buf[2], buf[3], buf[4], buf[5])
} }

View file

@ -2,4 +2,4 @@
package bluetooth package bluetooth
var _debug = false var debug = false