-
Notifications
You must be signed in to change notification settings - Fork 9
/
JsonRPCServer.h
81 lines (61 loc) · 1.84 KB
/
JsonRPCServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
JsonRPCServer.h - Simple JSON-RPC Server for Arduino
Created by Meir Tseitlin, March 5, 2014. This code is based on https://code.google.com/p/ajson-rpc/
Released under GPLv2 license.
*/
#ifndef JsonRPC_h
#define JsonRPC_h
#include "Arduino.h"
#include <Stream.h>
#include "aJSON.h"
enum JSON_RPC_RET_TYPE {
JSON_RPC_RET_TYPE_NONE,
JSON_RPC_RET_TYPE_NUMERIC,
JSON_RPC_RET_TYPE_STRING
};
class JsonRPCServer;
typedef void (JsonRPCServer::*JSON_PROC_T)(aJsonObject*);
typedef void (*JSON_PROC_STATIC_T)(JsonRPCServer*, aJsonObject*);
typedef int (JsonRPCServer::*JSON_PROC_NUM_T)(aJsonObject*);
typedef int (*JSON_PROC_NUM_STATIC_T)(JsonRPCServer*, aJsonObject*);
typedef String (JsonRPCServer::*JSON_PROC_STRING_T)(aJsonObject*);
typedef String (*JSON_PROC_STRING_STATIC_T)(JsonRPCServer*, aJsonObject*);
struct Mapping
{
String name;
JSON_PROC_STATIC_T callback;
JSON_RPC_RET_TYPE retType;
};
struct FuncMap
{
Mapping* mappings;
unsigned int capacity;
unsigned int used;
};
#define DECLARE_JSON_PROC(CONTAINER_NAME, METHOD_NAME, RET_TYPE) \
RET_TYPE METHOD_NAME(aJsonObject* params); \
static RET_TYPE stat_##METHOD_NAME(CONTAINER_NAME* container, aJsonObject* params) { \
return container->METHOD_NAME(params); \
}
#define BEGIN_JSON_REGISTRATION \
protected: \
void registerProcs() {
#define REGISTER_JSON_PROC(METHOD_NAME, RET_TYPE) \
registerMethod(#METHOD_NAME, (JSON_PROC_STATIC_T) &stat_##METHOD_NAME, RET_TYPE)
#define END_JSON_REGISTRATION \
}
class JsonRPCServer
{
public:
JsonRPCServer(Stream* stream);
void begin(int capacity);
void process();
protected:
void registerMethod(String methodName, JSON_PROC_STATIC_T callback, JSON_RPC_RET_TYPE type = JSON_RPC_RET_TYPE_NONE);
void processMessage(aJsonObject *msg);
virtual void registerProcs() = 0;
private:
FuncMap* mymap;
aJsonStream _jsonStream;
};
#endif