ooni-probe-cli/internal/libooniffi/ooniffi.go
Simone Basso 99b28c1d95
refactor: start building an Android package (#205)
* refactor: start building an Android package

Part of https://github.com/ooni/probe/issues/1335.

This seems also a good moment to move some packages out of the
engine, e.g., oonimkall. This package, for example, is a consumer
of the engine, so it makes sense it's not _inside_ it.

* fix: committed some stuff I didn't need to commit

* fix: oonimkall needs to be public to build

The side effect is that we will probably need to bump the major
version number every time we change one of these APIs.

(We can also of course choose to violate the basic guidelines of Go
software, but I believe this is bad form.)

I have no problem in bumping the major quite frequently and in
any case this monorepo solution is convinving me more than continuing
to keep a split between engine and cli. The need to embed assets to
make the probe more reliable trumps the negative effects of having to
~frequently bump major because we expose a public API.

* fix: let's not forget about libooniffi

Honestly, I don't know what to do with this library. I added it
to provide a drop in replacement for MK but I have no idea whether
it's used and useful. I would not feel comfortable exposing it,
unlike oonimkall, since we're not using it.

It may be that the right thing to do here is just to delete the
package and reduce the amount of code we're maintaining?

* woops, we're still missing the publish android script

* fix(publish-android.bash): add proper API key

* ouch fix another place where the name changed
2021-02-03 10:51:14 +01:00

153 lines
2.8 KiB
Go

package main
import (
//#include "ooniffi.h"
//
//#include <stdint.h>
//#include <stdlib.h>
//
//struct ooniffi_task_ {
// int64_t Handle;
//};
//
// struct ooniffi_event_ {
// char *String;
//};
"C"
"sync"
"unsafe"
"github.com/ooni/probe-cli/v3/pkg/oonimkall"
)
var (
idx C.int64_t
m = make(map[C.int64_t]*oonimkall.Task)
mu sync.Mutex
)
func cstring(s string) *C.char {
return C.CString(s)
}
func freestring(s *C.char) {
C.free(unsafe.Pointer(s))
}
func gostring(s *C.char) string {
return C.GoString(s)
}
const maxIdx = C.INT64_MAX
//export ooniffi_task_start_
func ooniffi_task_start_(settings *C.char) *C.ooniffi_task_t {
if settings == nil {
return nil
}
tp, err := oonimkall.StartTask(gostring(settings))
if err != nil {
return nil
}
mu.Lock()
defer mu.Unlock()
// TODO(bassosimone): the following if is basic protection against
// undefined behaviour, i.e., the counter wrapping around. A much
// better strategy would probably be to restart from 0. However it's
// also unclear if any device could run that many tests, so...
if idx >= maxIdx {
return nil
}
handle := idx
idx++
m[handle] = tp
task := (*C.ooniffi_task_t)(C.malloc(C.sizeof_ooniffi_task_t))
task.Handle = handle
return task
}
func setmaxidx() C.int64_t {
o := idx
idx = maxIdx
return o
}
func restoreidx(v C.int64_t) {
idx = v
}
//export ooniffi_task_wait_for_next_event
func ooniffi_task_wait_for_next_event(task *C.ooniffi_task_t) (event *C.ooniffi_event_t) {
if task != nil {
mu.Lock()
tp := m[task.Handle]
mu.Unlock()
if tp != nil {
event = (*C.ooniffi_event_t)(C.malloc(C.sizeof_ooniffi_event_t))
event.String = cstring(tp.WaitForNextEvent())
}
}
return
}
//export ooniffi_task_is_done
func ooniffi_task_is_done(task *C.ooniffi_task_t) C.int {
var isdone C.int = 1
if task != nil {
mu.Lock()
if tp := m[task.Handle]; tp != nil && !tp.IsDone() {
isdone = 0
}
mu.Unlock()
}
return isdone
}
//export ooniffi_task_interrupt
func ooniffi_task_interrupt(task *C.ooniffi_task_t) {
if task != nil {
mu.Lock()
if tp := m[task.Handle]; tp != nil {
tp.Interrupt()
}
mu.Unlock()
}
}
//export ooniffi_event_serialization_
func ooniffi_event_serialization_(event *C.ooniffi_event_t) (s *C.char) {
if event != nil {
s = event.String
}
return
}
//export ooniffi_event_destroy
func ooniffi_event_destroy(event *C.ooniffi_event_t) {
if event != nil {
C.free(unsafe.Pointer(event.String))
C.free(unsafe.Pointer(event))
}
}
//export ooniffi_task_destroy
func ooniffi_task_destroy(task *C.ooniffi_task_t) {
if task != nil {
mu.Lock()
tp := m[task.Handle]
delete(m, task.Handle)
mu.Unlock()
C.free(unsafe.Pointer(task))
if tp != nil { // drain task if needed
tp.Interrupt()
go func() {
for !tp.IsDone() {
tp.WaitForNextEvent()
}
}()
}
}
}
func main() {}