当前位置:   article > 正文

嵌入式linux开发实用工具小程序_海思linux嵌入开发

海思linux嵌入开发

Table of Contents

(一)十六进制字符转整型数字

(二)字符串转整型

(三)创建文件并填充固定数据

(四)批量处理图片

(五)海思HI3520 IO控制小程序

(六)路由追踪

(七)文件固定位置插入数据

(七)H264 I帧与P帧偏移

(八)获取本地IP地址


    在学习和工作开发的时候,经常需要使用到各种各样不太常用的操作,这种情况一般是自己手动写一些小程序来处理。因为它们不太常用,所以经常用了又没保存,等到下一次在使用的时候又需要重写,这样的非常浪费时间和精力。所以想在这里统一记录一下,以备下次重新使用。代码以实用为主,如果缺陷,欢迎指出。


(一)十六进制字符转整型数字

    功能:将16进制的字符串转换为10进制的数字。我是没有找到相应的库函数,所以参考网上的代码自己手动写了个函数来实现。常用的函数有atoi,atol,他们都是将10进制的数字字符串转换为int或是long类型,所以在有些情况下不适用。

  1. /*=============================================================================
  2. # FileName: hex2dec.cpp
  3. # Desc: Convert a hex string to a int number
  4. # Author: Caibiao Lee
  5. # Version:
  6. # LastChange: 2018-11-26
  7. # History:
  8. =============================================================================*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. int c2i(char ch)
  14. {
  15. // 如果是数字,则用数字的ASCII码减去48, 如果ch = '2' ,则 '2' - 48 = 2
  16. if(isdigit(ch))
  17. return ch - 48;
  18. // 如果是字母,但不是A~F,a~f则返回
  19. if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'z' )
  20. return -1;
  21. // 如果是大写字母,则用数字的ASCII码减去55, 如果ch = 'A' ,则 'A' - 55 = 10
  22. // 如果是小写字母,则用数字的ASCII码减去87, 如果ch = 'a' ,则 'a' - 87 = 10
  23. if(isalpha(ch))
  24. return isupper(ch) ? ch - 55 : ch - 87;
  25. return -1;
  26. }
  27. int hex2dec(char *hex)
  28. {
  29. int len;
  30. int num = 0;
  31. int temp;
  32. int bits;
  33. int i;
  34. char str[64] = {0};
  35. if(NULL==hex)
  36. {
  37. printf("input para error \n");
  38. return 0;
  39. }
  40. if(('0'==hex[0])&&(('X'==hex[1])||('x'==hex[1])))
  41. {
  42. strcpy(str,&hex[2]);
  43. }else
  44. {
  45. strcpy(str,hex);
  46. }
  47. printf("input num = %s \n",str);
  48. // 此例中 str = "1de" 长度为3, hex是main函数传递的
  49. len = strlen(str);
  50. for (i=0, temp=0; i<len; i++, temp=0)
  51. {
  52. // 第一次:i=0, *(str + i) = *(str + 0) = '1', 即temp = 1
  53. // 第二次:i=1, *(str + i) = *(str + 1) = 'd', 即temp = 13
  54. // 第三次:i=2, *(str + i) = *(str + 2) = 'd', 即temp = 14
  55. temp = c2i( *(str + i) );
  56. // 总共3位,一个16进制位用 4 bit保存
  57. // 第一次:'1'为最高位,所以temp左移 (len - i -1) * 4 = 2 * 4 = 8 位
  58. // 第二次:'d'为次高位,所以temp左移 (len - i -1) * 4 = 1 * 4 = 4 位
  59. // 第三次:'e'为最低位,所以temp左移 (len - i -1) * 4 = 0 * 4 = 0 位
  60. bits = (len - i - 1) * 4;
  61. temp = temp << bits;
  62. // 此处也可以用 num += temp;进行累加
  63. num = num | temp;
  64. }
  65. // 返回结果
  66. return num;
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. int l_s32Ret = 0;
  71. if(2!=argc)
  72. {
  73. printf("=====ERROR!======\n");
  74. printf("usage: %s Num \n", argv[0]);
  75. printf("eg 1: %s 0x400\n", argv[0]);
  76. return 0;
  77. }
  78. l_s32Ret = hex2dec(argv[1]);
  79. printf("value hex = 0x%x \n",l_s32Ret);
  80. printf("value dec = %d \n",l_s32Ret);
  81. return 0;
  82. }

运行结果: 

  1. biao@ubuntu:~/test/flash$ ./a.out 0x400
  2. input num = 400
  3. value hex = 0x400
  4. value dec = 1024
  5. biao@ubuntu:~/test/flash$

(二)字符串转整型

     功能:将正常输入的16进制或是10进制的字符串转换为int数据类型

  1. /*=============================================================================
  2. # FileName: hex2dec.cpp
  3. # Desc: Convert a hex/dec string to a int number
  4. # Author: Caibiao Lee
  5. # Version:
  6. # LastChange: 2018-12-03
  7. # History:
  8. =============================================================================*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. int String2int(char *strChar)
  14. {
  15. int len=0;
  16. const char *pstrCmp1="0123456789ABCDEF";
  17. const char *pstrCmp2="0123456789abcdef";
  18. char *pstr=NULL;
  19. int uiValue=0;
  20. int j=0;
  21. unsigned int t=0;
  22. int i=0;
  23. if(NULL==strChar)
  24. return -1;
  25. if(0>=(len=strlen((const char *)strChar)))
  26. return -1;
  27. if(NULL!=(pstr=strstr(strChar,"0x"))||NULL!=(pstr=strstr(strChar,"0X")))
  28. {
  29. pstr=(char *)strChar+2;
  30. if(0>=(len=strlen((const char *)pstr)))
  31. return -1;
  32. for(i=(len-1);i>=0;i--)
  33. {
  34. if(pstr[i]>'F')
  35. {
  36. for(t=0;t<strlen((const char *)pstrCmp2);t++)
  37. {
  38. if(pstrCmp2[t]==pstr[i])
  39. uiValue|=(t<<(j++*4));
  40. }
  41. }
  42. else
  43. {
  44. for(t=0;t<strlen((const char *)pstrCmp1);t++)
  45. {
  46. if(pstrCmp1[t]==pstr[i])
  47. uiValue|=(t<<(j++*4));
  48. }
  49. }
  50. }
  51. }
  52. else
  53. {
  54. uiValue=atoi((const char*)strChar);
  55. }
  56. return uiValue;
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. int l_s32Ret = 0;
  61. if(2!=argc)
  62. {
  63. printf("=====ERROR!======\n");
  64. printf("usage: %s Num \n", argv[0]);
  65. printf("eg 1: %s 0x400\n", argv[0]);
  66. return 0;
  67. }
  68. l_s32Ret = String2int(argv[1]);
  69. printf("value hex = 0x%x \n",l_s32Ret);
  70. printf("value dec = %d \n",l_s32Ret);
  71. return 0;
  72. }

(三)创建文件并填充固定数据

    功能:创建固定大小的一个文件,并且把这个文件填充为固定的数据。

  1. /*=============================================================================
  2. # FileName: CreateFile.cpp
  3. # Desc: 创建固定大小的文件,然后填充固定的数据
  4. # Author: Caibiao Lee
  5. # Version:
  6. # LastChange: 2018-11-26
  7. # History:
  8. =============================================================================*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. //#define FILL_DATA_VALUE 0xff
  14. #define FILL_DATA_VALUE 0x30 //char 0
  15. int c2i(char ch)
  16. {
  17. if(isdigit(ch))
  18. return ch - 48;
  19. if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'z' )
  20. return -1;
  21. if(isalpha(ch))
  22. return isupper(ch) ? ch - 55 : ch - 87;
  23. return -1;
  24. }
  25. int hex2dec(char *hex)
  26. {
  27. int len;
  28. int num = 0;
  29. int temp;
  30. int bits;
  31. int i;
  32. char str[64] = {0};
  33. if(NULL==hex)
  34. {
  35. printf("input para error \n");
  36. return 0;
  37. }
  38. if(('0'==hex[0])&&(('X'==hex[1])||('x'==hex[1])))
  39. {
  40. strcpy(str,&hex[2]);
  41. }else
  42. {
  43. strcpy(str,hex);
  44. }
  45. printf("input num = %s \n",str);
  46. len = strlen(str);
  47. for (i=0, temp=0; i<len; i++, temp=0)
  48. {
  49. temp = c2i( *(str + i) );
  50. bits = (len - i - 1) * 4;
  51. temp = temp << bits;
  52. num = num | temp;
  53. }
  54. return num;
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. FILE *l_pFile = NULL;
  59. int l_s32Rest = 0;
  60. unsigned int l_WriteLen = 0;
  61. unsigned int l_FileLen = 0;
  62. unsigned char TempData[1024] = {FILL_DATA_VALUE};
  63. if(3!=argc)
  64. {
  65. printf("usage: %s FileName FileLen \n ", argv[0]);
  66. printf("eg: %s ./Outfile.bin 0x400 \n ", argv[0]);
  67. return 0;
  68. };
  69. const char *l_pFileName = argv[1];
  70. if(NULL==l_pFileName)
  71. {
  72. printf("input file name is NULL \n");
  73. return -1;
  74. }
  75. if(('0'==argv[2][0])&&(('X'==argv[2][1])||('x'==argv[2][1])))
  76. {
  77. l_FileLen = hex2dec(argv[2]);
  78. }else
  79. {
  80. l_FileLen = atoi(argv[2]);
  81. }
  82. printf("Need To Write Data Len %d \n",l_FileLen);
  83. printf("Fill Data Vale = 0x%x \n",FILL_DATA_VALUE);
  84. for(int i=0;i<1024;i++)
  85. {
  86. TempData[i] = FILL_DATA_VALUE;
  87. }
  88. l_pFile = fopen(l_pFileName,"w+");
  89. if(l_pFile==NULL)
  90. {
  91. printf("open file %s error \n",l_pFileName);
  92. return -1;
  93. }
  94. while(l_WriteLen<l_FileLen)
  95. {
  96. if(l_FileLen<1024)
  97. {
  98. l_s32Rest = fwrite(TempData,1,l_FileLen,l_pFile);
  99. }
  100. else
  101. {
  102. l_s32Rest = fwrite(TempData,1,1024,l_pFile);
  103. }
  104. if(l_s32Rest <= 0)
  105. {
  106. break;
  107. };
  108. l_WriteLen +=l_s32Rest;
  109. }
  110. if(NULL!=l_pFile)
  111. {
  112. fclose(l_pFile);
  113. l_pFile = NULL;
  114. }
  115. return 0;
  116. }

运行结果:

  1. biao@ubuntu:~/test/flash$ gcc CreateFile.cpp
  2. biao@ubuntu:~/test/flash$ ls
  3. a.out CreateFile.cpp hex2dec.cpp main.cpp out.bin
  4. biao@ubuntu:~/test/flash$ ./a.out ./out.bin 0x10
  5. input num = 10
  6. Need To Write Data Len 16
  7. Fill Data Vale = 0x30
  8. biao@ubuntu:~/test/flash$ ls
  9. a.out CreateFile.cpp hex2dec.cpp main.cpp out.bin
  10. biao@ubuntu:~/test/flash$ vim out.bin
  11. 1 0000000000000000

(四)批量处理图片

功能:批处理将图片前面固定的字节数删除。

  1. /*=============================================================================
  2. # FileName: CutFile.cpp
  3. # Desc: 批量处理,将图片的前面固定字节删除
  4. # Author: Caibiao Lee
  5. # Version:
  6. # LastChange: 2018-11-26
  7. # History:
  8. =============================================================================*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/stat.h>
  14. #define START_READ_POSITION 128
  15. #define PHOTO_START_TIME 83641
  16. //l_s32PhotoTime = 92809;
  17. int Cut_file(char * InputFile)
  18. {
  19. FILE *l_pFileInput = NULL;
  20. FILE *l_pFileOutput = NULL;
  21. char l_ars8OutputName[128] = {0};
  22. unsigned char l_arru8TempData[1024] = {0};
  23. int l_s32Ret = 0;
  24. static unsigned int ls_u32Num = 0;
  25. if(NULL== InputFile)
  26. {
  27. goto ERROR;
  28. }
  29. //sprintf(l_ars8OutputName,"./outfile/_%s",&InputFile[8]);
  30. sprintf(l_ars8OutputName,"./outfile/00%d.jpg",ls_u32Num++);
  31. //printf("out file name %s \n",l_ars8OutputName);
  32. l_pFileInput = fopen(InputFile,"rb+");
  33. if(NULL==l_pFileInput)
  34. {
  35. printf("input file open error\n");
  36. goto ERROR;
  37. }
  38. l_pFileOutput = fopen(l_ars8OutputName,"w+");
  39. if(NULL==l_pFileOutput)
  40. {
  41. printf("out file open error\n");
  42. goto ERROR;
  43. }
  44. fseek(l_pFileInput,START_READ_POSITION,SEEK_SET);
  45. while(!feof(l_pFileInput))
  46. {
  47. l_s32Ret = fread(l_arru8TempData,1,1024,l_pFileInput);
  48. if(l_s32Ret<0)
  49. {
  50. break;
  51. }
  52. l_s32Ret = fwrite(l_arru8TempData,1,l_s32Ret,l_pFileOutput);
  53. if(l_s32Ret<0)
  54. {
  55. break;
  56. }
  57. }
  58. ERROR:
  59. if(NULL!=l_pFileOutput)
  60. {
  61. fclose(l_pFileOutput);
  62. l_pFileOutput =NULL;
  63. };
  64. if(NULL !=l_pFileInput);
  65. {
  66. fclose(l_pFileInput);
  67. l_pFileInput =NULL;
  68. }
  69. }
  70. int main(void)
  71. {
  72. char l_arrs8InputName[128] = {0};
  73. char l_s8PhotoChannel = 0;
  74. int l_s32PhotoTime = 0;
  75. l_s8PhotoChannel = 3;
  76. l_s32PhotoTime = PHOTO_START_TIME;
  77. /**从第一通道开始**/
  78. for(int j=1;j<l_s8PhotoChannel;j++)
  79. {
  80. for(int i=l_s32PhotoTime;i<235959;i++)
  81. {
  82. memset(l_arrs8InputName,0,sizeof(l_arrs8InputName));
  83. sprintf(l_arrs8InputName,"./image/%dY%06d.jpg",j,i);
  84. if(0==access(l_arrs8InputName,F_OK))
  85. {
  86. printf("%s\n",l_arrs8InputName);
  87. Cut_file(l_arrs8InputName);
  88. }
  89. }
  90. }
  91. }

