Beispiel #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
}
Beispiel #3
0
func main() {
	srv := &gatt.Server{
		Name:        "gophers",
		Connect:     func(c gatt.Conn) { log.Println("Connect: ", c) },
		Disconnect:  func(c gatt.Conn) { log.Println("Disconnect: ", c) },
		ReceiveRSSI: func(c gatt.Conn, rssi int) { log.Println("RSSI: ", c, " ", rssi) },
		Closed:      func(err error) { log.Println("Server closed: ", err) },
		StateChange: func(newState string) { log.Println("Server state change: ", newState) },
	}

	svc := srv.AddService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b"))

	n := 0
	rchar := svc.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b"))
	rchar.HandleRead(
		gatt.ReadHandlerFunc(
			func(resp gatt.ReadResponseWriter, req *gatt.ReadRequest) {
				fmt.Fprintf(resp, "count: %d", n)
				n++
			}),
	)

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

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

	log.Fatal(srv.AdvertiseAndServe())
}
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
}
Beispiel #5
0
func main() {
	serviceUUID := gatt.MustParseUUID("39170DC9-E537-43B0-AE6E-F7D2DE3031E0")
	writeUUID := gatt.MustParseUUID("275E9963-D7E4-47A5-B43A-8BFC360F5032")
	notifyUUID := gatt.MustParseUUID("77500CA7-CD0D-4FFE-88CD-07A3E8F509A5")
	bleListener := blenet.NewListener(serviceUUID, writeUUID, notifyUUID)

	page := `<html><body>  Hi! I am a <strong>{{.Os}}/{{.Arch}}</strong> system.  </body></html>`
	tpl := template.Must(template.New("home").Parse(page))

	http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
		sys := struct {
			Os   string
			Arch string
		}{
			Os:   runtime.GOOS,
			Arch: runtime.GOARCH,
		}
		tpl.Execute(resp, sys)
	})

	s := new(http.Server)
	log.Fatal(s.Serve(bleListener))
}
Beispiel #6
0
func main() {
	srv := &gatt.Server{Name: "GopherCon2014"}
	srv.Connect = func(c gatt.Conn) { log.Println("Connect: ", c) }
	srv.Disconnect = func(c gatt.Conn) { log.Println("Disconnect: ", c) }

	svc := srv.AddService(gatt.MustParseUUID("09fc95c0-c111-11e3-9904-0002a5d5c51b"))

	rchar := svc.AddCharacteristic(gatt.MustParseUUID("11fac9e0-c111-11e3-9246-0002a5d5c51b"))
	rchar.HandleReadFunc(
		func(resp gatt.ReadResponseWriter, req *gatt.ReadRequest) {
			tz, off := time.Now().Zone()
			fmt.Fprintf(resp, "%v (%d)", tz, off/3600) // HL
		})

	wchar := svc.AddCharacteristic(gatt.MustParseUUID("16fe0d80-c111-11e3-b8c8-0002a5d5c51b"))
	wchar.HandleWriteFunc(
		func(r gatt.Request, data []byte) (status byte) {
			log.Printf("Central wrote: %s", data) // HL
			return gatt.StatusSuccess
		})

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

	log.Printf("Advertising service %v", svc.UUID())
	log.Fatal(srv.AdvertiseAndServe())
}
func main() {
	d, err := gatt.NewDevice(option.DefaultServerOptions...)
	if err != nil {
		log.Fatalf("Failed to open device, err: %s", err)
	}

	// Register optional handlers.
	d.Handle(
		gatt.CentralConnected(func(c gatt.Central) { fmt.Println("Connect: ", c.ID()) }),
		gatt.CentralDisconnected(func(c gatt.Central) { fmt.Println("Disconnect: ", c.ID()) }),
	)

	// A mandatory handler for monitoring device state.
	onStateChanged := func(d gatt.Device, s gatt.State) {
		fmt.Printf("State: %s\n", s)
		switch s {
		case gatt.StatePoweredOn:
			// Setup GAP and GATT services for Linux implementation.
			// OS X doesn't export the access of these services.
			d.AddService(service.NewGapService("Gopher")) // no effect on OS X
			d.AddService(service.NewGattService())        // no effect on OS X

			// A simple count service for demo.
			s1 := service.NewCountService()
			d.AddService(s1)

			// A sensor service for demo.
			sSensor := service.NewSensorService()
			d.AddService(sSensor)

			// A fake battery service for demo.
			s2 := service.NewBatteryService()
			d.AddService(s2)

			// Advertise device name and service's UUIDs.
			d.AdvertiseNameAndServices("Gopher", []gatt.UUID{s1.UUID(), sSensor.UUID(), s2.UUID()})

			// Advertise as an OpenBeacon iBeacon
			d.AdvertiseIBeacon(gatt.MustParseUUID("AA6062F098CA42118EC4193EB73CCEB6"), 1, 2, -59)

		default:
		}
	}

	d.Init(onStateChanged)
	select {}
}
Beispiel #8
0
func main() {
	flag.Parse()
	d, err := gatt.NewDevice(
		gatt.LnxMaxConnections(*mc),
		gatt.LnxDeviceID(*dev, *chk),
		gatt.LnxSetAdvertisingParameters(&cmd.LESetAdvertisingParameters{
			AdvertisingIntervalMin: 0x00f4,
			AdvertisingIntervalMax: 0x00f4,
			AdvertisingChannelMap:  0x07,
		}),
	)

	if err != nil {
		log.Printf("Failed to open device, err: %s", err)
		return
	}

	// Register optional handlers.
	d.Handle(
		gatt.CentralConnected(func(c gatt.Central) { log.Println("Connect: ", c.ID()) }),
		gatt.CentralDisconnected(func(c gatt.Central) { log.Println("Disconnect: ", c.ID()) }),
	)

	// A mandatory handler for monitoring device state.
	onStateChanged := func(d gatt.Device, s gatt.State) {
		fmt.Printf("State: %s\n", s)
		switch s {
		case gatt.StatePoweredOn:
			// Get bdaddr with LnxSendHCIRawCommand()
			bdaddr(d)

			// Setup GAP and GATT services.
			d.AddService(service.NewGapService(*name))
			d.AddService(service.NewGattService())

			// Add a simple counter service.
			s1 := service.NewCountService()
			d.AddService(s1)

			// Add a simple counter service.
			s2 := service.NewBatteryService()
			d.AddService(s2)
			uuids := []gatt.UUID{s1.UUID(), s2.UUID()}

			// If id is zero, advertise name and services statically.
			if *id == time.Duration(0) {
				d.AdvertiseNameAndServices(*name, uuids)
				break
			}

			// If id is non-zero, advertise name and services and iBeacon alternately.
			go func() {
				for {
					// Advertise as a RedBear Labs iBeacon.
					d.AdvertiseIBeacon(gatt.MustParseUUID("5AFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), 1, 2, -59)
					time.Sleep(*id)

					// Advertise name and services.
					d.AdvertiseNameAndServices(*name, uuids)
					time.Sleep(*ii)
				}
			}()

		default:
		}
	}

	d.Init(onStateChanged)
	select {}
}
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
}