|
| 1 | +#include "libtock/tock.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +#include <libtock-sync/interface/console.h> |
| 6 | +#include <libtock/kernel/ipc.h> |
| 7 | + |
| 8 | +#define LOG_WIDTH 32 |
| 9 | + |
| 10 | +bool started = false; |
| 11 | +bool log_done = false; |
| 12 | +size_t screen_service = -1; |
| 13 | +char log_buf[LOG_WIDTH] __attribute__((aligned(LOG_WIDTH))); |
| 14 | + |
| 15 | +const char SCREEN_SERVICE_NAME[] = "org.tockos.tutorials.attestation.screen"; |
| 16 | + |
| 17 | +static void ipc_callback(__attribute__ ((unused)) int pid, |
| 18 | + __attribute__ ((unused)) int len, |
| 19 | + __attribute__ ((unused)) int arg2, |
| 20 | + __attribute__ ((unused)) void* ud) { |
| 21 | + started = true; |
| 22 | +} |
| 23 | + |
| 24 | +static void log_done_callback(__attribute__ ((unused)) int pid, |
| 25 | + __attribute__ ((unused)) int len, |
| 26 | + __attribute__ ((unused)) int arg2, |
| 27 | + __attribute__ ((unused)) void* ud) { |
| 28 | + log_done = true; |
| 29 | +} |
| 30 | + |
| 31 | +static void wait_for_start(void) { |
| 32 | + // Register an IPC callback and wait for it to be called by the |
| 33 | + // screen app based on the user's app selection. |
| 34 | + ipc_register_service_callback("org.tockos.tutorials.attestation.encryption", ipc_callback, |
| 35 | + NULL); |
| 36 | + yield_for(&started); |
| 37 | +} |
| 38 | + |
| 39 | +static int setup_logging() { |
| 40 | + returncode_t ret; |
| 41 | + |
| 42 | + // Find the PID of the screen logging service |
| 43 | + ret = ipc_discover(SCREEN_SERVICE_NAME, &screen_service); |
| 44 | + if (ret != RETURNCODE_SUCCESS) { |
| 45 | + printf("Screen logging service not found.\n"); |
| 46 | + return ret; |
| 47 | + } |
| 48 | + |
| 49 | + // Set up a callback and share so we can supply log messages |
| 50 | + // and know when they've been completely logged. |
| 51 | + ipc_register_client_callback(screen_service, log_done_callback, NULL); |
| 52 | + ipc_share(screen_service, log_buf, LOG_WIDTH); |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +static int log_to_screen(const char* message) { |
| 58 | + returncode_t ret; |
| 59 | + |
| 60 | + // Copy up to the log buffer's size of the message, with room for a null byte. |
| 61 | + uint16_t len = strnlen(message, sizeof(log_buf) - 1); |
| 62 | + memcpy(log_buf, message, len); |
| 63 | + |
| 64 | + // Add the null byte. |
| 65 | + log_buf[len] = '\0'; |
| 66 | + |
| 67 | + // Start the logging process. |
| 68 | + ret = ipc_notify_service(screen_service); |
| 69 | + if (ret != RETURNCODE_SUCCESS) { |
| 70 | + printf("Failed to request a log to screen.\n"); |
| 71 | + return ret; |
| 72 | + } |
| 73 | + |
| 74 | + // Wait for the log to complete. |
| 75 | + yield_for(&log_done); |
| 76 | + log_done = false; |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +int main(void) { |
| 82 | + // Wait to receive the signal to start from the app selector. |
| 83 | + wait_for_start(); |
| 84 | + |
| 85 | + // Set up logging service. |
| 86 | + setup_logging(); |
| 87 | + |
| 88 | + // Test logging to screen over IPC. |
| 89 | + char message_buf[32]; |
| 90 | + for (int i = 0; i < 10; i++) { |
| 91 | + sprintf(message_buf, "Test message #%i...", i); |
| 92 | + log_to_screen(message_buf); |
| 93 | + } |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments