uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fs.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #include "lwip/opt.h"
33 #include "lwip/def.h"
34 #include "fs.h"
35 #include <string.h>
36 #include <hash-djb2.h>
37 #include <romfs.h>
38 
39 /*-----------------------------------------------------------------------------------*/
40 /* Define the number of open files that we can support. */
41 #ifndef LWIP_MAX_OPEN_FILES
42 #define LWIP_MAX_OPEN_FILES 10
43 #endif
44 
45 static const u8_t * webfs_romfs = NULL;
46 
47 /* Define the file system memory allocation structure. */
48 struct webfs_table {
49  struct webfs_file file;
51 };
52 
53 /* Allocate file system memory */
55 
56 int webfs_open_custom(struct webfs_file *file, const char *name) {
57  uint32_t len, h;
58  const uint8_t * buf;
59  if (*name == '/')
60  name++;
61  h = hash_djb2((const uint8_t *) name, -1);
62  buf = romfs_get_file_by_hash(webfs_romfs, h, &len);
63  if (!buf)
64  return 0;
65 
66  file->data = (const char *) buf;
67  file->len = len;
68  file->index = len;
69  file->pextension = NULL;
70  file->http_header_included = 0;
71 
72  return 1;
73 }
74 
76 
77 /*-----------------------------------------------------------------------------------*/
78 static struct webfs_file *
79 webfs_malloc(void)
80 {
81  int i;
82  for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
83  if(webfs_memory[i].inuse == 0) {
84  webfs_memory[i].inuse = 1;
85  return(&webfs_memory[i].file);
86  }
87  }
88  return(NULL);
89 }
90 
91 /*-----------------------------------------------------------------------------------*/
92 static void
93 webfs_free(struct webfs_file *file)
94 {
95  int i;
96  for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
97  if(&webfs_memory[i].file == file) {
98  webfs_memory[i].inuse = 0;
99  break;
100  }
101  }
102  return;
103 }
104 
105 /*-----------------------------------------------------------------------------------*/
106 struct webfs_file *
107 webfs_open(const char *name)
108 {
109  struct webfs_file *file;
110 
111  file = webfs_malloc();
112  if(file == NULL) {
113  return NULL;
114  }
115 
116  if(webfs_open_custom(file, name)) {
117  return file;
118  }
119 
120  webfs_free(file);
121  return NULL;
122 }
123 
124 /*-----------------------------------------------------------------------------------*/
125 void
126 webfs_close(struct webfs_file *file)
127 {
128  webfs_free(file);
129 }
130 /*-----------------------------------------------------------------------------------*/
131 int
132 webfs_read(struct webfs_file *file, char *buffer, int count)
133 {
134  int read;
135 
136  if(file->index == file->len) {
137  return -1;
138  }
139 
140  read = file->len - file->index;
141  if(read > count) {
142  read = count;
143  }
144 
145  MEMCPY(buffer, (file->data + file->index), read);
146  file->index += read;
147 
148  return(read);
149 }
150 /*-----------------------------------------------------------------------------------*/
151 int webfs_bytes_left(struct webfs_file *file)
152 {
153  return file->len - file->index;
154 }
155 /*-----------------------------------------------------------------------------------*/
156 void webfs_init(const u8_t *romfs)
157 {
158  webfs_romfs = romfs;
159 }