/* * This function is called every time we scan an advertisement. */ static void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { struct AdvertisingData_t { uint8_t length; /* doesn't include itself */ GapAdvertisingData::DataType_t dataType; uint8_t data[0]; } AdvDataPacket; struct ApplicationData_t { uint8_t applicationSpecificId[2]; uint8_t frameType; uint8_t advPowerLevels; uint8_t uriData[URI_MAX_LENGTH]; } AppDataPacket; const uint8_t BEACON_UUID[sizeof(UUID::ShortUUIDBytes_t)] = {0xAA, 0xFE}; const uint8_t FRAME_TYPE_URL = 0x10; const uint8_t APPLICATION_DATA_OFFSET = sizeof(ApplicationData_t) + sizeof(AdvDataPacket.dataType) - sizeof(AppDataPacket.uriData); AdvertisingData_t *pAdvData; size_t index = 0; while(index < params->advertisingDataLen) { pAdvData = (AdvertisingData_t *)¶ms->advertisingData[index]; if (pAdvData->dataType == GapAdvertisingData::SERVICE_DATA) { ApplicationData_t *pAppData = (ApplicationData_t *) pAdvData->data; if (!memcmp(pAppData->applicationSpecificId, BEACON_UUID, sizeof(BEACON_UUID)) && (pAppData->frameType == FRAME_TYPE_URL)) { decodeURI(pAppData->uriData, pAdvData->length - APPLICATION_DATA_OFFSET, params->rssi); break; } } index += (pAdvData->length + 1); } }
/** * cgiInit * * Load the available CGI variables and create the PHP style _SERVER and _GET * dynamic variables. Both variables are created as associative arrays. **/ int cgiInit() { METHOD *pMethod; DATA *ptContent, *ptQuery, *ptArgs, *ptSERVER, *ptCBTREE, *ptGET, *ptPOST; char cProperty[MAX_BUF_SIZE], cValue[MAX_BUF_SIZE], cArgm[MAX_BUF_SIZE], *pcAllowed, *pcSrc, *pcArgm; int iArgCount, iSep, i; phResp = stdout; cgiEnvironment = newArray(NULL); // Setup a PHP style '$_SERVER' variable. if( (ptSERVER = newArray( "_SERVER" )) ) { for( i=0; cgiVarNames[i]; i++) { varNewProperty( cgiVarNames[i], getenv(cgiVarNames[i]), ptSERVER ); } varPush( cgiEnvironment, ptSERVER ); } /** * Setup a PHP style '$_CBTREE' variable. * * NOTE: On Apache HTTP servers any non CGI variables must be passed explicitly * using the 'SetEnv' or 'PassEnv' directive. For example in httpd.conf * add: * * PassEnv CBTREE_METHODS **/ if( (ptCBTREE = newArray( "_CBTREE" )) ) { for( i=0; cgiCbtreeNames[i]; i++) { varNewProperty( cgiCbtreeNames[i], getenv(cgiCbtreeNames[i]), ptCBTREE ); } varPush( cgiEnvironment, ptCBTREE ); } #ifdef _DEBUG varSet( cgiGetProperty("QUERY_STRING"), cDbgQS ); #endif switch( cgiGetMethodId() ) { case HTTP_V_DELETE: case HTTP_V_GET: if( (ptGET = newArray( "_GET" )) ) { if( (ptQuery = cgiGetProperty("QUERY_STRING")) ) { // Get the list of HTTP query arguments as a new array. ptArgs = varSplit( ptQuery, "&", false ); ptGET = newArray( "_GET" ); if( (iArgCount = varCount( ptArgs )) ) { for( i=0; i < iArgCount; i++ ) { if( (pcArgm = varGet(varGetByIndex( i, ptArgs ))) ) { // Decode special characters and treat each argument as a new property. pcSrc = decodeURI( pcArgm, cArgm, sizeof(cArgm)-1 ); iSep = strcspn( pcSrc, "=" ); strncpyz( cProperty, pcSrc, iSep ); pcSrc += pcSrc[iSep] ? iSep + 1: iSep; strcpy( cValue, pcSrc ); varNewProperty( cProperty, cValue, ptGET ); } } } destroy( ptArgs ); } varPush( cgiEnvironment, ptGET ); } break; case HTTP_V_POST: if( (ptPOST = newArray( "_POST" )) ) { // Read the content from stdin if( fgets(cArgm, sizeof(cArgm)-1, stdin) ) { ptContent = newString( "CONTENT", cArgm ); ptArgs = varSplit( ptContent, "&", false ); if( (iArgCount = varCount( ptArgs )) ) { for( i=0; i < iArgCount; i++ ) { if( (pcArgm = varGet(varGetByIndex( i, ptArgs ))) ) { // Decode special characters and treat each argument as a new property. pcSrc = decodeURI( pcArgm, cArgm, sizeof(cArgm)-1 ); iSep = strcspn( pcSrc, "=" ); strncpyz( cProperty, pcSrc, iSep ); pcSrc += pcSrc[iSep] ? iSep + 1: iSep; strcpy( cValue, pcSrc ); varNewProperty( cProperty, cValue, ptPOST ); } } } destroy( ptArgs ); } varPush( cgiEnvironment, ptPOST ); } break; } // Define which HTTP methods are allowed. if( ptCBTREE ) { pcAllowed = varGet(varGetProperty("CBTREE_METHODS", ptCBTREE)); snprintf( cProperty, sizeof(cProperty)-1,"GET,%s", (pcAllowed ? pcAllowed : "") ); pcArgm = strtok( cProperty, ", " ); while( pcArgm ) { strtrim( pcArgm, TRIM_M_WSP ); if( *pcArgm ) { if( (pMethod = _cgiGetMethodByName( pcArgm )) ) { pMethod->bAllowed = true; } } pcArgm = strtok( NULL, ", " ); } } return 1; }