运行结果:

  1. biao@ubuntu:~/test/photo$ gcc CutFile.cpp
  2. biao@ubuntu:~/test/photo$ ls
  3. a.out CutFile.cpp image outfile
  4. biao@ubuntu:~/test/photo$ ./a.out
  5. ./image/1Y083642.jpg
  6. ./image/1Y083714.jpg
  7. ./image/1Y083747.jpg
  8. ./image/1Y083820.jpg
  9. ./image/1Y083853.jpg
  10. ./image/1Y083925.jpg
  11. ./image/1Y084157.jpg
  12. ./image/1Y084228.jpg
  13. ./image/1Y084301.jpg
  14. ./image/1Y084334.jpg
  15. ./image/1Y084406.jpg
  16. ./image/1Y084439.jpg
  17. ./image/1Y084711.jpg
  18. ./image/1Y084742.jpg
  19. ./image/1Y173524.jpg
  20. ./image/1Y173556.jpg
  21. ./image/1Y173629.jpg
  22. ./image/1Y173702.jpg
  23. ./image/1Y173933.jpg
  24. ./image/1Y174004.jpg
  25. ./image/1Y174244.jpg
  26. ./image/1Y174315.jpg
  27. ./image/1Y174348.jpg
  28. ./image/1Y174420.jpg
  29. ./image/1Y174454.jpg
  30. ./image/1Y174733.jpg
  31. biao@ubuntu:~/test/photo$ tree
  32. .
  33. ├── a.out
  34. ├── CutFile.cpp
  35. ├── image
  36. │   ├── 1Y083642.jpg
  37. │   ├── 1Y083714.jpg
  38. │   ├── 1Y083747.jpg
  39. │   ├── 1Y083820.jpg
  40. │   ├── 1Y083853.jpg
  41. │   ├── 1Y083925.jpg
  42. │   ├── 1Y084157.jpg
  43. │   ├── 1Y084228.jpg
  44. │   ├── 1Y084301.jpg
  45. │   ├── 1Y084334.jpg
  46. │   ├── 1Y084406.jpg
  47. │   ├── 1Y084439.jpg
  48. │   ├── 1Y084711.jpg
  49. │   ├── 1Y084742.jpg
  50. │   ├── 1Y173524.jpg
  51. │   ├── 1Y173556.jpg
  52. │   ├── 1Y173629.jpg
  53. │   ├── 1Y173702.jpg
  54. │   ├── 1Y173933.jpg
  55. │   ├── 1Y174004.jpg
  56. │   ├── 1Y174244.jpg
  57. │   ├── 1Y174315.jpg
  58. │   ├── 1Y174348.jpg
  59. │   ├── 1Y174420.jpg
  60. │   ├── 1Y174454.jpg
  61. │   └── 1Y174733.jpg
  62. └── outfile
  63. ├── 000.jpg
  64. ├── 0010.jpg
  65. ├── 0011.jpg
  66. ├── 0012.jpg
  67. ├── 0013.jpg
  68. ├── 0014.jpg
  69. ├── 0015.jpg
  70. ├── 0016.jpg
  71. ├── 0017.jpg
  72. ├── 0018.jpg
  73. ├── 0019.jpg
  74. ├── 001.jpg
  75. ├── 0020.jpg
  76. ├── 0021.jpg
  77. ├── 0022.jpg
  78. ├── 0023.jpg
  79. ├── 0024.jpg
  80. ├── 0025.jpg
  81. ├── 002.jpg
  82. ├── 003.jpg
  83. ├── 004.jpg
  84. ├── 005.jpg
  85. ├── 006.jpg
  86. ├── 007.jpg
  87. ├── 008.jpg
  88. └── 009.jpg
  89. 2 directories, 54 files
  90. biao@ubuntu:~/test/photo$

    运行前需要创建两个目录,image用来存放需要处理的图片,outfile用来存放处理过后的文件。这种处理文件批处理方式很暴力,偶尔用用还是可以的。


