Example #1
0
func NewCountService() *gatt.Service {
	n := 0
	s := gatt.NewService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b"))
	s.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			fmt.Fprintf(rsp, "count: %d", n)
			n++
		})

	s.AddCharacteristic(gatt.MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Wrote:", string(data))
			return gatt.StatusSuccess
		})

	s.AddCharacteristic(gatt.MustParseUUID("1c927b50-c116-11e3-8a33-0800200c9a66")).HandleNotifyFunc(
		func(r gatt.Request, n gatt.Notifier) {
			cnt := 0
			for !n.Done() {
				fmt.Fprintf(n, "Count: %d", cnt)
				cnt++
				time.Sleep(time.Second)
			}
		})

	return s
}
func NewNanoPiBLEDemoService() *gatt.Service {
	data := make([]byte, 256)
	s := gatt.NewService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b"))
	s.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			fmt.Printf("Send data to device: Hi, by NanoPi")
			fmt.Printf("\n")
			fmt.Fprintf(rsp, "Hi, by NanoPi")
		})

	c := s.AddCharacteristic(gatt.MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b"))
	c.HandleWriteFunc(
		func(r gatt.Request, newData []byte) (status byte) {
			for i := 0; i < len(data) && i < len(newData); i++ {
				data[i] = newData[i]
			}
			fmt.Printf("Recv data from device: ")
			for i := 0; i < len(newData); i++ {
				fmt.Printf("%x ", newData[i])
			}
			fmt.Printf("\n")
			return gatt.StatusSuccess
		})

	return s
}
Example #3
0
File: gap.go Project: sjenning/gatt
// NOTE: OS X provides GAP and GATT services, and they can't be customized.
// For Linux/Embedded, however, this is something we want to fully control.
func NewGapService(name string) *gatt.Service {
	s := gatt.NewService(attrGAPUUID)
	s.AddCharacteristic(attrDeviceNameUUID).SetValue([]byte(name))
	s.AddCharacteristic(attrAppearanceUUID).SetValue(gapCharAppearanceGenericComputer)
	s.AddCharacteristic(attrPeripheralPrivacyUUID).SetValue([]byte{0x00})
	s.AddCharacteristic(attrReconnectionAddrUUID).SetValue([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
	s.AddCharacteristic(attrPeferredParamsUUID).SetValue([]byte{0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0xd0, 0x07})
	return s
}
Example #4
0
// NOTE: OS X provides GAP and GATT services, and they can't be customized.
// For Linux/Embedded, however, this is something we want to fully control.
func NewGattService() *gatt.Service {
	s := gatt.NewService(attrGATTUUID)
	s.AddCharacteristic(attrServiceChangedUUID).HandleNotifyFunc(
		func(r gatt.Request, n gatt.Notifier) {
			go func() {
				log.Printf("TODO: indicate client when the services are changed")
			}()
		})
	return s
}
Example #5
0
func NewBatteryService() *gatt.Service {
	lv := byte(100)
	s := gatt.NewService(gatt.UUID16(0x180F))
	c := s.AddCharacteristic(gatt.UUID16(0x2A19))
	c.HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			rsp.Write([]byte{lv})
			lv--
		})

	// FIXME: this cause connection interrupted on Mac.
	// Characteristic User Description
	// c.AddDescriptor(gatt.UUID16(0x2901)).SetValue([]byte("Battery level between 0 and 100 percent"))

	// Characteristic Presentation Format
	c.AddDescriptor(gatt.UUID16(0x2904)).SetValue([]byte{4, 1, 39, 173, 1, 0, 0})

	return s
}
func NewNanoPiBLEDemoService() *gatt.Service {

	data := make([]byte, 256)
	s := gatt.NewService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b"))
	s.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			fmt.Printf("Send data to device: Hi, by NanoPi")
			fmt.Printf("\n")
			fmt.Fprintf(rsp, "Hi, by NanoPi")
		})

	c := s.AddCharacteristic(gatt.MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b"))
	c.HandleWriteFunc(
		func(r gatt.Request, newData []byte) (status byte) {
			for i := 0; i < len(data) && i < len(newData); i++ {
				data[i] = newData[i]
			}
			fmt.Printf("Recv data from device: ")
			for i := 0; i < len(newData); i++ {
				fmt.Printf("%x ", newData[i])
			}
			fmt.Printf("\n")
			c := string(newData[:])
			fmt.Println(c)
			if strings.Contains(c, "LedSwitch") {
				//defer S3C2451.FreeS3C2451()
				//PF2, _ := S3C2451.CreateGPIO(S3C2451.PF, 2)
				//defer S3C2451.FreeGPIO(PF2)

				data := PF2.Port.GetData()
				if (data & (0x1 << 2)) > 0 {
					PF2.GPIOFSetData(S3C2451.LOW)
				} else {
					PF2.GPIOFSetData(S3C2451.HIGH)
				}
			}

			return gatt.StatusSuccess
		})

	return s
}
func NewSensorService() *gatt.Service {
	s := gatt.NewService(gatt.MustParseUUID("19fc95c0-c111-11e3-9904-0002a5d5c51b"))
	s.AddCharacteristic(gatt.MustParseUUID("21fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			out, err := exec.Command("sh", "-c", "sudo /home/pi/temperature.py").Output()
			if err != nil {
				fmt.Fprintf(rsp, "error occured %s", err)
				log.Println("Wrote: error %s", err)
			} else {
				stringout := string(out)
				stringout = strings.TrimSpace(stringout)
				fmt.Fprintf(rsp, stringout)
				log.Println("Wrote:", stringout)
			}
		})

	s.AddCharacteristic(gatt.MustParseUUID("31fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleReadFunc(
		func(rsp gatt.ResponseWriter, req *gatt.ReadRequest) {
			out, err := exec.Command("sh", "-c", "sudo /home/pi/humidity.py").Output()
			if err != nil {
				fmt.Fprintf(rsp, "error occured %s", err)
				log.Println("Wrote: error %s", err)
			} else {
				stringout := string(out)
				stringout = strings.TrimSpace(stringout)
				fmt.Fprintf(rsp, stringout)
				log.Println("Wrote:", stringout)
			}
		})

	s.AddCharacteristic(gatt.MustParseUUID("41fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Command received")
			exec.Command("sh", "-c", "sudo reboot").Output()
			return gatt.StatusSuccess
		})

	s.AddCharacteristic(gatt.MustParseUUID("51fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Command received to turn on")
			exec.Command("sh", "-c", "gpio -g mode 17 out").Output()
			exec.Command("sh", "-c", "gpio -g write 17 1").Output()
			return gatt.StatusSuccess
		})

	s.AddCharacteristic(gatt.MustParseUUID("61fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Command received to turn off")
			exec.Command("sh", "-c", "gpio -g mode 17 out").Output()
			exec.Command("sh", "-c", "gpio -g write 17 0").Output()
			return gatt.StatusSuccess
		})

	s.AddCharacteristic(gatt.MustParseUUID("71fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Command received to whistle ")
			exec.Command("sh", "-c", "aplay /home/pi/whistle_blow_01.wav").Output()
			return gatt.StatusSuccess
		})

	s.AddCharacteristic(gatt.MustParseUUID("81fac9e0-c111-11e3-9246-0002a5d5c51b")).HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Println("Command received to turn on and whistle")
			exec.Command("sh", "-c", "aplay /home/pi/whistle_blow_01.wav").Output()
			exec.Command("sh", "-c", "gpio -g mode 17 out").Output()
			exec.Command("sh", "-c", "gpio -g write 17 1").Output()
			return gatt.StatusSuccess
		})

	return s
}