1. 创建 monitor_fw_pthread.c
用于检测:
a. 是否已插入U盘
b. 是否发现新的固件
"NEW_FW_MAA"
c. 复制固件到指定的目录下
"/usr/myinst/"
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <stdbool.h>
-
//#include <io.h>
-
-
#define WIN_FIRMWARE_NAME "NEW_FW_MAA"
-
#define WIN_TARGET_PATH "/usr/myinst/"
-
-
static char g_fwPath[255];
-
-
-
static int execmd(char* cmd,char* result)
-
{
-
char buffer[128]; //定义缓冲区
-
FILE* pipe = popen(cmd, "r"); //打开管道,并执行命令
-
if (!pipe)
-
return 0; //返回0表示运行失败
-
-
while(!feof(pipe)) {
-
if(fgets(buffer, 128, pipe)){ //将管道输出到result中
-
strcat(result,buffer);
-
}
-
}
-
-
pclose(pipe); //关闭管道
-
return 1; //返回1表示运行成功
-
}
-
-
-
-
// is removed udisk ?
-
bool remove_udisk()
-
{
-
bool ret = false;
-
char cmd[255], result[1024];
-
-
memset(cmd, 0x00, sizeof(cmd));
-
memset(result, 0x00, sizeof(result));
-
sprintf(cmd, "%s", "mount | grep /media/root");
-
execmd(cmd, result);
-
if(strlen(result) > 10)
-
{
-
printf("Info: UDisk until alive. \n");
-
}
-
else
-
{
-
printf("Info: UDisk was removed. \n");
-
ret = true;
-
}
-
return ret;
-
}
-
-
bool found_udisk()
-
{
-
bool ret = false;
-
char cmd[255], result[1024];
-
char *p;
-
int i = 0;
-
memset(cmd, 0x00, sizeof(cmd));
-
memset(result, 0x00, sizeof(result));
-
-
sprintf(cmd, "%s", "mount | grep /media/root");
-
execmd(cmd, result);
-
-
p = strstr(result, "/media/root/");
-
if(p)
-
{
-
printf("Result: %s \n", result);
-
printf("Info: Found an udisk insert. \n");
-
memset(g_fwPath, 0x00, sizeof(g_fwPath));
-
-
while(*p != 0x20)
-
{
-
g_fwPath[i] = *p;
-
p++;
-
i++;
-
}
-
printf("Fw path: %s \n", g_fwPath);
-
-
ret = true;
-
}
-
-
return ret;
-
}
-
-
bool found_firmware()
-
{
-
bool ret = false;
-
char fileName[255];
-
memset(fileName, 0x00, sizeof(fileName));
-
sprintf(fileName, "%s/%s", g_fwPath, WIN_FIRMWARE_NAME);
-
if(access(fileName, F_OK) == 0)
-
{
-
ret = true;
-
printf("^_^ Found the new firmware. \n");
-
}
-
else
-
{
-
printf("^?^ Not found the new firmware. \n");
-
}
-
return ret;
-
}
-
-
void copy_firmware()
-
{
-
char fileName[255], cmd[255];
-
memset(fileName, 0x00, sizeof(fileName));
-
memset(cmd, 0x00, sizeof(cmd));
-
sprintf(fileName, "%s/%s", g_fwPath, WIN_FIRMWARE_NAME);
-
sprintf(cmd, "cp %s %s/MAAV", fileName, WIN_TARGET_PATH);
-
system(cmd);
-
printf("copy firmware: %s to %s done. \n", fileName, WIN_TARGET_PATH);
-
}
-
-
-
int main(int argc, char* argv[])
-
{
-
bool isIn = false;
-
bool isRemove = false;
-
bool isNewFw = false;
-
memset(g_fwPath, 0x00, sizeof(g_fwPath));
-
-
while(1)
-
{
-
if(isIn)
-
{
-
isRemove = remove_udisk();
-
if(isRemove)
-
{
-
isRemove = false;
-
isIn = false;
-
}
-
}
-
else
-
{
-
isIn = found_udisk();
-
if(isIn)
-
{
-
sleep(1);
-
isNewFw = found_firmware();
-
if(isNewFw)
-
{
-
sleep(1);
-
copy_firmware();
-
}
-
}
-
}
-
-
sleep(2);
-
}
-
-
}
2. 在 /root/.profile 下加入
/usr/lginst/monitor_fw_pthread &
阅读(5177) | 评论(0) | 转发(1) |