| 1 |
package gatt |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
type testHandler struct { |
| 13 |
readc chan []byte |
| 14 |
writec chan []byte |
| 15 |
} |
| 16 |
|
| 17 |
func (t *testHandler) Read(b []byte) (int, error) { |
| 18 |
r := <-t.readc |
| 19 |
if len(r) > len(b) { |
| 20 |
panic("fix this annoyance properly") |
| 21 |
} |
| 22 |
n := copy(b, r) |
| 23 |
return n, nil |
| 24 |
} |
| 25 |
|
| 26 |
func (t *testHandler) Write(b []byte) (int, error) { |
| 27 |
t.writec <- b |
| 28 |
return len(b), nil |
| 29 |
} |
| 30 |
|
| 31 |
func (t *testHandler) Close() error { return nil } |
| 32 |
|
| 33 |
func TestServing(t *testing.T) { |
| 34 |
h := &testHandler{readc: make(chan []byte), writec: make(chan []byte)} |
| 35 |
|
| 36 |
var wrote []byte |
| 37 |
|
| 38 |
svc := &Service{uuid: MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b")} |
| 39 |
|
| 40 |
svc.AddCharacteristic(MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc( |
| 41 |
func(resp ResponseWriter, req *ReadRequest) { |
| 42 |
io.WriteString(resp, "count: 1") |
| 43 |
}) |
| 44 |
|
| 45 |
svc.AddCharacteristic(MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b")).HandleWriteFunc( |
| 46 |
func(r Request, data []byte) (status byte) { |
| 47 |
wrote = data |
| 48 |
return StatusSuccess |
| 49 |
}) |
| 50 |
|
| 51 |
svc.AddCharacteristic(MustParseUUID("1c927b50-c116-11e3-8a33-0800200c9a66")).HandleNotifyFunc( |
| 52 |
func(r Request, n Notifier) { |
| 53 |
go func() { |
| 54 |
count := 0 |
| 55 |
for !n.Done() { |
| 56 |
data := []byte(fmt.Sprintf("Count: %d", count)) |
| 57 |
_, err := n.Write(data) |
| 58 |
if err != nil { |
| 59 |
panic(err) |
| 60 |
} |
| 61 |
count++ |
| 62 |
time.Sleep(10 * time.Millisecond) |
| 63 |
} |
| 64 |
}() |
| 65 |
}) |
| 66 |
|
| 67 |
longString := "A really long characteristic" |
| 68 |
svc.AddCharacteristic(MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51c")).HandleReadFunc( |
| 69 |
func(resp ResponseWriter, req *ReadRequest) { |
| 70 |
start := req.Offset |
| 71 |
end := req.Offset + req.Cap |
| 72 |
if len(longString) < start { |
| 73 |
start = len(longString) |
| 74 |
} |
| 75 |
|
| 76 |
if len(longString) < end { |
| 77 |
end = len(longString) |
| 78 |
} |
| 79 |
io.WriteString(resp, longString[start:end]) |
| 80 |
}) |
| 81 |
|
| 82 |
svc.AddCharacteristic(MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51d")).SetValue([]byte(longString)) |
| 83 |
|
| 84 |
gapSvc := NewService(attrGAPUUID) |
| 85 |
|
| 86 |
gapSvc.AddCharacteristic(attrDeviceNameUUID).SetValue([]byte("Gopher")) |
| 87 |
gapSvc.AddCharacteristic(attrAppearanceUUID).SetValue([]byte{0x00, 0x80}) |
| 88 |
gattSvc := NewService(attrGATTUUID) |
| 89 |
|
| 90 |
a := generateAttributes([]*Service{gapSvc, gattSvc, svc}, uint16(1)) |
| 91 |
go newCentral(a, net.HardwareAddr{}, h).loop() |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
rxtx := []struct { |
| 112 |
name string |
| 113 |
send string |
| 114 |
want string |
| 115 |
after func() |
| 116 |
}{ |
| 117 |
{ |
| 118 |
name: "set mtu to 135 -- mtu is 135", |
| 119 |
send: "028700", |
| 120 |
want: "038700", |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "set mtu to 23 -- mtu is 23", |
| 124 |
send: "021700", |
| 125 |
want: "031700", |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "bad req -- unsupported", |
| 129 |
send: "FF1234567890", |
| 130 |
want: "01ff000006", |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "find info [1,10] -- 1: 0x2800, 2: 0x2803, 3: 0x2a00, 4: 0x2803, 5: 0x2a01", |
| 134 |
send: "0401000A00", |
| 135 |
want: "050101000028020003280300002a040003280500012a", |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "find info [1,2] -- 1: 0x2800, 2: 0x2803", |
| 139 |
send: "0401000200", |
| 140 |
want: "05010100002802000328", |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "find by type [1,11] svc uuid -- handle range [7,14]", |
| 144 |
send: "0601000B0000281bc5d5a502000499e31111c1c095fc09", |
| 145 |
want: "070700ffff", |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "read by group [1,3] svc uuid -- unsupported group type at handle 1", |
| 149 |
send: "10010003001bc5d5a502000499e31111c1c095fc09", |
| 150 |
want: "0110010010", |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "read by group [1,3] 0x2800 -- group at [1,5]: 0x1800", |
| 154 |
send: "10010003000028", |
| 155 |
want: "1106010005000018", |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "read by group [1,14] 0x2800 -- group at [1,5]: 0x1800, [6,6]: 0x1801", |
| 159 |
send: "1001000E000028", |
| 160 |
want: "1106010005000018060006000118", |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "read by type [1,5] 0x2a00 (device name) -- found 2, 3", |
| 164 |
send: "0801000500002a", |
| 165 |
want: "09080300476f70686572", |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "read by type [4,5] 0x2a00 (device name) -- not found", |
| 169 |
send: "0804000500002a", |
| 170 |
want: "010804000a", |
| 171 |
}, |
| 172 |
{ |
| 173 |
name: "read by type [6,6] 0x2803 (attr char) -- not found", |
| 174 |
send: "08060006000328", |
| 175 |
want: "010806000a", |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "read char -- 'count: 1'", |
| 179 |
send: "0a0900", |
| 180 |
want: "0b636f756e743a2031", |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "read long char with handler -- 'A really long characte'", |
| 184 |
send: "0a1000", |
| 185 |
want: "0b41207265616c6c79206c6f6e67206368617261637465", |
| 186 |
}, |
| 187 |
{ |
| 188 |
name: "finish read long char with handler - '6973746963'", |
| 189 |
send: "0c10001700", |
| 190 |
want: "0d6973746963", |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "read long char with value -- 'A really long characte'", |
| 194 |
send: "0a1200", |
| 195 |
want: "0b41207265616c6c79206c6f6e67206368617261637465", |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "finish read long char with value - '6973746963'", |
| 199 |
send: "0c12001700", |
| 200 |
want: "0d6973746963", |
| 201 |
}, |
| 202 |
|
| 203 |
{ |
| 204 |
name: "write char 'abcdef' -- ok", |
| 205 |
send: "120b00616263646566", |
| 206 |
want: "13", |
| 207 |
after: func() { |
| 208 |
if string(wrote) != "abcdef" { |
| 209 |
t.Errorf("wrote: got %q want %q", wrote, "abcdef") |
| 210 |
} |
| 211 |
}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "start notify -- ok", |
| 215 |
send: "120e000100", |
| 216 |
want: "13", |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "-- notified 'Count: 0'", |
| 220 |
want: "1b0d00436f756e743a2030", |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "-- notified 'Count: 1'", |
| 224 |
want: "1b0d00436f756e743a2031", |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "-- notified 'Count: 2'", |
| 228 |
want: "1b0d00436f756e743a2032", |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "-- notified 'Count: 3'", |
| 232 |
want: "1b0d00436f756e743a2033", |
| 233 |
}, |
| 234 |
{ |
| 235 |
name: "stop notify -- ok", |
| 236 |
send: "120e000000", |
| 237 |
want: "13", |
| 238 |
}, |
| 239 |
} |
| 240 |
|
| 241 |
for _, tt := range rxtx { |
| 242 |
s, _ := hex.DecodeString(tt.send) |
| 243 |
if tt.send != "" { |
| 244 |
h.readc <- s |
| 245 |
} |
| 246 |
got := hex.EncodeToString(<-h.writec) |
| 247 |
if got != tt.want { |
| 248 |
t.Errorf("%s: sent %s got %s want %s", tt.name, tt.send, got, tt.want) |
| 249 |
continue |
| 250 |
} |
| 251 |
if tt.after != nil { |
| 252 |
tt.after() |
| 253 |
} |
| 254 |
} |
| 255 |
} |