forked from nttdots/go-dots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_display.go
216 lines (202 loc) · 6.46 KB
/
coap_display.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
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"strconv"
"github.com/nttdots/go-dots/libcoap"
)
/*
* Output formatted CoAP messages to the stdout.
*/
func CoapHeaderDisplay(request *libcoap.Pdu) string {
var result string = "\n"
// Version
result += fmt.Sprint(" 01.. .... = Version: 1\n")
// Type
var value, key = getTypeValue(request.Type)
result += fmt.Sprintf(" ..%s .... = Type: %s(%d)\n", decimalToBinary(value, 2), key, value)
// Token Length
var token_length int = len(request.Token)
result += fmt.Sprintf(" .... %s = Token Length: %d\n", decimalToBinary(token_length, 4), token_length)
// Code
value, key = getMethodValue(request.Code)
result += fmt.Sprintf(" Code: %s (%d)\n", key, value)
// Message ID
result += fmt.Sprintf(" Message ID: %d\n", request.MessageID)
// Token
result += fmt.Sprintf(" Token: %s\n", request.Token)
// Options
var option_count int = 0
result += getOptionValue(libcoap.OptionUriHost, request.OptionValues(libcoap.OptionUriHost), &option_count)
result += getOptionValue(libcoap.OptionUriPort, request.OptionValues(libcoap.OptionUriPort), &option_count)
result += getOptionValue(libcoap.OptionMaxage, request.OptionValues(libcoap.OptionMaxage), &option_count)
result += getOptionValue(libcoap.OptionEtag, request.OptionValues(libcoap.OptionEtag), &option_count)
result += getOptionValue(libcoap.OptionLocationPath, request.OptionValues(libcoap.OptionLocationPath), &option_count)
result += getOptionValue(libcoap.OptionLocationQuery, request.OptionValues(libcoap.OptionLocationQuery), &option_count)
result += getOptionValue(libcoap.OptionUriPath, request.OptionValues(libcoap.OptionUriPath), &option_count)
result += getOptionValue(libcoap.OptionObserve, request.OptionValues(libcoap.OptionObserve), &option_count)
result += getOptionValue(libcoap.OptionContentFormat, request.OptionValues(libcoap.OptionContentFormat), &option_count)
result += getOptionValue(libcoap.OptionIfMatch, request.OptionValues(libcoap.OptionIfMatch), &option_count)
result += getOptionValue(libcoap.OptionIfNoneMatch, request.OptionValues(libcoap.OptionIfNoneMatch), &option_count)
result += getOptionValue(libcoap.OptionProxyUri, request.OptionValues(libcoap.OptionProxyUri), &option_count)
result += getOptionValue(libcoap.OptionProxyScheme, request.OptionValues(libcoap.OptionProxyScheme), &option_count)
result += getOptionValue(libcoap.OptionSize1, request.OptionValues(libcoap.OptionSize1), &option_count)
result += getOptionValue(libcoap.OptionUriQuery, request.OptionValues(libcoap.OptionUriQuery), &option_count)
// End of Options marker
result += fmt.Sprintf(" End of options marker: %d\n", 255)
// get content_format string
var opt_content_format = request.OptionValues(libcoap.OptionContentFormat)
var content_format string = ""
for _, v := range opt_content_format {
content_format = GetContentFormatValue(v)
}
// Payload
result += fmt.Sprintf(" Payload: Payload Content-Format: %s, Length: %d\n", content_format, len(request.Data))
result += fmt.Sprintf(" Payload Desc: %s\n", content_format)
result += fmt.Sprintf(" JavaScript Object Notation: %s\n", content_format)
result += fmt.Sprintf(" Line-based text data: %s\n", content_format)
return result
}
func getTypeValue(t libcoap.Type) (int, string) {
switch t {
case libcoap.TypeCon:
return 0, "Confirmable"
case libcoap.TypeNon:
return 1, "Non-Confirmable"
case libcoap.TypeAck:
return 2, "Acknowledgement"
case libcoap.TypeRst:
return 3, "Reset"
default:
return 0, ""
}
}
func getMethodValue(t libcoap.Code) (int, string) {
switch t {
case libcoap.RequestGet:
return 1, "GET"
case libcoap.RequestPost:
return 2, "POST"
case libcoap.RequestPut:
return 3, "PUT"
case libcoap.RequestDelete:
return 4, "DELETE"
default:
return 0, ""
}
}
func getOptionValue(t libcoap.OptionKey, o []interface{}, op_count *int) string {
var retValue string = ""
// return nil if the interface equals to nil.
if o == nil {
return retValue
}
// Convert given OptionIDs to strings.
for _, v := range o {
var optName string = ""
var optValue string = ""
switch t {
case libcoap.OptionUriHost:
optName = "Uri-Host"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionUriPort:
optName = "Uri-Port"
optValue = fmt.Sprintf("%d", v)
break
case libcoap.OptionMaxage:
optName = "Max-Age"
optValue = fmt.Sprintf("%d", v)
break
case libcoap.OptionEtag:
optName = "E-Tag"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionLocationPath:
optName = "Location-Path"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionLocationQuery:
optName = "Location-Query"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionUriPath:
optName = "Uri-Path"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionObserve:
optName = "Observe"
optValue = fmt.Sprintf("%d", v)
break
case libcoap.OptionContentFormat:
optName = "Content-Format"
optValue = GetContentFormatValue(v)
break
case libcoap.OptionIfMatch:
optName = "If-Match"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionIfNoneMatch:
optName = "If-None-Match"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionProxyUri:
optName = "Proxy-Uri"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionProxyScheme:
optName = "Proxy-Schema"
optValue = fmt.Sprintf("%s", v)
break
case libcoap.OptionSize1:
optName = "Size1"
optValue = fmt.Sprintf("%d", v)
break
case libcoap.OptionUriQuery:
optName = "Uri-Query"
optValue = fmt.Sprintf("%s", v)
break
}
// display the option names if the value is not nil.
if optName != "" {
*op_count++
retValue += fmt.Sprintf(">Opt Name: #%d: %s: %s\n", *op_count, optName, optValue)
}
}
return retValue
}
func GetContentFormatValue(v interface{}) string {
switch v {
case libcoap.TextPlain:
return "text/plain"
case libcoap.AppJSON:
return "app/json"
case libcoap.AppExi:
return "app/exi"
case libcoap.AppLinkFormat:
return "app/linkformat"
case libcoap.AppOctets:
return "app/octets"
case libcoap.AppXML:
return "app/xml"
case libcoap.AppCbor:
return "app/cbor"
default:
return ""
}
}
func decimalToBinary(dec_value int, digit int) string {
var bin_value string = ""
var new_digit int = 0
// decimal to binary
for dec_value != 0 {
bin_value = strconv.Itoa(dec_value%2) + bin_value
dec_value = dec_value / 2
new_digit++
}
// zero-fill the upper bits
for new_digit < digit {
bin_value = "0" + bin_value
new_digit++
}
return bin_value
}