A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Committer:
dflet
Date:
Sat Sep 14 17:38:41 2013 +0000
Revision:
0:6ad60d78b315
Mostly working will serve the default pages index.html and config.html, but config.html is not woring at present.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:6ad60d78b315 1 /*****************************************************************************
dflet 0:6ad60d78b315 2 *
dflet 0:6ad60d78b315 3 * FlashDB.c
dflet 0:6ad60d78b315 4 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:6ad60d78b315 5 *
dflet 0:6ad60d78b315 6 * Redistribution and use in source and binary forms, with or without
dflet 0:6ad60d78b315 7 * modification, are permitted provided that the following conditions
dflet 0:6ad60d78b315 8 * are met:
dflet 0:6ad60d78b315 9 *
dflet 0:6ad60d78b315 10 * Redistributions of source code must retain the above copyright
dflet 0:6ad60d78b315 11 * notice, this list of conditions and the following disclaimer.
dflet 0:6ad60d78b315 12 *
dflet 0:6ad60d78b315 13 * Redistributions in binary form must reproduce the above copyright
dflet 0:6ad60d78b315 14 * notice, this list of conditions and the following disclaimer in the
dflet 0:6ad60d78b315 15 * documentation and/or other materials provided with the
dflet 0:6ad60d78b315 16 * distribution.
dflet 0:6ad60d78b315 17 *
dflet 0:6ad60d78b315 18 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:6ad60d78b315 19 * its contributors may be used to endorse or promote products derived
dflet 0:6ad60d78b315 20 * from this software without specific prior written permission.
dflet 0:6ad60d78b315 21 *
dflet 0:6ad60d78b315 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:6ad60d78b315 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:6ad60d78b315 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:6ad60d78b315 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:6ad60d78b315 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:6ad60d78b315 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:6ad60d78b315 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:6ad60d78b315 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:6ad60d78b315 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:6ad60d78b315 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:6ad60d78b315 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:6ad60d78b315 33 *
dflet 0:6ad60d78b315 34 *****************************************************************************/
dflet 0:6ad60d78b315 35 #include "mbed.h"
dflet 0:6ad60d78b315 36 #include "Common.h"
dflet 0:6ad60d78b315 37 #include "FlashDB.h"
dflet 0:6ad60d78b315 38 #include "HttpString.h"
dflet 0:6ad60d78b315 39 #include "string.h"
dflet 0:6ad60d78b315 40
dflet 0:6ad60d78b315 41
dflet 0:6ad60d78b315 42 /// This database flag indicates that all resource strings are to be matched with a case-sensitive string comparison function
dflet 0:6ad60d78b315 43 #define FLASH_DB_FLAG_CASE_SENSITIVE (1 << 0)
dflet 0:6ad60d78b315 44
dflet 0:6ad60d78b315 45 /**
dflet 0:6ad60d78b315 46 * The header structure of the flash database
dflet 0:6ad60d78b315 47 */
dflet 0:6ad60d78b315 48 //#ifdef __CCS__
dflet 0:6ad60d78b315 49 struct __attribute__ ((__packed__)) FlashDBHeader
dflet 0:6ad60d78b315 50 //#elif __IAR_SYSTEMS_ICC__
dflet 0:6ad60d78b315 51 //#pragma pack(1)
dflet 0:6ad60d78b315 52 //struct FlashDBHeader
dflet 0:6ad60d78b315 53 //#endif
dflet 0:6ad60d78b315 54 {
dflet 0:6ad60d78b315 55 /// Amount of content type entries
dflet 0:6ad60d78b315 56 uint16 uContentTypeAmount;
dflet 0:6ad60d78b315 57 /// Offset of content-type entry list
dflet 0:6ad60d78b315 58 uint16 uContentTypeOffset;
dflet 0:6ad60d78b315 59 /// Amount of content entries
dflet 0:6ad60d78b315 60 uint16 uContentAmount;
dflet 0:6ad60d78b315 61 /// Offset of content entry list
dflet 0:6ad60d78b315 62 uint16 uContentOffset;
dflet 0:6ad60d78b315 63 /// Database flags. See FLASH_DB_FLAG_*
dflet 0:6ad60d78b315 64 uint16 uFlags;
dflet 0:6ad60d78b315 65 };
dflet 0:6ad60d78b315 66
dflet 0:6ad60d78b315 67 /**
dflet 0:6ad60d78b315 68 * The header structure for a content entity in the database
dflet 0:6ad60d78b315 69 */
dflet 0:6ad60d78b315 70 //#ifdef __CCS__
dflet 0:6ad60d78b315 71 struct __attribute__ ((__packed__)) FlashDBContentTypeEntry
dflet 0:6ad60d78b315 72 //#elif __IAR_SYSTEMS_ICC__
dflet 0:6ad60d78b315 73 //#pragma pack(1)
dflet 0:6ad60d78b315 74 //struct FlashDBContentTypeEntry
dflet 0:6ad60d78b315 75 //#endif
dflet 0:6ad60d78b315 76 {
dflet 0:6ad60d78b315 77 /// Length of the value string. Should be 255 characters at most.
dflet 0:6ad60d78b315 78 uint16 uValueLength;
dflet 0:6ad60d78b315 79 /// Offset of the value string
dflet 0:6ad60d78b315 80 uint16 uValueOffset;
dflet 0:6ad60d78b315 81 };
dflet 0:6ad60d78b315 82
dflet 0:6ad60d78b315 83 extern const char FlashDBContentData[];
dflet 0:6ad60d78b315 84
dflet 0:6ad60d78b315 85 /**
dflet 0:6ad60d78b315 86 * The header structure for a content entity in the database
dflet 0:6ad60d78b315 87 */
dflet 0:6ad60d78b315 88 //#ifdef __CCS__
dflet 0:6ad60d78b315 89 struct __attribute__ ((__packed__)) FlashDBContentEntry
dflet 0:6ad60d78b315 90 //#elif __IAR_SYSTEMS_ICC__
dflet 0:6ad60d78b315 91 //#pragma pack(1)
dflet 0:6ad60d78b315 92 //struct FlashDBContentEntry
dflet 0:6ad60d78b315 93 //#endif
dflet 0:6ad60d78b315 94 {
dflet 0:6ad60d78b315 95 /// Length of the resource string. Should be 255 characters at most.
dflet 0:6ad60d78b315 96 uint16 uResourceLength;
dflet 0:6ad60d78b315 97 /// Offset of the resource string
dflet 0:6ad60d78b315 98 //uint32 uResourceOffset;
dflet 0:6ad60d78b315 99 unsigned long uResourceOffset;
dflet 0:6ad60d78b315 100 /// Flags. See FLASHDB_FLAG_*
dflet 0:6ad60d78b315 101 uint16 uFlags;
dflet 0:6ad60d78b315 102 /// Index of content type entry for this resource, in the content type list
dflet 0:6ad60d78b315 103 uint16 uContentType;
dflet 0:6ad60d78b315 104 /// Length (in bytes) of the content blob
dflet 0:6ad60d78b315 105 uint16 uContentLength;
dflet 0:6ad60d78b315 106 /// Offset of the content blob in the database
dflet 0:6ad60d78b315 107 //uint32 uContentOffset;
dflet 0:6ad60d78b315 108 unsigned long uContentOffset;
dflet 0:6ad60d78b315 109 };
dflet 0:6ad60d78b315 110
dflet 0:6ad60d78b315 111 extern const char FlashDBContentData[];
dflet 0:6ad60d78b315 112
dflet 0:6ad60d78b315 113 /**
dflet 0:6ad60d78b315 114 * The layout of the database file is as follows:
dflet 0:6ad60d78b315 115 * Database Header
dflet 0:6ad60d78b315 116 * Content type entry 1
dflet 0:6ad60d78b315 117 * Content type entry 2
dflet 0:6ad60d78b315 118 * and so on...
dflet 0:6ad60d78b315 119 * Content entry 1
dflet 0:6ad60d78b315 120 * Content entry 2
dflet 0:6ad60d78b315 121 * and so on...
dflet 0:6ad60d78b315 122 * Content type string 1
dflet 0:6ad60d78b315 123 * Content type string 2
dflet 0:6ad60d78b315 124 * and so on...
dflet 0:6ad60d78b315 125 * Content blob 1
dflet 0:6ad60d78b315 126 * Content blob 2
dflet 0:6ad60d78b315 127 * and so on...
dflet 0:6ad60d78b315 128 *
dflet 0:6ad60d78b315 129 * Note: All offsets in the structures are absolute offsets, meaning they are relative to the database base pointer, as passed to FlashDB_Init()
dflet 0:6ad60d78b315 130 */
dflet 0:6ad60d78b315 131
dflet 0:6ad60d78b315 132 /* this global shows a sample how static pages/files are stored in the memory */
dflet 0:6ad60d78b315 133
dflet 0:6ad60d78b315 134 #if 0
dflet 0:6ad60d78b315 135 struct StaticExamplePages
dflet 0:6ad60d78b315 136 {
dflet 0:6ad60d78b315 137 struct FlashDBHeader header;
dflet 0:6ad60d78b315 138 struct FlashDBContentTypeEntry contentTypeList[];
dflet 0:6ad60d78b315 139 struct FlashDBContentEntry ContentList[];
dflet 0:6ad60d78b315 140 char contentType[][];
dflet 0:6ad60d78b315 141 char resource[][];
dflet 0:6ad60d78b315 142 char content[][];
dflet 0:6ad60d78b315 143 };
dflet 0:6ad60d78b315 144 #endif
dflet 0:6ad60d78b315 145
dflet 0:6ad60d78b315 146 struct FlashDBHeader *gFlashDB;
dflet 0:6ad60d78b315 147
dflet 0:6ad60d78b315 148 /**
dflet 0:6ad60d78b315 149 * Initialize the flash database module
dflet 0:6ad60d78b315 150 * @param pDatabase Pointer to the beginning of the database in flash memory
dflet 0:6ad60d78b315 151 */
dflet 0:6ad60d78b315 152 void FlashDB_Init()
dflet 0:6ad60d78b315 153 {
dflet 0:6ad60d78b315 154 gFlashDB = (struct FlashDBHeader *)FlashDBContentData;
dflet 0:6ad60d78b315 155 }
dflet 0:6ad60d78b315 156
dflet 0:6ad60d78b315 157 /**
dflet 0:6ad60d78b315 158 * Find a content-type in the database
dflet 0:6ad60d78b315 159 * @param index The index of the content type in the database
dflet 0:6ad60d78b315 160 * @param[out] pResult Returns the string and its length
dflet 0:6ad60d78b315 161 * @return nonzero if successful. zero if index out of range.
dflet 0:6ad60d78b315 162 */
dflet 0:6ad60d78b315 163 int FlashDB_FindContentType(uint16 index, struct HttpBlob* pResult)
dflet 0:6ad60d78b315 164 {
dflet 0:6ad60d78b315 165 struct FlashDBContentTypeEntry *pContentTypeEntryList;
dflet 0:6ad60d78b315 166
dflet 0:6ad60d78b315 167 if (index > gFlashDB->uContentTypeAmount)
dflet 0:6ad60d78b315 168 return 0;
dflet 0:6ad60d78b315 169
dflet 0:6ad60d78b315 170 pContentTypeEntryList = (struct FlashDBContentTypeEntry *)((unsigned char*)gFlashDB + gFlashDB->uContentTypeOffset);
dflet 0:6ad60d78b315 171 pContentTypeEntryList = (struct FlashDBContentTypeEntry *)(pContentTypeEntryList + index);
dflet 0:6ad60d78b315 172 pResult->uLength = pContentTypeEntryList->uValueLength;
dflet 0:6ad60d78b315 173 pResult->pData = (unsigned char*)((unsigned char*)gFlashDB + pContentTypeEntryList->uValueOffset);
dflet 0:6ad60d78b315 174
dflet 0:6ad60d78b315 175 return 1;
dflet 0:6ad60d78b315 176 }
dflet 0:6ad60d78b315 177
dflet 0:6ad60d78b315 178 /**
dflet 0:6ad60d78b315 179 * Lookup content in the database, given a resource identifier
dflet 0:6ad60d78b315 180 * @param pResource The resource as given in the HTTP request
dflet 0:6ad60d78b315 181 * @param[out] pResult Returns the content and its metadata
dflet 0:6ad60d78b315 182 * @return nonzero if successful. zero if such a resource was not found.
dflet 0:6ad60d78b315 183 */
dflet 0:6ad60d78b315 184 int FlashDB_FindContent(struct HttpBlob pResource, struct FlashDBContent* pResult)
dflet 0:6ad60d78b315 185 {
dflet 0:6ad60d78b315 186 /* it is assumed that the resource list is sorted */
dflet 0:6ad60d78b315 187 struct FlashDBContentEntry *pContentFieldStart;
dflet 0:6ad60d78b315 188 struct FlashDBContentEntry *pContentFieldMoving;
dflet 0:6ad60d78b315 189 struct HttpBlob MainPage;
dflet 0:6ad60d78b315 190 uint16 uLowSearchIndex = 0;
dflet 0:6ad60d78b315 191 uint16 uMidSearchIndex;
dflet 0:6ad60d78b315 192 uint16 uHighSearchIndex;
dflet 0:6ad60d78b315 193 int uComparison;
dflet 0:6ad60d78b315 194 int16 found = -1; /* not found */
dflet 0:6ad60d78b315 195 struct HttpBlob ResourceBlob;
dflet 0:6ad60d78b315 196
dflet 0:6ad60d78b315 197 uHighSearchIndex = gFlashDB->uContentAmount - 1;
dflet 0:6ad60d78b315 198 pContentFieldStart = (struct FlashDBContentEntry *)((unsigned char*)gFlashDB + gFlashDB->uContentOffset);
dflet 0:6ad60d78b315 199
dflet 0:6ad60d78b315 200 while (uLowSearchIndex <= uHighSearchIndex)
dflet 0:6ad60d78b315 201 {
dflet 0:6ad60d78b315 202 uMidSearchIndex = (uLowSearchIndex + uHighSearchIndex)/2;
dflet 0:6ad60d78b315 203 pContentFieldMoving = pContentFieldStart + uMidSearchIndex;
dflet 0:6ad60d78b315 204 ResourceBlob.pData = (unsigned char*)((unsigned char*)gFlashDB + pContentFieldMoving->uResourceOffset);
dflet 0:6ad60d78b315 205 ResourceBlob.uLength = pContentFieldMoving->uResourceLength;
dflet 0:6ad60d78b315 206
dflet 0:6ad60d78b315 207 if(pResource.uLength ==1 && *(pResource.pData)=='/')
dflet 0:6ad60d78b315 208 {
dflet 0:6ad60d78b315 209 MainPage.uLength = 11;
dflet 0:6ad60d78b315 210 MainPage.pData = (unsigned char*)"/index.html";
dflet 0:6ad60d78b315 211 uComparison = HttpString_strcmp(MainPage, ResourceBlob);
dflet 0:6ad60d78b315 212 }
dflet 0:6ad60d78b315 213 else
dflet 0:6ad60d78b315 214 uComparison = HttpString_strcmp(pResource, ResourceBlob);
dflet 0:6ad60d78b315 215 // found the exact blob
dflet 0:6ad60d78b315 216 if (uComparison == 0)
dflet 0:6ad60d78b315 217 {
dflet 0:6ad60d78b315 218 found = 1;
dflet 0:6ad60d78b315 219 printf("Content found \r\n");
dflet 0:6ad60d78b315 220 break;
dflet 0:6ad60d78b315 221 }
dflet 0:6ad60d78b315 222
dflet 0:6ad60d78b315 223 // the requested blob is "larger" than the current blob
dflet 0:6ad60d78b315 224 else if (uComparison > 0)
dflet 0:6ad60d78b315 225 {
dflet 0:6ad60d78b315 226 uLowSearchIndex = uMidSearchIndex + 1;
dflet 0:6ad60d78b315 227
dflet 0:6ad60d78b315 228 }
dflet 0:6ad60d78b315 229 // the requested blob is "smaller" than the current blob
dflet 0:6ad60d78b315 230 else
dflet 0:6ad60d78b315 231 {
dflet 0:6ad60d78b315 232 uHighSearchIndex = uMidSearchIndex - 1;
dflet 0:6ad60d78b315 233
dflet 0:6ad60d78b315 234 }
dflet 0:6ad60d78b315 235 }
dflet 0:6ad60d78b315 236
dflet 0:6ad60d78b315 237 if (found == -1)
dflet 0:6ad60d78b315 238 {
dflet 0:6ad60d78b315 239 printf("Content not found \r\n");
dflet 0:6ad60d78b315 240 return 0;
dflet 0:6ad60d78b315 241 }
dflet 0:6ad60d78b315 242 else
dflet 0:6ad60d78b315 243 {
dflet 0:6ad60d78b315 244 pResult->uContentType = pContentFieldMoving->uContentType;
dflet 0:6ad60d78b315 245 pResult->uFlags = pContentFieldMoving->uFlags;
dflet 0:6ad60d78b315 246 pResult->contentBlob.uLength = pContentFieldMoving->uContentLength;
dflet 0:6ad60d78b315 247 pResult->contentBlob.pData = (unsigned char*)((unsigned char*)gFlashDB + pContentFieldMoving->uContentOffset);
dflet 0:6ad60d78b315 248 return 1;
dflet 0:6ad60d78b315 249 }
dflet 0:6ad60d78b315 250 }
dflet 0:6ad60d78b315 251