我和我追逐的梦

似曾相恋转眼过往已云烟 惋惜多情已经从前

Friday
Jan 16,2009

有时在跨架构porting的时候,如X86 To ARM, 内存的对齐问题并非如我们所愿,所以在用Gcc compiler的时候,要尽量使用__attribute__ ((__packed__)),比如:

struct ib_net_radio_t {
unsigned char word[2];
unsigned char packet;
unsigned char check;
unsigned char proto[2];
unsigned char payload[0];
}__attribute__ ((__packed__));

在ARM下用Gcc 2.9编译,如果不加上__attribute__ ((__packed__)),sizeof(struct ib_net_radio_t) == 8,加上之后,就是6. 在转packet的format的时候,这个得分外注意。

Tags: ,
Friday
Dec 12,2008

很多时候在调试code的时候,需要把内存数据同时以16进制和ASCII形式列印出来,比如网络封包、结构体的内容、buf内容等等,(当然IDE用户可以跳过忽视),所以写一个dump data的function来用。
原型为:
void dump_data(char* datastartp, const size_t maxlen);
其中,datastartp可以由其他类型的指针转换过来,比如一个struct,转换之后只是让他可以按字节流输出其内容。
源码:

下载: dump_data.c
  1. void
  2. dump_data(char* datastartp, const size_t maxlen)
  3. {
  4.     char line[(( (7+1+16*3+2+16+2) +3)/4)*4];
  5.     char* linep;
  6.     const unsigned char* datap;
  7.     size_t len, i, inc=0;
  8.  
  9.     len = maxlen;
  10.     datap = datastartp;
  11.  
  12.     while (len > 0)
  13.     {
  14.         memset(line, ' ', sizeof(line));
  15.         sprintf(line, "%07x:", inc);
  16.         inc++;
  17.         /* write hex dump */
  18.         linep = &line[7+1];
  19.         for (i = 0; i < 16; i++)
  20.         {
  21.             if (i >= len) 
  22.                 break;
  23.             sprintf(linep, " %02x", datap[i]);
  24.             linep += 3;
  25.         }
  26.         *linep = ' ';   /* remove '\0' */
  27.         /* write string dump */
  28.         linep = &line[7+1+16*3+2];
  29.         for (i = 0; i < 16; i++)
  30.         {
  31.             if (i >= len)  break;
  32.             if (isprint((char)datap[i])) 
  33.                 *linep++ = (char)datap[i];
  34.             else
  35.                 *linep++ = '.';
  36.         }
  37.         /* write end-of-line stuff */
  38.         line[7+1+16*3+2+16+0] = '\n';
  39.         line[7+1+16*3+2+16+1] = '\0';
  40.         printf(line)/* print to stdout */
  41.  
  42.         datap += 16;
  43.         if (len >= 16) 
  44.             len -= 16;
  45.         else
  46.             len  = 0;
  47.     }
  48. }

如何调用:

  1. struct ib_net_radio_t {
  2.     unsigned char word[2];
  3.     unsigned char packet;
  4.     unsigned char check;
  5.     unsigned char proto[2];
  6.     unsigned char payload[0];
  7. };
  8.  
  9. char* output;
  10.  
  11. output = (char*)radio//可以使用任意类型,只要将其强制转换到char*即可;
  12. printf("Dump Data:\n");
  13. dump_data(output, total_len);

输出结果如下:

  1. Dump Data:
  2. 0000000: 08 22 01 dd 08 06 00 01 08 00 06 04 01 fb 00 c0  ."...........??
  3. 0000001: ee 0c 11 fe c0 a8 fa 0a 00 00 00 00 00 00 00 00  ....括..........
  4. 0000002: 00 00                                            ..
Tags: ,
Thursday
Sep 18,2008

主机字节序和网络字节序的转换只要更改p[]的顺序即可。

  1. unsinged long int ipstring2long(const char *ipstr)
  2. {
  3.     int  ip[4];
  4.     int  matches;
  5.     unsinged long int tmp;
  6.     unsigned char   *p = (unsigned char*)&tmp;
  7.  
  8.     matches = sscanf(ipstr, "%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]);
  9.     if ( matches != 4 )
  10.     {
  11.         return (unsigned long int) - 1;
  12.     }
  13.     else 
  14.     {
  15.         p[3] = ip[0];
  16.         p[2] = ip[1];
  17.         p[1] = ip[2];
  18.         p[0] = ip[3];
  19.         return tmp;
  20.     }
  21.  
  22. }
Tags: ,

Calendar

    July 2010
    M T W T F S S
    « May    
     1234
    567891011
    12131415161718
    19202122232425
    262728293031  

About Me

    ID:Matrix Chou
    QQ:64502411
    Skype: choukuangjay
    Twitter: andylog
    Writing USB 3G Card Driver For Router/ADSL


Recent Comments



Blog联播



Visitors From

free counters


Subscription