#pragma once
#include
#include
#include
#include "cJSON.h"
#include "Bond\_Req.h"
/\* defined in Funnel\_Tree.h \*/
typedef struct funnel\_tree\_s funnel\_tree\_t;
typedef enum {
FUNNEL\_AUTH\_LEVEL\_NONE = 0,
FUNNEL\_AUTH\_LEVEL\_DISCOVER,
FUNNEL\_AUTH\_LEVEL\_CONTROL,
FUNNEL\_AUTH\_LEVEL\_ADMIN
} funnel\_auth\_level\_t;
#define FUNNEL\_CONNECTION\_BROADCAST -1
#define FUNNEL\_TRANSPORT\_GRATUITOUS -42
#define FUNNEL\_TRANSPORT\_HASH\_ONLY -43
/\*\*
\* request handler function pointer
\* @param ft pointer to corresponding funnel tree node
\* which contains data, key, and hash useful to this handler
\* @param req [in] pointer to request to handle
\* [out] reply to be sent by funnel, if FUNNEL\_REPLY\_IMMEDIATE
\*
\* @return FUNNEL\_REPLY\_DEFERRED:
\* who sends reply: timer associated with handler
\* who frees req: handler
\* children enumerated?: no
\*
\* FUNNEL\_REPLY\_IMMEDIATE
\* who sends reply: Funnel
\* who frees req: Funnel
\* children enumerated?: only if !Bond\_Status\_Is\_Error(req.status)
\*
\* @note While it is \_possible\_, for the handler to call Funnel\_Process\_Up(),
\* we strongly discourage it! The reason for the reply via req
\* parameter is that it avoids keeping the handler functions' stack
\* context, and furthermore, that this way other children can be
\* enumerated.
\*/
typedef void (\*request\_handler\_t) (funnel\_tree\_t \* ft,
bond\_req\_t \* req);
/\*\*
\* callback for sending bond requests on a transport
\* @param req request to send
\* @return true if message queued for sending
\* / false if message not sent
\*/
typedef bool (\*funnel\_transport\_cb\_t)(bond\_req\_t \* req);
/\*\*
\* initialize the Funnel singleton
\* @return true
\*/
bool Funnel\_Init();
/\*\*
\* free all funnel resources
\* @warning any funnel (or funnel\_tree) calls may fail after this point!
\* @return true
\*/
bool Funnel\_Deinit();
/\*\*
\* register a transport
\* @param f function to transmit an up bond request
\* @param transport\_id [out] id required by Funnel\_Process\_Down
\* @return true if added to transport table
\*/
bool Funnel\_Register\_Transport(funnel\_transport\_cb\_t f,
const char \*name, int \*transport\_id);
/\*\*
\* add hash to the reply whenever it's an object
\* @param req reply to augment
\* @param ft corresponding funnel tree node
\* @return true if hash added
\*/
bool Funnel\_Req\_Add\_Hash(bond\_req\_t \* req, funnel\_tree\_t \* ft);
/\*\*
\* add children enumeration to reply without hash
\* @param req reply to augment
\* @return true if children enumerated
\* @note Only adds to 200 replies.
\* We exclude 201 because those replies just contain the new id.
\*/
bool Funnel\_Req\_Add\_Children\_No\_Hash(bond\_req\_t \* req, funnel\_tree\_t \* ft);
/\*\*
\* add children enumeration to reply
\* @param req reply to augment
\* @return true if children enumerated
\* @note Only adds to 200 replies.
\* We exclude 201 because those replies just contain the new id.
\*/
bool Funnel\_Req\_Add\_Children(bond\_req\_t \* req, funnel\_tree\_t \* ft);
/\*\*
\* process a down request (from app/cloud to BOND)
\* @param req request to process
\* This function takes ownership of the req object,
\* and will free it, even under error conditions.
\* req becomes invalid after this function returns.
\* @return true
\*/
bool Funnel\_Process\_Down(bond\_req\_t \* req);
/\*\*
\* process an up request (from BOND to app/cloud)
\* @param req request to send
\* @return true
\*/
bool Funnel\_Process\_Up(bond\_req\_t \* req);
/\*\*
\* split the topic into the first and rest
\* @param topic topic string, delimited by forward-slashes
\* @param rest [out] will point to remainder of topic
\* @return newly-allocated string containing the first token of topic
\* Must be Osal\_Free'd by caller!
\*/
char \*Funnel\_Topic\_Split(const char \*topic, const char \* \* rest);
/\*\*
\* add the result of a GET request to the body of a non-idempotent reply
\* @param req reply
\* @return true on success
\*/
bool Funnel\_Check\_Add\_Nonidempotent\_Body(bond\_req\_t \* req);
/\*\*
\* generate a gratuitous request but don't send it
\* @param topic the topic to make the request on, the caller retains ownership
\* @param method the method to use for the request
\* @param body the body of the request, this function takes ownership
\* @param flags flags to add to the request, beyond the default
\* BOND\_REQ\_FLAG\_QUIET and BOND\_REQ\_FLAG\_BCAST
\* @param class the requestor class to use for this gratuitous request
\* @return the gratuitous request
\*/
bond\_req\_t \* Funnel\_Gratuitous\_Req(const char \* topic,
bond\_req\_method\_t method,
cJSON \* body,
uint8\_t flags,
bond\_req\_uuid\_class\_t class);
/\*\*
\* generate a gratuitous request and send it
\* @param topic the topic to make the request on, the caller retains ownership
\* @param method the method to use for the request
\* @param body the body of the request, this function takes ownership
\* @param class the class to use for the request
\* @return true iff request sent
\*/
bool Funnel\_Gratuitous\_Req\_Reply\_With\_Class(const char \*topic,
bond\_req\_method\_t method,
cJSON \* body,
bond\_req\_uuid\_class\_t class);
/\*\*
\* generate a gratuitous request and send it using BOND class
\* @param topic the topic to make the request on, the caller retains ownership
\* @param method the method to use for the request
\* @param body the body of the request, this function takes ownership
\* @return true iff request sent
\*/
bool Funnel\_Gratuitous\_Req\_Reply(const char \* topic,
bond\_req\_method\_t method,
cJSON \* body);
/\*\*
\* generate a gratuitous get and send it
\* @param topic
\* @return true if request sent
\*/
bool Funnel\_Gratuitous\_Get\_Reply(const char \*topic);
/\*\*
\* get name of transport (possibilities: "mqtt", "http", "cli", "bond")
\* @param transport\_id id (index) of transport
\* @return name as const string
\*/
const char \*Funnel\_Get\_Transport\_Name(int transport\_id);
/\*\*
\* convert 64-bit key to 8- or 16-nibble string
\* 8-nibble string used if upper 32-bits of key are zero
\* @param key
\* @param buf [out]
\* @param buf\_size must be >= 17
\* @return buf or NULL on failure
\*/
const char \* Funnel\_Key2String(uint64\_t key, char \* buf, uint32\_t buf\_size);
#pragma once
#include
#include
#include
#include "BFTL.h"
#include "Beau\_Enum.h"
#if defined(CONFIG\_BEAU\_CACHE)
#include "Beau\_Cache.h"
#endif
#define BEAU\_DEFAULT\_DB\_NAME "db"
#define BEAU\_KEY\_NONE 0
#pragma pack(push, 1)
typedef struct {
uint32\_t key\_lo; /\*\*< lower 32-bits of key \*/
uint32\_t parent\_lo;
uint32\_t key\_hi; /\*\*< upper 32-bits of key; zero pre-v2.22 \*/
uint32\_t parent\_hi;
uint16\_t type;
uint16\_t data\_len; /\* records limited to < 64kB \*/
uint32\_t data\_crc;
} beau\_header\_t;
#pragma pack(pop)
/\*\* sentinel to mark valid records \*/
extern const uint32\_t BEAU\_SENTINEL\_LIVE;
/\*\* sentinel to mark deleted records \*/
extern const uint32\_t BEAU\_SENTINEL\_DEAD;
/\*\* marks unused regions of flash \*/
extern const uint32\_t BEAU\_SENTINEL\_EMPTY;
typedef void beau\_list\_t;
void hton\_beau\_header(beau\_header\_t \* header);
#define hton\_beau\_header hton\_beau\_header
typedef void beau\_t;
extern beau\_t \*g\_beau\_db;
extern bool g\_beau\_dont\_create;
#define BEAU\_DEFAULT\_DB\_SIZE 0x400000
#define BEAU\_REC\_MAX\_SIZE 4000 // 96 bytes smaller than ESP32 page size
/\* port-specific public defines \*/
beau\_t \*Beau\_Init(const char \*partition\_name, uint32\_t partition\_default\_size);
void Beau\_Deinit(beau\_t \* b);
/\*\*
\* find a record and load it to caller buffer
\* @param key key to search for
\* @param bh [out] header of discovered (optional)
\* @param buf buffer to fill with loaded
\* @param buf\_size size of buffer
\* @param ntoh function to swap byte order of data structure.
\* Use this if your data structure contains any multi-byte
\* types such as uint32\_t or float.
\* May be NULL, in which case no endianness swap is performed.
\* @return true if record found
\* @note It is NOT an error if the buffer is too small,
\* the output will simply be truncated.
\* The caller may determine that buffer was
\* inadequate by comparing buf\_size with bh->data\_len.
\* If buffer is too large, we will simply only use the beginning.
\* The buffer will not be initialized to zero.
\* @note We have an ntoh argument to Beau\_Get/Set so as to enforce the client
\* to think about endianness. Otherwise we run the risk of having non-portable
\* data structures stored in the database.
\*/
bool Beau\_Get(beau\_t \* b,
uint64\_t key,
beau\_header\_t \* bh,
void \*buf, uint32\_t buf\_size, void (\*ntoh)(void \*)
);
/\*\*
\* find a record, allocate space for it, and return the buffer
\* @param key key to search for
\* @param bh [out] header of discovered (optional)
\* @param ntoh function to swap byte order of data structure.
\* Use this if your data structure contains any multi-byte
\* types such as uint32\_t or float.
\* May be NULL, in which case no endianness swap is performed.
\* @return NULL if not found,
\* otherwise, return buffer of lenth bh->data\_len
\* @note caller is responsible for freeing return buf with Osal\_Free
\*/
void \*Beau\_Get\_Alloc(beau\_t \* b,
uint64\_t key, beau\_header\_t \* bh, void (\*ntoh)(void \*)
);
/\*\*
\* write a record to the database (effectively overwriting any existing record)
\* @param key, parent, type, data\_len [in] header parameters of record to write
\* @param data [in] buffer of data to write
\* @param hton function to swap byte order of data structure.
\* Use this if your data structure contains any multi-byte
\* types such as uint32\_t or float.
\* May be NULL, in which case no endianness swap is performed.
\* @return true if record written successfully
\* @warning Buffer must not be read or written during Beau\_Set, due to high risk
\* of reading during endian swap! Make sure to use appropriate mutex
\* to lock your datastructure!
\* @note Compaction will occur if required,
\* and may introduce considerable latency
\* as the entire database is re-written.
\*/
bool Beau\_Set(beau\_t \* b,
uint64\_t key,
uint64\_t parent,
uint16\_t type, void \*data, uint32\_t data\_len, void (\*hton)(void \*)
);
/\*\*
\* mark a record as deleted
\* @param key record key to delete
\* @return true if record found and successfully crossed-out
\*/
bool Beau\_Delete(beau\_t \* b, uint64\_t key);
/\*\*
\* lock the database for exclusive access
\* @note blocks until lock obtained,
\* which can be a while if compaction is occuring
\*/
void Beau\_Lock(beau\_t \* b);
/\*\*
\* release the database mutex
\*/
void Beau\_Unlock(beau\_t \* b);
/\*\*
\* start an enumeration of all records of specified type
\* @param type type to enumerate, or BEAU\_TYPE\_ANY to enumerate all records
\* @note While multiple (nested) listings can be performed simultaneously,
\* be sure to lock with Beau\_Lock so that modifications to the
\* database do not cause missing or duplicate records to be returned.
\* @note In the future a reader/writer lock could be used here.
\* @return newly-allocated list object, or NULL if out of memory
\*/
beau\_list\_t \* Beau\_List\_Start(beau\_t \* b, uint16\_t type);
/\*\*
\* get next header in enumeration by type
\* @param list [in] list state from Beau\_List\_Start
\* This function shall return false if list is NULL.
\* [out] set to NULL on error, otherwise unmodified
\* @param bh [out] header found
\* @return true if bh valid, false if no more records
\* @note use Beau\_Get to obtain data corresponding to the record
\*/
bool Beau\_List\_Next(beau\_t \* b, beau\_list\_t \*\* list, beau\_header\_t \* bh);
/\*\*
\* scan and dump entire database to terminal
\*/
void Beau\_Print\_Dump(beau\_t \* b);
/\*\*
\* erase the underlying database partition
\* @warning: Do NOT use on the BOND ID partition!
\*/
void Beau\_Factory\_Reset(beau\_t \* b);
/\*\*
\* get information about geometry of underlying BFTL
\*/
bftl\_info\_t \*Beau\_Get\_BFTL\_Info(beau\_t \* b);
/\*\*
\* number of live records
\*/
int Beau\_Get\_Live\_Records(beau\_t \* b);
/\*\*
\* register API for specified Beau instance
\* @param b the beau instance for which to register the debug handler
\* @param name [optional] if not NULL, overrides the partition name from b
\*/
bool Beau\_API\_Init(beau\_t \* b, const char \* name);
/\*\*
\* get number of landmines
\* @param b
\* @return count
\*/
int Beau\_Landmine\_Count(beau\_t \* b);
/\*\*
\* get the 64-bit key given the beau header
\* @note Prior to v2.22 where we started storing the upper 32-bits,
\* we stored 0xFFFFFFFF in the reserved fields now used for key\_hi and
\* parent\_hi, however, the key/parent had a logical zero in the upper 32-bits.
\* For this reason, we map \*\_hi==0xFFFFFFFF to zero.
\* @param bh [description]
\* @return [description]
\*/
uint64\_t Beau\_Key\_Get(beau\_header\_t \* bh);
void Beau\_Key\_Set(beau\_header\_t \* bh, uint64\_t key);
uint64\_t Beau\_Parent\_Get(beau\_header\_t \* bh);
void Beau\_Parent\_Set(beau\_header\_t \* bh, uint64\_t parent);