Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ST25TB poller refining + write support #3239

Merged

Conversation

augustozanellato
Copy link
Contributor

@augustozanellato augustozanellato commented Nov 21, 2023

What's new

  • reworked async poller
  • added sync poller
  • added write support

This PR should be ~same to #3185 but based on the improved NFC APIs

Verification

  • Read a ST25TB and verify it's still read correctly

Checklist (For Reviewer)

  • PR has description of feature/bug or link to Confluence/Jira task
  • Description contains actions to verify feature/bugfix
  • I've built this code, uploaded it to the device and verified feature/bugfix

Copy link
Member

@gornekich gornekich left a comment

Choose a reason for hiding this comment

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

Hello @augustozanellato , thanks for PR!
First of all - everything works fine. However there are some things I want to discuss.

It would be nice if all pollers in NFC library looks similarly. That would help to maintain NFC subsystem a lot. I left some suggestions in code review.

Also I want to discuss poller states. Since your poller becomes bigger and you already added write capabilities in sync API, I suggest expanding poller itself. I would make these states in poller:

  • Idle - reset St25tbPoller instance
  • Select - selects card, emit "Ready" event with card type in event data
  • Request mode - emit "RequestMode" event and receive mode in event data. Modes are St25tbPollerModeRead and St25tbPollerModeWrite
  • Read - reads tag and switches to Fail or Success states
  • Write - emits "RequestBlockData" event and receive block and block number in event data. Performs write command and switches to Fail or Success states.
  • Fail - emits "Fail" event with error code in event data. Ends with furi_delay_ms(100) to switch context and switches to Idle state.
  • Success - emits "Success" event with error code in event data. Ends with furi_delay_ms(100) to switch context and switches to Idle state.

In this case your sync API would be easy to write and we will have easier way to implement "write" in NFC application. You can see how it's done in mf classic poller for example.

Please, let me know what you think about this. We can do this step by step: merge this PR first and then refactore st25tb poller in future

@gornekich
Copy link
Member

Also please don't forget to lint sources :)
./fbt format

@augustozanellato
Copy link
Contributor Author

Also please don't forget to lint sources :) ./fbt format

I usually have format on save enabled, I'll figure out why it didn't work this time.

It would be nice if all pollers in NFC library looks similarly.

The thing that made me architect this poller different from the other ones was that this one is kinda an exception from the standard poller in the sense that it doesn't have either a parent or any children protocols, but has to implement both "low level" (anticollision, selection etc) and "high level" (data reading and writing).

@augustozanellato
Copy link
Contributor Author

Please, let me know what you think about this. We can do this step by step: merge this PR first and then refactore st25tb poller in future

I think we should just iterate on this pr until we get to a decent shape without breaking API even more times :)

@augustozanellato
Copy link
Contributor Author

It took less time than expected.
There are two deviations from your suggestions:

  • there isn't an "idle" state, i think it'd be kinda useless, feel free to correct me on that;
  • there isn't a RequestBlockData event because I made RequestMode handle mode arguments.

@gornekich gornekich self-assigned this Nov 23, 2023
@gornekich gornekich added the NFC NFC-related label Nov 23, 2023
gornekich
gornekich previously approved these changes Nov 23, 2023
Copy link
Member

@gornekich gornekich left a comment

Choose a reason for hiding this comment

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

Well done, thank you! I think poller looks much better now

@augustozanellato
Copy link
Contributor Author

Well done, thank you! I think poller look much better now

Thanks for your suggestions :)

@gornekich
Copy link
Member

there isn't an "idle" state, i think it'd be kinda useless, feel free to correct me on that;

No problems with that. Usually I add Idle state to set all variables in poller instance to default values. Since there are not so many variables in this poller, Idle state is useless. If the poller will grow, we can add this state if necessary.

there isn't a RequestBlockData event because I made RequestMode handle mode arguments.

I was thinking about different Write mode behavior - to call "RequestBlockData" event, write blocks until upper layer provides data. When upper layer stops providing data poller switches to Success state. This is quite flexible behavior which allows you to write not only one block after anticollision, but perform multiple write operations in one communication session.
I don't see any reasons to change it right. We can return to this later, if we want to add "Write" option in NFC application. These changes will cause minor API update.

@augustozanellato
Copy link
Contributor Author

there isn't an "idle" state, i think it'd be kinda useless, feel free to correct me on that;

No problems with that. Usually I add Idle state to set all variables in poller instance to default values. Since there are not so many variables in this poller, Idle state is useless. If the poller will grow, we can add this state if necessary.

Yeah that makes sense.

there isn't a RequestBlockData event because I made RequestMode handle mode arguments.

I was thinking about different Write mode behavior - to call "RequestBlockData" event, write blocks until upper layer provides data. When upper layer stops providing data poller switches to Success state. This is quite flexible behavior which allows you to write not only one block after anticollision, but perform multiple write operations in one communication session. I don't see any reasons to change it right. We can return to this later, if we want to add "Write" option in NFC application. These changes will cause minor API update.

I didn't think about that, but that could be solved by having st25tb_poller_success_handler set poller state to St25tbPollerStateRequestMode so that in a communication session an arbitrary number of operations can be done. Tell me what do you think about this paradigm.

@gornekich
Copy link
Member

gornekich commented Nov 23, 2023

I didn't think about that, but that could be solved by having st25tb_poller_success_handler set poller state to St25tbPollerStateRequestMode so that in a communication session an arbitrary number of operations can be done. Tell me what do you think about this paradigm.

Yes, that's actually a good solution. Will you do that in this PR?

@augustozanellato
Copy link
Contributor Author

Yes, that's actually a good solution. Will you do that in this PR?

Done, I was just waiting for some feedback :)

@gornekich
Copy link
Member

Perfect, thanks!

@augustozanellato
Copy link
Contributor Author

The failing unit test is subghz related, I don't think that could be directly related to this pr 🤔

@gornekich
Copy link
Member

Yes, the problem is not this PR

@augustozanellato
Copy link
Contributor Author

Looks like extra - read specific card type - st25tb is now broken, I'll hook up a debugger and figure out why.

* type wasn't properly set on ready event
* sending NfcCustomEventPollerFailure on St25tbPollerEventTypeFailure caused poller to being freed and ultimately resulted in a thread crash
@augustozanellato
Copy link
Contributor Author

Bug fixed, while investigating that one I also found another one, more details in commit message.
PR should now be ready to review and merge.

@skotopes skotopes merged commit c1e0d02 into flipperdevices:dev Dec 1, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
NFC NFC-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants