TI's CC3100 websocket camera demo with Arducam mini ov5642 and freertos. Should work with other M3's. Work in progress test demo.
http/server/HttpString.cpp@1:e448e81c416f, 2015-09-11 (annotated)
- Committer:
- dflet
- Date:
- Fri Sep 11 15:38:33 2015 +0000
- Revision:
- 1:e448e81c416f
- Parent:
- 0:400d8e75a8d0
Removed some debud.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 0:400d8e75a8d0 | 1 | //***************************************************************************** |
dflet | 0:400d8e75a8d0 | 2 | // Copyright (C) 2014 Texas Instruments Incorporated |
dflet | 0:400d8e75a8d0 | 3 | // |
dflet | 0:400d8e75a8d0 | 4 | // All rights reserved. Property of Texas Instruments Incorporated. |
dflet | 0:400d8e75a8d0 | 5 | // Restricted rights to use, duplicate or disclose this code are |
dflet | 0:400d8e75a8d0 | 6 | // granted through contract. |
dflet | 0:400d8e75a8d0 | 7 | // The program may not be used without the written permission of |
dflet | 0:400d8e75a8d0 | 8 | // Texas Instruments Incorporated or against the terms and conditions |
dflet | 0:400d8e75a8d0 | 9 | // stipulated in the agreement under which this program has been supplied, |
dflet | 0:400d8e75a8d0 | 10 | // and under no circumstances can it be used with non-TI connectivity device. |
dflet | 0:400d8e75a8d0 | 11 | // |
dflet | 0:400d8e75a8d0 | 12 | //***************************************************************************** |
dflet | 0:400d8e75a8d0 | 13 | |
dflet | 0:400d8e75a8d0 | 14 | #include "string.h" |
dflet | 0:400d8e75a8d0 | 15 | #include <stdlib.h> |
dflet | 0:400d8e75a8d0 | 16 | #include "HttpString.h" |
dflet | 0:400d8e75a8d0 | 17 | #include "HttpDebug.h" |
dflet | 0:400d8e75a8d0 | 18 | //#include "HttpCore.h" |
dflet | 0:400d8e75a8d0 | 19 | |
dflet | 0:400d8e75a8d0 | 20 | /** |
dflet | 0:400d8e75a8d0 | 21 | * @addtogroup HttpString |
dflet | 0:400d8e75a8d0 | 22 | * @{ |
dflet | 0:400d8e75a8d0 | 23 | */ |
dflet | 0:400d8e75a8d0 | 24 | |
dflet | 0:400d8e75a8d0 | 25 | char digits[] = "0123456789"; |
dflet | 0:400d8e75a8d0 | 26 | char hexDigits[] = "0123456789abcdef"; |
dflet | 0:400d8e75a8d0 | 27 | |
dflet | 0:400d8e75a8d0 | 28 | int HttpString_strcmp(struct HttpBlob first, struct HttpBlob second) |
dflet | 0:400d8e75a8d0 | 29 | { |
dflet | 0:400d8e75a8d0 | 30 | int min,res; |
dflet | 0:400d8e75a8d0 | 31 | if (first.uLength > second.uLength) |
dflet | 0:400d8e75a8d0 | 32 | min = second.uLength; |
dflet | 0:400d8e75a8d0 | 33 | else |
dflet | 0:400d8e75a8d0 | 34 | min = first.uLength; |
dflet | 0:400d8e75a8d0 | 35 | |
dflet | 0:400d8e75a8d0 | 36 | // Compare a common length which might be equal |
dflet | 0:400d8e75a8d0 | 37 | res = memcmp(first.pData, second.pData, min); |
dflet | 0:400d8e75a8d0 | 38 | if (res != 0) |
dflet | 0:400d8e75a8d0 | 39 | return res; |
dflet | 0:400d8e75a8d0 | 40 | |
dflet | 0:400d8e75a8d0 | 41 | // Common length is equal, so the longer blob is "larger" |
dflet | 0:400d8e75a8d0 | 42 | if (first.uLength > second.uLength) |
dflet | 0:400d8e75a8d0 | 43 | return 1; |
dflet | 0:400d8e75a8d0 | 44 | else if (first.uLength < second.uLength) |
dflet | 0:400d8e75a8d0 | 45 | return -1; |
dflet | 0:400d8e75a8d0 | 46 | else |
dflet | 0:400d8e75a8d0 | 47 | return 0; |
dflet | 0:400d8e75a8d0 | 48 | } |
dflet | 0:400d8e75a8d0 | 49 | |
dflet | 0:400d8e75a8d0 | 50 | static void ToLower(char * str, UINT16 len) |
dflet | 0:400d8e75a8d0 | 51 | { |
dflet | 0:400d8e75a8d0 | 52 | int i; |
dflet | 0:400d8e75a8d0 | 53 | for (i=0 ; i<=len; i++) |
dflet | 0:400d8e75a8d0 | 54 | { |
dflet | 0:400d8e75a8d0 | 55 | if (str[i]>='A' && str[i]<='Z') |
dflet | 0:400d8e75a8d0 | 56 | str[i] = str[i] + 32; |
dflet | 0:400d8e75a8d0 | 57 | } |
dflet | 0:400d8e75a8d0 | 58 | } |
dflet | 0:400d8e75a8d0 | 59 | |
dflet | 0:400d8e75a8d0 | 60 | UINT8* HttpString_nextToken(char* pToken, UINT16 uTokenLength, struct HttpBlob blob) |
dflet | 0:400d8e75a8d0 | 61 | { |
dflet | 0:400d8e75a8d0 | 62 | UINT8* pch = blob.pData; |
dflet | 0:400d8e75a8d0 | 63 | struct HttpBlob partialBlob; |
dflet | 0:400d8e75a8d0 | 64 | struct HttpBlob token; |
dflet | 0:400d8e75a8d0 | 65 | token.pData = (UINT8*)pToken; |
dflet | 0:400d8e75a8d0 | 66 | token.uLength = uTokenLength; |
dflet | 0:400d8e75a8d0 | 67 | |
dflet | 0:400d8e75a8d0 | 68 | ToLower((char *)blob.pData, blob.uLength); |
dflet | 0:400d8e75a8d0 | 69 | |
dflet | 0:400d8e75a8d0 | 70 | while (pch < blob.pData + blob.uLength) |
dflet | 0:400d8e75a8d0 | 71 | { |
dflet | 0:400d8e75a8d0 | 72 | // Calculate how many blob bytes we should search |
dflet | 0:400d8e75a8d0 | 73 | int nMaxCount = blob.uLength - (pch - blob.pData) - uTokenLength + 1; |
dflet | 0:400d8e75a8d0 | 74 | if (nMaxCount < 1){ |
dflet | 0:400d8e75a8d0 | 75 | return NULL; |
dflet | 0:400d8e75a8d0 | 76 | } |
dflet | 0:400d8e75a8d0 | 77 | // Search for the first character of the token |
dflet | 0:400d8e75a8d0 | 78 | pch = (UINT8*)memchr(pch, pToken[0], nMaxCount); |
dflet | 0:400d8e75a8d0 | 79 | if (pch==NULL){ |
dflet | 0:400d8e75a8d0 | 80 | return NULL; |
dflet | 0:400d8e75a8d0 | 81 | } |
dflet | 0:400d8e75a8d0 | 82 | // Found first character, now compare the rest |
dflet | 0:400d8e75a8d0 | 83 | partialBlob.pData = pch; |
dflet | 0:400d8e75a8d0 | 84 | partialBlob.uLength = uTokenLength; |
dflet | 0:400d8e75a8d0 | 85 | if (HttpString_strcmp(token, partialBlob)==0) |
dflet | 0:400d8e75a8d0 | 86 | return pch; |
dflet | 0:400d8e75a8d0 | 87 | |
dflet | 0:400d8e75a8d0 | 88 | // Skip this byte, and look for the token in the rest of the blob |
dflet | 0:400d8e75a8d0 | 89 | ++pch; |
dflet | 0:400d8e75a8d0 | 90 | } |
dflet | 0:400d8e75a8d0 | 91 | return NULL; |
dflet | 0:400d8e75a8d0 | 92 | } |
dflet | 0:400d8e75a8d0 | 93 | |
dflet | 0:400d8e75a8d0 | 94 | UINT8* HttpString_nextDelimiter(char* pToken, UINT16 uTokenLength, struct HttpBlob blob) |
dflet | 0:400d8e75a8d0 | 95 | { |
dflet | 0:400d8e75a8d0 | 96 | UINT8* pch = blob.pData; |
dflet | 0:400d8e75a8d0 | 97 | struct HttpBlob partialBlob; |
dflet | 0:400d8e75a8d0 | 98 | struct HttpBlob token; |
dflet | 0:400d8e75a8d0 | 99 | token.pData = (UINT8*)pToken; |
dflet | 0:400d8e75a8d0 | 100 | token.uLength = uTokenLength; |
dflet | 0:400d8e75a8d0 | 101 | |
dflet | 0:400d8e75a8d0 | 102 | //ToLower((char *)blob.pData, blob.uLength); |
dflet | 0:400d8e75a8d0 | 103 | |
dflet | 0:400d8e75a8d0 | 104 | while (pch < blob.pData + blob.uLength) |
dflet | 0:400d8e75a8d0 | 105 | { |
dflet | 0:400d8e75a8d0 | 106 | // Calculate how many blob bytes we should search |
dflet | 0:400d8e75a8d0 | 107 | int nMaxCount = blob.uLength - (pch - blob.pData) - uTokenLength + 1; |
dflet | 0:400d8e75a8d0 | 108 | if (nMaxCount < 1) |
dflet | 0:400d8e75a8d0 | 109 | return NULL; |
dflet | 0:400d8e75a8d0 | 110 | |
dflet | 0:400d8e75a8d0 | 111 | // Search for the first character of the token |
dflet | 0:400d8e75a8d0 | 112 | pch = (UINT8*)memchr(pch, pToken[0], nMaxCount); |
dflet | 0:400d8e75a8d0 | 113 | if (pch==NULL) |
dflet | 0:400d8e75a8d0 | 114 | return NULL; |
dflet | 0:400d8e75a8d0 | 115 | |
dflet | 0:400d8e75a8d0 | 116 | // Found first character, now compare the rest |
dflet | 0:400d8e75a8d0 | 117 | partialBlob.pData = pch; |
dflet | 0:400d8e75a8d0 | 118 | partialBlob.uLength = uTokenLength; |
dflet | 0:400d8e75a8d0 | 119 | if (HttpString_strcmp(token, partialBlob)==0) |
dflet | 0:400d8e75a8d0 | 120 | return pch; |
dflet | 0:400d8e75a8d0 | 121 | |
dflet | 0:400d8e75a8d0 | 122 | // Skip this byte, and look for the token in the rest of the blob |
dflet | 0:400d8e75a8d0 | 123 | ++pch; |
dflet | 0:400d8e75a8d0 | 124 | } |
dflet | 0:400d8e75a8d0 | 125 | return NULL; |
dflet | 0:400d8e75a8d0 | 126 | } |
dflet | 0:400d8e75a8d0 | 127 | |
dflet | 0:400d8e75a8d0 | 128 | UINT32 HttpString_atou(struct HttpBlob string) |
dflet | 0:400d8e75a8d0 | 129 | { |
dflet | 0:400d8e75a8d0 | 130 | return atoi((const char*)string.pData); |
dflet | 0:400d8e75a8d0 | 131 | } |
dflet | 0:400d8e75a8d0 | 132 | |
dflet | 0:400d8e75a8d0 | 133 | void HttpString_utoa(UINT32 uNum, struct HttpBlob* pString) |
dflet | 0:400d8e75a8d0 | 134 | { |
dflet | 0:400d8e75a8d0 | 135 | char* ptr; |
dflet | 0:400d8e75a8d0 | 136 | UINT32 uTemp = uNum; |
dflet | 0:400d8e75a8d0 | 137 | |
dflet | 0:400d8e75a8d0 | 138 | // value 0 is a special format |
dflet | 0:400d8e75a8d0 | 139 | if (uNum == 0) |
dflet | 0:400d8e75a8d0 | 140 | { |
dflet | 0:400d8e75a8d0 | 141 | pString->uLength = 1; |
dflet | 0:400d8e75a8d0 | 142 | *pString->pData = '0'; |
dflet | 0:400d8e75a8d0 | 143 | return; |
dflet | 0:400d8e75a8d0 | 144 | } |
dflet | 0:400d8e75a8d0 | 145 | |
dflet | 0:400d8e75a8d0 | 146 | // Find out the length of the number, in decimal base |
dflet | 0:400d8e75a8d0 | 147 | pString->uLength = 0; |
dflet | 0:400d8e75a8d0 | 148 | while (uTemp > 0) |
dflet | 0:400d8e75a8d0 | 149 | { |
dflet | 0:400d8e75a8d0 | 150 | uTemp /= 10; |
dflet | 0:400d8e75a8d0 | 151 | pString->uLength++; |
dflet | 0:400d8e75a8d0 | 152 | } |
dflet | 0:400d8e75a8d0 | 153 | |
dflet | 0:400d8e75a8d0 | 154 | // Do the actual formatting, right to left |
dflet | 0:400d8e75a8d0 | 155 | uTemp = uNum; |
dflet | 0:400d8e75a8d0 | 156 | ptr = (char*)pString->pData + pString->uLength; |
dflet | 0:400d8e75a8d0 | 157 | while (uTemp > 0) |
dflet | 0:400d8e75a8d0 | 158 | { |
dflet | 0:400d8e75a8d0 | 159 | --ptr; |
dflet | 0:400d8e75a8d0 | 160 | *ptr = digits[uTemp % 10]; |
dflet | 0:400d8e75a8d0 | 161 | uTemp /= 10; |
dflet | 0:400d8e75a8d0 | 162 | } |
dflet | 0:400d8e75a8d0 | 163 | } |
dflet | 0:400d8e75a8d0 | 164 | |
dflet | 0:400d8e75a8d0 | 165 | void HttpString_htoa(UINT32 uNum, struct HttpBlob* pString, UINT8 bPadZero) |
dflet | 0:400d8e75a8d0 | 166 | { |
dflet | 0:400d8e75a8d0 | 167 | UINT8* ptr; |
dflet | 0:400d8e75a8d0 | 168 | UINT32 uTemp = uNum; |
dflet | 0:400d8e75a8d0 | 169 | |
dflet | 0:400d8e75a8d0 | 170 | if (!bPadZero) |
dflet | 0:400d8e75a8d0 | 171 | { |
dflet | 0:400d8e75a8d0 | 172 | // value 0 is a special format |
dflet | 0:400d8e75a8d0 | 173 | if (uNum == 0) |
dflet | 0:400d8e75a8d0 | 174 | { |
dflet | 0:400d8e75a8d0 | 175 | pString->uLength = 1; |
dflet | 0:400d8e75a8d0 | 176 | *pString->pData = '0'; |
dflet | 0:400d8e75a8d0 | 177 | return; |
dflet | 0:400d8e75a8d0 | 178 | } |
dflet | 0:400d8e75a8d0 | 179 | |
dflet | 0:400d8e75a8d0 | 180 | // Find out the length of the number, in hexadecimal base |
dflet | 0:400d8e75a8d0 | 181 | pString->uLength = 0; |
dflet | 0:400d8e75a8d0 | 182 | while (uTemp > 0) |
dflet | 0:400d8e75a8d0 | 183 | { |
dflet | 0:400d8e75a8d0 | 184 | uTemp /= 16; |
dflet | 0:400d8e75a8d0 | 185 | pString->uLength++; |
dflet | 0:400d8e75a8d0 | 186 | } |
dflet | 0:400d8e75a8d0 | 187 | } |
dflet | 0:400d8e75a8d0 | 188 | |
dflet | 0:400d8e75a8d0 | 189 | // Do the actual formatting, right to left |
dflet | 0:400d8e75a8d0 | 190 | uTemp = uNum; |
dflet | 0:400d8e75a8d0 | 191 | ptr = pString->pData + pString->uLength; |
dflet | 0:400d8e75a8d0 | 192 | while (ptr > pString->pData) |
dflet | 0:400d8e75a8d0 | 193 | { |
dflet | 0:400d8e75a8d0 | 194 | --ptr; |
dflet | 0:400d8e75a8d0 | 195 | *ptr = (UINT8)hexDigits[uTemp % 16]; |
dflet | 0:400d8e75a8d0 | 196 | uTemp /= 16; |
dflet | 0:400d8e75a8d0 | 197 | } |
dflet | 0:400d8e75a8d0 | 198 | } |
dflet | 0:400d8e75a8d0 | 199 | |
dflet | 0:400d8e75a8d0 | 200 | /// @} |
dflet | 0:400d8e75a8d0 | 201 |