diff --git a/adapter_ninafw.go b/adapter_ninafw.go index 43eebe3..3d9098a 100644 --- a/adapter_ninafw.go +++ b/adapter_ninafw.go @@ -105,6 +105,9 @@ func newBLEStack(uart *machine.UART) (*hci, *att) { a := newATT(h) h.att = a + l := newL2CAP(h) + h.l2cap = l + return h, a } diff --git a/att_ninafw.go b/att_ninafw.go index b09e228..cc51927 100644 --- a/att_ninafw.go +++ b/att_ninafw.go @@ -12,9 +12,6 @@ import ( ) const ( - attCID = 0x0004 - bleCTL = 0x0008 - attOpError = 0x01 attOpMTUReq = 0x02 attOpMTUResponse = 0x03 @@ -261,6 +258,7 @@ type att struct { lastErrorHandle uint16 lastErrorCode uint8 mtu uint16 + maxMTU uint16 services []rawService characteristics []rawCharacteristic descriptors []rawDescriptor @@ -284,6 +282,7 @@ func newATT(hci *hci) *att { lastHandle: 0x0001, attributes: []rawAttribute{}, localServices: []rawService{}, + maxMTU: 23, } } @@ -408,12 +407,19 @@ func (a *att) writeReq(connectionHandle, valueHandle uint16, data []byte) error func (a *att) mtuReq(connectionHandle, mtu uint16) error { if debug { - println("att.mtuReq:", connectionHandle) + println("att.mtuReq:", connectionHandle, mtu) } a.busy.Lock() defer a.busy.Unlock() + if mtu > a.maxMTU { + mtu = a.maxMTU + } + + // save mtu for connection + a.mtu = mtu + var b [3]byte b[0] = attOpMTUReq binary.LittleEndian.PutUint16(b[1:], mtu) @@ -425,6 +431,12 @@ func (a *att) mtuReq(connectionHandle, mtu uint16) error { return a.waitUntilResponse() } +func (a *att) setMaxMTU(mtu uint16) error { + a.maxMTU = mtu + + return nil +} + func (a *att) sendReq(handle uint16, data []byte) error { a.clearResponse() @@ -1050,17 +1062,21 @@ func (a *att) poll() error { return nil } -func (a *att) addConnection(handle uint16) { +func (a *att) addConnection(handle uint16) error { a.connections = append(a.connections, handle) + + return nil } -func (a *att) removeConnection(handle uint16) { +func (a *att) removeConnection(handle uint16) error { for i := range a.connections { if a.connections[i] == handle { a.connections = append(a.connections[:i], a.connections[i+1:]...) - return + break } } + + return nil } func (a *att) addLocalAttribute(typ attributeType, parent uint16, uuid UUID, permissions CharacteristicPermissions, value []byte) uint16 { diff --git a/hci_ninafw.go b/hci_ninafw.go index d0a00f8..a3402c7 100644 --- a/hci_ninafw.go +++ b/hci_ninafw.go @@ -87,6 +87,11 @@ const ( const ( hciACLLenPos = 4 hciEvtLenPos = 2 + + attCID = 0x0004 + bleCTL = 0x0008 + signalingCID = 0x0005 + securityCID = 0x0006 ) var ( @@ -113,6 +118,8 @@ type leConnectData struct { role uint8 peerBdaddrType uint8 peerBdaddr [6]uint8 + interval uint16 + timeout uint16 } type hci struct { @@ -120,6 +127,7 @@ type hci struct { softCTS machine.Pin softRTS machine.Pin att *att + l2cap *l2cap buf []byte address [6]byte cmdCompleteOpcode uint16 @@ -128,6 +136,7 @@ type hci struct { scanning bool advData leAdvertisingReport connectData leConnectData + maxPkt uint8 } func newHCI(uart *machine.UART) *hci { @@ -263,6 +272,26 @@ func (h *hci) setLeEventMask(eventMask uint64) error { return h.sendCommandWithParams(ogfLECtrl<