File: scanner.go

package info (click to toggle)
golang-github-bettercap-nrf24 0.0~git20190219.aa37e6d-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: makefile: 3
file content (69 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download
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
package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/bettercap/nrf24"
)

var (
	hopPeriod = 100 * time.Millisecond
)

func address(raw []byte) string {
	parts := []string{}
	for _, b := range raw {
		parts = append(parts, fmt.Sprintf("%02X", b))
	}
	return strings.Join(parts, ":")
}

func main() {
	fmt.Printf("nRF24LU1+ - RFStorm Scanner\n\n")

	dongle, err := nrf24.Open()
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	defer dongle.Close()

	fmt.Printf("device open: %s\n", dongle.String())

	if err = dongle.EnableLNA(); err != nil {
		fmt.Printf("error enabling LNA: %v\n", err)
	} else {
		fmt.Printf("LNA enabled\n")
	}

	if err = dongle.EnterPromiscMode(); err != nil {
		fmt.Printf("error: %v\n", err)
		return
	} else {
		fmt.Printf("device is in promisc mode\n\n")
	}

	ch := 1
	chIndex := 0
	lastHop := time.Time{}

	for {
		if time.Since(lastHop) >= hopPeriod {
			ch = nrf24.LoopChannels(&chIndex)
			if err := dongle.SetChannel(ch); err != nil {
				fmt.Printf("error setting channel %d: %v\n", ch, err)
			} else {
				lastHop = time.Now()
			}
		}

		if buf, err := dongle.ReceivePayload(); err != nil {
			fmt.Printf("error receiving payload on channel %d: %v\n", ch, err)
		} else if len(buf) >= 5 {
			addr, payload := buf[0:5], buf[5:]
			fmt.Printf("found device %s on channel %d (payload:%x)\n", address(addr), ch, payload)
		}
	}
}