qcacmn: Add qdf_file and qdf_parse files to QDF

Add an QDF abstraction for reading a file. Also, add the first consumer,
qdf_ini_parse(), as well. This is pure parsing logic, which offloads the
actual content handling to the caller via callback functions.

Change-Id: Idfca74c5543a5b0da7ddd1bdc6e2ad2be59ed36b
CRs-Fixed: 2184463
This commit is contained in:
Dustin Brown
2018-02-02 14:15:19 -08:00
committed by snandini
parent a4fd883481
commit 677a261213
4 changed files with 310 additions and 0 deletions

54
qdf/inc/qdf_file.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2018 The Linux Foundation. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* DOC: Thin filesystem API abstractions
*/
#ifndef __QDF_FILE_H
#define __QDF_FILE_H
#include "qdf_status.h"
/**
* qdf_file_read() - read the entire contents of a file
* @path: the full path of the file to read
* @out_buf: double pointer for referring to the file contents buffer
*
* This API allocates a new, null-terminated buffer containing the contents of
* the file at @path. On success, @out_buf points to this new buffer, otherwise
* @out_buf is set to NULL.
*
* Consumers must free the allocated buffer by calling qdf_file_buf_free().
*
* Return: QDF_STATUS
*/
QDF_STATUS qdf_file_read(const char *path, char **out_buf);
/**
* qdf_file_buf_free() - free a previously allocated file buffer
* @file_buf: pointer to the file buffer to free
*
* This API is used in conjunction with qdf_file_read().
*
* Return: None
*/
void qdf_file_buf_free(char *file_buf);
#endif /* __QDF_FILE_H */

58
qdf/inc/qdf_parse.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2018 The Linux Foundation. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* DOC: Thin filesystem API abstractions
*/
#ifndef __QDF_PARSE_H
#define __QDF_PARSE_H
#include "qdf_status.h"
/**
* cfg_ini_parse() - parse an ini file
* @ini_path: The full file path of the ini file to parse
* @item_cb: Ini item (key/value pair) handler callback function
* Return QDF_STATUS_SUCCESS to continue parsing, else to abort
* @section_cb: Ini section header handler callback function
* Return QDF_STATUS_SUCCESS to continue parsing, else to abort
*
* The *.ini file format is a simple format consiting of a list of key/value
* pairs (items), separated by an '=' character. Comments are initiated with
* a '#' character. Sections are also supported, using '[' and ']' around the
* section name. e.g.
*
* # comments are started with a '#' character
* # items are key/value string pairs, separated by the '=' character
* someKey1=someValue1
* someKey2=someValue2 # this is also a comment
*
* # section headers are enclosed in square brackets
* [some section header] # new section begins
* someKey3=someValue3
*
* Return: QDF_STATUS
*/
QDF_STATUS
qdf_ini_parse(const char *ini_path,
QDF_STATUS (*item_cb)(const char *key, const char *value),
QDF_STATUS (*section_cb)(const char *name));
#endif /* __QDF_PARSE_H */

66
qdf/linux/src/qdf_file.c Normal file
View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 2018 The Linux Foundation. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <linux/firmware.h>
#include "qdf_file.h"
#include "qdf_mem.h"
#include "qdf_module.h"
#include "qdf_status.h"
#include "qdf_trace.h"
#include "qdf_types.h"
QDF_STATUS qdf_file_read(const char *path, char **out_buf)
{
int errno;
const struct firmware *fw;
char *buf;
*out_buf = NULL;
errno = request_firmware(&fw, path, NULL);
if (errno) {
qdf_err("Failed to read file %s", path);
return QDF_STATUS_E_FAILURE;
}
/* qdf_mem_malloc zeros new memory; +1 size ensures null-termination */
buf = qdf_mem_malloc(fw->size + 1);
if (!buf) {
qdf_err("Failed to allocate file buffer of %zuB", fw->size + 1);
release_firmware(fw);
return QDF_STATUS_E_NOMEM;
}
qdf_mem_copy(buf, fw->data, fw->size);
release_firmware(fw);
*out_buf = buf;
return QDF_STATUS_SUCCESS;
}
qdf_export_symbol(qdf_file_read);
void qdf_file_buf_free(char *file_buf)
{
QDF_BUG(file_buf);
if (!file_buf)
return;
qdf_mem_free(file_buf);
}
qdf_export_symbol(qdf_file_buf_free);

132
qdf/src/qdf_parse.c Normal file
View File

@@ -0,0 +1,132 @@
/*
* Copyright (c) 2018 The Linux Foundation. All rights reserved.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "qdf_file.h"
#include "qdf_module.h"
#include "qdf_parse.h"
#include "qdf_status.h"
#include "qdf_str.h"
#include "qdf_trace.h"
#include "qdf_types.h"
QDF_STATUS
qdf_ini_parse(const char *ini_path,
QDF_STATUS (*item_cb)(const char *key, const char *value),
QDF_STATUS (*section_cb)(const char *name))
{
QDF_STATUS status;
char *fbuf;
char *cursor;
status = qdf_file_read(ini_path, &fbuf);
if (QDF_IS_STATUS_ERROR(status)) {
qdf_err("Failed to read *.ini file @ %s", ini_path);
return status;
}
/* foreach line */
cursor = fbuf;
while (*cursor != '\0') {
char *key = cursor;
char *value = NULL;
bool comment = false;
bool eol = false;
/*
* Look for the end of the line, while noting any
* value ('=') or comment ('#') indicators
*/
while (!eol) {
switch (*cursor) {
case '\r':
case '\n':
*cursor = '\0';
cursor++;
/* fall through */
case '\0':
eol = true;
break;
case '=':
/*
* The first '=' is the value indicator.
* Subsequent '=' are valid value characters.
*/
if (!value && !comment) {
value = cursor + 1;
*cursor = '\0';
}
cursor++;
break;
case '#':
/*
* We don't process comments, so we can null-
* terminate unconditionally here (unlike '=').
*/
comment = true;
*cursor = '\0';
/* fall through */
default:
cursor++;
break;
}
}
key = qdf_str_trim(key);
/*
* Ignoring comments, a valid ini line contains one of:
* 1) some 'key=value' config item
* 2) section header
* 3) a line containing whitespace
*/
if (value) {
status = item_cb(key, value);
if (QDF_IS_STATUS_ERROR(status))
goto free_fbuf;
} else if (key[0] == '[') {
int32_t len = qdf_str_len(key);
if (key[len - 1] != ']') {
qdf_err("Invalid *.ini syntax '%s'", key);
} else {
key[len - 1] = '\0';
status = section_cb(key + 1);
if (QDF_IS_STATUS_ERROR(status))
goto free_fbuf;
}
} else if (key[0] != '\0') {
qdf_err("Invalid *.ini syntax '%s'", key);
}
/* skip remaining EoL characters */
while (*cursor == '\n' || *cursor == '\r')
cursor++;
}
status = QDF_STATUS_SUCCESS;
free_fbuf:
qdf_file_buf_free(fbuf);
return status;
}
qdf_export_symbol(qdf_ini_parse);