forked from andig/gosml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
99 lines (83 loc) · 2.12 KB
/
time.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
package sml
import "fmt"
type Time uint32
func TimeParse(buf *Buffer) (Time, error) {
/*
if (BufOptionalIsSkipped(buf)) {
return 0;
}
Time *tme = TimeInit();
if (BufGetNextType(buf) != TYPELIST) {
buf->error = 1;
goto error;
}
if (BufGetNextLength(buf) != 2) {
buf->error = 1;
goto error;
}
tme->tag = U8Parse(buf);
if (BufHasErrors(buf)) goto error;
int type = BufGetNextType(buf);
switch (type) {
case TYPEUNSIGNED:
tme->data.timestamp = U32Parse(buf);
if (BufHasErrors(buf)) goto error;
break;
case TYPELIST:
// Some meters (e.g. FROETEC Multiflex ZG22) giving not one uint32
// as timestamp, but a list of 3 values.
// Ignoring these values, so that parsing does not fail.
BufGetNextLength(buf); // should we check the length here?
u32 *t1 = U32Parse(buf);
if (BufHasErrors(buf)) goto error;
i16 *t2 = I16Parse(buf);
if (BufHasErrors(buf)) goto error;
i16 *t3 = I16Parse(buf);
if (BufHasErrors(buf)) goto error;
fprintf(stderr,
"libsml: error: Time as list[3]: ignoring value[0]=%u value[1]=%d value[2]=%d\n",
*t1, *t2, *t3);
break;
default:
goto error;
}
*/
// TODO return proper timestamps
if skip := BufOptionalIsSkipped(buf); skip {
return 0, nil
}
Debug(buf, "TimeParse")
if err := Expect(buf, TYPELIST, 2); err != nil {
return 0, err
}
// time.tag
if _, err := U8Parse(buf); err != nil {
return 0, err
}
var timestamp uint32
var err error
typefield := BufGetNextType(buf)
switch typefield {
case TYPEUNSIGNED:
if timestamp, err = U32Parse(buf); err != nil {
return 0, err
}
case TYPELIST:
// Some meters (e.g. FROETEC Multiflex ZG22) giving not one uint32
// as timestamp, but a list of 3 values.
// Ignoring these values, so that parsing does not fail.
BufGetNextLength(buf) // should we check the length here?
if _, err := U32Parse(buf); err != nil {
return 0, err
}
if _, err := I16Parse(buf); err != nil {
return 0, err
}
if _, err := I16Parse(buf); err != nil {
return 0, err
}
default:
return 0, fmt.Errorf("Invalid time format %02x", typefield)
}
return Time(timestamp), nil
}