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