APUE Learning Example Source Code
guowenxue
2019-06-26 937cb95ee88ce4f57cb4510be459a72033fbbb94
update mqttd program, add ID support
4 files modified
88 ■■■■■ changed files
pj1_mqttd/etc/conf.c 17 ●●●●● patch | view | raw | blame | history
pj1_mqttd/etc/conf.h 2 ●●●●● patch | view | raw | blame | history
pj1_mqttd/etc/mqttd.conf 2 ●●●●● patch | view | raw | blame | history
pj1_mqttd/main.c 67 ●●●● patch | view | raw | blame | history
pj1_mqttd/etc/conf.c
@@ -25,9 +25,11 @@
    if( !conf_file )
    { 
        strncpy(ctx->id, "\"x86host02\"", sizeof(ctx->id));
        /* logger settings */
        strncpy(ctx->logfile, "/tmp/mqttd.log", sizeof(ctx->logfile));
        //strncpy(ctx->logfile, "/tmp/mqttd.log", sizeof(ctx->logfile));
        strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
        ctx->loglevel = LOG_LEVEL_DEBUG;
        ctx->logsize = 1024;
@@ -92,6 +94,19 @@
    log_nrml("Logger system initialise ok\n");
    /*+------------------------------------------------------+
     *|              parser production ID                    |
     *+------------------------------------------------------+*/
    if( !(str=iniparser_getstring(ini, "common:id", NULL)) )
    {
        log_err("ERROR: Parser production ID failure\n");
        return -2;
    }
    snprintf(ctx->id, sizeof(ctx->id), "\"%s\"", str);
    //strncpy(ctx->id, str, sizeof(ctx->id) );
    log_nrml("Parser production ID [%s]\n", ctx->id);
    /*+------------------------------------------------------+
pj1_mqttd/etc/conf.h
@@ -34,6 +34,8 @@
typedef struct mqtt_ctx_s
{
    char          id[32];       /* production ID */
    /* logger settings */
    char          logfile[128]; /* logger record file */
    int           loglevel;     /* logger level  */
pj1_mqttd/etc/mqttd.conf
@@ -1,3 +1,5 @@
[common]
id="x86host02"
[logger]
pj1_mqttd/main.c
@@ -257,9 +257,8 @@
void proc_json_items(cJSON *root)
{
    int                    i;
    cJSON                 *item;
    char                  *value;
    cJSON                 *item;
    cJSON                 *array;
    if( !root )
    {
@@ -267,44 +266,26 @@
        return ;
    }
    for( i=0; i<cJSON_GetArraySize(root); i++ )
    item = cJSON_GetObjectItem(root, "camera");
    if( !item )
    {
        item = cJSON_GetArrayItem(root, i);
        if( !item )
            break;
        /* if item is cJSON_Object, then recursive call proc_json */
        if( cJSON_Object == item->type )
        {
            proc_json_items(item);
        }
        else if( cJSON_Array == item->type )
        {
            /* Logic C920 camera control  */
            if( !strcasecmp(item->string, "camera") )
            {
                array = cJSON_GetArrayItem(item, 0);
                if( NULL != array )
                {
                    cJSON        *led_item;
                    if( NULL != (led_item=cJSON_GetObjectItem(array , "C920")) )
                    {
                        log_nrml("turn Logic C920 camera '%s'\n", led_item->valuestring);
                        turn_camera( led_item->valuestring );
                    }
                }
            }
        }
        log_err("cJSON_Parse get camera failure: %s\n", cJSON_GetErrorPtr());
        return ;
    }
    value = cJSON_PrintUnformatted(item);
    turn_camera(value);
    free(value);
}
void sub_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
    cJSON                 *root = NULL;
    char                  *out;
    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
    cJSON                  *root = NULL;
    cJSON                  *item;
    char                   *value;
    if ( !message->payloadlen )
    {
@@ -312,7 +293,7 @@
        return ;
    }
    log_nrml("Subscriber receive message: '%s'\n", message->payload);
    log_dbg("Subscriber receive message: '%s'\n", message->payload);
    root = cJSON_Parse(message->payload);
    if( !root )
@@ -321,8 +302,26 @@
        return ;
    }
    item = cJSON_GetObjectItem(root, "id");
    if( !item )
    {
        log_err("cJSON_Parse get ID failure: %s\n", cJSON_GetErrorPtr());
        goto OUT;
    }
    value = cJSON_PrintUnformatted(item);
    if( strcasecmp(value, ctx->id) )
    {
        free(value);
        goto OUT;
    }
    free(value);
    log_nrml("Subscriber[%s] receive message: '%s'\n", ctx->id, message->payload);
    proc_json_items(root); 
OUT:
    cJSON_Delete(root); /* must delete it, or it will result memory leak */
    return ;
}