mirror of
https://github.com/Unifi-Tools/UFiber.Configurator.git
synced 2025-02-05 18:28:28 +00:00
Merge pull request #3 from Unifi-Tools/allow-hex-serial
Allow HEX Serial and SLID values.
This commit is contained in:
commit
92d9754c73
8
.vscode/launch.json
vendored
8
.vscode/launch.json
vendored
|
@ -10,7 +10,13 @@
|
||||||
"args": [
|
"args": [
|
||||||
"--host",
|
"--host",
|
||||||
"192.168.200.2",
|
"192.168.200.2",
|
||||||
"--dry-run"
|
"--dry-run",
|
||||||
|
"--vendor",
|
||||||
|
"HWTC",
|
||||||
|
"--serial",
|
||||||
|
"41-4C-43-4C-90-12-34-5a",
|
||||||
|
"--slid",
|
||||||
|
"12345"
|
||||||
],
|
],
|
||||||
"cwd": "${workspaceFolder}/src/UFiber.Configurator",
|
"cwd": "${workspaceFolder}/src/UFiber.Configurator",
|
||||||
"console": "internalConsole",
|
"console": "internalConsole",
|
||||||
|
|
|
@ -59,7 +59,7 @@ Usage:
|
||||||
UFiber.Configurator [options]
|
UFiber.Configurator [options]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--host <host> IP or hostname of the target UFiber device.
|
--host <host> IP or hostname of the target UFiber device. [default: 192.168.1.1]
|
||||||
--user <user> SSH user name. [default: ubnt]
|
--user <user> SSH user name. [default: ubnt]
|
||||||
--pw <pw> SSH password. [default: ubnt]
|
--pw <pw> SSH password. [default: ubnt]
|
||||||
--port <port> SSH port of the target UFiber device. [default: 22]
|
--port <port> SSH port of the target UFiber device. [default: 22]
|
||||||
|
@ -67,8 +67,10 @@ Options:
|
||||||
--slid <slid> The SLID (or PLOAM Password).
|
--slid <slid> The SLID (or PLOAM Password).
|
||||||
--vendor <vendor> 4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is
|
--vendor <vendor> 4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is
|
||||||
built.
|
built.
|
||||||
--serial <serial> 8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is
|
--serial <serial> 8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial
|
||||||
built.
|
number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX
|
||||||
|
value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to
|
||||||
|
Vendor Id with '--vendor'.
|
||||||
--mac <mac> The desired MAC address to clone.
|
--mac <mac> The desired MAC address to clone.
|
||||||
--version Show version information
|
--version Show version information
|
||||||
-?, -h, --help Show help and usage information
|
-?, -h, --help Show help and usage information
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using static System.BitConverter;
|
using static System.BitConverter;
|
||||||
|
|
||||||
namespace UFiber.Configurator
|
namespace UFiber.Configurator
|
||||||
|
@ -11,6 +12,8 @@ namespace UFiber.Configurator
|
||||||
private const int NvRamCrcLength = 4;
|
private const int NvRamCrcLength = 4;
|
||||||
private const uint NvRamOffset = 0x580;
|
private const uint NvRamOffset = 0x580;
|
||||||
private const uint NvRamLength = 0x400;
|
private const uint NvRamLength = 0x400;
|
||||||
|
private static readonly Regex _snMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){7}([a-fA-F0-9]){2}$");
|
||||||
|
private static readonly Regex _pwMatcher = new Regex(@"^(([a-fA-F0-9]){2}-){9}([a-fA-F0-9]){2}$");
|
||||||
public uint Checksum { get; private set; }
|
public uint Checksum { get; private set; }
|
||||||
public byte[] AfeId { get; }
|
public byte[] AfeId { get; }
|
||||||
public byte[] VoiceBoardId { get; }
|
public byte[] VoiceBoardId { get; }
|
||||||
|
@ -118,7 +121,16 @@ namespace UFiber.Configurator
|
||||||
|
|
||||||
public void SetGponSerialNumber(string serialNumber)
|
public void SetGponSerialNumber(string serialNumber)
|
||||||
{
|
{
|
||||||
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber));
|
if (_snMatcher.IsMatch(serialNumber))
|
||||||
|
{
|
||||||
|
this.SetGponId(AsBytes(serialNumber.Replace("-", "")[0..8]));
|
||||||
|
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber.Replace("-", "")[8..]));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (serialNumber.Contains("-")) throw new InvalidOperationException($"Invalid serial number: {serialNumber}.");
|
||||||
|
SetGponSerialNumber(Encoding.UTF8.GetBytes(serialNumber));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetGponSerialNumber(ReadOnlySpan<byte> serialNumber)
|
public void SetGponSerialNumber(ReadOnlySpan<byte> serialNumber)
|
||||||
|
@ -149,7 +161,18 @@ namespace UFiber.Configurator
|
||||||
throw new ArgumentOutOfRangeException(nameof(password));
|
throw new ArgumentOutOfRangeException(nameof(password));
|
||||||
}
|
}
|
||||||
|
|
||||||
var bits = Encoding.UTF8.GetBytes(password);
|
byte[] bits = default!;
|
||||||
|
|
||||||
|
if (_pwMatcher.IsMatch(password))
|
||||||
|
{
|
||||||
|
password = password.Replace("-", "").PadLeft(MaxPasswordLength, '0');
|
||||||
|
bits = AsBytes(password);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
password = password.PadLeft(MaxPasswordLength, '0');
|
||||||
|
bits = Encoding.UTF8.GetBytes(password);
|
||||||
|
}
|
||||||
|
|
||||||
SetBytes(bits.AsSpan(), PasswordOffset, password.Length);
|
SetBytes(bits.AsSpan(), PasswordOffset, password.Length);
|
||||||
|
|
||||||
|
@ -245,14 +268,14 @@ namespace UFiber.Configurator
|
||||||
|
|
||||||
while (dataIndex < data.Length)
|
while (dataIndex < data.Length)
|
||||||
{
|
{
|
||||||
var highNibble = ToHex(data[dataIndex++]);
|
var highNibble = FromHex(data[dataIndex++]);
|
||||||
var lowNibble = ToHex(data[dataIndex++]);
|
var lowNibble = FromHex(data[dataIndex++]);
|
||||||
bits[bitsIndex++] = (byte)(highNibble << 4 | lowNibble);
|
bits[bitsIndex++] = (byte)(highNibble << 4 | lowNibble);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bits;
|
return bits;
|
||||||
|
|
||||||
static int ToHex(char c) =>
|
static int FromHex(char c) =>
|
||||||
c switch
|
c switch
|
||||||
{
|
{
|
||||||
>= 'a' and <= 'f' => 10 + (c - 'a'),
|
>= 'a' and <= 'f' => 10 + (c - 'a'),
|
||||||
|
|
|
@ -34,7 +34,7 @@ var rootCommand = new RootCommand("Apply configuration changes to UFiber devices
|
||||||
"4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne),
|
"4-digit Vendor Id (e.g. HWTC, MTSC, etc.). Combined with --serial, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne),
|
||||||
new Option<string>(
|
new Option<string>(
|
||||||
"--serial",
|
"--serial",
|
||||||
"8-digit serial number (e.g. 01234567). Combined with --vendor, a GPON Serial Number is built.", ArgumentArity.ZeroOrOne),
|
"8-digit (e.g. 01234567) serial number or 16-digit (e.g. 41-4C-43-4C-xx-xx-xx-xx) HEX serial number. Combined with --vendor, a GPON Serial Number is built. Note: If a 16-digit HEX value is provided, the first 4 bytes (8 digits) will replace whatever value was passed to Vendor Id with '--vendor'.", ArgumentArity.ZeroOrOne),
|
||||||
new Option<string>(
|
new Option<string>(
|
||||||
"--mac",
|
"--mac",
|
||||||
"The desired MAC address to clone.", ArgumentArity.ZeroOrOne),
|
"The desired MAC address to clone.", ArgumentArity.ZeroOrOne),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user