-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write long characteristic #83
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,6 +252,26 @@ func (p *Client) WriteCharacteristic(c *ble.Characteristic, v []byte, noRsp bool | |
return p.ac.Write(c.ValueHandle, v) | ||
} | ||
|
||
// WriteLongCharacteristic writes a characteristic value that is longer than the MTU to a server. [Vol 3, Part F, 3.4.6] | ||
func (p *Client) WriteLongCharacteristic(c *ble.Characteristic, v []byte) error { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
offset := 0 | ||
prepareWriteLength := 0 | ||
|
||
for offset < len(v) { | ||
prepareWriteLength = MinInt( len(v) - offset, p.conn.TxMTU()-5 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a function here adds a bit of complexity when an Suggestion:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could also avoid having to track maxLength := p.conn.TxMTU() - 5
for len(v) > 0 {
length := len(v)
if length > maxLength {
length = maxLength
}
...
v = v[length:]
} |
||
value := v[offset:offset+prepareWriteLength] | ||
if _, _, _, err := p.ac.PrepareWrite(c.ValueHandle, uint16(offset), value ); err != nil { | ||
return err | ||
} | ||
offset += prepareWriteLength | ||
} | ||
|
||
return p.ac.ExecuteWrite(0x01) // flags: 0x00=cancel all, 0x01=write all | ||
} | ||
|
||
// ReadDescriptor reads a characteristic descriptor from a server. [Vol 3, Part G, 4.12.1] | ||
func (p *Client) ReadDescriptor(d *ble.Descriptor) ([]byte, error) { | ||
p.Lock() | ||
|
@@ -401,3 +421,10 @@ type sub struct { | |
nHandler ble.NotificationHandler | ||
iHandler ble.NotificationHandler | ||
} | ||
|
||
func MinInt( a int, b int) int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are ok with my suggestion above, this function can be removed. |
||
if a < b { | ||
return a | ||
} | ||
return b | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than panic, this should return an error. Forcing a panic upon a calling program is bit jarring for the caller to deal with.