Exemplo n.º 1
0
static void write_json(FILE *file, ParsingObject *pego, char *source, int level) {
    if (pego[0]) {
        if (pego[0]->child_size > 0) {
            if (isJsonArray(pego[0])) {
                write_json_array(file, pego, source, level);
            } else {
                write_json_object(file, pego, source, level);
            }
        } else {
            fprintf(file, "\"");
            if (pego[0]->value == NULL) {
                for (long j = pego[0]->start_pos; j < pego[0]->end_pos; j++) {
                    fprintf(file, "%c", source[j]);
                }
            } else {
                fprintf(file, "%s", pego[0]->value);
            }
            fprintf(file, "\"");
        }
    } else {
        fprintf(stderr, "%p tag:null\n", pego);
    }
}
Exemplo n.º 2
0
int TestMonitor::sendCmd(QString &devID,uint32_t action,QByteArray *data)
{
    if (!devID.contains("test123"))
    {
        DEBUG_INFO << "invalid dev id found";
        return PL_RET_FAIL;
    }

    if (data != NULL)
    {
        if ( !isJsonArray(*data) && !isJsonObject(*data))
        {
            DEBUG_INFO << "invalid command data,must be json format";
            return PL_RET_FAIL;
        }
    }

    if (action == PL_REQ_GET_PIM_DATA)
    {
        return pimTest(devID, action, data);
    }

    return generalTest(devID, action, data);
}
Exemplo n.º 3
0
/** Parses plugin interfaces. Loads the function names present in the corresponding plugin defined in the service specification JSON
* @param[in] inf_file file path for the plugin interface
* @return returns true upon successful parsing and false otherwise
*/
bool parsePluginInterfaces(char *inf_file) {
    char *out;
    int numentries = 0, i = 0;
    cJSON *json = NULL, *jitem, *child;
    bool status = true;
    FILE *fp = fopen(inf_file, "rb");

    if (fp == NULL) {
        fprintf(stderr,"Error can't open file %s\n", inf_file);
        status = false;
    } else {
        fseek(fp, 0, SEEK_END);
        long size = ftell(fp);
        rewind(fp);

        // read the file
        char *buffer = (char *)malloc(size+1);
        if (buffer != NULL) {
            fread(buffer, 1, size, fp);
            // parse the file
            json = cJSON_Parse(buffer);
        }
        if (json == NULL || !json) {
            fprintf(stderr,"Error before: [%s]\n",cJSON_GetErrorPtr());
            status = false;
        } else {
            #if DEBUG
                out = cJSON_Print(json, 2);
                printf("%s\n", out);
                free(out);
            #endif

            if (!isJsonObject(json)) {
                handleParseInterfacesError();
            }

            jitem = cJSON_GetObjectItem(json, "functions");
            if (!isJsonArray(jitem)) {
                handleParseInterfacesError();
            }

            child = jitem->child;
            /* How many entries in the array? */
            while (child && numentries++ < MAX_PROPERTIES) {
                child = child->next;
            }
            if (!numentries) {
                handleParseInterfacesError();
            }

            g_funcEntries = numentries;
            g_funcSignatures = (char **)malloc(numentries*sizeof(char*));
            if (!g_funcSignatures) {
                handleParseInterfacesError();
            }

            memset(g_funcSignatures,0,numentries*sizeof(char*));
            child = jitem->child;
            while (child) {
                if (!isJsonString(child)) {
                    handleParseInterfacesError();
                }

                g_funcSignatures[i] = strdup(child->valuestring);
                child = child->next;
                #if DEBUG
                    printf("g_funcSignature = %s\n", g_funcSignatures[i]);
                #endif
                i++;
            }

endParseInterfaces:
            cJSON_Delete(json);
        }

        free(buffer);
        fclose(fp); // close the file pointer
    }

    return status;
}