OpenWrt Forum Archive

Topic: how to use the uci C api

The content of this topic has been archived on 29 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello All,

Where can I find some examples  of using  uci  in C or C++. 
For instance if I want to collect the different lan interface names, how can I do this in C ?

I suggest you to check into the Lua UCI bindings, as they provide full UCI functions to Lua, and that the "Lua side" of it is understandable in C without prior Lua knowledge (package uci, file "lua/uci.c").

Hope it helps!

Compiling UCI as stand alone with an example using the C API
1. Compiling UCI as stand alone

cd ~
git clone git://nbd.name/uci.git ~/uci
cd ~/uci
cmake -DBUILD_LUA=off .
make install DESTDIR=$HOME

2. Example code using the UCI C API
~/uci/main.c

#include <stdio.h>
#include <string.h>
#include <uci.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
  struct uci_context *c;
  struct uci_ptr p;
  char *a = strdup ("wireless.@wifi-iface[0].ssid");

  c = uci_alloc_context ();
  if (uci_lookup_ptr (c, &p, a, true) != UCI_OK)
    {
      uci_perror (c, "XXX");
      return 1;
    }

  printf("%s\n", p.o->v.string);
  uci_free_context (c);
  free (a);
  return 0;
}

3. Compile the example

cc -I$HOME/usr/include -L$HOME/usr/lib main.c -luci -o uci-test

4. Run the compiled example binary

export LD_LIBRARY_PATH=$HOME/usr/lib
./uci-test
XXX: Entry not found

(Last edited by written_direcon on 15 Nov 2012, 18:14)

The discussion might have continued from here.