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

GenericBatteryGet and GenericBatteryStatus implementation #533

Merged
merged 6 commits into from
Sep 23, 2022

Conversation

JulesDommartin
Copy link
Contributor

Hello, since I asked for this feature, I tried to implement it myself following the same code architecture and syntax as the other mesh Generic messages.

I hope it will be merged soon, and that it will help the project !

@CLAassistant
Copy link

CLAassistant commented Sep 21, 2022

CLA assistant check
All committers have signed the CLA.

@roshanrajaratnam
Copy link
Member

@JulesDommartin thank you for your contribution and it will definitely help our project! By the way please make sure to sign the CLA so that I can merge your changes straight away,

Copy link
Member

@roshanrajaratnam roshanrajaratnam left a comment

Choose a reason for hiding this comment

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

Some minor improvements that could be done. Everything else looks good 🥇

@@ -40,9 +40,9 @@ void parseStatusParameters() {
final ByteBuffer buffer = ByteBuffer.wrap(mParameters).order(ByteOrder.LITTLE_ENDIAN);
Copy link
Member

Choose a reason for hiding this comment

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

We don't need a byte buffer here, as the endianness will not matter as we are just dealing with single octets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I will remove this line and work with mParameters directly.

if (buffer.limit() > GENERIC_BATTERY_STATUS_MANDATORY_LENGTH) {
mTimeToDischarge = buffer.get() | (buffer.get() << 8) | (buffer.get() << 16);
mTimeToCharge = buffer.get() | (buffer.get() << 8) | (buffer.get() << 16);
if (buffer.limit() >= GENERIC_BATTERY_STATUS_MANDATORY_LENGTH) {
Copy link
Member

Choose a reason for hiding this comment

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

This check is not required as the status will be always 8 bytes,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove this check though ? I thought it will be useful in case of a bad implementation in the embedded software.

Comment on lines 44 to 45
mTimeToDischarge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF) << 8) | ((buffer.get() & 0xFF) << 16);
mTimeToCharge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF)<< 8) | ((buffer.get() & 0xFF) << 16);
Copy link
Member

Choose a reason for hiding this comment

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

Once we get rid of the bytebuffer, we could directly work with the mParameters
(mParameters[1] & 0xFF) | ((mParameters[2] & 0xFF) << 8) | (mParameters[3] & 0xFF) << 16) and so forth.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@@ -40,9 +40,9 @@ void parseStatusParameters() {
final ByteBuffer buffer = ByteBuffer.wrap(mParameters).order(ByteOrder.LITTLE_ENDIAN);
mBatteryLevel = buffer.get();
Copy link
Member

Choose a reason for hiding this comment

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

Once we get rid of the byte buffer we could get the battery status as follows may be mParameters[0] & 0xFF?

Copy link
Contributor Author

@JulesDommartin JulesDommartin Sep 22, 2022

Choose a reason for hiding this comment

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

I think that the & 0xFF is not needed here because the value is between 0x00 and 0x64. It can only be 0xFF if the battery level is unknown following the Mesh specifications, if I am right. But yes, like you said I will get the value from mParameters ! 😄

mTimeToCharge = buffer.get() | (buffer.get() << 8) | (buffer.get() << 16);
if (buffer.limit() >= GENERIC_BATTERY_STATUS_MANDATORY_LENGTH) {
mTimeToDischarge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF) << 8) | ((buffer.get() & 0xFF) << 16);
mTimeToCharge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF)<< 8) | ((buffer.get() & 0xFF) << 16);
mFlags = buffer.get();
Copy link
Member

Choose a reason for hiding this comment

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

Same as above here...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@JulesDommartin
Copy link
Contributor Author

I don't understand why the CLA shows that I didn't accept them. It seems like it did work
image

What should I do more ? Did I missed something ?

@JulesDommartin
Copy link
Contributor Author

I don't understand why the CLA shows that I didn't accept them. It seems like it did work image

What should I do more ? Did I missed something ?

Nevermind, I got it, my account wasn't configured properly. Now it is fine ! 😄

Copy link
Member

@roshanrajaratnam roshanrajaratnam left a comment

Choose a reason for hiding this comment

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

Apart from the two minor issues, everything else is fine.

@Override
void parseStatusParameters() {
MeshLogger.verbose(TAG, "Received generic battery status from: " + MeshAddress.formatAddress(mMessage.getSrc(), true));
mBatteryLevel = mParameters[0];
Copy link
Member

Choose a reason for hiding this comment

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

The & 0xFF is needed here to convert to an unsigned byte.

if (mParameters.length >= GENERIC_BATTERY_STATUS_MANDATORY_LENGTH) {
mTimeToDischarge = (mParameters[1] & 0xFF) | ((mParameters[2] & 0xFF) << 8) | ((mParameters[3] & 0xFF) << 16);
mTimeToCharge = (mParameters[4] & 0xFF) | ((mParameters[5] & 0xFF) << 8) | ((mParameters[6] & 0xFF) << 16);
mFlags = mParameters[7];
Copy link
Member

Choose a reason for hiding this comment

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

The & 0xFF is needed here to convert to an unsigned byte.

Copy link
Member

@roshanrajaratnam roshanrajaratnam left a comment

Choose a reason for hiding this comment

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

Great! thanks again for your contribution.

@roshanrajaratnam roshanrajaratnam merged commit e67d33a into NordicSemiconductor:main Sep 23, 2022
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.

3 participants