搜索
bottom↓
回复: 0

Oepnwrt UCI interface.

[复制链接]

出0入0汤圆

发表于 2018-8-24 12:48:45 | 显示全部楼层 |阅读模式
大家好,

        最近调试产品时用到openwrt,
       
        硬件平台:MT7688
        软件版本:openwrt 18.06

        需要设置网络参数,google找了一下,可以使用UCI interface,其头文件如下:

  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. * GNU Lesser General Public License for more details.
  13. */

  14. #ifndef __LIBUCI_H
  15. #define __LIBUCI_H

  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif

  19. //#include "uci_config.h"

  20. /*
  21. * you can use these defines to enable debugging behavior for
  22. * apps compiled against libuci:
  23. *
  24. * #define UCI_DEBUG_TYPECAST:
  25. *   enable uci_element typecast checking at run time
  26. *
  27. */

  28. #include <stdbool.h>
  29. #include <setjmp.h>
  30. #include <stdio.h>
  31. #include <stdint.h>

  32. #define UCI_CONFDIR "/etc/config"
  33. #define UCI_SAVEDIR "/tmp/.uci"
  34. #define UCI_DIRMODE 0700
  35. #define UCI_FILEMODE 0600

  36. enum
  37. {
  38.         UCI_OK = 0,
  39.         UCI_ERR_MEM,
  40.         UCI_ERR_INVAL,
  41.         UCI_ERR_NOTFOUND,
  42.         UCI_ERR_IO,
  43.         UCI_ERR_PARSE,
  44.         UCI_ERR_DUPLICATE,
  45.         UCI_ERR_UNKNOWN,
  46.         UCI_ERR_LAST
  47. };

  48. struct uci_list;
  49. struct uci_list
  50. {
  51.         struct uci_list *next;
  52.         struct uci_list *prev;
  53. };

  54. struct uci_ptr;
  55. struct uci_element;
  56. struct uci_package;
  57. struct uci_section;
  58. struct uci_option;
  59. struct uci_delta;
  60. struct uci_context;
  61. struct uci_backend;
  62. struct uci_parse_option;
  63. struct uci_parse_context;


  64. /**
  65. * uci_alloc_context: Allocate a new uci context
  66. */
  67. extern struct uci_context *uci_alloc_context(void);

  68. /**
  69. * uci_free_context: Free the uci context including all of its data
  70. */
  71. extern void uci_free_context(struct uci_context *ctx);

  72. /**
  73. * uci_perror: Print the last uci error that occured
  74. * @ctx: uci context
  75. * @str: string to print before the error message
  76. */
  77. extern void uci_perror(struct uci_context *ctx, const char *str);

  78. /**
  79. * uci_geterror: Get an error string for the last uci error
  80. * @ctx: uci context
  81. * @dest: target pointer for the string
  82. * @str: prefix for the error message
  83. *
  84. * Note: string must be freed by the caller
  85. */
  86. extern void uci_get_errorstr(struct uci_context *ctx, char **dest, const char *str);

  87. /**
  88. * uci_import: Import uci config data from a stream
  89. * @ctx: uci context
  90. * @stream: file stream to import from
  91. * @name: (optional) assume the config has the given name
  92. * @package: (optional) store the last parsed config package in this variable
  93. * @single: ignore the 'package' keyword and parse everything into a single package
  94. *
  95. * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
  96. * if 'package' points to a non-null struct pointer, enable delta tracking and merge
  97. */
  98. extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single);

  99. /**
  100. * uci_export: Export one or all uci config packages
  101. * @ctx: uci context
  102. * @stream: output stream
  103. * @package: (optional) uci config package to export
  104. * @header: include the package header
  105. */
  106. extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header);

  107. /**
  108. * uci_load: Parse an uci config file and store it in the uci context
  109. *
  110. * @ctx: uci context
  111. * @name: name of the config file (relative to the config directory)
  112. * @package: store the loaded config package in this variable
  113. */
  114. extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package);

  115. /**
  116. * uci_unload: Unload a config file from the uci context
  117. *
  118. * @ctx: uci context
  119. * @package: pointer to the uci_package struct
  120. */
  121. extern int uci_unload(struct uci_context *ctx, struct uci_package *p);

  122. /**
  123. * uci_lookup_ptr: Split an uci tuple string and look up an element tree
  124. * @ctx: uci context
  125. * @ptr: lookup result struct
  126. * @str: uci tuple string to look up
  127. * @extended: allow extended syntax lookup
  128. *
  129. * if extended is set to true, uci_lookup_ptr supports the following
  130. * extended syntax:
  131. *
  132. * Examples:
  133. *   network.@interface[0].ifname ('ifname' option of the first interface section)
  134. *   network.@interface[-1]       (last interface section)
  135. * Note: uci_lookup_ptr will automatically load a config package if necessary
  136. * @str must not be constant, as it will be modified and used for the strings inside @ptr,
  137. * thus it must also be available as long as @ptr is in use.
  138. *
  139. * This function returns UCI_ERR_NOTFOUND if the package specified in the tuple
  140. * string cannot be found.  Otherwise it will return UCI_OK.
  141. *
  142. * Note that failures in looking up other parts, if they are also specfied,
  143. * including section and option, will also have a return value UCI_OK but with
  144. * ptr->flags * UCI_LOOKUP_COMPLETE not set.
  145. */
  146. extern int uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended);

  147. /**
  148. * uci_add_section: Add an unnamed section
  149. * @ctx: uci context
  150. * @p: package to add the section to
  151. * @type: section type
  152. * @res: pointer to store a reference to the new section in
  153. */
  154. extern int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res);

  155. /**
  156. * uci_set: Set an element's value; create the element if necessary
  157. * @ctx: uci context
  158. * @ptr: uci pointer
  159. *
  160. * The updated/created element is stored in ptr->last
  161. */
  162. extern int uci_set(struct uci_context *ctx, struct uci_ptr *ptr);

  163. /**
  164. * uci_add_list: Append a string to an element list
  165. * @ctx: uci context
  166. * @ptr: uci pointer (with value)
  167. *
  168. * Note: if the given option already contains a string value,
  169. * it will be converted to an 1-element-list before appending the next element
  170. */
  171. extern int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr);

  172. /**
  173. * uci_del_list: Remove a string from an element list
  174. * @ctx: uci context
  175. * @ptr: uci pointer (with value)
  176. *
  177. */
  178. extern int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr);

  179. /**
  180. * uci_reorder: Reposition a section
  181. * @ctx: uci context
  182. * @s: uci section to reposition
  183. * @pos: new position in the section list
  184. */
  185. extern int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos);

  186. /**
  187. * uci_rename: Rename an element
  188. * @ctx: uci context
  189. * @ptr: uci pointer (with value)
  190. */
  191. extern int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr);

  192. /**
  193. * uci_delete: Delete a section or option
  194. * @ctx: uci context
  195. * @ptr: uci pointer
  196. */
  197. extern int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr);

  198. /**
  199. * uci_save: save change delta for a package
  200. * @ctx: uci context
  201. * @p: uci_package struct
  202. */
  203. extern int uci_save(struct uci_context *ctx, struct uci_package *p);

  204. /**
  205. * uci_commit: commit changes to a package
  206. * @ctx: uci context
  207. * @p: uci_package struct pointer
  208. * @overwrite: overwrite existing config data and flush delta
  209. *
  210. * committing may reload the whole uci_package data,
  211. * the supplied pointer is updated accordingly
  212. */
  213. extern int uci_commit(struct uci_context *ctx, struct uci_package **p, bool overwrite);

  214. /**
  215. * uci_list_configs: List available uci config files
  216. * @ctx: uci context
  217. *
  218. * caller is responsible for freeing the allocated memory behind list
  219. */
  220. extern int uci_list_configs(struct uci_context *ctx, char ***list);

  221. /**
  222. * uci_set_savedir: override the default delta save directory
  223. * @ctx: uci context
  224. * @dir: directory name
  225. *
  226. * This will also try adding the specified dir to the end of delta pathes.
  227. */
  228. extern int uci_set_savedir(struct uci_context *ctx, const char *dir);

  229. /**
  230. * uci_set_savedir: override the default config storage directory
  231. * @ctx: uci context
  232. * @dir: directory name
  233. */
  234. extern int uci_set_confdir(struct uci_context *ctx, const char *dir);

  235. /**
  236. * uci_add_delta_path: add a directory to the search path for change delta files
  237. * @ctx: uci context
  238. * @dir: directory name
  239. *
  240. * This function allows you to add directories, which contain 'overlays'
  241. * for the active config, that will never be committed.
  242. *
  243. * Adding a duplicate directory will cause UCI_ERR_DUPLICATE be returned.
  244. */
  245. extern int uci_add_delta_path(struct uci_context *ctx, const char *dir);

  246. /**
  247. * uci_revert: revert all changes to a config item
  248. * @ctx: uci context
  249. * @ptr: uci pointer
  250. */
  251. extern int uci_revert(struct uci_context *ctx, struct uci_ptr *ptr);

  252. /**
  253. * uci_parse_argument: parse a shell-style argument, with an arbitrary quoting style
  254. * @ctx: uci context
  255. * @stream: input stream
  256. * @str: pointer to the current line (use NULL for parsing the next line)
  257. * @result: pointer for the result
  258. */
  259. extern int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result);

  260. /**
  261. * uci_set_backend: change the default backend
  262. * @ctx: uci context
  263. * @name: name of the backend
  264. *
  265. * The default backend is "file", which uses /etc/config for config storage
  266. */
  267. extern int uci_set_backend(struct uci_context *ctx, const char *name);

  268. /**
  269. * uci_validate_text: validate a value string for uci options
  270. * @str: value
  271. *
  272. * this function checks whether a given string is acceptable as value
  273. * for uci options
  274. */
  275. extern bool uci_validate_text(const char *str);

  276. /**
  277. * uci_parse_ptr: parse a uci string into a uci_ptr
  278. * @ctx: uci context
  279. * @ptr: target data structure
  280. * @str: string to parse
  281. *
  282. * str is modified by this function
  283. */
  284. int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str);

  285. /**
  286. * uci_lookup_next: lookup a child element
  287. * @ctx: uci context
  288. * @e: target element pointer
  289. * @list: list of elements
  290. * @name: name of the child element
  291. *
  292. * if parent is NULL, the function looks up the package with the given name
  293. */
  294. int uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name);

  295. /**
  296. * uci_parse_section: look up a set of options
  297. * @s: uci section
  298. * @opts: list of options to look up
  299. * @n_opts: number of options to look up
  300. * @tb: array of pointers to found options
  301. */
  302. void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
  303.                        int n_opts, struct uci_option **tb);

  304. /**
  305. * uci_hash_options: build a hash over a list of options
  306. * @tb: list of option pointers
  307. * @n_opts: number of options
  308. */
  309. uint32_t uci_hash_options(struct uci_option **tb, int n_opts);


  310. /* UCI data structures */
  311. enum uci_type {
  312.         UCI_TYPE_UNSPEC = 0,
  313.         UCI_TYPE_DELTA = 1,
  314.         UCI_TYPE_PACKAGE = 2,
  315.         UCI_TYPE_SECTION = 3,
  316.         UCI_TYPE_OPTION = 4,
  317.         UCI_TYPE_PATH = 5,
  318.         UCI_TYPE_BACKEND = 6,
  319.         UCI_TYPE_ITEM = 7,
  320.         UCI_TYPE_HOOK = 8,
  321. };

  322. enum uci_option_type {
  323.         UCI_TYPE_STRING = 0,
  324.         UCI_TYPE_LIST = 1,
  325. };

  326. enum uci_flags {
  327.         UCI_FLAG_STRICT =        (1 << 0), /* strict mode for the parser */
  328.         UCI_FLAG_PERROR =        (1 << 1), /* print parser error messages */
  329.         UCI_FLAG_EXPORT_NAME =   (1 << 2), /* when exporting, name unnamed sections */
  330.         UCI_FLAG_SAVED_DELTA = (1 << 3), /* store the saved delta in memory as well */
  331. };

  332. struct uci_element
  333. {
  334.         struct uci_list list;
  335.         enum uci_type type;
  336.         char *name;
  337. };

  338. struct uci_backend
  339. {
  340.         struct uci_element e;
  341.         char **(*list_configs)(struct uci_context *ctx);
  342.         struct uci_package *(*load)(struct uci_context *ctx, const char *name);
  343.         void (*commit)(struct uci_context *ctx, struct uci_package **p, bool overwrite);

  344.         /* private: */
  345.         const void *ptr;
  346.         void *priv;
  347. };

  348. struct uci_context
  349. {
  350.         /* list of config packages */
  351.         struct uci_list root;

  352.         /* parser context, use for error handling only */
  353.         struct uci_parse_context *pctx;

  354.         /* backend for import and export */
  355.         struct uci_backend *backend;
  356.         struct uci_list backends;

  357.         /* uci runtime flags */
  358.         enum uci_flags flags;

  359.         char *confdir;
  360.         char *savedir;

  361.         /* search path for delta files */
  362.         struct uci_list delta_path;

  363.         /* private: */
  364.         int err;
  365.         const char *func;
  366.         jmp_buf trap;
  367.         bool internal, nested;
  368.         char *buf;
  369.         int bufsz;
  370. };

  371. struct uci_package
  372. {
  373.         struct uci_element e;
  374.         struct uci_list sections;
  375.         struct uci_context *ctx;
  376.         bool has_delta;
  377.         char *path;

  378.         /* private: */
  379.         struct uci_backend *backend;
  380.         void *priv;
  381.         int n_section;
  382.         struct uci_list delta;
  383.         struct uci_list saved_delta;
  384. };

  385. struct uci_section
  386. {
  387.         struct uci_element e;
  388.         struct uci_list options;
  389.         struct uci_package *package;
  390.         bool anonymous;
  391.         char *type;
  392. };

  393. struct uci_option
  394. {
  395.         struct uci_element e;
  396.         struct uci_section *section;
  397.         enum uci_option_type type;
  398.         union {
  399.                 struct uci_list list;
  400.                 char *string;
  401.         } v;
  402. };

  403. /*
  404. * UCI_CMD_ADD is used for anonymous sections or list values
  405. */
  406. enum uci_command {
  407.         UCI_CMD_ADD,
  408.         UCI_CMD_REMOVE,
  409.         UCI_CMD_CHANGE,
  410.         UCI_CMD_RENAME,
  411.         UCI_CMD_REORDER,
  412.         UCI_CMD_LIST_ADD,
  413.         UCI_CMD_LIST_DEL,
  414.         __UCI_CMD_MAX,
  415.         __UCI_CMD_LAST = __UCI_CMD_MAX - 1
  416. };
  417. extern char const uci_command_char[];

  418. struct uci_delta
  419. {
  420.         struct uci_element e;
  421.         enum uci_command cmd;
  422.         char *section;
  423.         char *value;
  424. };

  425. struct uci_ptr
  426. {
  427.         enum uci_type target;
  428.         enum {
  429.                 UCI_LOOKUP_DONE =     (1 << 0),
  430.                 UCI_LOOKUP_COMPLETE = (1 << 1),
  431.                 UCI_LOOKUP_EXTENDED = (1 << 2),
  432.         } flags;

  433.         struct uci_package *p;
  434.         struct uci_section *s;
  435.         struct uci_option *o;
  436.         struct uci_element *last;

  437.         const char *package;
  438.         const char *section;
  439.         const char *option;
  440.         const char *value;
  441. };

  442. struct uci_parse_option {
  443.         const char *name;
  444.         enum uci_option_type type;
  445. };


  446. /* linked list handling */
  447. #ifndef offsetof
  448. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  449. #endif

  450. /**
  451. * container_of - cast a member of a structure out to the containing structure
  452. * @ptr:    the pointer to the member.
  453. * @type:   the type of the container struct this is embedded in.
  454. * @member: the name of the member within the struct.
  455. */
  456. #ifndef container_of
  457. #define container_of(ptr, type, member) \
  458.         ((type *) ((char *)ptr - offsetof(type,member)))
  459. #endif


  460. /**
  461. * uci_list_entry: casts an uci_list pointer to the containing struct.
  462. * @_type: config, section or option
  463. * @_ptr: pointer to the uci_list struct
  464. */
  465. #define list_to_element(ptr) \
  466.         container_of(ptr, struct uci_element, list)

  467. /**
  468. * uci_foreach_entry: loop through a list of uci elements
  469. * @_list: pointer to the uci_list struct
  470. * @_ptr: iteration variable, struct uci_element
  471. *
  472. * use like a for loop, e.g:
  473. *   uci_foreach(&list, p) {
  474. *           ...
  475. *   }
  476. */
  477. #define uci_foreach_element(_list, _ptr)                \
  478.         for(_ptr = list_to_element((_list)->next);        \
  479.                 &_ptr->list != (_list);                        \
  480.                 _ptr = list_to_element(_ptr->list.next))

  481. /**
  482. * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion
  483. * @_list: pointer to the uci_list struct
  484. * @_tmp: temporary variable, struct uci_element *
  485. * @_ptr: iteration variable, struct uci_element *
  486. *
  487. * use like a for loop, e.g:
  488. *   uci_foreach(&list, p) {
  489. *           ...
  490. *   }
  491. */
  492. #define uci_foreach_element_safe(_list, _tmp, _ptr)                \
  493.         for(_ptr = list_to_element((_list)->next),                \
  494.                 _tmp = list_to_element(_ptr->list.next);        \
  495.                 &_ptr->list != (_list);                        \
  496.                 _ptr = _tmp, _tmp = list_to_element(_ptr->list.next))

  497. /**
  498. * uci_list_empty: returns true if a list is empty
  499. * @list: list head
  500. */
  501. #define uci_list_empty(list) ((list)->next == (list))

  502. /* wrappers for dynamic type handling */
  503. #define uci_type_backend UCI_TYPE_BACKEND
  504. #define uci_type_delta UCI_TYPE_DELTA
  505. #define uci_type_package UCI_TYPE_PACKAGE
  506. #define uci_type_section UCI_TYPE_SECTION
  507. #define uci_type_option UCI_TYPE_OPTION

  508. /* element typecasting */
  509. #ifdef UCI_DEBUG_TYPECAST
  510. static const char *uci_typestr[] = {
  511.         [uci_type_backend] = "backend",
  512.         [uci_type_delta] = "delta",
  513.         [uci_type_package] = "package",
  514.         [uci_type_section] = "section",
  515.         [uci_type_option] = "option",
  516. };

  517. static void uci_typecast_error(int from, int to)
  518. {
  519.         fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]);
  520. }

  521. #define BUILD_CAST(_type) \
  522.         static inline struct uci_ ## _type *uci_to_ ## _type (struct uci_element *e) \
  523.         { \
  524.                 if (e->type != uci_type_ ## _type) { \
  525.                         uci_typecast_error(e->type, uci_type_ ## _type); \
  526.                 } \
  527.                 return (struct uci_ ## _type *) e; \
  528.         }

  529. BUILD_CAST(backend)
  530. BUILD_CAST(delta)
  531. BUILD_CAST(package)
  532. BUILD_CAST(section)
  533. BUILD_CAST(option)

  534. #else
  535. #define uci_to_backend(ptr) container_of(ptr, struct uci_backend, e)
  536. #define uci_to_delta(ptr) container_of(ptr, struct uci_delta, e)
  537. #define uci_to_package(ptr) container_of(ptr, struct uci_package, e)
  538. #define uci_to_section(ptr) container_of(ptr, struct uci_section, e)
  539. #define uci_to_option(ptr)  container_of(ptr, struct uci_option, e)
  540. #endif

  541. /**
  542. * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast
  543. * @ctx: uci context
  544. * @type: {package,section,option}
  545. * @name: string containing the name of the element
  546. * @datasize: additional buffer size to reserve at the end of the struct
  547. */
  548. #define uci_alloc_element(ctx, type, name, datasize) \
  549.         uci_to_ ## type (uci_alloc_generic(ctx, uci_type_ ## type, name, sizeof(struct uci_ ## type) + datasize))

  550. #define uci_dataptr(ptr) \
  551.         (((char *) ptr) + sizeof(*ptr))

  552. /**
  553. * uci_lookup_package: look up a package
  554. * @ctx: uci context
  555. * @name: name of the package
  556. */
  557. static inline struct uci_package *
  558. uci_lookup_package(struct uci_context *ctx, const char *name)
  559. {
  560.         struct uci_element *e = NULL;
  561.         if (uci_lookup_next(ctx, &e, &ctx->root, name) == 0)
  562.                 return uci_to_package(e);
  563.         else
  564.                 return NULL;
  565. }

  566. /**
  567. * uci_lookup_section: look up a section
  568. * @ctx: uci context
  569. * @p: package that the section belongs to
  570. * @name: name of the section
  571. */
  572. static inline struct uci_section *
  573. uci_lookup_section(struct uci_context *ctx, struct uci_package *p, const char *name)
  574. {
  575.         struct uci_element *e = NULL;
  576.         if (uci_lookup_next(ctx, &e, &p->sections, name) == 0)
  577.                 return uci_to_section(e);
  578.         else
  579.                 return NULL;
  580. }

  581. /**
  582. * uci_lookup_option: look up an option
  583. * @ctx: uci context
  584. * @section: section that the option belongs to
  585. * @name: name of the option
  586. */
  587. static inline struct uci_option *
  588. uci_lookup_option(struct uci_context *ctx, struct uci_section *s, const char *name)
  589. {
  590.         struct uci_element *e = NULL;
  591.         if (uci_lookup_next(ctx, &e, &s->options, name) == 0)
  592.                 return uci_to_option(e);
  593.         else
  594.                 return NULL;
  595. }

  596. static inline const char *
  597. uci_lookup_option_string(struct uci_context *ctx, struct uci_section *s, const char *name)
  598. {
  599.         struct uci_option *o;

  600.         o = uci_lookup_option(ctx, s, name);
  601.         if (!o || o->type != UCI_TYPE_STRING)
  602.                 return NULL;

  603.         return o->v.string;
  604. }

  605. #ifndef BITS_PER_LONG
  606. #define BITS_PER_LONG (8 * sizeof(unsigned long))
  607. #endif

  608. static inline void uci_bitfield_set(unsigned long *bits, int bit)
  609. {
  610.         bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG));
  611. }

  612. #ifdef __cplusplus
  613. }
  614. #endif

  615. #endif