(五)海思HI3520 IO控制小程序

嵌入式设备系统一般为了节省空间,一般都会对系统进行裁剪,所以很多有用的命令都会被删除。在嵌入式设备中要调试代码也是比较麻烦的,一般只能看串口打印。现在写了个小程序,专门用来查看和控制海思Hi3520DV300芯片的IO电平状态。

  1. /*=============================================================================
  2. # FileName: Hi3520_IO_CTRL.cpp
  3. # Desc: Hi3520DV300 IO Write and Read
  4. # Author: Caibiao Lee
  5. # Version:
  6. # LastChange: 2018-11-30
  7. # History:
  8. =============================================================================*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "hstGpioAL.h"
  12. int PrintfInputTips(char *ps8Name)
  13. {
  14. printf("=========== error!!! ========\n\n");
  15. printf("usage Write: %s GPIO bit value \n", ps8Name);
  16. printf("usage Read : %s GPIO bit \n", ps8Name);
  17. printf("eg Write 1 to GPIO1_bit02 : %s 1 2 1\n", ps8Name);
  18. printf("eg Read GPIO1_bit02 Value : %s 1 2 \n\n", ps8Name);
  19. printf("=============BT20==================\n")
  20. printf("USB HUB GPIO_0_2 1_UP; 0_Down \n");
  21. printf("RESET_HD GPIO_13_0 0_EN; 1_disEN\n");
  22. printf("Power_HD GPIO_13_3 1_UP; 0_Down \n");
  23. return 0;
  24. }
  25. int main(int argc, char **argv)
  26. {
  27. if((3!=argc)&&(4!=argc))
  28. {
  29. PrintfInputTips(argv[0]);
  30. return -1;
  31. }
  32. unsigned char l_u8GPIONum = 0;
  33. unsigned char l_u8GPIOBit = 0;
  34. unsigned char l_u8SetValue = 0;
  35. GPIO_GROUP_E l_eGpioGroup;
  36. GPIO_BIT_E l_eBit;
  37. GPIO_DATA_E l_eData;
  38. l_u8GPIONum = atoi(argv[1]);
  39. l_u8GPIOBit = atoi(argv[2]);
  40. if(l_u8GPIONum<14)
  41. {
  42. l_eGpioGroup = (GPIO_GROUP_E)l_u8GPIONum;
  43. }else
  44. {
  45. printf("l_u8GPIONum error l_u8GPIONum = %d\n",l_u8GPIONum);
  46. return -1;
  47. };
  48. if(l_u8GPIOBit<8)
  49. {
  50. l_eBit = (GPIO_BIT_E)l_u8GPIOBit;
  51. }else
  52. {
  53. printf("l_u8GPIOBit error l_u8GPIOBit = %d\n",l_u8GPIOBit);
  54. return -1;
  55. }
  56. if(NULL!=argv[3])
  57. {
  58. l_u8SetValue = atoi(argv[3]);
  59. if(0==l_u8SetValue)
  60. {
  61. l_eData = (GPIO_DATA_E)l_u8SetValue;
  62. }else if(1==l_u8SetValue)
  63. {
  64. l_eData = (GPIO_DATA_E)l_u8SetValue;
  65. }else
  66. {
  67. printf("l_u8SetValue error l_u8SetValue = %d\n",l_u8SetValue);
  68. }
  69. }
  70. if(3==argc)
  71. {/**read**/
  72. printf("read GPIO%d Bit%d \n",l_u8GPIONum,l_u8GPIOBit);
  73. /**set input**/
  74. HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_INPUT);
  75. /**read **/
  76. char l_s8bit_val = 0;
  77. HstGpio_Get_Value(l_eGpioGroup, l_eBit, &l_s8bit_val);
  78. printf("read Data = %d \n",l_s8bit_val);
  79. }else if(4==argc)
  80. {/**write**/
  81. printf("Write GPIO %d; Bit %d; Value %d\n",l_u8GPIONum,l_u8GPIOBit,l_u8SetValue);
  82. /***set IO output*/
  83. HstGpio_Set_Direction(l_eGpioGroup, l_eBit, GPIO_OUPUT);
  84. /**Write To IO**/
  85. HstGpio_Set_Value(l_eGpioGroup,l_eBit,l_eData);
  86. }else
  87. {
  88. }
  89. return 0;
  90. }

    完整工程代码下载地址:Hi3520DV300 IO读写应用小程序


