summaryrefslogtreecommitdiff
path: root/source/debug.c
blob: f2972383223fa46d35a61a0420240fd36315385d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    3dsspec - Nintendo 3DS Spectrum emulator

    Copyright (C) 2021  Ian Cowburn <ianc@noddybox.co.uk>

    This program 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 program 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 program; if not, see <http://www.gnu.org/licenses/>

    -------------------------------------------------------------------------

    Provides an interface for debug

*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <3ds.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>

#include "debug.h"
#include "gui.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/* ---------------------------------------- GLOBAL EXTERNALS
*/
int                     debug_enabled = FALSE;

/* ---------------------------------------- PRIVATE DAYA
*/
static char     debug_host[256];
static char     debug_port[32];
static int      sock = -1;

/* ---------------------------------------- PRIVATE FUNCTIONS
*/
static void Error(const char *format, ...)
{
    static char buff[4096];
    va_list va;

    va_start(va, format);
    vsnprintf(buff, 4096, format, va);
    buff[4095] = 0;
    va_end(va);

    GUI_Alert(FALSE, buff);
}

static void OpenDebugConnection(void)
{
    if (sock == -1)
    {
        struct addrinfo *addr, *addr_p;
        struct addrinfo hints = {0};

        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(debug_host, debug_port, &hints, &addr) == 0)
        {
            for(addr_p = addr; addr_p && sock == -1; addr_p = addr_p->ai_next)
            {
                sock = socket(addr_p->ai_family,
                              addr_p->ai_socktype,
                              addr_p->ai_protocol);

                if (sock != -1)
                {
                    if (connect(sock, addr_p->ai_addr, addr_p->ai_addrlen)
                                                                        == -1)
                    {
                        Error("Failed to connect socket");
                        close(sock);
                        sock = -1;
                    }
                }
                else
                {
                    Error("Failed to create socket(%d,%d,%d)\n%d %s",
                                                        addr_p->ai_family,
                                                        addr_p->ai_socktype,
                                                        addr_p->ai_protocol,
                                                        errno,
                                                        strerror(errno));
                }
            }
        }
        else
        {
            Error("Failed to getaddrinfo(%s,%s)", debug_host, debug_port);
        }
    }
}

/* ---------------------------------------- EXPORTED INTERFACES
*/
void DEBUG_SetAddress(const char *host, const char *port)
{
    strncpy(debug_host, host, (sizeof debug_host) - 1);
    strncpy(debug_port, port, (sizeof debug_port) - 1);

    debug_host[(sizeof debug_host) - 1] = 0;
    debug_port[(sizeof debug_port) - 1] = 0;

    if (sock != -1)
    {
        close(sock);
        sock = -1;
    }
}


void DEBUG_Output(const char *format, ...)
{
    static char buff[4096];

    if (debug_enabled)
    {
        va_list va;

        OpenDebugConnection();

        va_start(va, format);
        vsnprintf(buff, 4096, format, va);
        buff[4095] = 0;
        va_end(va);

        if (sock != -1)
        {
            send(sock, buff, strlen(buff), 0);
        }
    }
}


/* END OF FILE */