Solution Security and Safety - JawaharT/Best-Practices-On-Azure-Sphere GitHub Wiki
An example of this program run is shown below with different types of inputs and their outcomes:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int end_integer_input(){
// Temp variables
int digit=0, flag=1;
// Store input, for instance from embedded system keypad
static char number[10] = "12";
// Loop through each char in input
while(number[digit] != '\0'){
flag = isdigit(number[digit]);
// Not an Int found, otherwise keep going
if (!flag){
break;
}
digit++;
}
if(flag) {
// Reusing digit variable to convert input into integer
digit = atoi(number);
// Checks if integer input is appropriate for Fibonacci program
if ((digit >= 1) && (digit <= 47)){
return digit;
}else{
printf("Not a Valid Integer to generate sequence\n");
return 0;
}
}else{
printf("Not an Integer\n");
return 0;
}
}
void generate_sequence(int 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;
}
}
}
void main(){
int end_pos = end_integer_input();
if(end_pos >= 1){
generate_sequence(end_pos);
}
}
Output with generous input (12):
0 1 1 2 3 5 8 13 21 34 55 89
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int end_integer_input(){
// Temp variables
int digit=0, flag=1;
// Store input
static char number[10] = "123";
// Loop through each char in input
while(number[digit] != '\0'){
flag = isdigit(number[digit]);
// Not an Int found, otherwise keep going
if (!flag){
break;
}
digit++;
}
if(flag) {
// Reusing digit variable to convert input into integer
digit = atoi(number);
// Checks if integer input is appropriate for Fibonacci program
if ((digit >= 1) && (digit <= 47)){
return digit;
}else{
printf("Not a Valid Integer to generate sequence\n");
return 0;
}
}else{
printf("Not an Integer\n");
return 0;
}
}
void generate_sequence(int 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;
}
}
}
void main(){
int end_pos = end_integer_input();
if(end_pos >= 1){
generate_sequence(end_pos);
}
}
Output with not ideal input (123):
Not a Valid Integer to generate sequence
This is the full program including generate_sequence, to showcase that the modifications do not require a whole software to be changed. The modifications made include
Inside end_integer_input:
- Input is taken as a string
- Loop through each individual character in the string to make sure all are digits
- If the character is identified to not be a digit, the loop is broken otherwise continued until the end. Flag variable is used to keep track of the identification
- “Not an Integer” is displayed if a character that is not an integer is identified
- If it is an Integer check for only positive real numbers that are between 1 and 47 respectively otherwise exit
Inside main:
- Extra condition is added to make sure only positive real numbers call generate_sequence
// Button A state variable, set to button not-pressed
static GPIO_Value_Type currentButtonAState = GPIO_Value_High;
// Termination state
volatile sig_atomic_t terminationRequired = false;
static void ButtonTimerEventHandler(EventData *eventData){
bool buttonAPressed = false;
if (ConsumeTimerFdEvent(buttonPollTimerFd) != 0) {
terminationRequired = true;
return;
}
// Task 1: Check for button A press
GPIO_Value_Type newButtonAState;
int result = GPIO_GetValue(buttonAGpioFd, &newButtonAState);
if (result != 0) {
printf("ERROR: Could not read button GPIO: %s (%d).\n", strerror(errno), errno);
terminationRequired = true;
return;
}
// Task 2: Check if the button has GPIO_Value_Low when pressed and GPIO_Value_High when released
if (newButtonAState != currentButtonAState) {
if (newButtonAState == GPIO_Value_Low) {
printf("Button A pressed!\n");
buttonAPressed = true;
}else {
printf("Button A released!\n");
}
// Update the static variable to use next time
currentButtonAState = newButtonAState;
}
}
This is the full program for handling button A presses from the Azure Sphere board.
Task 1:
- newButtonAState declaration to store the file descriptor for GPIO button
- result will store the current button status
- The result is then checked if the GPIO button connection is successful before use, if failed then application is terminated
Task 2:
- First only perform the task if there is a change in button state
- Then make sure to confirm the state of the button and update the state variable to reflect it