Monday, May 09, 2016

Using POSIX time related functions shows PTP time

One major different: The PTP time counts from 1990. POSIX function count from 1970.

The POSIX struct tm is
int tm_year
int tm_month
int tm_day
int tm_min
int tm_sec

Because I do not find function, I create the easy one to fill tm_xxx below.

/*
Copyright (C) 2016 YKLin

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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct _TIMESTAMP_S{
    uint16        epoch_number;
    uint32        seconds;
    uint32        nanoseconds;
} timestamp_t;
void _converttime(struct tm *mytime, uint64 epoch){
    int years=0, months=0,days=0,hours=0,minutes=0;
    if(!mytime){
        return;
    }
    if(epoch >= 31556926UL){
        years = epoch/31556926UL;
        epoch -= years*31556926UL;
    }
    if(epoch >= 2629743UL){
        months = epoch/2629743UL;
        epoch -= months*2629743UL;
    }
    if(epoch >= 86400UL){
        days = epoch/86400UL;
        epoch -= days*86400UL;
    }
    if(epoch >= 3600UL){
        hours = epoch/3600UL;
        epoch -= hours*3600UL;
    }
    if(epoch >= 60){
        minutes = epoch/60;
        epoch -= minutes*60;
    }
    mytime->tm_sec = (int)epoch;
    mytime->tm_year += years;
    mytime->tm_mon += months;
    mytime->tm_mday += days;
    mytime->tm_hour += hours;
    mytime->tm_min += minutes;
}
int _convertTimestapTotm(struct tm *mytime, timestamp_t *ts){
    if(!mytime || !ts){
        return FAILED;
    }
   _converttime(mytime, (uint64)ts->epoch_number<<32);
   _converttime(mytime, (uint64)ts->seconds);
    _converttime(mytime, (uint64)0x83aa7e80);
    return SUCCESS;
}

Thursday, April 28, 2016

Modify binary file

It's been a while not writing anything because the my career changing to verify IC.
However, there is the little python script for modifying binary file byte by byte. The following code opens a file and seek to offset 0xAFF7 at the beginning. It writes 0xCC, 0xE2, 0x54, 0x7a into file.

import struct
magic = (0xCCE2, 0x547A)
with open("binary.bin", "r+b") as f:
    f.seek(0xAFF7)
    for m in magic:
        f.write(struct.pack('h', m))
    f.close()


Wednesday, December 05, 2012

Wireshark filter packet contains

Record a function of wireshark: Filter contains of a packet
Syntax:
[Protocol] contains [hex|"STRING"]

For example:
1. Filter packet which has hex 0xea in contains:
tcp contains ea

2. Filter packet which has string "test" in contains:
http contains "test"

Thursday, March 01, 2012

No needed to enter a user name at windows 7 while booting up

I have an experiment about sleep/hibernate test at windows 7. Once I wake up or resume, win 7 always asks me to choose an account and password.

After I google, there is a GUI page to skip login at win7. Please run this command “ netplwiz”

As below figure, all you have to do is cancel “Users must enter a user name and password to use this computer”

Tuesday, December 20, 2011

Rename files at linux

I recall "mv" while my colleague ask me how to rename lots of files.
Here is the question:
There are many files. How do we modify its extended name? For example, a.log, b.log, c.log...etc.
We need to change extended from "log" to "txt".

The below answer is incorrect:
mv *.log *.txt

"rename" is the correct command for this time. Here is man page of rename
NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified
       as the first argument.  The perlexpr argument is a Perl expression
       which is expected to modify the $_ string in Perl for at least some of
       the filenames specified.  If a given filename is not modified by the
       expression, it will not be renamed.  If no filenames are given on the
       command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the
       extension, you might say

               rename 's/\.bak$//' *.bak

       To translate uppercase names to lower, you'd use

               rename 'y/A-Z/a-z/' *


For my case, I can use following command:
 rename 's/\.log$/\.txt/' *.log


Wednesday, May 11, 2011

printk within uptime

All you have to do:
Turn on the CONFIG_PRINTK_TIME kernel option

Wala~You get it!

Sunday, January 09, 2011

select function does not stop

I use named FIFO and select function to do IPC programming. When program execute to "if(select(myfd+d, myfdSet, NULL, NULL, &waittime) > 0 && IS_SET(myfdSet))", it always fulfill condition even there is no any process wrote into named FIFO.
After I examin again, I find out that I open file as "O_RDONLY". Refer to Linux's man page, there is a section to discript it:


Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks.

Hence, I modify open statement from myfd = open("/tmp/a", O_RDONLY) to myfd = open("/tmp/a", O_RDWR|O_NONBLOCK). Everything is perfect right.