-
Notifications
You must be signed in to change notification settings - Fork 182
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
GenericBatteryGet and GenericBatteryStatus implementation #533
Conversation
@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, |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
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.
mTimeToDischarge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF) << 8) | ((buffer.get() & 0xFF) << 16); | ||
mTimeToCharge = (buffer.get() & 0xFF) | ((buffer.get() & 0xFF)<< 8) | ((buffer.get() & 0xFF) << 16); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this 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]; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
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 !