diff --git a/cli/src/discover.c b/cli/src/discover.c index c711221..2f2e854 100644 --- a/cli/src/discover.c +++ b/cli/src/discover.c @@ -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 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[]) { Arguments arguments = { 0 }; @@ -82,7 +117,7 @@ CHIAKI_EXPORT int chiaki_cli_cmd_discover(ChiakiLog *log, int argc, char *argv[] } ChiakiDiscoveryThread thread; - err = chiaki_discovery_thread_start(&thread, &discovery); + err = chiaki_discovery_thread_start(&thread, &discovery, discovery_cb, NULL); if(err != CHIAKI_ERR_SUCCESS) { CHIAKI_LOGE(log, "Discovery thread init failed"); diff --git a/lib/include/chiaki/discovery.h b/lib/include/chiaki/discovery.h index 203a6fa..9e3be7f 100644 --- a/lib/include/chiaki/discovery.h +++ b/lib/include/chiaki/discovery.h @@ -51,7 +51,9 @@ typedef enum chiaki_discovery_host_state_t CHIAKI_DISCOVERY_HOST_STATE_STANDBY } 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; const char *system_version; @@ -60,7 +62,9 @@ typedef struct chiaki_discovery_srch_response_t const char *host_name; const char *host_type; 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); @@ -75,14 +79,18 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(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); +typedef void (*ChiakiDiscoveryCb)(ChiakiDiscoveryHost *host, void *user); + typedef struct chiaki_discovery_thread_t { ChiakiDiscovery *discovery; ChiakiThread thread; ChiakiStopPipe stop_pipe; + ChiakiDiscoveryCb cb; + void *cb_user; } 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); #ifdef __cplusplus diff --git a/lib/src/discovery.c b/lib/src/discovery.c index 9b64487..11f1a4c 100644 --- a/lib/src/discovery.c +++ b/lib/src/discovery.c @@ -25,6 +25,18 @@ #include #include +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) { @@ -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; 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) { - printf("%s: %s\n", header->key, header->value); if(strcmp(header->key, "system-version") == 0) response->system_version = header->value; 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; else if(strcmp(header->key, "host-id") == 0) 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); @@ -154,9 +171,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery, 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->cb = cb; + thread->cb_user = cb_user; ChiakiErrorCode err = chiaki_stop_pipe_init(&thread->stop_pipe); 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_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); if(err != CHIAKI_ERR_SUCCESS) + { CHIAKI_LOGI(discovery->log, "Discovery Response invalid"); + continue; + } + + if(thread->cb) + thread->cb(&response, thread->cb_user); } return NULL;