Ejemplo n.º 1
0
static int fhs_event_add_resource(struct fe_handler *feh, char *res, FAMEvent *fe)
{
        FAMRequest fr;
        SaHpiRptEntryT *rpt;
        DIR *pdir;
        struct dirent *pd;
        char  *path, *root_path;
        int len;
        

#ifndef UNIT_TEST
        root_path = g_hash_table_lookup(feh->ohh->config, "root_path");
#else
        root_path = "/home/guorj/HPI/openhpi/src/plugins/simulator/test/resources";
#endif
        len = strlen(root_path) + strlen(fe->filename) + 30;
        path = g_malloc(len);

        sprintf(path, "%s/%s", root_path, fe->filename);
        pdir = opendir(path);
        if (!pdir) {
            printf("%s is not directory\n", path);
            g_free(path);
            return -1;
        }
  
        rpt = g_malloc0(sizeof(*rpt));
#if 0
        parse rpt entry(contain entity path, then calulate resource id)
        
        insert resource event into event queue;
#else
        printf("add resource:%s\n", fe->filename);
#endif
        feh->ids = sim_util_add_res_id(feh->ids, fe->filename, rpt->EntryId, 
                                       &rpt->ResourceEntity);
        for (pd = readdir(pdir); pd; pd = readdir(pdir)) {
                DIR *tmp;
                unsigned int index;

                if (str2uint32(pd->d_name, &index)) continue;
#if 0
                insert rdr event into event queue; 
#else
                printf("add rdr:%s\n", pd->d_name);
#endif
                sprintf(path, "%s/%s/%s/sensor", root_path, res, pd->d_name); 
                tmp = opendir(path);
                if (tmp) {
                        FAMMonitorDirectory(fe->fc, path, &fr, (void *)req_rdr);
                        sim_util_add_rdr_id(feh->ids, fe->filename, fr.reqnum, index);
                        printf("monitor sensor:%s\n", pd->d_name);
                        close(tmp);
                }
        }
        closedir(pdir);
        g_free(path);
        return 0;
}
Ejemplo n.º 2
0
int do_show(jokedb_struct *jokedb) {

    // define buffer to store user provided 
    int joke_id_buf_sz = 11;
    char joke_id_buf[joke_id_buf_sz];
    uint32_t joke_id;
    char joke_str[MAX_JOKE_STRING_LEN] = { 0 };
    int id_type;
    
    // send SHOWWHICHID msg
    send(SHOWWHICHID, cgc_strlen(SHOWWHICHID));

provide_joke_id:
    // send LISTPROMPT and handle user input
    cgc_memset(joke_id_buf, '\0', joke_id_buf_sz);
    prompt_user(SHOWPROMPT, joke_id_buf, joke_id_buf_sz);

    // is joke_id_buf a number?

    id_type = is_numeric(joke_id_buf);
    // numeric and >= 0
    if (id_type == 0) {
        joke_id = str2uint32(joke_id_buf);
        if (joke_id == 1337) {
            send(EASTEREGG, cgc_strlen(EASTEREGG));           
        } else if (joke_id < joke_count(jokedb)) {
            send_joke(&jokedb->jokes[joke_id]);
        } else {
            send(BADIDERROR, cgc_strlen(BADIDERROR));
            goto provide_joke_id;
        }
    // not numeric
    } else if (id_type == -1 && streq(joke_id_buf, "RANDOM") == 0) {
        send_random_joke(jokedb);
    // either numeric < 0, or not numeric that is not "RANDOM"
    } else {
        send(BADIDERROR, cgc_strlen(BADIDERROR));
        goto provide_joke_id;
    }
    
    return 0;
}
Ejemplo n.º 3
0
static int fhs_event_add_resource(struct fe_handler *feh, char *res, FAMEvent *fe)
{
        FAMRequest fr;
        DIR *pdir;
        struct dirent *pd;
        char  *path, *root_path;
        int len;
        struct oh_event *event;
        SaHpiResourceIdT  rid;
        SaHpiRptEntryT *rpt;
        
 
        root_path = g_hash_table_lookup(feh->ohh->config, "root_path");
        len = strlen(root_path) + strlen(fe->filename) + 30;
        path = g_malloc(len);

        sprintf(path, "%s/%s", root_path, fe->filename);
        pdir = opendir(path);
        if (!pdir) {
            printf("%s is not directory\n", path);
            g_free(path);
            return -1;
        }

        event = g_malloc0(sizeof(*event));
        event->type = OH_ET_RESOURCE;
        rpt = &event->u.res_event.entry;
        sprintf(path, "%s/%s/rpt", root_path, fe->filename);
        sim_parser_get_rpt(path, rpt);
        rid = oh_uid_from_entity_path(&rpt->ResourceEntity);
        rpt->ResourceId = rid;

        sim_util_add_resource(feh->ohh->rptcache, rpt, g_strdup(fe->filename));
        sim_util_insert_event(&feh->ohh->eventq, event);

        for (pd = readdir(pdir); pd; pd = readdir(pdir)) {
                DIR *tmp;
                unsigned int index;
                sim_rdr_id_t  *sid = NULL;
                SaHpiRdrT *rdr;

                if (str2uint32(pd->d_name, &index)) continue;

                event = g_malloc0(sizeof(*event));
                event->type = OH_ET_RDR;
                rdr = &event->u.rdr_event.rdr;
                sprintf(path, "%s/%s/%s/rdr", root_path, fe->filename,pd->d_name);
                sim_parser_get_rdr(path, rdr);
                sim_util_insert_event(&feh->ohh->eventq, event);
                sprintf(path, "%s/%s/%s/sensor", root_path, res, pd->d_name); 
                tmp = opendir(path);
                if (tmp) {
                        closedir(tmp);
                        FAMMonitorDirectory(fe->fc, path, &fr, REQ_RDR);
                        sid = g_malloc0(sizeof(*sid));
                        sid->rid = rid;
                        sid->index = index;
                        sid->reqnum = fe->fr.reqnum;
                        printf("monitor sensor:%s\n", pd->d_name);
                }
                sim_util_add_rdr(feh->ohh->rptcache, rid, rdr, sid);
        }
        closedir(pdir);
        g_free(path);
        return 0;
}