OpenWrt LibUbi implementation

2019-08-28 03:52发布

问题:

i'm trying to develop an application (written in ANSI C) for an OpenWrt router using libuci. I've read this useful post: How to find out if the eth0 mode is static or dhcp?

and i've develop a piece of my application that is able to read network data (in this case i read if ppp is enabled) using uci library.

char path[]="network.ppp.enabled";
struct  uci_ptr ptr;
struct  uci_context *c = uci_alloc_context();       

if(!c) return;

if (strcmp(typeCmd, "GET") == 0){

    if ((uci_lookup_ptr(c, &ptr, path, true) != UCI_OK) || (ptr.o==NULL || ptr.o->v.string==NULL)) {
            uci_free_context(c);
            return;
        }

    if(ptr.flags & UCI_LOOKUP_COMPLETE)
            strcpy(buffer, ptr.o->v.string);

    uci_free_context(c);

    printf("\n\nUCI result data: %s\n\n", buffer);
}

now i want try to SET new network data (so i want enable ppp -> set ppp to 1) I've write:

}else if (strcmp(typeCmd, "SET") == 0){

    if ((uci_lookup_ptr(c, &ptr, path, true) != UCI_OK) || (ptr.o==NULL || ptr.o->v.string==NULL)) {
            uci_free_context(c);
            return;
        }

    ptr.o->v.string = "1";
    if ((uci_set(c, &ptr) != UCI_OK) || (ptr.o==NULL || ptr.o->v.string==NULL)) {
            uci_free_context(c);
            return;
        }

    if (uci_commit(c, struct uci_package **p, true) != UCI_OK){
            uci_free_context(c);
            return;
        }
}

LibUci documentation is non-existent, there is just some info in the file uci.h, i don't know how fill uci_ptr struct, so i've retrieve it from uci_lookup_ptr , i've change ptr.o->v.string and launch uci_set with new params, but about uci_commit i don't know about struct uci_package **p.

Someone call share with me a bit of documentation or show me some examples?

Thanks a lot

回答1:

The documentation is very thin on UCI. The way I figured it out is by using the uci_ptr's .value property from the uci structure.

Fromt that, I change the line:

ptr.o->v.string = "1";

to:

ptr.value = "1";

I also changed your commit line as follows:

uci_commit(ctx, &ptr.p, false);

That worked for me.