func cmdRead(c *cli.Context) error { if err := doGetUUID(c); err != nil { return err } if err := doConnect(c); err != nil { return err } if err := doDiscover(c); err != nil { return err } if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil { b, err := curr.client.ReadCharacteristic(u.(*ble.Characteristic)) if err != nil { return errors.Wrap(err, "can't read characteristic") } fmt.Printf(" Value %x | %q\n", b, b) return nil } if u := curr.profile.Find(ble.NewDescriptor(curr.uuid)); u != nil { b, err := curr.client.ReadDescriptor(u.(*ble.Descriptor)) if err != nil { return errors.Wrap(err, "can't read descriptor") } fmt.Printf(" Value %x | %q\n", b, b) return nil } return errNoUUID }
func newCCCD(c *ble.Characteristic) *ble.Descriptor { d := ble.NewDescriptor(ble.ClientCharacteristicConfigUUID) d.HandleRead(ble.ReadHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) { cccs := req.Conn().(*conn).cccs ccc := cccs[c.Handle] binary.Write(rsp, binary.LittleEndian, ccc) })) d.HandleWrite(ble.WriteHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) { cn := req.Conn().(*conn) old := cn.cccs[c.Handle] ccc := binary.LittleEndian.Uint16(req.Data()) oldNotify := old&cccNotify != 0 oldIndicate := old&cccIndicate != 0 newNotify := ccc&cccNotify != 0 newIndicate := ccc&cccIndicate != 0 if newNotify && !oldNotify { if c.Property&ble.CharNotify == 0 { rsp.SetStatus(ble.ErrUnlikely) return } send := func(b []byte) (int, error) { return cn.svr.notify(c.ValueHandle, b) } cn.nn[c.Handle] = ble.NewNotifier(send) go c.NotifyHandler.ServeNotify(req, cn.nn[c.Handle]) } if !newNotify && oldNotify { cn.nn[c.Handle].Close() } if newIndicate && !oldIndicate { if c.Property&ble.CharIndicate == 0 { rsp.SetStatus(ble.ErrUnlikely) return } send := func(b []byte) (int, error) { return cn.svr.indicate(c.ValueHandle, b) } cn.in[c.Handle] = ble.NewNotifier(send) go c.IndicateHandler.ServeNotify(req, cn.in[c.Handle]) } if !newIndicate && oldIndicate { cn.in[c.Handle].Close() } cn.cccs[c.Handle] = ccc })) return d }
func cmdWrite(c *cli.Context) error { if err := doGetUUID(c); err != nil { return err } if err := doConnect(c); err != nil { return err } if err := doDiscover(c); err != nil { return err } if u := curr.profile.Find(ble.NewCharacteristic(curr.uuid)); u != nil { err := curr.client.WriteCharacteristic(u.(*ble.Characteristic), []byte("hello"), true) return errors.Wrap(err, "can't write characteristic") } if u := curr.profile.Find(ble.NewDescriptor(curr.uuid)); u != nil { err := curr.client.WriteDescriptor(u.(*ble.Descriptor), []byte("fixme")) return errors.Wrap(err, "can't write descriptor") } return errNoUUID }