APM:Libraries
stubs.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17 * **** This file incorporates work covered by the following copyright and ****
18 * **** permission notice: ****
19 *
20 * Copyright (c) 2009 by Michael Fischer. All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the author nor the names of its contributors may
32 * be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
43 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
45 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 ****************************************************************************
49 * History:
50 *
51 * 28.03.09 mifi First Version, based on the original syscall.c from
52 * newlib version 1.17.0
53 * 17.08.09 gdisirio Modified the file for use under ChibiOS/RT
54 * 15.11.09 gdisirio Added read and write handling
55 ****************************************************************************/
56 
57 /*
58  * This file is free software: you can redistribute it and/or modify it
59  * under the terms of the GNU General Public License as published by the
60  * Free Software Foundation, either version 3 of the License, or
61  * (at your option) any later version.
62  *
63  * This file is distributed in the hope that it will be useful, but
64  * WITHOUT ANY WARRANTY; without even the implied warranty of
65  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
66  * See the GNU General Public License for more details.
67  *
68  * You should have received a copy of the GNU General Public License along
69  * with this program. If not, see <http://www.gnu.org/licenses/>.
70  *
71  * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
72  */
73 #include <sys/unistd.h>
74 #include <errno.h>
75 #include <string.h>
76 #include <sys/stat.h>
77 #include <sys/types.h>
78 
79 #include "ch.h"
80 #if defined(STDOUT_SD) || defined(STDIN_SD)
81 #include "hal.h"
82 #endif
83 
84 uint8_t _before_main = 1;
85 
86 /***************************************************************************/
87 
89 int _read(struct _reent *r, int file, char * ptr, int len)
90 {
91  (void)r;
92 #if defined(STDIN_SD)
93  if (!len || (file != 0)) {
94  __errno_r(r) = EINVAL;
95  return -1;
96  }
97  len = sdRead(&STDIN_SD, (uint8_t *)ptr, (size_t)len);
98  return len;
99 #else
100  (void)file;
101  (void)ptr;
102  (void)len;
103  __errno_r(r) = EINVAL;
104  return -1;
105 #endif
106 }
107 
108 /***************************************************************************/
109 
110 __attribute__((used))
111 int _lseek(struct _reent *r, int file, int ptr, int dir)
112 {
113  (void)r;
114  (void)file;
115  (void)ptr;
116  (void)dir;
117 
118  return 0;
119 }
120 
121 /***************************************************************************/
122 
123 __attribute__((used))
124 int _write(struct _reent *r, int file, char * ptr, int len)
125 {
126  (void)r;
127  (void)file;
128  (void)ptr;
129 #if defined(STDOUT_SD)
130  if (file != 1) {
131  __errno_r(r) = EINVAL;
132  return -1;
133  }
134  sdWrite(&STDOUT_SD, (uint8_t *)ptr, (size_t)len);
135 #endif
136  return len;
137 }
138 
139 /***************************************************************************/
140 
141 __attribute__((used))
142 int _close(struct _reent *r, int file)
143 {
144  (void)r;
145  (void)file;
146 
147  return 0;
148 }
149 
150 /***************************************************************************/
151 
152 __attribute__((used))
153 caddr_t _sbrk(struct _reent *r, int incr)
154 {
155 #if CH_CFG_USE_MEMCORE
156  void *p;
157 
158  chDbgCheck(incr >= 0);
159  p = chHeapAlloc(NULL, (size_t)incr);
160  if (p == NULL) {
161  __errno_r(r) = ENOMEM;
162  return (caddr_t)-1;
163  }
164  return (caddr_t)p;
165 #else
166  (void)incr;
167  __errno_r(r) = ENOMEM;
168  return (caddr_t)-1;
169 #endif
170 }
171 
172 
173 /***************************************************************************/
174 
175 __attribute__((used))
176 int _fstat(struct _reent *r, int file, struct stat * st)
177 {
178  (void)r;
179  (void)file;
180 
181  memset(st, 0, sizeof(*st));
182  st->st_mode = S_IFCHR;
183  return 0;
184 }
185 
186 /***************************************************************************/
187 
188 __attribute__((used))
189 int _isatty(struct _reent *r, int fd)
190 {
191  (void)r;
192  (void)fd;
193 
194  return 1;
195 }
196 
197 __attribute__((used))
198 pid_t _getpid(void)
199 {
200  return 0;
201 }
202 
203 __attribute__((used))
204 void _exit( int status )
205 {
206  (void)status;
207  while( 1 );
208 }
209 
210 __attribute__((used))
211 void _fini(void)
212 {
213 }
214 
215 __attribute__((used))
216 int _kill( int pid, int sig )
217 {
218  (void)pid; (void)sig;
219  return -1;
220 }
221 
222 /*** EOF ***/
caddr_t _sbrk(int nbytes)
Definition: syscalls.c:127
int _close(int fd)
Definition: syscalls.c:136
__attribute__((used))
Definition: stubs.c:88
void _exit(int status)
Definition: syscalls.c:54
int _write(int fd, const char *buf, size_t cnt)
Definition: syscalls.c:191
int _lseek(int fd, off_t pos, int whence)
Definition: syscalls.c:153
int _getpid(void)
Definition: syscalls.c:59
int _isatty(int fd)
Definition: syscalls.c:145
int _kill(int pid, int sig)
Definition: syscalls.c:48
#define NULL
Definition: hal_types.h:59
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1319
uint8_t _before_main
Definition: stubs.c:84
int _fstat(int fd, struct stat *st)
Definition: syscalls.c:140
int _read(int fd, char *buf, size_t cnt)
Definition: system.cpp:126