-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrfidapp.hpp
200 lines (159 loc) · 6.25 KB
/
rfidapp.hpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
MIT License
Copyright(c) 2019 Evan Ross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* \file
* This smart contract "dApp" runs on the EOS blockchain to accept and
* manage RFID scanned tag data on chain.
* The scanned tag data is placed into one FIFO queue per EOS account.
* The data is available through standard EOS RPC API calls.
* The data for one account can be distinguished through device ID.
*
* This contract is written to an older but workable EOS contract spec:
* https://developers.eos.io/eosio-cpp/v1.1.0/docs
*
* \author
* Evan Ross <contact@firmwaremodules.com>
*/
#include <eosiolib/eosio.hpp>
#include <math.h>
#include <string>
using std::string;
using std::vector;
class rfidapp : public eosio::contract {
public:
rfidapp(account_name self) : contract(self), _scanners(self, self) {}
struct scandata {
uint32_t scan_time;
uint32_t recv_time;
uint32_t dev_id;
std::vector<uint8_t> tag_id; // 7 bytes
#if 0
// Non-default consructor now allowed.
scandata(uint32_t scan_time, uint32_t recv_time, uint32_t dev_id,
const std::vector<uint8_t> &tag_uid)
: scan_time(scan_time), recv_time(recv_time), dev_id(dev_id), tag_id(tag_uid)
{
//tag_id = tag_uid;
}
#endif
void reset() {
scan_time = 0;
recv_time = 0;
dev_id = 0;
tag_id.clear();
}
};
struct stats {
double min;
double max;
double var;
double mean;
void reset() {
min = HUGE_VAL;
max = 0.0;
var = 0.0;
mean = 0.0;
}
};
/*
* A scanner structure manages scanner data for one account.
* Typically one account is assigned to each device, however
* we do provision a "dev_id" field in each submitted scanner data item
* so that multiple devices can use one account. Suitable network
* resources must be provisioned for each account to support the number
* of transactions expected of the deployed RFID system.
*/
// @abi table scanners i64
struct scanner {
account_name name;
stats latency_stats;
/* FIFO of scan data. This is capped based on the ammount of RAM supplied. */
std::vector<scandata> scan_data;
uint32_t num_transactions;
uint32_t time_first_tx_s; /* Time the device first came on-line */
uint32_t time_last_tx_s; /* Time the device last submitted data */
scanner() {
reset_state();
}
void reset_state() {
latency_stats.reset();
scan_data.clear();
num_transactions = 0;
time_first_tx_s = 0;
time_last_tx_s = 0;
}
auto primary_key() const { return name; }
/* Accounts/devices with highest latency data */
/* Accounts/devices with most transactions (i.e. most used) */
EOSLIB_SERIALIZE(scanner, (name)(latency_stats)(scan_data)(num_transactions)(time_first_tx_s)(time_last_tx_s))
};
/* RFID scanner data
* View this on the chain with:
* cleos get table <contract account name> <scanner acccount name> scanners
* <code> <scope> <multi-index-table type name>
*/
typedef eosio::multi_index<N(scanners), scanner> scanners;
/// @abi action
/// Create a new scanner data storage table for the provided user account.
/// An existing account must be provided.
/// Fee: 0 EOS.
/// User account must have necessary RAM, CPU and bandwidth resources staked.
/// Returns: "Receipt" containing connection string for placement into RFID device.
void newscanner(account_name device);
/// @abi action
/// Create a new account with the necessary resources staked.
/// Fee: 0 EOS.
/// Requires necessary EOS. EOS resources can be polled via getresrouces() API.
/// Parameters:
/// Amount of EOS to transfer
/// New account public key
//
/// EOS is consumed and allocated as follows:
/// 1 EOS - fee
/// RAM (see resource allocator)
/// CPU - nominal - 0.1 EOS.
/// Remainder allocated to Bandwidth.
///
/// Returns:
/// "Receipt" containing connection string for placement into RFID device
/// including unique and randomly generated account name.
void newaccount();
/// @abi action
/// Reset.
void reset();
/// @abi action
/// Submit scanned tag data to the platform.
/// Parameters:
/// Account name
/// Device ID
/// Scan time - Posix time: seconds since epoch UTC (standard EOS/node Date format)
/// Tag UID - vector of 7 bytes.
void submit(account_name device, uint32_t device_id, uint32_t node_time, const std::vector<uint8_t> &tag_uid);
/// @abi action
/// Show version.
void version();
/// @abi action
/// Determine network resources for a given set of conditions.
/// Note: resources required to execute this API will be charged to the account calling it.
void resources();
private:
/* Instance of the scanners table with default code and scope (_self). */
scanners _scanners;
};