Solution Reliability - JawaharT/Best-Practices-On-Azure-Sphere GitHub Wiki

Solutions for Reliability

Solution to exercise one

#include <assert.h> 
#include <stdio.h> 

// Sequence generator 
int generate_sequence(end_pos){ 
    int fib_sequence[end_pos]; 
    fib_sequence[0] = 0; 
    printf("%d ", 0); 

    if (end_pos >= 2){ 
        fib_sequence[1] = 1; 
        printf("%d ", 1); 

        for(int index = 0; index < end_pos-2; index++){ 
            int next_number = fib_sequence[index] + fib_sequence[index+1]; 
            printf("%d ", next_number); 
            fib_sequence[index+2] = next_number; 
        } 
    } 
    return fib_sequence[end_pos-1]; 
} 

// Testing without Framework 
int main() { 
  printf("Test Sequence with pos 2: "); 
  assert(1 == generate_sequence(2)); 

  printf("\nTest Sequence with pos 3: "); 
  assert(1 == generate_sequence(3)); 

  printf("\nTest Sequence with pos 4: "); 
  assert(2 == generate_sequence(4)); 

  printf("\nTest Sequence with pos 10: "); 
  assert(34 == generate_sequence(10));

  printf("\nTest Sequence with pos 47: "); 
  assert(1836311903 == generate_sequence(47));

  // Expected to fail, due to Int limits 
  printf("\nTest Sequence with pos 48: "); 
  assert(2971215073 == generate_sequence(48)); 
} 

Modifications made:

  • Removed End_integer_input function, will now manually parse integers into generate_sequence
  • Generate_sequence will now return the last Fibonacci number; this is used to compare inside the assert test

The Assert function is designed to fail and end the current program at that point if the statement parsed is false. This is unlike modern testing frameworks that will test all assert statements and return the number of passed and failed tests.

Test Sequence with pos 2: 0 1  
Test Sequence with pos 3: 0 1 1  
Test Sequence with pos 4: 0 1 1 2  
Test Sequence with pos 10: 0 1 1 2 3 5 8 13 21 34  
Test Sequence with pos 47: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903  

2Ubsu09tEj.o: /tmp/2Ubsu09tEj.c:42: main: Assertion `2971215073 == generate_sequence(48)' failed. 
Aborted 

As you can see above, these tests have shown the problems with the program which can be used to improve the reliability of the system it will be part of.

Solution to exercise two

static volatile sig_atomic_t terminationRequired = false;
static volatile sig_atomic_t _exitCode = 0;

// Create your exit code enum here
typedef enum {
    ExitCode_Invalid_Cmd_Args = 1,
    ExitCode_Unable_To_Open_Peripherals = 2,
    ExitCode_EventLoop_Error = 3
} exit_codes;

/// <summary>
///     Main entry point for this application.
/// </summary>
int main(int argc, char* argv[]) {
    Log_Debug("Application Started.\n");
    dx_registerTerminationHandler();

    if (!dx_configParseCmdLineArguments(argc, argv, &dx_config)) {
        // Add an potential exitcode here
        _exitCode = ExitCode_Invalid_Cmd_Args;
        terminationRequired = true;
    }

   // Create and open peripherals for button poll timer, button GPIO and epoll
   if (InitPeripheralsAndHandlers() != 0) {
       // Add an potential exitcode here
       _exitCode = ExitCode_Unable_To_Open_Peripherals;
       terminationRequired = true;
   }

   // Use epoll to wait for events and trigger handlers, until an error
   while (!terminationRequired) {
       int result = EventLoop_Run(dx_timerGetEventLoop(), -1, true);
       if ((WaitForEventAndCallHandler(epollFd) != 0) || (result == -1)) {

           // Add an potential exitcode here
	   _exitcode = ExitCode_EventLoop_Error;
           terminationRequired = true;

           ClosePeripheralsAndHandlers();
       }
   }
   
   return 0;
}

This is exercise is to demonstrate how to effectively create and use an exitcode enum inside an Azure Sphere project. This can be extended throughout an application and can have as many as 49 user defined exit codes. There are C standard exit codes to keep in mind which in turn keeps troubleshooting more straightforward as discussed previously in the 'How: Reliability' section.

⚠️ **GitHub.com Fallback** ⚠️