GuoWenxue
2022-04-22 0fd2ff9f9aca1f30d6b4620f90802223cc221fe0
iotd/conf/conf.c
@@ -20,10 +20,15 @@
    _TYPE_OUTPUT,
};
/* conf format:  "{light_indoor:6:0},{light_livroomL:13:0},{light_livroomR:19:0},{light_hallway:26:0}"  */
int parser_gpio_info(int type, gpio_t *gpio, char *conf)
{
    char              *ptr;
    char              *pstart;
    char              *pend;
    char               buf[64];
    int                cnt = 0;
    int                rv = 0;
    if( !gpio || !conf || strlen(conf)<3 )
    {
@@ -31,14 +36,79 @@
        return -1;
    }
    printf("conf: %s\n", conf);
    pstart = strchr(conf, '{');
    pend = strchr(conf, '}');
    pstart = strchr(conf, '{');
    if( !pstart )
        return 0;
    pend = strchr(pstart, '}');
    while( pstart && pend )
    {
        memset(buf, 0, sizeof(buf));
        strncpy(buf, pstart+1, pend-pstart-1);
        /* parser and get the GPIO name, BCM pin number, active power level */
        {
            /* check GPIO configure name too long or not */
            ptr = strchr(buf, ':');
            if( !ptr )
            {
                log_err("Found invalid GPIO configure: %s\n", buf);
                goto NEXT_LOOP;
            }
            if( ptr-buf > sizeof(gpio->input[cnt].name) )
            {
                log_err("Found GPIO name too long\n", buf);
                goto NEXT_LOOP;
            }
            /* use sscanf() to parser GPIO configured values */
            if(_TYPE_INPUT == type )
            {
                rv = sscanf(buf, "%[^:]:%d:%d", gpio->input[cnt].name, &gpio->input[cnt].pin, &gpio->input[cnt].active_level);
                if( 3 == rv)
                {
                    log_nrml("parser GPIO input[%s] BCM[%d] active[%d]\n", gpio->input[cnt].name, gpio->input[cnt].pin, gpio->input[cnt].active_level);
                    if( strstr(gpio->input[cnt].name, "infrared") )
                    {
                        log_nrml("parser GPIO enable infrared detect\n");
                        gpio->infrared_enable = 1;
                    }
                    cnt++;
                    gpio->incnt = cnt;
                }
                else
                {
                    log_err("Found invalid GPIO configure: %s\n", buf);
                }
            }
            else
            {
                rv = sscanf(buf, "%[^:]:%d:%d", gpio->output[cnt].name, &gpio->output[cnt].pin, &gpio->output[cnt].active_level);
                if( 3 == rv)
                {
                    log_nrml("parser GPIO output[%s] BCM[%d] active[%d]\n", gpio->output[cnt].name, gpio->output[cnt].pin, gpio->output[cnt].active_level);
                    cnt++;
                    gpio->outcnt = cnt;
                }
                else
                {
                    log_err("Found invalid GPIO configure: %s\n", buf);
                }
            }
        }
NEXT_LOOP:
        pstart = strchr(pend, '{');
        if( pstart )
            pend = strchr(pstart, '}');
    }
    return gpio->outcnt;
}
int parser_conf(const char *conf_file, iotd_ctx_t *ctx, int debug)
@@ -158,7 +228,7 @@
        log_nrml("parser SHT2x sensor enabled\n");
    }
    hal_ctx->ds18b20_enable = iniparser_getint(ini, "hardware:sht2x", 0);
    hal_ctx->ds18b20_enable = iniparser_getint(ini, "hardware:ds18b20", 0);
    if( hal_ctx->ds18b20_enable )
    {
        log_nrml("parser DS18B20 sensor enabled\n");
@@ -201,13 +271,16 @@
    if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) )
    {
        log_err("ERROR: Parser MQTT subscribe topic failure\n");
        return -2;
        log_warn("WARNNING: Parser MQTT subscribe topic failure\n");
    }
    strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) );
    else
    {
        strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) );
        mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0);
        mqtt_ctx->sub_enable = 1;
    mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0);
    log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos);
        log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos);
    }
    /*+------------------------------------------------------+
     *|             parser publisher settings                |
@@ -215,13 +288,17 @@
    if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) )
    {
        log_err("ERROR: Parser MQTT publisher topic failure\n");
        return -2;
        log_warn("WARNNING: Parser MQTT publisher topic failure\n");
    }
    strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) );
    else
    {
        strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) );
        mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0);
        mqtt_ctx->interval = iniparser_getint(ini, "publisher:interval", 60);
        mqtt_ctx->pub_enable = 1;
    mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0);
    log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos);
        log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos);
    }
    return 0;