Skip to content

pkg/semtech-loramac: refactor API and make it thread-safe#8798

Merged
jia200x merged 3 commits intoRIOT-OS:masterfrom
aabadie:hackathon_ietf
Apr 19, 2018
Merged

pkg/semtech-loramac: refactor API and make it thread-safe#8798
jia200x merged 3 commits intoRIOT-OS:masterfrom
aabadie:hackathon_ietf

Conversation

@aabadie
Copy link
Copy Markdown
Contributor

@aabadie aabadie commented Mar 19, 2018

Contribution description

This PR is a refactoring of the Semtech loramac package: the idea was initially to introduce a dedicated struct for handling the mac in a thread-safe way. The immediate consequence is a global API change of the package but thanks to this, any access to the internal mac can be protected using mutexes and data exchange is simplified between the upstream code and the RIOT adaption code (using ipc messages).
I also went a little further by refactoring other things:

  • remove useless static structures => saves a lot of memory
  • remove access from the thread handler function to static variables defined in the loramac upstream code
  • decouple the send from the receive => in the long run, it should be possible to adapt this package to the netif interface/thread
  • adapt the test application and the documentation

There might things I overlooked so comments and tests are very welcome.

cc @jia200x and @dylad who might be interested.

Issues/PRs references

None

@aabadie aabadie added Area: pkg Area: External package ports Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Area: LoRa Area: LoRa radio support labels Mar 19, 2018
@aabadie aabadie requested a review from jia200x March 19, 2018 08:00
@aabadie aabadie force-pushed the hackathon_ietf branch 3 times, most recently from 4488dd0 to a82b048 Compare March 19, 2018 08:28
@jia200x
Copy link
Copy Markdown
Member

jia200x commented Mar 19, 2018

Hi @aabadie

Nice approach!
I only have some comments in the thread-safe part. I will do a quick review.

if (strcmp("deveui", argv[2]) == 0) {
uint8_t deveui[LORAMAC_DEVEUI_LEN];
semtech_loramac_get_deveui(deveui);
semtech_loramac_get_deveui(&loramac, deveui);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would have preferred here the usage of IPC rather than direct calls + mutex. IMO:

  • If there's a single entry point for all commands, there shouldn't be needs for mutex (e.g here)
  • In can be integrated to netapi, since most of these commands are network options.

What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Basically, this means reusing MSG_TYPE_LORAMAC_CMD

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I understand what you mean and I avoided it on purpose. That would have required other internal get/set functions, with switch/case depending on the parameters, very similar to netdev.

The actual approach is simpler and I think it ends up to the same result.

If the semtech-loramac API is later integrated into netif, then it will be possible to call it in the netapi. I need to rework my first attempt to achieve this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I understand what you mean and I avoided it on purpose. That would have required other internal get/set functions, with switch/case depending on the parameters, very similar to netdev.

Yes, unless it's done via snippets (this is, writing integrated LoRaMAC code in a function and calling it via LORAMAC_CMD, but might confuse end users...)

Do you think it's possible to do the same with Join, etc?

kernel_pid_t semtech_loramac_pid;
kernel_pid_t semtech_loramac_handler_pid;

sx127x_t sx127x;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think it could make sense to also isolate these kind of variables in a struct?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(e.g this could come from the netdev_t pointer)

Copy link
Copy Markdown
Contributor Author

@aabadie aabadie Mar 19, 2018

Choose a reason for hiding this comment

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

The problem is that it is used by the radio adaption code as an extern variable. Using a get/set function for passing/retrieving the pointer as you initially did won't be better I think.
The best solution would be to pass it to all semtech-loramac function but then it will require massive patches in the upstream code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, you are right. I forgot that :)

@aabadie aabadie force-pushed the hackathon_ietf branch 2 times, most recently from 9dda9fd to 27ad634 Compare March 24, 2018 21:24
@dylad dylad self-assigned this Mar 25, 2018
return datarate;
}

void semtech_loramac_set_rx2_params(semtech_loramac_t *mac,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have a compilation error here because semtech_loramac_t *mac argument is not used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return datarate;
}

void semtech_loramac_set_rx2_params(semtech_loramac_channel_params_t params)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we declare it as static ? In the current state, this function is not threadsafe if user call it from its app.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, and it saves a few bytes (24) in ROM. Changed

@aabadie aabadie force-pushed the hackathon_ietf branch 2 times, most recently from 4669665 to c53ece8 Compare March 26, 2018 05:35
@dylad
Copy link
Copy Markdown
Member

dylad commented Mar 26, 2018

I don't know if it is related to this PR but I'm encountering issue with the following command :

> loramac set tx_power 14

> loramac get tx_power

TX power index: 0
> loramac set tx_power 10

> loramac get tx_power

TX power index: 0

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Mar 26, 2018

I'm encountering issue with the following command

It is definitely a bug, and not only on this branch, I can reproduce it on master. ChannelsDefaultTxPower should be used to set the TX power.

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Mar 26, 2018

@dylad, see #8835

Copy link
Copy Markdown
Member

@dylad dylad left a comment

Choose a reason for hiding this comment

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

ACK
Tested work on custom hardware and a TTN gateway.
Join OTAA and data exchanges work as expected.
waiting for @jia200x's ACK before merging.

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Mar 26, 2018

