1 /**
2  * Copyright (2018) Bestech Inc. All rights reserved.
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 #ifndef MEM_FILE_H
18 #define MEM_FILE_H
19 
20 #include <stdio.h>
21 #include "mem_list.h"
22 #include "cmsis_os.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
29 
30 /*
31    mem file path: /mem://xxxxxx.xxx
32 
33 */
34 
35 typedef struct {
36     osSemaphoreId _osSemaphoreId;
37     osSemaphoreDef_t _osSemaphoreDef;
38 #if defined(CMSIS_OS_RTX) && (osCMSIS < 0x20000U)
39     uint32_t _semaphore_data[2];
40 #endif
41 } rw_sem_t;
42 
43 typedef struct{
44     char *buff;
45     rw_sem_t* rw_lock;
46     int  length;
47     int  r_offset;
48     int  w_offset;
49     int  users;
50 }mem_file_cb_t;
51 
52 typedef struct{
53     char *name;
54     char *addr;
55     int  length;
56 }mem_file_t;
57 
58 typedef struct{
59     bool (*is_valid_mem_file)(const char *path);
60     bool(* is_valid_mem_file_fd)(mem_file_cb_t *stream);
61     mem_file_cb_t* (*fopen)(const char *path, char *rw_mode);
62     int (*fseek)(mem_file_cb_t *stream, long offset, int whence);
63     long (*ftell)(mem_file_cb_t *stream);
64     void (*rewind)(mem_file_cb_t *stream);
65     int  (*fgetpos)(mem_file_cb_t *stream, fpos_t *pos);
66     int (*fsetpos)(mem_file_cb_t *stream, const fpos_t *pos);
67     size_t (*fread)(void *ptr, size_t size, size_t nmemb, mem_file_cb_t *stream);
68     size_t (*fwrite)(const void *ptr, size_t size, size_t nmemb,mem_file_cb_t *stream);
69     int (*fclose)(mem_file_cb_t *stream);
70     int (*flen)(mem_file_cb_t *stream);
71     int (*fregister)(const char *path,  char *addr, int size);
72 
73 }mem_file_ops_t;
74 
75  mem_file_ops_t* get_mem_file_system(void);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 
82 #endif // MEM_FILE_H
83