func (c *conn) write(cid int, b []byte) (int, error) {
	flag := uint8(0) // ACL data continuation flag
	tlen := len(b)   // Total length of the l2cap payload

	logger.Info("l2cap", "W", fmt.Sprintf("[% X]", b))
	w := append(
		[]byte{
			0,    // packet type
			0, 0, // attr
			0, 0, // dlen
			uint8(tlen), uint8(tlen >> 8), // l2cap header
			uint8(cid), uint8(cid >> 8), // l2cap header
		}, b...)

	n := 4 + tlen // l2cap header + l2cap payload
	for n > 0 {
		dlen := n
		if dlen > c.hci.bufSize {
			dlen = c.hci.bufSize
		}
		w[0] = 0x02 // packetTypeACL
		w[1] = uint8(c.attr)
		w[2] = uint8(c.attr>>8) | flag
		w[3] = uint8(dlen)
		w[4] = uint8(dlen >> 8)

		// make sure we don't send more buffers than the controller can handdle
		c.hci.bufCnt <- struct{}{}

		c.hci.d.Write(w[:5+dlen])
		w = w[dlen:] // advance the pointer to the next segment, if any.
		flag = 0x10  // the rest of iterations attr continued segments, if any.
		n -= dlen
	}

	return len(b), nil
}
// write writes the l2cap payload to the controller.
// It first prepend the l2cap header (4-bytes), and diassemble the payload
// if it is larger than the HCI LE buffer size that the conntroller can support.
write writes the l2cap payload to the controller.
It first prepend the l2cap header (4-bytes), and diassemble the payload
if it is larger than the HCI LE buffer size that the conntroller can support.
// write writes the l2cap payload to the controller . // It first prepend the l2cap header ( 4 - bytes ) and diassemble the payload // if it is larger than the HCI LE buffer size that the conntroller can support .
func (c *conn) write(cid int, b []byte) (int, error) {
	flag := uint8(0) 
	tlen := len(b)   
	logger.Info("l2cap", "W", fmt.Sprintf("[% X]", b))
	w := append(
		[]byte{
			0,    
			0, 0, 
			0, 0, 
			uint8(tlen), uint8(tlen >> 8), 
			uint8(cid), uint8(cid >> 8), 
		}, b...)
	n := 4 + tlen  + l2cap payload
	for n > 0 {
		dlen := n
		if dlen > c.hci.bufSize {
			dlen = c.hci.bufSize
		}
		w[0] = 0x02 
		w[1] = uint8(c.attr)
		w[2] = uint8(c.attr>>8) | flag
		w[3] = uint8(dlen)
		w[4] = uint8(dlen >> 8)
		c.hci.bufCnt <- struct{}{}
		c.hci.d.Write(w[:5+dlen])
		w = w[dlen:] 
		flag = 0x10  
		n -= dlen
	}
	return len(b), nil
}
func ( c * conn ) write ( cid int , b [ ] byte ) ( int , error ) { flag := uint8 ( 0 ) tlen := len ( b ) logger . Info ( " " , " " , fmt . Sprintf ( " " , b ) ) w := append ( [ ] byte { 0 , 0 , 0 , 0 , 0 , uint8 ( tlen ) , uint8 ( tlen >> 8 ) , uint8 ( cid ) , uint8 ( cid >> 8 ) , } , b ... ) n := 4 + tlen for n > 0 { dlen := n if dlen > c . hci . bufSize { dlen = c . hci . bufSize } w [ 0 ] = 0x02 w [ 1 ] = uint8 ( c . attr ) w [ 2 ] = uint8 ( c . attr >> 8 ) | flag w [ 3 ] = uint8 ( dlen ) w [ 4 ] = uint8 ( dlen >> 8 ) c . hci . bufCnt <- struct { } { } c . hci . d . Write ( w [ : 5 + dlen ] ) w = w [ dlen : ] flag = 0x10 n -= dlen } return len ( b ) , nil }
write writes the l2cap payload to the controller.
write writes the l2cap payload to the controller .
// ACL data continuation flag // Total length of the l2cap payload // packet type // attr // dlen // l2cap header // l2cap header // l2cap header + l2cap payload // packetTypeACL // make sure we don't send more buffers than the controller can handdle // advance the pointer to the next segment, if any. // the rest of iterations attr continued segments, if any.