1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 */
9 /*
10 This file is part of UFFS, the Ultra-low-cost Flash File System.
11
12 Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
13
14 UFFS is free software; you can redistribute it and/or modify it under
15 the GNU Library General Public License as published by the Free Software
16 Foundation; either version 2 of the License, or (at your option) any
17 later version.
18
19 UFFS is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 or GNU Library General Public License, as applicable, for more details.
23
24 You should have received a copy of the GNU General Public License
25 and GNU Library General Public License along with UFFS; if not, write
26 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA.
28
29 As a special exception, if other files instantiate templates or use
30 macros or inline functions from this file, or you compile this file
31 and link it with other works to produce a work based on this file,
32 this file does not by itself cause the resulting work to be covered
33 by the GNU General Public License. However the source code for this
34 file must still be made available in accordance with section (3) of
35 the GNU General Public License v2.
36
37 This exception does not invalidate any other reasons why a work based
38 on this file might be covered by the GNU General Public License.
39 */
40
41 /**
42 * \file uffs_os_posix.c
43 * \brief Emulation on POSIX host. This is just a dumb implementation, does not really create semaphores.
44 * \author Ricky Zheng
45 */
46
47 #include "uffs_config.h"
48 #include "uffs/uffs_os.h"
49 #include "uffs/uffs_public.h"
50 //#include <time.h>
51
52 #define PFX "os : "
53
uffs_SemCreate(OSSEM * sem)54 int uffs_SemCreate(OSSEM *sem)
55 {
56 static int count = 0;
57 char name [RT_NAME_MAX+1];
58 struct rt_mutex *mutex = RT_NULL;
59
60 rt_snprintf(name, sizeof(name), "usem%d", count++);
61 mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
62 if (mutex != RT_NULL)
63 {
64 *sem = (OSSEM *)mutex;
65 return 0;
66 }
67 uffs_Perror(UFFS_MSG_SERIOUS, "can't get a semphore");
68 return -1;
69 }
70
uffs_SemWait(OSSEM sem)71 int uffs_SemWait(OSSEM sem)
72 {
73 return rt_mutex_take((struct rt_mutex *)sem, RT_WAITING_FOREVER);
74 }
75
uffs_SemSignal(OSSEM sem)76 int uffs_SemSignal(OSSEM sem)
77 {
78 return rt_mutex_release((struct rt_mutex *)sem);
79 }
80
uffs_SemDelete(OSSEM * sem)81 int uffs_SemDelete(OSSEM *sem)
82 {
83 int ret = -1;
84
85 if (sem) {
86 ret = rt_mutex_delete((struct rt_mutex *)(*sem));
87 if (ret == RT_EOK) {
88 *sem = 0;
89 }
90 }
91 return ret;
92 }
93
uffs_OSGetTaskId(void)94 int uffs_OSGetTaskId(void)
95 {
96 //TODO: ... return current task ID ...
97 return 0;
98 }
99
uffs_GetCurDateTime(void)100 unsigned int uffs_GetCurDateTime(void)
101 {
102 // FIXME: return system time, please modify this for your platform !
103 // or just return 0 if you don't care about file time.
104 #if 0
105 time_t tvalue;
106
107 tvalue = time(NULL);
108
109 return (unsigned int)tvalue;
110 #endif
111 return 0;
112 }
113
114 #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
sys_malloc(struct uffs_DeviceSt * dev,unsigned int size)115 static void * sys_malloc(struct uffs_DeviceSt *dev, unsigned int size)
116 {
117 dev = dev;
118 uffs_Perror(UFFS_MSG_NORMAL, "system memory alloc %d bytes", size);
119 return rt_malloc(size);
120 }
121
sys_free(struct uffs_DeviceSt * dev,void * p)122 static URET sys_free(struct uffs_DeviceSt *dev, void *p)
123 {
124 dev = dev;
125 rt_free(p);
126 return U_SUCC;
127 }
128
uffs_MemSetupSystemAllocator(uffs_MemAllocator * allocator)129 void uffs_MemSetupSystemAllocator(uffs_MemAllocator *allocator)
130 {
131 allocator->malloc = sys_malloc;
132 allocator->free = sys_free;
133 }
134 #endif
135
136 #if !defined(RT_THREAD)
137 /* debug message output throught 'printf' */
138 static void output_dbg_msg(const char *msg);
139 static struct uffs_DebugMsgOutputSt m_dbg_ops = {
140 output_dbg_msg,
141 NULL,
142 };
143
output_dbg_msg(const char * msg)144 static void output_dbg_msg(const char *msg)
145 {
146 rt_kprintf("%s", msg);
147 }
148
uffs_SetupDebugOutput(void)149 void uffs_SetupDebugOutput(void)
150 {
151 uffs_InitDebugMessageOutput(&m_dbg_ops, UFFS_MSG_NOISY);
152 }
153 #else
154
uffs_SetupDebugOutput(void)155 void uffs_SetupDebugOutput(void)
156 {
157 }
158 #endif
159