-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_file.c
338 lines (287 loc) · 7.39 KB
/
app_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2020 Rockchip Electronic Co.,Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "app_main.h"
#define DBG_SECTION_NAME "FILE"
#define DBG_LEVEL DBG_INFO
#include "rtdbg.h"
#define APP_INFO_FILE USERDATA_PATH"sys.inf"
struct file_info
{
char target[MAX_FILE_NAME];
size_t total_file;
size_t cur_file;
};
static struct file_info g_info = {{0}, 0, 0};
void get_app_info(struct app_main_data_t *info)
{
struct app_main_data_t app_info;
FILE *fd;
info->clock_style = 1;
info->play_mode = APP_PLAY_LIST;
info->bl_time = APP_TIMING_LIGHTOFF;
info->bl = 50;
info->play_vol = APP_PLAY_VOL_DEFAULT;
fd = fopen(APP_INFO_FILE, "rb");
if (!fd)
{
save_app_info(info);
return;
}
fread((char *)&app_info, 1, sizeof(struct app_main_data_t), fd);
if (app_info.clock_style >= 0 && app_info.clock_style < 4)
info->clock_style = app_info.clock_style;
if (app_info.play_mode >= APP_PLAY_LIST && app_info.play_mode <= APP_PLAY_RANDOM)
info->play_mode = app_info.play_mode;
if (app_info.bl_time >= APP_TIMING_LIGHTOFF_MIN)
info->bl_time = app_info.bl_time;
else
info->bl_time = 0;
if (app_info.bl >= 20 && app_info.bl <= 100)
info->bl = app_info.bl;
if (app_info.play_vol >= APP_PLAY_VOL_MIN && app_info.play_vol <= APP_PLAY_VOL_MAX)
info->play_vol = app_info.play_vol;
fclose(fd);
}
void save_app_info(struct app_main_data_t *info)
{
FILE *fd;
fd = fopen(APP_INFO_FILE, "wb+");
if (!fd)
return;
fwrite((char *)info, 1, sizeof(struct app_main_data_t), fd);
fclose(fd);
}
int rootfs_check(void)
{
rt_device_t root_dev;
int format = 0;
root_dev = rt_device_find("root");
RT_ASSERT(root_dev);
if (dfs_filesystem_get_mounted_path(root_dev) == NULL)
{
LOG_W("root is not mounted");
dfs_mkfs("elm", "root");
if (dfs_mount("root", "/", "elm", 0, 0) < 0)
{
LOG_E("mount root failed");
format |= ROOT_NO_MOUNTED;
}
}
#ifdef RT_SDCARD_MOUNT_POINT
rt_device_t sd_dev;
int retry = 0;
if (access(RT_SDCARD_MOUNT_POINT, F_OK) < 0)
{
LOG_I("create sd mount point %s.", RT_SDCARD_MOUNT_POINT);
mkdir(RT_SDCARD_MOUNT_POINT, 0);
}
/* Wait for sd0 be registed */
do
{
sd_dev = rt_device_find("sd0");
rt_thread_mdelay(10);
retry++;
if (retry > 100)
break;
}
while (!sd_dev);
if (sd_dev)
{
if (dfs_filesystem_get_mounted_path(sd_dev) == NULL)
{
if (dfs_mount("sd0", RT_SDCARD_MOUNT_POINT, "elm", 0, 0) < 0)
{
LOG_I("sd0 is not mounted");
format |= SD0_NO_MOUNTED;
}
}
}
#endif
return format;
}
uint32_t check_audio_type(char *file_name)
{
char *str;
str = strstr(file_name, ".");
if (str)
{
if (strcmp(str + 1, "wav") == 0)
return RT_EOK;
if (strcmp(str + 1, "mp3") == 0)
return RT_EOK;
}
return -RT_ERROR;
}
char *get_audio(void)
{
if (g_info.cur_file == 0)
return NULL;
return g_info.target;
}
static char *get_audio_by_index(uint32_t index)
{
DIR *dir = NULL;
struct dirent *dr = NULL;
uint32_t cnt = 0;
dir = opendir(MUSIC_DIR_PATH);
if (NULL == dir)
{
LOG_E("open dir %s fail", MUSIC_DIR_PATH);
return NULL;
}
while (cnt < index)
{
dr = readdir(dir);
if (dr != NULL)
{
if (!strcmp(dr->d_name, ".") || !strcmp(dr->d_name, ".."))
continue;
if (dr->d_type == 1)
{
LOG_D("dr->d_name=%s", dr->d_name);
if (check_audio_type(dr->d_name) == RT_EOK)
cnt++;
}
}
else
{
break;
}
}
closedir(dir);
if (dr)
{
LOG_D("found file [%s]", dr->d_name);
snprintf(g_info.target, sizeof(g_info.target),
"%s/%s%c", MUSIC_DIR_PATH, dr->d_name, '\0');
return g_info.target;
}
return NULL;
}
#if 0
/* remove file and return left file count */
uint32_t remove_file(const char *path, char *file_name)
{
char str[256];
sprintf(str, "%s/%s", path, file_name);
if (unlink(str) == 0)
return scan_audio(path);
return -RT_ERROR;
}
#endif
/* return audio file count in path */
void scan_audio(void)
{
DIR *dir = NULL;
struct dirent *dr = NULL;
uint32_t cnt = 0;
int ret;
ret = access(MUSIC_DIR_PATH, F_OK);
if (ret < 0)
{
mkdir(MUSIC_DIR_PATH, 0);
g_info.total_file = 0;
g_info.cur_file = 0;
return;
}
dir = opendir(MUSIC_DIR_PATH);
if (NULL == dir)
{
LOG_E("open dir %s fail", MUSIC_DIR_PATH);
g_info.total_file = 0;
g_info.cur_file = 0;
return;
}
LOG_D("open dir %s success :%p", MUSIC_DIR_PATH, dir);
while (1)
{
dr = readdir(dir);
if (dr != NULL)
{
if (!strcmp(dr->d_name, ".") || !strcmp(dr->d_name, ".."))
continue;
LOG_D("d_name = %s dtype = %d", dr->d_name, dr->d_type);
if (dr->d_type == 1)
{
LOG_D("dr->d_name=%s", dr->d_name);
if (check_audio_type(dr->d_name) == RT_EOK)
{
cnt++;
if (cnt == 1 && g_info.cur_file == 0)
{
g_info.cur_file = 1;
snprintf(g_info.target, sizeof(g_info.target),
"%s/%s%c", MUSIC_DIR_PATH, dr->d_name, '\0');
}
/*
else if (cnt == g_info.cur_file)
{
snprintf(g_info.target, sizeof(g_info.target),
"%s/%s%c", MUSIC_DIR_PATH, dr->d_name, '\0');
}
*/
}
}
}
if (dr == NULL)
{
closedir(dir);
break;
}
}
LOG_D("PATH [%s]: have %ld files", MUSIC_DIR_PATH, cnt);
g_info.total_file = cnt;
if (g_info.cur_file > g_info.total_file)
{
g_info.cur_file = g_info.total_file;
}
}
char *app_random_file(void)
{
uint32_t index;
char *target;
if (g_info.total_file == 1)
return g_info.target;
do
{
index = rand() % g_info.total_file + 1;
}
while (g_info.cur_file == index);
target = get_audio_by_index(index);
if (target != NULL)
g_info.cur_file = index;
return target;
}
char *app_next_file(void)
{
uint32_t index;
char *target;
if (g_info.total_file == 1)
return g_info.target;
if (g_info.cur_file < g_info.total_file)
index = g_info.cur_file + 1;
else
index = 1;
target = get_audio_by_index(index);
if (target != NULL)
g_info.cur_file = index;
return target;
}
char *app_prev_file(void)
{
uint32_t index;
char *target;
if (g_info.total_file == 1)
return g_info.target;
if (g_info.cur_file > 1)
index = g_info.cur_file - 1;
else
index = g_info.total_file;
target = get_audio_by_index(index);
if (target != NULL)
g_info.cur_file = index;
return target;
}