@dylad unfortunately I don't have hardware to test. We bought a gateway by January and haven't received any news about it... codewise looks OK to me though

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 9, 2018

@aabadie just tried it with a nucleo-l152 and SX1276, but I get:

Just confirmed this also happens with B-L072z-LRWAN1 (but this issue is not present in the master branch):

> loramac join abp
Join procedure succeeded!
> loramac tx asd
Failed: mac is busy

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 9, 2018

Just confirmed this also happens with B-L072z-LRWAN1

Ok, with ABP join procedure. I know where it comes from and I'll fix it

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 9, 2018

@aabadie it also happens in #8639

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 9, 2018

it also happens in #8639

This is normal since it's based on this one ;)

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 10, 2018

@aabadie the Mac busy msg vanished, but it hangs with ABP:

> loramac join abp
Join procedure succeeded!
> loramac tx asd
help
help
help
help
help
help
help
help

I will investigate

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 10, 2018

Do you have the same problem on master ? (ABP activation is not my main use case, so I didn't test it)

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 10, 2018

I will investigate

I think I know what is the problem: there's no fallback when the mcps_confirm event is not "status ok" and no message is sent from the MAC thread to the caller thread. I worked on that problem recently and have a branch that fixes this partially.

@dylad
Copy link
Copy Markdown
Member

dylad commented Apr 10, 2018

But I did not encounter this issue on my hardware, isn't it weird ?

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 10, 2018

But I did not encounter this issue on my hardware, isn't it weird ?

More or less. That can happen if the server reply is not in the receiving time frame of the end device. In general it works.
It would be great if @jia200x could provide more information on his setup (iotlab node used, DR, etc) but also try the other activation method (OTAA).

@dylad
Copy link
Copy Markdown
Member

dylad commented Apr 19, 2018

Any news on this ?

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 19, 2018

I will try to find some time between today and tomorrow

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 19, 2018

@jia200x, I tested on iotlab and the problem you raised with ABP activation is also present in master. When using ABP activation, the TX confirmable mode doesn't work because no confirmation message is received by the end device. I know how to fix it (already worked on that in this branch)

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 19, 2018

There's problem with receiving data when using ABP activation (this is also the case in master).

@dylad
Copy link
Copy Markdown
Member

dylad commented Apr 19, 2018

So now the question is : Should we fix it here ? or in another PR ?

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 19, 2018

Should we fix it here ? or in another PR ?

I prefer in another PR: see #8982 (and mainly 99af4f4)

@dylad
Copy link
Copy Markdown
Member

dylad commented Apr 19, 2018

prefer in another PR: see #8982 (and mainly 99af4f4)

Great, I'll give it a try this afternoon if possible.
@jia200x should we merge this one ?

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 19, 2018

@dylad I'm OK with merging this one since the bug is not related to this PR.

Copy link
Copy Markdown
Member

@jia200x jia200x left a comment

Choose a reason for hiding this comment

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

ACK


static uint8_t _semtech_loramac_radio_payload[SX127X_RX_BUFFER_SIZE];
static semtech_loramac_rx_data_t _semtech_loramac_rx_data;
LoRaMacPrimitives_t semtech_loramac_primitives;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same question as above here

kernel_pid_t semtech_loramac_pid;
kernel_pid_t semtech_loramac_handler_pid;

sx127x_t sx127x;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, you are right. I forgot that :)

if (strcmp("deveui", argv[2]) == 0) {
uint8_t deveui[LORAMAC_DEVEUI_LEN];
semtech_loramac_get_deveui(deveui);
semtech_loramac_get_deveui(&loramac, deveui);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I understand what you mean and I avoided it on purpose. That would have required other internal get/set functions, with switch/case depending on the parameters, very similar to netdev.

Yes, unless it's done via snippets (this is, writing integrated LoRaMAC code in a function and calling it via LORAMAC_CMD, but might confuse end users...)

Do you think it's possible to do the same with Join, etc?

@aabadie
Copy link
Copy Markdown
Contributor Author

aabadie commented Apr 19, 2018

Do you think it's possible to do the same with Join, etc?

I don't think so. Because the join/tx are triggering a complex workflow in the MAC with timers etc. Using the loramac_cmd and IPC messages makes more sense in this case.
Setting/Getting MAC parameters is just writing in a variables in the loramac-node code, that's why I think mutexes are more handy.

@jia200x
Copy link
Copy Markdown
Member

jia200x commented Apr 19, 2018

I don't think so. Because the join/tx are triggering a complex workflow in the MAC with timers etc. Using the loramac_cmd and IPC messages makes more sense in this case.
Setting/Getting MAC parameters is just writing in a variables in the loramac-node code, that's why I think mutexes are more handy.

Sounds reasonable.

Ok, then GO.

@jia200x jia200x merged commit b8ac8c9 into RIOT-OS:master Apr 19, 2018
maxvankessel pushed a commit to maxvankessel/RIOT that referenced this pull request Apr 20, 2018
pkg/semtech-loramac: refactor API and make it thread-safe
@aabadie aabadie deleted the hackathon_ietf branch June 1, 2018 18:55
@cladmi cladmi added this to the Release 2018.07 milestone Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: LoRa Area: LoRa radio support Area: pkg Area: External package ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: API change Integration Process: PR contains or issue proposes an API change. Should be handled with care. Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants