Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Fixed #5
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Feb 11, 2019
1 parent fe72ade commit 22508a9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion OnewheelBluetooth/Classes/OnewheelType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public enum OnewheelType
{
ONEWHEEL = 0,
ONEWHEEL_PLUS = 1,
ONEWHEEL_XR = 2
ONEWHEEL_PLUS_XR = 2
}
}
52 changes: 46 additions & 6 deletions OnewheelBluetooth/Classes/OnewheelUnlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ public class OnewheelUnlockHelper : IDisposable
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
/// <summary>
/// The Gemini firmware revision number.
/// The Gemini firmware revision number for the Onewheel Plus.
/// </summary>
private readonly byte[] FIRMWARE_REVISION_BYTES = new byte[] { 0x0f, 0xc2 };
private readonly byte[] FIRMWARE_REVISION_OW_PLUS_BYTES = new byte[] { 0x0f, 0xc2 };

/// <summary>
/// The Gemini firmware revision number for the Onewheel Plus XR.
/// </summary>
private readonly byte[] FIRMWARE_REVISION_OW_PLUS_XR_BYTES = new byte[] { 0x10, 0x26 };

/// <summary>
/// The first three bytes of a challenge message from the Onewheel.
Expand Down Expand Up @@ -45,6 +50,7 @@ public class OnewheelUnlockHelper : IDisposable
private bool disposed = false;

private readonly OnewheelBoard ONEWHEEL;
private OnewheelType type;

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
Expand All @@ -64,7 +70,15 @@ public OnewheelUnlockHelper(OnewheelBoard onewheel)
#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--
private bool IsOwPlusFirmwareRevision(byte[] data)
{
return data[0] == FIRMWARE_REVISION_OW_PLUS_BYTES[0] && data[1] == FIRMWARE_REVISION_OW_PLUS_BYTES[1];
}

private bool IsOwPlusXrFirmwareRevision(byte[] data)
{
return data[0] == FIRMWARE_REVISION_OW_PLUS_XR_BYTES[0] && data[1] == FIRMWARE_REVISION_OW_PLUS_XR_BYTES[1];
}

#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
Expand All @@ -79,7 +93,7 @@ public void Start()
Logger.Info("Requesting firmware version for Gemini unlock...");
byte[] data = await ONEWHEEL.ReadBytesAsync(OnewheelCharacteristicsCache.CHARACTERISTIC_FIRMWARE_REVISION);

if (data == null || data[0] != FIRMWARE_REVISION_BYTES[0] || data[1] != FIRMWARE_REVISION_BYTES[1])
if (!CheckForGeminiFirmwareRevision(data))
{
Logger.Info("Unlock failed - Gemini firmware does not match: " + (data is null ? "null" : Utils.ByteArrayToHexString(data)));
return;
Expand Down Expand Up @@ -138,7 +152,26 @@ public byte[] CalcResponse(byte[] challenge)
#endregion

#region --Misc Methods (Private)--
private bool DoFirstBytesMatch()
private bool CheckForGeminiFirmwareRevision(byte[] data)
{
if (!(data is null) && data.Length >= 2)
{
if (IsOwPlusXrFirmwareRevision(data))
{
type = OnewheelType.ONEWHEEL_PLUS_XR;
return true;
}
else if (IsOwPlusFirmwareRevision(data))
{
type = OnewheelType.ONEWHEEL_PLUS;
return true;
}
return false;
}
return false;
}

private bool CheckIfFirstChallengeBytesMatch()
{
return SERIAL_READ_CACHE.Count >= 3
&& SERIAL_READ_CACHE[0] == CHALLENGE_FIRT_BYTES[0]
Expand Down Expand Up @@ -191,7 +224,14 @@ private async void OnUnlockTimeout(ThreadPoolTimer timer)
}
try
{
await ONEWHEEL.WriteBytesAsync(OnewheelCharacteristicsCache.CHARACTERISTIC_FIRMWARE_REVISION, FIRMWARE_REVISION_BYTES);
if (type == OnewheelType.ONEWHEEL_PLUS)
{
await ONEWHEEL.WriteBytesAsync(OnewheelCharacteristicsCache.CHARACTERISTIC_FIRMWARE_REVISION, FIRMWARE_REVISION_OW_PLUS_BYTES);
}
else
{
await ONEWHEEL.WriteBytesAsync(OnewheelCharacteristicsCache.CHARACTERISTIC_FIRMWARE_REVISION, FIRMWARE_REVISION_OW_PLUS_XR_BYTES);
}
Logger.Debug("Sent Gemini unlock firmware revision.");
}
catch (Exception e)
Expand Down Expand Up @@ -222,7 +262,7 @@ private async void CACHE_CharacteristicChanged(OnewheelCharacteristicsCache send
Utils.ReverseByteOrderIfNeeded(args.NEW_VALUE);

AddSerialReadDataToCache(args.NEW_VALUE);
if (DoFirstBytesMatch() && SERIAL_READ_CACHE.Count == 20)
if (CheckIfFirstChallengeBytesMatch() && SERIAL_READ_CACHE.Count == 20)
{
await CalcAndSendResponseAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion OnewheelBluetooth/Classes/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static double ToAmpere(uint value, OnewheelType type)
multiplier = 1.8;
break;

case OnewheelType.ONEWHEEL_XR:
case OnewheelType.ONEWHEEL_PLUS_XR:
multiplier = 1.8; // Not validated
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void LoadRidingModes()
MODES.Add(new RidingModeDataTemplate() { Mode = Consts.RIDING_MODE_OW_CLASSIC_ELEVATE });
break;

case OnewheelType.ONEWHEEL_XR:
case OnewheelType.ONEWHEEL_PLUS_XR:
case OnewheelType.ONEWHEEL_PLUS:
MODES.Add(new RidingModeDataTemplate() { Mode = Consts.RIDING_MODE_OW_PLUS_SEQUOIA });
MODES.Add(new RidingModeDataTemplate() { Mode = Consts.RIDING_MODE_OW_PLUS_CRUZ });
Expand Down

0 comments on commit 22508a9

Please sign in to comment.