Пример #1
0
/*****************************************************************************
 函 数 名  : power_on_log_save
 功能描述  : 保存开机log
 输入参数  :
 输出参数  : 无
 返 回 值  :
 调用函数  :
 被调函数  :
*****************************************************************************/
static int power_on_log_save( void )
{
    long pos;
    unsigned int pf;
    mm_segment_t old_fs;
    ssize_t rt = 0;
    char buf[128] = {0};
    struct rtc_time tm;
    char *power_on_reason[4];
#if (MBB_CHARGE == FEATURE_ON)
    char *power_on_mode[5];
#else
    char *power_on_mode[3];
#endif
    BATT_LEVEL_ENUM  battery_level;

    tm = power_item_info.time;
    battery_level = chg_get_batt_level();


    power_on_reason[0] = "Charger";
    power_on_reason[1] = "Power Key";
    power_on_reason[2] = "Warm Reset";
    power_on_reason[3] = "Unknown";
#if (MBB_CHG_PLATFORM_V7R2 == FEATURE_ON)
    power_on_mode[0] = "EXCEPTION";
    power_on_mode[1] = "NORMAL";
    power_on_mode[2] = "PWN CHARGING";
	power_on_mode[3] = "UPDATE";
    power_on_mode[4] = "INVALID";
#else
    power_on_mode[0] = "PWN CHARGING";
    power_on_mode[1] = "NORMAL";
    power_on_mode[2] = "UPDATE";
#endif
    /* 记录开机信息(时间、次数、关机原因) */
    snprintf(buf, sizeof(buf) - 1, "power on reason(E5): %s, power on mode : %s, current battery voltage: %d, current time: %4d-%02d-%02d %02d:%02d:%02d\r\n", \
            power_on_reason[power_item_info.reason], power_on_mode[power_item_info.mode], battery_level, tm.tm_year, tm.tm_mon, \
            tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    #if ( FEATURE_ON == MBB_MLOG )
    mlog_print(MLOG_POWER, mlog_lv_info, "POWERON mode %s\n" ,power_on_mode[power_item_info.mode]);
    mlog_print(MLOG_POWER, mlog_lv_info, "POWERON due to %s\n" ,power_on_reason[power_item_info.reason]);
    mlog_set_statis_info("on_times",1); 
    #endif

    old_fs = get_fs(); //lint !e63

    set_fs(KERNEL_DS);

    /*coverity[check_return] */
    pf = (unsigned int)sys_open(EXCH_POWER_LOG_PATH, O_RDWR | O_CREAT, 0666);
    /*coverity[unchecked_value] */
    if(IS_ERR((const void*)pf))
    {
        pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_POWER_LOG_PATH);
        return (int)pf;
    }

    /*coverity[unchecked_value] */
    pos = sys_lseek(pf, 0, SEEK_END);
    if(pos > EXCH_ONOFF_LOG_MAX){
        /* 文件超过 16k,删除重新打开 */
        sys_rmdir(EXCH_POWER_LOG_PATH);
        /*coverity[check_return] */
        pf = (unsigned int)sys_open(EXCH_POWER_LOG_PATH, O_RDWR | O_CREAT, 0666);
        /*coverity[unchecked_value] */
        if(IS_ERR((const void*)pf))
        {
            pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_POWER_LOG_PATH);
            return (int)pf;
        }
    }
    else{
        /*coverity[unchecked_value] */
        sys_lseek(pf, pos, SEEK_SET);
    }

    /*coverity[unchecked_value] */
    rt = sys_write(pf, (const char*)buf, strlen(buf));
    if(rt<0)
    {
        pr_dbg("error occured happened when write file %s, exiting.\n", EXCH_POWER_LOG_PATH);
        /*coverity[unchecked_value] */
        sys_close(pf);
        set_fs(old_fs);
        return (int)rt;
    }

    /*coverity[unchecked_value] */
    sys_close(pf);
    set_fs(old_fs);

    pr_dbg(KERN_DEBUG "power on log save.\n ");

    return (int)rt;
}
/******************************************************************************
*  Function:  power_off_log_save
*  Description: save the power off log( reason and battery voltage ).
*  Input:
*         None
*  Output:
*         None
*  Return:
*         None
*  Note  :
********************************************************************************/
LOCAL_1 int power_off_log_save( void )
{
    long pos;
    unsigned int pf;
    mm_segment_t old_fs;
    struct rtc_time tm;
    struct timespec ts;
    int     rt;
    char    buf[128];
    char    *reboot_reason[] = {"NORMAL", "BAD BATTERY", "LOWBATTERY", "OVERTEMP", \
                                    "RM_CHARGER", "UPDATE", "REBOOT", "INVALID"};

    BATT_LEVEL_ENUM                 battery_level = chg_get_batt_level();
    DRV_SHUTDOWN_REASON_ENUM        rb;

    getnstimeofday(&ts);

    rtc_time_to_tm((unsigned long)ts.tv_sec, &tm);

    power_off_ctrl.time = tm;
    rb = power_off_ctrl.reason;

    pr_dbg("%4d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year, tm.tm_mon, \
        tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    /* 记录关机信息(时间、次数、关机原因) */
    snprintf(buf, sizeof(buf) - 1, "system close reason(E5): %s, current battery voltage: %d, current time: %4d-%02d-%02d %02d:%02d:%02d\n", \
        reboot_reason[rb], battery_level, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    old_fs = get_fs(); //lint !e63

    set_fs(KERNEL_DS);

    /*coverity[check_return] */
    pf = (unsigned int)sys_open(EXCH_RESET_LOG_PATH, O_RDWR | O_CREAT, 0666);
    /*coverity[unchecked_value] */
    if(IS_ERR((const void*)pf))
    {
        pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_RESET_LOG_PATH);
        return (int)pf;
    }

    /*coverity[unchecked_value] */
    pos = sys_lseek(pf, 0, SEEK_END);
    if(pos > EXCH_ONOFF_LOG_MAX){
        /* 文件超过 16k,删除重新打开 */
        sys_rmdir(EXCH_RESET_LOG_PATH);
        /*coverity[check_return] */
        pf = (unsigned int)sys_open(EXCH_RESET_LOG_PATH, O_RDWR | O_CREAT, 0666);
        /*coverity[unchecked_value] */
        if(IS_ERR((const void*)pf))
        {
            pr_dbg( "error occured happened when open file %s, exiting.\n", EXCH_RESET_LOG_PATH);
            return (int)pf;
        }
    }
    else{
        /*coverity[unchecked_value] */
        sys_lseek(pf, pos, SEEK_SET);
    }

    /*coverity[unchecked_value] */
    rt = sys_write(pf, (const char*)buf, strlen(buf));
    if(rt<0)
    {
        pr_dbg("error occured happened when write file %s, exiting.\n", EXCH_RESET_LOG_PATH);
        /*coverity[unchecked_value] */
        sys_close( pf );
        set_fs(old_fs);
        return rt;
    }

    pr_dbg(KERN_DEBUG "power off log save.");

    /*coverity[unchecked_value] */
    sys_close( pf );
    set_fs(old_fs);

    return rt;
}