BLE EddystoneService example
This example is a fork of the following mbed-os example:
https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-ble-EddystoneService/
Please read the documentation in this page.
source/URLFrame.cpp@1:9db4d46bb63f, 2016-07-28 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Jul 28 23:14:36 2016 +0100
- Revision:
- 1:9db4d46bb63f
- Parent:
- 0:4c8f8bf32a99
- Child:
- 2:9ee673e0b86a
Merge branch 'master' of https://github.com/ARMmbed/mbed-os-example-ble
Commit copied from ./src/github.com/ARMmbed/mbed-os-example-ble
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 1:9db4d46bb63f | 1 | /* mbed Microcontroller Library |
mbed_official | 1:9db4d46bb63f | 2 | * Copyright (c) 2006-2015 ARM Limited |
mbed_official | 1:9db4d46bb63f | 3 | * |
mbed_official | 1:9db4d46bb63f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 1:9db4d46bb63f | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 1:9db4d46bb63f | 6 | * You may obtain a copy of the License at |
mbed_official | 1:9db4d46bb63f | 7 | * |
mbed_official | 1:9db4d46bb63f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 1:9db4d46bb63f | 9 | * |
mbed_official | 1:9db4d46bb63f | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 1:9db4d46bb63f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 1:9db4d46bb63f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 1:9db4d46bb63f | 13 | * See the License for the specific language governing permissions and |
mbed_official | 1:9db4d46bb63f | 14 | * limitations under the License. |
mbed_official | 1:9db4d46bb63f | 15 | */ |
mbed_official | 1:9db4d46bb63f | 16 | |
mbed_official | 1:9db4d46bb63f | 17 | #include "URLFrame.h" |
mbed_official | 1:9db4d46bb63f | 18 | |
mbed_official | 1:9db4d46bb63f | 19 | URLFrame::URLFrame(void) |
mbed_official | 1:9db4d46bb63f | 20 | { |
mbed_official | 1:9db4d46bb63f | 21 | urlDataLength = 0; |
mbed_official | 1:9db4d46bb63f | 22 | memset(urlData, 0, sizeof(UrlData_t)); |
mbed_official | 1:9db4d46bb63f | 23 | } |
mbed_official | 1:9db4d46bb63f | 24 | |
mbed_official | 1:9db4d46bb63f | 25 | URLFrame::URLFrame(const char *urlDataIn) |
mbed_official | 1:9db4d46bb63f | 26 | { |
mbed_official | 1:9db4d46bb63f | 27 | encodeURL(urlDataIn); |
mbed_official | 1:9db4d46bb63f | 28 | } |
mbed_official | 1:9db4d46bb63f | 29 | |
mbed_official | 1:9db4d46bb63f | 30 | URLFrame::URLFrame(UrlData_t urlDataIn, uint8_t urlDataLengthIn) |
mbed_official | 1:9db4d46bb63f | 31 | { |
mbed_official | 1:9db4d46bb63f | 32 | urlDataLength = (urlDataLengthIn > URL_DATA_MAX) ? URL_DATA_MAX : urlDataLengthIn; |
mbed_official | 1:9db4d46bb63f | 33 | memcpy(urlData, urlDataIn, urlDataLength); |
mbed_official | 1:9db4d46bb63f | 34 | } |
mbed_official | 1:9db4d46bb63f | 35 | |
mbed_official | 1:9db4d46bb63f | 36 | void URLFrame::constructURLFrame(uint8_t* rawFrame, int8_t advPowerLevel) |
mbed_official | 1:9db4d46bb63f | 37 | { |
mbed_official | 1:9db4d46bb63f | 38 | size_t index = 0; |
mbed_official | 1:9db4d46bb63f | 39 | rawFrame[index++] = EDDYSTONE_UUID[0]; // 16-bit Eddystone UUID |
mbed_official | 1:9db4d46bb63f | 40 | rawFrame[index++] = EDDYSTONE_UUID[1]; |
mbed_official | 1:9db4d46bb63f | 41 | rawFrame[index++] = FRAME_TYPE_URL; // 1B Type |
mbed_official | 1:9db4d46bb63f | 42 | rawFrame[index++] = advPowerLevel; // 1B Power @ 0meter |
mbed_official | 1:9db4d46bb63f | 43 | memcpy(rawFrame + index, urlData, urlDataLength); // Encoded URL |
mbed_official | 1:9db4d46bb63f | 44 | } |
mbed_official | 1:9db4d46bb63f | 45 | |
mbed_official | 1:9db4d46bb63f | 46 | size_t URLFrame::getRawFrameSize(void) const |
mbed_official | 1:9db4d46bb63f | 47 | { |
mbed_official | 1:9db4d46bb63f | 48 | return urlDataLength + FRAME_MIN_SIZE_URL + EDDYSTONE_UUID_SIZE; |
mbed_official | 1:9db4d46bb63f | 49 | } |
mbed_official | 1:9db4d46bb63f | 50 | |
mbed_official | 1:9db4d46bb63f | 51 | uint8_t* URLFrame::getEncodedURLData(void) |
mbed_official | 1:9db4d46bb63f | 52 | { |
mbed_official | 1:9db4d46bb63f | 53 | return urlData; |
mbed_official | 1:9db4d46bb63f | 54 | } |
mbed_official | 1:9db4d46bb63f | 55 | |
mbed_official | 1:9db4d46bb63f | 56 | uint8_t URLFrame::getEncodedURLDataLength(void) const |
mbed_official | 1:9db4d46bb63f | 57 | { |
mbed_official | 1:9db4d46bb63f | 58 | return urlDataLength; |
mbed_official | 1:9db4d46bb63f | 59 | } |
mbed_official | 1:9db4d46bb63f | 60 | |
mbed_official | 1:9db4d46bb63f | 61 | void URLFrame::setURLData(const char *urlDataIn) |
mbed_official | 1:9db4d46bb63f | 62 | { |
mbed_official | 1:9db4d46bb63f | 63 | encodeURL(urlDataIn); |
mbed_official | 1:9db4d46bb63f | 64 | } |
mbed_official | 1:9db4d46bb63f | 65 | |
mbed_official | 1:9db4d46bb63f | 66 | void URLFrame::setEncodedURLData(const uint8_t* urlEncodedDataIn, const uint8_t urlEncodedDataLengthIn) |
mbed_official | 1:9db4d46bb63f | 67 | { |
mbed_official | 1:9db4d46bb63f | 68 | urlDataLength = urlEncodedDataLengthIn; |
mbed_official | 1:9db4d46bb63f | 69 | memcpy(urlData, urlEncodedDataIn, urlEncodedDataLengthIn); |
mbed_official | 1:9db4d46bb63f | 70 | } |
mbed_official | 1:9db4d46bb63f | 71 | |
mbed_official | 1:9db4d46bb63f | 72 | void URLFrame::encodeURL(const char *urlDataIn) |
mbed_official | 1:9db4d46bb63f | 73 | { |
mbed_official | 1:9db4d46bb63f | 74 | const char *prefixes[] = { |
mbed_official | 1:9db4d46bb63f | 75 | "http://www.", |
mbed_official | 1:9db4d46bb63f | 76 | "https://www.", |
mbed_official | 1:9db4d46bb63f | 77 | "http://", |
mbed_official | 1:9db4d46bb63f | 78 | "https://", |
mbed_official | 1:9db4d46bb63f | 79 | }; |
mbed_official | 1:9db4d46bb63f | 80 | const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); |
mbed_official | 1:9db4d46bb63f | 81 | const char *suffixes[] = { |
mbed_official | 1:9db4d46bb63f | 82 | ".com/", |
mbed_official | 1:9db4d46bb63f | 83 | ".org/", |
mbed_official | 1:9db4d46bb63f | 84 | ".edu/", |
mbed_official | 1:9db4d46bb63f | 85 | ".net/", |
mbed_official | 1:9db4d46bb63f | 86 | ".info/", |
mbed_official | 1:9db4d46bb63f | 87 | ".biz/", |
mbed_official | 1:9db4d46bb63f | 88 | ".gov/", |
mbed_official | 1:9db4d46bb63f | 89 | ".com", |
mbed_official | 1:9db4d46bb63f | 90 | ".org", |
mbed_official | 1:9db4d46bb63f | 91 | ".edu", |
mbed_official | 1:9db4d46bb63f | 92 | ".net", |
mbed_official | 1:9db4d46bb63f | 93 | ".info", |
mbed_official | 1:9db4d46bb63f | 94 | ".biz", |
mbed_official | 1:9db4d46bb63f | 95 | ".gov" |
mbed_official | 1:9db4d46bb63f | 96 | }; |
mbed_official | 1:9db4d46bb63f | 97 | const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); |
mbed_official | 1:9db4d46bb63f | 98 | |
mbed_official | 1:9db4d46bb63f | 99 | urlDataLength = 0; |
mbed_official | 1:9db4d46bb63f | 100 | memset(urlData, 0, sizeof(UrlData_t)); |
mbed_official | 1:9db4d46bb63f | 101 | |
mbed_official | 1:9db4d46bb63f | 102 | if ((urlDataIn == NULL) || (strlen(urlDataIn) == 0)) { |
mbed_official | 1:9db4d46bb63f | 103 | return; |
mbed_official | 1:9db4d46bb63f | 104 | } |
mbed_official | 1:9db4d46bb63f | 105 | |
mbed_official | 1:9db4d46bb63f | 106 | /* |
mbed_official | 1:9db4d46bb63f | 107 | * handle prefix |
mbed_official | 1:9db4d46bb63f | 108 | */ |
mbed_official | 1:9db4d46bb63f | 109 | for (size_t i = 0; i < NUM_PREFIXES; i++) { |
mbed_official | 1:9db4d46bb63f | 110 | size_t prefixLen = strlen(prefixes[i]); |
mbed_official | 1:9db4d46bb63f | 111 | if (strncmp(urlDataIn, prefixes[i], prefixLen) == 0) { |
mbed_official | 1:9db4d46bb63f | 112 | urlData[urlDataLength++] = i; |
mbed_official | 1:9db4d46bb63f | 113 | urlDataIn += prefixLen; |
mbed_official | 1:9db4d46bb63f | 114 | break; |
mbed_official | 1:9db4d46bb63f | 115 | } |
mbed_official | 1:9db4d46bb63f | 116 | } |
mbed_official | 1:9db4d46bb63f | 117 | |
mbed_official | 1:9db4d46bb63f | 118 | /* |
mbed_official | 1:9db4d46bb63f | 119 | * handle suffixes |
mbed_official | 1:9db4d46bb63f | 120 | */ |
mbed_official | 1:9db4d46bb63f | 121 | while (*urlDataIn && (urlDataLength < URL_DATA_MAX)) { |
mbed_official | 1:9db4d46bb63f | 122 | /* check for suffix match */ |
mbed_official | 1:9db4d46bb63f | 123 | size_t i; |
mbed_official | 1:9db4d46bb63f | 124 | for (i = 0; i < NUM_SUFFIXES; i++) { |
mbed_official | 1:9db4d46bb63f | 125 | size_t suffixLen = strlen(suffixes[i]); |
mbed_official | 1:9db4d46bb63f | 126 | if (strncmp(urlDataIn, suffixes[i], suffixLen) == 0) { |
mbed_official | 1:9db4d46bb63f | 127 | urlData[urlDataLength++] = i; |
mbed_official | 1:9db4d46bb63f | 128 | urlDataIn += suffixLen; |
mbed_official | 1:9db4d46bb63f | 129 | break; /* from the for loop for checking against suffixes */ |
mbed_official | 1:9db4d46bb63f | 130 | } |
mbed_official | 1:9db4d46bb63f | 131 | } |
mbed_official | 1:9db4d46bb63f | 132 | /* This is the default case where we've got an ordinary character which doesn't match a suffix. */ |
mbed_official | 1:9db4d46bb63f | 133 | if (i == NUM_SUFFIXES) { |
mbed_official | 1:9db4d46bb63f | 134 | urlData[urlDataLength++] = *urlDataIn; |
mbed_official | 1:9db4d46bb63f | 135 | ++urlDataIn; |
mbed_official | 1:9db4d46bb63f | 136 | } |
mbed_official | 1:9db4d46bb63f | 137 | } |
mbed_official | 1:9db4d46bb63f | 138 | } |