...

Source file src/edge-infra.dev/pkg/sds/lib/edid/edid.go

Documentation: edge-infra.dev/pkg/sds/lib/edid

     1  package edid
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/anoopengineer/edidparser/edid"
     8  )
     9  
    10  // EDID represents a display's extended display identification data:
    11  // https://wikipedia.org/wiki/Extended_Display_Identification_Data.
    12  type EDID struct {
    13  	*edid.Edid
    14  	bytes []byte
    15  }
    16  
    17  // "ManufacturerID-ProductCode" from EDID data.
    18  func (edid *EDID) String() string {
    19  	if edid == nil {
    20  		return ""
    21  	}
    22  	return fmt.Sprintf("%s-%d", edid.ManufacturerId, edid.ProductCode)
    23  }
    24  
    25  // Raw EDID bytes.
    26  func (edid *EDID) Bytes() []byte {
    27  	if edid == nil {
    28  		return nil
    29  	}
    30  	return edid.bytes
    31  }
    32  
    33  // Whether EDID matches a target EDID. False if either are nil.
    34  func (edid *EDID) Matches(target *EDID) bool {
    35  	if edid == nil || target == nil {
    36  		return false
    37  	}
    38  	return Matches(*edid, *target)
    39  }
    40  
    41  // Parses EDID from byte data.
    42  func NewFromBytes(bytes []byte) (*EDID, error) {
    43  	edid, err := edid.NewEdid(bytes)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("failed to parse EDID data: %w", err)
    46  	}
    47  	return &EDID{
    48  		Edid:  edid,
    49  		bytes: bytes,
    50  	}, nil
    51  }
    52  
    53  // Parses EDID from the byte data in the file.
    54  func NewFromFile(path string) (*EDID, error) {
    55  	bytes, err := os.ReadFile(path)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("failed to read file %s: %w", path, err)
    58  	}
    59  
    60  	if string(bytes) == "" {
    61  		return nil, fmt.Errorf("EDID file is empty")
    62  	}
    63  
    64  	return NewFromBytes(bytes)
    65  }
    66  
    67  // A and B match if they have the same (non-empty) manufacturer ID and product code.
    68  func Matches(a, b EDID) bool {
    69  	if a.ManufacturerId == "" || b.ManufacturerId == "" {
    70  		return false
    71  	} else if a.ManufacturerId != b.ManufacturerId {
    72  		return false
    73  	}
    74  
    75  	if a.ProductCode == 0 || b.ProductCode == 0 {
    76  		return false
    77  	} else if a.ProductCode != b.ProductCode {
    78  		return false
    79  	}
    80  
    81  	return true
    82  }
    83  

View as plain text