Skip to content

Commit 48b54d7

Browse files
runcommmartinv
authored andcommitted
feat: implement getSerial/getMac on osx
Signed-off-by: Antonio Murdaca <[email protected]>
1 parent 8a4e965 commit 48b54d7

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

cmd/device_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: (C) 2025 Intel Corporation
22
// SPDX-License-Identifier: Apache 2.0
33

4-
//go:build !linux
4+
//go:build !linux && !darwin
55

66
package cmd
77

cmd/device_info_darwin.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation
2+
// SPDX-License-Identifier: Apache 2.0
3+
4+
package cmd
5+
6+
// #cgo LDFLAGS: -framework CoreFoundation -framework IOKit
7+
// #include <CoreFoundation/CoreFoundation.h>
8+
// #include <IOKit/IOKitLib.h>
9+
//
10+
// const char *
11+
// getSerialNumber()
12+
// {
13+
// CFMutableDictionaryRef matching = IOServiceMatching("IOPlatformExpertDevice");
14+
// io_service_t service = IOServiceGetMatchingService(kIOMainPortDefault, matching);
15+
// CFStringRef serialNumber = IORegistryEntryCreateCFProperty(service,
16+
// CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0);
17+
// const char *str = CFStringGetCStringPtr(serialNumber, kCFStringEncodingUTF8);
18+
// IOObjectRelease(service);
19+
//
20+
// return str;
21+
// }
22+
import "C"
23+
24+
import (
25+
"fmt"
26+
"net"
27+
)
28+
29+
func getSerial() (string, error) {
30+
serialNumber := C.GoString(C.getSerialNumber())
31+
return serialNumber, nil
32+
}
33+
34+
func getMac(iface string) (string, error) {
35+
interfaces, err := net.Interfaces()
36+
if err != nil {
37+
return "", err
38+
}
39+
for _, i := range interfaces {
40+
if i.Name == iface {
41+
if i.HardwareAddr.String() == "00:00:00:00:00:00" {
42+
return "", fmt.Errorf("mac address for %s is zero", iface)
43+
}
44+
return i.HardwareAddr.String(), nil
45+
}
46+
}
47+
return "", fmt.Errorf("mac address for %s not found", iface)
48+
}

0 commit comments

Comments
 (0)