-
Notifications
You must be signed in to change notification settings - Fork 13
/
scmg.go
143 lines (118 loc) · 3.11 KB
/
scmg.go
1
2
3
4
5
6
7
8
9
10
11
12
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
54
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2019-2024 go-sccp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package sccp
import (
"encoding/binary"
"fmt"
"io"
)
// SCMGType is type of SCMG message.
type SCMGType uint8
// Table 23/Q.713
const (
_ SCMGType = iota
SCMGTypeSSA
SCMGTypeSSP
SCMGTypeSST
SCMGTypeSOR
SCMGTypeSOG
SCMGTypeSSC
)
// SCMG represents a SCCP Management message (SCMG).
// Chapter 5.3/Q.713
type SCMG struct {
Type SCMGType
AffectedSSN uint8
AffectedPC uint16
SubsystemMultiplicityIndicator uint8
SCCPCongestionLevel uint8
}
// NewSCMG creates a new SCMG.
func NewSCMG(typ SCMGType, affectedSSN uint8, affectedPC uint16, subsystemMultiplicityIndicator uint8, sccpCongestionLevel uint8) *SCMG {
s := &SCMG{
Type: typ,
AffectedSSN: affectedSSN,
AffectedPC: affectedPC,
SubsystemMultiplicityIndicator: subsystemMultiplicityIndicator,
SCCPCongestionLevel: sccpCongestionLevel,
}
return s
}
// MarshalBinary returns the byte sequence generated from a SCMG instance.
func (s *SCMG) MarshalBinary() ([]byte, error) {
b := make([]byte, s.MarshalLen())
if err := s.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (s *SCMG) MarshalTo(b []byte) error {
l := len(b)
if l < s.MarshalLen() {
return io.ErrUnexpectedEOF
}
b[0] = uint8(s.Type)
b[1] = s.AffectedSSN
binary.LittleEndian.PutUint16(b[2:4], s.AffectedPC)
b[4] = s.SubsystemMultiplicityIndicator
if s.Type == SCMGTypeSSC {
b[5] = s.SCCPCongestionLevel
}
return nil
}
// ParseSCMG decodes given byte sequence as a SCMG.
func ParseSCMG(b []byte) (*SCMG, error) {
s := &SCMG{}
if err := s.UnmarshalBinary(b); err != nil {
return nil, err
}
return s, nil
}
// UnmarshalBinary sets the values retrieved from byte sequence in a SCMG.
func (s *SCMG) UnmarshalBinary(b []byte) error {
l := len(b)
if l < 5 {
return io.ErrUnexpectedEOF
}
s.Type = SCMGType(b[0])
s.AffectedSSN = b[1]
s.AffectedPC = binary.LittleEndian.Uint16(b[2:4])
s.SubsystemMultiplicityIndicator = b[4]
if s.Type == SCMGTypeSSC {
if l < 6 {
return io.ErrUnexpectedEOF
}
s.SCCPCongestionLevel = b[5]
}
return nil
}
// MarshalLen returns the serial length.
func (s *SCMG) MarshalLen() int {
// Table 24/Q.713 – SCMG messages
l := 5
// Table 25/Q.713 – SSC
if s.Type == SCMGTypeSSC {
l += 1
}
return l
}
// String returns the SCMG values in human readable format.
func (s *SCMG) String() string {
return fmt.Sprintf("{Type: %d, Affected SSN: %v, Affected PC: %v, Subsystem Multiplicity Indicator: %d, SCCP Congestion Level: %d}",
s.Type,
s.AffectedSSN,
s.AffectedPC,
s.SubsystemMultiplicityIndicator,
s.SCCPCongestionLevel,
)
}
// MessageType returns the Message Type in int.
func (s *SCMG) MessageType() SCMGType {
return s.Type
}
// MessageTypeName returns the Message Type in string.
func (s *SCMG) MessageTypeName() string {
return "SCMG"
}