Skip to content

Commit

Permalink
Refactor nintendo switch host CB
Browse files Browse the repository at this point in the history
  • Loading branch information
H0neyBadger committed May 7, 2020
1 parent 96204dd commit a5f58b2
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 279 deletions.
3 changes: 3 additions & 0 deletions switch/include/discoverymanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include <host.h>

static void Discovery(ChiakiDiscoveryHost*, void*);

class DiscoveryManager
{
private:
Expand All @@ -37,6 +39,7 @@ class DiscoveryManager
int Discover(const char *discover_ip_dest="255.255.255.255");
int ParseSettings();
Host* GetHost(std::string);
void DiscoveryCB(ChiakiDiscoveryHost*);
};

#endif //CHIAKI_DISCOVERYMANAGER_H
28 changes: 14 additions & 14 deletions switch/include/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ extern "C"
#include "exception.h"

class DiscoveryManager;
static void DiscoveryCB(ChiakiDiscoveryHost*, void*);
static void RegistCB(ChiakiRegistEvent*, void*);
static bool VideoCB(uint8_t*, size_t, void*);
static void InitAudioCB(unsigned int, unsigned int, void*);
static void AudioCB(int16_t*, size_t, void*);
static void Discovery(ChiakiDiscoveryHost*, void*);
static void Regist(ChiakiRegistEvent*, void*);
static bool Video(uint8_t*, size_t, void*);
static void InitAudio(unsigned int, unsigned int, void*);
static void Audio(int16_t*, size_t, void*);


class Host
Expand Down Expand Up @@ -85,19 +85,12 @@ class Host
AVCodec *codec;
AVCodecContext *codec_context;
struct SwsContext * sws_context = NULL;
int UpdateFrame();
// audio vars
SDL_AudioDeviceID audio_device_id;
// use friend class and methodes
// to allow private var access
friend class DiscoveryManager;
friend class Settings;
friend void DiscoveryCB(ChiakiDiscoveryHost*, void*);
friend void RegistCB(ChiakiRegistEvent*, void*);
friend bool VideoCB(uint8_t*, size_t, void*);
friend void InitAudioCB(unsigned int, unsigned int, void*);
friend void AudioCB(int16_t*, size_t, void*);

public:
// internal state
bool discovered = false;
Expand All @@ -108,14 +101,21 @@ class Host
// share picture frame
// with main function
AVFrame *pict;
Host();
static Host * GetOrCreate(std::map<std::string, Host>*, std::string*);
Host(ChiakiLog *log):log(log) {};
Host(){};
static Host * GetOrCreate(ChiakiLog*, std::map<std::string, Host>*, std::string*);
void InitVideo();
int Register(std::string pin);
int Wakeup();
int ConnectSession();
void StartSession();
bool ReadGameKeys(SDL_Event*, ChiakiControllerState*);
void SendFeedbackState(ChiakiControllerState*);
// callback methods
void RegistCB(ChiakiRegistEvent*);
bool VideoCB(uint8_t*, size_t);
void InitAudioCB(unsigned int, unsigned int);
void AudioCB(int16_t*, size_t);
};

