Uci Command To Change Gateway Address

Question:

We can use terminal port to change the configuration of Ethernet port parameters like server/client, IP address, port number but we can’t use C language to change these parameters by your SDK.

Can you provide a short sample code to teach us how to write a C language by using the API of uci to change the Ethernet configuration.

For example: the default IP address is “192.168.1.1” and I want to change the address to “192.168.2.12” in C language.

 

Answer:

UCI_C_Example

Comments

This post currently has one response

  • For better expreience and convenint editing, detail codes are listed below.

    Compile:
        cc -I$HOME/usr/include -L$HOME/usr/lib setaddr.c -luci -o setaddr
    Run the compiled example binary:
        export LD_LIBRARY_PATH=$HOME/usr/lib
         ./setaddr

     

    #include <stdio.h>
    #include <string.h>
    #include <uci.h>
    #include <stdlib.h>
    //Simple define a related package       
    #define UCI_CFG_PKG "/etc/config/network"
    #define UCI_PACKAGE "network"
    #define UCI_SECTION "lan"
    #define UCI_OPTION "ipaddr"
    #define UCI_VALUE "192.168.2.12"

    static int uci_set_option_string(const char *section, const char *option, const char *value)
    {
            int ret;
            struct uci_context *ctx = NULL;
            struct uci_package* pkg;
            struct uci_section* sec;
            struct uci_ptr ptr;

            char* err_str = NULL;
            char err_msg[256] = {'\0'};

            if(ctx == NULL)
            {
                    ctx = uci_alloc_context();              //apply a context
                    if(ctx==NULL)
                    {
                            snprintf(err_msg, sizeof(err_msg),"Failed to alloc uci context");
                            return -1;
                    }
            }

            ret = uci_load(ctx, UCI_CFG_PKG, &pkg);         //Load required uci file
            if(ret !=0 || pkg == NULL)
            {
                    snprintf(err_msg, sizeof(err_msg),"Failed to load package: '%s' with error", UCI_CFG_PKG);
                    goto error_pkg;
            }

            sec = uci_lookup_section(ctx, pkg, section);
            if(sec == NULL)
            {
                    snprintf(err_msg, sizeof(err_msg), "Failed to find section: '%s'", section);
                    goto error_msg;
            }

            memset(&ptr, 0, sizeof(struct uci_ptr));

            ptr.package = UCI_PACKAGE;
            ptr.section = section;
            ptr.option = option;
            ptr.value = value;

            ptr.p = pkg;
            ptr.s = sec;

      ret = uci_set(ctx, &ptr);
            if(ret != 0)
            {
                    snprintf(err_msg, sizeof(err_msg), "Failed to set option: '%s' with error", option);
                    goto error_uci;
            }

            uci_save(ctx, pkg);
            uci_commit(ctx, &pkg, false);           //Commit the changes or it will not effect the local file.  

            uci_unload(ctx, pkg);
            uci_free_context(ctx);
            return 0;

            error_uci:
                    uci_unload(ctx, pkg);
            error_pkg:
                    uci_get_errorstr(ctx, &err_str, err_msg);
                    free(err_str);
                    return -1;
            error_msg:
                    uci_unload(ctx, pkg);
                    return -1;
    }

    int main(int argc, char **argv)
    {
            uci_set_option_string(UCI_SECTION, UCI_OPTION, UCI_VALUE);
            return 0;
    }
                         

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar