Example of using the Enigma encryption library.

Dependencies:   Enigma

Committer:
hudakz
Date:
Tue Sep 24 16:58:11 2019 +0000
Revision:
3:83f583d06005
Parent:
0:3381509d20af
Example of using the Enigma encryption library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:3381509d20af 1 /* mbed Microcontroller Library
hudakz 0:3381509d20af 2 * Copyright (c) 2018 ARM Limited
hudakz 0:3381509d20af 3 * SPDX-License-Identifier: Apache-2.0
hudakz 0:3381509d20af 4 */
hudakz 0:3381509d20af 5
hudakz 0:3381509d20af 6 #ifndef STATS_REPORT_H
hudakz 0:3381509d20af 7 #define STATS_REPORT_H
hudakz 0:3381509d20af 8
hudakz 0:3381509d20af 9 #include <inttypes.h>
hudakz 0:3381509d20af 10 #include "mbed.h"
hudakz 0:3381509d20af 11
hudakz 0:3381509d20af 12 /**
hudakz 0:3381509d20af 13 * System Reporting library. Provides runtime information on device:
hudakz 0:3381509d20af 14 * - CPU sleep, idle, and wake times
hudakz 0:3381509d20af 15 * - Heap and stack usage
hudakz 0:3381509d20af 16 * - Thread information
hudakz 0:3381509d20af 17 * - Static system information
hudakz 0:3381509d20af 18 */
hudakz 0:3381509d20af 19 class SystemReport {
hudakz 0:3381509d20af 20 mbed_stats_heap_t heap_stats;
hudakz 0:3381509d20af 21 mbed_stats_cpu_t cpu_stats;
hudakz 0:3381509d20af 22 mbed_stats_sys_t sys_stats;
hudakz 0:3381509d20af 23
hudakz 0:3381509d20af 24 mbed_stats_thread_t *thread_stats;
hudakz 0:3381509d20af 25 uint8_t thread_count;
hudakz 0:3381509d20af 26 uint8_t max_thread_count;
hudakz 0:3381509d20af 27 uint32_t sample_time_ms;
hudakz 0:3381509d20af 28
hudakz 0:3381509d20af 29 public:
hudakz 0:3381509d20af 30 /**
hudakz 0:3381509d20af 31 * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
hudakz 0:3381509d20af 32 */
hudakz 0:3381509d20af 33 SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
hudakz 0:3381509d20af 34 {
hudakz 0:3381509d20af 35 thread_stats = new mbed_stats_thread_t[max_thread_count];
hudakz 0:3381509d20af 36
hudakz 0:3381509d20af 37 // Collect the static system information
hudakz 0:3381509d20af 38 mbed_stats_sys_get(&sys_stats);
hudakz 0:3381509d20af 39
hudakz 0:3381509d20af 40 printf("=============================== SYSTEM INFO ================================\r\n");
hudakz 0:3381509d20af 41 printf("Mbed OS Version: %" PRIu32 " \r\n", sys_stats.os_version);
hudakz 0:3381509d20af 42 printf("CPU ID: 0x%" PRIx32 " \r\n", sys_stats.cpu_id);
hudakz 0:3381509d20af 43 printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
hudakz 0:3381509d20af 44 printf("Compiler Version: %" PRIu32 " \r\n", sys_stats.compiler_version);
hudakz 0:3381509d20af 45
hudakz 0:3381509d20af 46 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
hudakz 0:3381509d20af 47 if (sys_stats.ram_size[i] != 0) {
hudakz 0:3381509d20af 48 printf("RAM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 " \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
hudakz 0:3381509d20af 49 }
hudakz 0:3381509d20af 50 }
hudakz 0:3381509d20af 51 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
hudakz 0:3381509d20af 52 if (sys_stats.rom_size[i] != 0) {
hudakz 0:3381509d20af 53 printf("ROM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 " \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
hudakz 0:3381509d20af 54 }
hudakz 0:3381509d20af 55 }
hudakz 0:3381509d20af 56 }
hudakz 0:3381509d20af 57
hudakz 0:3381509d20af 58 ~SystemReport(void)
hudakz 0:3381509d20af 59 {
hudakz 0:3381509d20af 60 free(thread_stats);
hudakz 0:3381509d20af 61 }
hudakz 0:3381509d20af 62
hudakz 0:3381509d20af 63 /**
hudakz 0:3381509d20af 64 * Report on each Mbed OS Platform stats API
hudakz 0:3381509d20af 65 */
hudakz 0:3381509d20af 66 void report_state(void)
hudakz 0:3381509d20af 67 {
hudakz 0:3381509d20af 68 report_cpu_stats();
hudakz 0:3381509d20af 69 report_heap_stats();
hudakz 0:3381509d20af 70 report_thread_stats();
hudakz 0:3381509d20af 71
hudakz 0:3381509d20af 72 // Clear next line to separate subsequent report logs
hudakz 0:3381509d20af 73 printf("\r\n");
hudakz 0:3381509d20af 74 }
hudakz 0:3381509d20af 75
hudakz 0:3381509d20af 76 /**
hudakz 0:3381509d20af 77 * Report CPU idle and awake time in terms of percentage
hudakz 0:3381509d20af 78 */
hudakz 0:3381509d20af 79 void report_cpu_stats(void)
hudakz 0:3381509d20af 80 {
hudakz 0:3381509d20af 81 static uint64_t prev_idle_time = 0;
hudakz 0:3381509d20af 82
hudakz 0:3381509d20af 83 printf("================= CPU STATS =================\r\n");
hudakz 0:3381509d20af 84
hudakz 0:3381509d20af 85 // Collect and print cpu stats
hudakz 0:3381509d20af 86 mbed_stats_cpu_get(&cpu_stats);
hudakz 0:3381509d20af 87
hudakz 0:3381509d20af 88 uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
hudakz 0:3381509d20af 89 uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
hudakz 0:3381509d20af 90 uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
hudakz 0:3381509d20af 91 prev_idle_time = cpu_stats.idle_time;
hudakz 0:3381509d20af 92
hudakz 0:3381509d20af 93 printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
hudakz 0:3381509d20af 94 }
hudakz 0:3381509d20af 95
hudakz 0:3381509d20af 96 /**
hudakz 0:3381509d20af 97 * Report current heap stats. Current heap refers to the current amount of
hudakz 0:3381509d20af 98 * allocated heap. Max heap refers to the highest amount of heap allocated
hudakz 0:3381509d20af 99 * since reset.
hudakz 0:3381509d20af 100 */
hudakz 0:3381509d20af 101 void report_heap_stats(void)
hudakz 0:3381509d20af 102 {
hudakz 0:3381509d20af 103 printf("================ HEAP STATS =================\r\n");
hudakz 0:3381509d20af 104
hudakz 0:3381509d20af 105 // Collect and print heap stats
hudakz 0:3381509d20af 106 mbed_stats_heap_get(&heap_stats);
hudakz 0:3381509d20af 107
hudakz 0:3381509d20af 108 printf("Current heap: %" PRIu32 "\r\n", heap_stats.current_size);
hudakz 0:3381509d20af 109 printf("Max heap size: %" PRIu32 "\r\n", heap_stats.max_size);
hudakz 0:3381509d20af 110 }
hudakz 0:3381509d20af 111
hudakz 0:3381509d20af 112 /**
hudakz 0:3381509d20af 113 * Report active thread stats
hudakz 0:3381509d20af 114 */
hudakz 0:3381509d20af 115 void report_thread_stats(void)
hudakz 0:3381509d20af 116 {
hudakz 0:3381509d20af 117 printf("================ THREAD STATS ===============\r\n");
hudakz 0:3381509d20af 118
hudakz 0:3381509d20af 119 // Collect and print running thread stats
hudakz 0:3381509d20af 120 int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
hudakz 0:3381509d20af 121
hudakz 0:3381509d20af 122 for (int i = 0; i < count; i++) {
hudakz 0:3381509d20af 123 printf("ID: 0x%" PRIx32 " \r\n", thread_stats[i].id);
hudakz 0:3381509d20af 124 printf("Name: %s \r\n", thread_stats[i].name);
hudakz 0:3381509d20af 125 printf("State: %" PRIu32 " \r\n", thread_stats[i].state);
hudakz 0:3381509d20af 126 printf("Priority: %" PRIu32 " \r\n", thread_stats[i].priority);
hudakz 0:3381509d20af 127 printf("Stack Size: %" PRIu32 " \r\n", thread_stats[i].stack_size);
hudakz 0:3381509d20af 128 printf("Stack Space: %" PRIu32 " \r\n", thread_stats[i].stack_space);
hudakz 0:3381509d20af 129 }
hudakz 0:3381509d20af 130 }
hudakz 0:3381509d20af 131 };
hudakz 0:3381509d20af 132
hudakz 0:3381509d20af 133 #endif // STATS_REPORT_H