|
3 | 3 | package ble
|
4 | 4 |
|
5 | 5 | import (
|
6 |
| - "fmt" |
7 | 6 | k "machine/usb/hid/keyboard"
|
8 | 7 |
|
9 | 8 | "tinygo.org/x/bluetooth"
|
@@ -75,7 +74,7 @@ func (k *Keyboard) Connect() error {
|
75 | 74 | return err
|
76 | 75 | }
|
77 | 76 |
|
78 |
| - registerHID() |
| 77 | + k.registerHID() |
79 | 78 |
|
80 | 79 | return nil
|
81 | 80 | }
|
@@ -224,238 +223,3 @@ func (k *Keyboard) Down(c k.Keycode) error {
|
224 | 223 | _, err := reportIn.Write(k.report[:9])
|
225 | 224 | return err
|
226 | 225 | }
|
227 |
| - |
228 |
| -func connect() error { |
229 |
| - |
230 |
| - // peerPKey := make([]byte, 0, 64) |
231 |
| - // privLesc, err := ecdh.P256().GenerateKey(rand.Reader) |
232 |
| - // if err != nil { |
233 |
| - // return err |
234 |
| - // } |
235 |
| - // lescChan := make(chan struct{}) |
236 |
| - bluetooth.SetSecParamsBonding() |
237 |
| - //bluetooth.SetSecParamsLesc() |
238 |
| - bluetooth.SetSecCapabilities(bluetooth.NoneGapIOCapability) |
239 |
| - // time.Sleep(4 * time.Second) |
240 |
| - // println("getting own pub key") |
241 |
| - // var key []byte |
242 |
| - |
243 |
| - // pk := privLesc.PublicKey().Bytes() |
244 |
| - // pubKey := swapEndinan(pk[1:]) |
245 |
| - //bluetooth.SetLesPublicKey(swapEndinan(privLesc.PublicKey().Bytes()[1:])) |
246 |
| - // pubKey = nil |
247 |
| - //println(" key is set") |
248 |
| - |
249 |
| - // println("register lesc callback") |
250 |
| - // adapter.SetLescRequestHandler( |
251 |
| - // func(pubKey []byte) { |
252 |
| - // peerPKey = pubKey |
253 |
| - // close(lescChan) |
254 |
| - // }, |
255 |
| - // ) |
256 |
| - |
257 |
| - println("def adv") |
258 |
| - adv := adapter.DefaultAdvertisement() |
259 |
| - println("adv config") |
260 |
| - adv.Configure(bluetooth.AdvertisementOptions{ |
261 |
| - LocalName: "tinygo-corne", |
262 |
| - ServiceUUIDs: []bluetooth.UUID{ |
263 |
| - bluetooth.ServiceUUIDDeviceInformation, |
264 |
| - bluetooth.ServiceUUIDBattery, |
265 |
| - bluetooth.ServiceUUIDHumanInterfaceDevice, |
266 |
| - }, |
267 |
| - }) |
268 |
| - println("adv start") |
269 |
| - return adv.Start() |
270 |
| - |
271 |
| - // select { |
272 |
| - // case <-lescChan: |
273 |
| - // peerPKey = append([]byte{0x04}, swapEndinan(peerPKey)...) |
274 |
| - // p, err := ecdh.P256().NewPublicKey(peerPKey) |
275 |
| - // if err != nil { |
276 |
| - // println("failed on parsing pub:", err.Error()) |
277 |
| - // return err |
278 |
| - // } |
279 |
| - // println("calculating ecdh") |
280 |
| - // key, err = privLesc.ECDH(p) |
281 |
| - // if err != nil { |
282 |
| - // println("failed on curving:", err.Error()) |
283 |
| - // return errfffffff |
284 |
| - // } |
285 |
| - // println("key len:", len(key)) |
286 |
| - // return bluetooth.ReplyLesc(swapEndinan(key)) |
287 |
| - // } |
288 |
| - |
289 |
| -} |
290 |
| - |
291 |
| -func swapEndinan(in []byte) []byte { |
292 |
| - var reverse = make([]byte, len(in)) |
293 |
| - for i, b := range in[:32] { |
294 |
| - |
295 |
| - reverse[31-i] = b |
296 |
| - } |
297 |
| - if len(in) > 32 { |
298 |
| - for i, b := range in[32:] { |
299 |
| - reverse[63-i] = b |
300 |
| - } |
301 |
| - } |
302 |
| - |
303 |
| - return reverse |
304 |
| -} |
305 |
| - |
306 |
| -func registerHID() { |
307 |
| - adapter.AddService(&bluetooth.Service{ |
308 |
| - UUID: bluetooth.ServiceUUIDDeviceInformation, |
309 |
| - Characteristics: []bluetooth.CharacteristicConfig{ |
310 |
| - { |
311 |
| - UUID: bluetooth.CharacteristicUUIDManufacturerNameString, |
312 |
| - Flags: bluetooth.CharacteristicReadPermission, |
313 |
| - Value: []byte("Nice Keyboards"), |
314 |
| - }, |
315 |
| - { |
316 |
| - UUID: bluetooth.CharacteristicUUIDModelNumberString, |
317 |
| - Flags: bluetooth.CharacteristicReadPermission, |
318 |
| - Value: []byte("nice!nano"), |
319 |
| - }, |
320 |
| - { |
321 |
| - UUID: bluetooth.CharacteristicUUIDPnPID, |
322 |
| - Flags: bluetooth.CharacteristicReadPermission, |
323 |
| - Value: []byte{0x02, 0x8a, 0x24, 0x66, 0x82, 0x34, 0x36}, |
324 |
| - //Value: []byte{0x02, uint8(0x10C4 >> 8), uint8(0x10C4 & 0xff), uint8(0x0001 >> 8), uint8(0x0001 & 0xff)}, |
325 |
| - }, |
326 |
| - }, |
327 |
| - }) |
328 |
| - adapter.AddService(&bluetooth.Service{ |
329 |
| - UUID: bluetooth.ServiceUUIDBattery, |
330 |
| - Characteristics: []bluetooth.CharacteristicConfig{ |
331 |
| - { |
332 |
| - UUID: bluetooth.CharacteristicUUIDBatteryLevel, |
333 |
| - Value: []byte{80}, |
334 |
| - Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicNotifyPermission, |
335 |
| - }, |
336 |
| - }, |
337 |
| - }) |
338 |
| - // gacc |
339 |
| - /* |
340 |
| - device name r |
341 |
| - apperance r |
342 |
| - peripheral prefreed connection |
343 |
| -
|
344 |
| - */ |
345 |
| - |
346 |
| - adapter.AddService(&bluetooth.Service{ |
347 |
| - UUID: bluetooth.ServiceUUIDGenericAccess, |
348 |
| - Characteristics: []bluetooth.CharacteristicConfig{ |
349 |
| - { |
350 |
| - UUID: bluetooth.CharacteristicUUIDDeviceName, |
351 |
| - Flags: bluetooth.CharacteristicReadPermission, |
352 |
| - Value: []byte("tinygo-corne"), |
353 |
| - }, |
354 |
| - { |
355 |
| - |
356 |
| - UUID: bluetooth.New16BitUUID(0x2A01), |
357 |
| - Flags: bluetooth.CharacteristicReadPermission, |
358 |
| - Value: []byte{uint8(0x03c4 >> 8), uint8(0x03c4 & 0xff)}, /// []byte(strconv.Itoa(961)), |
359 |
| - }, |
360 |
| - // { |
361 |
| - // UUID: bluetooth.CharacteristicUUIDPeripheralPreferredConnectionParameters, |
362 |
| - // Flags: bluetooth.CharacteristicReadPermission, |
363 |
| - // Value: []byte{0x02}, |
364 |
| - // }, |
365 |
| - |
366 |
| - // // // |
367 |
| - }, |
368 |
| - }) |
369 |
| - |
370 |
| - //v := []byte{0x85, 0x02} // 0x85, 0x02 |
371 |
| - reportValue := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} |
372 |
| - |
373 |
| - //var reportmap bluetooth.Characteristic |
374 |
| - |
375 |
| - // hid |
376 |
| - adapter.AddService(&bluetooth.Service{ |
377 |
| - UUID: bluetooth.ServiceUUIDHumanInterfaceDevice, |
378 |
| - /* |
379 |
| - - hid information r |
380 |
| - - report map r |
381 |
| - - report nr |
382 |
| - - client charecteristic configuration |
383 |
| - - report reference |
384 |
| - - report nr |
385 |
| - - client charecteristic configuration |
386 |
| - - report reference |
387 |
| - - hid control point wnr |
388 |
| - */ |
389 |
| - Characteristics: []bluetooth.CharacteristicConfig{ |
390 |
| - // { |
391 |
| - // UUID: bluetooth.CharacteristicUUIDHIDInformation, |
392 |
| - // Flags: bluetooth.CharacteristicReadPermission, |
393 |
| - // Value: []byte{uint8(0x0111 >> 8), uint8(0x0111 & 0xff), uint8(0x0002 >> 8), uint8(0x0002 & 0xff)}, |
394 |
| - // }, |
395 |
| - { |
396 |
| - //Handle: &reportmap, |
397 |
| - UUID: bluetooth.CharacteristicUUIDReportMap, |
398 |
| - Flags: bluetooth.CharacteristicReadPermission, |
399 |
| - Value: reportMap, |
400 |
| - }, |
401 |
| - { |
402 |
| - |
403 |
| - Handle: &reportIn, |
404 |
| - UUID: bluetooth.CharacteristicUUIDReport, |
405 |
| - Value: reportValue[:], |
406 |
| - Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicNotifyPermission, |
407 |
| - }, |
408 |
| - { |
409 |
| - // protocl mode |
410 |
| - UUID: bluetooth.New16BitUUID(0x2A4E), |
411 |
| - Flags: bluetooth.CharacteristicWriteWithoutResponsePermission | bluetooth.CharacteristicReadPermission, |
412 |
| - // Value: []byte{uint8(1)}, |
413 |
| - // WriteEvent: func(client bluetooth.Connection, offset int, value []byte) { |
414 |
| - // print("protocol mode") |
415 |
| - // }, |
416 |
| - }, |
417 |
| - { |
418 |
| - UUID: bluetooth.CharacteristicUUIDHIDControlPoint, |
419 |
| - Flags: bluetooth.CharacteristicWriteWithoutResponsePermission, |
420 |
| - // Value: []byte{0x02}, |
421 |
| - }, |
422 |
| - }, |
423 |
| - }) |
424 |
| -} |
425 |
| - |
426 |
| -func connectToSplit() (bluetooth.DeviceCharacteristic, error) { |
427 |
| - var tx bluetooth.DeviceCharacteristic |
428 |
| - var foundDevice bluetooth.ScanResult |
429 |
| - err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) { |
430 |
| - fmt.Printf("%q:%#v\n", result.LocalName(), result.Address.String()) |
431 |
| - if result.LocalName() != "corne-right" { |
432 |
| - return |
433 |
| - } |
434 |
| - foundDevice = result |
435 |
| - |
436 |
| - // Stop the scan. |
437 |
| - err := adapter.StopScan() |
438 |
| - if err != nil { |
439 |
| - return |
440 |
| - } |
441 |
| - }) |
442 |
| - if err != nil { |
443 |
| - return tx, err |
444 |
| - } |
445 |
| - device, err := adapter.Connect(foundDevice.Address, bluetooth.ConnectionParams{}) |
446 |
| - if err != nil { |
447 |
| - return tx, err |
448 |
| - } |
449 |
| - services, err := device.DiscoverServices([]bluetooth.UUID{bluetooth.ServiceUUIDNordicUART}) |
450 |
| - if err != nil { |
451 |
| - return tx, err |
452 |
| - } |
453 |
| - service := services[0] |
454 |
| - chars, err := service.DiscoverCharacteristics([]bluetooth.UUID{bluetooth.CharacteristicUUIDUARTTX}) |
455 |
| - if err != nil { |
456 |
| - return tx, err |
457 |
| - } |
458 |
| - tx = chars[0] |
459 |
| - return tx, nil |
460 |
| - |
461 |
| -} |
0 commit comments