-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.go
177 lines (134 loc) · 4.06 KB
/
utils.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
package canopus
import (
"fmt"
"math/rand"
"regexp"
"strings"
"time"
)
// Returns the string value for a Message Payload
func PayloadAsString(p MessagePayload) string {
if p == nil {
return ""
}
return p.String()
}
// GenerateMessageId generate a uint16 Message ID
func GenerateMessageID() uint16 {
MESSAGEID_MUTEX.Lock()
if CurrentMessageID != 65535 {
CurrentMessageID++
} else {
CurrentMessageID = 1
}
MESSAGEID_MUTEX.Unlock()
return uint16(CurrentMessageID)
}
var genChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
// GenerateToken generates a random token by a given length
func GenerateToken(l int) string {
rand.Seed(time.Now().UTC().UnixNano())
token := make([]rune, l)
for i := range token {
token[i] = genChars[rand.Intn(len(genChars))]
}
return string(token)
}
// CoreResourcesFromString Converts to CoRE Resources Object from a CoRE String
func CoreResourcesFromString(str string) []*CoreResource {
var re = regexp.MustCompile(`(<[^>]+>\s*(;\s*\w+\s*(=\s*(\w+|"([^"\\]*(\\.[^"\\]*)*)")\s*)?)*)`)
var elemRe = regexp.MustCompile(`<[^>]*>`)
var resources []*CoreResource
m := re.FindAllString(str, -1)
for _, match := range m {
elemMatch := elemRe.FindString(match)
target := elemMatch[1 : len(elemMatch)-1]
resource := NewCoreResource()
resource.Target = target
if len(match) > len(elemMatch) {
attrs := strings.Split(match[len(elemMatch)+1:], ";")
for _, attr := range attrs {
pair := strings.Split(attr, "=")
resource.AddAttribute(pair[0], strings.Replace(pair[1], "\"", "", -1))
}
}
resources = append(resources, resource)
}
return resources
}
// CoapCodeToString returns the string representation of a CoapCode
func CoapCodeToString(code CoapCode) string {
switch code {
case Get:
return "GET"
case Post:
return "POST"
case Put:
return "PUT"
case Delete:
return "DELETE"
case CoapCodeEmpty:
return "0 Empty"
case CoapCodeCreated:
return "201 Created"
case CoapCodeDeleted:
return "202 Deleted"
case CoapCodeValid:
return "203 Valid"
case CoapCodeChanged:
return "204 Changed"
case CoapCodeContent:
return "205 Content"
case CoapCodeBadRequest:
return "400 Bad Request"
case CoapCodeUnauthorized:
return "401 Unauthorized"
case CoapCodeBadOption:
return "402 Bad Option"
case CoapCodeForbidden:
return "403 Forbidden"
case CoapCodeNotFound:
return "404 Not Found"
case CoapCodeMethodNotAllowed:
return "405 Method Not Allowed"
case CoapCodeNotAcceptable:
return "406 Not Acceptable"
case CoapCodePreconditionFailed:
return "412 Precondition Failed"
case CoapCodeRequestEntityTooLarge:
return "413 Request Entity Too Large"
case CoapCodeUnsupportedContentFormat:
return "415 Unsupported Content Format"
case CoapCodeInternalServerError:
return "500 Internal Server Error"
case CoapCodeNotImplemented:
return "501 Not Implemented"
case CoapCodeBadGateway:
return "502 Bad Gateway"
case CoapCodeServiceUnavailable:
return "503 Service Unavailable"
case CoapCodeGatewayTimeout:
return "504 Gateway Timeout"
case CoapCodeProxyingNotSupported:
return "505 Proxying Not Supported"
default:
return "Unknown"
}
}
// ValidCoapMediaTypeCode Checks if a MediaType is of a valid code
func ValidCoapMediaTypeCode(mt MediaType) bool {
switch mt {
case MediaTypeTextPlain, MediaTypeTextXML, MediaTypeTextCsv, MediaTypeTextHTML, MediaTypeImageGif,
MediaTypeImageJpeg, MediaTypeImagePng, MediaTypeImageTiff, MediaTypeAudioRaw, MediaTypeVideoRaw,
MediaTypeApplicationLinkFormat, MediaTypeApplicationXML, MediaTypeApplicationOctetStream, MediaTypeApplicationRdfXML,
MediaTypeApplicationSoapXML, MediaTypeApplicationAtomXML, MediaTypeApplicationXmppXML, MediaTypeApplicationExi,
MediaTypeApplicationFastInfoSet, MediaTypeApplicationSoapFastInfoSet, MediaTypeApplicationJSON,
MediaTypeApplicationXObitBinary, MediaTypeTextPlainVndOmaLwm2m, MediaTypeTlvVndOmaLwm2m,
MediaTypeJSONVndOmaLwm2m, MediaTypeOpaqueVndOmaLwm2m:
return true
}
return false
}
func logMsg(a ...interface{}) (n int, err error) {
return fmt.Println(a)
}