URL encoding
This commit is contained in:
		
							
								
								
									
										86
									
								
								BTLE.cpp
									
									
									
									
									
								
							
							
						
						
									
										86
									
								
								BTLE.cpp
									
									
									
									
									
								
							@@ -1,5 +1,6 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2013 Florian Echtler <floe@butterbrot.org>
 | 
			
		||||
 * Eddystone part Copyright (c) 2018 Pavel Brychta <pablo@xpablo.cz>
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU General Public License
 | 
			
		||||
@@ -19,6 +20,36 @@ const uint8_t frequency[3] = { 2,26,80};  // physical frequency (2400+x MHz)
 | 
			
		||||
#define month(m) month_lookup[ (( ((( (m[0] % 24) * 13) + m[1]) % 24) * 13) + m[2]) % 24 ]
 | 
			
		||||
const uint8_t month_lookup[24] = { 0,6,0,4,0,1,0,17,0,8,0,0,3,0,0,0,18,2,16,5,9,0,1,7 };
 | 
			
		||||
 | 
			
		||||
const char PR0[] PROGMEM = "http://www.";
 | 
			
		||||
const char PR1[] PROGMEM = "https://www.";
 | 
			
		||||
const char PR2[] PROGMEM = "http://";
 | 
			
		||||
const char PR3[] PROGMEM = "https://";
 | 
			
		||||
const char * const prefixes[] PROGMEM = {
 | 
			
		||||
  PR0, PR1, PR2, PR3,
 | 
			
		||||
};
 | 
			
		||||
#define NUM_PREFIXES (sizeof(prefixes) / sizeof(char *))
 | 
			
		||||
 | 
			
		||||
const char SU0[] PROGMEM = ".com/";
 | 
			
		||||
const char SU1[] PROGMEM = ".org/";
 | 
			
		||||
const char SU2[] PROGMEM = ".edu/";
 | 
			
		||||
const char SU3[] PROGMEM = ".net/";
 | 
			
		||||
const char SU4[] PROGMEM = ".info/";
 | 
			
		||||
const char SU5[] PROGMEM = ".biz/";
 | 
			
		||||
const char SU6[] PROGMEM = ".gov/";
 | 
			
		||||
const char SU7[] PROGMEM = ".com";
 | 
			
		||||
const char SU8[] PROGMEM = ".org";
 | 
			
		||||
const char SU9[] PROGMEM = ".edu";
 | 
			
		||||
const char SU10[] PROGMEM = ".net";
 | 
			
		||||
const char SU11[] PROGMEM = ".info";
 | 
			
		||||
const char SU12[] PROGMEM = ".biz";
 | 
			
		||||
const char SU13[] PROGMEM = ".gov";
 | 
			
		||||
 | 
			
		||||
const char * const suffixes[] PROGMEM = {
 | 
			
		||||
  SU0, SU1, SU2, SU3, SU4, SU5, SU6, SU7, SU8, SU9, SU10, SU11, SU12, SU13,
 | 
			
		||||
};
 | 
			
		||||
#define NUM_SUFFIXES (sizeof(suffixes) / sizeof(char *))
 | 
			
		||||
 | 
			
		||||
#define MAX_URL_DATA 18
 | 
			
		||||
 | 
			
		||||
// change buffer contents to "wire bit order"
 | 
			
		||||
void BTLE::swapbuf( uint8_t len ) {
 | 
			
		||||
@@ -52,7 +83,7 @@ BTLE::BTLE( RF24* _radio ):
 | 
			
		||||
 | 
			
		||||
// Simple converter from arduino float to a nRF_Float.
 | 
			
		||||
// Supports values from -167772 to +167772, with two decimal places.
 | 
			
		||||
nRF_Float 
 | 
			
		||||
nRF_Float
 | 
			
		||||
BTLE::to_nRF_Float(float t) {
 | 
			
		||||
  int32_t ret;
 | 
			
		||||
  int32_t exponent = -2;
 | 
			
		||||
@@ -103,7 +134,7 @@ bool BTLE::advertise( void* buf, uint8_t buflen ) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Broadcast an advertisement packet with a specific data type
 | 
			
		||||
// Standardized data types can be seen here: 
 | 
			
		||||
// Standardized data types can be seen here:
 | 
			
		||||
// https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
 | 
			
		||||
bool BTLE::advertise( uint8_t data_type, void* buf, uint8_t buflen ) {
 | 
			
		||||
	// name & total payload size
 | 
			
		||||
@@ -184,7 +215,7 @@ bool BTLE::listen(int timeout) {
 | 
			
		||||
		// decode: swap bit order, un-whiten
 | 
			
		||||
		swapbuf( sizeof(buffer) );
 | 
			
		||||
		whiten( sizeof(buffer) );
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// size is w/o header+CRC -> add 2 bytes header
 | 
			
		||||
		total_size = inbuf[1]+2;
 | 
			
		||||
		uint8_t in_crc[3];
 | 
			
		||||
@@ -254,3 +285,52 @@ void BTLE::crc( uint8_t len, uint8_t* dst ) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t BTLE::encodeURL(uint8_t* encodedUrl, const char *rawUrl)
 | 
			
		||||
{
 | 
			
		||||
  uint8_t urlDataLength = 0;
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * Fill with one more 0 than max url data size to ensure its null terminated
 | 
			
		||||
   * And can be printed out for debug purposes
 | 
			
		||||
   */
 | 
			
		||||
  memset(encodedUrl, 0, MAX_URL_DATA + 1);
 | 
			
		||||
 | 
			
		||||
  if ((rawUrl == NULL) || (strlen(rawUrl) == 0)) {
 | 
			
		||||
    return urlDataLength;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
   * handle prefix
 | 
			
		||||
   */
 | 
			
		||||
  for (size_t i = 0; i < NUM_PREFIXES; i++) {
 | 
			
		||||
    size_t prefixLen = strlen_P((char *)pgm_read_word(&prefixes[i]));
 | 
			
		||||
        if (strncmp_P(rawUrl, (char *)pgm_read_word(&prefixes[i]), prefixLen) == 0) {
 | 
			
		||||
            encodedUrl[urlDataLength++]  = i;
 | 
			
		||||
            rawUrl                      += prefixLen;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * handle suffixes
 | 
			
		||||
     */
 | 
			
		||||
    while (*rawUrl && (urlDataLength <= MAX_URL_DATA)) {
 | 
			
		||||
        /* check for suffix match */
 | 
			
		||||
        size_t i;
 | 
			
		||||
        for (i = 0; i < NUM_SUFFIXES; i++) {
 | 
			
		||||
            size_t suffixLen = strlen_P((char *)pgm_read_word(&suffixes[i]));
 | 
			
		||||
            if (strncmp_P(rawUrl, (char *)pgm_read_word(&suffixes[i]), suffixLen) == 0) {
 | 
			
		||||
                encodedUrl[urlDataLength++]  = i;
 | 
			
		||||
                rawUrl                      += suffixLen;
 | 
			
		||||
                break; /* from the for loop for checking against suffixes */
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        /* This is the default case where we've got an ordinary character which doesn't match a suffix. */
 | 
			
		||||
        if (i == NUM_SUFFIXES) {
 | 
			
		||||
            encodedUrl[urlDataLength++] = *rawUrl;
 | 
			
		||||
            ++rawUrl;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return urlDataLength;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user