/********************************************************************************* * Copyright: (C) 2018 LingYun IoT System Studio * All rights reserved. * * Filename: iotd_conf.c * Description: This file is for iotd configure file parser API * * Version: 1.0.0(2018年11月20日) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "2018年11月20日 12时11分46秒" * ********************************************************************************/ #include "iotd_conf.h" #include "cp_iniparser.h" int parser_iotd_conf(char *conf_file, iotd_conf_t *iotd_conf) { dictionary *ini; char *str; int data; logger_t *logger; nbiot_conf_t *nbiot; lora_conf_t *lora; mqtt_conf_t *mqtt; if(!conf_file || !iotd_conf ) { log_err("Invalid input arguments\n"); return -1; } ini = iniparser_load(conf_file); if (ini==NULL) { log_err("cannot parse file: %s\n", conf_file); return -2 ; } memset(iotd_conf, 0, sizeof(*iotd_conf)); logger = &iotd_conf->logger; nbiot = &iotd_conf->nbiot_conf; lora = &iotd_conf->lora_conf; mqtt = &iotd_conf->mqtt_conf; /*+----------------------------------+ | Program logger configuration | +----------------------------------+*/ str = iniparser_getstring(ini, "log:file", NULL); strncpy(logger->file, (strlen(str)>0 ? str : DEF_LOG_FILE), FILENAME_LEN); log_nrml("Set log file [%s] from configuration.\n", logger->file); /* If not set the log level from command line, then use the configure one*/ if( !(logger->flag&CP_LOGGER_LEVEL_OPT) ) { logger->level = iniparser_getint(ini, "log:level", LOG_LEVEL_NRML); log_nrml("Set log level[%d] from configuration.\n", logger->level); } /* Set the log maximum file size in the configure file */ logger->size = iniparser_getint(ini, "log:size", DEF_LOG_MAXSIZE); logger->size = logger->size>DEF_LOG_MAXSIZE ? DEF_LOG_MAXSIZE : logger->size; log_nrml("Set log size [%dKiB] from configuration\n", logger->size); /*+----------------------------------+ | NB-IoT Module configuration | +----------------------------------+*/ data = iniparser_getint(ini, "nbiot:enable", 0); nbiot->enable = data? ENABLE : DISABLE; printf("Confiugre NB-IoT thread [%s]\n", nbiot->enable ? "enable" : "disable"); str = iniparser_getstring(ini, "nbiot:comport", NULL); if( NULL!=str && strlen(str) > 0 ) { strncpy(nbiot->comport, str, sizeof(nbiot->comport)); } else { log_err("Configure without NB-IoT comport device, thread will not start\n"); nbiot->enable = DISABLE; } log_nrml("Configure NB-IoT comport device as '%s'\n", lora->comport); nbiot->baudrate = iniparser_getint(ini, "nbiot:baudrate", 9600); log_nrml("Configure NB-IoT comport baudrate as '%d'\n", nbiot->baudrate); str = iniparser_getstring(ini, "nbiot:settings", "8N1N"); if( NULL!=str && strlen(str) >= 4 ) { strncpy(nbiot->settings, str, 4); log_nrml("Configure NB-IoT comport settings as '%s'\n", nbiot->settings); } else { log_err("Configure Invalid NB-IoT comport settings '%s', thread will not start.\n", str); nbiot->enable = DISABLE; } /*+----------------------------------+ | LoRa Module configuration | +----------------------------------+*/ data = iniparser_getint(ini, "lora:enable", 0); lora->enable = data? ENABLE : DISABLE; printf("Confiugre LoRa thread [%s]\n", lora->enable ? "enable" : "disable"); str = iniparser_getstring(ini, "lora:comport", NULL); if( NULL!=str && strlen(str) > 0 ) { strncpy(lora->comport, str, sizeof(lora->comport)); } else { log_err("Configure without LoRa comport device, thread will not start\n"); lora->enable = DISABLE; } log_nrml("Configure LoRa comport device as '%s'\n", lora->comport); lora->baudrate = iniparser_getint(ini, "lora:baudrate", 115200); log_nrml("Configure LoRa comport baudrate as '%d'\n", lora->baudrate); str = iniparser_getstring(ini, "lora:settings", "8N1N"); if( NULL!=str && strlen(str) >= 4 ) { strncpy(lora->settings, str, 4); log_nrml("Configure LoRa comport settings as '%s'\n", lora->settings); } else { log_err("Configure Invalid LoRa comport settings '%s', thread will not start.\n", str); lora->enable = DISABLE; } /*+----------------------------------+ | MQTT Server configuration | +----------------------------------+*/ data = iniparser_getint(ini, "mqtt:enable", 0); mqtt->enable = data? ENABLE : DISABLE; printf("Confiugre MQTT thread [%s]\n", lora->enable ? "enable" : "disable"); str = iniparser_getstring(ini, "mqtt:hostname", NULL); if( !str || strlen(str) <= 0 ) { log_err("Configure MQTT Server hostname invalid, thread will not start\n"); mqtt->enable = DISABLE; } strncpy(mqtt->hostname, str, HOSTN_LEN); log_nrml("Configure MQTT Server hostname: \"%s\" \n", mqtt->hostname); data = iniparser_getint(ini, "mqtt:port", 0); if( !data ) { log_err("Configure MQTT Server port invalid, thread will not start\n"); mqtt->enable = DISABLE; } mqtt->port = data; log_nrml("Configure MQTT Server port: [%d]\n", mqtt->port); iniparser_freedict(ini); return 0; }