Understand a library and print the value of a struct

Hello everybody, I’m a rails developer and I have basic knowledge of arduino and c/c++

I 'm writing an alarm system using the Linkit On as board and some bluetooth LE sensors, and to capture the info broadecasted by the sensor I’m using a library.

Here is a portion of the library with the function I’m trying to change:

int SMSGateway::buttonsPressed(int num)
{
  LGATTDeviceInfo info = {0};
   typeBLEInfo mInfo;
   int numberAlert = 0;
   for (int i = 0; i < num; i++)
   {
      c.getScanResult(i, info);

      #ifdef _DEBUG_INFO
      Serial.printf("[LGATTC ino]dev address : [%x:%x:%x:%x:%x:%x] rssi [%d]", 
      info.bd_addr.addr[5], info.bd_addr.addr[4], info.bd_addr.addr[3], info.bd_addr.addr[2], info.bd_addr.addr[1], info.bd_addr.addr[0],
      info.rssi);
      Serial.println();
      #endif

      int lenAdvertData = info.eir[8];
      memcpy(&mInfo, &info.eir[9], lenAdvertData - 1);
      memcpy(&mInfo.nameBLE, &info.eir[9 + lenAdvertData + 2], 12); 

      Serial.printf(mInfo.uuid);
      if(memcmp(myUuid, mInfo.uuid, 16 ) == 0){

          if(mInfo.minor[1] == 0x12 || mInfo.minor[1] == 0x11){
             memcpy(&BLEInfo[numberAlert], &mInfo, 32);
             numberAlert++;

             #ifdef _DEBUG_INFO
             Serial.println("\n<<<<<< Alert >>>>>>\n");
             #endif

          }else if(mInfo.minor[1] == 0x02){

             #ifdef _DEBUG_INFO
             Serial.println("\n<<<<<< Normal >>>>>>\n");
             #endif

          }
      }
   }
  return numberAlert;
}

I’m trying to understand the code and I understand that buttonsPressed is a function of the SMSGateway library and this function return an integer with the amount of alarm triggered.

The function was written by someone that use some bluetooth buttons… I have a sensor (that works as a button but with a different broadcasted value )

Info is an object of type LGATTDeviceInfo, and according to the api library is

typedef struct {
  LGATTAddress bd_addr;
  int32_t rssi;
  uint8_t eir_len;
  uint8_t eir[256];
} LGATTDeviceInfo;

LGATTAddress bd_addr ->Address of Bluetooth 4.0 device. 
int32_t rssi -> Received signal strength indication. 
uint8_t eir_len -> Length of equipment identity register. 
uint8_t eir[256] -> Content of equipment identity register.

mInfo is a typeBLEInfo, and this is declared by the library

 typedef struct {
  byte uuid[16];
  byte major[2];
  byte minor[2];
  byte nameBLE[12];
}typeBLEInfo;

The function scan every second for BLE devices and return for every device found a LGATTDeviceInfo object.

This serial print return me the objects

Serial.printf("[LGATTC ino]dev address : [%x:%x:%x:%x:%x:%x] rssi [%d]", 
      info.bd_addr.addr[5], info.bd_addr.addr[4], info.bd_addr.addr[3], info.bd_addr.addr[2], info.bd_addr.addr[1], info.bd_addr.addr[0],
      info.rssi);

The address is ok, the rssi (signal strength) is ok

At this point I cannot understand nothing in the code: I must read the “uuid” and the “nameBLE” and I cannot find a way to print these value in console. I need of course to adjust this conditional to trigger the alarm

 if(mInfo.minor[1] == 0x12 || mInfo.minor[1] == 0x11){
             memcpy(&BLEInfo[numberAlert], &mInfo, 32);
             numberAlert++;
            
          }

Any hint?

I must read the "uuid" and the "nameBLE" and I cannot find a way to print these value in console.

If "uuid" means "the uuid member of the minfo object", I can't see why you can't see the data. Reading the data doesn't make a lot of sense. There data is there. Use it.

The same holds true for nameBLE.

As you can see I try to add

Serial.printf(mInfo.uuid);

In the console I have for every device the same 8 bit string...

And also if I use

Serial.printf(mInfo.nameBLE);

Same data printed in console...

:frowning:

As you can see

Well, no, I can't.

I try to add

Where?

Post your code.

int SMSGateway::buttonsPressed(int num)
{
  LGATTDeviceInfo info = {0};
   typeBLEInfo mInfo;
   int numberAlert = 0;
   for (int i = 0; i < num; i++)
   {
      c.getScanResult(i, info);

      #ifdef _DEBUG_INFO
      Serial.printf("[LGATTC ino]dev address : [%x:%x:%x:%x:%x:%x] rssi [%d]", 
      info.bd_addr.addr[5], info.bd_addr.addr[4], info.bd_addr.addr[3], info.bd_addr.addr[2], info.bd_addr.addr[1], info.bd_addr.addr[0],
      info.rssi);
      Serial.println();
      #endif

      int lenAdvertData = info.eir[8];
      memcpy(&mInfo, &info.eir[9], lenAdvertData - 1);
      memcpy(&mInfo.nameBLE, &info.eir[9 + lenAdvertData + 2], 12); 

      Serial.printf(mInfo.uuid);
      if(memcmp(myUuid, mInfo.uuid, 16 ) == 0){

          if(mInfo.minor[1] == 0x12 || mInfo.minor[1] == 0x11){
             memcpy(&BLEInfo[numberAlert], &mInfo, 32);
             numberAlert++;

             #ifdef _DEBUG_INFO
             Serial.println("\n<<<<<< Alert >>>>>>\n");
             #endif

          }else if(mInfo.minor[1] == 0x02){

             #ifdef _DEBUG_INFO
             Serial.println("\n<<<<<< Normal >>>>>>\n");
             #endif

          }
      }
   }
  return numberAlert;
}

Before the if I add

Serial.printf(mInfo.uuid);

Same value for every device.
Is there a way to print mInfo?

Same value for every device.

Which leads me to believe that you are printing data about the receiver, not the transmitter.

Don't know.
Every book I read about Arduino is too basic.
For example I cannot understand the & in front of some variable in the memcpy function...

I try to find an advanced programming book.

For example I cannot understand the & in front of some variable in the memcpy function...

It's the address-of operator.

The memcpy() function needs to know where to copy data to/from. So, you pass it the address of the array to read from/write to.

Ok, I understand my fault.
I read a C++ manual and I understand that eir[256] is an array with 256 element. In ruby the syntax is different so I wrong. I loop in the array and I print the values and now they are different

[LGATTC ino]dev address : [20:c3:8f:86:b5:96] rssi [-75]
2
1
6
3
2
0
160
16
9
82
69
67
65
77
53
50
71
74
88
48
49
50
53
49
5
18
80
0
80
0
2
10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
196
17
31
240
0
0

Now I must understand how can I transform these values into the uuid and the name of my device... :smiley:

Now I must understand how can I transform these values into the uuid and the name of my device

The snippets of code that you posted show you that. The length of the uuid value is in eir[8]. The uuid value starts in eir[9]. The BLEName starts in eir[9 + lenAdvertData + 2], where lenAdvertData is the value in eir[8].

Of course now I understand.
I'm a ruby developer, eir[256] is the item of the array eir at index 256 :smiley:

robypez:
Of course now I understand.
I'm a ruby developer, eir[256] is the item of the array eir at index 256 :smiley:

Yes. Except that eir only has 256 elements, so the index values run from 0 to 255.