#include "TestCommon.h"
#include "BasicTCPSocket.h"
#include "StringHelper.h"
#include "TimeoutType.h"

namespace MARTe {

const char8 * const debug_test_config = 
"DebugService = {"
"    Class = DebugService "
"    ControlPort = 8095 "
"    UdpPort = 8096 "
"    StreamIP = \"127.0.0.1\" "
"}"
"App = {"
"    Class = RealTimeApplication "
"    +Functions = {"
"        Class = ReferenceContainer "
"        +GAM1 = {"
"            Class = IOGAM "
"            InputSignals = {"
"                Counter = { DataSource = Timer Type = uint32 Frequency = 1000 }"
"            }"
"            OutputSignals = {"
"                Counter = { DataSource = DDB Type = uint32 }"
"            }"
"        }"
"        +GAM2 = {"
"            Class = IOGAM "
"            InputSignals = {"
"                Counter = { DataSource = TimerSlow Type = uint32 Frequency = 10 }"
"            }"
"            OutputSignals = {"
"                Counter = { DataSource = Logger Type = uint32 }"
"            }"
"        }"
"    }"
"    +Data = {"
"        Class = ReferenceContainer "
"        DefaultDataSource = DDB "
"        +Timer = { Class = LinuxTimer SleepTime = 1000 Signals = { Counter = { Type = uint32 } } }"
"        +TimerSlow = { Class = LinuxTimer SleepTime = 100000 Signals = { Counter = { Type = uint32 } } }"
"        +Logger = { Class = LoggerDataSource Signals = { Counter = { Type = uint32 } } }"
"        +DDB = { Class = GAMDataSource Signals = { Counter = { Type = uint32 } } }"
"        +DAMS = { Class = TimingDataSource }"
"    }"
"    +States = {"
"        Class = ReferenceContainer "
"        +State1 = { Class = RealTimeState +Threads = { Class = ReferenceContainer +Thread1 = { Class = RealTimeThread Functions = {GAM1 GAM2} } } }"
"    }"
"    +Scheduler = { Class = GAMScheduler TimingDataSource = DAMS }"
"}";

bool SendCommandGAM(uint16 port, const char8* cmd, StreamString &reply) {
    BasicTCPSocket client;
    if (!client.Open()) return false;
    if (!client.Connect("127.0.0.1", port)) return false;
    
    uint32 s = StringHelper::Length(cmd);
    if (!client.Write(cmd, s)) return false;
    
    char buffer[16384];
    uint32 size = 16384;
    TimeoutType timeout(5000);
    if (client.Read(buffer, size, timeout)) {
        reply.Write(buffer, size);
        client.Close();
        return true;
    }
    client.Close();
    return false;
}

}
