Debian Codegraph @

examples/server_lnx.go

raw symbols SCIP
1 // +build
2
3 package main
4
5 import (
6 "bytes"
7 "flag"
8 "fmt"
9 "log"
10 "time"
11
12 "github.com/paypal/gatt"
13 "github.com/paypal/gatt/examples/service"
14 "github.com/paypal/gatt/linux/cmd"
15 )
16
17 // server_lnx implements a GATT server.
18 // It uses some linux specific options for more finer control over the device.
19
20 var (
21 mc = flag.Int("mc", 1, "Maximum concurrent connections")
22 id = flag.Duration("id", 0, "ibeacon duration")
23 ii = flag.Duration("ii", 5*time.Second, "ibeacon interval")
24 name = flag.String("name", "Gopher", "Device Name")
25 chmap = flag.Int("chmap", 0x7, "Advertising channel map")
26 dev = flag.Int("dev", -1, "HCI device ID")
27 chk = flag.Bool("chk", true, "Check device LE support")
28 )
29
30 // cmdReadBDAddr implements cmd.CmdParam for demostrating LnxSendHCIRawCommand()
31 type cmdReadBDAddr struct{}
32
33 func (c cmdReadBDAddr) Marshal(b []byte) {}
34 func (c cmdReadBDAddr) Opcode() int { return 0x1009 }
35 func (c cmdReadBDAddr) Len() int { return 0 }
36
37 // Get bdaddr with LnxSendHCIRawCommand() for demo purpose
38 func bdaddr(d gatt.Device) {
39 rsp := bytes.NewBuffer(nil)
40 if err := d.Option(gatt.LnxSendHCIRawCommand(&cmdReadBDAddr{}, rsp)); err != nil {
41 fmt.Printf("Failed to send HCI raw command, err: %s", err)
42 }
43 b := rsp.Bytes()
44 if b[0] != 0 {
45 fmt.Printf("Failed to get bdaddr with HCI Raw command, status: %d", b[0])
46 }
47 log.Printf("BD Addr: %02X:%02X:%02X:%02X:%02X:%02X", b[6], b[5], b[4], b[3], b[2], b[1])
48 }
49
50 func main() {
51 flag.Parse()
52 d, err := gatt.NewDevice(
53 gatt.LnxMaxConnections(*mc),
54 gatt.LnxDeviceID(*dev, *chk),
55 gatt.LnxSetAdvertisingParameters(&cmd.LESetAdvertisingParameters{
56 AdvertisingIntervalMin: 0x00f4,
57 AdvertisingIntervalMax: 0x00f4,
58 AdvertisingChannelMap: 0x07,
59 }),
60 )
61
62 if err != nil {
63 log.Printf("Failed to open device, err: %s", err)
64 return
65 }
66
67 // Register optional handlers.
68 d.Handle(
69 gatt.CentralConnected(func(c gatt.Central) { log.Println("Connect: ", c.ID()) }),
70 gatt.CentralDisconnected(func(c gatt.Central) { log.Println("Disconnect: ", c.ID()) }),
71 )
72
73 // A mandatory handler for monitoring device state.
74 onStateChanged := func(d gatt.Device, s gatt.State) {
75 fmt.Printf("State: %s\n", s)
76 switch s {
77 case gatt.StatePoweredOn:
78 // Get bdaddr with LnxSendHCIRawCommand()
79 bdaddr(d)
80
81 // Setup GAP and GATT services.
82 d.AddService(service.NewGapService(*name))
83 d.AddService(service.NewGattService())
84
85 // Add a simple counter service.
86 s1 := service.NewCountService()
87 d.AddService(s1)
88
89 // Add a simple counter service.
90 s2 := service.NewBatteryService()
91 d.AddService(s2)
92 uuids := []gatt.UUID{s1.UUID(), s2.UUID()}
93
94 // If id is zero, advertise name and services statically.
95 if *id == time.Duration(0) {
96 d.AdvertiseNameAndServices(*name, uuids)
97 break
98 }
99
100 // If id is non-zero, advertise name and services and iBeacon alternately.
101 go func() {
102 for {
103 // Advertise as a RedBear Labs iBeacon.
104 d.AdvertiseIBeacon(gatt.MustParseUUID("5AFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), 1, 2, -59)
105 time.Sleep(*id)
106
107 // Advertise name and services.
108 d.AdvertiseNameAndServices(*name, uuids)
109 time.Sleep(*ii)
110 }
111 }()
112
113 default:
114 }
115 }
116
117 d.Init(onStateChanged)
118 select {}
119 }