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
#define TOKS_MAX_WAITERS   4000

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

size_t   toks_strlen(const char * ch);
char *   toks_strcpy(char * dst, const char * src);

void *   toks_calloc(size_t n, size_t size);
void     toks_free(void *);

#define  ntapi toks_ntapi

enum app_tags {
	TAG_HELP,
	TAG_VERSION,
	TAG_DAEMON,
	TAG_SYSROOT,
	TAG_UUID,
	TAG_CONNECT,
	TAG_TOKENS,
	TAG_ABORT,
	TAG_ACQUIRE,
	TAG_RELEASE,
	TAG_TIMEOUT,
	TAG_REFSTR,
	TAG_PID,
	TAG_SYSPID,
	TAG_CTRLPID,
	TAG_CSYSPID,
	TAG_LOGFILE,
	TAG_LOGLEVEL,
};

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 toks_waiter *	waiters;
	struct _nt_port_keys    keys;
	int64_t                 timeout;
	char *			refstr;
	void *			hevent;
	void *			hsvcdir;
	void *			hpiddir;
	void *			hsvclink;
	void *			hservice;
	void *			hserver;
	void *			hlog;
	int			nwaiters;
	int			ntokens;
	int			tokpid;
	int			tsyspid;
	int			ctrlpid;
	int			csyspid;
	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 int64_t toks_get_driver_timeout(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->timeout;
}

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

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_hpiddir(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hpiddir;
}

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

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 void * toks_get_driver_hserver(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hserver;
}

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

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 int toks_get_driver_nwaiters(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->nwaiters;
}

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

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 int toks_get_driver_tokpid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->tokpid;
}

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

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

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

static inline char * toks_get_driver_refstr(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->refstr;
}

static inline void toks_set_driver_refstr(const struct toks_driver_ctx * dctx, const char * refstr)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);

	if (ictx->refstr) {
		toks_free(ictx->refstr);
		ictx->refstr = 0;
	}

	if (refstr && (ictx->refstr = toks_calloc(1,toks_strlen(refstr)+1)))
		toks_strcpy(ictx->refstr,refstr);
}

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_log_file(void ** hfile, void * hat, const char * arg, bool fprivate);
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