add debug info

This commit is contained in:
anlicheng 2025-08-19 17:03:27 +08:00
parent 2c05d71dbe
commit 77a631a001
3 changed files with 16 additions and 10 deletions

View File

@ -6,7 +6,14 @@
//
import Foundation
struct ARPPacket {
struct ARPPacket: CustomStringConvertible {
var description: String {
return """
opcode: \(self.opcode), sender_ip: \(SDLUtil.int32ToIp(self.senderIP)), sender_mac: \(SDLUtil.formatMacAddress(mac: senderMAC)),
target_ip: \(SDLUtil.int32ToIp(self.targetIP)), target_mac: \(SDLUtil.formatMacAddress(mac: targetMAC))
"""
}
// ARP
enum Opcode: UInt16 {
case request = 0x01

View File

@ -232,7 +232,7 @@ public class SDLContext: @unchecked Sendable {
private func handleSuperEvent(event: SDLSuperClient.SuperEvent) async throws {
switch event {
case .ready:
self.logger.log("[SDLContext] get registerSuper, mac address: \(Self.formatMacAddress(mac: self.devAddr.mac))", level: .debug)
self.logger.log("[SDLContext] get registerSuper, mac address: \(SDLUtil.formatMacAddress(mac: self.devAddr.mac))", level: .debug)
guard let message = try await self.superClient?.registerSuper(context: self).get() else {
return
}
@ -391,7 +391,7 @@ public class SDLContext: @unchecked Sendable {
await self.arpServer.append(ip: arpPacket.senderIP, mac: arpPacket.senderMAC)
}
} else {
self.logger.log("[SDLContext] get invalid arp packet, target_ip: \(arpPacket)", level: .debug)
self.logger.log("[SDLContext] get invalid arp packet: \(arpPacket), target_ip: \(SDLUtil.int32ToIp(arpPacket.targetIP)), net ip: \(SDLUtil.int32ToIp(self.devAddr.netAddr))", level: .debug)
}
} else {
self.logger.log("[SDLContext] get invalid arp packet", level: .debug)
@ -622,11 +622,4 @@ public class SDLContext: @unchecked Sendable {
return Data(macAddress)
}
// mac
private static func formatMacAddress(mac: Data) -> String {
let bytes = [UInt8](mac)
return bytes.map { String(format: "%02X", $0) }.joined(separator: ":").lowercased()
}
}

View File

@ -42,5 +42,11 @@ struct SDLUtil {
return ip & mask == compareIp & mask
}
public static func formatMacAddress(mac: Data) -> String {
let bytes = [UInt8](mac)
return bytes.map { String(format: "%02X", $0) }.joined(separator: ":").lowercased()
}
}