More SRCH Response Parsing

This commit is contained in:
Florian Märkl 2019-08-11 20:53:48 +02:00
commit 1ad1cf3db8
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 76 additions and 8 deletions

View file

@ -60,6 +60,41 @@ static int parse_opt(int key, char *arg, struct argp_state *state)
static struct argp argp = { options, parse_opt, 0, doc, 0, 0, 0 }; static struct argp argp = { options, parse_opt, 0, doc, 0, 0, 0 };
static void discovery_cb(ChiakiDiscoveryHost *host, void *user)
{
ChiakiLog *log = user;
CHIAKI_LOGI(log, "--");
CHIAKI_LOGI(log, "Discovered Host:");
CHIAKI_LOGI(log, "State: %s", chiaki_discovery_host_state_string(host->state));
if(host->system_version)
CHIAKI_LOGI(log, "System Version: %s", host->system_version);
if(host->device_discovery_protocol_version)
CHIAKI_LOGI(log, "Device Discovery Protocol Version: %s", host->device_discovery_protocol_version);
if(host->host_request_port)
CHIAKI_LOGI(log, "Request Port: %hu", (unsigned short)host->host_request_port);
if(host->host_name)
CHIAKI_LOGI(log, "Host Name: %s", host->host_name);
if(host->host_type)
CHIAKI_LOGI(log, "Host Type: %s", host->host_type);
if(host->host_id)
CHIAKI_LOGI(log, "Host ID: %s", host->host_id);
if(host->running_app_titleid)
CHIAKI_LOGI(log, "Running App Title ID: %s", host->running_app_titleid);
if(host->running_app_name)
CHIAKI_LOGI(log, "Running App Name: %s%s", host->running_app_name, (strcmp(host->running_app_name, "Persona 5") == 0 ? " (best game ever)" : ""));
CHIAKI_LOGI(log, "--");
}
CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[]) CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[])
{ {
Arguments arguments = { 0 }; Arguments arguments = { 0 };
@ -82,7 +117,7 @@ CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[]
} }
ChiakiDiscoveryThread thread; ChiakiDiscoveryThread thread;
err = chiaki_discovery_thread_start(&thread, &discovery); err = chiaki_discovery_thread_start(&thread, &discovery, discovery_cb, NULL);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
{ {
CHIAKI_LOGE(log, "Discovery thread init failed"); CHIAKI_LOGE(log, "Discovery thread init failed");

View file

@ -51,7 +51,9 @@ typedef enum chiaki_discovery_host_state_t
CHIAKI_DISCOVERY_HOST_STATE_STANDBY CHIAKI_DISCOVERY_HOST_STATE_STANDBY
} ChiakiDiscoveryHostState; } ChiakiDiscoveryHostState;
typedef struct chiaki_discovery_srch_response_t const char *chiaki_discovery_host_state_string(ChiakiDiscoveryHostState state);
typedef struct chiaki_discovery_host_t
{ {
ChiakiDiscoveryHostState state; ChiakiDiscoveryHostState state;
const char *system_version; const char *system_version;
@ -60,7 +62,9 @@ typedef struct chiaki_discovery_srch_response_t
const char *host_name; const char *host_name;
const char *host_type; const char *host_type;
const char *host_id; const char *host_id;
} ChiakiDiscoverySrchResponse; const char *running_app_titleid;
const char *running_app_name;
} ChiakiDiscoveryHost;
CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, ChiakiDiscoveryPacket *packet); CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, ChiakiDiscoveryPacket *packet);
@ -75,14 +79,18 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
CHIAKI_EXPORT void chiaki_discovery_fini(ChiakiDiscovery *discovery); CHIAKI_EXPORT void chiaki_discovery_fini(ChiakiDiscovery *discovery);
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery, ChiakiDiscoveryPacket *packet, struct sockaddr *addr, size_t addr_size); CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery, ChiakiDiscoveryPacket *packet, struct sockaddr *addr, size_t addr_size);
typedef void (*ChiakiDiscoveryCb)(ChiakiDiscoveryHost *host, void *user);
typedef struct chiaki_discovery_thread_t typedef struct chiaki_discovery_thread_t
{ {
ChiakiDiscovery *discovery; ChiakiDiscovery *discovery;
ChiakiThread thread; ChiakiThread thread;
ChiakiStopPipe stop_pipe; ChiakiStopPipe stop_pipe;
ChiakiDiscoveryCb cb;
void *cb_user;
} ChiakiDiscoveryThread; } ChiakiDiscoveryThread;
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery); CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery, ChiakiDiscoveryCb cb, void *cb_user);
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread); CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_stop(ChiakiDiscoveryThread *thread);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -25,6 +25,18 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
const char *chiaki_discovery_host_state_string(ChiakiDiscoveryHostState state)
{
switch(state)
{
case CHIAKI_DISCOVERY_HOST_STATE_READY:
return "ready";
case CHIAKI_DISCOVERY_HOST_STATE_STANDBY:
return "standby";
default:
return "unknown";
}
}
CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, ChiakiDiscoveryPacket *packet) CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, ChiakiDiscoveryPacket *packet)
{ {
@ -39,7 +51,7 @@ CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, Chiaki
} }
} }
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_srch_response_parse(ChiakiDiscoverySrchResponse *response, char *buf, size_t buf_size) CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_srch_response_parse(ChiakiDiscoveryHost *response, char *buf, size_t buf_size)
{ {
ChiakiHttpResponse http_response; ChiakiHttpResponse http_response;
ChiakiErrorCode err = chiaki_http_response_parse(&http_response, buf, buf_size); ChiakiErrorCode err = chiaki_http_response_parse(&http_response, buf, buf_size);
@ -63,7 +75,6 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_srch_response_parse(ChiakiDiscove
for(ChiakiHttpHeader *header = http_response.headers; header; header=header->next) for(ChiakiHttpHeader *header = http_response.headers; header; header=header->next)
{ {
printf("%s: %s\n", header->key, header->value);
if(strcmp(header->key, "system-version") == 0) if(strcmp(header->key, "system-version") == 0)
response->system_version = header->value; response->system_version = header->value;
else if(strcmp(header->key, "device-discovery-protocol-version") == 0) else if(strcmp(header->key, "device-discovery-protocol-version") == 0)
@ -76,6 +87,12 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_srch_response_parse(ChiakiDiscove
response->host_type = header->value; response->host_type = header->value;
else if(strcmp(header->key, "host-id") == 0) else if(strcmp(header->key, "host-id") == 0)
response->host_id = header->value; response->host_id = header->value;
else if(strcmp(header->key, "running-app-titleid") == 0)
response->running_app_titleid = header->value;
else if(strcmp(header->key, "running-app-name") == 0)
response->running_app_name = header->value;
//else
// printf("unknown %s: %s\n", header->key, header->value);
} }
chiaki_http_response_fini(&http_response); chiaki_http_response_fini(&http_response);
@ -154,9 +171,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery,
static void *discovery_thread_func(void *user); static void *discovery_thread_func(void *user);
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery) CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_thread_start(ChiakiDiscoveryThread *thread, ChiakiDiscovery *discovery, ChiakiDiscoveryCb cb, void *cb_user)
{ {
thread->discovery = discovery; thread->discovery = discovery;
thread->cb = cb;
thread->cb_user = cb_user;
ChiakiErrorCode err = chiaki_stop_pipe_init(&thread->stop_pipe); ChiakiErrorCode err = chiaki_stop_pipe_init(&thread->stop_pipe);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
@ -224,10 +243,16 @@ static void *discovery_thread_func(void *user)
CHIAKI_LOGV(discovery->log, "Discovery received:\n%s", buf); CHIAKI_LOGV(discovery->log, "Discovery received:\n%s", buf);
chiaki_log_hexdump_raw(discovery->log, CHIAKI_LOG_VERBOSE, (const uint8_t *)buf, n); chiaki_log_hexdump_raw(discovery->log, CHIAKI_LOG_VERBOSE, (const uint8_t *)buf, n);
ChiakiDiscoverySrchResponse response; ChiakiDiscoveryHost response;
err = chiaki_discovery_srch_response_parse(&response, buf, n); err = chiaki_discovery_srch_response_parse(&response, buf, n);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGI(discovery->log, "Discovery Response invalid"); CHIAKI_LOGI(discovery->log, "Discovery Response invalid");
continue;
}
if(thread->cb)
thread->cb(&response, thread->cb_user);
} }
return NULL; return NULL;