(六)路由追踪

路由追踪用来查看网络路由状态,在linux设备中,可以使用traceroute 和tracepath 命令来跟踪路由。在我UbuntuPC端我使用tracepath跟踪我主机到百度的路由如下:

  1. biao@ubuntu:~$
  2. biao@ubuntu:~$
  3. biao@ubuntu:~$ tracepath www.baidu.com
  4. 1?: [LOCALHOST] pmtu 1500
  5. 1: 192.168.20.254 1.425ms
  6. 1: 192.168.20.254 1.733ms
  7. 2: 10.10.10.254 1.997ms
  8. 3: 10.10.10.254 1.522ms pmtu 1492
  9. 3: 100.64.0.1 10.884ms
  10. 4: 113.106.44.49 4.968ms asymm 5
  11. 5: 202.105.158.77 4.629ms
  12. 6: 113.96.4.42 11.837ms
  13. 7: no reply
  14. 8: 14.29.121.190 9.454ms
  15. 9: no reply
  16. 10: no reply
  17. 11: no reply
  18. 12: no reply
  19. 13: no reply
  20. 14: no reply
  21. 15: no reply
  22. ^C
  23. biao@ubuntu:~$

在我一台使用移动物联网卡的设备上运行traceroute,它是4G卡拨号上网的,他的路由情况如下:

  1. ~ #
  2. ~ #
  3. ~ # traceroute www.baidu.com
  4. traceroute to www.baidu.com (111.13.100.92), 30 hops max, 38 byte packets
  5. 1 * 192.168.16.17 (192.168.16.17) 177.186 ms 79.267 ms
  6. 2 * * *
  7. 3 117.132.190.74 (117.132.190.74) 68.455 ms 51.361 ms 59.812 ms
  8. 4 * * *
  9. 5 221.183.38.90 (221.183.38.90) 83.953 ms 87.263 ms 74.621 ms
  10. 6 111.13.98.101 (111.13.98.101) 81.521 ms 111.13.98.93 (111.13.98.93) 77.664 ms 111.13.98.101 (111.13.98.101) 79.678 ms
  11. 7 * * 111.13.112.57 (111.13.112.57) 418.551 ms
  12. 8 * * *
  13. 9 * * *
  14. 10 *

