Skip to content

Commit

Permalink
improve log format of ping command
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Jan 13, 2024
1 parent 10f20de commit 29b8d0c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ Use "halsecur [command] --help" for more information about a command.

### Ping
```bash
$ ./halsecur ping --host 192.168.3.232 --mac 54:10:EC:85:28:BB --debug --count 1
DEBU[2023-12-26T20:58:34+01:00] Connecting to 192.168.3.232:4000
DEBU[2023-12-26T20:58:34+01:00] requestTC: SrcMAC=0x000000000009, DstMAC=0x5410EC8528BB, BodyLength=0x0, packet=[Tag=0x1, Token=0x476B2ED2, CommandID=0x0 (0x0), payload=[], Checksum=0x0, isResponse=false], Checksum=0x0, isResponse: false
DEBU[2023-12-26T20:58:34+01:00] Request: SrcMAC=0x000000000009, DstMAC=0x5410EC8528BB, BodyLength=0x0, packet=[Tag=0x1, Token=0x476B2ED2, CommandID=0x0 (0x0), payload=[], Checksum=0x0, isResponse=false], Checksum=0x0, isResponse: false
DEBU[2023-12-26T20:58:34+01:00] Request bytes: 3030303030303030303030393534313045433835323842423030303930313437364232454432303042434435
DEBU[2023-12-26T20:58:34+01:00] Length of received bytes: 44
DEBU[2023-12-26T20:58:34+01:00] Response bytes: 5410EC8528BB000000000006000901476B2ED2803CCB
DEBU[2023-12-26T20:58:34+01:00] Received TC: SrcMAC=0x5410EC8528BB, DstMAC=0x000000000006, BodyLength=0x9, packet=[Tag=0x1, Token=0x476B2ED2, CommandID=0x0 (0x80), payload=[], Checksum=0x3C, isResponse=true], Checksum=0xCB, isResponse: true
DEBU[2023-12-26T20:58:34+01:00] received 1 packet(s)
DEBU[2023-12-26T20:58:36+01:00] -------------------
$ ./halsecur ping --host 192.168.3.232 --mac 54:10:EC:85:28:BB --count 3 --delay 1000
INFO[2024-01-13T22:36:49+01:00] Response received in 65 ms
INFO[2024-01-13T22:36:50+01:00] Response received in 64 ms
INFO[2024-01-13T22:36:51+01:00] Response received in 62 ms
```

### Get device name
Expand Down
22 changes: 18 additions & 4 deletions cli/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ package cmd
import (
"bisecur/cli"
"bisecur/sdk"
"fmt"
"github.com/spf13/viper"
"os"
"time"

"github.com/spf13/cobra"
)

const (
minDalayValue = 500
)

func init() {
var (
count int
delay int
)

pingCmd := &cobra.Command{
Expand All @@ -25,13 +32,19 @@ func init() {
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

// input validation. Try not to do DOS attack against the gateway.
if delay < minDalayValue {
log.Fatalf("Invalid delay value: %d", delay)
os.Exit(1)
}

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = ping(localMac, mac, host, port, count, token)
err = ping(localMac, mac, host, port, count, time.Duration(delay)*time.Millisecond, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
Expand All @@ -40,10 +53,11 @@ func init() {
}
rootCmd.AddCommand(pingCmd)

pingCmd.Flags().IntVar(&count, "count", 3, "Amount of the ping packages will be sent to the device")
pingCmd.Flags().IntVarP(&count, "count", "c", 3, "Number of ping packages")
pingCmd.Flags().IntVarP(&delay, "delay", "d", 1000, fmt.Sprintf("Miliseconds between ping packets. Must be at least %d", minDalayValue))
}

func ping(localMac [6]byte, mac [6]byte, host string, port int, count int, token uint32) error {
func ping(localMac [6]byte, mac [6]byte, host string, port int, count int, delay time.Duration, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
Expand All @@ -57,7 +71,7 @@ func ping(localMac [6]byte, mac [6]byte, host string, port int, count int, token
}
}()

err = client.Ping(count)
err = client.Ping(count, delay)
if err != nil {
return err
}
Expand Down
25 changes: 20 additions & 5 deletions sdk/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,45 @@ func (c *Client) IsOpened() bool {
return c.connection != nil
}

func (c *Client) Ping(count int) error {
func (c *Client) Ping(count int, delay time.Duration) error {
received := 0

for i := 0; i < count; i++ {
requestTc := c.getTransmissionContainer(COMMANDID_PING, payload.EmptyPayload())
c.log.Debugf("requestTC: %v", requestTc)

sendTimestamp := time.Now().UnixMilli()
responseTc, err := c.transmitCommandWithResponse(requestTc)
receivedTimestamp := time.Now().UnixMilli()

c.log.Debugf("responseTC: %v", responseTc)

if err != nil {
return fmt.Errorf("%v", err)
//return fmt.Errorf("%v", err)
c.log.Errorf("%v", err)
continue
}

if responseTc == nil {
return fmt.Errorf("unexpected nil responseTc value")
//return fmt.Errorf("unexpected nil responseTc value")
c.log.Errorf("unexpected nil responseTc value")
continue
}

if !responseTc.isResponseFor(requestTc) {
return fmt.Errorf("received unexpected packet: %s", responseTc)
//return fmt.Errorf("received unexpected packet", responseTc)
c.log.Errorf("received unexpected packet")
continue
}

received = received + 1
c.log.Debugf("received %d packet(s)", received)

rtt := receivedTimestamp - sendTimestamp
c.log.Infof("Response received in %d ms", rtt)

if i < count {
time.Sleep(time.Second * 2)
time.Sleep(delay)
c.log.Debugf("-------------------")
}
}
Expand Down

0 comments on commit 29b8d0c

Please sign in to comment.