guowenxue
2021-04-21 e0e3a637e92dbe3c7700cc541410ff19b3b0c80c
update iotd hal functions
6 files modified
125 ■■■■ changed files
iotd/conf/conf.c 20 ●●●●● patch | view | raw | blame | history
iotd/hal/gpio.c 90 ●●●●● patch | view | raw | blame | history
iotd/hal/gpio.h 2 ●●● patch | view | raw | blame | history
iotd/hal/hal.c 9 ●●●● patch | view | raw | blame | history
iotd/hal/sht20.c 2 ●●●●● patch | view | raw | blame | history
iotd/hal/tsl2561.c 2 ●●● patch | view | raw | blame | history
iotd/conf/conf.c
@@ -266,14 +266,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");
    }
    else
    {
    strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) );
    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);
    mqtt_ctx->sub_enable = 1;
        log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos);
    }
    /*+------------------------------------------------------+
     *|             parser publisher settings                |
@@ -281,15 +283,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");
    }
    else
    {
    strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) );
    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);
    mqtt_ctx->pub_enable = 1;
        log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos);
    }
    return 0;
}
iotd/hal/gpio.c
@@ -21,18 +21,100 @@
int gpio_init(gpio_t *gpio)
{
    int                 i;
    gpio_info_t        *gpioinfo;
    s_gpio = gpio;
    if( !gpio )
    {
        log_err("Invalid input arguments $gpio\n");
        return -1;
    }
    s_gpio = gpio;
    if( !gpio->incnt && !gpio->outcnt )
    {
        log_warn("WARNNING: No GPIO pins configured\n");
        return 0;
}
    /*  gpiod open chip */
    s_chip = gpiod_chip_open_by_name(RPI_GPIONAME);
    if( !s_chip )
    {
        log_err("gpiod open chip failure, maybe you need running as root\n");
        return -2;
    }
    log_nrml("gpiod initialise open chip ok\n");
    /*  gpiod request all output pins */
    for(i=0; i<gpio->outcnt; i++)
    {
        gpio->output[i].line = gpiod_chip_get_line(s_chip, gpio->output[i].pin);
        if( !gpio->output[i].line )
        {
            log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->output[i].name, gpio->output[i].pin );
            return -2;
        }
        gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level);
        log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin);
    }
    /*  gpiod request all input pins */
    for(i=0; i<gpio->incnt; i++)
    {
        gpio->input[i].line = gpiod_chip_get_line(s_chip, gpio->input[i].pin);
        if( !gpio->input[i].line )
        {
            log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->input[i].name, gpio->input[i].pin );
            return -2;
        }
        if( gpio->output[i].active_level )
        {
            gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name);
            log_nrml("gpiod request '%s' pin[#%d] for rising edge event ok\n", gpio->input[i].name, gpio->input[i].pin);
        }
        else
        {
            gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name);
            log_nrml("gpiod request '%s' pin[#%d] for falling edge event ok\n", gpio->input[i].name, gpio->input[i].pin);
        }
    }
}
void gpio_term(void)
{
    int              i;
    log_nrml("start teriminated GPIO\n");
    if( !s_gpio->incnt && !s_gpio->outcnt )
    {
    return ;
    }
    for(i=0; i<s_gpio->outcnt; i++)
    {
        gpiod_line_release(s_gpio->output[i].line);
    }
    for(i=0; i<s_gpio->incnt; i++)
    {
        gpiod_line_release(s_gpio->input[i].line);
    }
    gpiod_chip_close(s_chip);
}
void gpio_out(char *name, int cmd)
@@ -51,17 +133,17 @@
    if( !found )
    {
        log_err("GPIO output for [%s] pin not found\n", name);
        log_err("GPIO output for '%s' pin not found\n", name);
        return ;
    }
    if( OFF == cmd )
    {
        gpiod_line_set_value(s_gpio->output[i].lines, s_gpio->output[i].active_level);
        gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level);
    }
    else
    {
        gpiod_line_set_value(s_gpio->output[i].lines, !s_gpio->output[i].active_level);
        gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level);
    }
    return ;
iotd/hal/gpio.h
@@ -27,7 +27,7 @@
    char                 name[32];      /*  GPIO connected module name */
    int                  pin;          /*  GPIO BCM pin number */
    int                  active_level;  /*  active power level */
    struct gpiod_line   *lines;         /*  gpiod lines */
    struct gpiod_line   *line;          /*  gpiod line */
} gpio_info_t;
iotd/hal/hal.c
@@ -17,7 +17,6 @@
int hal_init(hal_ctx_t *ctx)
{
    int                   i;
    gpio_info_t          *gpio = NULL;
    if(!ctx)
    {
@@ -32,6 +31,10 @@
            log_err("R&H sensor SHT2X initialise failure\n");
            return -2;
        }
        else
        {
            log_nrml("R&H sensor SHT2X initialise okay\n");
        }
    }
    if( ctx->lux_enable )
@@ -41,6 +44,10 @@
            log_err("LUX sensor TSL2561 initialise failure\n");
            return -2;
        }
        else
        {
            log_nrml("LUX sensor TSL2561 initialise okay\n");
        }
    }
    gpio_init(&ctx->gpio);
iotd/hal/sht20.c
@@ -107,6 +107,7 @@
        return -2;
    }
    log_dbg("SHT2X initialise ok, s_sht2x_fd=%d\n", s_sht2x_fd);
    return s_sht2x_fd;
}
@@ -259,6 +260,7 @@
        return -2;
    }
    log_dbg("SHT2X initialise ok, s_sht2x_fd=%d\n", s_sht2x_fd);
    return 0;
}
iotd/hal/tsl2561.c
@@ -46,7 +46,7 @@
    return -1;
    }
    log_nrml("TSL2561 initialise ok, s_tsl_fd=%d\n", s_tsl_fd);
    log_dbg("TSL2561 initialise ok, s_tsl_fd=%d\n", s_tsl_fd);
    return s_tsl_fd;
}