📄 pot.c
字号:
/* * mtd - simple memory technology device manipulation tool * * Copyright (C) 2005 Waldemar Brodkorb <wbx@dass-it.de>, * Felix Fietkau <nbd@openwrt.org> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: pot.c,v 1.1.2.2 2007/01/08 06:15:03 agui Exp $ * * The code is based on the linux-mtd examples. */#include <limits.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <stdint.h>#include <fcntl.h>#include <errno.h>#include <error.h>#include <time.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/param.h>#include <sys/mount.h>#include <sys/stat.h>#include <sys/reboot.h>#include <string.h>#include <linux/mtd/mtd.h>#include "pot.h"#if 0#define POT_MTD "/dev/mtd/4"#define POT_MTD_RO "/dev/mtd/4ro"#define NULL_FD "null_fd"#define LINE_LEN 20#define POT_LEN 540 /*byte */#define POT_MAX_VALUE 4320 /*4320m */#define POT_RESOLUTION 1 /*60s*/#define POT_REPACKTHRESHOLD 30 /*30m */#define u_char unsigned char#define u_int unsigned int#define RIGHT_ONE_BIT(x) (u_char)(x >> 1)#endifu_int pot_value = 0;u_int char_len = 0;u_int detect_char_bit(u_char value);int first_char_not_zero();int init();int main_loop();void finish();int devfd;int null_fd;u_char byte_value;int main(){ int res;re_init: res = init(); if(res == -1){ printf("POT init() fail, do it again .\n"); goto re_init; } if(res == 1){ printf("Jump to finish . \n"); goto finish_tag; } main_loop();finish_tag: finish(); return 0;}int init(){ u_int byte_len; pot_value = 0; char_len = 0; byte_value = 0xFF; devfd = open(POT_MTD,O_RDWR); if(devfd <= 0){ goto mtd_erro; } char_len = jump_to_first_not_zero(); if(char_len >= POT_LEN) goto pot_max; byte_len =detect_char_bit(byte_value); pot_value = (char_len * 8) + 8 - byte_len; null_fd = open(NULL_FD, O_RDONLY); if(devfd <= 0){ goto mtd_erro; }exit: printf("pot dev: devfd = %d null dev: null_fd = %d\n pot_value = %d minute(s)\n", devfd, null_fd, pot_value); return 0;mtd_erro: printf("open mtd POT Error \n"); return -1;null_fd_error: close(devfd); return -1;pot_max: printf("POT length reach Threshold %d byte\n",POT_LEN); return 1;}void finish(){ close(devfd); close(null_fd); //while(1); // didn't exit}int main_loop(){ struct timeval timeout; //timeout.tv_sec = POT_RESOLUTION *60; timeout.tv_sec = POT_RESOLUTION *60; timeout.tv_usec = 1; while(1){ select(null_fd +1,NULL,NULL,NULL,&timeout);// wait 30s timeout.tv_sec = POT_RESOLUTION *60; timeout.tv_usec = 1; read(devfd, &byte_value,1); //read one byte lseek(devfd,-1,SEEK_CUR); //return to the byte byte_value = RIGHT_ONE_BIT(byte_value); write(devfd,&byte_value,1); if(byte_value == 0x00){ char_len ++; pot_value = char_len * 8; if(char_len >= POT_LEN) goto exit; }else{ lseek(devfd,-1,SEEK_CUR); } }exit: printf("POT length reach Threshold %d byte\n",POT_LEN); return 0;}int jump_to_first_not_zero() //return the num of byte, not 0x00 at the head, and read the first byte which isn't ZERO { int res = -1; u_char value; do{ res++; read(devfd, &value,1); }while(value == 0x00); lseek(devfd,-1,SEEK_CUR); //point to first byte which isn't 0x00 byte_value = value; exit: printf("jump_to_first_not_zero return %d read value = %02X\n",res, byte_value); return res;}u_int detect_char_bit(u_char value){ u_int res = 0; if(value == 0x00){ res = 0; goto exit; } if(value == 0xFF){ res = 8; goto exit; } do{ res ++; value = value >> 1; }while(value);exit: return res;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -