1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#include <errno.h>
27#include <unistd.h>
28
29#ifndef CHAR_CTRL_C
30#define CHAR_CTRL_C (3)
31#endif
32
33void mp_hal_set_interrupt_char(char c);
34
35#define mp_hal_stdio_poll unused // this is not implemented, nor needed
36void mp_hal_stdio_mode_raw(void);
37void mp_hal_stdio_mode_orig(void);
38
39#if MICROPY_PY_BUILTINS_INPUT && MICROPY_USE_READLINE == 0
40
41#include <malloc.h>
42#include "py/misc.h"
43#include "input.h"
44#define mp_hal_readline mp_hal_readline
45static inline int mp_hal_readline(vstr_t *vstr, const char *p) {
46 char *line = prompt((char *)p);
47 vstr_add_str(vstr, line);
48 free(line);
49 return 0;
50}
51
52#elif MICROPY_PY_BUILTINS_INPUT && MICROPY_USE_READLINE == 1
53
54#include "py/misc.h"
55#include "lib/mp-readline/readline.h"
56// For built-in input() we need to wrap the standard readline() to enable raw mode
57#define mp_hal_readline mp_hal_readline
58static inline int mp_hal_readline(vstr_t *vstr, const char *p) {
59 mp_hal_stdio_mode_raw();
60 int ret = readline(vstr, p);
61 mp_hal_stdio_mode_orig();
62 return ret;
63}
64
65#endif
66
67static inline void mp_hal_delay_us(mp_uint_t us) {
68 usleep(us);
69}
70#define mp_hal_ticks_cpu() 0
71
72// This macro is used to implement PEP 475 to retry specified syscalls on EINTR
73#define MP_HAL_RETRY_SYSCALL(ret, syscall, raise) { \
74 for (;;) { \
75 MP_THREAD_GIL_EXIT(); \
76 ret = syscall; \
77 MP_THREAD_GIL_ENTER(); \
78 if (ret == -1) { \
79 int err = errno; \
80 if (err == EINTR) { \
81 mp_handle_pending(true); \
82 continue; \
83 } \
84 raise; \
85 } \
86 break; \
87 } \
88}
89
90#define RAISE_ERRNO(err_flag, error_val) \
91 { if (err_flag == -1) \
92 { mp_raise_OSError(error_val); } }
93
94#if MICROPY_PY_BLUETOOTH
95enum {
96 MP_HAL_MAC_BDADDR,
97};
98
99void mp_hal_get_mac(int idx, uint8_t buf[6]);
100#endif
101