Skip to content

Lora: Remove singleton pattern #6692

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

Merged
merged 2 commits into from
Apr 24, 2018
Merged

Lora: Remove singleton pattern #6692

merged 2 commits into from
Apr 24, 2018

Conversation

kivaisan
Copy link
Contributor

@kivaisan kivaisan commented Apr 20, 2018

Description

LoRaMacCrypto was the last source that was not yet converted to a C++ class. Since that is now a C++ class, also singleton pattern can be removed from LoRa stack. This will simplify the code and enable easier tracking of memory consumption and further refactorings.

Pull request type

[ ] Fix
[X] Refactor
[ ] New target
[ ] Feature
[ ] Breaking change

@kivaisan
Copy link
Contributor Author

@AnttiKauppila, @hasnainvirk, @kjbracey-arm please review.

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
LoRaMacCrypto::LoRaMacCrypto()
{
memset(mic_block_b0, 0, sizeof(mic_block_b0));
Copy link
Contributor

Choose a reason for hiding this comment

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

Could avoid explicit memsets by zero-initting arrays like any other element:

LoRaMacCrypto::LoRaMacCrypto() : mic_block_b0(), a_block(), ...

You will still need to manually set the non-zero elements though (with current C++03).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I'll change this.

@@ -70,7 +70,8 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
} device_states_t;

public:
static LoRaWANStack& get_lorawan_stack();
LoRaWANStack();
~LoRaWANStack();
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see any reason to be defining an empty destructor here. Yes, it's an existing pattern, but tidy up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll remove the empty destructor as this class is not supposed to be herited.

int encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key,
uint32_t address, uint8_t dir, uint32_t seq_counter,
uint8_t *enc_buffer);
#define LORAMAC_MIC_BLOCK_B0_SIZE 16
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a fan of this define - it's uniquely specified in one place - sizeof mic_block_b0. You can just use that to reference, and the size of the mic_block is obviously where it's defined: mic_block_b0[16].

(I often try to avoid excessive defines, having been traumatised by a source file that had 100 lines like this:

HW_REG_A = REG_A_INIT_VALUE;
HW_REG_B = REG_B_INIT_VALUE;

Yay, pointless indirection. Don't need a define for a constant that occurs in exactly 1 place, unless you need to expose it as a config option.)

int decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key,
uint32_t address, uint8_t dir, uint32_t seq_counter,
uint8_t *dec_buffer);
#define LORAMAC_AES_CMAC_KEY_LENGTH 16
Copy link
Contributor

Choose a reason for hiding this comment

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

On a similar note, this wouldn't be needed if you passed the key length as a parameter to compute_mic, and that's sizeof nwk_skey. Would actually be a bit tighter. Or the key could actually be a proper type - a struct with a 16-byte array in it, so you can use sizeof inside compute_mic.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 20, 2018

Review jenkins CI failures, related

@kivaisan
Copy link
Contributor Author

CI failure was caused by missing constructor when LoRa was built with missing mbedtls configurations. I'll fix this.

@kivaisan
Copy link
Contributor Author

@kjbracey-arm please review again.

kjbracey
kjbracey previously approved these changes Apr 20, 2018
@0xc0170
Copy link
Contributor

0xc0170 commented Apr 20, 2018

Please review docs failure in Travis CI

@kivaisan
Copy link
Contributor Author

@0xc0170 For some reason Doxygen started to complain about the Semtech's header even though I didn't modify it. But this is now fixed.

Copy link
Contributor

@hasnainvirk hasnainvirk left a comment

Choose a reason for hiding this comment

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

Just a textual change required.

@@ -542,6 +544,11 @@ class LoRaMac {
*/
LoRaMacChannelPlan channel_plan;

/**
* Channel planning subsystem
Copy link
Contributor

Choose a reason for hiding this comment

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

Its a crypto subsystem

Kimmo Vaisanen added 2 commits April 20, 2018 16:01
In order to get rid of static variables, LoRaCrypto functionality is now
in a C++ class.
After changing LoRaMacCrypto as C++ class, we no longer have static variables
in LoRa implementation. Therefore singleton pattern can be removed.
@kivaisan
Copy link
Contributor Author

@hasnainvirk Text fixed.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 20, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Apr 20, 2018

Build : SUCCESS

Build number : 1818
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6692/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Apr 20, 2018

@mbed-ci
Copy link

mbed-ci commented Apr 20, 2018

@kivaisan
Copy link
Contributor Author

@0xc0170 Could you restart the test? This failure does not seem to be related to this PR.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 24, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Apr 24, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants