2022-03-21 23:15:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
extern bool StatusServiceSignalEvent(const char *jsonEvent);
|
|
|
|
extern void SetEventCallback(void *cb);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2022-03-22 16:30:14 +00:00
|
|
|
// SignalHandler defines a minimal interface
|
|
|
|
// a signal handler needs to implement.
|
|
|
|
//nolint
|
|
|
|
type SignalHandler interface {
|
|
|
|
HandleSignal(string)
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:15:53 +00:00
|
|
|
// SignalHandler is a simple callback function that gets called when any signal is received
|
|
|
|
type MobileSignalHandler func([]byte)
|
|
|
|
|
|
|
|
// storing the current mobile signal handler here
|
|
|
|
var mobileSignalHandler MobileSignalHandler
|
|
|
|
|
|
|
|
// SignalEnvelope is a general signal sent upward from node to app
|
|
|
|
type SignalEnvelope struct {
|
2022-04-03 00:22:42 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Event interface{} `json:"event"`
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEnvelope creates new envlope of given type and event payload.
|
2022-04-03 00:22:42 +00:00
|
|
|
func NewEnvelope(signalType string, event interface{}) *SignalEnvelope {
|
2022-03-21 23:15:53 +00:00
|
|
|
return &SignalEnvelope{
|
2022-04-03 00:22:42 +00:00
|
|
|
Type: signalType,
|
|
|
|
Event: event,
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send sends application signal (in JSON) upwards to application (via default notification handler)
|
2022-04-03 00:22:42 +00:00
|
|
|
func send(signalType string, event interface{}) {
|
|
|
|
signal := NewEnvelope(signalType, event)
|
2022-03-21 23:15:53 +00:00
|
|
|
data, err := json.Marshal(&signal)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("marshal signal error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If a Go implementation of signal handler is set, let's use it.
|
|
|
|
if mobileSignalHandler != nil {
|
|
|
|
mobileSignalHandler(data)
|
|
|
|
} else {
|
|
|
|
// ...and fallback to C implementation otherwise.
|
|
|
|
str := C.CString(string(data))
|
|
|
|
C.StatusServiceSignalEvent(str)
|
|
|
|
C.free(unsafe.Pointer(str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMobileSignalHandler setup geth callback to notify about new signal
|
|
|
|
// used for gomobile builds
|
|
|
|
//nolint
|
|
|
|
func SetMobileSignalHandler(handler SignalHandler) {
|
|
|
|
mobileSignalHandler = func(data []byte) {
|
|
|
|
if len(data) > 0 {
|
|
|
|
handler.HandleSignal(string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setEventCallback(cb unsafe.Pointer) {
|
|
|
|
C.SetEventCallback(cb)
|
|
|
|
}
|