BLE EddystoneService example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers URLFrame.cpp Source File

URLFrame.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "URLFrame.h"
00018 
00019 URLFrame::URLFrame(void)
00020 {
00021     urlDataLength = 0;
00022     memset(urlData, 0, sizeof(UrlData_t));
00023 }
00024 
00025 URLFrame::URLFrame(const char *urlDataIn)
00026 {
00027     encodeURL(urlDataIn);
00028 }
00029 
00030 URLFrame::URLFrame(UrlData_t urlDataIn, uint8_t urlDataLengthIn)
00031 {
00032     urlDataLength = (urlDataLengthIn > URL_DATA_MAX) ? URL_DATA_MAX : urlDataLengthIn;
00033     memcpy(urlData, urlDataIn, urlDataLength);
00034 }
00035 
00036 void URLFrame::constructURLFrame(uint8_t* rawFrame, int8_t advPowerLevel)
00037 {
00038     size_t index = 0;
00039     rawFrame[index++] = EDDYSTONE_UUID[0];            // 16-bit Eddystone UUID
00040     rawFrame[index++] = EDDYSTONE_UUID[1];
00041     rawFrame[index++] = FRAME_TYPE_URL;               // 1B  Type
00042     rawFrame[index++] = advPowerLevel;                // 1B  Power @ 0meter
00043     memcpy(rawFrame + index, urlData, urlDataLength); // Encoded URL
00044 }
00045 
00046 size_t URLFrame::getRawFrameSize(void) const
00047 {
00048     return urlDataLength + FRAME_MIN_SIZE_URL + EDDYSTONE_UUID_SIZE;
00049 }
00050 
00051 uint8_t* URLFrame::getEncodedURLData(void)
00052 {
00053     return urlData;
00054 }
00055 
00056 uint8_t URLFrame::getEncodedURLDataLength(void) const
00057 {
00058     return urlDataLength;
00059 }
00060 
00061 void URLFrame::setURLData(const char *urlDataIn)
00062 {
00063     encodeURL(urlDataIn);
00064 }
00065 
00066 void URLFrame::setEncodedURLData(const uint8_t* urlEncodedDataIn, const uint8_t urlEncodedDataLengthIn)
00067 {
00068     urlDataLength = urlEncodedDataLengthIn;
00069     memcpy(urlData, urlEncodedDataIn, urlEncodedDataLengthIn);
00070 }
00071 
00072 void URLFrame::encodeURL(const char *urlDataIn)
00073 {
00074     const char  *prefixes[] = {
00075         "http://www.",
00076         "https://www.",
00077         "http://",
00078         "https://",
00079     };
00080     const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
00081     const char  *suffixes[]   = {
00082         ".com/",
00083         ".org/",
00084         ".edu/",
00085         ".net/",
00086         ".info/",
00087         ".biz/",
00088         ".gov/",
00089         ".com",
00090         ".org",
00091         ".edu",
00092         ".net",
00093         ".info",
00094         ".biz",
00095         ".gov"
00096     };
00097     const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
00098 
00099     urlDataLength = 0;
00100     memset(urlData, 0, sizeof(UrlData_t));
00101 
00102     if ((urlDataIn == NULL) || (strlen(urlDataIn) == 0)) {
00103         return;
00104     }
00105 
00106     /*
00107      * handle prefix
00108      */
00109     for (size_t i = 0; i < NUM_PREFIXES; i++) {
00110         size_t prefixLen = strlen(prefixes[i]);
00111         if (strncmp(urlDataIn, prefixes[i], prefixLen) == 0) {
00112             urlData[urlDataLength++]  = i;
00113             urlDataIn                      += prefixLen;
00114             break;
00115         }
00116     }
00117 
00118     /*
00119      * handle suffixes
00120      */
00121     while (*urlDataIn && (urlDataLength < URL_DATA_MAX)) {
00122         /* check for suffix match */
00123         size_t i;
00124         for (i = 0; i < NUM_SUFFIXES; i++) {
00125             size_t suffixLen = strlen(suffixes[i]);
00126             if (strncmp(urlDataIn, suffixes[i], suffixLen) == 0) {
00127                 urlData[urlDataLength++]  = i;
00128                 urlDataIn                      += suffixLen;
00129                 break; /* from the for loop for checking against suffixes */
00130             }
00131         }
00132         /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
00133         if (i == NUM_SUFFIXES) {
00134             urlData[urlDataLength++] = *urlDataIn;
00135             ++urlDataIn;
00136         }
00137     }
00138 }