Releasing receive buffer memory on Lora Radio event.

Home Forums mDot/xDot Releasing receive buffer memory on Lora Radio event.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17281
    Tom Hill
    Participant

    I have subscribed to RadioEvents when Initializing the LORA communication on my mDot using the mdot api method as shown below.

    //subscribe to the LORA Events.
    m_dot->setEvents(this);

    When I had discussed about subscribing this event a while back in this forum I was told to ensure to release the recv buffer on completion of the recv event. However since I have migrated to mbed 5, if try to release the recv buffer (uint8_t*), the mdot hangs when the call returns back to my main thread. If I stop releasing the recv buffer, everything works well. So I am hoping this is not an issue, because I don’t want to create a memory leak in my application, which would eventually kill my mdot application? In the sample code on the mbed web site I noticed that this recv buffer is not released. So before I go with this approach I am hoping to get confirmation from the Multitech developers, its okay not release the recv buffer?

    This is my current code….

    void TestLora::MacEvent(LoRaMacEventFlags* flags, LoRaMacEventInfo* info) {
        std::string msg = "OK";
        switch (info->Status) {
            case LORAMAC_EVENT_INFO_STATUS_OK:
                m_TransmissionDoneOrTimedOut = true;
                break;
            case LORAMAC_EVENT_INFO_STATUS_ERROR:
                msg = "ERROR";
                break;
            case LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT:
               m_TransmissionDoneOrTimedOut = true; 
                msg = "TX_TIMEOUT";
                break;
            case LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT:
                m_bReceiveDoneOrTimedOut = true;
                msg = "RX_TIMEOUT";
                break;
            case LORAMAC_EVENT_INFO_STATUS_RX_ERROR:
                m_bReceiveDoneOrTimedOut = true;
                msg = "RX_ERROR";
                break;
            case LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL:
                msg = "JOIN_FAIL";
                break;
            case LORAMAC_EVENT_INFO_STATUS_DOWNLINK_FAIL:
                m_bReceiveDoneOrTimedOut = true;
                msg = "DOWNLINK_FAIL";
                break;
            case LORAMAC_EVENT_INFO_STATUS_ADDRESS_FAIL:
                msg = "ADDRESS_FAIL";
                break;
            case LORAMAC_EVENT_INFO_STATUS_MIC_FAIL:
                msg = "MIC_FAIL";
                break;
            default:
                break;
        }
        
        if (mts::MTSLog::getLogLevel() == mts::MTSLog::TRACE_LEVEL) {
            
            logTrace("Event: %s", msg.c_str());
        
            logTrace("Flags Tx: %d Rx: %d RxData: %d RxSlot: %d LinkCheck: %d JoinAccept: %d",
                     flags->Bits.Tx, flags->Bits.Rx, flags->Bits.RxData, flags->Bits.RxSlot, flags->Bits.LinkCheck, flags->Bits.JoinAccept);
            logTrace("Info: Status: %d ACK: %d Retries: %d TxDR: %d RxPort: %d RxSize: %d RSSI: %d SNR: %d Energy: %d Margin: %d Gateways: %d",
                     info->Status, info->TxAckReceived, info->TxNbRetries, info->TxDatarate, info->RxPort, info->RxBufferSize,
                     info->RxRssi, info->RxSnr, info->Energy, info->DemodMargin, info->NbGateways);
        }
    
        if (flags->Bits.Rx) {
            //set this flag to true.
            m_bReceiveDoneOrTimedOut = true;
            logDebug("Rx %d bytes", info->RxBufferSize);
            
            if (info->RxBufferSize > 0 && info->RxBuffer != NULL) {
                
                //queue the downlink packet for processing.
                TestMDot* pTestMDot = TestMDot::getInstance();
                if(pTestMDot != NULL) {
                    pTestMDot->QueueDownlinkPacket(info->RxBuffer,info->RxBufferSize);
                }
                
                //logDebug("Releasing Memory....");
                //delete[] info->RxBuffer;
                //info->RxBuffer = NULL;
            }
        }
    }
    

    Thanks,
    Yogesh

    #17288
    Mike Fiore
    Blocked

    Yogesh,

    The implementation has changed so that consumers of the API no longer need to free the memory. You’ll notice that the mDotEvent object has its own buffer and that the RX data from the event gets copied into that. So there isn’t any dynamic allocation WRT the RX data that you need to worry about.

    Cheers,
    Mike

    #17346
    Tom Hill
    Participant

    Thanks Mike for confirming. That helps a lot, I guess I didn’t notice the buffer on the mDotEvent object.

    Thanks,
    Yogesh

    #19863
    Momo SMITH
    Participant

    Hello guys,
    I have a problem with the class RadioEvent.

    if (flags->Bits.Rx) {
    logDebug(“Rx %d bytes”, info->RxBufferSize);
    if (info->RxBufferSize > 0) {
    // print RX data as string and hexadecimal
    std::string rx((const char*)info->RxBuffer, info->RxBufferSize);
    printf(“Rx data: %s [%s]\r\n”, rx.c_str(), mts::Text::bin2hexString(info->RxBuffer, info->RxBufferSize).c_str());
    }
    }

    When my gateway send a message without caractere null, the code work but when there is a caractere null, the first %s doesn’t post a complet message and then the second %s post a complet message in hex.
    I want receive a string message. How i can resolve this problem to receive a complet message in string ?

    Thanks you

    #19865
    Jason Reiss
    Keymaster

    Use memcpy

    #19869
    Jason Reiss
    Keymaster

    Or if you want to output all characters to serial then use putc for each or use Serial object write function.

    String functions such as printf will stop at first NULL.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.