<commit_before>/**
 * Native CPU lpm.h implementation
 *
 * Uses system calls to emulate CPU power modes.
 *
 * Copyright (C) 2013 <NAME>
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License. See the file LICENSE in the top level directory for more
 * details.
 *
 * @ingroup lpm
 * @ingroup native_cpu
 * @{
 * @file
 * @author  <NAME> <<EMAIL>>
 * @}
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef MODULE_UART0
#include <sys/select.h>
#include <errno.h>
#endif
#include <err.h>

#include "lpm.h"
#include "debug.h"
#include "cpu.h"

#include "native_internal.h"
#ifdef MODULE_UART0
#include "board_internal.h"
#endif

static enum lpm_mode native_lpm;

void lpm_init(void)
{
    DEBUG("lpm_init()\n");
    native_lpm = LPM_ON;
    return;
}

void _native_lpm_sleep()
{
#ifdef MODULE_UART0
    int nfds;

    /* set fds */
    FD_ZERO(&_native_rfds);
    nfds = _native_set_uart_fds();
    nfds++;

    _native_in_syscall++; // no switching here
    nfds = select(nfds, &_native_rfds, NULL, NULL, NULL);
    _native_in_syscall--;

    DEBUG("_native_lpm_sleep: returned: %i\n", nfds);

    if (nfds != -1) {
        /* uart ready, handle input */
        /* TODO: switch to ISR context */
        _native_handle_uart0_input();
    }
    else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
        /* would block / resource unavailable .. it appears a
         * contended socket can show this behavior sometimes */
        _native_in_syscall++;
        warn("_native_lpm_sleep: select()");
        _native_in_syscall--;
        return;
    }
    else if (errno != EINTR) {
        /* select failed for reason other than signal */
        err(EXIT_FAILURE, "lpm_set(): select()");
    }

    /* otherwise select was interrupted because of a signal, continue below */
#else
    _native_in_syscall++; // no switching here
    pause();
    _native_in_syscall--;
#endif

    if (_native_sigpend > 0) {
        DEBUG("\n\n\t\treturn from syscall, calling native_irq_handler\n\n");
        _native_in_syscall++;
        _native_syscall_leave();
    }
}

/**
 * LPM_IDLE uses sleep() to wait for interrupts
 * LPM_OFF exits process
 * other modes not supported at the moment
 */
enum lpm_mode lpm_set(enum lpm_mode target)
{
    enum lpm_mode last_lpm;

    //DEBUG("lpm_set(%i)\n", target);

    last_lpm = native_lpm;
    native_lpm = target;

    switch(native_lpm) { /* @contiki :-p  */
        case LPM_ON:
            break;

        case LPM_IDLE:
            //DEBUG("lpm_set(): pause()\n");

            //pause();
            _native_lpm_sleep();
            break;

        /* XXX: unfinished modes: */
        case LPM_SLEEP:
            /*TODO: implement*/
            printf("XXX: lpm_set(): LPM_SLEEP not implemented\n");
            //sigsuspend();

        case LPM_POWERDOWN:
            /*TODO: implement*/
            printf("XXX: lpm_set(): LPM_POWERDOWN not implemented\n");
            //sigsuspend();

        case LPM_OFF:
            printf("lpm_set(): exit()\n");
            exit(0);
            break;

        default:
            DEBUG("XXX: unsupported power mode: %i\n", native_lpm);
            exit(1);
    }

    return last_lpm;
}

void lpm_awake(void)
{
    DEBUG("XXX: lpm_awake()\n");
    native_lpm = LPM_ON;
    return;
}

void lpm_begin_awake(void)
{
    DEBUG("XXX: lpm_begin_awake()\n");
    return;
}

void lpm_end_awake(void)
{
    DEBUG("XXX: lpm_end_awake()\n");
    native_lpm = LPM_ON;
    return;
}

