iBeacon primo test

Files at this revision

API Documentation at this revision

Comitter:
pinofal
Date:
Tue Sep 08 05:52:44 2020 +0000
Parent:
42:092c08942a29
Commit message:
iBeacon primo test

Changed in this revision

source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/main.cpp	Fri Sep 08 14:45:27 2017 +0100
+++ b/source/main.cpp	Tue Sep 08 05:52:44 2020 +0000
@@ -1,45 +1,52 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+// test con STM32F401 + IDB05A1.
+// App utilizzata per i test TxEddystone-URL
 
 #include <events/mbed_events.h>
 #include "mbed.h"
 #include "ble/BLE.h"
+#include "ble/DiscoveredCharacteristic.h"
+#include "ble/DiscoveredService.h"
+
+#define MAXNUMCHAR 255 // massima dimensione del comando inviato nell'URI
 
 static const int URI_MAX_LENGTH = 18;             // Maximum size of service data in ADV packets
 
 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
 
-DigitalOut led1(LED1, 1);
+// genera un oggetto serial collegato al PC
+Serial pc(USBTX, USBRX);
 
+// comando ricevuto da iBeacon = caratteri tra prefisso e suffisso dell'indirizzo URI
+char caComando[MAXNUMCHAR];
+// ultimo comando ricevuto da iBeacon
+char caOldComando[MAXNUMCHAR];    
+
+DigitalOut led1(LED1, 1); // LED su scheda
+  
 void periodicCallback(void)
 {
     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
 }
 
