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