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