#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "bsp.h"
#include "my.h"
#define BIN_TRACE                   0
#define SOCKET_TRACE                1
#define ESC                         0x1B
#define kbmap(c)                  ((c) - '0')
#define SIZEOF_EP0STO               64
#define SIZEOF_EP0_BLOCK            4
#define SIZEOF_EP1STO               32
#define SIZEOF_EP1_BLOCK            8
rui8_t running;
static DWORD tick_msec;         
static rui8_t ep0sto[SIZEOF_EP0STO],
              ep1sto[SIZEOF_EP1STO];
#if BIN_TRACE == 1      
static FILE *ftbin;
#endif
#if SOCKET_TRACE == 1   
    #include "tcptrc.h"
    
    #define TRC_IP_ADDR                 "127.0.0.1"
    
    #define TRC_TCP_PORT                6602
    
    static SOCKET tsock;
    #define TCP_TRACE_OPEN() \
    if (tcp_trace_open(TRC_TCP_PORT, \
                       TRC_IP_ADDR, &tsock) < 0) \
    { \
        printf("Can't open socket %s:%u\n", \
               TRC_IP_ADDR, TRC_TCP_PORT); \
        exit(EXIT_FAILURE); \
    }
    #define TCP_TRACE_CLOSE() \
    tcp_trace_close(tsock)
    #define TCP_TRACE_SEND(d) \
    tcp_trace_send(tsock, d)
#else
    #define TCP_TRACE_OPEN()    (void)0
    #define TCP_TRACE_CLOSE()   (void)0
    #define TCP_TRACE_SEND(d)   (void)0
#endif
#if BIN_TRACE == 1
    #define FTBIN_FLUSH(d) \
        fwrite (d, 1, 1, ftbin); \
        fflush(ftbin)
    #define FTBIN_CLOSE() \
        fclose(ftbin)
    #define FTBIN_OPEN() \
        if ((ftbin = fopen("../ftbin", "w+b")) == NULL) \
        { \
            perror("Can't open file\n"); \
            exit(EXIT_FAILURE); \
        }
#else
    #define FTBIN_FLUSH(d)      (void)0
    #define FTBIN_CLOSE()       (void)0
    #define FTBIN_OPEN()        (void)0
#endif
static DWORD WINAPI
isr_tmr_thread(LPVOID par)      
{
    (void)par;
    while (running)
    {
        Sleep(tick_msec);
    }
    return 0;
}
static DWORD WINAPI
isr_kbd_thread(LPVOID par)      
{
    int c;
    MyEvt *mye;
    (void)par;
    while (running)
    {
        c = _getch();
        if (c == ESC)
        {
        }
        else
        {
            mye->ts = (rui16_t)rand();
        }
    }
    return 0;
}
static void
print_banner(void)
{
    printf("Abstract Hierarchical State Machine (AHSM) example\n\n");
    printf("Port version     = %s\n", rkh_get_port_version());
    printf("Port description = %s\n\n", rkh_get_port_desc());
    printf("Description: \n\n"
           "The goal of this demo application is to explain how to \n"
           "represent a state machine using the RKH framework. To do \n"
           "that is proposed a simple and abstract example, which is \n"
           "shown in the documentation file Figure 1 section \n"
           "\"Representing a State Machine\". \n\n\n");
    printf("1.- Press <numbers> to send events to state machine. \n");
    printf("2.- Press ESC to quit \n\n\n");
}
void
{
    DWORD thtmr_id, thkbd_id;
    HANDLE hth_tmr, hth_kbd;
    
    tick_msec = 1000UL / BSP_TICKS_PER_SEC;
    running = (rui8_t)1;
    
    hth_tmr = CreateThread(NULL, 1024, &isr_tmr_thread, 0, 0, &thtmr_id);
    SetThreadPriority(hth_tmr, THREAD_PRIORITY_TIME_CRITICAL);
    
    hth_kbd = CreateThread(NULL, 1024, &isr_kbd_thread, 0, 0, &thkbd_id);
    SetThreadPriority(hth_kbd, THREAD_PRIORITY_NORMAL);
    rkh_fwk_epool_register(ep0sto, SIZEOF_EP0STO, SIZEOF_EP0_BLOCK);
    rkh_fwk_epool_register(ep1sto, SIZEOF_EP1STO, SIZEOF_EP1_BLOCK);
}
void
{
}
void
{
    RKH_WAIT_FOR_EVENTS();      
}
void
{
}
void
{
    fprintf(stderr, "RKH_ASSERT: [%d] line from %s "
            "file\n", line, file);
    __debugbreak();
}
#if RKH_CFG_TRC_EN == 1
void
{
    FTBIN_OPEN();
    TCP_TRACE_OPEN();
}
void
{
    FTBIN_CLOSE();
    TCP_TRACE_CLOSE();
}
{
}
void
{
    rui8_t *d;
    {
        FTBIN_FLUSH(d);
        TCP_TRACE_SEND(*d);
    }
}
#endif
void
bsp_init(int argc, char *argv[])
{
    (void)argc;
    (void)argv;
    srand((unsigned)time(NULL));
    print_banner();
    
}