Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,57 @@ type Event struct {
// The event handler function.
// Return true to terminate
type EventHandlerFunc func(Event) bool
type subscriberFunc struct {
eventName string
fn EventHandlerFunc
}

// Emitter is an object to emit and handle Event(s)
type Emitter struct {
handlers map[string]EventHandlerFunc
event chan Event
verbose bool
handlers map[string]EventHandlerFunc
event chan Event
subscribe chan subscriberFunc
verbose bool
clean chan bool
}

func MakeEmitter() Emitter {
return Emitter{handlers: make(map[string]EventHandlerFunc), event: make(chan Event), subscribe: make(chan subscriberFunc), clean: make(chan bool)}
}

// Init initialize the emitter and start a goroutine to execute the event handlers
func (e *Emitter) Init() {
e.handlers = make(map[string]EventHandlerFunc)
e.event = make(chan Event)

// event handler
go func() {
for {
ev := <-e.event

if fn, ok := e.handlers[ev.Name]; ok {
if fn(ev) {
break
select {
case ev := <-e.event:
if fn, ok := e.handlers[ev.Name]; ok {
fn(ev)
} else if fn, ok := e.handlers[ALL]; ok {
fn(ev)
} else {
if e.verbose {
log.Println("unhandled Emit", ev)
}
}
} else if fn, ok := e.handlers[ALL]; ok {
if fn(ev) {
break
case cb := <-e.subscribe:
if cb.fn == nil {
delete(e.handlers, cb.eventName)
} else {
e.handlers[cb.eventName] = cb.fn
}
} else {
if e.verbose {
log.Println("unhandled Emit", ev)
case cleaning := <-e.clean:
if cleaning == true {
close(e.event)
close(e.subscribe)
close(e.clean)
return
}
}
}

close(e.event) // TOFIX: this causes new "emits" to panic.
//close(e.event) // TOFIX: this causes new "emits" to panic.
}()
}

Expand All @@ -74,9 +91,9 @@ func (e *Emitter) Emit(ev Event) {

// On(event, cb) registers an handler for the specified event
func (e *Emitter) On(event string, fn EventHandlerFunc) {
if fn == nil {
delete(e.handlers, event)
} else {
e.handlers[event] = fn
}
e.subscribe <- subscriberFunc{eventName: event, fn: fn}
}

func (e *Emitter) Close() {
e.clean <- true
}
11 changes: 5 additions & 6 deletions goble.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ type BLE struct {
}

func New() *BLE {
ble := &BLE{peripherals: map[string]*Peripheral{}, Emitter: Emitter{}}
ble.Emitter.Init()
ble := &BLE{peripherals: map[string]*Peripheral{}, Emitter: MakeEmitter()}
ble.conn = xpc.XpcConnect("com.apple.blued", ble)
xpc.Uname(&ble.utsname)
return ble
Expand Down Expand Up @@ -434,10 +433,10 @@ func (ble *BLE) HandleXpcEvent(event xpc.Dict, err error) {

case 70, 95: // read
deviceUuid := args.MustGetUUID("kCBMsgArgDeviceUUID")
characteristicsHandle := args.MustGetInt("kCBMsgArgCharacteristicHandle")
characteristicsHandle := args.GetInt("kCBMsgArgCharacteristicHandle", 0)
//result := args.MustGetInt("kCBMsgArgResult")
isNotification := args.GetInt("kCBMsgArgIsNotification", 0) != 0
data := args.MustGetBytes("kCBMsgArgData")
data := args.GetBytes("kCBMsgArgData", []byte{})

if p, ok := ble.peripherals[deviceUuid.String()]; ok {
for _, s := range p.Services {
Expand All @@ -456,12 +455,12 @@ func (ble *BLE) sendCBMsg(id int, args xpc.Dict) {
if ble.verbose {
log.Printf("sendCBMsg %#v\n", message)
}

ble.conn.Send(xpc.Dict{"kCBMsgId": id, "kCBMsgArgs": args}, ble.verbose)
ble.conn.Send(message, ble.verbose)
}

// initialize BLE
func (ble *BLE) Init() {
ble.Emitter.Init()
ble.sendCBMsg(1, xpc.Dict{"kCBMsgArgName": fmt.Sprintf("goble-%v", time.Now().Unix()),
"kCBMsgArgOptions": xpc.Dict{"kCBInitOptionShowPowerAlert": 0}, "kCBMsgArgType": 0})
}
Expand Down