AzureSphere/AzureSphereSampleDevice/main.c
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");
    sendMessageButtonGpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_1);
    if (sendMessageButtonGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_BUTTON_1: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_MessageButton;
    }

    // SAMPLE_LED is used to show Device Twin settings state
    Log_Debug("Opening SAMPLE_LED as output.\n");
    deviceTwinStatusLedGpioFd =
        GPIO_OpenAsOutput(SAMPLE_LED, GPIO_OutputMode_PushPull, GPIO_Value_High);
    if (deviceTwinStatusLedGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_LED: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_TwinStatusLed;
    }

    // Set up a timer to poll for button events.
    static const struct timespec buttonPressCheckPeriod = {.tv_sec = 0, .tv_nsec = 1000 * 1000};
    buttonPollTimer = CreateEventLoopPeriodicTimer(eventLoop, &ButtonPollTimerEventHandler,
                                                   &buttonPressCheckPeriod);
    if (buttonPollTimer == NULL) {
        return ExitCode_Init_ButtonPollTimer;
    }

    azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds;
    struct timespec azureTelemetryPeriod = {.tv_sec = azureIoTPollPeriodSeconds, .tv_nsec = 0};
    azureTimer =
        CreateEventLoopPeriodicTimer(eventLoop, &AzureTimerEventHandler, &azureTelemetryPeriod);
    if (azureTimer == NULL) {
        return ExitCode_Init_AzureTimer;
    }

    return ExitCode_Success;
}
/// <summary>
///     Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.
/// </summary>
/// <returns>
///     ExitCode_Success if all resources were allocated successfully; otherwise another
///     ExitCode value which indicates the specific failure.
/// </returns>

Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.


ExitCode_Success if all resources were allocated successfully; otherwise another
ExitCode value which indicates the specific failure.
Set up SIGTERM termination handler initialize peripherals and set up event handlers . ExitCode_Success if all resources were allocated successfully ; otherwise another ExitCode value which indicates the specific failure .
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;
    }
    Log_Debug("Opening SAMPLE_BUTTON_1 as input.\n");
    sendMessageButtonGpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_1);
    if (sendMessageButtonGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_BUTTON_1: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_MessageButton;
    }
    Log_Debug("Opening SAMPLE_LED as output.\n");
    deviceTwinStatusLedGpioFd =
        GPIO_OpenAsOutput(SAMPLE_LED, GPIO_OutputMode_PushPull, GPIO_Value_High);
    if (deviceTwinStatusLedGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_LED: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_TwinStatusLed;
    }
    static const struct timespec buttonPressCheckPeriod = {.tv_sec = 0, .tv_nsec = 1000 * 1000};
    buttonPollTimer = CreateEventLoopPeriodicTimer(eventLoop, &ButtonPollTimerEventHandler,
                                                   &buttonPressCheckPeriod);
    if (buttonPollTimer == NULL) {
        return ExitCode_Init_ButtonPollTimer;
    }
    azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds;
    struct timespec azureTelemetryPeriod = {.tv_sec = azureIoTPollPeriodSeconds, .tv_nsec = 0};
    azureTimer =
        CreateEventLoopPeriodicTimer(eventLoop, &AzureTimerEventHandler, &azureTelemetryPeriod);
    if (azureTimer == NULL) {
        return ExitCode_Init_AzureTimer;
    }
    return ExitCode_Success;
}
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 ( " \n " ) ; return ExitCode_Init_EventLoop ; } Log_Debug ( " \n " ) ; sendMessageButtonGpioFd = GPIO_OpenAsInput ( SAMPLE_BUTTON_1 ) ; if ( sendMessageButtonGpioFd == -1 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; return ExitCode_Init_MessageButton ; } Log_Debug ( " \n " ) ; deviceTwinStatusLedGpioFd = GPIO_OpenAsOutput ( SAMPLE_LED , GPIO_OutputMode_PushPull , GPIO_Value_High ) ; if ( deviceTwinStatusLedGpioFd == -1 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; return ExitCode_Init_TwinStatusLed ; } static const struct timespec buttonPressCheckPeriod = { . tv_sec = 0 , . tv_nsec = 1000 * 1000 } ; buttonPollTimer = CreateEventLoopPeriodicTimer ( eventLoop , & ButtonPollTimerEventHandler , & buttonPressCheckPeriod ) ; if ( buttonPollTimer == NULL ) { return ExitCode_Init_ButtonPollTimer ; } azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds ; struct timespec azureTelemetryPeriod = { . tv_sec = azureIoTPollPeriodSeconds , . tv_nsec = 0 } ; azureTimer = CreateEventLoopPeriodicTimer ( eventLoop , & AzureTimerEventHandler , & azureTelemetryPeriod ) ; if ( azureTimer == NULL ) { return ExitCode_Init_AzureTimer ; } return ExitCode_Success ; }
<summary>
Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.
<summary > Set up SIGTERM termination handler initialize peripherals and set up event handlers .
// Open SAMPLE_BUTTON_1 GPIO as input // SAMPLE_LED is used to show Device Twin settings state // Set up a timer to poll for button events.