From 946967f09ce0ab9ab438a0647d393601dd0fca23 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 4 Dec 2006 18:23:06 +0000 Subject: Abstracted out part of the concept of an SPI device to a generalised NVS device. Separated the mechanisms of non-volatile storage access and non-volatile stored options. --- src/core/nvo.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/nvs.c | 106 ------------------------------------------------------- 2 files changed, 109 insertions(+), 106 deletions(-) create mode 100644 src/core/nvo.c delete mode 100644 src/core/nvs.c (limited to 'src/core') diff --git a/src/core/nvo.c b/src/core/nvo.c new file mode 100644 index 000000000..9bd92526a --- /dev/null +++ b/src/core/nvo.c @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2006 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +/** @file + * + * Non-volatile stored options + * + */ + +static size_t nvo_options_len ( struct nvs_options *nvo ) { + struct dhcp_option *option; + uint8_t sum; + unsigned int i; + size_t len; + + for ( sum = 0, i = 0 ; i < nvo->nvs->size ; i++ ) { + sum += * ( ( uint8_t * ) ( nvo->options->data + i ) ); + } + if ( sum != 0 ) { + DBG ( "NVO %p has bad checksum %02x; assuming empty\n", + nvo, sum ); + return 0; + } + + option = nvo->options->data; + if ( option->tag == DHCP_PAD ) { + DBG ( "NVO %p has bad start; assuming empty\n", nvo ); + return 0; + } + + option = find_dhcp_option ( nvo->options, DHCP_END ); + if ( ! option ) { + DBG ( "NVO %p has no end tag; assuming empty\n", nvo ); + return 0; + } + + len = ( ( void * ) option - nvo->options->data + 1 ); + DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n", + nvo, len, nvo->nvs->size ); + + return len; +} + +int nvo_register ( struct nvs_options *nvo ) { + struct dhcp_option *option; + int rc; + + nvo->options = alloc_dhcp_options ( nvo->nvs->size ); + if ( ! nvo->options ) { + DBG ( "NVO %p could not allocate %zd bytes\n", + nvo, nvo->nvs->size ); + rc = -ENOMEM; + goto err; + } + + if ( ( rc = nvo->nvs->read ( nvo->nvs, 0, nvo->options->data, + nvo->nvs->size ) ) != 0 ) { + DBG ( "NVO %p could not read [0,%zd)\n", + nvo, nvo->nvs->size ); + goto err; + } + + nvo->options->len = nvo->options->max_len; + nvo->options->len = nvo_options_len ( nvo ); + if ( ! nvo->options->len ) { + option = nvo->options->data; + option->tag = DHCP_END; + nvo->options->len = 1; + } + + register_dhcp_options ( nvo->options ); + + return 0; + + err: + + free_dhcp_options ( nvo->options ); + nvo->options = NULL; + return rc; +} + +void nvo_unregister ( struct nvs_options *nvo ) { + if ( nvo->options ) { + unregister_dhcp_options ( nvo->options ); + free_dhcp_options ( nvo->options ); + nvo->options = NULL; + } +} diff --git a/src/core/nvs.c b/src/core/nvs.c deleted file mode 100644 index 9b4d1310a..000000000 --- a/src/core/nvs.c +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2006 Michael Brown . - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or any later version. - * - * This program 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 - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include -#include -#include -#include - -/** @file - * - * Non-volatile storage - * - */ - -static size_t nvs_options_len ( struct nvs_device *nvs ) { - struct dhcp_option *option; - uint8_t sum; - unsigned int i; - size_t len; - - for ( sum = 0, i = 0 ; i < nvs->len ; i++ ) { - sum += * ( ( uint8_t * ) ( nvs->options->data + i ) ); - } - if ( sum != 0 ) { - DBG ( "NVS %p has bad checksum %02x; assuming empty\n", - nvs, sum ); - return 0; - } - - option = nvs->options->data; - if ( option->tag == DHCP_PAD ) { - DBG ( "NVS %p has bad start; assuming empty\n", nvs ); - return 0; - } - - option = find_dhcp_option ( nvs->options, DHCP_END ); - if ( ! option ) { - DBG ( "NVS %p has no end tag; assuming empty\n", nvs ); - return 0; - } - - len = ( ( void * ) option - nvs->options->data + 1 ); - DBG ( "NVS %p contains %zd bytes of options (maximum %zd)\n", - nvs, len, nvs->len ); - - return len; -} - -int nvs_register ( struct nvs_device *nvs ) { - struct dhcp_option *option; - int rc; - - nvs->options = alloc_dhcp_options ( nvs->len ); - if ( ! nvs->options ) { - DBG ( "NVS %p could not allocate %zd bytes\n", nvs, nvs->len ); - rc = -ENOMEM; - goto err; - } - - if ( ( rc = nvs->op->read ( nvs, 0, nvs->options->data, - nvs->len ) ) != 0 ) { - DBG ( "NVS %p could not read [0,%zd)\n", nvs, nvs->len ); - goto err; - } - - nvs->options->len = nvs->options->max_len; - nvs->options->len = nvs_options_len ( nvs ); - if ( ! nvs->options->len ) { - option = nvs->options->data; - option->tag = DHCP_END; - nvs->options->len = 1; - } - - register_dhcp_options ( nvs->options ); - - return 0; - - err: - - free_dhcp_options ( nvs->options ); - nvs->options = NULL; - return rc; -} - -void nvs_unregister ( struct nvs_device *nvs ) { - if ( nvs->options ) { - unregister_dhcp_options ( nvs->options ); - free_dhcp_options ( nvs->options ); - nvs->options = NULL; - } -} -- cgit v1.2.3-55-g7522