diff --git a/src/components/PageComponents/Config/Network.tsx b/src/components/PageComponents/Config/Network.tsx index 1b31db59..00426104 100644 --- a/src/components/PageComponents/Config/Network.tsx +++ b/src/components/PageComponents/Config/Network.tsx @@ -1,6 +1,10 @@ import type { NetworkValidation } from "@app/validation/config/network.js"; import { DynamicForm } from "@components/Form/DynamicForm.js"; import { useDevice } from "@core/stores/deviceStore.js"; +import { + convertIntToIpAddress, + convertIpAddressToInt, +} from "@core/utils/ip.js"; import { Protobuf } from "@meshtastic/js"; export const Network = (): JSX.Element => { @@ -13,9 +17,12 @@ export const Network = (): JSX.Element => { case: "network", value: { ...data, - ipv4Config: new Protobuf.Config.Config_NetworkConfig_IpV4Config( - data.ipv4Config, - ), + ipv4Config: new Protobuf.Config.Config_NetworkConfig_IpV4Config({ + ip: convertIpAddressToInt(data.ipv4Config.ip) ?? 0, + gateway: convertIpAddressToInt(data.ipv4Config.gateway) ?? 0, + subnet: convertIpAddressToInt(data.ipv4Config.subnet) ?? 0, + dns: convertIpAddressToInt(data.ipv4Config.dns) ?? 0, + }), }, }, }), @@ -25,7 +32,19 @@ export const Network = (): JSX.Element => { return ( onSubmit={onSubmit} - defaultValues={config.network} + defaultValues={{ + ...config.network, + ipv4Config: { + ip: convertIntToIpAddress(config.network?.ipv4Config?.ip ?? 0), + gateway: convertIntToIpAddress( + config.network?.ipv4Config?.gateway ?? 0, + ), + subnet: convertIntToIpAddress( + config.network?.ipv4Config?.subnet ?? 0, + ), + dns: convertIntToIpAddress(config.network?.ipv4Config?.dns ?? 0), + }, + }} fieldGroups={[ { label: "WiFi Config", diff --git a/src/core/utils/ip.ts b/src/core/utils/ip.ts new file mode 100644 index 00000000..27d7aa9e --- /dev/null +++ b/src/core/utils/ip.ts @@ -0,0 +1,14 @@ +export function convertIntToIpAddress(int: number): string { + return `${int & 0xff}.${(int >> 8) & 0xff}.${(int >> 16) & 0xff}.${(int >> 24) & 0xff}`; +} + +export function convertIpAddressToInt(ip: string): number | null { + return ( + ip + .split(".") + .reverse() + .reduce((ipnum, octet) => { + return (ipnum << 8) + Number.parseInt(octet); + }, 0) >>> 0 + ); +} diff --git a/src/validation/config/network.ts b/src/validation/config/network.ts index 8f851d69..41db88d7 100644 --- a/src/validation/config/network.ts +++ b/src/validation/config/network.ts @@ -41,21 +41,24 @@ export class NetworkValidation export class NetworkValidationIpV4Config implements - Omit + Omit< + Protobuf.Config.Config_NetworkConfig_IpV4Config, + keyof Message | "ip" | "gateway" | "subnet" | "dns" + > { @IsIP() @IsOptional() - ip: number; + ip: string; @IsIP() @IsOptional() - gateway: number; + gateway: string; @IsIP() @IsOptional() - subnet: number; + subnet: string; @IsIP() @IsOptional() - dns: number; + dns: string; }