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,
|
|
|
|
} PSYC_Bool;
|
|
|
|
|
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,
|
|
|
|
} PSYC_Part;
|
|
|
|
|
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,
|
|
|
|
} PSYC_Type;
|
|
|
|
|
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,
|
|
|
|
} PSYC_ListType;
|
|
|
|
|
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-23 14:51:46 +00:00
|
|
|
} PSYC_ModifierFlag;
|
2011-04-22 21:16:50 +00:00
|
|
|
|
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,
|
|
|
|
} PSYC_PacketFlag;
|
2011-04-22 21:16:50 +00:00
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
const char* ptr;
|
|
|
|
} PSYC_Array;
|
|
|
|
|
2011-04-23 15:16:05 +00:00
|
|
|
#define PSYC_C2ARRAY(string) { sizeof(string)-1, string }
|
|
|
|
#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-22 20:44:14 +00:00
|
|
|
char oper; // not call it 'operator' as C++ may not like that..?
|
|
|
|
PSYC_Array name;
|
2011-04-22 21:16:50 +00:00
|
|
|
PSYC_Array value;
|
2011-04-23 14:51:46 +00:00
|
|
|
PSYC_ModifierFlag flag;
|
2011-04-22 20:44:14 +00:00
|
|
|
} PSYC_Modifier;
|
|
|
|
|
|
|
|
/* intermediate struct for a PSYC packet */
|
2011-04-23 14:51:46 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
PSYC_Modifier** routing; // Routing header
|
|
|
|
PSYC_Modifier** entity; // Entitiy header
|
2011-04-22 20:44:14 +00:00
|
|
|
PSYC_Array method;
|
|
|
|
PSYC_Array data;
|
2011-04-23 14:51:46 +00:00
|
|
|
size_t length; /// Length of content part
|
|
|
|
PSYC_PacketFlag flag;
|
2011-04-22 20:44:14 +00:00
|
|
|
} PSYC_Packet;
|
|
|
|
|
2011-04-23 14:51:46 +00:00
|
|
|
PSYC_Modifier PSYC_newModifier(char* oper, PSYC_Array* name, PSYC_Array* value, PSYC_ModifierFlag flag);
|
|
|
|
|
|
|
|
PSYC_Modifier PSYC_newModifier2(char* oper, char* name, size_t namelen, char* value, size_t valuelen, PSYC_ModifierFlag flag);
|
|
|
|
|
2011-04-23 14:58:02 +00:00
|
|
|
PSYC_Packet PSYC_newPacket(PSYC_Modifier** routing, PSYC_Modifier **entity, PSYC_Array* method, PSYC_Array* data, PSYC_PacketFlag flag);
|
2011-04-23 14:51:46 +00:00
|
|
|
|
2011-04-23 14:58:02 +00:00
|
|
|
PSYC_Packet PSYC_newPacket2(PSYC_Modifier** routing, PSYC_Modifier **entity, char* method, size_t methodlen, char* data, size_t datalen, PSYC_PacketFlag flag);
|
2011-04-22 20:44:14 +00:00
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
/// Routing vars in alphabetical order.
|
|
|
|
extern const PSYC_Array PSYC_routingVars[];
|
|
|
|
/// Number of routing vars.
|
|
|
|
extern const size_t PSYC_routingVarsNum;
|
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Get the type of variable name.
|
|
|
|
*/
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_Bool PSYC_isRoutingVar(const char* name, size_t len);
|
2011-04-20 17:18:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of variable name.
|
|
|
|
*/
|
2011-04-22 15:09:32 +00:00
|
|
|
PSYC_Type PSYC_getVarType(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.
|
|
|
|
*/
|
|
|
|
int PSYC_inherits(char* sho, size_t slen,
|
|
|
|
char* lon, size_t llen);
|
|
|
|
|
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-20 13:36:56 +00:00
|
|
|
int PSYC_matches(char* sho, size_t slen,
|
|
|
|
char* lon, size_t llen);
|
2011-04-16 15:30:03 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Callback for PSYC_text() that produces a value for a match.
|
2011-04-18 14:51:54 +00:00
|
|
|
*
|
|
|
|
* The application looks up a match such as _fruit from [_fruit] and
|
|
|
|
* if found writes its current value from its variable store into the
|
|
|
|
* outgoing buffer.. "Apple" for example. The template returns the
|
|
|
|
* number of bytes written. 0 is a legal return value. Should the
|
|
|
|
* callback return -1, PSYC_text leaves the original template text as is.
|
|
|
|
*/
|
2011-04-18 14:54:56 +00:00
|
|
|
typedef int (*PSYC_textCB)(uint8_t* match, size_t mlen,
|
|
|
|
uint8_t** buffer, size_t * blen);
|
2011-04-18 14:51:54 +00:00
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
/**
|
|
|
|
* Fills out text templates by asking a callback for content.
|
2011-04-18 14:51:54 +00:00
|
|
|
*
|
|
|
|
* Copies the contents of the template into the buffer while looking
|
|
|
|
* for braceOpen and braceClose strings and calling the callback for
|
|
|
|
* each enclosed string between these braces. Should the callback
|
|
|
|
* return -1, the original template text is copied as is.
|
|
|
|
*
|
|
|
|
* By default PSYC's "[" and "]" are used but you can provide any other
|
|
|
|
* brace strings such as "${" and "}" or "<!--" and "-->".
|
|
|
|
*
|
|
|
|
* See also http://about.psyc.eu/psyctext
|
|
|
|
*/
|
2011-04-18 14:54:56 +00:00
|
|
|
int PSYC_text(uint8_t* template, size_t tlen,
|
|
|
|
uint8_t** buffer, size_t * blen,
|
2011-04-18 14:51:54 +00:00
|
|
|
PSYC_textCB lookupValue,
|
|
|
|
char* braceOpen, char* braceClose);
|
|
|
|
|
2011-04-20 17:18:35 +00:00
|
|
|
#endif // PSYC_H
|