2011-04-19 20:57:49 +00:00
|
|
|
/** @file psyc.h
|
|
|
|
*
|
2011-04-20 17:18:35 +00:00
|
|
|
* @brief Main PSYC interface providing crucial functionality.
|
2011-04-19 20:57:49 +00:00
|
|
|
*/
|
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/** @mainpage PSYC Core Library
|
2011-04-19 20:57:49 +00:00
|
|
|
*
|
|
|
|
* @section intro_sec Introduction
|
|
|
|
*
|
|
|
|
* This is the introduction.
|
|
|
|
*
|
|
|
|
* @section install_sec Installation
|
|
|
|
*
|
|
|
|
* @subsection step1 Step 1: Opening the box
|
2011-04-20 17:18:35 +00:00
|
|
|
*
|
2011-04-19 20:57:49 +00:00
|
|
|
* etc...
|
|
|
|
*/
|
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
#ifndef PSYC_H
|
|
|
|
# define PSYC_H
|
|
|
|
|
2011-04-16 15:30:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday)
|
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_FALSE = 0,
|
|
|
|
PSYC_TRUE = 1,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycBool;
|
2011-04-20 17:18:35 +00:00
|
|
|
|
2011-04-21 12:20:24 +00:00
|
|
|
/**
|
|
|
|
* PSYC packet parts.
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_PART_RESET = -1,
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_PART_ROUTING,
|
2011-04-21 12:20:24 +00:00
|
|
|
PSYC_PART_LENGTH,
|
|
|
|
PSYC_PART_CONTENT,
|
|
|
|
PSYC_PART_METHOD,
|
|
|
|
PSYC_PART_DATA,
|
|
|
|
PSYC_PART_END,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycPart;
|
2011-04-21 12:20:24 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Different types that a variable can have.
|
|
|
|
*
|
|
|
|
* This enum lists PSYC variable types that
|
|
|
|
* this library is capable of checking for
|
|
|
|
* validity. Other variable types are treated
|
|
|
|
* as opaque data.
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_TYPE_UNKNOWN,
|
2011-04-20 17:18:35 +00:00
|
|
|
PSYC_TYPE_AMOUNT,
|
|
|
|
PSYC_TYPE_COLOR,
|
|
|
|
PSYC_TYPE_DATE,
|
|
|
|
PSYC_TYPE_DEGREE,
|
|
|
|
PSYC_TYPE_ENTITY,
|
|
|
|
PSYC_TYPE_FLAG,
|
|
|
|
PSYC_TYPE_LANGUAGE,
|
|
|
|
PSYC_TYPE_LIST,
|
|
|
|
PSYC_TYPE_NICK,
|
|
|
|
PSYC_TYPE_PAGE,
|
|
|
|
PSYC_TYPE_UNIFORM,
|
|
|
|
PSYC_TYPE_TIME,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycType;
|
2011-04-20 17:18:35 +00:00
|
|
|
|
2011-04-21 12:20:24 +00:00
|
|
|
/**
|
|
|
|
* List types.
|
|
|
|
* Possible types are text and binary.
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_LIST_TEXT = 1,
|
|
|
|
PSYC_LIST_BINARY = 2,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycListType;
|
2011-04-21 12:20:24 +00:00
|
|
|
|
2011-04-23 14:51:46 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2011-04-22 21:16:50 +00:00
|
|
|
PSYC_MODIFIER_CHECK_LENGTH = 0,
|
|
|
|
PSYC_MODIFIER_NEED_LENGTH = 1,
|
|
|
|
PSYC_MODIFIER_NO_LENGTH = 2,
|
|
|
|
PSYC_MODIFIER_ROUTING = 3,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycModifierFlag;
|
2011-04-22 21:16:50 +00:00
|
|
|
|
2011-04-26 15:23:06 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_LIST_CHECK_LENGTH = 0,
|
|
|
|
PSYC_LIST_NEED_LENGTH = 1,
|
|
|
|
PSYC_LIST_NO_LENGTH = 2,
|
|
|
|
} psycListFlag;
|
|
|
|
|
2011-04-23 14:51:46 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PSYC_PACKET_CHECK_LENGTH = 0,
|
|
|
|
PSYC_PACKET_NEED_LENGTH = 1,
|
|
|
|
PSYC_PACKET_NO_LENGTH = 2,
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycPacketFlag;
|
2011-04-22 21:16:50 +00:00
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t length;
|
2011-04-25 12:20:13 +00:00
|
|
|
const char *ptr;
|
2011-04-25 21:40:38 +00:00
|
|
|
} psycString;
|
2011-04-22 15:09:32 +00:00
|
|
|
|
2011-04-29 20:58:19 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
psycString name;
|
|
|
|
int value;
|
|
|
|
} psycMatchVar;
|
|
|
|
|
2011-04-25 12:20:13 +00:00
|
|
|
/**
|
2011-04-25 21:40:38 +00:00
|
|
|
* Shortcut for creating a psycString.
|
2011-04-25 12:20:13 +00:00
|
|
|
*
|
|
|
|
* @param memory Pointer to the buffer.
|
|
|
|
* @param length Length of that buffer.
|
|
|
|
*
|
2011-04-25 21:40:38 +00:00
|
|
|
* @return An instance of the psycString struct.
|
2011-04-25 12:20:13 +00:00
|
|
|
*/
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycString psyc_newString (const char *str, size_t strlen);
|
2011-04-25 12:20:13 +00:00
|
|
|
|
|
|
|
#define PSYC_C2STR(string) {sizeof(string)-1, string}
|
2011-04-23 15:16:05 +00:00
|
|
|
#define PSYC_C2ARG(string) string, sizeof(string)-1
|
|
|
|
|
2011-04-22 20:44:14 +00:00
|
|
|
/* intermediate struct for a PSYC variable modification */
|
2011-04-23 14:51:46 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-04-25 12:20:13 +00:00
|
|
|
char oper; // not call it 'operator' as C++ may not like that..
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString name;
|
|
|
|
psycString value;
|
|
|
|
psycModifierFlag flag;
|
|
|
|
} psycModifier;
|
2011-04-22 20:44:14 +00:00
|
|
|
|
2011-04-25 12:20:13 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-04-25 12:39:58 +00:00
|
|
|
size_t lines;
|
2011-04-25 21:40:38 +00:00
|
|
|
psycModifier *modifiers;
|
2011-04-26 15:23:06 +00:00
|
|
|
} psycHeader;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t num_elems;
|
|
|
|
psycString *elems;
|
|
|
|
size_t length;
|
|
|
|
psycListFlag flag;
|
|
|
|
} psycList;
|
2011-04-25 12:20:13 +00:00
|
|
|
|
2011-05-03 20:18:35 +00:00
|
|
|
/** intermediate struct for a PSYC packet */
|
2011-04-23 14:51:46 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-04-28 13:20:07 +00:00
|
|
|
psycHeader routing; ///< Routing header.
|
|
|
|
psycHeader entity; ///< Entity header.
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString method;
|
|
|
|
psycString data;
|
2011-04-28 13:20:07 +00:00
|
|
|
size_t routingLength; ///< Length of routing part.
|
|
|
|
size_t contentLength; ///< Length of content part.
|
|
|
|
size_t length; ///< Total length of packet.
|
2011-04-25 21:40:38 +00:00
|
|
|
psycPacketFlag flag;
|
|
|
|
} psycPacket;
|
2011-04-22 20:44:14 +00:00
|
|
|
|
2011-04-25 21:40:38 +00:00
|
|
|
inline int psyc_version();
|
2011-04-25 20:02:24 +00:00
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
/** Check if a modifier needs length */
|
|
|
|
inline psycModifierFlag psyc_checkModifierLength(psycModifier *m);
|
|
|
|
|
|
|
|
/** Get the total length of a modifier. */
|
|
|
|
inline size_t psyc_getModifierLength(psycModifier *m);
|
|
|
|
|
|
|
|
/** Create new modifier */
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value,
|
2011-04-26 20:44:48 +00:00
|
|
|
psycModifierFlag flag);
|
2011-04-23 14:51:46 +00:00
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
/** Create new modifier */
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycModifier psyc_newModifier2(char oper,
|
2011-04-26 20:44:48 +00:00
|
|
|
const char *name, size_t namelen,
|
|
|
|
const char *value, size_t valuelen,
|
|
|
|
psycModifierFlag flag);
|
|
|
|
|
|
|
|
/** Check if a list needs length */
|
|
|
|
inline psycListFlag psyc_checkListLength(psycList *list);
|
2011-04-23 14:51:46 +00:00
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
/** Get the total length of a list. */
|
|
|
|
inline psycListFlag psyc_getListLength(psycList *list);
|
|
|
|
|
|
|
|
/** Check if a packet needs length */
|
|
|
|
inline psycPacketFlag psyc_checkPacketLength(psycPacket *p);
|
|
|
|
|
|
|
|
/** Calculate and set the length of packet parts and total packet length */
|
|
|
|
inline size_t psyc_setPacketLength(psycPacket *p);
|
|
|
|
|
|
|
|
/** Create new list */
|
2011-04-26 15:23:06 +00:00
|
|
|
inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag);
|
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
/** Create new packet */
|
2011-04-26 15:23:06 +00:00
|
|
|
inline psycPacket psyc_newPacket(psycHeader *routing,
|
2011-04-26 20:44:48 +00:00
|
|
|
psycHeader *entity,
|
|
|
|
psycString *method, psycString *data,
|
|
|
|
psycPacketFlag flag);
|
2011-04-23 14:51:46 +00:00
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
/** Create new packet */
|
2011-04-25 21:40:38 +00:00
|
|
|
inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen,
|
2011-04-26 20:44:48 +00:00
|
|
|
psycModifier *entity, size_t entitylen,
|
|
|
|
const char *method, size_t methodlen,
|
|
|
|
const char *data, size_t datalen,
|
|
|
|
psycPacketFlag flag);
|
2011-04-22 20:44:14 +00:00
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
/// Routing vars in alphabetical order.
|
2011-04-25 21:40:38 +00:00
|
|
|
extern const psycString PSYC_routingVars[];
|
2011-04-29 20:58:19 +00:00
|
|
|
extern const psycMatchVar PSYC_varTypes[];
|
2011-04-22 15:09:32 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Get the type of variable name.
|
|
|
|
*/
|
2011-04-25 21:40:38 +00:00
|
|
|
psycBool psyc_isRoutingVar(const char *name, size_t len);
|
2011-04-20 17:18:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of variable name.
|
|
|
|
*/
|
2011-04-29 20:58:19 +00:00
|
|
|
psycType psyc_getVarType(const char *name, size_t len);
|
2011-04-20 17:18:35 +00:00
|
|
|
|
2011-04-20 21:08:20 +00:00
|
|
|
/**
|
|
|
|
* Checks if long keyword string inherits from short keyword string.
|
|
|
|
*/
|
2011-04-25 21:40:38 +00:00
|
|
|
int psyc_inherits(char *sho, size_t slen,
|
2011-04-25 12:20:13 +00:00
|
|
|
char *lon, size_t llen);
|
2011-04-20 21:08:20 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Checks if short keyword string matches long keyword string.
|
2011-04-16 15:30:03 +00:00
|
|
|
*/
|
2011-04-25 21:40:38 +00:00
|
|
|
int psyc_matches(char *sho, size_t slen,
|
2011-04-25 12:20:13 +00:00
|
|
|
char *lon, size_t llen);
|
2011-04-16 15:30:03 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
#endif // PSYC_H
|