Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hexabus.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func (p *QueryPacket) Decode(packet []byte) (err error) {
// Hexabus Write Packet
// used to set a writeable entpoint id to a certain value, there is no response for that o
// other than an Error Packet on fail
// example:
// var wPack hexabus.WritePacket = hexabus.WritePacket{hexabus.FLAG_NONE, 1, hexabus.DTYPE_BOOL, false}
type WritePacket struct {
// 4 bytes header
// 1 byte packet type
Expand Down
34 changes: 6 additions & 28 deletions net6.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type EID struct {
Writable bool // writeable
}

// Switch IPv6 address, quantity Example:
// eids, err := hexabus.QueryEids("[fafa::50:c4ff:fe04:8390]", 32)
func QueryEids(address string, eid_qty uint16) ([]EID, error) {
eid_mask := []uint16{}
eid_descriptors := []uint16{}
Expand Down Expand Up @@ -104,6 +106,7 @@ func (p QueryPacket) Send(address string) ([]byte, error) {
if err != nil {
return nil, err
}
defer conn.Close()

err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
Expand Down Expand Up @@ -135,49 +138,23 @@ func (p WritePacket) Send(address string) error {
if !validPort.MatchString(address) {
address += ":" + PORT
}
readbuf := make([]byte, 152)
conn, err := net.DialTimeout("udp6", address, time.Duration(NET_TIMEOUT)*time.Second)
if err != nil {
return err
}
defer conn.Close()

err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
return err
}

// Write the packet
_, err = conn.Write(packet)
if err != nil {
return err
}

n, err := conn.Read(readbuf)

if err != nil {
if opErr, ok := err.(net.Error); ok && !opErr.Timeout() {
return err
}
}

if n > 0 {
err = checkCRC(readbuf[:n])
if err != nil {
return err
}
err = checkHeader(readbuf[:n])
if err != nil {
return err
}
ptype, err := PacketType(readbuf[:n])
if err != nil {
return err
}
if ptype == PTYPE_ERROR {
ep := ErrorPacket{}
ep.Decode(readbuf[:n])
return Error(ep.Error)
}
}
return nil
}

Expand All @@ -195,6 +172,7 @@ func (p EpQueryPacket) Send(address string) ([]byte, error) {
if err != nil {
return nil, err
}
defer conn.Close()

err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
Expand Down