#endif
18 changes: 9 additions & 9 deletions switch/include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class Settings {
// the aim is not to have bulletproof parser
// the goal is to read/write inernal flat configuration file
const std::map<Settings::ConfigurationItem, std::regex> re_map = {
{ HOST_NAME, std::regex("\\[\\s*(.+)\\s*\\]") },
{ HOST_IP, std::regex("\\s*host_ip\\s*=\\s*\"?(\\d+\\.\\d+\\.\\d+\\.\\d+)\"?") },
{ PSN_ONLINE_ID, std::regex("\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") },
{ PSN_ACCOUNT_ID, std::regex("\\s*psn_account_id\\s*=\\s*\"?([\\w/=]+)\"?") },
{ RP_KEY, std::regex("\\s*rp_key\\s*=\\s*\"?(\\w+)\"?") },
{ RP_KEY_TYPE, std::regex("\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") },
{ RP_REGIST_KEY, std::regex("\\s*rp_regist_key\\s*=\\s*\"?(\\w+)\"?") },
{ VIDEO_RESOLUTION, std::regex("\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") },
{ VIDEO_FPS, std::regex("\\s*video_fps\\s*=\\s*\"?(60|30)\"?") },
{ HOST_NAME, std::regex("^\\[\\s*(.+)\\s*\\]") },
{ HOST_IP, std::regex("^\\s*host_ip\\s*=\\s*\"?(\\d+\\.\\d+\\.\\d+\\.\\d+)\"?") },
{ PSN_ONLINE_ID, std::regex("^\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") },
{ PSN_ACCOUNT_ID, std::regex("^\\s*psn_account_id\\s*=\\s*\"?([\\w/=]+)\"?") },
{ RP_KEY, std::regex("^\\s*rp_key\\s*=\\s*\"?(\\w+)\"?") },
{ RP_KEY_TYPE, std::regex("^\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") },
{ RP_REGIST_KEY, std::regex("^\\s*rp_regist_key\\s*=\\s*\"?(\\w+)\"?") },
{ VIDEO_RESOLUTION, std::regex("^\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") },
{ VIDEO_FPS, std::regex("^\\s*video_fps\\s*=\\s*\"?(60|30)\"?") },
};

ConfigurationItem ParseLine(std::string *line, std::string *value);
Expand Down
136 changes: 62 additions & 74 deletions switch/src/discoverymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,81 +23,11 @@

#include <discoverymanager.h>

static void DiscoveryCB(ChiakiDiscoveryHost *host, void *user)
{
// the user ptr is passed as
// chiaki_discovery_thread_start arg
std::map<std::string, Host> *hosts = (std::map<std::string, Host>*) user;
std::string key = host->host_name;
Host *n_host = Host::GetOrCreate(hosts, &key);

ChiakiLog* log = nullptr;
CHIAKI_LOGI(log, "--");
CHIAKI_LOGI(log, "Discovered Host:");
CHIAKI_LOGI(log, "State: %s", chiaki_discovery_host_state_string(host->state));
/* host attr
uint32_t host_addr;
int system_version;
int device_discovery_protocol_version;
std::string host_name;
std::string host_type;
std::string host_id;
*/
n_host->state = host->state;
// add host ptr to list
if(host->system_version){
// example: 07020001
n_host->system_version = atoi(host->system_version);
CHIAKI_LOGI(log, "System Version: %s", host->system_version);
}
if(host->device_discovery_protocol_version)
n_host->device_discovery_protocol_version = atoi(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_addr)
n_host->host_addr = host->host_addr;
CHIAKI_LOGI(log, "Host Addr: %s", host->host_addr);

if(host->host_name)
// FIXME
n_host->host_name = 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", host->running_app_name);

CHIAKI_LOGI(log, "--");
static void Discovery(ChiakiDiscoveryHost *discovered_host, void *user){
DiscoveryManager *dm = (DiscoveryManager*) user;
dm->DiscoveryCB(discovered_host);
}

int DiscoveryManager::ParseSettings(){
/*
std::string ap_ssid;
std::string ap_bssid;
std::string ap_key;
std::string ap_name;
std::string ps4_nickname;
// mac address = 48 bits
uint8_t ps4_mac[6];
char rp_regist_key[CHIAKI_SESSION_AUTH_SIZE];
uint32_t rp_key_type;
uint8_t rp_key[0x10];
*/
return 0;
}


int DiscoveryManager::Discover(const char *discover_ip_dest)
{
// use broadcast address to discover host form local network
Expand All @@ -111,7 +41,7 @@ int DiscoveryManager::Discover(const char *discover_ip_dest)
}

ChiakiDiscoveryThread thread;
err = chiaki_discovery_thread_start(&thread, &discovery, DiscoveryCB, hosts);
err = chiaki_discovery_thread_start(&thread, &discovery, Discovery, this);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(log, "Discovery thread init failed");
Expand Down Expand Up @@ -174,3 +104,61 @@ Host* DiscoveryManager::GetHost(std::string ps4_nickname){
return 0;
}
}

void DiscoveryManager::DiscoveryCB(ChiakiDiscoveryHost *discovered_host){

// the user ptr is passed as
// chiaki_discovery_thread_start arg

std::string key = discovered_host->host_name;
Host *host = Host::GetOrCreate(this->log, hosts, &key);

CHIAKI_LOGI(this->log, "--");
CHIAKI_LOGI(this->log, "Discovered Host:");
CHIAKI_LOGI(this->log, "State: %s", chiaki_discovery_host_state_string(discovered_host->state));
/* host attr
uint32_t host_addr;
int system_version;
int device_discovery_protocol_version;
std::string host_name;
std::string host_type;
std::string host_id;
*/
host->state = discovered_host->state;
// add host ptr to list
if(discovered_host->system_version){
// example: 07020001
host->system_version = atoi(discovered_host->system_version);
CHIAKI_LOGI(this->log, "System Version: %s", discovered_host->system_version);
}
if(discovered_host->device_discovery_protocol_version)
host->device_discovery_protocol_version = atoi(discovered_host->device_discovery_protocol_version);
CHIAKI_LOGI(this->log, "Device Discovery Protocol Version: %s", discovered_host->device_discovery_protocol_version);

if(discovered_host->host_request_port)
CHIAKI_LOGI(this->log, "Request Port: %hu", (unsigned short)discovered_host->host_request_port);

if(discovered_host->host_addr)
host->host_addr = discovered_host->host_addr;
CHIAKI_LOGI(this->log, "Host Addr: %s", discovered_host->host_addr);

if(discovered_host->host_name)
// FIXME
host->host_name = discovered_host->host_name;
CHIAKI_LOGI(this->log, "Host Name: %s", discovered_host->host_name);

if(discovered_host->host_type)
CHIAKI_LOGI(this->log, "Host Type: %s", discovered_host->host_type);

if(discovered_host->host_id)
CHIAKI_LOGI(this->log, "Host ID: %s", discovered_host->host_id);

if(discovered_host->running_app_titleid)
CHIAKI_LOGI(this->log, "Running App Title ID: %s", discovered_host->running_app_titleid);

if(discovered_host->running_app_name)
CHIAKI_LOGI(this->log, "Running App Name: %s", discovered_host->running_app_name);

CHIAKI_LOGI(this->log, "--");
}

Loading

0 comments on commit a5f58b2

Please sign in to comment.