-
Notifications
You must be signed in to change notification settings - Fork 38
/
pdu_fields.go
117 lines (92 loc) · 2.17 KB
/
pdu_fields.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
package smpp34
import "strconv"
const FieldValueErr FieldErr = "Invalid field value"
type Field interface {
Length() interface{}
Value() interface{}
String() string
ByteArray() []byte
}
type FieldErr string
type SMField struct {
value []byte
}
type VariableField struct {
value []byte
}
type FixedField struct {
size uint8
value uint8
}
func NewField(f string, v interface{}) Field {
switch f {
case SOURCE_ADDR_TON, SOURCE_ADDR_NPI, DEST_ADDR_TON, DEST_ADDR_NPI, ESM_CLASS, PROTOCOL_ID, PRIORITY_FLAG, REGISTERED_DELIVERY, REPLACE_IF_PRESENT_FLAG, DATA_CODING, SM_DEFAULT_MSG_ID, INTERFACE_VERSION, ADDR_TON, ADDR_NPI, SM_LENGTH, MESSAGE_STATE, ERROR_CODE:
return NewFixedField(uint8(v.(int)))
case SERVICE_TYPE, SOURCE_ADDR, DESTINATION_ADDR, SCHEDULE_DELIVERY_TIME, VALIDITY_PERIOD, SYSTEM_ID, PASSWORD, SYSTEM_TYPE, ADDRESS_RANGE, MESSAGE_ID, FINAL_DATE:
return NewVariableField([]byte(v.(string)))
case SHORT_MESSAGE:
switch v.(type) {
case []byte:
return NewSMField(v.([]byte))
default:
return NewSMField([]byte(v.(string)))
}
}
return nil
}
func NewSMField(v []byte) Field {
i := &SMField{v}
f := Field(i)
return f
}
func NewVariableField(v []byte) Field {
i := &VariableField{v}
f := Field(i)
return f
}
func NewFixedField(v uint8) Field {
i := &FixedField{1, v}
f := Field(i)
return f
}
func (v *VariableField) Length() interface{} {
l := len(v.value)
return l
}
func (v *VariableField) Value() interface{} {
return v.value
}
func (v *VariableField) String() string {
return string(v.value)
}
func (v *VariableField) ByteArray() []byte {
return append(v.value, 0x00)
}
func (f *FixedField) Length() interface{} {
return uint8(1)
}
func (f *FixedField) Value() interface{} {
return f.value
}
func (f *FixedField) String() string {
return strconv.Itoa(int(f.value))
}
func (f *FixedField) ByteArray() []byte {
return packUi8(f.value)
}
func (f FieldErr) Error() string {
return string(f)
}
func (v *SMField) Length() interface{} {
l := len(v.value)
return l
}
func (v *SMField) Value() interface{} {
return v.value
}
func (v *SMField) String() string {
return string(v.value)
}
func (v *SMField) ByteArray() []byte {
return v.value
}