static ExitCode InitPeripheralsAndHandlers()

in Tutorials/MemoryUsage/Stage2/main.c [284:334]


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");
    appendNodeButtonGpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_1);
    if (appendNodeButtonGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_BUTTON_1: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_ButtonAddNode;
    }

    // Open SAMPLE_BUTTON_2 GPIO as input, and set up a timer to poll it
    Log_Debug("Opening SAMPLE_BUTTON_2 as input.\n");
    deleteNodeButtonGpioFd = GPIO_OpenAsInput(SAMPLE_BUTTON_2);
    if (deleteNodeButtonGpioFd == -1) {
        Log_Debug("ERROR: Could not open SAMPLE_BUTTON_2: %s (%d).\n", strerror(errno), errno);
        return ExitCode_Init_ButtonDeleteNode;
    }

    // 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;
    }

    // Init the cURL library in order to demonstrate tracking of shared library heap memory usage
    CURLcode res = 0;
    if ((res = curl_global_init(CURL_GLOBAL_ALL)) != CURLE_OK) {
        Log_Debug(" (curl err=%d, '%s')\n", res, curl_easy_strerror(res));
        return ExitCode_Init_CurlGlobalInit;
    }

    if ((curlHandle = curl_easy_init()) == NULL) {
        Log_Debug("curl_easy_init() failed\n");
        return ExitCode_Init_CurlHandleInit;
    }

    return ExitCode_Success;
}