-
Notifications
You must be signed in to change notification settings - Fork 19
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
BlueGiga Bluetooth Stack #145
Conversation
There are some conflicts to resolve. The main branch has evolved meanwhile. As a git newbie: When you locally operate on your cloned repository ... it becomes stale when you do not pull updates from the server. When you create a new branch, it will create it locally and not from the remote latest & greatest repository. |
Good idea to use the lib, but why is then this gigantic .cs file still in the code? is that a leftover? |
Regards the addition of the semaphore.... ... I would love to work without multi-threading constructs in the higher level code but I am not totally sure how to fix the situation. We have a method for request & response queries against the hub. I use this in all synchronous activities ... but that is more a synchronous from a logical but not technical perspective. I have the feeling we pull in here the technical layer into the business logic layer. So far I understand from your comments, that BlueGiga is swallowing messages when too many are issued. Or does it swallow responses? Does maybe a inbox/outbox system within the BlueGiga stack works for the messages? (so only a single thread operates the BlueGiga adapter)? |
The big BGLib.cs is deleted now. Wasn't really not needed anymore. I did take it out of the project by "Exclude form project" in VS Studio, but didn't really delete it... |
Regards the addition of the semaphore in DiscoverPorts.cs:
I don't feel that this change is a technical one; we just make sure that we receive an answer/notification before sending the next request. Should really make no problems whatever BLE-implementation is used. The problem is more the way the LEGO-Hub works by just using one single characteristic for everything. So this one characteristic has to handle it all. And it seems that the Hub is very limmited in buffering commands (see also https://lego.github.io/lego-ble-wireless-protocol-docs/index.html#document-4-Buffering); so implementing this wait-for-notification-strategy here in DiscoverPorts is actually a working solution :-) |
Regarding conflicitng files: |
Architecturally the Lego Wireless Protocol is a message based protocols in both directions. There are messages send to it without confirmation and the hub sends messages without being asked for. They form often but not always request-response pairs. Therefore building the system in a request-response pattern in general is not right and also not the right assumption. Building a pattern where we await some response will not work generally. But indeed, I see your point. There is a load problem. I have however, another suspicion. While I worked with the WebBluetooth for this project, I realized, that the WebBluetooth adapter is just too slow during its setup process and WinRT was so fast it was able to work with it. Can it be, that WinRT Bluetooth is just fast enough while the BlueGiga stack has problems processing the load? Anyway. When I have time during the week, I will try to write a DiscoverPort method which uses the function I gave you. Generally, I hoped that the event based system is working but I think this method can be rewritten to be more request/response like. |
I did some tests today with the "Load" of the BlueGigadevice: I also added a check into BGLibExt (in a local copy of the repos) whether the sending of the RequestMessages returns an error from the Hub - no errors in sending: all sends are acknowledged by the Hub.
But these notifications ( step 3. above) are NOT SEND from the Hub for all messages send in step 1. I know this is not a real proof (for that I would need a Bluetooth/BLE-sniffer), but I'm nearly convinced that the Hubs are the problem, not the Bluegiga-adapter itself nor the BGLibExt.NET-library. It handles a real big load during advertisment and also receives all the initially port-announcemnts which the Hub fires directly after connect (see DumpStaticPortInfos.cs where you delay for 2 seconds after the Connect). So the Hub "needs more time" :-) to react on the requestMessages; otherwise it will skip some of them. Another indication for that: Playing arround in DiscoverPorts.cs by inserting some async waits between the _protocol.SendMessageAsync()-calls also makes the problem better (most times the number of missing notifications gets to 10% of the requested). But playing arround with wait-times is hard and errorprone (from my experience). It's better so tell the system for what condition is has to wait (waiting for a free ressource) - and that is exactly what semaphores do. |
I tend to agree with your analysis that Blue giga is not the problem. I do not believe in some WinRT magic. That interface is too general purpose to do some magic. It might be slow or fast, might implement some stack better or worse, but it will not do something too specific like awaiting something. Like stated, I will rewrite a more synchronous DiscoverPort algorithm. That should fix this. Most parts of the library are very coordinated and not choreographed like this. If it turns out, the hubs are indeed to slow, than we will add throttling and a inbox/outbox behavior to the protocol and make the behavior configurable. Thanks for all the efforts. Your work and progress is amazing. |
By the way your English is absolutely fine. Just do not take mine as a reference. I am also a German. With terrible grades in English but an English speaking workplace and wife. That helps 🙂 |
Interesting to hear you're also German :-) We should switch language on this conversation :-) - No,No... :-) Best regards |
I am not afraid of locking semaphores but of mental complexity of the code. Let me do the discoverports code in a library style and then we see how it fits |
I created a pull request #152 .. have a look whether that works for you. |
As also written in #152 I have tested the new version of DiscoverPorts.cs successfuly. |
I merged this now. There are still some formatting things, but I will clean this out to avoid the roundtrips |
@tthiery You still found formatting isues. Can you tell me which kind they were? I used an .editorconfig inside Visual Studio (I .gitignored it) coming directly from MS for which they say it supports the defacto-standard for C#-formatting. And VS didn't report any warning at all to me. Do you also use a .editorconfig-file ? (the little one in the root of your repos can't be all :-) ) |
Not C# formatting. There were two files touched by this PR due to newlines and another one for a unused using directive. Unfortunately, I could not edit the PR so the changes are in the git log. And the readme need some different writing 🙂 All good. No worries. |
Regards editorconfig: yeahish. I strictly follow default C# guidelines. Basically what a vanilla VS or VS Code does for C#. So I do not see much purpose in an editorconfig. How did it help you? |
Regards .editorconfig:
in the .editorconfig helped me a lot, because on build (and also during coding, clear:-) I received a lot of warnings (you remember my first commits) which reminded me to reformat. |
I've now changed the BlueGiga-implementation to use the BGLibExt-nuget-package instead of the "old" and unmaintained bglib.cs I used before. This makes the implementation much leaner :-)
Also I used now arguments in the examples and in the CLI.
I've found two strange behaviours / errors:
This throwed an exception when called with -p X option (PortId) but no device was attached to that port; see my suggestion of handling that in the file.
This lost a lot of messages; I think it is the same problem as with Hub_Properties, but the solution is different. I added a semaphore to make sure that every send first receives a message before firing the next send. I really did a lot of tests with that in the CLI-tool. With BleuGiga it works now perfect. But please test it with WinRT; should work, but I cannot test...
In the CLI I changed the way you use the Boolean option --trace. I feel it is very unusual to have to append a "true" after it. The option.HasValue() return true, if the option itself is given --> enought to set it to true; The Command.OptionType if --trace is therefore set to "NoValue".
Have fun in reviewing!
Closes #77