10b966c5eSopenharmony_ci/******************************************************************************
20b966c5eSopenharmony_ci *
30b966c5eSopenharmony_ci *  Copyright (C) 2009-2012 Broadcom Corporation
40b966c5eSopenharmony_ci *
50b966c5eSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License");
60b966c5eSopenharmony_ci *  you may not use this file except in compliance with the License.
70b966c5eSopenharmony_ci *  You may obtain a copy of the License at:
80b966c5eSopenharmony_ci *
90b966c5eSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
100b966c5eSopenharmony_ci *
110b966c5eSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
120b966c5eSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS,
130b966c5eSopenharmony_ci *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140b966c5eSopenharmony_ci *  See the License for the specific language governing permissions and
150b966c5eSopenharmony_ci *  limitations under the License.
160b966c5eSopenharmony_ci *
170b966c5eSopenharmony_ci ******************************************************************************/
180b966c5eSopenharmony_ci
190b966c5eSopenharmony_ci/******************************************************************************
200b966c5eSopenharmony_ci *
210b966c5eSopenharmony_ci *  Filename:      conf.c
220b966c5eSopenharmony_ci *
230b966c5eSopenharmony_ci *  Description:   Contains functions to conduct run-time module configuration
240b966c5eSopenharmony_ci *                 based on entries present in the .conf file
250b966c5eSopenharmony_ci *
260b966c5eSopenharmony_ci ******************************************************************************/
270b966c5eSopenharmony_ci
280b966c5eSopenharmony_ci#define LOG_TAG "bt_vnd_conf"
290b966c5eSopenharmony_ci
300b966c5eSopenharmony_ci#include <string.h>
310b966c5eSopenharmony_ci#include <stdio.h>
320b966c5eSopenharmony_ci#include <utils/Log.h>
330b966c5eSopenharmony_ci#include "bt_vendor_brcm.h"
340b966c5eSopenharmony_ci
350b966c5eSopenharmony_ci/******************************************************************************
360b966c5eSopenharmony_ci**  Externs
370b966c5eSopenharmony_ci******************************************************************************/
380b966c5eSopenharmony_ciint userial_set_port(char *p_conf_name, char *p_conf_value, int param);
390b966c5eSopenharmony_ciint hw_set_patch_file_path(char *p_conf_name, char *p_conf_value, int param);
400b966c5eSopenharmony_ciint hw_set_patch_file_name(char *p_conf_name, char *p_conf_value, int param);
410b966c5eSopenharmony_ci#if (VENDOR_LIB_RUNTIME_TUNING_ENABLED == TRUE)
420b966c5eSopenharmony_ciint hw_set_patch_settlement_delay(char *p_conf_name, char *p_conf_value, int param);
430b966c5eSopenharmony_ci#endif
440b966c5eSopenharmony_ci
450b966c5eSopenharmony_ci/******************************************************************************
460b966c5eSopenharmony_ci**  Local type definitions
470b966c5eSopenharmony_ci******************************************************************************/
480b966c5eSopenharmony_ci
490b966c5eSopenharmony_ci#define CONF_COMMENT '#'
500b966c5eSopenharmony_ci#define CONF_DELIMITERS " =\n\r\t"
510b966c5eSopenharmony_ci#define CONF_VALUES_DELIMITERS "=\n\r\t"
520b966c5eSopenharmony_ci#define CONF_MAX_LINE_LEN 255
530b966c5eSopenharmony_ci
540b966c5eSopenharmony_citypedef int(conf_action_t)(char *p_conf_name, char *p_conf_value, int param);
550b966c5eSopenharmony_ci
560b966c5eSopenharmony_citypedef struct {
570b966c5eSopenharmony_ci    const char *conf_entry;
580b966c5eSopenharmony_ci    conf_action_t *p_action;
590b966c5eSopenharmony_ci    int param;
600b966c5eSopenharmony_ci} conf_entry_t;
610b966c5eSopenharmony_ci
620b966c5eSopenharmony_ci/******************************************************************************
630b966c5eSopenharmony_ci**  Static variables
640b966c5eSopenharmony_ci******************************************************************************/
650b966c5eSopenharmony_ci
660b966c5eSopenharmony_ci/*
670b966c5eSopenharmony_ci * Current supported entries and corresponding action functions
680b966c5eSopenharmony_ci */
690b966c5eSopenharmony_cistatic const conf_entry_t conf_table[] = {
700b966c5eSopenharmony_ci    {"UartPort", userial_set_port, 0},
710b966c5eSopenharmony_ci    {"FwPatchFilePath", hw_set_patch_file_path, 0},
720b966c5eSopenharmony_ci    {"FwPatchFileName", hw_set_patch_file_name, 0},
730b966c5eSopenharmony_ci#if (VENDOR_LIB_RUNTIME_TUNING_ENABLED == TRUE)
740b966c5eSopenharmony_ci    {"FwPatchSettlementDelay", hw_set_patch_settlement_delay, 0},
750b966c5eSopenharmony_ci#endif
760b966c5eSopenharmony_ci    {(const char *)NULL, NULL, 0}
770b966c5eSopenharmony_ci};
780b966c5eSopenharmony_ci
790b966c5eSopenharmony_ci/*****************************************************************************
800b966c5eSopenharmony_ci**   CONF INTERFACE FUNCTIONS
810b966c5eSopenharmony_ci*****************************************************************************/
820b966c5eSopenharmony_ci
830b966c5eSopenharmony_ci/*******************************************************************************
840b966c5eSopenharmony_ci**
850b966c5eSopenharmony_ci** Function        vnd_load_conf
860b966c5eSopenharmony_ci**
870b966c5eSopenharmony_ci** Description     Read conf entry from p_path file one by one and call
880b966c5eSopenharmony_ci**                 the corresponding config function
890b966c5eSopenharmony_ci**
900b966c5eSopenharmony_ci** Returns         None
910b966c5eSopenharmony_ci**
920b966c5eSopenharmony_ci*******************************************************************************/
930b966c5eSopenharmony_civoid vnd_load_conf(const char *p_path)
940b966c5eSopenharmony_ci{
950b966c5eSopenharmony_ci    FILE *p_file;
960b966c5eSopenharmony_ci    char *p_name;
970b966c5eSopenharmony_ci    char *p_value;
980b966c5eSopenharmony_ci    conf_entry_t *p_entry;
990b966c5eSopenharmony_ci    char line[CONF_MAX_LINE_LEN + 1]; /* add 1 for \0 char */
1000b966c5eSopenharmony_ci
1010b966c5eSopenharmony_ci    HILOGI("Attempt to load conf from %s", p_path);
1020b966c5eSopenharmony_ci    if ((p_file = fopen(p_path, "r")) == NULL) {
1030b966c5eSopenharmony_ci        HILOGI("vnd_load_conf file >%s< not found", p_path);
1040b966c5eSopenharmony_ci        return;
1050b966c5eSopenharmony_ci    }
1060b966c5eSopenharmony_ci
1070b966c5eSopenharmony_ci    /* read line by line */
1080b966c5eSopenharmony_ci    while (fgets(line, CONF_MAX_LINE_LEN + 1, p_file) != NULL) {
1090b966c5eSopenharmony_ci        if (line[0] == CONF_COMMENT) {
1100b966c5eSopenharmony_ci            continue;
1110b966c5eSopenharmony_ci        }
1120b966c5eSopenharmony_ci
1130b966c5eSopenharmony_ci        p_name = strtok(line, CONF_DELIMITERS);
1140b966c5eSopenharmony_ci        if (p_name == NULL) {
1150b966c5eSopenharmony_ci            continue;
1160b966c5eSopenharmony_ci        }
1170b966c5eSopenharmony_ci
1180b966c5eSopenharmony_ci        p_value = strtok(NULL, CONF_DELIMITERS);
1190b966c5eSopenharmony_ci        if (p_value == NULL) {
1200b966c5eSopenharmony_ci            HILOGW("vnd_load_conf: missing value for name: %s", p_name);
1210b966c5eSopenharmony_ci            continue;
1220b966c5eSopenharmony_ci        }
1230b966c5eSopenharmony_ci
1240b966c5eSopenharmony_ci        p_entry = (conf_entry_t *)conf_table;
1250b966c5eSopenharmony_ci        while (p_entry->conf_entry != NULL) {
1260b966c5eSopenharmony_ci            if (strcmp(p_entry->conf_entry, (const char *)p_name) == 0) {
1270b966c5eSopenharmony_ci                p_entry->p_action(p_name, p_value, p_entry->param);
1280b966c5eSopenharmony_ci                break;
1290b966c5eSopenharmony_ci            }
1300b966c5eSopenharmony_ci
1310b966c5eSopenharmony_ci            p_entry++;
1320b966c5eSopenharmony_ci        }
1330b966c5eSopenharmony_ci    }
1340b966c5eSopenharmony_ci
1350b966c5eSopenharmony_ci    (void)fclose(p_file);
1360b966c5eSopenharmony_ci}
137