内部使用的libstring库
1.添加一些指针的判断
2.set uci option 和get,添加了互斥锁
3.一些自定义常用函数
libstring.h
#define TRUE 1
#define FALSE 0
#define SUCCESS 0
#define FAIL -1
#define BUF_SIZE_1024 1024
#define BUF_SIZE_128 128
typedef uint8_t UBOOL8;
typedef int64_t SINT64;
/** Signed 32 bit integer. */
typedef int32_t SINT32;
/** Signed 16 bit integer. */
typedef int16_t SINT16;
/** Signed 8 bit integer. */
typedef int8_t SINT8;
/** Unsigned 64 bit integer.
* This data type was introduced in TR-106 Issue 1, Admendment 2, Sept. 2008
*/
typedef uint64_t UINT64;
/** Unsigned 32 bit integer. */
typedef uint32_t UINT32;
/** Unsigned 16 bit integer. */
typedef uint16_t UINT16;
/** Unsigned 8 bit integer. */
typedef uint8_t UINT8;
char *openUtil_strstr(const char *s1, const char *s2);
int openUtil_strcmp(const char *s1, const char *s2);
int openUtil_strcasecmp(const char *s1, const char *s2);
int openUtil_strncmp(const char *s1, const char *s2, SINT32 n);
int openUtil_strncasecmp(const char *s1, const char *s2, SINT32 n);
char *openUtil_strcpy(char *dest, const char *src);
char *openUtil_strcat(char *dest, const char *src);
char *openUtil_strncpy(char *dest, const char *src, SINT32 dlen);
int openUtil_strlen(const char *src);
char *openUtil_rtrim(char *str);
int GetNVRamData(char* pPath,char* pBuf, int nBufLen);
int CommitNVRamData(char* pCommFile);
int SetNVRamData(char* pName, int bCommit, char* pCommFile);
int GetCMStatusData(char *getValue, char *output, int size);
void openUtil_Reboot(void);
void openUtil_FactoryReset(void);
libstring.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> /* for isDigit, really should be in oal_strconv.c */
#include <sys/stat.h> /* this really should be in oal_strconv.c */
#include <arpa/inet.h> /* for inet_aton */
#include <sys/time.h> /* for inet_aton */
#include <pthread.h>
#include "libstring.h"
#define cprintf(fmt, args...) do { \
FILE *fp = fopen("/dev/console", "w"); \
if (fp) { \
fprintf(fp, fmt , ## args); \
fclose(fp); \
} \
} while (0)
static pthread_mutex_t uci_set_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t uci_get_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t uci_commit_mutex = PTHREAD_MUTEX_INITIALIZER;
//if find option in file return 0, else return -1
int check_uci_option_exist(const char* pName)
{
char file_path[BUF_SIZE_1024] = {0};
char cmd_buf[BUF_SIZE_1024] = {0};
char uci_option[BUF_SIZE_1024] = {0};
char out_buf[BUF_SIZE_1024] = {0};
char pName_buf[BUF_SIZE_1024] = {0};
char str[BUF_SIZE_128 + 1];
char *ret=NULL;
char *uci_ret=NULL;
int fun_ret=-1;
FILE *fp=NULL;
if ((pName == NULL) )
{
return fun_ret;
}
//cprintf("pName:%s\n",pName);
strcpy(pName_buf,pName);
//cprintf("pName_buf:%s\n",pName_buf);
sscanf(pName_buf, "%[^.]", file_path);
//cprintf("file_path:%s\n",file_path);
ret = strrchr(pName_buf, '.');
//printf("ret:%s\n",ret);
//printf("ret+1:%s\n",ret+1);
strcpy(uci_option,ret+1);
//cprintf("uci_option:%s\n",uci_option);
//判断文件是否打开失败
if ( (fp = fopen(file_path, "rt")) == NULL )
{
cprintf("Fail to open file!");
return fun_ret; //not exist
}
//循环读取文件的每一行数据
while( fgets(str, BUF_SIZE_128, fp) != NULL )
{
//cprintf("str:%s\n", str);
uci_ret = strstr(str,uci_option);
if(uci_ret != NULL)
{
//cprintf("uci_ret:%s\n", uci_ret);
//fclose(fp);
fun_ret = 0; //uci option exist
break;
}
}
//操作结束后关闭文件
fclose(fp);
return fun_ret;//not exist
}
char *openUtil_strstr(const char *s1, const char *s2)
{
const char emptyStr = '\0';
if (s1 == NULL)
{
s1 = &emptyStr;
}
if (s2 == NULL)
{
s2 = &emptyStr;
}
return strstr(s1,s2);
}
int openUtil_strcmp(const char *s1, const char *s2)
{
char emptyStr = '\0';
char *str1 = (char *) s1;
char *str2 = (char *) s2;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strcmp(str1, str2);
}
int openUtil_strcasecmp(const char *s1, const char *s2)
{
char emptyStr = '\0';
char *str1 = (char *) s1;
char *str2 = (char *) s2;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strcasecmp(str1, str2);
}
int openUtil_strncmp(const char *s1, const char *s2, SINT32 n)
{
char emptyStr = '\0';
char *str1 = (char *) s1;
char *str2 = (char *) s2;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strncmp(str1, str2, n);
}
int openUtil_strncasecmp(const char *s1, const char *s2, SINT32 n)
{
char emptyStr = '\0';
char *str1 = (char *) s1;
char *str2 = (char *) s2;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strncasecmp(str1, str2, n);
}
char *openUtil_strcpy(char *dest, const char *src)
{
char emptyStr = '\0';
char *str1 = (char *)dest;
char *str2 = (char *)src;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strcpy(str1, str2);
}
char *openUtil_strcat(char *dest, const char *src)
{
char emptyStr = '\0';
char *str1 = (char *)dest;
char *str2 = (char *)src;
if (str1 == NULL)
{
str1 = &emptyStr;
}
if (str2 == NULL)
{
str2 = &emptyStr;
}
return strcat(str1, str2);
}
char *openUtil_strncpy(char *dest, const char *src, SINT32 dlen)
{
if((src == NULL) || (dest == NULL))
{
printf("null pointer reference src =%u ,dest =%u\n", src, dest);
return dest;
}
if( strlen(src)+1 > (UINT32) dlen )
{
printf("truncating:src string length > dest buffer\n");
strncpy(dest,src,dlen-1);
dest[dlen-1] ='\0';
}
else
{
strcpy(dest,src);
}
return dest;
}
int openUtil_strlen(const char *src)
{
char emptyStr[1] = {0};
char *str = (char *)src;
if(src == NULL)
{
str = emptyStr;
}
return strlen(str);
}
/*Remove the tail blank character
Standard blank characters include:
' ' (0x20) space (SPC)
'\t'(0x09) horizontal tab (TAB)
'\n'(0x0a) newline (LF)
'\v'(0x0b) vertical tab (VT)
'\f'(0x0c) feed (FF) page change
'\r'(0x0D) carriage return (CR) return character
Change line:windows \r\n linux \n mac \r
*/
char *openUtil_rtrim(char *str)
{
if (str == NULL || *str == '\0')
{
return str;
}
int len = strlen(str);
char *p = str + len - 1;
while (p >= str && isspace(*p))
{
*p = '\0';
--p;
}
return str;
}
int GetNVRamData(char* pName, char* pBuf, int nBufLen)
{
int bRet = FALSE;
FILE *fp=NULL;
int getlen = 0, i = 0 ;
char szCmd[BUF_SIZE_1024] = {0};
if ((pName == NULL) || (pBuf == NULL) || (nBufLen <= 0))
{
return FALSE;
}
if (strlen(pName) > (BUF_SIZE_1024 - 24))
{
return FALSE;
}
cprintf("\n\nwait lock...%s,%d........pName:%s\n",__FUNCTION__,__LINE__, pName);
if (pthread_mutex_lock(&uci_get_mutex) != 0) //Add Lock
{
printf("%s(%d)lock error!\n");
}
cprintf("Get lock...%s,%d.......pName:%s\n",__FUNCTION__,__LINE__,pName);
int option_exist_ret = check_uci_option_exist(pName);
cprintf("!!!!!!!!!!!option_exist_ret:%d.......pName:%s\n",option_exist_ret,pName);
//cprintf("111111111111111111111111111111111111111\n");
if(option_exist_ret == 0)//find uci option exist
{
#ifdef PLATFORM_RALINK
sprintf(szCmd,"nvram_get 2860 %s", pName);
memset(pBuf, 0, nBufLen);
if( (fp = popen(szCmd, "r")) == NULL )
{
return FALSE;
}
if(fgets(pBuf, nBufLen, fp) != NULL)
{
getlen = strlen(pBuf);
bRet = TRUE;
for(i = getlen ; i > 0; i--)
{
if(pBuf[i] == '\r' || pBuf[i] == '\n')
{
pBuf[i] = '\0';
}
}
}
pclose(fp);
#else
#ifdef TEST_IN_WINDOWS
sprintf(szCmd,"uci -c ../overlay/nvm_defaults/etc/config get %s", pName);
#else
sprintf(szCmd,"uci get %s", pName);
#endif
if( (fp = popen(szCmd, "r")) == NULL )
{
return FALSE;
}
if(fgets(pBuf, nBufLen, fp) != NULL)
{
if (strstr(pBuf,"Entry not found") != NULL)
{
memset(pBuf, 0, nBufLen);
}
else
{
bRet = TRUE;
getlen = strlen(pBuf);
for (i = getlen - 1; i > 0; i--)
{
if ((pBuf[i] != '\r') && (pBuf[i] != '\n'))
break;
pBuf[i] = '\0';
}
}
}
pclose(fp);
#endif
}
else
{
cprintf("Not find in the uci...%s,%d.......pName:%s\n",__FUNCTION__,__LINE__,pName);
memset(pBuf, 0, nBufLen);
cprintf("Not find in the uci...%s,%d.......pBuf:%s\n",__FUNCTION__,__LINE__,pBuf);
}
if (pthread_mutex_unlock(&uci_get_mutex) != 0) //UnLock
{
printf("%s(%d)UnLock error!\n");
}
cprintf("Release lock...%s,%d.......pName:%s\n\n\n",__FUNCTION__,__LINE__,pName);
return bRet;
}
int CommitNVRamData(char* pCommFile)
{
char szCmd[BUF_SIZE_1024] = {0};
if (pCommFile == NULL)
{
return FALSE;
}
if (pthread_mutex_lock(&uci_commit_mutex) != 0) //Add Lock
{
printf("%s(%d)lock error!\n");
}
memset(szCmd, 0, BUF_SIZE_1024);
sprintf(szCmd,"uci commit %s", pCommFile);
system(szCmd);
system("/bin/sync");
if (pthread_mutex_unlock(&uci_commit_mutex) != 0) //UnLock
{
printf("%s(%d)UnLock error!\n");
}
return TRUE;
}
int SetNVRamData(char* pName, int bCommit, char* pCommFile)
{
char szCmd[BUF_SIZE_1024] = {0};
if ((pName == NULL) || ((bCommit) && (pCommFile == NULL)))
return FALSE;
if (strlen(pName) > (BUF_SIZE_1024 - 24))
return FALSE;
cprintf("\n\nSET wait lock...%s,%d.......pName:%s\n",__FUNCTION__,__LINE__,pName);
if (pthread_mutex_lock(&uci_set_mutex) != 0) //Add Lock
{
printf("%s(%d)lock error!\n");
}
cprintf("SET get lock...%s,%d..........pName:%s\n",__FUNCTION__,__LINE__,pName);
#ifdef TEST_IN_WINDOWS
sprintf(szCmd,"uci -c ../overlay/nvm_defaults/etc/config set %s", pName);
#else
sprintf(szCmd,"uci set %s", pName);
#endif
system(szCmd);
memset(szCmd, 0, BUF_SIZE_1024);
sprintf(szCmd,"uci commit %s", pCommFile);
system(szCmd);
system("/bin/sync");
if (pthread_mutex_unlock(&uci_set_mutex) != 0) //UnLock
{
printf("%s(%d)UnLock error!\n");
}
cprintf("SET release lock...%s,%d......pName:%s\n\n",__FUNCTION__,__LINE__,pName);
return TRUE;
}
int GetCMStatusData(char *getValue, char *output, int size)
{
FILE *fp= NULL;
char cmd_buf[512] = {0};
if(strstr(getValue,"PINStatus") || strstr(getValue,"PLMN"))
{
sprintf(cmd_buf, "grep -r '%s' /tmp/cm_status | awk -F ':' '{print $2}'" , getValue);
}
else
{
sprintf(cmd_buf, "grep -r '%s' /tmp/cm_status | awk -F ':' '{print $2}' |cut -d\" \" -f2" , getValue);
}
fp = popen(cmd_buf, "r");
if (fp)
{
if(fgets(output, size, fp) != NULL)
{
if(strstr(output,"UnKnown")!=NULL)
{
output[2]='k';
}
if(output[strlen(output)-1] == '\n')
output[strlen(output)-1] = '\0';
}
pclose(fp);
}
return 0;
}
void openUtil_Reboot(void)
{
system("/bin/sync");
sleep(1);
system("/bin/sync");
sleep(1);
system("reboot -f");
}
void openUtil_FactoryReset(void)
{
system("jffs2reset -y");
system("/bin/sync");
sleep(1);
system("/bin/sync");
sleep(1);
system("reboot -f");
}
Makefile
EXEC = libstring.so
SRC = libstring.c
CFLAGS += -fPIC
LDFLAGS += -shared
all: $(EXEC)
$(EXEC): $(SRC)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC) $(LDLIBS)
clean:
-rm -f $(EXEC) *.elf *.gdb *.o *.so