Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:ef0a1a6d4a40, committed 2013-09-14
- Comitter:
- kleberkruger
- Date:
- Sat Sep 14 22:58:28 2013 +0000
- Commit message:
- Fault injector system.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/FaultInjector.cpp Sat Sep 14 22:58:28 2013 +0000
@@ -0,0 +1,132 @@
+/*
+ * File: FaultsInjector.cpp
+ * Author: Kleber Kruger
+ *
+ * Created on 11 de Junho de 2013, 09:20
+ */
+
+#include "FaultInjector.h"
+
+MemoryRegion FaultInjector::memoryRegions[] = {
+ {"32 kB local SRAM", 0x10004000, 0x10008000 - 1, (0x10008000 - 0x10004000)},
+ {"16 kB AHB SRAM1", 0x20080000, 0x20084000 - 1, (0x20084000 - 0x20080000)},
+ {"END MEMORY", 0, 0, 0}
+};
+
+/**
+ * Construtor da classe.
+ * Configura as classes que podem ser criadas dinamicamente.
+ */
+FaultInjector::FaultInjector() {
+ srand(time(NULL));
+ memorySize = 0;
+ for (int i = 0; memoryRegions[i].size; i++) {
+ memorySize += memoryRegions[i].size;
+ }
+}
+
+/**
+ * Destrutor da classe.
+ */
+FaultInjector::~FaultInjector() {
+}
+
+/**
+ * Função que injeta falhas no sistema.
+ *
+ * @param changedBytes - quantidade de bytes alterados dentro da faixa de endereço.
+ * @param changedBits - quantidade de bits que foram alterados (0 (auto)... 8).
+ */
+void FaultInjector::injectFaults(unsigned int changedBytes, unsigned int changedBits) {
+ unsigned int addrStart, addrEnd;
+ DigitalOut led3(LED3);
+ //addrStart = (unsigned) getRandomIntPositive(0x10004000, 0x10008000 - 1);
+ //addrEnd = getRandomIntPositive(addrStart, 0x10008000 - 1);
+ addrStart = (unsigned) getRandomIntPositive(0, memorySize - 1);
+ addrEnd = getRandomIntPositive(addrStart, memorySize - 1);
+ led3 = 1;
+ wait(0.3);
+ led3 = 0;
+ injectFaults(addrStart, addrEnd, changedBytes, changedBits);
+}
+
+unsigned int FaultInjector::getByteMemory(unsigned int startAddr, unsigned int endAddr) {
+ int i;
+ unsigned int number, limit;
+ number = getRandomIntPositive(startAddr, endAddr);
+ limit = memoryRegions[0].size - 1;
+ i = 0;
+ while (number > limit) {
+ limit += memoryRegions[i].size;
+ i++;
+ }
+ return memoryRegions[i].startAddr + (number - limit);
+}
+
+/**
+ * Função que injeta falhas no sistema.
+ *
+ * @param addrStart - faixa de endereço inicial.
+ * @param addrEnd - faixa de endereço final.
+ * @param changedBytes - quantidade de bytes alterados dentro da faixa de endereço.
+ * @param changedBits - quantidade de bits que foram alterados (0 (auto)... 8).
+ */
+void FaultInjector::injectFaults(unsigned int addrStart, unsigned int addrEnd,
+ unsigned int changedBytes, unsigned int changedBits) {
+
+ int count, temp, position;
+ unsigned char *addr;
+ char buffer[32];
+ bool drawn[8]; // bits sorteados (para não alterar o mesmo bit mais de uma vez).
+
+ if (changedBytes < 1 || changedBytes > (addrEnd - addrStart))
+ changedBytes = DEFAULT_CHANGED_BYTES;
+
+ if (changedBits > 8) // 0 para automático, 8 para alterar todos os bits
+ changedBits = DEFAULT_CHANGED_BITS;
+
+ temp = changedBits;
+
+ LocalFileSystem local("local");
+ FILE *fp = fopen("/local/falhas.txt", "a");
+
+ if (fp != NULL) {
+ time_t seconds = time(NULL);
+ strftime(buffer, 32, "%d/%m/%Y %H:%M:%S", localtime(&seconds));
+
+ fprintf(fp, "%s\n", buffer);
+// fprintf(fp, "Endereco inicial: %d\n", addrStart);
+// fprintf(fp, "Endereco final: %d\n", addrEnd);
+// fprintf(fp, "Bytes alterados: %d\n", changedBytes);
+// fprintf(fp, "Bits alterados: %d\n\n", changedBits);
+
+ for (int i = 0; i < changedBytes; i++) {
+ char value[9];
+ addr = (unsigned char *) getByteMemory(addrStart, addrEnd);
+ itoa(*addr, value, 2);
+
+ fprintf(fp, "%d\n", i + 1);
+ fprintf(fp, "Endereco (em byte): %p\n", addr);
+ fprintf(fp, "Valor correto: %s\n", value);
+
+ if (changedBits == 0)
+ temp = getRandomIntPositive(1, 8); // Valor 0 altera n bits automaticamente.
+
+ for (count = 0; count < 8; count++)
+ drawn[count] = false;
+
+ count = 0;
+ while (count < temp) {
+ position = getRandomIntPositive(0, 7);
+ if (drawn[position] != true) {
+ drawn[position] = true;
+ count++;
+ (*addr) ^= (1 << position); // Altera um bit de um endereço (byte) de memória.
+ }
+ }
+ itoa(*addr, value, 2);
+ fprintf(fp, "Valor alterado: %s\n\n", value);
+ }
+ fclose(fp);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/FaultInjector.h Sat Sep 14 22:58:28 2013 +0000
@@ -0,0 +1,43 @@
+/*
+ * File: FaultsInjector.h
+ * Author: Kleber Kruger
+ *
+ * Created on 11 de Junho de 2013, 09:20
+ */
+
+#ifndef FAULTINJECTOR_H
+#define FAULTINJECTOR_H
+
+#include "Utils.h"
+
+typedef struct {
+ char name[32];
+ unsigned int startAddr;
+ unsigned int endAddr;
+ unsigned int size;
+} MemoryRegion;
+
+class FaultInjector {
+public:
+
+ static const int DEFAULT_CHANGED_BYTES = 1; // Quantidade de bytes alterados em cada injeção de falha.
+ static const int DEFAULT_CHANGED_BITS = 0; // Quantidade de bits alterados dentro do byte. (1 - 8); 0 = Aleatório.
+
+ FaultInjector();
+ virtual ~FaultInjector();
+
+ void injectFaults(unsigned int changedBytes, unsigned int changedBits);
+
+private:
+
+ unsigned int memorySize;
+
+ static MemoryRegion memoryRegions[];
+
+ unsigned int getByteMemory(unsigned int startAddr, unsigned int endAddr);
+
+ void injectFaults(unsigned int addrStart, unsigned int addrEnd,
+ unsigned int changedBytes, unsigned int changedBits);
+};
+
+#endif /* FAULTSINJECTOR_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Main.cpp Sat Sep 14 22:58:28 2013 +0000
@@ -0,0 +1,21 @@
+/*
+ * File: Main.cpp
+ * Author: Kleber Kruger
+ *
+ * Created on 11 de Junho de 2013, 01:23
+ */
+
+#include "FaultInjector.h"
+
+using namespace mbed;
+
+/*
+ *
+ */
+int main(void) {
+
+ FaultInjector injector;
+ injector.injectFaults(05, 06);
+
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Utils.cpp Sat Sep 14 22:58:28 2013 +0000
@@ -0,0 +1,78 @@
+/*
+ * File: Utils.cpp
+ * Author: Kleber Kruger
+ *
+ * Created on 11 de Junho de 2013, 11:39
+ */
+
+#include "Utils.h"
+
+/**
+ * Gera um número aleatório entre o intervalo mínimo e máximo.
+ *
+ * @param min - número mínimo
+ * @param max - número máximo
+ */
+unsigned int getRandomIntPositive(unsigned int min, unsigned int max) {
+ int k;
+ double d;
+ d = (double) rand() / ((double) RAND_MAX + 1);
+ k = d * (max - min + 1);
+ return min + k;
+}
+
+/**
+ * Gera um número aleatório entre o intervalo mínimo e máximo.
+ *
+ * @param min - número mínimo
+ * @param max - número máximo
+ */
+double getRandomDouble(double min, double max) {
+ double d, k;
+ d = (double) rand() / ((double) RAND_MAX + 1);
+ k = d * (max - min + 1);
+ return min + k;
+}
+
+/**
+ * Gera um número aleatório entre o intervalo mínimo e máximo.
+ *
+ * @param min - número mínimo
+ * @param max - número máximo
+ */
+float getRandomFloat(float min, float max) {
+ int rd = (int) getRandomDouble((int) (min * 10), (int) (max * 10) + 1);
+ return (float) rd / 10;
+}
+
+/**
+ * C++ version 0.4 char* style "itoa":
+ * Written by Lukás Chmela
+ * Released under GPLv3.
+ */
+char *itoa(int value, char *result, int base) {
+ // check that the base if valid
+ if (base < 2 || base > 36) {
+ *result = '\0';
+ return result;
+ }
+
+ char* ptr = result, *ptr1 = result, tmp_char;
+ int tmp_value;
+
+ do {
+ tmp_value = value;
+ value /= base;
+ *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
+ } while (value);
+
+ // Apply negative sign
+ if (tmp_value < 0) *ptr++ = '-';
+ *ptr-- = '\0';
+ while (ptr1 < ptr) {
+ tmp_char = *ptr;
+ *ptr-- = *ptr1;
+ *ptr1++ = tmp_char;
+ }
+ return result;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Utils.h Sat Sep 14 22:58:28 2013 +0000 @@ -0,0 +1,21 @@ +/* + * File: Utils.h + * Author: Kleber Kruger + * + * Created on 11 de Junho de 2013, 11:39 + */ + +#ifndef UTILS_H +#define UTILS_H + +#include "mbed.h" + +unsigned int getRandomIntPositive(unsigned int min, unsigned int max); + +double getRandomDouble(double min, double max); + +float getRandomFloat(float min, float max); + +char *itoa(int value, char *result, int base); + +#endif /* UTILS_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Sep 14 22:58:28 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17 \ No newline at end of file