Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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 urlDataLength)
00031 {
00032     if (urlDataLength > URL_DATA_MAX) {
00033         memcpy(urlData, urlDataIn, URL_DATA_MAX);
00034     } else {
00035         memcpy(urlData, urlDataIn, urlDataLength);
00036     }
00037 }
00038 
00039 void URLFrame::constructURLFrame(uint8_t* rawFrame, int8_t advPowerLevel)
00040 {
00041     size_t index = 0;
00042     rawFrame[index++] = EDDYSTONE_UUID[0];            // 16-bit Eddystone UUID
00043     rawFrame[index++] = EDDYSTONE_UUID[1];
00044     rawFrame[index++] = FRAME_TYPE_URL;               // 1B  Type
00045     rawFrame[index++] = advPowerLevel;                // 1B  Power @ 0meter
00046     memcpy(rawFrame + index, urlData, urlDataLength); // Encoded URL
00047 }
00048 
00049 size_t URLFrame::getRawFrameSize(void) const
00050 {
00051     return urlDataLength + FRAME_MIN_SIZE_URL + EDDYSTONE_UUID_SIZE;
00052 }
00053 
00054 uint8_t* URLFrame::getEncodedURLData(void)
00055 {
00056     return urlData;
00057 }
00058 
00059 uint8_t URLFrame::getEncodedURLDataLength(void) const
00060 {
00061     return urlDataLength;
00062 }
00063 
00064 void URLFrame::setURLData(const char *urlDataIn)
00065 {
00066     encodeURL(urlDataIn);
00067 }
00068 
00069 void URLFrame::setEncodedURLData(const uint8_t* urlEncodedDataIn, const uint8_t urlEncodedDataLengthIn)
00070 {
00071     urlDataLength = urlEncodedDataLengthIn;
00072     memcpy(urlData, urlEncodedDataIn, urlEncodedDataLengthIn);
00073 }
00074 
00075 void URLFrame::encodeURL(const char *urlDataIn)
00076 {
00077     const char  *prefixes[] = {
00078         "http://www.",
00079         "https://www.",
00080         "http://",
00081         "https://",
00082     };
00083     const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
00084     const char  *suffixes[]   = {
00085         ".com/",
00086         ".org/",
00087         ".edu/",
00088         ".net/",
00089         ".info/",
00090         ".biz/",
00091         ".gov/",
00092         ".com",
00093         ".org",
00094         ".edu",
00095         ".net",
00096         ".info",
00097         ".biz",
00098         ".gov"
00099     };
00100     const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
00101 
00102     urlDataLength = 0;
00103     memset(urlData, 0, sizeof(UrlData_t));
00104 
00105     if ((urlDataIn == NULL) || (strlen(urlDataIn) == 0)) {
00106         return;
00107     }
00108 
00109     /*
00110      * handle prefix
00111      */
00112     for (size_t i = 0; i < NUM_PREFIXES; i++) {
00113         size_t prefixLen = strlen(prefixes[i]);
00114         if (strncmp(urlDataIn, prefixes[i], prefixLen) == 0) {
00115             urlData[urlDataLength++]  = i;
00116             urlDataIn                      += prefixLen;
00117             break;
00118         }
00119     }
00120 
00121     /*
00122      * handle suffixes
00123      */
00124     while (*urlDataIn && (urlDataLength < URL_DATA_MAX)) {
00125         /* check for suffix match */
00126         size_t i;
00127         for (i = 0; i < NUM_SUFFIXES; i++) {
00128             size_t suffixLen = strlen(suffixes[i]);
00129             if (strncmp(urlDataIn, suffixes[i], suffixLen) == 0) {
00130                 urlData[urlDataLength++]  = i;
00131                 urlDataIn                      += suffixLen;
00132                 break; /* from the for loop for checking against suffixes */
00133             }
00134         }
00135         /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
00136         if (i == NUM_SUFFIXES) {
00137             urlData[urlDataLength++] = *urlDataIn;
00138             ++urlDataIn;
00139         }
00140     }
00141 }