Skip to content

Commit

Permalink
rename dump
Browse files Browse the repository at this point in the history
  • Loading branch information
half2me committed Feb 14, 2024
1 parent 111609f commit d082208
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 46 deletions.
47 changes: 1 addition & 46 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,20 @@ import (
"context"
"github.com/half2me/antgo/ant"
"github.com/half2me/antgo/device"
"github.com/half2me/antgo/driver/emulator"
"github.com/half2me/antgo/driver/usb"
"log"
"net"
"os/signal"
"syscall"
"time"
)

const (
HOST = "localhost"
PORT = "9999"
TYPE = "tcp"
)

func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

// TCP Client
tcpServer, err := net.ResolveTCPAddr(TYPE, HOST+":"+PORT)

if err != nil {
log.Println(err.Error())
return
}

conn, err := net.DialTCP(TYPE, nil, tcpServer)
if err != nil {
log.Println(err.Error())
return
}

//close the connection
defer conn.Close()

// workaround for libusb log bug
log.SetOutput(usb.FixLibUsbLog(log.Writer()))

//dev, err := usb.AutoDetectDevice()
dev, err := emulator.NewEmulator("examples/123.cap")
dev, err := usb.AutoDetectDevice()
defer dev.Close()
if err != nil {
log.Println(err.Error())
Expand All @@ -70,24 +43,6 @@ func main() {
go device.DumpBroadcastMessages(ctx, dev, messages)

for msg := range messages {
<-time.After(500 * time.Millisecond) // Artificial delay for
log.Println(msg)

// we only need s&c or power messages
if !(msg.DeviceType() == ant.DEVICE_TYPE_SPEED_AND_CADENCE ||
msg.DeviceType() == ant.DEVICE_TYPE_POWER) {
continue
}

// ignore power sensor service packets
if msg.DeviceType() == ant.DEVICE_TYPE_POWER && ant.PowerMessage(msg).DataPageNumber() != 0x10 {
continue
}

_, err = conn.Write(msg)
if err != nil {
log.Printf("Write failed: %s\n", err.Error())
stop()
}
}
}
93 changes: 93 additions & 0 deletions cmd/netdump/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"context"
"github.com/half2me/antgo/ant"
"github.com/half2me/antgo/device"
"github.com/half2me/antgo/driver/emulator"
"github.com/half2me/antgo/driver/usb"
"log"
"net"
"os/signal"
"syscall"
"time"
)

const (
HOST = "localhost"
PORT = "9999"
TYPE = "tcp"
)

func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

// TCP Client
tcpServer, err := net.ResolveTCPAddr(TYPE, HOST+":"+PORT)

if err != nil {
log.Println(err.Error())
return
}

conn, err := net.DialTCP(TYPE, nil, tcpServer)
if err != nil {
log.Println(err.Error())
return
}

//close the connection
defer conn.Close()

// workaround for libusb log bug
log.SetOutput(usb.FixLibUsbLog(log.Writer()))

//dev, err := usb.AutoDetectDevice()
dev, err := emulator.NewEmulator("examples/123.cap")
defer dev.Close()
if err != nil {
log.Println(err.Error())
return
}

// initialize device
err = device.Reset(dev)
if err != nil {
log.Println(err.Error())
return
}

// Start RX Scan mode
err = device.StartRxScanMode(dev)
if err != nil {
log.Println(err.Error())
return
}

// Start reading broadcast messages
messages := make(chan ant.BroadcastMessage)
go device.DumpBroadcastMessages(ctx, dev, messages)

for msg := range messages {
<-time.After(500 * time.Millisecond) // Artificial delay for
log.Println(msg)

// we only need s&c or power messages
if !(msg.DeviceType() == ant.DEVICE_TYPE_SPEED_AND_CADENCE ||
msg.DeviceType() == ant.DEVICE_TYPE_POWER) {
continue
}

// ignore power sensor service packets
if msg.DeviceType() == ant.DEVICE_TYPE_POWER && ant.PowerMessage(msg).DataPageNumber() != 0x10 {
continue
}

_, err = conn.Write(msg)
if err != nil {
log.Printf("Write failed: %s\n", err.Error())
stop()
}
}
}

0 comments on commit d082208

Please sign in to comment.