|
| 1 | +#include <math.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <libtock-sync/services/alarm.h> |
| 6 | +#include <libtock/interface/button.h> |
| 7 | +#include <libtock/kernel/app_loader.h> |
| 8 | +#include <libtock/tock.h> |
| 9 | + |
| 10 | +#include <inttypes.h> |
| 11 | + |
| 12 | +/****************************************************************************************************** |
| 13 | +* Short ID and version of app that has to be uninstalled (Blink in this case) |
| 14 | +******************************************************************************************************/ |
| 15 | +const uint32_t short_id = 4246331976; |
| 16 | +const uint32_t version = 0; |
| 17 | + |
| 18 | +/****************************************************************************************************** |
| 19 | +* Callback Tracking Flags |
| 20 | +******************************************************************************************************/ |
| 21 | +static bool uninstall_done = false; // to check if setup is done |
| 22 | +static bool app_uninstall = false; // to track if app has to be uninstalled |
| 23 | + |
| 24 | +/****************************************************************************************************** |
| 25 | +* Callback functions |
| 26 | +* |
| 27 | +* Set button callback to initiate the uninstall on pressing buttons |
| 28 | +******************************************************************************************************/ |
| 29 | + |
| 30 | +static void app_uninstall_done_callback(__attribute__((unused)) int arg0, |
| 31 | + __attribute__((unused)) int arg1, |
| 32 | + __attribute__((unused)) int arg2, |
| 33 | + __attribute__((unused)) void* ud) { |
| 34 | + |
| 35 | + if (arg0 != RETURNCODE_SUCCESS) { |
| 36 | + printf("[Error] Uninstall failed: %d.\n", arg0); |
| 37 | + } else { |
| 38 | + printf("[Success] Uninstalled app successfully.\n"); |
| 39 | + } |
| 40 | + uninstall_done = true; |
| 41 | +} |
| 42 | + |
| 43 | +// Callback for button presses. |
| 44 | +static void button_callback(__attribute__ ((unused)) returncode_t retval, int btn_num, __attribute__ ( |
| 45 | + (unused)) bool pressed) { |
| 46 | + // Callback for button presses. |
| 47 | + if (btn_num == 0) { |
| 48 | + app_uninstall = true; |
| 49 | + } else { |
| 50 | + printf("[App Uninstaller] Invalid button selected. Unable to uninstall!\n"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +/****************************************************************************************************** |
| 56 | +* Main |
| 57 | +******************************************************************************************************/ |
| 58 | + |
| 59 | +int main(void) { |
| 60 | + printf("[Log] Simple test app to uninstall an app during runtime.\n"); |
| 61 | + |
| 62 | + // check if app loader driver exists |
| 63 | + if (!libtock_app_loader_exists()) { |
| 64 | + printf("No App Loader driver!\n"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + int count; |
| 69 | + int err = libtock_button_count(&count); |
| 70 | + // Ensure there is a button to use. |
| 71 | + if (err < 0) return err; |
| 72 | + printf("[Log] There are %d buttons on this board.\n", count); |
| 73 | + |
| 74 | + // Enable interrupts on each button. |
| 75 | + for (int i = 0; i < count; i++) { |
| 76 | + libtock_button_notify_on_press(i, button_callback); |
| 77 | + } |
| 78 | + |
| 79 | + printf("[Log] Waiting for a button press.\n"); |
| 80 | + |
| 81 | + while (1) { |
| 82 | + if (app_uninstall) { |
| 83 | + printf("Uninstalling app with Short ID: %" PRIu32 " and version %" PRIu32 "\n", short_id, version); |
| 84 | + int ret = libtock_app_loader_uninstall(short_id, version, app_uninstall_done_callback); |
| 85 | + if (ret != RETURNCODE_SUCCESS) { |
| 86 | + printf("[Error] Uninstall Failed: %d.\n", ret); |
| 87 | + tock_exit(ret); |
| 88 | + } |
| 89 | + yield_for(&uninstall_done); |
| 90 | + uninstall_done = false; |
| 91 | + app_uninstall = false; |
| 92 | + } |
| 93 | + yield(); |
| 94 | + } |
| 95 | +} |
0 commit comments