in Tutorials/ErrorReporting/Stage1/main.c [281:355]
static ExitCode InitPeripheralsAndHandlers(void)
{
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = TerminationHandler;
sigaction(SIGTERM, &action, NULL);
eventLoop = EventLoop_Create();
if (eventLoop == NULL) {
Log_Debug("Could not create event loop.\n");
return ExitCode_Init_EventLoop;
}
// Open SAMPLE_BUTTON_1 GPIO as input
Log_Debug("Opening SAMPLE_BUTTON_1 as input.\n");
ledBlinkButton1GpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_1);
if (ledBlinkButton1GpioFd == -1) {
Log_Debug("ERROR: Could not open SAMPLE_BUTTON_1: %s (%d).\n", strerror(errno), errno);
return ExitCode_Init_Button1;
}
// Open SAMPLE_BUTTON_2 GPIO as input, and set up a timer to poll it
Log_Debug("Opening SAMPLE_BUTTON_2 as input.\n");
ledBlinkButton2GpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_2);
if (ledBlinkButton2GpioFd == -1) {
Log_Debug("ERROR: Could not open SAMPLE_BUTTON_2: %s (%d).\n", strerror(errno), errno);
return ExitCode_Init_Button2;
}
// Set up a timer to poll the buttons
struct timespec buttonPressCheckPeriod1Ms = {.tv_sec = 0, .tv_nsec = 1000000};
buttonPollTimer = CreateEventLoopPeriodicTimer(eventLoop, &ButtonTimerEventHandler,
&buttonPressCheckPeriod1Ms);
if (buttonPollTimer == NULL) {
return ExitCode_Init_ButtonPollTimer;
}
// Open SAMPLE_RGBLED_BLUE GPIO, set as output with value GPIO_Value_High (off), and set up a
// timer to blink it
Log_Debug("Opening SAMPLE_RGBLED_BLUE as output.\n");
blinkingLedBlueGpioFd =
GPIO_OpenAsOutput(SAMPLE_RGBLED_BLUE, GPIO_OutputMode_PushPull, GPIO_Value_High);
if (blinkingLedBlueGpioFd == -1) {
Log_Debug("ERROR: Could not open SAMPLE_RGBLED_BLUE GPIO: %s (%d).\n", strerror(errno),
errno);
return ExitCode_Init_LedBlue;
}
struct timespec blinkPeriod = {.tv_sec = 0, .tv_nsec = 500000000};
blinkTimer =
CreateEventLoopPeriodicTimer(eventLoop, &BlinkingLedTimerEventHandler, &blinkPeriod);
if (blinkTimer == NULL) {
return ExitCode_Init_LedBlinkTimer;
}
// Open SAMPLE_RGBLED_GREEN GPIO, set as output with value GPIO_Value_High (off), and set up a
// timer to blink it
Log_Debug("Opening SAMPLE_RGBLED_GREEN as output.\n");
blinkingLedGreenGpioFd =
GPIO_OpenAsOutput(SAMPLE_RGBLED_GREEN, GPIO_OutputMode_PushPull, GPIO_Value_High);
if (blinkingLedGreenGpioFd == -1) {
Log_Debug("ERROR: Could not open SAMPLE_RGBLED_GREEN GPIO: %s (%d).\n", strerror(errno),
errno);
return ExitCode_Init_LedGreen;
}
// Check is network is ready every second.
static const struct timespec oneSecond = {.tv_sec = 1, .tv_nsec = 0};
networkReadyCheckTimer =
CreateEventLoopPeriodicTimer(eventLoop, &NetworkReadyCheckTimerEventHandler, &oneSecond);
if (networkReadyCheckTimer == NULL) {
return ExitCode_Init_NetworkReadyCheckTimer;
}
return ExitCode_Success;
}