sysparams: get/set int32 and int8 apis. (#209)

Rename the get/set_int api functions to get/set_int32.

Add get/set_int8 api functions too in anticipation of more efficient implementations for these.
This commit is contained in:
Our Air Quality 2016-09-15 16:17:26 +10:00 committed by Johan Kanflo
parent 97de07f8b2
commit 4c78db81d8
2 changed files with 68 additions and 13 deletions

View file

@ -169,7 +169,7 @@ sysparam_status_t sysparam_get_info(uint32_t *base_addr, uint32_t *num_sectors);
*/ */
sysparam_status_t sysparam_get_data(const char *key, uint8_t **destptr, size_t *actual_length, bool *is_binary); sysparam_status_t sysparam_get_data(const char *key, uint8_t **destptr, size_t *actual_length, bool *is_binary);
/** Get the value associate with a key (static buffers only) /** Get the value associated with a key (static buffers only)
* *
* This performs the same function as sysparam_get_data() but without * This performs the same function as sysparam_get_data() but without
* performing any memory allocations. It can thus be used before the heap has * performing any memory allocations. It can thus be used before the heap has
@ -231,8 +231,7 @@ sysparam_status_t sysparam_get_string(const char *key, char **destptr);
/** Get the int32_t value associated with a key /** Get the int32_t value associated with a key
* *
* This routine can be used if you know that the value in a key will (or at * This routine can be used if you know that the value in a key will (or at
* least should) be an integer value. It will parse the stored data as a * least should) be an int32_t value.
* number (in standard decimal or "0x" hex notation) and return the result.
* *
* Note: If the status result is anything other than ::SYSPARAM_OK, the value * Note: If the status result is anything other than ::SYSPARAM_OK, the value
* in `result` is not changed. This means it is possible to set a default * in `result` is not changed. This means it is possible to set a default
@ -251,7 +250,31 @@ sysparam_status_t sysparam_get_string(const char *key, char **destptr);
* @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data * @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data
* @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash * @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash
*/ */
sysparam_status_t sysparam_get_int(const char *key, int32_t *result); sysparam_status_t sysparam_get_int32(const char *key, int32_t *result);
/** Get the int8_t value associated with a key
*
* This routine can be used if you know that the value in a key will (or at
* least should) be a uint8_t binary value.
*
* Note: If the status result is anything other than ::SYSPARAM_OK, the value
* in `result` is not changed. This means it is possible to set a default
* value before calling this function which will be left as-is if a sysparam
* value could not be successfully read.
*
* @param[in] key Key name (zero-terminated string)
* @param[out] result Pointer to a location to hold returned boolean value
*
* @retval ::SYSPARAM_OK Value successfully retrieved.
* @retval ::SYSPARAM_NOTFOUND Key/value not found.
* @retval ::SYSPARAM_PARSEFAILED The retrieved value could not be parsed as a
* boolean setting.
* @retval ::SYSPARAM_ERR_NOINIT sysparam_init() must be called first
* @retval ::SYSPARAM_ERR_NOMEM Unable to allocate memory
* @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data
* @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash
*/
sysparam_status_t sysparam_get_int8(const char *key, int8_t *result);
/** Get the boolean value associated with a key /** Get the boolean value associated with a key
* *
@ -333,9 +356,8 @@ sysparam_status_t sysparam_set_string(const char *key, const char *value);
/** Set a key's value as a number /** Set a key's value as a number
* *
* Converts an int32_t value to a decimal number and writes it to the * Write an int32_t binary value to the specified key. This does the inverse of
* specified key. This does the inverse of the sysparam_get_int() * the sysparam_get_int32() function.
* function.
* *
* @param[in] key Key name (zero-terminated string) * @param[in] key Key name (zero-terminated string)
* @param[in] value Value to set * @param[in] value Value to set
@ -348,7 +370,28 @@ sysparam_status_t sysparam_set_string(const char *key, const char *value);
* @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data * @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data
* @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash * @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash
*/ */
sysparam_status_t sysparam_set_int(const char *key, int32_t value); sysparam_status_t sysparam_set_int32(const char *key, int32_t value);
/** Set a key's value as a number
*
* Write an int8_t binary value to the specified key. This does the inverse of
* the sysparam_get_int8() function.
*
* Note that if the key already contains a value which parses to the same
* boolean (true/false) value, it is left unchanged.
*
* @param[in] key Key name (zero-terminated string)
* @param[in] value Value to set
*
* @retval ::SYSPARAM_OK Value successfully set.
* @retval ::SYSPARAM_ERR_BADVALUE An empty key was provided.
* @retval ::SYSPARAM_ERR_FULL No space left in sysparam area
* (or too many keys in use)
* @retval ::SYSPARAM_ERR_NOMEM Unable to allocate memory
* @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data
* @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash
*/
sysparam_status_t sysparam_set_int8(const char *key, int8_t value);
/** Set a key's value as a boolean (yes/no) string /** Set a key's value as a boolean (yes/no) string
* *
@ -356,9 +399,6 @@ sysparam_status_t sysparam_set_int(const char *key, int32_t value);
* specified key. This does the inverse of the sysparam_get_bool() * specified key. This does the inverse of the sysparam_get_bool()
* function. * function.
* *
* Note that if the key already contains a value which parses to the same
* boolean (true/false) value, it is left unchanged.
*
* @param[in] key Key name (zero-terminated string) * @param[in] key Key name (zero-terminated string)
* @param[in] value Value to set * @param[in] value Value to set
* *

View file

@ -724,7 +724,7 @@ sysparam_status_t sysparam_get_string(const char *key, char **destptr) {
return SYSPARAM_OK; return SYSPARAM_OK;
} }
sysparam_status_t sysparam_get_int(const char *key, int32_t *result) { sysparam_status_t sysparam_get_int32(const char *key, int32_t *result) {
char *buffer; char *buffer;
char *endptr; char *endptr;
int32_t value; int32_t value;
@ -744,6 +744,17 @@ sysparam_status_t sysparam_get_int(const char *key, int32_t *result) {
return SYSPARAM_OK; return SYSPARAM_OK;
} }
sysparam_status_t sysparam_get_int8(const char *key, int8_t *result) {
int32_t value;
sysparam_status_t status;
status = sysparam_get_int32(key, &value);
if (status == SYSPARAM_OK) {
*result = value;
}
return status;
}
sysparam_status_t sysparam_get_bool(const char *key, bool *result) { sysparam_status_t sysparam_get_bool(const char *key, bool *result) {
char *buffer; char *buffer;
sysparam_status_t status; sysparam_status_t status;
@ -967,7 +978,7 @@ sysparam_status_t sysparam_set_string(const char *key, const char *value) {
return sysparam_set_data(key, (const uint8_t *)value, strlen(value), false); return sysparam_set_data(key, (const uint8_t *)value, strlen(value), false);
} }
sysparam_status_t sysparam_set_int(const char *key, int32_t value) { sysparam_status_t sysparam_set_int32(const char *key, int32_t value) {
uint8_t buffer[12]; uint8_t buffer[12];
int len; int len;
@ -975,6 +986,10 @@ sysparam_status_t sysparam_set_int(const char *key, int32_t value) {
return sysparam_set_data(key, buffer, len, false); return sysparam_set_data(key, buffer, len, false);
} }
sysparam_status_t sysparam_set_int8(const char *key, int8_t value) {
return sysparam_set_int32(key, value);
}
sysparam_status_t sysparam_set_bool(const char *key, bool value) { sysparam_status_t sysparam_set_bool(const char *key, bool value) {
uint8_t buf[4] = {0xff, 0xff, 0xff, 0xff}; uint8_t buf[4] = {0xff, 0xff, 0xff, 0xff};
bool old_value; bool old_value;