-
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
Merged
roshanrajaratnam
merged 6 commits into
NordicSemiconductor:main
from
JulesDommartin:main
Sep 23, 2022
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43f4709
Added generic battery get message and status parsing
JulesDommartin c9f5422
Merge remote-tracking branch 'upstream/main'
JulesDommartin 65f73a3
Fixed size of buffer which was not 10 but 8
JulesDommartin cf0e0ec
Fixed GenericBatteryStatus parsing
JulesDommartin fb0d2ab
Simplified code and followed Nordic advices
JulesDommartin 3d3533c
Added 0xFF mask to get the unsigned byte value
JulesDommartin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
mesh/src/main/java/no/nordicsemi/android/mesh/transport/GenericBatteryGet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package no.nordicsemi.android.mesh.transport; | ||
|
||
|
||
import androidx.annotation.NonNull; | ||
import no.nordicsemi.android.mesh.ApplicationKey; | ||
import no.nordicsemi.android.mesh.opcodes.ApplicationMessageOpCodes; | ||
import no.nordicsemi.android.mesh.utils.SecureUtils; | ||
|
||
public class GenericBatteryGet extends ApplicationMessage { | ||
|
||
private static final String TAG = GenericBatteryGet.class.getSimpleName(); | ||
private static final int OP_CODE = ApplicationMessageOpCodes.GENERIC_BATTERY_GET; | ||
|
||
/** | ||
* Constructs a Generic Battery Get message | ||
* | ||
* @param appKey application key | ||
*/ | ||
public GenericBatteryGet(@NonNull ApplicationKey appKey) { | ||
super(appKey); | ||
assembleMessageParameters(); | ||
} | ||
|
||
@Override | ||
void assembleMessageParameters() { | ||
mAid = SecureUtils.calculateK4(mAppKey.getKey()); | ||
} | ||
|
||
@Override | ||
public int getOpCode() { | ||
return OP_CODE; | ||
} | ||
} |
242 changes: 242 additions & 0 deletions
242
mesh/src/main/java/no/nordicsemi/android/mesh/transport/GenericBatteryStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
package no.nordicsemi.android.mesh.transport; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
import no.nordicsemi.android.mesh.logger.MeshLogger; | ||
import no.nordicsemi.android.mesh.opcodes.ApplicationMessageOpCodes; | ||
import no.nordicsemi.android.mesh.utils.MeshAddress; | ||
|
||
/** | ||
* To be used as a wrapper class to create generic level status message. | ||
*/ | ||
@SuppressWarnings({"WeakerAccess"}) | ||
public class GenericBatteryStatus extends ApplicationStatusMessage { | ||
|
||
private static final String TAG = GenericBatteryStatus.class.getSimpleName(); | ||
private static final int GENERIC_BATTERY_STATUS_MANDATORY_LENGTH = 8; | ||
private static final int OP_CODE = ApplicationMessageOpCodes.GENERIC_BATTERY_STATUS; | ||
private byte mBatteryLevel; | ||
private int mTimeToDischarge; | ||
private int mTimeToCharge; | ||
private byte mFlags; | ||
|
||
/** | ||
* Constructs GenericBatteryStatus message | ||
* @param message access message | ||
*/ | ||
public GenericBatteryStatus(@NonNull AccessMessage message) { | ||
super(message); | ||
this.mMessage = message; | ||
this.mParameters = message.getParameters(); | ||
parseStatusParameters(); | ||
} | ||
|
||
@Override | ||
void parseStatusParameters() { | ||
MeshLogger.verbose(TAG, "Received generic battery status from: " + MeshAddress.formatAddress(mMessage.getSrc(), true)); | ||
mBatteryLevel = mParameters[0]; | ||
MeshLogger.verbose(TAG, "Battery level: " + mBatteryLevel); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
MeshLogger.verbose(TAG, "Time to discharge: " + mTimeToDischarge); | ||
MeshLogger.verbose(TAG, "Time to charge: " + mTimeToCharge); | ||
MeshLogger.verbose(TAG, "Flags: " + mFlags); | ||
} | ||
} | ||
|
||
@Override | ||
public int getOpCode() { | ||
return OP_CODE; | ||
} | ||
|
||
/** | ||
* Returns the battery level of the node | ||
* | ||
* @return battery level | ||
*/ | ||
public byte getBatteryLevel() { | ||
return mBatteryLevel; | ||
} | ||
|
||
/** | ||
* Returns the time to discharge of the battery | ||
* | ||
* @return time to discharge | ||
*/ | ||
public int getTimeToDischarge() { | ||
return mTimeToDischarge; | ||
} | ||
|
||
/** | ||
* Returns the time to charge of the battery | ||
* | ||
* @return time to charge | ||
*/ | ||
public int getTimeToCharge() { | ||
return mTimeToCharge; | ||
} | ||
|
||
|
||
/** | ||
* Returns the battery flags | ||
* | ||
* @return battery flags | ||
*/ | ||
public byte getFlags() { | ||
return mFlags; | ||
} | ||
|
||
/** | ||
* Returns the battery presence | ||
* | ||
* @return BatteryPresence | ||
*/ | ||
public BatteryPresence getBatteryPresence() { | ||
return BatteryPresence.getBatteryPresence(mFlags & 0x03); | ||
} | ||
|
||
/** | ||
* Returns the battery charge level | ||
* | ||
* @return BatteryIndicator | ||
*/ | ||
public BatteryIndicator getBatteryIndicator() { | ||
return BatteryIndicator.getBatteryIndicator((mFlags >> 2) & 0x03); | ||
} | ||
|
||
/** | ||
* Returns the battery charging state | ||
* | ||
* @return BatteryChargingState | ||
*/ | ||
public BatteryChargingState getBatteryChargingState() { | ||
return BatteryChargingState.getBatteryChargingState((mFlags >> 4) & 0x03); | ||
} | ||
|
||
/** | ||
* Returns the battery serviceability | ||
* | ||
* @return BatteryServiceability | ||
*/ | ||
public BatteryServiceability batteryServiceability() { | ||
return BatteryServiceability.getBatteryServiceability((mFlags >> 6) & 0x03); | ||
} | ||
|
||
/** | ||
* Battery presence values enumeration | ||
*/ | ||
public enum BatteryPresence { | ||
NOT_PRESENT(0b00), | ||
REMOVABLE(0b01), | ||
NOT_REMOVABLE(0b10), | ||
UNKNOWN(0b11); | ||
|
||
private int flag; | ||
//Constructor to initialize the instance variable | ||
BatteryPresence(int flag) { | ||
this.flag = flag; | ||
} | ||
|
||
public int getFlag() { | ||
return this.flag; | ||
} | ||
|
||
public static BatteryPresence getBatteryPresence(int flag) { | ||
for (BatteryPresence bp : BatteryPresence.values()) { | ||
if (bp.flag == flag) return bp; | ||
} | ||
throw new IllegalArgumentException("BatteryPresence flag not found"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Battery indicator values enumeration | ||
*/ | ||
public enum BatteryIndicator { | ||
CRITICALLY_LOW(0b00), | ||
LOW(0b01), | ||
GOOD(0b10), | ||
UNKNOWN(0b11); | ||
|
||
private int flag; | ||
//Constructor to initialize the instance variable | ||
BatteryIndicator(int flag) { | ||
this.flag = flag; | ||
} | ||
|
||
public int getFlag() { | ||
return this.flag; | ||
} | ||
|
||
public static BatteryIndicator getBatteryIndicator(int flag) { | ||
for (BatteryIndicator bi : BatteryIndicator.values()) { | ||
if (bi.flag == flag) return bi; | ||
} | ||
throw new IllegalArgumentException("BatteryIndicator flag not found"); | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* Battery charging state values enumeration | ||
*/ | ||
public enum BatteryChargingState { | ||
NOT_CHARGEABLE(0b00), | ||
NOT_CHARGING(0b01), | ||
CHARGING(0b10), | ||
UNKNOWN(0b11); | ||
|
||
private int flag; | ||
//Constructor to initialize the instance variable | ||
BatteryChargingState(int flag) { | ||
this.flag = flag; | ||
} | ||
|
||
public int getFlag() { | ||
return this.flag; | ||
} | ||
|
||
public static BatteryChargingState getBatteryChargingState(int flag) { | ||
for (BatteryChargingState bcs : BatteryChargingState.values()) { | ||
if (bcs.flag == flag) return bcs; | ||
} | ||
throw new IllegalArgumentException("BatteryChargingState flag not found"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Battery serviceability values enumeration | ||
*/ | ||
public enum BatteryServiceability { | ||
RESERVED(0b00), | ||
SERVICE_NOT_REQUIRED(0b01), | ||
SERVICE_REQUIRED(0b10), | ||
UNKNOWN(0b11); | ||
|
||
private int flag; | ||
//Constructor to initialize the instance variable | ||
BatteryServiceability(int flag) { | ||
this.flag = flag; | ||
} | ||
|
||
public int getFlag() { | ||
return this.flag; | ||
} | ||
|
||
public static BatteryServiceability getBatteryServiceability(int flag) { | ||
for (BatteryServiceability bs : BatteryServiceability.values()) { | ||
if (bs.flag == flag) return bs; | ||
} | ||
throw new IllegalArgumentException("BatteryServiceability flag not found"); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.