1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <unistd.h>
16 #include "cmsis_os2.h"
17 #include "los_compiler.h"
18 #include "los_debug.h"
19 #include "los_task.h"
20 #include "nvs.h"
21 #include "ohos_init.h"
22 #include "ohos_types.h"
23 #include "pthread.h"
24 #include "samgr_lite.h"
25 #include "stdio.h"
26
27 #define DELAY_1S 1000
28 #define DELAY_10S (DELAY_1S * 10)
29
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wattribute-alias"
s_raise(int sig)32 static int s_raise(int sig)
33 {
34 if (SIGABRT == sig) {
35 LOS_TaskDelete(LOS_CurTaskIDGet());
36 while (1) {
37 LOS_Msleep(DELAY_1S);
38 }
39 }
40 return 0;
41 }
42
43 int raise(int sig) __attribute__((alias("s_raise")));
44
s_raise_r(struct _reent * r,int sig)45 static int s_raise_r(struct _reent *r, int sig)
46 {
47 return 0;
48 }
49 int _raise_r(struct _reent *r, int sig) __attribute__((alias("s_raise_r")));
50
51 #pragma GCC diagnostic pop
52
_open_r(struct _reent * r,const char * path,int flags,int mode)53 int _open_r(struct _reent *r, const char *path, int flags, int mode)
54 {
55 return _open(path, flags, mode);
56 }
_close_r(struct _reent * r,int fd)57 int _close_r(struct _reent *r, int fd)
58 {
59 return _close(fd);
60 }
_lseek_r(struct _reent * r,int fd,off_t size,int mode)61 off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode)
62 {
63 return _lseek(fd, size, mode);
64 }
65
_link_r(struct _reent * r,const char * n1,const char * n2)66 int _link_r(struct _reent *r, const char *n1, const char *n2)
67 {
68 return link(n1, n2);
69 }
_unlink_r(struct _reent * r,const char * path)70 int _unlink_r(struct _reent *r, const char *path)
71 {
72 return _unlink(path);
73 }
_stat_r(struct _reent * r,const char * path,struct stat * st)74 int _stat_r(struct _reent *r, const char *path, struct stat *st)
75 {
76 return _stat(path, st);
77 }
78
_rename_r(struct _reent * r,const char * src,const char * dst)79 int _rename_r(struct _reent *r, const char *src, const char *dst)
80 {
81 return rename(src, dst);
82 }
83
_fstat_r(struct _reent * r,int fd,struct stat * st)84 int _fstat_r(struct _reent *r, int fd, struct stat *st)
85 {
86 return _fstat(fd, st);
87 }
88
_getpid_r(struct _reent * r)89 int _getpid_r(struct _reent *r)
90 {
91 return LOS_CurTaskIDGet();
92 }
93
_kill_r(struct _reent * r,int pid,int sig)94 int _kill_r(struct _reent *r, int pid, int sig)
95 {
96 return LOS_TaskDelete(pid);
97 }
98
_sbrk_r(struct _reent * r,ptrdiff_t sz)99 void *_sbrk_r(struct _reent *r, ptrdiff_t sz)
100 {
101 char *name;
102 UINT32 taskId = LOS_CurTaskIDGet();
103 name = LOS_TaskNameGet(taskId);
104 esp_rom_printf("\e[0;36msbrk.taskName=%s taskId=%d\e[0m\r\n", name ? name : "NULL", taskId);
105 LOS_TaskDelete(taskId);
106 LOS_TaskSuspend(taskId);
107 while (1) {
108 LOS_TaskDelay(DELAY_10S);
109 }
110 }
111