A Port of TI's Webserver for the CC3000
Embed:
(wiki syntax)
Show/hide line numbers
FlashDB.cpp
00001 /***************************************************************************** 00002 * 00003 * FlashDB.c 00004 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * Neither the name of Texas Instruments Incorporated nor the names of 00019 * its contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00025 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00026 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 *****************************************************************************/ 00035 #include "mbed.h" 00036 #include "Common.h" 00037 #include "FlashDB.h" 00038 #include "HttpString.h" 00039 #include "string.h" 00040 00041 00042 /// This database flag indicates that all resource strings are to be matched with a case-sensitive string comparison function 00043 #define FLASH_DB_FLAG_CASE_SENSITIVE (1 << 0) 00044 00045 /** 00046 * The header structure of the flash database 00047 */ 00048 //#ifdef __CCS__ 00049 struct __attribute__ ((__packed__)) FlashDBHeader 00050 //#elif __IAR_SYSTEMS_ICC__ 00051 //#pragma pack(1) 00052 //struct FlashDBHeader 00053 //#endif 00054 { 00055 /// Amount of content type entries 00056 uint16 uContentTypeAmount; 00057 /// Offset of content-type entry list 00058 uint16 uContentTypeOffset; 00059 /// Amount of content entries 00060 uint16 uContentAmount; 00061 /// Offset of content entry list 00062 uint16 uContentOffset; 00063 /// Database flags. See FLASH_DB_FLAG_* 00064 uint16 uFlags; 00065 }; 00066 00067 /** 00068 * The header structure for a content entity in the database 00069 */ 00070 //#ifdef __CCS__ 00071 struct __attribute__ ((__packed__)) FlashDBContentTypeEntry 00072 //#elif __IAR_SYSTEMS_ICC__ 00073 //#pragma pack(1) 00074 //struct FlashDBContentTypeEntry 00075 //#endif 00076 { 00077 /// Length of the value string. Should be 255 characters at most. 00078 uint16 uValueLength; 00079 /// Offset of the value string 00080 uint16 uValueOffset; 00081 }; 00082 00083 extern const char FlashDBContentData[]; 00084 00085 /** 00086 * The header structure for a content entity in the database 00087 */ 00088 //#ifdef __CCS__ 00089 struct __attribute__ ((__packed__)) FlashDBContentEntry 00090 //#elif __IAR_SYSTEMS_ICC__ 00091 //#pragma pack(1) 00092 //struct FlashDBContentEntry 00093 //#endif 00094 { 00095 /// Length of the resource string. Should be 255 characters at most. 00096 uint16 uResourceLength; 00097 /// Offset of the resource string 00098 //uint32 uResourceOffset; 00099 unsigned long uResourceOffset; 00100 /// Flags. See FLASHDB_FLAG_* 00101 uint16 uFlags; 00102 /// Index of content type entry for this resource, in the content type list 00103 uint16 uContentType; 00104 /// Length (in bytes) of the content blob 00105 uint16 uContentLength; 00106 /// Offset of the content blob in the database 00107 //uint32 uContentOffset; 00108 unsigned long uContentOffset; 00109 }; 00110 00111 extern const char FlashDBContentData[]; 00112 00113 /** 00114 * The layout of the database file is as follows: 00115 * Database Header 00116 * Content type entry 1 00117 * Content type entry 2 00118 * and so on... 00119 * Content entry 1 00120 * Content entry 2 00121 * and so on... 00122 * Content type string 1 00123 * Content type string 2 00124 * and so on... 00125 * Content blob 1 00126 * Content blob 2 00127 * and so on... 00128 * 00129 * Note: All offsets in the structures are absolute offsets, meaning they are relative to the database base pointer, as passed to FlashDB_Init() 00130 */ 00131 00132 /* this global shows a sample how static pages/files are stored in the memory */ 00133 00134 #if 0 00135 struct StaticExamplePages 00136 { 00137 struct FlashDBHeader header; 00138 struct FlashDBContentTypeEntry contentTypeList[]; 00139 struct FlashDBContentEntry ContentList[]; 00140 char contentType[][]; 00141 char resource[][]; 00142 char content[][]; 00143 }; 00144 #endif 00145 00146 struct FlashDBHeader *gFlashDB; 00147 00148 /** 00149 * Initialize the flash database module 00150 * @param pDatabase Pointer to the beginning of the database in flash memory 00151 */ 00152 void FlashDB_Init() 00153 { 00154 gFlashDB = (struct FlashDBHeader *)FlashDBContentData; 00155 } 00156 00157 /** 00158 * Find a content-type in the database 00159 * @param index The index of the content type in the database 00160 * @param[out] pResult Returns the string and its length 00161 * @return nonzero if successful. zero if index out of range. 00162 */ 00163 int FlashDB_FindContentType(uint16 index, struct HttpBlob* pResult) 00164 { 00165 struct FlashDBContentTypeEntry *pContentTypeEntryList; 00166 00167 if (index > gFlashDB->uContentTypeAmount) 00168 return 0; 00169 00170 pContentTypeEntryList = (struct FlashDBContentTypeEntry *)((unsigned char*)gFlashDB + gFlashDB->uContentTypeOffset); 00171 pContentTypeEntryList = (struct FlashDBContentTypeEntry *)(pContentTypeEntryList + index); 00172 pResult->uLength = pContentTypeEntryList->uValueLength; 00173 pResult->pData = (unsigned char*)((unsigned char*)gFlashDB + pContentTypeEntryList->uValueOffset); 00174 00175 return 1; 00176 } 00177 00178 /** 00179 * Lookup content in the database, given a resource identifier 00180 * @param pResource The resource as given in the HTTP request 00181 * @param[out] pResult Returns the content and its metadata 00182 * @return nonzero if successful. zero if such a resource was not found. 00183 */ 00184 int FlashDB_FindContent(struct HttpBlob pResource, struct FlashDBContent* pResult) 00185 { 00186 /* it is assumed that the resource list is sorted */ 00187 struct FlashDBContentEntry *pContentFieldStart; 00188 struct FlashDBContentEntry *pContentFieldMoving; 00189 struct HttpBlob MainPage; 00190 uint16 uLowSearchIndex = 0; 00191 uint16 uMidSearchIndex; 00192 uint16 uHighSearchIndex; 00193 int uComparison; 00194 int16 found = -1; /* not found */ 00195 struct HttpBlob ResourceBlob; 00196 00197 uHighSearchIndex = gFlashDB->uContentAmount - 1; 00198 pContentFieldStart = (struct FlashDBContentEntry *)((unsigned char*)gFlashDB + gFlashDB->uContentOffset); 00199 00200 while (uLowSearchIndex <= uHighSearchIndex) 00201 { 00202 uMidSearchIndex = (uLowSearchIndex + uHighSearchIndex)/2; 00203 pContentFieldMoving = pContentFieldStart + uMidSearchIndex; 00204 ResourceBlob.pData = (unsigned char*)((unsigned char*)gFlashDB + pContentFieldMoving->uResourceOffset); 00205 ResourceBlob.uLength = pContentFieldMoving->uResourceLength; 00206 00207 if(pResource.uLength ==1 && *(pResource.pData)=='/') 00208 { 00209 MainPage.uLength = 11; 00210 MainPage.pData = (unsigned char*)"/index.html"; 00211 uComparison = HttpString_strcmp(MainPage, ResourceBlob); 00212 } 00213 else 00214 uComparison = HttpString_strcmp(pResource, ResourceBlob); 00215 // found the exact blob 00216 if (uComparison == 0) 00217 { 00218 found = 1; 00219 printf("Content found \r\n"); 00220 break; 00221 } 00222 00223 // the requested blob is "larger" than the current blob 00224 else if (uComparison > 0) 00225 { 00226 uLowSearchIndex = uMidSearchIndex + 1; 00227 00228 } 00229 // the requested blob is "smaller" than the current blob 00230 else 00231 { 00232 uHighSearchIndex = uMidSearchIndex - 1; 00233 00234 } 00235 } 00236 00237 if (found == -1) 00238 { 00239 printf("Content not found \r\n"); 00240 return 0; 00241 } 00242 else 00243 { 00244 pResult->uContentType = pContentFieldMoving->uContentType; 00245 pResult->uFlags = pContentFieldMoving->uFlags; 00246 pResult->contentBlob.uLength = pContentFieldMoving->uContentLength; 00247 pResult->contentBlob.pData = (unsigned char*)((unsigned char*)gFlashDB + pContentFieldMoving->uContentOffset); 00248 return 1; 00249 } 00250 } 00251
Generated on Wed Jul 13 2022 13:30:51 by
1.7.2