本篇记录是续上一篇、openWRT 中 Web 配置参数的延续,系统中有web的配置方法,程序中需要获取配置内容,更新程序运行状态。
下面直接把程序贴上来,不解释什么了。
#include <chrono>
#include <iostream>
#include <thread>
#include <mutex>
#include <fstream>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#include "uci.h" //> libuci.so 接口头文件,具体 uci 提供 api 内容.
uint32_t Lora_GateWay::Uci_Get_GateWay_Sn(const char *sn){
struct uci_context *c;
struct uci_ptr p;
char *a = strdup("gatewayConf.parament.sn");
c = uci_alloc_context();
try
{
/* code */
if (UCI_OK != uci_lookup_ptr(c, &p, a, true)) {
uci_perror(c, "no found!\n");
spdlog::error("{} no found :{}",__FILE__, a);
uci_free_context(c);
free(a);
return -1;
}
std::printf("SN: %s, len:%d\n", p.o->v.string, strlen(p.o->v.string));
if(strlen(p.o->v.string) <= 25){
std::strcpy((char*)sn, p.o->v.string);
//std::memcpy((char*)sn, (char*)p.o->v.string, strlen(p.o->v.string));
uci_free_context(c);
free(a);
return (0);
}else
{
/* code */
uci_free_context(c);
free(a);
return -2;
}
}
catch(const std::exception& e)
{
spdlog::error("{} Exception: {}",__FILE__, e.what());
uci_free_context(c);
free(a);
return -3;
}
}
uint32_t Lora_GateWay::Uci_Get_GateWay_ipv4addr(const char *addr){
struct uci_context *c;
struct uci_ptr p;
char *a = strdup("gatewayConf.parament.IPv4addr");
c = uci_alloc_context();
try
{
/* code */
if (UCI_OK != uci_lookup_ptr(c, &p, a, true)) {
uci_perror(c, "no found!\n");
spdlog::error("{} no found :{}",__FILE__, a);
uci_free_context(c);
free(a);
return -1;
}
printf("IPv4addr: %s, len:%d\n", p.o->v.string, strlen(p.o->v.string));
std::strcpy((char*)addr, p.o->v.string);
uci_free_context(c);
free(a);
return(0);
}
catch(const std::exception& e)
{
spdlog::error("{} Exception: {}",__FILE__, e.what());
uci_free_context(c);
free(a);
return(-2);
}
}
uint32_t Lora_GateWay::Uci_Get_GateWay_heartbeat(const char *hb){
struct uci_context *c;
struct uci_ptr p;
char *a = strdup("gatewayConf.parament.HeartBeat");
c = uci_alloc_context();
try
{
/* code */
if (UCI_OK != uci_lookup_ptr(c, &p, a, true)) {
uci_perror(c, "no found!\n");
spdlog::error("{} no found :{}",__FILE__, a);
uci_free_context(c);
free(a);
return -1;
}
printf("HeartBeat: %s\n", p.o->v.string);
std::strcpy((char*)hb, p.o->v.string);
uci_free_context(c);
free(a);
return(0);
}
catch(const std::exception& e)
{
spdlog::error("{} Exception: {}",__FILE__, e.what());
uci_free_context(c);
free(a);
return(-2);
}
}
此程序是 Lora_GateWay类的三个方法,获取 luci 配置的数据内容。