(七)文件固定位置插入数据

       在文件的固定位置插入固定的数据

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BASIC_FILE_NAME "./nandflash.bin"
  5. #define UBOOT_FILE_NAME "./u-boot.bin"
  6. #define KERNEL_FILE_NAME "./kernel.bin"
  7. #define ROOTFS_FILE_NAME "./rootfs.bin"
  8. #define APP_FILE_NAME "./app.bin"
  9. #define UBOOT_POSITION 0x00
  10. #define KERNEL_POSITION 0x100000
  11. #define ROOTFS_POSITION 0x500000
  12. #define APP_POSITION 0x2700000
  13. int InsertData(FILE *pfBasic,FILE *psInsert,int s32Position)
  14. {
  15. int l_S32Ret = 0;
  16. unsigned char l_arru8Temp[1024] = {0xff};
  17. fseek(pfBasic,s32Position,SEEK_SET);
  18. fseek(psInsert,0,SEEK_SET);
  19. while(1)
  20. {
  21. l_S32Ret = fread(l_arru8Temp,1,1024,psInsert);
  22. if(l_S32Ret > 0)
  23. {
  24. l_S32Ret = fwrite(l_arru8Temp,1,l_S32Ret,pfBasic);
  25. if(l_S32Ret<=0)
  26. {
  27. printf("line %d error l_S32Ret = %d \n",__LINE__,l_S32Ret);
  28. return -1;
  29. }
  30. }else
  31. {
  32. break;
  33. }
  34. }
  35. return 0;
  36. }
  37. int main(void)
  38. {
  39. int l_s32Ret = 0;
  40. FILE *l_pfBasec = NULL;
  41. FILE *l_pfUboot = NULL;
  42. FILE *l_pfKernel = NULL;
  43. FILE *l_pfRootfs = NULL;
  44. FILE *l_pfApp = NULL;
  45. l_pfBasec = fopen(BASIC_FILE_NAME,"r+");
  46. if(NULL==l_pfBasec)
  47. {
  48. printf("line %d error \n",__LINE__);
  49. goto ERROR;
  50. }
  51. l_pfUboot = fopen(UBOOT_FILE_NAME,"r");
  52. if(NULL==l_pfUboot)
  53. {
  54. printf("line %d error \n",__LINE__);
  55. goto ERROR;
  56. }
  57. l_pfKernel = fopen(KERNEL_FILE_NAME,"r");
  58. if(NULL==l_pfKernel)
  59. {
  60. printf("line %d error \n",__LINE__);
  61. goto ERROR;
  62. }
  63. l_pfRootfs = fopen(ROOTFS_FILE_NAME,"r");
  64. if(NULL==l_pfRootfs)
  65. {
  66. printf("line %d error \n",__LINE__);
  67. goto ERROR;
  68. }
  69. l_pfApp = fopen(APP_FILE_NAME,"r");
  70. if(NULL==l_pfApp)
  71. {
  72. printf("line %d error \n",__LINE__);
  73. goto ERROR;
  74. }
  75. if(0> InsertData(l_pfBasec,l_pfUboot,UBOOT_POSITION))
  76. {
  77. printf("line %d error \n",__LINE__);
  78. goto ERROR;
  79. }
  80. if(0> InsertData(l_pfBasec,l_pfKernel,KERNEL_POSITION))
  81. {
  82. printf("line %d error \n",__LINE__);
  83. goto ERROR;
  84. }
  85. if(0> InsertData(l_pfBasec,l_pfRootfs,ROOTFS_POSITION))
  86. {
  87. printf("line %d error \n",__LINE__);
  88. goto ERROR;
  89. }
  90. if(0> InsertData(l_pfBasec,l_pfApp,APP_POSITION))
  91. {
  92. printf("line %d error \n",__LINE__);
  93. goto ERROR;
  94. }
  95. ERROR:
  96. if(NULL!=l_pfBasec)
  97. {
  98. fclose(l_pfBasec);
  99. l_pfBasec = NULL;
  100. }
  101. if(NULL!=l_pfUboot)
  102. {
  103. fclose(l_pfUboot);
  104. l_pfUboot = NULL;
  105. }
  106. if(NULL!=l_pfKernel)
  107. {
  108. fclose(l_pfKernel);
  109. l_pfKernel = NULL;
  110. }
  111. if(NULL!=l_pfRootfs)
  112. {
  113. fclose(l_pfRootfs);
  114. l_pfRootfs = NULL;
  115. }
  116. if(NULL!=l_pfApp)
  117. {
  118. fclose(l_pfApp);
  119. l_pfApp = NULL;
  120. }
  121. return 0;
  122. }

