github.com/transparency-dev/armored-witness-applet@v0.1.1/trusted_applet/handler.go (about)

     1  // Copyright 2022 The Armored Witness Applet authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"log"
    19  	"math"
    20  	"runtime"
    21  	"time"
    22  
    23  	"github.com/usbarmory/GoTEE/syscall"
    24  	enet "github.com/usbarmory/imx-enet"
    25  
    26  	"github.com/transparency-dev/armored-witness-os/api/rpc"
    27  )
    28  
    29  func eventHandler() {
    30  	var handler rpc.Handler
    31  
    32  	handler.G, handler.P = runtime.GetG()
    33  
    34  	if err := syscall.Call("RPC.Register", handler, nil); err != nil {
    35  		log.Fatalf("TA event handler registration error, %v", err)
    36  	}
    37  
    38  	n := 0
    39  	out := make([]byte, enet.MTU)
    40  
    41  	for {
    42  		// To avoid losing interrupts, re-enabling must happen only
    43  		// after we are sleeping.
    44  		go syscall.Write(FIQ, nil, 0)
    45  
    46  		// sleep indefinitely until woken up by runtime.WakeG
    47  		time.Sleep(math.MaxInt64)
    48  
    49  		// check for Ethernet RX event
    50  		for n = rxFromEth(out); n > 0; n = rxFromEth(out) {
    51  			rx(out[0:n])
    52  		}
    53  	}
    54  }