复制代码


交叉编译连接的时候需要指定头文件的位置及库文件的位置,我直接将这两个文件放到了交叉编译工具链的的sysroot里面去了,例如,mipsel-openwrt-linux-gcc -o xxx xxx.c  -I./ -luci -lubox  [libuci依赖libubox,这两个库文件可以从build_dir/target.../中找到]
Example code:

  1. static int set_tuple_cfg(char *tuple, const char *param) {

  2.         if((tuple == NULL) || (param == NULL)) {

  3.                 return -1;
  4.         }

  5.         struct uci_ptr ptr;
  6.         struct uci_context *ctx = uci_alloc_context();
  7.         if(!ctx) {

  8.                 return -1;
  9.         }

  10.         if((uci_lookup_ptr(ctx, &ptr, tuple, false) != UCI_OK) || (ptr.o == NULL || ptr.o->v.string == NULL)) {
  11.        
  12.                 perror("Read tuple error");       
  13.                 uci_free_context(ctx);
  14.                 return -1;

  15.         }
  16.         if(ptr.flags & UCI_LOOKUP_COMPLETE) {

  17.         //        strcpy(buffer, ptr.o->v.string);
  18.                 ptr.value = param;
  19.                 if(uci_set(ctx, &ptr) != UCI_OK) {

  20.                         perror("Set tuple %s tuple error");
  21.                         uci_free_context(ctx);
  22.                         return -1;
  23.                 }
  24.         }
  25.         if(uci_commit(ctx, &ptr.p, false) != UCI_OK) {

  26.                 perror("commit %s tuple error");
  27.                 uci_free_context(ctx);
  28.                 return -1;
  29.         }
  30.         uci_free_context(ctx);

  31.         return 0;
  32. }

  33. static int get_tuple_cfg(char *tuple, char *param) {

  34.         if((tuple == NULL) || (param == NULL)) {

  35.                 return -1;
  36.         }

  37.         struct uci_ptr ptr;
  38.         struct uci_context *ctx = uci_alloc_context();
  39.         if(!ctx) {

  40.                 return -1;
  41.         }

  42.         if((uci_lookup_ptr(ctx, &ptr, tuple, false) != UCI_OK) || (ptr.o == NULL || ptr.o->v.string == NULL)) {
  43.        
  44.                 perror("Read tuple error");       
  45.                 uci_free_context(ctx);
  46.                 return -1;

  47.         }
  48.         if(ptr.flags & UCI_LOOKUP_COMPLETE) {

  49.                 strcpy(param, ptr.o->v.string);
  50.         }
  51.         uci_free_context(ctx);

  52.         return 0;
  53. }

  54. int netget_func(char *ip, char *netmask, char *gw) {


  55.         if((ip == NULL) || (netmask == NULL) || (gw == NULL)) {

  56.                 return -1;
  57.         }       

  58.         char ip_path[] = "network.lan.ipaddr";       
  59.         if(get_tuple_cfg(ip_path, ip) == -1) {

  60.                 return -1;
  61.         }

  62.         char mask_path[] = "network.lan.netmask";       
  63.         if(get_tuple_cfg(mask_path, netmask) == -1) {

  64.                 return -1;
  65.         }

  66.         char gw_path[] = "network.lan.gateway";       
  67.         if(get_tuple_cfg(gw_path, gw) == -1) {

  68.                 return -1;
  69.         }

  70.         return 0;
  71. }


  72. int netcfg_func(const char *ip, const char *netmask, const char *gw) {

  73.         if((ip == NULL) || (netmask == NULL) || (gw == NULL)) {

  74.                 return -1;
  75.         }       
  76.         char ip_path[] = "network.lan.ipaddr";       
  77.         if(set_tuple_cfg(ip_path, ip) == -1) {

  78.                 return -1;
  79.         }
  80.         char mask_path[] = "network.lan.netmask";       
  81.         if(set_tuple_cfg(mask_path, netmask) == -1) {

  82.                 return -1;
  83.         }
  84.         char gw_path[] = "network.lan.gateway";       
  85.         if(set_tuple_cfg(gw_path, gw) == -1) {

  86.                 return -1;
  87.         }
  88.         return 0;
  89. }
复制代码


希望对大家有所帮助,相互学习,谢谢。

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 05:12

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表