common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "common.h"
  2. #include "ohpoc.h"
  3. MY_CLOCK My_Clock;
  4. int GetParaFromStr(char *Str,char *Para,char *Value){
  5. int ValueLen=0,ParaLen=0;
  6. char ParaTemp[22];
  7. char *p;
  8. char d;
  9. int i=0;
  10. if(0==*Str || 0==*Para)return 0;
  11. while(0!=(d=*Para) && ParaLen<20){
  12. ParaTemp[i++]=d;
  13. if(d=='=' || d==';')return 0;
  14. Para++;
  15. ParaLen++;
  16. }
  17. ParaTemp[ParaLen++]='=';
  18. ParaTemp[ParaLen]=0;
  19. p=strstr(Str,ParaTemp);
  20. p+=ParaLen;
  21. //--
  22. while(*p>0x20 && ';'!=*p && ValueLen<=80){
  23. *Value=*p;
  24. p++;
  25. Value++;
  26. ValueLen++;
  27. }
  28. *Value=0;
  29. return ValueLen;
  30. }
  31. char msgCmp(char *msg, char *target){
  32. char *p1=target, *p2=msg;
  33. while(*p1 != 0){
  34. if(*p1 != *p2) return 0;
  35. p1++;p2++;
  36. }
  37. return 1;
  38. }
  39. // '56'->0X56
  40. unsigned char charStrToHex(char d1, char d2){
  41. unsigned char p1=d1;
  42. unsigned char p2=d2;
  43. if(p1>='0' && p1 <='9') p1 -= 0x30;
  44. else if(p1>='a' && p1 <='f') p1 -= 0x57;
  45. else if(p1>='A' && p1 <='F') p1 -= 0x37;
  46. else return 0;
  47. p1<<=4;p1&=0xF0;
  48. if(p2>='0' && p2 <='9') p2 -= 0x30;
  49. else if(p2>='a' && p2 <='f') p2 -= 0x57;
  50. else if(p2>='A' && p2 <='F') p2 -= 0x37;
  51. else return 0;
  52. return (p1|p2);
  53. }
  54. /*
  55. restoreDataFormatByHex
  56. "313233"=>{0x31,0x32,0x33}
  57. */
  58. unsigned char restoreDataFormatByHex(unsigned char *src, unsigned short len)
  59. {
  60. unsigned short i,j,k;
  61. unsigned char temp[2];
  62. if(0==src) return 1;
  63. j=0;
  64. for(i=0;i<len/2;i++){
  65. temp[0]=src[2*i];
  66. temp[1]=src[2*i+1];
  67. for(k=0;k<2;k++){
  68. if(temp[k]>='0' && temp[k]<='9') temp[k] -= 0x30;
  69. else if(temp[k]>='a' && temp[k]<='f') temp[k] -= 0x57;
  70. else if(temp[k]>='A' && temp[k]<='F') temp[k] -= 0x37;
  71. else return 2;
  72. }
  73. temp[0] <<= 4;
  74. temp[0] &= 0xF0;
  75. src[j++] = temp[0] | temp[1];
  76. }
  77. return 0;
  78. }
  79. void LwEndingToBiEndingStr(char *data, int len){
  80. char tt[2];
  81. int i;
  82. for(i=0;i<len/4;i++){
  83. tt[0]=data[4*i];
  84. tt[1]=data[4*i+1];
  85. data[4*i]=data[4*i+2];
  86. data[4*i+1]=data[4*i+3];
  87. data[4*i+2]=tt[0];
  88. data[4*i+3]=tt[1];
  89. }
  90. }
  91. void RTC_Init(void)
  92. {
  93. My_Clock.year=2000;
  94. My_Clock.month=1;
  95. My_Clock.day=1,
  96. My_Clock.hour=0;
  97. My_Clock.min=0;
  98. My_Clock.sec=0;
  99. }
  100. void RTC_Update(void)
  101. {
  102. if(++My_Clock.sec>=60){
  103. My_Clock.sec=0;
  104. if(++My_Clock.min>=60){
  105. My_Clock.min=0;
  106. if(++My_Clock.hour>=24){
  107. My_Clock.hour=0;
  108. }
  109. }
  110. }
  111. }
  112. unsigned char AscToHex(unsigned char aHex)
  113. {
  114. if((aHex>=0)&&(aHex<=9))
  115. aHex += 0x30;
  116. else if((aHex>=10)&&(aHex<=15))//A-F
  117. //aHex += 0x37;
  118. aHex += 0x57;
  119. else aHex = 0xff;
  120. return aHex;
  121. }
  122. void AscStrTurnHexStr(char *AscStr, char *HexStr)
  123. {
  124. char *pAscStr=AscStr;
  125. unsigned char d,h,l;
  126. while(0!=(d=(unsigned char)*pAscStr++)){
  127. l=d&0x0f;
  128. h=d>>4;
  129. *HexStr++=AscToHex(h);
  130. *HexStr++=AscToHex(l);
  131. }
  132. *HexStr=0;
  133. }