Newer
Older
/****************************************************************************
* hn70ap/apps/libhn70ap/eeprom.c
*
* Copyright (C) 2018 Sebastien Lorquet. All rights reserved.
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/* EEPROM configuration management */
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <hn70ap/eeprom.h>
struct param_s
{
const char *name; /* Config name */
int type; /* Config type */
int addr; /* Storage address of first byte in EEPROM */
int mask; /* For boolean configs, mask value of bit config */
};
/* EEPROM configuration
* Address length Default Name Type Desciption
* 0 : Config validation marker. If FF, EEPROM is virgin, and default config is loaded
* 1-127 : Config entries described below
*/
const struct param_s eeconfig_params[] =
{
{"dhcp", EECONFIG_TYPE_BOOL, 0x01, 0x01}, /* 0x01 - 0x01 DHCP Client Enable */
/* 0x02 - 0x03 RFU for more network options */
{"ip" , EECONFIG_TYPE_IP , 0x04, 0}, /* 0x04 - 0x07 Static IP address to use if DHCP is not enabled */
{"mask", EECONFIG_TYPE_IP , 0x08, 0}, /* 0x08 - 0x0B Static IP mask to use if DHCP is not enabled */
{"gw" , EECONFIG_TYPE_IP , 0x0C, 0}, /* 0x0C - 0x0F IP address of default gateway */
{"dns" , EECONFIG_TYPE_IP , 0x10, 0}, /* 0x10 - 0x13 IP address of DNS server (independent of DHCP enable)*/
{"call", EECONFIG_TYPE_CALL, 0x14, 0}, /* 0x14 - 0x1B HAM callsign, zero padded */
{"ssid", EECONFIG_TYPE_BYTE, 0x1C, 0}, /* 0x1C - 0x1C HAM Station ID */
/* 0x1D - 0x7F RFU for more options */
};
#define EECONFIG_PARAMS_COUNT (sizeof(eeconfig_params) / sizeof(eeconfig_params[0]))
#define HN70AP_CONFIG_INSTALLED 0x42 //Vlue set at address zero of eeprom if
/*----------------------------------------------------------------------------*/
uint8_t buf;
int ret;
*default_set = false;
/* Read the configuration enabled marker */
ret = hn70ap_eeprom_read(0, &buf, 1);
if(ret != OK)
{
return ret;
}
/* Not set? Set defaults */
if( buf != HN70AP_CONFIG_INSTALLED)
{
*default_set = true;
/* Set network address mode to DHCP */
/* Clean network address and mask */
buf = HN70AP_CONFIG_INSTALLED;
hn70ap_eeprom_write(0, &buf, 1);
}
/* Done */
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
/*----------------------------------------------------------------------------*/
int hn70ap_eeprom_read(uint32_t addr, uint8_t *buf, uint32_t len)
{
int fd;
fd = open(HN70AP_EECONFIG_DEVICE, O_RDONLY);
if(fd < 0)
{
return ERROR;
}
if(lseek(fd, addr, SEEK_SET) != OK)
{
close(fd);
return ERROR;
}
if(read(fd, buf, len) != len)
{
close(fd);
return ERROR;
}
close(fd);
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeprom_write(uint32_t addr, uint8_t *buf, uint32_t len)
{
int fd;
fd = open(HN70AP_EECONFIG_DEVICE, O_WRONLY);
if(fd < 0)
{
return ERROR;
}
if(lseek(fd, addr, SEEK_SET) != OK)
{
close(fd);
return ERROR;
}
if(write(fd, buf, len) != len)
{
close(fd);
return ERROR;
}
close(fd);
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_describe(int index, char *namebuf, int namebuflen, uint32_t *type)
{
if(index < 0) return ERROR;
if(index >= EECONFIG_PARAMS_COUNT) return ERROR;
if(namebuf == NULL) return ERROR;
strncpy(namebuf, eeconfig_params[index].name, namebuflen);
if(type)
{
*type = eeconfig_params[index].type;
}
return OK;
}
/*----------------------------------------------------------------------------*/
{
int i;
for(i = 0; i < EECONFIG_PARAMS_COUNT; i++)
{
if(!strcmp(name, eeconfig_params[i].name))
{
return i;
}
}
return -1;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_getbool(char *name, bool *value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
uint8_t buf;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_BOOL)
{
return ERROR;
}
ret = hn70ap_eeprom_read(eeconfig_params[index].addr, &buf, 1);
if(ret != OK)
{
return ERROR;
}
*value = (buf & eeconfig_params[index].mask) == eeconfig_params[index].mask;
return OK;
}
/*----------------------------------------------------------------------------*/
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_IP)
{
return ERROR;
}
ret = hn70ap_eeprom_read(eeconfig_params[index].addr, (uint8_t*)value, 4);
if(ret != OK)
{
return ERROR;
}
return OK;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_getbyte(char *name, uint8_t *value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_BYTE)
{
return ERROR;
}
ret = hn70ap_eeprom_read(eeconfig_params[index].addr, value, 1);
if(ret != OK)
{
return ERROR;
}
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_getcall(char *name, char *value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_CALL)
{
return ERROR;
}
ret = hn70ap_eeprom_read(eeconfig_params[index].addr, (uint8_t*)value, 8);
if(ret != OK)
{
return ERROR;
}
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_setbool(char *name, bool value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
uint8_t buf;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_BOOL)
{
return ERROR;
}
ret = hn70ap_eeprom_read(eeconfig_params[index].addr, &buf, 1);
if(ret != OK)
{
return ERROR;
}
if(value)
{
buf |= eeconfig_params[index].mask;
}
else
{
buf &= ~eeconfig_params[index].mask;
}
ret = hn70ap_eeprom_write(eeconfig_params[index].addr, &buf, 1);
if(ret != OK)
{
return ERROR;
}
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_setip(char *name, in_addr_t value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_IP)
{
return ERROR;
}
ret = hn70ap_eeprom_write(eeconfig_params[index].addr, (uint8_t*)&value, 4);
if(ret != OK)
{
return ERROR;
}
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_setbyte(char *name, uint8_t value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_BYTE)
{
return ERROR;
}
ret = hn70ap_eeprom_write(eeconfig_params[index].addr, &value, 1);
if(ret != OK)
{
return ERROR;
}
return OK;
}
/*----------------------------------------------------------------------------*/
int hn70ap_eeconfig_setcall(char *name, char *value)
{
int index = hn70ap_eeconfig_find(name);
int ret;
if(index < 0)
{
return ERROR;
}
if(eeconfig_params[index].type != EECONFIG_TYPE_CALL)
{
return ERROR;
}
ret = hn70ap_eeprom_write(eeconfig_params[index].addr, (uint8_t*)value, 8);
if(ret != OK)
{
return ERROR;
}
return OK;
}