Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // test con STM32F401 + IDB05A1. 00002 // App utilizzata per i test TxEddystone-URL 00003 00004 #include <events/mbed_events.h> 00005 #include "mbed.h" 00006 #include "ble/BLE.h" 00007 #include "ble/DiscoveredCharacteristic.h" 00008 #include "ble/DiscoveredService.h" 00009 00010 #define MAXNUMCHAR 255 // massima dimensione del comando inviato nell'URI 00011 00012 static const int URI_MAX_LENGTH = 18; // Maximum size of service data in ADV packets 00013 00014 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00015 00016 // genera un oggetto serial collegato al PC 00017 Serial pc(USBTX, USBRX); 00018 00019 // comando ricevuto da iBeacon = caratteri tra prefisso e suffisso dell'indirizzo URI 00020 char caComando[MAXNUMCHAR]; 00021 // ultimo comando ricevuto da iBeacon 00022 char caOldComando[MAXNUMCHAR]; 00023 00024 DigitalOut led1(LED1, 1); // LED su scheda 00025 00026 void periodicCallback(void) 00027 { 00028 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00029 } 00030 00031 //**************************************** 00032 // Decodifica URI ricevuto da Eddystone 00033 //**************************************** 00034 void decodeURI(const uint8_t* uriData, const size_t uriLen) 00035 { 00036 00037 const char *prefixes[] = 00038 { 00039 "http://www.", 00040 "https://www.", 00041 "http://", 00042 "https://", 00043 "urn:uuid:" 00044 }; 00045 00046 const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *); 00047 00048 const char *suffixes[] = 00049 { 00050 ".com/", 00051 ".org/", 00052 ".edu/", 00053 ".net/", 00054 ".info/", 00055 ".biz/", 00056 ".gov/", 00057 ".com", 00058 ".org", 00059 ".edu", 00060 ".net", 00061 ".info", 00062 ".biz", 00063 ".gov" 00064 }; 00065 const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *); 00066 size_t index = 0; 00067 00068 00069 /*------------- Diagnostica ------------------- 00070 pc.printf("sizeofprefixes: %d\r\n", sizeof(prefixes)); 00071 pc.printf("sizeofchar: %d\r\n", sizeof(char *)); 00072 pc.printf("NUM_PREFIXES: %d\r\n", NUM_PREFIXES); 00073 00074 pc.printf("sizeofsuffixes: %d\r\n", sizeof(suffixes)); 00075 pc.printf("sizeofchar: %d\r\n", sizeof(char *)); 00076 pc.printf("NUM_SUFFIXES: %d\r\n", NUM_SUFFIXES); 00077 ------------- Diagnostica -------------------*/ 00078 00079 //pc.printf("uriLen: %d\r\n", uriLen); // diagnostica 00080 //pc.printf("uriData: %s\r\n", uriData); // diagnostica 00081 00082 //------------- Diagnostica ------------------- 00083 // il primo carattere contiene l'indice dell'array dei prefissi: p.e. indice 2 significa prefixes[2] = "http://" 00084 if (uriData[index] < NUM_PREFIXES) 00085 { 00086 // stampa prefisso// 00087 //pc.printf("%s", prefixes[uriData[index]]); // diagnostica 00088 index++; 00089 } 00090 else 00091 { 00092 // il prefisso non è tra quelli codificati 00093 pc.printf("URL Scheme was not encoded!"); 00094 return; 00095 } 00096 pc.printf("\n\r"); 00097 // Inizializza array comando 00098 for(index = 0; index < MAXNUMCHAR; index++) 00099 { 00100 caComando[index] = 0; 00101 } 00102 index=1; 00103 // estrai il comando tra indice = 1 e indice = uriLen-1 00104 while(index < (uriLen-1)) 00105 { 00106 // stampa il dato tra prefisso e suffisso 00107 //pc.printf("%c", uriData[index]); // Diagnostica 00108 caComando[index-1]= uriData[index]; 00109 index++; 00110 } 00111 pc.printf("\n\r"); 00112 00113 // stampa suffisso che si trova all'indice(uriLen-1) 00114 //pc.printf("%s\n\r", suffixes[uriData[uriLen-1]]); // Diagnostica 00115 //------------- Diagnostica ------------------- 00116 } 00117 00118 //************************************************************** 00119 // This function is called every time we scan an advertisement 00120 //************************************************************** 00121 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) 00122 { 00123 struct AdvertisingData_t 00124 { 00125 uint8_t length; /* doesn't include itself */ 00126 GapAdvertisingData::DataType_t dataType; 00127 uint8_t data[1]; 00128 } AdvDataPacket; 00129 00130 struct ApplicationData_t 00131 { 00132 uint8_t applicationSpecificId[2]; 00133 uint8_t frameType; 00134 uint8_t advPowerLevels; 00135 uint8_t uriData[URI_MAX_LENGTH]; 00136 } AppDataPacket; 00137 00138 const uint8_t BEACON_UUID[sizeof(UUID::ShortUUIDBytes_t)] = {0xAA, 0xFE}; 00139 const uint8_t FRAME_TYPE_URL = 0x10; 00140 const uint8_t APPLICATION_DATA_OFFSET = sizeof(ApplicationData_t) + sizeof(AdvDataPacket.dataType) - sizeof(AppDataPacket.uriData); 00141 int nIndex; 00142 AdvertisingData_t *pAdvData; 00143 size_t index = 0; 00144 00145 // parsing dell'Advertising ricevuto 00146 while(index < params->advertisingDataLen) 00147 { 00148 pAdvData = (AdvertisingData_t *)¶ms->advertisingData[index]; 00149 //+++pc.printf("pAdv DataType= %x\r\n",pAdvData->dataType); // Diagnostica 00150 00151 if (pAdvData->dataType == GapAdvertisingData::SERVICE_DATA) 00152 { 00153 ApplicationData_t *pAppData = (ApplicationData_t *) pAdvData->data; 00154 00155 //+++pc.printf("pAppData ApplicationSpecificID= %x\r\n",pAppData->applicationSpecificId); // Diagnostica 00156 //+++pc.printf("pAppData frameType= %x\r\n",pAppData->frameType); // Diagnostica 00157 //pc.printf("------------------------------> Sono arrivato qui\r\n"); // Diagnostica 00158 if (!memcmp(pAppData->applicationSpecificId, BEACON_UUID, sizeof(BEACON_UUID)) 00159 && 00160 (pAppData->frameType == FRAME_TYPE_URL)) 00161 { 00162 decodeURI(pAppData->uriData, pAdvData->length - APPLICATION_DATA_OFFSET); 00163 00164 pc.printf("-------------------------------------------------> Sono partito da qui\r\n"); 00165 // genera messaggio vocale se il comando ricevuto è diverso da quello ricevuto precedentemente 00166 //pc.printf("caComando: %s\r\n", caComando); //Diagnostica 00167 //pc.printf("caOldComando: %s\r\n", caOldComando); //Diagnostica 00168 if(strcmp(caComando,caOldComando) != 0) 00169 { 00170 if(strcmp(caComando,"prova") == 0) 00171 { 00172 pc.printf("Ricevuto Comando PROVA\r\n"); 00173 } 00174 00175 if(strcmp(caComando,"welcome") == 0) 00176 { 00177 pc.printf("Ricevuto Comando WELCOME\r\n"); // diagnostica 00178 } 00179 00180 if(strcmp(caComando,"uscita") == 0) 00181 { 00182 pc.printf("Ricevuto Comando USCITA\r\n"); // diagnostica 00183 } 00184 00185 if(strcmp(caComando,"ingresso") == 0) 00186 { 00187 pc.printf("Ricevuto Comando INGRESSO\r\n"); 00188 } 00189 00190 if(strcmp(caComando,"cambioora") == 0) 00191 { 00192 pc.printf("Ricevuto Comando CAMBIO ORA\r\n"); 00193 } 00194 00195 00196 // copia caComando in caOldComando 00197 for (nIndex = 0; nIndex < MAXNUMCHAR; nIndex++) 00198 { 00199 if(nIndex < strlen(caComando)) 00200 { 00201 caOldComando[nIndex] = caComando[nIndex]; 00202 } 00203 else 00204 { 00205 // filling della stringa di comando 00206 caOldComando[nIndex] = 0; 00207 } 00208 } 00209 } 00210 } 00211 } 00212 index += (pAdvData->length + 1); 00213 } 00214 } 00215 //*************************************** 00216 // CallBack avviata su BLEInitError 00217 //*************************************** 00218 void onBleInitError(BLE &ble, ble_error_t error) 00219 { 00220 /* Initialization error handling should go here */ 00221 } 00222 00223 //*************************************** 00224 // Callback avviata su BLEInitComplete 00225 //*************************************** 00226 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00227 { 00228 BLE& ble = params->ble; 00229 ble_error_t error = params->error; 00230 00231 if (error != BLE_ERROR_NONE) 00232 { 00233 onBleInitError(ble, error); 00234 return; 00235 } 00236 00237 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) 00238 { 00239 return; 00240 } 00241 00242 ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */); 00243 ble.gap().startScan(advertisementCallback); 00244 } 00245 //************************************************** 00246 // Callback avviata su scheduleBleEventsPtocessing 00247 //************************************************** 00248 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) 00249 { 00250 BLE &ble = BLE::Instance(); 00251 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00252 } 00253 00254 /************/ 00255 /* MAIN */ 00256 /************/ 00257 int main() 00258 { 00259 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto 00260 pc.baud(921600); //921600 bps// configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto 00261 pc.printf("Hallo\r\n"); 00262 00263 // inizializza variabili 00264 for(int i=0; i < MAXNUMCHAR; i++) 00265 { 00266 caComando[i]=0; 00267 caOldComando[i]=0; 00268 } 00269 00270 eventQueue.call_every(100, periodicCallback); 00271 00272 BLE &ble = BLE::Instance(); 00273 ble.onEventsToProcess(scheduleBleEventsProcessing); 00274 ble.init(bleInitComplete); 00275 00276 eventQueue.dispatch_forever(); 00277 00278 return 0; 00279 00280 }
Generated on Tue Jul 12 2022 14:15:17 by
1.7.2