(七)H264 I帧与P帧偏移

    在H264存储的时候,P帧比I帧存储得快了14帧,需要将所有的P帧往后移动14帧。

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/time.h>
  8. #include <errno.h>
  9. /*************************************************
  10. Function: FindStartCode3
  11. Description: 判断是否为0x00000001,如果是返回1
  12. Return:
  13. Others:
  14. Author: licaibiao
  15. Date: 2017-12-06
  16. *************************************************/
  17. static int FindStartCode3 (unsigned char *Buf)
  18. {
  19. if(Buf[0]!=0 || Buf[1]!=0 || Buf[2] !=0 || Buf[3] !=1)
  20. {
  21. return 0;
  22. }
  23. else
  24. {
  25. return 1;
  26. }
  27. }
  28. int read_data(FILE *p_fd,unsigned char* pBuf,int* ps32Len)
  29. {
  30. int l_s32Pos = 0;
  31. int l_s32Rewind = -4;
  32. int l_s32NaluLen = 0;
  33. unsigned char l_u8FindFlag = 0;
  34. unsigned char *l_pBuf = NULL;
  35. l_pBuf = (unsigned char*)malloc (1024*1024*10);
  36. /**从码流中读取4个字节**/
  37. if (4 != fread (l_pBuf, 1, 4, p_fd))
  38. {
  39. free(l_pBuf);
  40. return -1;
  41. }
  42. /**判断是否是00 00 00 01**/
  43. if(FindStartCode3(l_pBuf))
  44. {
  45. l_u8FindFlag = 1;
  46. l_s32Pos = 4;
  47. }
  48. else
  49. {
  50. l_u8FindFlag = 0;
  51. printf("【[%s:%d] start code error \n", __func__, __LINE__);
  52. return -1;
  53. }
  54. /**查找下一个开始字符标志位**/
  55. l_u8FindFlag = 0;
  56. while(!l_u8FindFlag)
  57. {
  58. /**判断是否到了文件尾**/
  59. if(feof(p_fd))
  60. {
  61. l_s32NaluLen = l_s32Pos - 1;
  62. memcpy (pBuf, l_pBuf, l_s32NaluLen);
  63. free(pBuf);
  64. pBuf = NULL;
  65. *ps32Len = l_s32NaluLen;
  66. return -2;
  67. }
  68. /**读取一个字节到pBuf 中**/
  69. l_pBuf[l_s32Pos++] = fgetc(p_fd);
  70. if(FindStartCode3(&l_pBuf[l_s32Pos-4]))
  71. {
  72. l_u8FindFlag = 1;
  73. }
  74. }
  75. /**把文件指针指向前一个NALU的末尾 **/
  76. if (0 != fseek (p_fd, l_s32Rewind, SEEK_CUR))
  77. {
  78. free(l_pBuf);
  79. printf("【[%s:%d] GetAnnexbNALU: Cannot fseek in the bit stream file \n", __func__, __LINE__);
  80. }
  81. l_s32NaluLen = l_s32Pos + l_s32Rewind;
  82. memcpy(pBuf,l_pBuf,l_s32NaluLen);
  83. *ps32Len = l_s32NaluLen;
  84. return 0;
  85. }
  86. int main()
  87. {
  88. unsigned char *l_pBuf = NULL;
  89. unsigned char *l_pTemp = NULL;
  90. unsigned char *l_pIFrame = NULL;
  91. unsigned char *l_pPFrame = NULL;
  92. unsigned char* l_IPack[30];
  93. unsigned char* l_PPack[30];
  94. unsigned int l_ILen[33] = {0};
  95. unsigned int l_PLen[33] = {0};
  96. unsigned int i =0;
  97. unsigned int j =0;
  98. unsigned int p =0;
  99. unsigned int l_s32ILen = 0;
  100. unsigned int l_s32PLen = 0;
  101. unsigned int l_d = 0;
  102. int l_s32Ret = 0;
  103. int l_s32Len = 0;
  104. FILE *l_fpInput = NULL;
  105. FILE *l_fpOutput = NULL;
  106. printf("biao debug %d \n",__LINE__);
  107. l_fpInput = fopen("./001.264","rb");
  108. if(NULL==l_fpInput)
  109. {
  110. printf("open error \n");
  111. return -1;
  112. }
  113. l_fpOutput = fopen("./002.h264","w+");
  114. if(NULL==l_fpOutput)
  115. {
  116. printf("open error \n");
  117. return -1;
  118. }
  119. printf("biao debug %d \n",__LINE__);
  120. l_pBuf = (unsigned char*)malloc (1024*1024*1);
  121. l_pTemp = (unsigned char*)malloc (1024*1024*1);
  122. l_pIFrame = (unsigned char*)malloc (1024*1024*1);
  123. l_pPFrame = (unsigned char*)malloc (1024*1024*1);
  124. for(i=0;i<33;i++)
  125. {
  126. l_IPack[i] = (unsigned char*)malloc (500*1024);
  127. l_PPack[i] = (unsigned char*)malloc (500*1024);
  128. }
  129. i = 0;
  130. p = 14;
  131. while(1)
  132. {
  133. l_s32Ret = read_data(l_fpInput,l_pBuf,&l_s32Len);
  134. if(0==l_s32Ret)
  135. {
  136. if(0x61!=l_pBuf[4]) /**I**/
  137. {
  138. memcpy(&l_pIFrame[l_s32ILen],l_pBuf,l_s32Len);
  139. l_s32ILen +=l_s32Len;
  140. if(0x65==l_pBuf[4])
  141. {
  142. printf("write I %d\n",i);
  143. memcpy(l_IPack[i],l_pIFrame,l_s32ILen);
  144. l_ILen[i] = l_s32ILen;
  145. i++;
  146. l_s32ILen = 0;
  147. }
  148. }
  149. if(0x61==l_pBuf[4]) /**P**/
  150. {
  151. memcpy(&l_pPFrame[l_s32PLen],l_pBuf,l_s32Len);
  152. l_s32PLen +=l_s32Len;
  153. p++;
  154. if(0==(p%44))
  155. {
  156. printf("write P %d frame=%d l_s32PLen = %d\n",j,p,l_s32PLen);
  157. memcpy(l_PPack[j],l_pPFrame,l_s32PLen);
  158. l_PLen[j] = l_s32PLen;
  159. j++;
  160. l_s32PLen = 0;
  161. }
  162. }
  163. }else
  164. {
  165. break;
  166. }
  167. }
  168. for(i=0;i<j;i++)
  169. {
  170. fwrite(l_IPack[i+3],1,l_ILen[i+3],l_fpOutput);
  171. fwrite(l_PPack[i],1,l_PLen[i],l_fpOutput);
  172. }
  173. fclose(l_fpInput);
  174. fclose(l_fpOutput);
  175. free(l_pBuf);
  176. free(l_pTemp);
  177. free(l_pIFrame);
  178. free(l_pPFrame);
  179. for(i=0;i<33;i++)
  180. {
  181. free(l_IPack[i]);
  182. free(l_PPack[i]);
  183. }
  184. return 0;
  185. }

