lib.go 2.11 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package c3000cdi

import (
8
9
10
11
12
	"dcu-container-toolkit/internal/logger"
	"dcu-container-toolkit/internal/lookup/root"
	"dcu-container-toolkit/pkg/c3000cdi/transform"
	"dcu-container-toolkit/pkg/go-c3000lib/pkg/device"
	"dcu-container-toolkit/pkg/go-c3000smi/pkg/c3000smi"
songlinfeng's avatar
songlinfeng committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	"fmt"
)

type c3000cdilib struct {
	logger             logger.Interface
	c3000smicmd        c3000smi.Interface
	mode               Mode
	devicelib          device.Interface
	deviceNamers       DeviceNamers
	driverRoot         string
	devRoot            string
	dtkCDIHookPath     string
	ldconfigPath       string
	configSearchPaths  []string
	librarySearchPaths []string

	vendor string
	class  string

	driver *root.Driver

	mergedDeviceOptions []transform.MergedDeviceOption
}

// New creates a new c3000cdi library
func New(opts ...Option) (Interface, error) {
	l := &c3000cdilib{}
	for _, opt := range opts {
		opt(l)
	}
	if l.mode == "" {
		l.mode = ModeAuto
	}
	if l.logger == nil {
		l.logger = logger.New()
	}
	if len(l.deviceNamers) == 0 {
		indexNamer, _ := NewDeviceNamer(DeviceNameStrategyIndex)
		l.deviceNamers = []DeviceNamer{indexNamer}
	}
	if l.dtkCDIHookPath == "" {
54
		l.dtkCDIHookPath = "/usr/bin/dcu-cdi-hook"
songlinfeng's avatar
songlinfeng committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	}
	if l.driverRoot == "" {
		l.driverRoot = "/"
	}
	if l.devRoot == "" {
		l.devRoot = "/"
	}

	l.driver = root.New(
		root.WithLogger(l.logger),
		root.WithDriverRoot(l.driverRoot),
		root.WithLibrarySearchPaths(l.librarySearchPaths...),
		root.WithConfigSearchPaths(l.configSearchPaths...),
	)
	if l.c3000smicmd == nil {
		smicmd, err := c3000smi.NewSmiCommand(l.logger)
		if err != nil {
			return nil, fmt.Errorf("failed to new smi commd: %v", err)
		}
		l.c3000smicmd = smicmd
	}
	if l.devicelib == nil {
		l.devicelib = device.New(l.c3000smicmd)
	}

	var lib Interface
	switch l.resolveMode() {
	case ModeSmi:
		lib = (*c3000smilib)(l)
	default:
		return nil, fmt.Errorf("unknown mode %q", l.mode)
	}

	w := wrapper{
		Interface:           lib,
		vendor:              l.vendor,
		class:               l.class,
		mergedDeviceOptions: l.mergedDeviceOptions,
	}
	return &w, nil
}