Skip to content
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

hci: implement write handler #236

Merged
merged 2 commits into from
Feb 2, 2024
Merged

hci: implement write handler #236

merged 2 commits into from
Feb 2, 2024

Conversation

deadprogram
Copy link
Member

This PR extends the hci to implements the WriteHandler on Characteristic.

Note this PR depends on #235 and will need to be rebased once that PR is merged.

@deadprogram
Copy link
Member Author

This device has been working without error for over 24 hours now on this PR and #235

https://cdn.masto.host/socialtinygoorg/media_attachments/files/111/833/433/634/525/388/original/59a4c59d4e5e9f58.mp4

@@ -22,6 +22,7 @@ type hciAdapter struct {

connectedDevices []Device
notificationsStarted bool
charWriteHandlers []charWriteHandler

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why this isn't a Go map, keyed by handle uint16?

   type callback func(connection Connection, offset int, value []byte)
   map[uint16]callback

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the implementation from https://github.com/tinygo-org/bluetooth/blob/dev/adapter_sd.go#L49 to keep things uniform.

return nil

case (time.Now().UnixNano()-start)/int64(time.Second) > defaultTimeoutSeconds:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is outside the scope of the PR, but is this equivalent code for the time math?

timeout := time.Now().Add(defaultTimeoutSeconds)
for {
    ...
    switch {
        case time.Now().After(timeout):
            return ErrATTTimeout

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess it might be a little faster due to the integer part of the calculation? I also copied that from elsewhere, so perhaps @aykevl can elaborate on why (or why not?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly no idea, you'd have to look at the source of .Add or do a benchmark to check.
In general, when in doubt (or without a benchmark) I recommend taking the simpler approach - in this case, time.Now().Add.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to replace some of these sprinkled thru my code...

@bgould bgould merged commit 9a53d2a into dev Feb 2, 2024
5 checks passed
@bgould bgould deleted the hci_write_handler branch February 2, 2024 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants