Blob Blame History Raw
#ifndef TOKSVC_DRIVER_IMPL_H
#define TOKSVC_DRIVER_IMPL_H

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>

#include <stdint.h>
#include <stdio.h>

#include <toksvc/toksvc.h>
#include "argv/argv.h"

#define TOKS_OPTV_ELEMENTS 64

extern   const struct argv_option toks_default_options[];
extern   const ntapi_vtbl * toks_ntapi;

#define  ntapi toks_ntapi

enum app_tags {
	TAG_HELP,
	TAG_VERSION,
	TAG_DAEMON,
	TAG_SYSROOT,
	TAG_UUID,
	TAG_CONNECT,
	TAG_TOKENS,
	TAG_ACQUIRE,
};

struct toks_ticks {
	nt_filetime	pcfreq;
	int32_t		(*qpc)(nt_filetime *);
};

struct toks_driver_ctx_impl {
	nt_rtdata *		rtdata;
	struct toks_common_ctx	cctx;
	struct toks_driver_ctx	ctx;
	struct toks_ticks	ticks;
	struct toks_token *	tokens;
	struct _nt_port_keys    keys;
	void *			hevent;
	void *			hsvcdir;
	void *			hsvclink;
	void *			hservice;
	int			ntokens;
	nt_guid			uuid;
};

static inline struct toks_driver_ctx_impl * toks_get_driver_ictx(const struct toks_driver_ctx * dctx)
{
	uintptr_t addr;

	if (dctx) {
		addr = (uintptr_t)dctx - offsetof(struct toks_driver_ctx_impl,ctx);
		return (struct toks_driver_ctx_impl *)addr;
	}

	return 0;
}

static inline nt_rtdata * toks_get_driver_rtdata(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->rtdata;
}

static inline void * toks_get_driver_hevent(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hevent;
}

static inline void * toks_get_driver_hsvcdir(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hsvcdir;
}

static inline void toks_set_driver_hsvcdir(const struct toks_driver_ctx * dctx, void * hsvcdir)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hsvcdir = hsvcdir;
}

static inline void * toks_get_driver_hsvclink(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hsvclink;
}

static inline void toks_set_driver_hsvclink(const struct toks_driver_ctx * dctx, void * hsvclink)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hsvclink = hsvclink;
}

static inline void * toks_get_driver_hservice(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hservice;
}

static inline void toks_set_driver_hservice(const struct toks_driver_ctx * dctx, void * hservice)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hservice = hservice;
}

static inline struct _nt_port_keys * toks_get_driver_keys(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return &ictx->keys;
}

static inline const nt_guid * toks_get_driver_uuid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return &ictx->uuid;
}

static inline struct toks_token * toks_get_driver_tokens(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->tokens;
}

static inline int toks_get_driver_ntokens(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->ntokens;
}

static inline void toks_query_performance_counters(const struct toks_driver_ctx * dctx, nt_filetime * ticks)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);

	if (!(ictx->ticks.qpc(ticks)))
		ticks->quad = 0;
}

int32_t toks_open_file(void ** hfile, void * hat, const char * arg, bool fprivate);
int32_t toks_open_dir(void ** hfile, void * hat, const char * arg, bool fprivate);

#endif