Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/URLFrame.cpp@0:1c7da5f83647, 2016-11-29 (annotated)
- Committer:
- sarahmarshy
- Date:
- Tue Nov 29 06:29:10 2016 +0000
- Revision:
- 0:1c7da5f83647
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sarahmarshy | 0:1c7da5f83647 | 1 | /* |
| sarahmarshy | 0:1c7da5f83647 | 2 | * Copyright (c) 2006-2016 Google Inc, All Rights Reserved |
| sarahmarshy | 0:1c7da5f83647 | 3 | * |
| sarahmarshy | 0:1c7da5f83647 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| sarahmarshy | 0:1c7da5f83647 | 5 | * you may not use this file except in compliance with the License. |
| sarahmarshy | 0:1c7da5f83647 | 6 | * You may obtain a copy of the License at |
| sarahmarshy | 0:1c7da5f83647 | 7 | * |
| sarahmarshy | 0:1c7da5f83647 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| sarahmarshy | 0:1c7da5f83647 | 9 | * |
| sarahmarshy | 0:1c7da5f83647 | 10 | * Unless required by applicable law or agreed to in writing, software |
| sarahmarshy | 0:1c7da5f83647 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| sarahmarshy | 0:1c7da5f83647 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| sarahmarshy | 0:1c7da5f83647 | 13 | * See the License for the specific language governing permissions and |
| sarahmarshy | 0:1c7da5f83647 | 14 | * limitations under the License. |
| sarahmarshy | 0:1c7da5f83647 | 15 | */ |
| sarahmarshy | 0:1c7da5f83647 | 16 | |
| sarahmarshy | 0:1c7da5f83647 | 17 | #include "URLFrame.h" |
| sarahmarshy | 0:1c7da5f83647 | 18 | |
| sarahmarshy | 0:1c7da5f83647 | 19 | /* CONSTRUCTOR */ |
| sarahmarshy | 0:1c7da5f83647 | 20 | URLFrame::URLFrame(void) |
| sarahmarshy | 0:1c7da5f83647 | 21 | { |
| sarahmarshy | 0:1c7da5f83647 | 22 | } |
| sarahmarshy | 0:1c7da5f83647 | 23 | |
| sarahmarshy | 0:1c7da5f83647 | 24 | void URLFrame::setUnencodedUrlData(uint8_t* rawFrame, int8_t advTxPower, const char *rawUrl) |
| sarahmarshy | 0:1c7da5f83647 | 25 | { |
| sarahmarshy | 0:1c7da5f83647 | 26 | uint8_t encodedUrl[ENCODED_BUF_SIZE]; |
| sarahmarshy | 0:1c7da5f83647 | 27 | int encodedUrlLen = encodeURL(encodedUrl, rawUrl); |
| sarahmarshy | 0:1c7da5f83647 | 28 | encodedUrlLen = (encodedUrlLen > MAX_URL_DATA) ? MAX_URL_DATA : encodedUrlLen; |
| sarahmarshy | 0:1c7da5f83647 | 29 | setData(rawFrame, advTxPower, reinterpret_cast<const uint8_t*>(encodedUrl), encodedUrlLen); |
| sarahmarshy | 0:1c7da5f83647 | 30 | } |
| sarahmarshy | 0:1c7da5f83647 | 31 | |
| sarahmarshy | 0:1c7da5f83647 | 32 | void URLFrame::clearFrame(uint8_t* frame) { |
| sarahmarshy | 0:1c7da5f83647 | 33 | frame[FRAME_LEN_OFFSET] = 0; // Set frame length to zero to clear it |
| sarahmarshy | 0:1c7da5f83647 | 34 | } |
| sarahmarshy | 0:1c7da5f83647 | 35 | |
| sarahmarshy | 0:1c7da5f83647 | 36 | void URLFrame::setData(uint8_t* rawFrame, int8_t advTxPower, const uint8_t* encodedUrlData, uint8_t encodedUrlLen) |
| sarahmarshy | 0:1c7da5f83647 | 37 | { |
| sarahmarshy | 0:1c7da5f83647 | 38 | uint8_t index = 0; |
| sarahmarshy | 0:1c7da5f83647 | 39 | rawFrame[index++] = URL_HEADER_LEN + encodedUrlLen; // INDEX=0 = Frame Length = encodedURL size + 4 bytes of header below |
| sarahmarshy | 0:1c7da5f83647 | 40 | rawFrame[index++] = EDDYSTONE_UUID[0]; // FRAME 16-bit Eddystone UUID Low Byte (little endian) |
| sarahmarshy | 0:1c7da5f83647 | 41 | rawFrame[index++] = EDDYSTONE_UUID[1]; // FRAME 16-bit Eddystone UUID High Byte |
| sarahmarshy | 0:1c7da5f83647 | 42 | rawFrame[index++] = FRAME_TYPE_URL; // URL Frame Type |
| sarahmarshy | 0:1c7da5f83647 | 43 | rawFrame[index++] = advTxPower; // Power @ 0meter |
| sarahmarshy | 0:1c7da5f83647 | 44 | |
| sarahmarshy | 0:1c7da5f83647 | 45 | memcpy(rawFrame + index, encodedUrlData, encodedUrlLen); |
| sarahmarshy | 0:1c7da5f83647 | 46 | } |
| sarahmarshy | 0:1c7da5f83647 | 47 | |
| sarahmarshy | 0:1c7da5f83647 | 48 | uint8_t* URLFrame::getData(uint8_t* rawFrame) { |
| sarahmarshy | 0:1c7da5f83647 | 49 | return &(rawFrame[URL_DATA_OFFSET]); |
| sarahmarshy | 0:1c7da5f83647 | 50 | } |
| sarahmarshy | 0:1c7da5f83647 | 51 | |
| sarahmarshy | 0:1c7da5f83647 | 52 | |
| sarahmarshy | 0:1c7da5f83647 | 53 | uint8_t URLFrame::getDataLength(uint8_t* rawFrame) { |
| sarahmarshy | 0:1c7da5f83647 | 54 | return rawFrame[FRAME_LEN_OFFSET] - EDDYSTONE_UUID_LEN; |
| sarahmarshy | 0:1c7da5f83647 | 55 | } |
| sarahmarshy | 0:1c7da5f83647 | 56 | |
| sarahmarshy | 0:1c7da5f83647 | 57 | uint8_t* URLFrame::getAdvFrame(uint8_t* rawFrame) |
| sarahmarshy | 0:1c7da5f83647 | 58 | { |
| sarahmarshy | 0:1c7da5f83647 | 59 | return &(rawFrame[ADV_FRAME_OFFSET]); |
| sarahmarshy | 0:1c7da5f83647 | 60 | } |
| sarahmarshy | 0:1c7da5f83647 | 61 | |
| sarahmarshy | 0:1c7da5f83647 | 62 | uint8_t URLFrame::getAdvFrameLength(uint8_t* rawFrame) |
| sarahmarshy | 0:1c7da5f83647 | 63 | { |
| sarahmarshy | 0:1c7da5f83647 | 64 | return rawFrame[FRAME_LEN_OFFSET]; |
| sarahmarshy | 0:1c7da5f83647 | 65 | } |
| sarahmarshy | 0:1c7da5f83647 | 66 | |
| sarahmarshy | 0:1c7da5f83647 | 67 | uint8_t* URLFrame::getEncodedUrl(uint8_t* rawFrame) |
| sarahmarshy | 0:1c7da5f83647 | 68 | { |
| sarahmarshy | 0:1c7da5f83647 | 69 | return &(rawFrame[URL_VALUE_OFFSET]); |
| sarahmarshy | 0:1c7da5f83647 | 70 | } |
| sarahmarshy | 0:1c7da5f83647 | 71 | |
| sarahmarshy | 0:1c7da5f83647 | 72 | uint8_t URLFrame::getEncodedUrlLength(uint8_t* rawFrame) |
| sarahmarshy | 0:1c7da5f83647 | 73 | { |
| sarahmarshy | 0:1c7da5f83647 | 74 | return rawFrame[ADV_FRAME_OFFSET] - URL_HEADER_LEN; |
| sarahmarshy | 0:1c7da5f83647 | 75 | } |
| sarahmarshy | 0:1c7da5f83647 | 76 | |
| sarahmarshy | 0:1c7da5f83647 | 77 | |
| sarahmarshy | 0:1c7da5f83647 | 78 | uint8_t URLFrame::encodeURL(uint8_t* encodedUrl, const char *rawUrl) |
| sarahmarshy | 0:1c7da5f83647 | 79 | { |
| sarahmarshy | 0:1c7da5f83647 | 80 | uint8_t urlDataLength = 0; |
| sarahmarshy | 0:1c7da5f83647 | 81 | |
| sarahmarshy | 0:1c7da5f83647 | 82 | const char *prefixes[] = { |
| sarahmarshy | 0:1c7da5f83647 | 83 | "http://www.", |
| sarahmarshy | 0:1c7da5f83647 | 84 | "https://www.", |
| sarahmarshy | 0:1c7da5f83647 | 85 | "http://", |
| sarahmarshy | 0:1c7da5f83647 | 86 | "https://", |
| sarahmarshy | 0:1c7da5f83647 | 87 | }; |
| sarahmarshy | 0:1c7da5f83647 | 88 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
| sarahmarshy | 0:1c7da5f83647 | 89 | const char *suffixes[] = { |
| sarahmarshy | 0:1c7da5f83647 | 90 | ".com/", |
| sarahmarshy | 0:1c7da5f83647 | 91 | ".org/", |
| sarahmarshy | 0:1c7da5f83647 | 92 | ".edu/", |
| sarahmarshy | 0:1c7da5f83647 | 93 | ".net/", |
| sarahmarshy | 0:1c7da5f83647 | 94 | ".info/", |
| sarahmarshy | 0:1c7da5f83647 | 95 | ".biz/", |
| sarahmarshy | 0:1c7da5f83647 | 96 | ".gov/", |
| sarahmarshy | 0:1c7da5f83647 | 97 | ".com", |
| sarahmarshy | 0:1c7da5f83647 | 98 | ".org", |
| sarahmarshy | 0:1c7da5f83647 | 99 | ".edu", |
| sarahmarshy | 0:1c7da5f83647 | 100 | ".net", |
| sarahmarshy | 0:1c7da5f83647 | 101 | ".info", |
| sarahmarshy | 0:1c7da5f83647 | 102 | ".biz", |
| sarahmarshy | 0:1c7da5f83647 | 103 | ".gov" |
| sarahmarshy | 0:1c7da5f83647 | 104 | }; |
| sarahmarshy | 0:1c7da5f83647 | 105 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
| sarahmarshy | 0:1c7da5f83647 | 106 | |
| sarahmarshy | 0:1c7da5f83647 | 107 | /* |
| sarahmarshy | 0:1c7da5f83647 | 108 | * Fill with one more 0 than max url data size to ensure its null terminated |
| sarahmarshy | 0:1c7da5f83647 | 109 | * And can be printed out for debug purposes |
| sarahmarshy | 0:1c7da5f83647 | 110 | */ |
| sarahmarshy | 0:1c7da5f83647 | 111 | memset(encodedUrl, 0, MAX_URL_DATA + 1); |
| sarahmarshy | 0:1c7da5f83647 | 112 | |
| sarahmarshy | 0:1c7da5f83647 | 113 | if ((rawUrl == NULL) || (strlen(rawUrl) == 0)) { |
| sarahmarshy | 0:1c7da5f83647 | 114 | return urlDataLength; |
| sarahmarshy | 0:1c7da5f83647 | 115 | } |
| sarahmarshy | 0:1c7da5f83647 | 116 | |
| sarahmarshy | 0:1c7da5f83647 | 117 | /* |
| sarahmarshy | 0:1c7da5f83647 | 118 | * handle prefix |
| sarahmarshy | 0:1c7da5f83647 | 119 | */ |
| sarahmarshy | 0:1c7da5f83647 | 120 | for (size_t i = 0; i < NUM_PREFIXES; i++) { |
| sarahmarshy | 0:1c7da5f83647 | 121 | size_t prefixLen = strlen(prefixes[i]); |
| sarahmarshy | 0:1c7da5f83647 | 122 | if (strncmp(rawUrl, prefixes[i], prefixLen) == 0) { |
| sarahmarshy | 0:1c7da5f83647 | 123 | encodedUrl[urlDataLength++] = i; |
| sarahmarshy | 0:1c7da5f83647 | 124 | rawUrl += prefixLen; |
| sarahmarshy | 0:1c7da5f83647 | 125 | break; |
| sarahmarshy | 0:1c7da5f83647 | 126 | } |
| sarahmarshy | 0:1c7da5f83647 | 127 | } |
| sarahmarshy | 0:1c7da5f83647 | 128 | |
| sarahmarshy | 0:1c7da5f83647 | 129 | /* |
| sarahmarshy | 0:1c7da5f83647 | 130 | * handle suffixes |
| sarahmarshy | 0:1c7da5f83647 | 131 | */ |
| sarahmarshy | 0:1c7da5f83647 | 132 | while (*rawUrl && (urlDataLength <= MAX_URL_DATA)) { |
| sarahmarshy | 0:1c7da5f83647 | 133 | /* check for suffix match */ |
| sarahmarshy | 0:1c7da5f83647 | 134 | size_t i; |
| sarahmarshy | 0:1c7da5f83647 | 135 | for (i = 0; i < NUM_SUFFIXES; i++) { |
| sarahmarshy | 0:1c7da5f83647 | 136 | size_t suffixLen = strlen(suffixes[i]); |
| sarahmarshy | 0:1c7da5f83647 | 137 | if (strncmp(rawUrl, suffixes[i], suffixLen) == 0) { |
| sarahmarshy | 0:1c7da5f83647 | 138 | encodedUrl[urlDataLength++] = i; |
| sarahmarshy | 0:1c7da5f83647 | 139 | rawUrl += suffixLen; |
| sarahmarshy | 0:1c7da5f83647 | 140 | break; /* from the for loop for checking against suffixes */ |
| sarahmarshy | 0:1c7da5f83647 | 141 | } |
| sarahmarshy | 0:1c7da5f83647 | 142 | } |
| sarahmarshy | 0:1c7da5f83647 | 143 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
| sarahmarshy | 0:1c7da5f83647 | 144 | if (i == NUM_SUFFIXES) { |
| sarahmarshy | 0:1c7da5f83647 | 145 | encodedUrl[urlDataLength++] = *rawUrl; |
| sarahmarshy | 0:1c7da5f83647 | 146 | ++rawUrl; |
| sarahmarshy | 0:1c7da5f83647 | 147 | } |
| sarahmarshy | 0:1c7da5f83647 | 148 | } |
| sarahmarshy | 0:1c7da5f83647 | 149 | return urlDataLength; |
| sarahmarshy | 0:1c7da5f83647 | 150 | } |
| sarahmarshy | 0:1c7da5f83647 | 151 | |
| sarahmarshy | 0:1c7da5f83647 | 152 | void URLFrame::setAdvTxPower(uint8_t* rawFrame, int8_t advTxPower) |
| sarahmarshy | 0:1c7da5f83647 | 153 | { |
| sarahmarshy | 0:1c7da5f83647 | 154 | rawFrame[URL_TXPOWER_OFFSET] = advTxPower; |
| sarahmarshy | 0:1c7da5f83647 | 155 | } |