B4R Question [inline-c] how to receive several strings\arrays from C-code ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Working with BLE devices it needs to get the device: name, mac and current string value of it.

How to pass such struct back to B4R ?

Module name 'espbleclient':
Private Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private result() As Byte   'single returning value
    Type bleServerType (name() As Byte, mac() As Byte, value() As Byte)
    Private curserver As bleServerType
End Sub

#if c
....
        String res = (String)myDevice->getName().c_str()
        byte b1[res.length() + 1];
           res.getBytes(b1, sizeof(b1));
        b4r_espbleclient::_result->data = b1;            ;    //THIS PASSED TO B4R OK !
        b4r_espbleclient::_result->length = sizeof(b1);
     
        //BUT NEED TO PASS the struct
     
        _bleservertype* tmp = (_bleservertype*)b4r_espbleclient::_curserver;
        tmp->name = (B4R::B4RString*)res;                                                               // error
        tmp->mac = myDevice->getAddress().toString().c_str();
        tmp->value = value.c_str();
     
        b4r_espbleclient::_server_found();

How to get back whole the type var "curserver" CORRECTLY ?

Task is collect the info of the device list, to check them in a loop.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
If to try to pass like this - compilation is OK, but MCU rebooted due to an error

B4X:
        _bleservertype* tmp = (_bleservertype*)b4r_espbleclient::_curserver;

         // REBOOT IS HERE !

        byte b1[res.length() + 1];  
        res.getBytes(b1, sizeof(b1));  
        tmp->name->data = b1;
       
        byte b2[mac.length() + 1];
        mac.getBytes(b2, sizeof(b2));
        tmp->mac->data = b2;
               
        byte b3[valstr.length() + 1];
        mac.getBytes(b3, sizeof(b3));
        tmp->value->data = b3;
       
        b4r_espbleclient::_server_found();
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You always need to consider the place where the array was allocated.
In this case it was allocated on the stack so it becomes invalid once this code execution completes.
You will need to copy it to the heap for it to remain valid. It is a bit complicated.

The code in ByteConverter.cpp and rESP8266WiFi.cpp can help.
 
Upvote 0
Top