/* 
 * This software is licensed under terms of the GNU GENERAL PUBLIC LICENSE
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software  is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Source code is available at http://ppcenter.free.fr
 */

#include "../common/satcom.h"
#include "sc_dummy.h"






////////////////////////////////////////////////////////////////////////
// Derived class functions
////////////////////////////////////////////////////////////////////////



/**
 *  DLL startup : Display help screen.
**/
char SC_dummy::sc_start(void)
{
    scl_log(SCLT_DLLPG, 0, "[DUMMY]Start");
    return SC_NOERROR;
}

/**
 *  Set internal parameter.
**/
char SC_dummy::sc_set(char* parameter, char* value)
{
    scl_log(SCLT_DLLPG, 0, "[DUMMY]Set(%s, %s)", parameter, value);
    return SC_NOERROR;
}

/**
 *  Close USB Data Link.
**/
char SC_dummy::sc_end(void)
{
    scl_log(SCLT_DLLPG, 0, "[DUMMY]End");
    return SC_NOERROR;
}





/**
 *  Receive data from Saturn.
**/
char SC_dummy::sc_receive(unsigned long address, unsigned char* buffer, unsigned long length)
{
    scl_log(SCLT_DLLPG, 0, "[DUMMY]Receive(adr=0x%08X, len=%d)", address, length);

    /* Simulates the download of data. */
    unsigned long psz = (8000*length)/442364;//8000;
    if(psz < 30) psz = 30;
    unsigned long p=0;
    STATUS_CALLBACK(CLBK_START, 0, buffer, 0, length);
    for(p=0; p<length; p++)
    {
        if(rand()%100 > 60)
        {
            buffer[p] = 'a' + rand()%('z' - 'a');
        }
        else if(rand()%100 > 60)
        {
            buffer[p] = 'A' + rand()%('Z' - 'A');
        }
        else
        {
            buffer[p] = '\0';
        }
    }
    for(p=0; p<length; p+=psz)
    {
        scl_log(SCLT_DLLPG, 9, " [RCV]p=%d", p);
        Sleep(80+(rand()%40));
        STATUS_CALLBACK(CLBK_RECEIVE, address, buffer, p, length);
    }
    STATUS_CALLBACK(CLBK_END, 0, buffer, length, length);

    return SC_NOERROR;
}




/**
 *  Send data to Saturn.
**/
char SC_dummy::sc_send(unsigned long address, unsigned char* buffer, unsigned long length, char exec_flag)
{
    scl_log(SCLT_DLLPG, 0, "[DUMMY]Send(adr=0x%08X, len=%d, exec=%d)", address, length, exec_flag);

    /* Simulates the upload of data. */
    unsigned long psz = 8000;
    unsigned long p=0;
    STATUS_CALLBACK(CLBK_START, 0, buffer, 0, length);
    for(p=0; p<length; p+=psz)
    {
        Sleep(80+(rand()%40));
        STATUS_CALLBACK(CLBK_SEND, address, buffer, p, length);
    }
    STATUS_CALLBACK(CLBK_END, 0, buffer, length, length);

    return SC_NOERROR;
}



/**
 *  Return DLL version informations.
**/
char* SC_dummy::sc_version(void)
{
    sprintf(_version, (char*)("SatLink dummy device 0.1"));
    return _version;
}