enum lpm_mode lpm_get(void)
{
    return native_lpm;
}
<commit_msg>native: removed unreachable code<commit_after>/**
 * Native CPU lpm.h implementation
 *
 * Uses system calls to emulate CPU power modes.
 *
 * Copyright (C) 2013 <NAME>
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License. See the file LICENSE in the top level directory for more
 * details.
 *
 * @ingroup lpm
 * @ingroup native_cpu
 * @{
 * @file
 * @author  <NAME> <<EMAIL>>
 * @}
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef MODULE_UART0
#include <sys/select.h>
#include <errno.h>
#endif
#include <err.h>

#include "lpm.h"
#include "debug.h"
#include "cpu.h"

#include "native_internal.h"
#ifdef MODULE_UART0
#include "board_internal.h"
#endif

static enum lpm_mode native_lpm;

void lpm_init(void)
{
    DEBUG("lpm_init()\n");
    native_lpm = LPM_ON;
    return;
}

void _native_lpm_sleep()
{
#ifdef MODULE_UART0
    int nfds;

    /* set fds */
    FD_ZERO(&_native_rfds);
    nfds = _native_set_uart_fds();
    nfds++;

    _native_in_syscall++; // no switching here
    nfds = select(nfds, &_native_rfds, NULL, NULL, NULL);
    _native_in_syscall--;

    DEBUG("_native_lpm_sleep: returned: %i\n", nfds);

    if (nfds != -1) {
        /* uart ready, handle input */
        /* TODO: switch to ISR context */
        _native_handle_uart0_input();
    }
    else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
        /* would block / resource unavailable .. it appears a
         * contended socket can show this behavior sometimes */
        _native_in_syscall++;
        warn("_native_lpm_sleep: select()");
        _native_in_syscall--;
        return;
    }
    else if (errno != EINTR) {
        /* select failed for reason other than signal */
        err(EXIT_FAILURE, "lpm_set(): select()");
    }

    /* otherwise select was interrupted because of a signal, continue below */
#else
    _native_in_syscall++; // no switching here
    pause();
    _native_in_syscall--;
#endif

    if (_native_sigpend > 0) {
        DEBUG("\n\n\t\treturn from syscall, calling native_irq_handler\n\n");
        _native_in_syscall++;
        _native_syscall_leave();
    }
}

/**
 * LPM_IDLE uses sleep() to wait for interrupts
 * LPM_OFF exits process
 * other modes not supported at the moment
 */
enum lpm_mode lpm_set(enum lpm_mode target)
{
    enum lpm_mode last_lpm;

    //DEBUG("lpm_set(%i)\n", target);

    last_lpm = native_lpm;
    native_lpm = target;

    switch(native_lpm) { /* @contiki :-p  */
        case LPM_ON:
            break;

        case LPM_IDLE:
            //DEBUG("lpm_set(): pause()\n");

            //pause();
            _native_lpm_sleep();
            break;

        /* XXX: unfinished modes: */
        case LPM_SLEEP:
            /*TODO: implement*/
            printf("XXX: lpm_set(): LPM_SLEEP not implemented\n");
            //sigsuspend();

        case LPM_POWERDOWN:
            /*TODO: implement*/
            printf("XXX: lpm_set(): LPM_POWERDOWN not implemented\n");
            //sigsuspend();

        case LPM_OFF:
            printf("lpm_set(): exit()\n");
            exit(0);

        default:
            DEBUG("XXX: unsupported power mode: %i\n", native_lpm);
            exit(1);
    }

    return last_lpm;
}

void lpm_awake(void)
{
    DEBUG("XXX: lpm_awake()\n");
    native_lpm = LPM_ON;
    return;
}

void lpm_begin_awake(void)
{
    DEBUG("XXX: lpm_begin_awake()\n");
    return;
}

void lpm_end_awake(void)
{
    DEBUG("XXX: lpm_end_awake()\n");
    native_lpm = LPM_ON;
    return;
}

enum lpm_mode lpm_get(void)
{
    return native_lpm;
}

{'fraction_non_alphanumeric': 0.13253348214285715, 'fraction_numerical': 0.0033482142857142855, 'mean_word_length': 2.4649589173513773, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6159542', 'n_tokens_mistral': 2589, 'n_tokens_neox': 2482, 'n_words': 612}