forked from papamitra/go-dbus
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmessage.go
206 lines (177 loc) · 4.2 KB
/
message.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dbus
import (
"bytes"
"sync"
)
// See the D-Bus tutorial for information about message types.
// http://dbus.freedesktop.org/doc/dbus-tutorial.html#messages
type MessageType uint8
const (
TypeInvalid MessageType = iota
TypeMethodCall
TypeMethodReturn
TypeError
TypeSignal
)
var messageTypeString = map[MessageType]string{
TypeInvalid: "invalid",
TypeMethodCall: "method_call",
TypeMethodReturn: "method_return",
TypeSignal: "signal",
TypeError: "error",
}
func (t MessageType) String() string { return messageTypeString[t] }
type MessageFlag uint8
const (
FlagNoReplyExpected MessageFlag = 1 << iota
FlagNoAutoStart
)
type Message struct {
Type MessageType
Flags MessageFlag
Protocol int
bodyLength int
Path string
Dest string
Iface string
Member string
Sig string
Params []interface{}
serial int
replySerial uint32
ErrorName string
// Sender;
}
var serialMutex sync.Mutex
var messageSerial = int(0)
func _GetNewSerial() int {
serialMutex.Lock()
messageSerial++
serial := messageSerial
serialMutex.Unlock()
return serial
}
// Create a new message with Flags == 0 and Protocol == 1.
func NewMessage() *Message {
msg := new(Message)
msg.serial = _GetNewSerial()
msg.replySerial = 0
msg.Flags = 0
msg.Protocol = 1
msg.Params = make([]interface{}, 0)
return msg
}
func (p *Message) _BufferToMessage(buff []byte) (int, error) {
slice, bufIdx, e := Parse(buff, "yyyyuua(yv)", 0)
if e != nil {
return 0, e
}
p.Type = MessageType(slice[1].(byte))
p.Flags = MessageFlag(slice[2].(byte))
p.Protocol = int(slice[3].(byte))
p.bodyLength = int(slice[4].(uint32))
p.serial = int(slice[5].(uint32))
if vec, ok := slice[6].([]interface{}); ok {
for _, v := range vec {
tmpSlice := v.([]interface{})
t := int(tmpSlice[0].(byte))
val := tmpSlice[1]
switch t {
case 1:
p.Path = val.(string)
case 2:
p.Iface = val.(string)
case 3:
p.Member = val.(string)
case 4:
p.ErrorName = val.(string)
case 5:
p.replySerial = val.(uint32)
case 6:
p.Dest = val.(string)
case 7:
// FIXME
case 8:
p.Sig = val.(string)
}
}
}
idx := _Align(8, bufIdx)
if 0 < p.bodyLength {
p.Params, idx, _ = Parse(buff, p.Sig, idx)
}
return idx, nil
}
func _Unmarshal(buff []byte) (*Message, int, error) {
msg := NewMessage()
idx, e := msg._BufferToMessage(buff)
if e != nil {
return nil, 0, e
}
return msg, idx, nil
}
func (p *Message) _Marshal() ([]byte, error) {
buff := bytes.NewBuffer([]byte{})
_AppendByte(buff, byte('l')) // little Endian
_AppendByte(buff, byte(p.Type))
_AppendByte(buff, byte(p.Flags))
_AppendByte(buff, byte(p.Protocol))
tmpBuff := bytes.NewBuffer([]byte{})
_AppendParamsData(tmpBuff, p.Sig, p.Params)
_AppendUint32(buff, uint32(len(tmpBuff.Bytes())))
_AppendUint32(buff, uint32(p.serial))
_AppendArray(buff, 1,
func(b *bytes.Buffer) {
if p.Path != "" {
_AppendAlign(8, b)
_AppendByte(b, 1) // path
_AppendByte(b, 1) // signature size
_AppendByte(b, 'o')
_AppendByte(b, 0)
_AppendString(b, p.Path)
}
if p.Iface != "" {
_AppendAlign(8, b)
_AppendByte(b, 2) // interface
_AppendByte(b, 1) // signature size
_AppendByte(b, 's')
_AppendByte(b, 0)
_AppendString(b, p.Iface)
}
if p.Member != "" {
_AppendAlign(8, b)
_AppendByte(b, 3) // member
_AppendByte(b, 1) // signature size
_AppendByte(b, 's')
_AppendByte(b, 0)
_AppendString(b, p.Member)
}
if p.replySerial != 0 {
_AppendAlign(8, b)
_AppendByte(b, 5) // reply serial
_AppendByte(b, 1) // signature size
_AppendByte(b, 'u')
_AppendByte(b, 0)
_AppendUint32(b, uint32(p.replySerial))
}
if p.Dest != "" {
_AppendAlign(8, b)
_AppendByte(b, 6) // destination
_AppendByte(b, 1) // signature size
_AppendByte(b, 's')
_AppendByte(b, 0)
_AppendString(b, p.Dest)
}
if p.Sig != "" {
_AppendAlign(8, b)
_AppendByte(b, 8) // signature
_AppendByte(b, 1) // signature size
_AppendByte(b, 'g')
_AppendByte(b, 0)
_AppendSignature(b, p.Sig)
}
})
_AppendAlign(8, buff)
_AppendParamsData(buff, p.Sig, p.Params)
return buff.Bytes(), nil
}