(八)获取本地IP地址

    在linux设备中获取本地IP地址可以使用下面的程序,支持最大主机有三个网口的设备,当然这个网卡数可以修改。

  1. #include <stdio.h>
  2. #include <ifaddrs.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. int get_local_ip(char *ps8IpList)
  7. {
  8. struct ifaddrs *ifAddrStruct;
  9. char l_s8IpAddr[INET_ADDRSTRLEN];
  10. void *tmpAddrPtr;
  11. int l_s32IPCount = 0;
  12. getifaddrs(&ifAddrStruct);
  13. while (ifAddrStruct != NULL)
  14. {
  15. if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
  16. {
  17. tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
  18. inet_ntop(AF_INET, tmpAddrPtr, l_s8IpAddr, INET_ADDRSTRLEN);
  19. if (strcmp(l_s8IpAddr, "127.0.0.1") != 0)
  20. {
  21. if(l_s32IPCount == 0)
  22. {
  23. memcpy(ps8IpList, l_s8IpAddr, INET_ADDRSTRLEN);
  24. } else
  25. {
  26. memcpy(ps8IpList+INET_ADDRSTRLEN, l_s8IpAddr, INET_ADDRSTRLEN);
  27. }
  28. l_s32IPCount++;
  29. }
  30. }
  31. ifAddrStruct=ifAddrStruct->ifa_next;
  32. }
  33. freeifaddrs(ifAddrStruct);
  34. return l_s32IPCount;
  35. }
  36. int main()
  37. {
  38. char l_arrs8IpAddrList[3][INET_ADDRSTRLEN];
  39. int l_s32AddrCount;
  40. memset(l_arrs8IpAddrList, 0, sizeof(l_arrs8IpAddrList));
  41. l_s32AddrCount = get_local_ip(*l_arrs8IpAddrList);
  42. for(l_s32AddrCount;l_s32AddrCount>0;l_s32AddrCount--)
  43. {
  44. printf("Server Local IP%d: %s\n",l_s32AddrCount,l_arrs8IpAddrList[l_s32AddrCount-1]);
  45. }
  46. return 0;
  47. }

------------------------------------------2022.08.28日更新------------------------------------------

该博客将停止更新 

新的文章内容和附件工程文件更新到了

Gong众号 : liwen01

-----------------------------------------------2022.08.28-----------------------------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/313006
推荐阅读
相关标签
  

闽ICP备14008679号