最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

go - How to encode NGAPPDU WarningMessageContents - Stack Overflow

programmeradmin4浏览0评论

I have been trying to encode to byte the message string "Severe Earthquake expected immediately" into the NGAPPDU message WarningMessageContents which by definition in the specification ETSI TS 138 413 V17.11.0 (2025-01) is WarningMessageContents ::= OCTET STRING (SIZE(1..9600)) but was having issue, wireshark is showing error and is about the decoded content. The page number is calculated correctly. Could anyone help me figure out what I might be missing in the code below. Other specification of the coding scheme can be found in section 5 of ETSI TS 123 038 V17.0.0 (2022-04), thanks in advance.

const (
  MaxCBSPageSize  = 82   // Bytes per page (after GSM 7-bit encoding)
  MaxPages        = 15   // 3GPP TS 23.041 limit
  MaxMessageBytes = 9600 // Total CBS message size limit
)

type WarningMessageContents struct {
  Value aper.OctetString `aper:"sizeLB:1,sizeUB:9600"`
}
type OctetString []byte

// Function to encode CBS message according to ETSI TS 123 041
func EncodeCBSWarningMessage(message string) (*ngapType.WarningMessageContents, error) {
  // Calculate number of pages
  messageLen := len(message)
  numPages := (messageLen + MaxCBSPageSize - 1) / MaxCBSPageSize // Ceiling division

  // Strict validation for maximum pages
  if numPages > 15 {
    return nil, fmt.Errorf("too many CBS pages (must be <= 15), found: %d", numPages)
  }

  // Validate total size
  if messageLen < 1 || messageLen > 9600 {
    return nil, errors.New("warning message size out of range (must be between 1 and 9600 bytes)")
  }

  // Build CBS formatted message
  cbsMessage := make([]byte, 0, messageLen+numPages*5) // Pre-allocate with some extra space

  // First byte is Total Number of Pages
  cbsMessage = append(cbsMessage, byte(numPages))

  // Split message into pages
  for i := 0; i < numPages; i++ {
    start := i * MaxCBSPageSize
    end := start + MaxCBSPageSize
    if end > messageLen {
      end = messageLen
    }

    // Precise page header according to CBS specifications
    pageHeader := []byte{
      byte(i + 1), // Page Number (1-based)
      0x01,        // Data Coding Scheme (GSM 7-bit)
      0x00,        // Language Group (English)
    }

    // Append page header
    cbsMessage = append(cbsMessage, pageHeader...)

    // Append page content
    cbsMessage = append(cbsMessage, []byte(message[start:end])...)
  }

  // Create WarningMessageContents
  return &ngapType.WarningMessageContents{
    Value: aper.OctetString(cbsMessage),
  }, nil
}

发布评论

评论列表(0)

  1. 暂无评论