2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "../util/c99defs.h"
|
2013-12-30 09:14:28 -08:00
|
|
|
#include "../util/bmem.h"
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Procedure call data structure
|
|
|
|
*
|
|
|
|
* This is used to store parameters (and return value) sent to/from signals,
|
|
|
|
* procedures, and callbacks.
|
|
|
|
*/
|
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
enum call_param_type {
|
|
|
|
CALL_PARAM_TYPE_VOID,
|
|
|
|
CALL_PARAM_TYPE_INT,
|
|
|
|
CALL_PARAM_TYPE_FLOAT,
|
|
|
|
CALL_PARAM_TYPE_BOOL,
|
|
|
|
CALL_PARAM_TYPE_PTR,
|
|
|
|
CALL_PARAM_TYPE_STRING
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CALL_PARAM_IN (1<<0)
|
|
|
|
#define CALL_PARAM_OUT (1<<1)
|
|
|
|
|
2013-12-26 01:02:24 -08:00
|
|
|
struct calldata {
|
|
|
|
size_t size; /* size of the stack, in bytes */
|
|
|
|
size_t capacity; /* capacity of the stack, in bytes */
|
|
|
|
uint8_t *stack;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
typedef struct calldata calldata_t;
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2013-12-26 01:02:24 -08:00
|
|
|
static inline void calldata_init(struct calldata *data)
|
|
|
|
{
|
|
|
|
memset(data, 0, sizeof(struct calldata));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void calldata_free(struct calldata *data)
|
|
|
|
{
|
|
|
|
bfree(data->stack);
|
|
|
|
}
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
EXPORT bool calldata_get_data(const calldata_t *data, const char *name,
|
|
|
|
void *out, size_t size);
|
2014-09-25 17:44:05 -07:00
|
|
|
EXPORT void calldata_set_data(calldata_t *data, const char *name,
|
|
|
|
const void *in, size_t new_size);
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2013-12-26 01:02:24 -08:00
|
|
|
static inline void calldata_clear(struct calldata *data)
|
|
|
|
{
|
|
|
|
if (data->stack) {
|
|
|
|
data->size = sizeof(size_t);
|
2015-05-30 19:28:53 -07:00
|
|
|
memset(data->stack, 0, sizeof(size_t));
|
2013-12-26 01:02:24 -08:00
|
|
|
}
|
|
|
|
}
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-03-01 04:54:55 -08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* NOTE: 'get' functions return true only if paramter exists, and is the
|
|
|
|
* same type. They return false otherwise. */
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline bool calldata_get_int(const calldata_t *data, const char *name,
|
2014-03-01 04:54:55 -08:00
|
|
|
long long *val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
return calldata_get_data(data, name, val, sizeof(*val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline bool calldata_get_float (const calldata_t *data, const char *name,
|
2014-01-04 12:28:27 -08:00
|
|
|
double *val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
return calldata_get_data(data, name, val, sizeof(*val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline bool calldata_get_bool (const calldata_t *data, const char *name,
|
2013-12-30 10:09:32 -08:00
|
|
|
bool *val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
return calldata_get_data(data, name, val, sizeof(*val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline bool calldata_get_ptr (const calldata_t *data, const char *name,
|
2014-02-14 14:13:36 -08:00
|
|
|
void *p_ptr)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
return calldata_get_data(data, name, p_ptr, sizeof(p_ptr));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
EXPORT bool calldata_get_string(const calldata_t *data, const char *name,
|
2013-12-25 21:40:33 -08:00
|
|
|
const char **str);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
2014-03-01 04:54:55 -08:00
|
|
|
/* call if you know your data is valid */
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline long long calldata_int(const calldata_t *data, const char *name)
|
2014-01-04 12:28:27 -08:00
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
long long val = 0;
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_get_int(data, name, &val);
|
2014-01-04 12:28:27 -08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline double calldata_float(const calldata_t *data, const char *name)
|
2014-01-04 12:28:27 -08:00
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
double val = 0.0;
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_get_float(data, name, &val);
|
2014-01-04 12:28:27 -08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline bool calldata_bool(const calldata_t *data, const char *name)
|
2014-01-04 12:28:27 -08:00
|
|
|
{
|
2014-03-01 04:54:55 -08:00
|
|
|
bool val = false;
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_get_bool(data, name, &val);
|
2014-01-04 12:28:27 -08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline void *calldata_ptr(const calldata_t *data, const char *name)
|
2014-01-04 12:28:27 -08:00
|
|
|
{
|
|
|
|
void *val;
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_get_ptr(data, name, &val);
|
2014-01-04 12:28:27 -08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
static inline const char *calldata_string(const calldata_t *data,
|
|
|
|
const char *name)
|
2014-01-04 12:28:27 -08:00
|
|
|
{
|
|
|
|
const char *val;
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_get_string(data, name, &val);
|
2014-01-04 12:28:27 -08:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void calldata_set_int (calldata_t *data, const char *name,
|
2014-03-01 04:54:55 -08:00
|
|
|
long long val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, &val, sizeof(val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void calldata_set_float (calldata_t *data, const char *name,
|
2014-01-04 12:28:27 -08:00
|
|
|
double val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, &val, sizeof(val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void calldata_set_bool (calldata_t *data, const char *name,
|
2013-12-30 10:09:32 -08:00
|
|
|
bool val)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, &val, sizeof(val));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void calldata_set_ptr (calldata_t *data, const char *name,
|
2013-12-30 10:09:32 -08:00
|
|
|
void *ptr)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, &ptr, sizeof(ptr));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void calldata_set_string(calldata_t *data, const char *name,
|
2013-12-25 21:40:33 -08:00
|
|
|
const char *str)
|
|
|
|
{
|
|
|
|
if (str)
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, str, strlen(str)+1);
|
2013-12-25 21:40:33 -08:00
|
|
|
else
|
2014-08-05 17:49:28 -07:00
|
|
|
calldata_set_data(data, name, NULL, 0);
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|