+//****************************************
+// Decodifica URI ricevuto da Eddystone
+//****************************************
 void decodeURI(const uint8_t* uriData, const size_t uriLen)
 {
-    const char *prefixes[] = {
+   
+    const char *prefixes[] = 
+    {
         "http://www.",
         "https://www.",
         "http://",
         "https://",
         "urn:uuid:"
     };
+        
     const size_t NUM_PREFIXES = sizeof(prefixes) / sizeof(char *);
-    const char *suffixes[] = {
+    
+    const char *suffixes[] = 
+    {
         ".com/",
         ".org/",
         ".edu/",
@@ -56,43 +63,72 @@
         ".gov"
     };
     const size_t NUM_SUFFIXES = sizeof(suffixes) / sizeof(char *);
-
     size_t index = 0;
+    
+    
+    /*------------- Diagnostica -------------------
+    pc.printf("sizeofprefixes: %d\r\n", sizeof(prefixes));
+    pc.printf("sizeofchar: %d\r\n", sizeof(char *));
+    pc.printf("NUM_PREFIXES: %d\r\n", NUM_PREFIXES);
+    
+    pc.printf("sizeofsuffixes: %d\r\n", sizeof(suffixes));
+    pc.printf("sizeofchar: %d\r\n", sizeof(char *));
+    pc.printf("NUM_SUFFIXES: %d\r\n", NUM_SUFFIXES);
+    ------------- Diagnostica -------------------*/
 
-    /* First byte is the URL Scheme. */
-    if (uriData[index] < NUM_PREFIXES) {
-        printf("%s", prefixes[uriData[index]]);
+    //pc.printf("uriLen: %d\r\n", uriLen); // diagnostica
+    //pc.printf("uriData: %s\r\n", uriData); // diagnostica
+
+    //------------- Diagnostica -------------------
+    // il primo carattere contiene l'indice dell'array dei prefissi: p.e. indice 2 significa prefixes[2] = "http://"
+    if (uriData[index] < NUM_PREFIXES) 
+    {
+        // stampa prefisso//
+        //pc.printf("%s", prefixes[uriData[index]]); // diagnostica
         index++;
-    } else {
-        printf("URL Scheme was not encoded!");
+    } 
+    else 
+    {
+        // il prefisso non è tra quelli codificati 
+        pc.printf("URL Scheme was not encoded!");
         return;
     }
-
-    /* From second byte onwards we can have a character or a suffix */
-    while(index < uriLen) {
-        if (uriData[index] < NUM_SUFFIXES) {
-            printf("%s", suffixes[uriData[index]]);
-        } else {
-            printf("%c", uriData[index]);
-        }
+    pc.printf("\n\r");     
+    // Inizializza array comando
+    for(index = 0; index < MAXNUMCHAR; index++) 
+    {
+        caComando[index] = 0;
+    }
+    index=1;
+    // estrai il comando tra indice = 1 e indice = uriLen-1
+    while(index < (uriLen-1)) 
+    {
+        // stampa il dato tra prefisso e suffisso
+        //pc.printf("%c", uriData[index]); // Diagnostica
+        caComando[index-1]= uriData[index];
         index++;
     }
-
-    printf("\n\r");
+    pc.printf("\n\r");
+    
+    // stampa suffisso che si trova all'indice(uriLen-1)
+    //pc.printf("%s\n\r", suffixes[uriData[uriLen-1]]); // Diagnostica
+    //------------- Diagnostica -------------------
 }
 
-/*
- * This function is called every time we scan an advertisement.
- */
+//**************************************************************
+// This function is called every time we scan an advertisement
+//**************************************************************
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-    struct AdvertisingData_t {
+    struct AdvertisingData_t 
+    {
         uint8_t                        length; /* doesn't include itself */
         GapAdvertisingData::DataType_t dataType;
         uint8_t                        data[1];
     } AdvDataPacket;
 
-    struct ApplicationData_t {
+    struct ApplicationData_t 
+    {
         uint8_t applicationSpecificId[2];
         uint8_t frameType;
         uint8_t advPowerLevels;
@@ -100,55 +136,138 @@
     } 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);
-
+    const uint8_t FRAME_TYPE_URL = 0x10;
+    const uint8_t APPLICATION_DATA_OFFSET = sizeof(ApplicationData_t) + sizeof(AdvDataPacket.dataType) - sizeof(AppDataPacket.uriData);
+    int nIndex;
     AdvertisingData_t *pAdvData;
     size_t index = 0;
-    while(index < params->advertisingDataLen) {
+    
+    // parsing dell'Advertising ricevuto
+    while(index < params->advertisingDataLen) 
+    {
         pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
-        if (pAdvData->dataType == GapAdvertisingData::SERVICE_DATA) {
+        //+++pc.printf("pAdv DataType= %x\r\n",pAdvData->dataType); // Diagnostica
+
+        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)) {
+            
+            //+++pc.printf("pAppData ApplicationSpecificID= %x\r\n",pAppData->applicationSpecificId); // Diagnostica
+            //+++pc.printf("pAppData frameType= %x\r\n",pAppData->frameType); // Diagnostica
+            //pc.printf("------------------------------> Sono arrivato qui\r\n"); // Diagnostica
+            if (!memcmp(pAppData->applicationSpecificId, BEACON_UUID, sizeof(BEACON_UUID))
+                                             && 
+                            (pAppData->frameType == FRAME_TYPE_URL)) 
+            {
                 decodeURI(pAppData->uriData, pAdvData->length - APPLICATION_DATA_OFFSET);
-                break;
+                
+                pc.printf("-------------------------------------------------> Sono partito da qui\r\n");
+                // genera messaggio vocale se il comando ricevuto è diverso da quello ricevuto precedentemente
+                //pc.printf("caComando: %s\r\n", caComando); //Diagnostica
+                //pc.printf("caOldComando: %s\r\n", caOldComando); //Diagnostica
+                if(strcmp(caComando,caOldComando) != 0)
+                {
+                    if(strcmp(caComando,"prova") == 0)
+                    {
+                        pc.printf("Ricevuto Comando PROVA\r\n");
+                    }
+                    
+                    if(strcmp(caComando,"welcome") == 0)
+                    {
+                        pc.printf("Ricevuto Comando WELCOME\r\n"); // diagnostica
+                    }
+                
+                    if(strcmp(caComando,"uscita") == 0)
+                    {
+                        pc.printf("Ricevuto Comando USCITA\r\n"); // diagnostica
+                    }
+                    
+                    if(strcmp(caComando,"ingresso") == 0)
+                    {
+                        pc.printf("Ricevuto Comando INGRESSO\r\n");
+                    }
+                    
+                    if(strcmp(caComando,"cambioora") == 0)
+                    {
+                        pc.printf("Ricevuto Comando CAMBIO ORA\r\n");
+                    }
+                    
+                    
+                    // copia caComando in caOldComando
+                    for (nIndex = 0; nIndex < MAXNUMCHAR; nIndex++)
+                    {
+                        if(nIndex < strlen(caComando))
+                        {
+                            caOldComando[nIndex] = caComando[nIndex];
+                        }
+                        else
+                        {
+                            // filling della stringa di comando
+                            caOldComando[nIndex] = 0;
+                        }
+                    }
+                }    
             }
         }
         index += (pAdvData->length + 1);
     }
 }
-
+//***************************************
+// CallBack avviata su BLEInitError
+//***************************************
 void onBleInitError(BLE &ble, ble_error_t error)
 {
    /* Initialization error handling should go here */
 }
 
+//***************************************
+// Callback avviata su BLEInitComplete
+//***************************************
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
     BLE&        ble   = params->ble;
     ble_error_t error = params->error;
 
-    if (error != BLE_ERROR_NONE) {
+    if (error != BLE_ERROR_NONE) 
+    {
         onBleInitError(ble, error);
         return;
     }
 
-    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) 
+    {
         return;
     }
 
     ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
     ble.gap().startScan(advertisementCallback);
 }
-
-void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
+//**************************************************
+// Callback avviata su scheduleBleEventsPtocessing
+//**************************************************
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) 
+{
     BLE &ble = BLE::Instance();
     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
 }
 
+/************/
+/*   MAIN   */
+/************/
 int main()
 {
-    eventQueue.call_every(500, periodicCallback);
+    // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto
+    pc.baud(921600); //921600 bps// configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto
+    pc.printf("Hallo\r\n");
+
+    // inizializza variabili
+    for(int i=0; i < MAXNUMCHAR; i++)
+    {
+        caComando[i]=0;
+        caOldComando[i]=0;
+    }
+
+    eventQueue.call_every(100, periodicCallback);
 
     BLE &ble = BLE::Instance();
     ble.onEventsToProcess(scheduleBleEventsProcessing);
@@ -157,4 +276,5 @@
     eventQueue.dispatch_forever();
 
     return 0;
-}
+
+}
\ No newline at end of file