-
Notifications
You must be signed in to change notification settings - Fork 13
/
commonregs.h
executable file
·224 lines (212 loc) · 12.8 KB
/
commonregs.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 07/06/2011
*/
#ifndef _COMMONREGS_H
#define _COMMONREGS_H
#include "swstatus.h"
#include "nvolat.h"
#include "register.h"
#include "register.h"
#include "swap.h"
const void setSysState(uint8_t id, uint8_t *state);
const void setFreqChannel(uint8_t id, uint8_t *channel);
const void setDevAddress(uint8_t id, uint8_t *addr);
const void setNetworkId(uint8_t rId, uint8_t *nId);
const void setTxInterval(uint8_t id, uint8_t *interval);
#define DECLARE_COMMON_CALLBACKS()
/**
* Macros for the definition of common register indexes
*/
#define DEFINE_COMMON_REGINDEX_START() \
enum CUSTOM_REGINDEX \
{ \
REGI_PRODUCTCODE = 0, \
REGI_HWVERSION, \
REGI_FWVERSION, \
REGI_SYSSTATE, \
REGI_FREQCHANNEL, \
REGI_SECUOPTION, \
REGI_PASSWORD, \
REGI_SECUNONCE, \
REGI_NETWORKID, \
REGI_DEVADDRESS, \
REGI_TXINTERVAL,
#define DEFINE_COMMON_REGINDEX_END() };
#define DEFINE_REGINDEX_START() DEFINE_COMMON_REGINDEX_START()
#define DEFINE_REGINDEX_END() DEFINE_COMMON_REGINDEX_END()
/**
* Macro for the definition of registers common to all SWAP devices
*/
#define DEFINE_COMMON_REGISTERS() \
/* Product code */ \
static uint8_t dtProductCode[8] = {(uint8_t)(SWAP_MANUFACT_ID >> 24), (uint8_t)(SWAP_MANUFACT_ID >> 16) , (uint8_t)(SWAP_MANUFACT_ID >> 8), (uint8_t)(SWAP_MANUFACT_ID), \
(uint8_t)(SWAP_PRODUCT_ID >> 24), (uint8_t)(SWAP_PRODUCT_ID >> 16) , (uint8_t)(SWAP_PRODUCT_ID >> 8), (uint8_t)(SWAP_PRODUCT_ID)}; \
REGISTER regProductCode(dtProductCode, sizeof(dtProductCode), NULL, NULL); \
/* Hardware version */ \
static uint8_t dtHwVersion[4] = {(uint8_t)(HARDWARE_VERSION >> 24), (uint8_t)(HARDWARE_VERSION >> 16) , (uint8_t)(HARDWARE_VERSION >> 8), (uint8_t)(HARDWARE_VERSION)}; \
REGISTER regHwVersion(dtHwVersion, sizeof(dtHwVersion), NULL, NULL); \
/* Firmware version */ \
static uint8_t dtFwVersion[4] = {(uint8_t)(FIRMWARE_VERSION >> 24), (uint8_t)(FIRMWARE_VERSION >> 16) , (uint8_t)(FIRMWARE_VERSION >> 8), (uint8_t)(FIRMWARE_VERSION)}; \
REGISTER regFwVersion(dtFwVersion, sizeof(dtFwVersion), NULL, NULL); \
/* System state */ \
REGISTER regSysState(&swap.systemState, sizeof(swap.systemState), NULL, &setSysState); \
/* Frequency channel */ \
REGISTER regFreqChannel(&panstamp.radio.channel, sizeof(panstamp.radio.channel), NULL, &setFreqChannel, SWDTYPE_INTEGER, NVOLAT_FREQ_CHANNEL); \
/* Security option */ \
REGISTER regSecuOption(&swap.security, sizeof(swap.security), NULL, NULL); \
/* Security password (not implemented yet) */ \
static uint8_t dtPassword[1]; \
REGISTER regPassword(dtPassword, sizeof(dtPassword), NULL, NULL); \
/* Security nonce */ \
REGISTER regSecuNonce(&swap.nonce, sizeof(swap.nonce), NULL, NULL); \
/* Network Id */ \
REGISTER regNetworkId(panstamp.radio.syncWord, sizeof(panstamp.radio.syncWord), NULL, &setNetworkId, SWDTYPE_OTHER, NVOLAT_SYNC_WORD); \
/* Device address */ \
REGISTER regDevAddress((uint8_t*)&swap.devAddress, sizeof(swap.devAddress), NULL, &setDevAddress, SWDTYPE_INTEGER, NVOLAT_DEVICE_ADDRESS); \
/* Periodic Tx interval */ \
REGISTER regTxInterval((uint8_t*)&swap.txInterval, sizeof(swap.txInterval), NULL, &setTxInterval, SWDTYPE_INTEGER, NVOLAT_TX_INTERVAL);
/**
* Macros for the declaration of global table of registers
*/
#define DECLARE_REGISTERS_START() \
REGISTER *regTable[] = { \
®ProductCode, \
®HwVersion, \
®FwVersion, \
®SysState, \
®FreqChannel, \
®SecuOption, \
®Password, \
®SecuNonce, \
®NetworkId, \
®DevAddress, \
®TxInterval,
#define DECLARE_REGISTERS_END() \
}; \
/* Size of regTable */ \
uint8_t regTableSize = sizeof(regTable)/sizeof(*regTable);
/**
* Macro for the definition of getter/setter functions related to all common registers
*/
#define DEFINE_COMMON_CALLBACKS() \
/** \
* setSysState \
* \
* Set system state \
* \
* 'id' Register ID \
* 'state' New system state \
*/ \
const void setSysState(uint8_t id, uint8_t *state) \
{ \
swap.systemState = state[0]; \
switch(state[0]) \
{ \
case SYSTATE_RESTART: \
/* Send status message before restarting the mote */ \
swap.getRegister(REGI_SYSSTATE)->sendSwapStatus(); \
panstamp.reset(); \
break; \
case SYSTATE_UPGRADE: \
panstamp.goToWirelessBoot(); \
break; \
default: \
break; \
} \
} \
\
/** \
* setFreqChannel \
* \
* Set frequency channel \
* \
* 'id' Register ID \
* 'channel' New channel \
*/ \
const void setFreqChannel(uint8_t id, uint8_t *channel) \
{ \
if (channel[0] != regFreqChannel.value[0]) \
{ \
/* Send status message before entering the new \
frequency channel */ \
SWSTATUS packet = SWSTATUS(regFreqChannel.id, channel, regFreqChannel.length); \
packet.send(); \
/* Update register value */ \
panstamp.radio.setChannel(channel[0]); \
} \
} \
\
\
/** \
* setDevAddress \
* \
* Set device address \
* \
* 'id' Register ID \
* 'addr' New device address \
*/ \
const void setDevAddress(uint8_t id, uint8_t *addr) \
{ \
/* Send status before setting the new address */ \
SWSTATUS packet = SWSTATUS(regDevAddress.id, addr, regDevAddress.length); \
packet.send(); \
/* Set new SWAP address. BE to LE conversion */ \
regDevAddress.setValueFromBeBuffer(addr); \
/* Update register value */ \
panstamp.radio.setDevAddress(addr[regDevAddress.length-1]); \
} \
\
/** \
* setNetworkId \
* \
* Set network id \
* \
* 'rId' Register ID \
* 'nId' New network id \
*/ \
const void setNetworkId(uint8_t rId, uint8_t *nId) \
{ \
if ((nId[0] != regNetworkId.value[0]) || \
(nId[1] != regNetworkId.value[1])) \
{ \
/* Send status before taking the new network ID */ \
SWSTATUS packet = SWSTATUS(regNetworkId.id, nId, regNetworkId.length); \
packet.send(); \
/* Update register value */ \
panstamp.radio.setSyncWord(nId); \
} \
} \
/** \
* setTxInterval \
* \
* Set periodic Tx interval \
* \
* 'id' Register ID \
* 'interval' New interval (in seconds) \
*/ \
const void setTxInterval(uint8_t id, uint8_t *interval) \
{ \
/* Set new Tx interval. BE to LE conversion */ \
regTxInterval.setValueFromBeBuffer(interval); \
}
#endif