| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /******************************************************************************
- 版权所有:
- 文件名称: rt_env.c
- 文件版本: 01.01
- 创建作者: zhaoyang
- 创建日期: 2023-12-17
- 功能说明:
- 其它说明:
- 修改记录:
- */
- /*------------------------------- 头文件 --------------------------------------
- */
- #include "rt.h"
- /*------------------------------- 宏定义 --------------------------------------
- */
- #define SETENV_PROG "/usr/sbin/fw_setenv"
- /*------------------------------ 类型结构 -------------------------------------
- */
- struct env_file g_env_file[] =
- {
- {"ethact=", NULL},
- {"ipaddr=", NULL},
- {"netmask=", NULL},
- {"gatewayip=", NULL},
- {"ver_u_boot=", NULL},
- {"ver_kernel=", NULL},
- {"ver_root_fs=",NULL},
- {"auth=", NULL},
- {"mt_port_on=", NULL},
- {"pid=", NULL},
- {"net1mac=", NULL},
- {"net2mac=", NULL},
- {"net3mac=", NULL},
- };
- #define ENV_FILE_NUM ((int)(sizeof(g_env_file)/sizeof(struct env_file)))
- /*------------------------------ 全局变量 -------------------------------------
- */
- u8 *g_file_buf;
- /*------------------------------ 函数声明 -------------------------------------
- */
- /*------------------------------ 外部函数 -------------------------------------
- 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
- */
- int env_init(void)
- {
- int i,j,file_length, name_len,buf_len,row_len,file_offset;
- int pfile;
- off_t pos;
- //return 0; //版本文件设置为默认值
- // 打开文件
- // pfile = rt_file_open("/tmp/env.dat",O_CREAT|O_RDONLY ,0);
- pfile = rt_file_open("/tmp/env.dat",O_RDONLY ,0);
- if(IS_ERR(pfile))
- {
- rt_printf("env data open err!\r\n");
- return -1;
- }
- // 得到文件长度
- file_length = rt_file_getfile_size(pfile);
- if(file_length <= 0)
- {
- rt_file_close(pfile,0);
- return -2;
- }
- // 分配内存
- //printf("file_length = %d\n", file_length);
- g_file_buf = (u8 *)rt_malloc(file_length);
- if(g_file_buf == NULL)
- {
- rt_file_close(pfile,0);
- return -3;
- }
- pos = 0;
- if(rt_file_read(pfile,(char *)g_file_buf,file_length,&pos) != file_length)
- {
- rt_file_close(pfile,0);
- rt_free(g_file_buf);
- return -4;
- }
- // 关闭文件
- rt_file_close(pfile,0);
-
- file_offset = 0;
- row_len = 0;
- name_len = 0;
- for(i=0;i<ENV_FILE_NUM;i++)
- {
- //找到\r\n位置,得到一行长度
- for(j = file_offset; j < file_length; j++)
- {
- if(g_file_buf[j] == 0x0a)
- {
- row_len = j+1;
- break;
- }
- }
-
- name_len = strlen(g_env_file[i].name);
- buf_len = row_len - file_offset - name_len - 1;
-
-
- if(strncmp(g_env_file[i].name, (char *)g_file_buf+file_offset, name_len) == 0)
- {
- g_env_file[i].buf = (s8 *)(g_file_buf+file_offset+name_len);
-
- *(g_env_file[i].buf + buf_len) = 0;
- }
- file_offset = row_len;
- }
- return 0;
- }
- int env_exit(void)
- {
- if(g_file_buf)
- {
- rt_free(g_file_buf);
- g_file_buf = 0;
- }
- return 0;
- }
- char * env_get_info(int no)
- {
- if(no >= ENV_FILE_NUM)
- {
- return NULL;
- }
- else
- {
- return (char *)g_env_file[no].buf;
- }
- }
- int env_setenv(char *name,char *value)
- {
- return 0;
- /*
- int ret;
- char argv[256] = {0x00};
-
- ret = access(SETENV_PROG, X_OK);
- if(!ret)
- {
- sprintf(argv, "%s %s %s", SETENV_PROG, name, value);
- ret = system(argv);
- }
-
- return ret;
- */
- }
- int env_restart(void)
- {
- return 0;
- /*
- int ret;
-
- ret = access("/app/m", X_OK);
- if(!ret)
- {
- ret = system("/app/m");
- }
- rt_printf("env_restart:ret=%d", ret);
- return ret;
- */
- }
- /*------------------------------ 文件结束 -------------------------------------
- */
|