summaryrefslogtreecommitdiff
path: root/software/communication/data_link.go
diff options
context:
space:
mode:
Diffstat (limited to 'software/communication/data_link.go')
-rw-r--r--software/communication/data_link.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/software/communication/data_link.go b/software/communication/data_link.go
new file mode 100644
index 0000000..d6e752d
--- /dev/null
+++ b/software/communication/data_link.go
@@ -0,0 +1,63 @@
+package communication
+
+const (
+ SLIP_END = 0xC0
+ SLIP_ESC = 0xDB
+ SLIP_ESC_END = 0xDC
+ SLIP_ESC_ESC = 0xDD
+ SLIP_MAX_PAYLOAD = 1500
+)
+
+type dataLink struct {
+ rx chan []byte
+}
+
+func newDataLink() dataLink {
+ dl := dataLink{}
+
+ dl.rx = make(chan []byte)
+
+ return dl
+}
+
+func (dl *dataLink) start(source chan byte) {
+ go dl.receive(source)
+}
+
+func (dl *dataLink) receive(source chan byte) {
+ for {
+ frame := unslip(source)
+ dl.rx <- frame
+ }
+}
+
+func unslip(source chan byte) []byte {
+ escaped := false
+ buffer := make([]byte, 0)
+
+ for {
+ octet := <-source
+ if escaped {
+ switch octet {
+ case SLIP_ESC_END:
+ buffer = append(buffer, SLIP_END)
+ case SLIP_ESC_ESC:
+ buffer = append(buffer, SLIP_ESC)
+ }
+ } else {
+ switch octet {
+ case SLIP_END:
+ break
+ case SLIP_ESC:
+ escaped = true
+ continue
+ default:
+ buffer = append(buffer, octet)
+ continue
+ }
+ break
+ }
+ }
+
+ return buffer
+}