github.com/free5gc/openapi@v1.0.8/supported_feature.go (about)

     1  package openapi
     2  
     3  import (
     4  	"encoding/hex"
     5  )
     6  
     7  // SupportedFeature - bytes used to indicate the features supported by a API
     8  // that is used as defined in subclause 6.6 in 3GPP TS 29.500
     9  type SupportedFeature []byte
    10  
    11  // NewSupportedFeature - new NewSupportedFeature from string
    12  func NewSupportedFeature(suppFeat string) (SupportedFeature, error) {
    13  	// padding for hex decode
    14  	if len(suppFeat)%2 != 0 {
    15  		suppFeat = "0" + suppFeat
    16  	}
    17  
    18  	supportedFeature, err := hex.DecodeString(suppFeat)
    19  	return supportedFeature, err
    20  }
    21  
    22  // String - convert SupportedFeature to hex format
    23  func (suppoertedFeature SupportedFeature) String() string {
    24  	return hex.EncodeToString(suppoertedFeature)
    25  }
    26  
    27  // GetFeature - get nth feature is supported
    28  func (suppoertedFeature SupportedFeature) GetFeature(n int) bool {
    29  	byteIndex := len(suppoertedFeature) - ((n - 1) / 8) - 1
    30  	bitShift := uint8((n - 1) % 8)
    31  
    32  	if byteIndex < 0 {
    33  		return false
    34  	}
    35  
    36  	if suppoertedFeature[byteIndex]&(0x01<<bitShift) > 0 {
    37  		return true
    38  	}
    39  
    40  	return false
    41  }
    42  
    43  // NegotiateWith - Negotiate with other supported feature
    44  func (suppoertedFeature SupportedFeature) NegotiateWith(incomingSuppFeat SupportedFeature) SupportedFeature {
    45  	var suppFeatA, suppFeatB, negotiateFeature SupportedFeature
    46  	var negotiatedFeatureLength, lengthDiff int
    47  	// padding short one
    48  	if len(suppoertedFeature) < len(incomingSuppFeat) {
    49  		suppFeatA = incomingSuppFeat
    50  		suppFeatB = make(SupportedFeature, len(incomingSuppFeat))
    51  		lengthDiff = len(incomingSuppFeat) - len(suppoertedFeature)
    52  		copy(suppFeatB[lengthDiff:], suppoertedFeature)
    53  		negotiatedFeatureLength = len(incomingSuppFeat)
    54  	} else {
    55  		suppFeatA = suppoertedFeature
    56  		suppFeatB = make(SupportedFeature, len(suppoertedFeature))
    57  		lengthDiff = len(suppoertedFeature) - len(incomingSuppFeat)
    58  		copy(suppFeatB[lengthDiff:], incomingSuppFeat)
    59  		negotiatedFeatureLength = len(suppoertedFeature)
    60  	}
    61  
    62  	negotiateFeature = make(SupportedFeature, negotiatedFeatureLength)
    63  
    64  	for i := 0; i < negotiatedFeatureLength; i++ {
    65  		negotiateFeature[i] = suppFeatA[i] & suppFeatB[i]
    66  	}
    67  
    68  	return negotiateFeature
    69  }