2023-08-10 13:30:38 +00:00
|
|
|
package library
|
2022-03-21 23:15:53 +00:00
|
|
|
|
|
|
|
/*
|
2022-04-12 12:12:14 +00:00
|
|
|
#include <stddef.h>
|
2022-03-21 23:15:53 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2023-12-15 14:46:21 +00:00
|
|
|
extern bool ServiceSignalEvent(void *cb, const char *jsonEvent);
|
2022-03-21 23:15:53 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-03-21 23:15:53 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2023-07-07 01:38:23 +00:00
|
|
|
// MobileSignalHandler is a simple callback function that gets called when any signal is received
|
2022-03-21 23:15:53 +00:00
|
|
|
type MobileSignalHandler func([]byte)
|
|
|
|
|
|
|
|
// storing the current mobile signal handler here
|
|
|
|
var mobileSignalHandler MobileSignalHandler
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
// 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-12 12:12:14 +00:00
|
|
|
func newEnvelope(signalType string, event interface{}) *signalEnvelope {
|
|
|
|
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)
|
2023-12-15 14:46:21 +00:00
|
|
|
func send(instance *WakuInstance, signalType string, event interface{}) {
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
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.
|
2023-12-15 14:46:21 +00:00
|
|
|
if instance.mobileSignalHandler != nil {
|
|
|
|
instance.mobileSignalHandler(data)
|
2022-03-21 23:15:53 +00:00
|
|
|
} else {
|
|
|
|
// ...and fallback to C implementation otherwise.
|
2023-08-10 13:30:38 +00:00
|
|
|
dataStr := string(data)
|
|
|
|
str := C.CString(dataStr)
|
2023-12-15 14:46:21 +00:00
|
|
|
C.ServiceSignalEvent(instance.cb, str)
|
2022-03-21 23:15:53 +00:00
|
|
|
C.free(unsafe.Pointer(str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 01:38:23 +00:00
|
|
|
// SetEventCallback is to set a callback in order to receive application
|
|
|
|
// signals which are used to react to asynchronous events in waku.
|
2023-12-15 14:46:21 +00:00
|
|
|
func SetEventCallback(instance *WakuInstance, cb unsafe.Pointer) {
|
|
|
|
if err := validateInstance(instance, None); err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.cb = cb
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
2023-10-07 20:28:07 +00:00
|
|
|
|
|
|
|
// SetMobileSignalHandler sets the callback to be executed when a signal
|
|
|
|
// is received in a mobile device
|
2023-12-15 14:46:21 +00:00
|
|
|
func SetMobileSignalHandler(instance *WakuInstance, m MobileSignalHandler) {
|
|
|
|
if err := validateInstance(instance, None); err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.mobileSignalHandler = m
|
2023-10-07 20:28:07 +00:00
|
|
|
}
|