32 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
36 #define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
42 #include <avr/pgmspace.h>
43 #include <avr/eeprom.h>
52 #include "contiki-net.h"
59 #if CONTIKI_CONF_RANDOM_MAC
60 extern uint8_t rng_get_uint8(
void);
62 generate_new_eui64(uint8_t eui64[8]) {
64 eui64[1] = rng_get_uint8();
65 eui64[2] = rng_get_uint8();
68 eui64[5] = rng_get_uint8();
69 eui64[6] = rng_get_uint8();
70 eui64[7] = rng_get_uint8();
76 extern uint8_t default_mac_address[8];
77 extern uint8_t default_server_name[16];
78 extern uint8_t default_domain_name[30];
80 uint8_t default_mac_address[8] PROGMEM = PARAMS_EUI64ADDR;
81 uint8_t default_server_name[] PROGMEM = PARAMS_SERVERNAME;
82 uint8_t default_domain_name[] PROGMEM = PARAMS_DOMAINNAME;
85 #if PARAMETER_STORAGE==0
88 params_get_eui64(uint8_t *eui64) {
89 #if CONTIKI_CONF_RANDOM_MAC
90 PRINTD(
"Generating random EUI64 MAC\n");
91 generate_new_eui64(eui64);
95 for (i=0;i<
sizeof(default_mac_address);i++) eui64[i] = pgm_read_byte_near(default_mac_address+i);
99 #elif PARAMETER_STORAGE==1
107 uint8_t eemem_mac_address[] EEMEM = PARAMS_EUI64ADDR;
108 uint8_t eemem_server_name[] EEMEM = PARAMS_SERVERNAME;
109 uint8_t eemem_domain_name[] EEMEM = PARAMS_DOMAINNAME;
112 uint16_t eemem_nodeid EEMEM = PARAMS_NODEID;
113 uint8_t eemem_channel[2] EEMEM = {PARAMS_CHANNEL, ~PARAMS_CHANNEL};
114 uint16_t eemem_panid EEMEM = PARAMS_PANID;
115 uint16_t eemem_panaddr EEMEM = PARAMS_PANADDR;
116 uint8_t eemem_txpower EEMEM = PARAMS_TXPOWER;
118 #if CONTIKI_CONF_RANDOM_MAC
119 static uint8_t randomeui64;
123 params_get_channel(
void) {
125 *(uint16_t *)x = eeprom_read_word ((uint16_t *)&eemem_channel);
127 if( (x[0]<11) || (x[0] > 26)) x[1]=x[0];
129 if((uint8_t)x[0]!=(uint8_t)~x[1]) {
131 uint8_t i,buffer[32];
132 PRINTD(
"EEPROM is corrupt, rewriting with defaults.\n");
133 #if CONTIKI_CONF_RANDOM_MAC
134 PRINTD(
"Generating random EUI64 MAC\n");
135 generate_new_eui64(&buffer);
138 for (i=0;i<
sizeof(default_mac_address);i++) buffer[i] = pgm_read_byte_near(default_mac_address+i);
142 eeprom_write_block(&buffer, &eemem_mac_address,
sizeof(eemem_mac_address));
143 for (i=0;i<
sizeof(default_server_name);i++) buffer[i] = pgm_read_byte_near(default_server_name+i);
144 eeprom_write_block(&buffer, &eemem_server_name,
sizeof(eemem_server_name));
145 for (i=0;i<
sizeof(default_domain_name);i++) buffer[i] = pgm_read_byte_near(default_domain_name+i);
146 eeprom_write_block(&buffer, &eemem_domain_name,
sizeof(eemem_domain_name));
147 eeprom_write_word(&eemem_panid , PARAMS_PANID);
148 eeprom_write_word(&eemem_panaddr, PARAMS_PANADDR);
149 eeprom_write_byte(&eemem_txpower, PARAMS_TXPOWER);
150 eeprom_write_word(&eemem_nodeid, PARAMS_NODEID);
151 x[0] = PARAMS_CHANNEL;
153 eeprom_write_word((uint16_t *)&eemem_channel, *(uint16_t *)x);
160 params_get_eui64(uint8_t *eui64) {
162 eeprom_read_block ((
void *)eui64, &eemem_mac_address,
sizeof(rimeaddr_t));
164 #if CONTIKI_CONF_RANDOM_MAC
171 params_get_panid(
void) {
172 return eeprom_read_word(&eemem_panid);
175 params_get_panaddr(
void) {
176 return eeprom_read_word (&eemem_panaddr);
179 params_get_txpower(
void)
181 return eeprom_read_byte(&eemem_txpower);
187 params_get_channel() {
190 if (settings_get(SETTINGS_KEY_CHANNEL, 0,(
unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
191 PRINTD(
"<-Get RF channel %u\n",x);
194 if (settings_add_uint8(SETTINGS_KEY_CHANNEL,x ) == SETTINGS_STATUS_OK) {
195 PRINTD(
"->Set EEPROM RF channel to %d\n",x);
201 params_get_eui64(uint8_t *eui64) {
202 size_t size =
sizeof(rimeaddr_t);
203 if(settings_get(SETTINGS_KEY_EUI64, 0, (
unsigned char*)eui64, &size) == SETTINGS_STATUS_OK) {
204 PRINTD(
"<-Get EUI64 MAC\n");
207 #if CONTIKI_CONF_RANDOM_MAC
208 PRINTD(
"Generating random EUI64 MAC\n");
209 generate_new_eui64(eui64);
211 {uint8_t i;
for (i=0;i<8;i++) eui64[i] = pgm_read_byte_near(default_mac_address+i);}
213 if (settings_add(SETTINGS_KEY_EUI64,(
unsigned char*)eui64,8) == SETTINGS_STATUS_OK) {
214 PRINTD(
"->Set EEPROM MAC address\n");
216 #if CONTIKI_CONF_RANDOM_MAC
223 params_get_panid(
void) {
226 if (settings_get(SETTINGS_KEY_PAN_ID, 0,(
unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
227 PRINTD(
"<-Get PAN ID of %04x\n",x);
230 if (settings_add_uint16(SETTINGS_KEY_PAN_ID,x)==SETTINGS_STATUS_OK) {
231 PRINTD(
"->Set EEPROM PAN ID to %04x\n",x);
237 params_get_panaddr(
void) {
240 if (settings_get(SETTINGS_KEY_PAN_ADDR, 0,(
unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
241 PRINTD(
"<-Get PAN address of %04x\n",x);
244 if (settings_add_uint16(SETTINGS_KEY_PAN_ADDR,x)==SETTINGS_STATUS_OK) {
245 PRINTD(
"->Set EEPROM PAN address to %04x\n",x);
251 params_get_txpower(
void) {
254 if (settings_get(SETTINGS_KEY_TXPOWER, 0,(
unsigned char*)&x, &size) == SETTINGS_STATUS_OK) {
255 PRINTD(
"<-Get tx power of %d (0=max)\n",x);
258 if (settings_add_uint8(SETTINGS_KEY_TXPOWER,x)==SETTINGS_STATUS_OK) {
259 PRINTD(
"->Set EEPROM tx power of %d (0=max)\n",x);