forked from iomz/go-llrp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
82 lines (70 loc) · 1.77 KB
/
tag.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
// Copyright (c) 2018 Iori Mizutani
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package llrp
import (
"bytes"
"encoding/gob"
"strconv"
"github.com/iomz/go-llrp/binutil"
)
// Tag holds a single virtual tag content
type Tag struct {
PCBits uint16
EPC []byte
}
// MarshalBinary overwrites the marshaller in gob encoding *Tag
func (tag *Tag) MarshalBinary() (_ []byte, err error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(tag.PCBits)
enc.Encode(tag.EPC)
return buf.Bytes(), err
}
// UnmarshalBinary overwrites the unmarshaller in gob decoding *Tag
func (tag *Tag) UnmarshalBinary(data []byte) (err error) {
dec := gob.NewDecoder(bytes.NewReader(data))
if err = dec.Decode(&tag.PCBits); err != nil {
return
}
if err = dec.Decode(&tag.EPC); err != nil {
return
}
return
}
// IsEqual to another Tag by taking one as its argument
// return true if they are the same
func (tag *Tag) IsEqual(tt *Tag) bool {
if tag.PCBits == tt.PCBits && bytes.Equal(tag.EPC, tt.EPC) {
return true
}
return false
}
// IsDuplicate to test another Tag by comparing only EPC
// return true if the EPCs are the same
func (tag *Tag) IsDuplicate(tt *Tag) bool {
if bytes.Equal(tag.EPC, tt.EPC) {
return true
}
return false
}
// NewTag onstructs a Tag struct from a TagRecord
func NewTag(tagRecord *TagRecord) (*Tag, error) {
// PCbits
pc64, err := strconv.ParseUint(tagRecord.PCBits, 16, 16)
if err != nil {
return &Tag{}, err
}
pc := uint16(pc64)
// EPC
epc, err := makeByteID(tagRecord.EPC)
if err != nil {
return &Tag{}, err
}
return &Tag{pc, epc}, nil
}
func makeByteID(s string) ([]byte, error) {
id, err := binutil.ParseBinRuneSliceToUint8Slice([]rune(s))
return binutil.Pack([]interface{}{id}), err
}