Contiki 3.x
newlib-syscalls.c
1 /*
2  * Copyright (C) 2015, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/stat.h>
32 #include <errno.h>
33 
34 #include "uart.h"
35 #include "helpers.h"
36 
37 #define CONSOLE_OUTPUT_DEV QUARK_X1000_UART_1
38 
39 #define HEAP_MAX_SIZE 2048
40 
41 static char _heap[HEAP_MAX_SIZE];
42 static char *prog_break = _heap;
43 
44 int
45 _close_r(struct _reent *ptr, int file)
46 {
47  /* Stubbed function */
48  ptr->_errno = ENOTSUP;
49  return -1;
50 }
51 /*---------------------------------------------------------------------------*/
52 void
53 _exit(int status)
54 {
55  halt();
56 }
57 /*---------------------------------------------------------------------------*/
58 int
59 _getpid_r(struct _reent *ptr)
60 {
61  /* Stubbed function */
62  ptr->_errno = ENOTSUP;
63  return -1;
64 }
65 /*---------------------------------------------------------------------------*/
66 int
67 _isatty_r(struct _reent *ptr, int file)
68 {
69  /* Stubbed function */
70  ptr->_errno = ENOTSUP;
71  return 0;
72 }
73 /*---------------------------------------------------------------------------*/
74 int
75 _kill_r(struct _reent *ptr, int pid, int signal)
76 {
77  /* Stubbed function */
78  ptr->_errno = ENOTSUP;
79  return -1;
80 }
81 /*---------------------------------------------------------------------------*/
82 int
83 _read_r(struct _reent *ptr, int file, char *buf, int len)
84 {
85  /* Stubbed function */
86  ptr->_errno = ENOTSUP;
87  return -1;
88 }
89 /*---------------------------------------------------------------------------*/
90 int
91 _write_r(struct _reent *ptr, int file, const char *buf, int len)
92 {
93  int ret;
94  int i;
95 
96  switch(file) {
97  case 0:
98  ptr->_errno = EBADF;
99  ret = -1;
100  break;
101 
102  case 1:
103  case 2:
104  for(i = 0; i < len; i++) {
105  /* Since file descriptors 1 and 2 (stdout and stderr) are mapped to a
106  * serial console, we should translate the 'newline' escape sequence
107  * to 'carriage return' (CR) followed by 'line feed' (LF) ASCII
108  * characters.
109  */
110  if(buf[i] == '\n') {
111  quarkX1000_uart_tx(CONSOLE_OUTPUT_DEV, '\r');
112  }
113  quarkX1000_uart_tx(CONSOLE_OUTPUT_DEV, buf[i]);
114  }
115 
116  ret = len;
117  break;
118 
119  default:
120  /* We don't support any filesystem yet. */
121  ptr->_errno = ENOTSUP;
122  ret = -1;
123  break;
124  }
125 
126  return ret;
127 }
128 /*---------------------------------------------------------------------------*/
129 int
130 _lseek_r(struct _reent *ptr, int file, int p, int dir)
131 {
132  /* Stubbed function */
133  ptr->_errno = ENOTSUP;
134  return -1;
135 }
136 /*---------------------------------------------------------------------------*/
137 int
138 _fstat_r(struct _reent *ptr, int file, struct stat *st)
139 {
140  /* We don't support the standard input yet so file descriptor 0 is not
141  * supported by this function. Additionally, we don't have support for
142  * any filesystem thus file descriptors greater than 2 are not supported
143  * as well.
144  *
145  * We support standard ouput and error (file descriptors 1 and 2) only.
146  */
147  if(file == 0 || file > 2) {
148  ptr->_errno = ENOTSUP;
149  return -1;
150  }
151 
152  st->st_mode = S_IFCHR;
153  return 0;
154 }
155 /*---------------------------------------------------------------------------*/
156 caddr_t
157 _sbrk_r(struct _reent *ptr, int incr)
158 {
159  char *prev_prog_break;
160 
161  /* If the new program break overruns the maximum heap address, we return
162  * "Out of Memory" error to the user.
163  */
164  if(prog_break + incr > _heap + HEAP_MAX_SIZE) {
165  ptr->_errno = ENOMEM;
166  return NULL;
167  }
168 
169  prev_prog_break = prog_break;
170 
171  prog_break += incr;
172 
173  return prev_prog_break;
174 }
#define NULL
The null pointer.