| 1 |
package gatt |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
const ( |
| 6 |
StatusSuccess = 0 |
| 7 |
StatusInvalidOffset = 1 |
| 8 |
StatusUnexpectedError = 2 |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type Request struct { |
| 15 |
Central Central |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
type ReadRequest struct { |
| 20 |
Request |
| 21 |
Cap int |
| 22 |
Offset int |
| 23 |
} |
| 24 |
|
| 25 |
type Property int |
| 26 |
|
| 27 |
|
| 28 |
const ( |
| 29 |
CharBroadcast Property = 0x01 |
| 30 |
CharRead Property = 0x02 |
| 31 |
CharWriteNR Property = 0x04 |
| 32 |
CharWrite Property = 0x08 |
| 33 |
CharNotify Property = 0x10 |
| 34 |
CharIndicate Property = 0x20 |
| 35 |
CharSignedWrite Property = 0x40 |
| 36 |
CharExtended Property = 0x80 |
| 37 |
) |
| 38 |
|
| 39 |
func (p Property) String() (result string) { |
| 40 |
if (p & CharBroadcast) != 0 { |
| 41 |
result += "broadcast " |
| 42 |
} |
| 43 |
if (p & CharRead) != 0 { |
| 44 |
result += "read " |
| 45 |
} |
| 46 |
if (p & CharWriteNR) != 0 { |
| 47 |
result += "writeWithoutResponse " |
| 48 |
} |
| 49 |
if (p & CharWrite) != 0 { |
| 50 |
result += "write " |
| 51 |
} |
| 52 |
if (p & CharNotify) != 0 { |
| 53 |
result += "notify " |
| 54 |
} |
| 55 |
if (p & CharIndicate) != 0 { |
| 56 |
result += "indicate " |
| 57 |
} |
| 58 |
if (p & CharSignedWrite) != 0 { |
| 59 |
result += "authenticateSignedWrites " |
| 60 |
} |
| 61 |
if (p & CharExtended) != 0 { |
| 62 |
result += "extendedProperties " |
| 63 |
} |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
type Service struct { |
| 69 |
uuid UUID |
| 70 |
chars []*Characteristic |
| 71 |
|
| 72 |
h uint16 |
| 73 |
endh uint16 |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func NewService(u UUID) *Service { |
| 78 |
return &Service{uuid: u} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func (s *Service) AddCharacteristic(u UUID) *Characteristic { |
| 85 |
for _, c := range s.chars { |
| 86 |
if c.uuid.Equal(u) { |
| 87 |
panic("service already contains a characteristic with uuid " + u.String()) |
| 88 |
} |
| 89 |
} |
| 90 |
c := &Characteristic{uuid: u, svc: s} |
| 91 |
s.chars = append(s.chars, c) |
| 92 |
return c |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (s *Service) UUID() UUID { return s.uuid } |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func (s *Service) Name() string { |
| 101 |
return knownServices[s.uuid.String()].Name |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (s *Service) Handle() uint16 { return s.h } |
| 106 |
|
| 107 |
|
| 108 |
func (s *Service) EndHandle() uint16 { return s.endh } |
| 109 |
|
| 110 |
|
| 111 |
func (s *Service) SetHandle(h uint16) { s.h = h } |
| 112 |
|
| 113 |
|
| 114 |
func (s *Service) SetEndHandle(endh uint16) { s.endh = endh } |
| 115 |
|
| 116 |
|
| 117 |
func (s *Service) SetCharacteristics(chars []*Characteristic) { s.chars = chars } |
| 118 |
|
| 119 |
|
| 120 |
func (s *Service) Characteristics() []*Characteristic { return s.chars } |
| 121 |
|
| 122 |
|
| 123 |
type Characteristic struct { |
| 124 |
uuid UUID |
| 125 |
props Property |
| 126 |
secure Property |
| 127 |
svc *Service |
| 128 |
cccd *Descriptor |
| 129 |
descs []*Descriptor |
| 130 |
|
| 131 |
value []byte |
| 132 |
|
| 133 |
|
| 134 |
rhandler ReadHandler |
| 135 |
whandler WriteHandler |
| 136 |
nhandler NotifyHandler |
| 137 |
|
| 138 |
h uint16 |
| 139 |
vh uint16 |
| 140 |
endh uint16 |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func NewCharacteristic(u UUID, s *Service, props Property, h uint16, vh uint16) *Characteristic { |
| 145 |
c := &Characteristic{ |
| 146 |
uuid: u, |
| 147 |
svc: s, |
| 148 |
props: props, |
| 149 |
h: h, |
| 150 |
vh: vh, |
| 151 |
} |
| 152 |
|
| 153 |
return c |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func (c *Characteristic) Handle() uint16 { return c.h } |
| 158 |
|
| 159 |
|
| 160 |
func (c *Characteristic) VHandle() uint16 { return c.vh } |
| 161 |
|
| 162 |
|
| 163 |
func (c *Characteristic) EndHandle() uint16 { return c.endh } |
| 164 |
|
| 165 |
|
| 166 |
func (c *Characteristic) Descriptor() *Descriptor { return c.cccd } |
| 167 |
|
| 168 |
|
| 169 |
func (c *Characteristic) SetHandle(h uint16) { c.h = h } |
| 170 |
|
| 171 |
|
| 172 |
func (c *Characteristic) SetVHandle(vh uint16) { c.vh = vh } |
| 173 |
|
| 174 |
|
| 175 |
func (c *Characteristic) SetEndHandle(endh uint16) { c.endh = endh } |
| 176 |
|
| 177 |
|
| 178 |
func (c *Characteristic) SetDescriptor(cccd *Descriptor) { c.cccd = cccd } |
| 179 |
|
| 180 |
|
| 181 |
func (c *Characteristic) SetDescriptors(descs []*Descriptor) { c.descs = descs } |
| 182 |
|
| 183 |
|
| 184 |
func (c *Characteristic) UUID() UUID { |
| 185 |
return c.uuid |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func (c *Characteristic) Name() string { |
| 191 |
return knownCharacteristics[c.uuid.String()].Name |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func (c *Characteristic) Service() *Service { |
| 196 |
return c.svc |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
func (c *Characteristic) Properties() Property { |
| 201 |
return c.props |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (c *Characteristic) Descriptors() []*Descriptor { |
| 206 |
return c.descs |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func (c *Characteristic) AddDescriptor(u UUID) *Descriptor { |
| 213 |
for _, d := range c.descs { |
| 214 |
if d.uuid.Equal(u) { |
| 215 |
panic("service already contains a characteristic with uuid " + u.String()) |
| 216 |
} |
| 217 |
} |
| 218 |
d := &Descriptor{uuid: u, char: c} |
| 219 |
c.descs = append(c.descs, d) |
| 220 |
return d |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
func (c *Characteristic) SetValue(b []byte) { |
| 228 |
if c.rhandler != nil { |
| 229 |
panic("charactristic has been configured with a read handler") |
| 230 |
} |
| 231 |
c.props |= CharRead |
| 232 |
|
| 233 |
c.value = make([]byte, len(b)) |
| 234 |
copy(c.value, b) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
func (c *Characteristic) HandleRead(h ReadHandler) { |
| 242 |
if c.value != nil { |
| 243 |
panic("charactristic has been configured with a static value") |
| 244 |
} |
| 245 |
c.props |= CharRead |
| 246 |
|
| 247 |
c.rhandler = h |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
func (c *Characteristic) HandleReadFunc(f func(rsp ResponseWriter, req *ReadRequest)) { |
| 252 |
c.HandleRead(ReadHandlerFunc(f)) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
func (c *Characteristic) HandleWrite(h WriteHandler) { |
| 261 |
c.props |= CharWrite | CharWriteNR |
| 262 |
|
| 263 |
c.whandler = h |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
func (c *Characteristic) HandleWriteFunc(f func(r Request, data []byte) (status byte)) { |
| 268 |
c.HandleWrite(WriteHandlerFunc(f)) |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
func (c *Characteristic) HandleNotify(h NotifyHandler) { |
| 275 |
if c.cccd != nil { |
| 276 |
return |
| 277 |
} |
| 278 |
p := CharNotify | CharIndicate |
| 279 |
c.props |= p |
| 280 |
c.nhandler = h |
| 281 |
|
| 282 |
|
| 283 |
secure := Property(0) |
| 284 |
|
| 285 |
|
| 286 |
if c.secure&p != 0 { |
| 287 |
secure = CharRead | CharWrite |
| 288 |
} |
| 289 |
cd := &Descriptor{ |
| 290 |
uuid: attrClientCharacteristicConfigUUID, |
| 291 |
props: CharRead | CharWrite | CharWriteNR, |
| 292 |
secure: secure, |
| 293 |
|
| 294 |
|
| 295 |
value: []byte{0x00, 0x00}, |
| 296 |
char: c, |
| 297 |
} |
| 298 |
c.cccd = cd |
| 299 |
c.descs = append(c.descs, cd) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
func (c *Characteristic) HandleNotifyFunc(f func(r Request, n Notifier)) { |
| 304 |
c.HandleNotify(NotifyHandlerFunc(f)) |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
type Descriptor struct { |
| 313 |
uuid UUID |
| 314 |
char *Characteristic |
| 315 |
props Property |
| 316 |
secure Property |
| 317 |
|
| 318 |
h uint16 |
| 319 |
value []byte |
| 320 |
|
| 321 |
rhandler ReadHandler |
| 322 |
whandler WriteHandler |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
func (d *Descriptor) Handle() uint16 { return d.h } |
| 327 |
|
| 328 |
|
| 329 |
func (d *Descriptor) SetHandle(h uint16) { d.h = h } |
| 330 |
|
| 331 |
|
| 332 |
func NewDescriptor(u UUID, h uint16, char *Characteristic) *Descriptor { |
| 333 |
cd := &Descriptor{ |
| 334 |
uuid: u, |
| 335 |
h: h, |
| 336 |
char: char, |
| 337 |
} |
| 338 |
return cd |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
func (d *Descriptor) UUID() UUID { |
| 343 |
return d.uuid |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
func (d *Descriptor) Name() string { |
| 349 |
return knownDescriptors[d.uuid.String()].Name |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
func (d *Descriptor) Characteristic() *Characteristic { |
| 354 |
return d.char |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
func (d *Descriptor) SetValue(b []byte) { |
| 361 |
if d.rhandler != nil { |
| 362 |
panic("descriptor has been configured with a read handler") |
| 363 |
} |
| 364 |
d.props |= CharRead |
| 365 |
|
| 366 |
d.value = make([]byte, len(b)) |
| 367 |
copy(d.value, b) |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
func (d *Descriptor) HandleRead(h ReadHandler) { |
| 374 |
if d.value != nil { |
| 375 |
panic("descriptor has been configured with a static value") |
| 376 |
} |
| 377 |
d.props |= CharRead |
| 378 |
|
| 379 |
d.rhandler = h |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
func (d *Descriptor) HandleReadFunc(f func(rsp ResponseWriter, req *ReadRequest)) { |
| 384 |
d.HandleRead(ReadHandlerFunc(f)) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
func (d *Descriptor) HandleWrite(h WriteHandler) { |
| 391 |
d.props |= CharWrite | CharWriteNR |
| 392 |
|
| 393 |
d.whandler = h |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
func (d *Descriptor) HandleWriteFunc(f func(r Request, data []byte) (status byte)) { |
| 398 |
d.HandleWrite(WriteHandlerFunc(f)) |
| 399 |
} |