common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "common.h"
  2. #include "ql_type.h"
  3. unsigned long htoi(char s[])
  4. {
  5. int i = 0;
  6. unsigned long long n = 0;
  7. if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) i = 2;
  8. else i = 0;
  9. for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i){
  10. if (tolower(s[i]) > '9') n = 16 * n + (10 + tolower(s[i]) - 'a');
  11. else n = 16 * n + (tolower(s[i]) - '0');
  12. }
  13. return n;
  14. }
  15. int GetParaFromStr(char *Str,char *Para,char *Value){
  16. int ValueLen=0,ParaLen=0;
  17. char ParaTemp[22];
  18. char *p;
  19. char d;
  20. int i=0;
  21. if(0==*Str || 0==*Para)return 0;
  22. while(0!=(d=*Para) && ParaLen<20){
  23. ParaTemp[i++]=d;
  24. if(d=='=' || d==';')return 0;
  25. Para++;
  26. ParaLen++;
  27. }
  28. ParaTemp[ParaLen++]='=';
  29. ParaTemp[ParaLen]=0;
  30. p=strstr(Str,ParaTemp);
  31. p+=ParaLen;
  32. //--
  33. while(*p>0x20 && ';'!=*p && ValueLen<=80){
  34. *Value=*p;
  35. p++;
  36. Value++;
  37. ValueLen++;
  38. }
  39. *Value=0;
  40. return ValueLen;
  41. }
  42. char msgCmp(char *msg, char *target){
  43. char *p1=target, *p2=msg;
  44. while(*p1 != 0){
  45. if(*p1 != *p2) return 0;
  46. p1++;p2++;
  47. }
  48. return 1;
  49. }
  50. // '56'->0X56
  51. unsigned char charStrToHex(char d1, char d2){
  52. unsigned char p1=d1;
  53. unsigned char p2=d2;
  54. if(p1>='0' && p1 <='9') p1 -= 0x30;
  55. else if(p1>='a' && p1 <='f') p1 -= 0x57;
  56. else if(p1>='A' && p1 <='F') p1 -= 0x37;
  57. else return 0;
  58. p1<<=4;p1&=0xF0;
  59. if(p2>='0' && p2 <='9') p2 -= 0x30;
  60. else if(p2>='a' && p2 <='f') p2 -= 0x57;
  61. else if(p2>='A' && p2 <='F') p2 -= 0x37;
  62. else return 0;
  63. return (p1|p2);
  64. }
  65. /*
  66. restoreDataFormatByHex
  67. "313233"=>{0x31,0x32,0x33}
  68. */
  69. unsigned char restoreDataFormatByHex(unsigned char *src, unsigned short len)
  70. {
  71. unsigned short i,j,k;
  72. unsigned char temp[2];
  73. if(0==src) return 1;
  74. j=0;
  75. for(i=0;i<len/2;i++){
  76. temp[0]=src[2*i];
  77. temp[1]=src[2*i+1];
  78. for(k=0;k<2;k++){
  79. if(temp[k]>='0' && temp[k]<='9') temp[k] -= 0x30;
  80. else if(temp[k]>='a' && temp[k]<='f') temp[k] -= 0x57;
  81. else if(temp[k]>='A' && temp[k]<='F') temp[k] -= 0x37;
  82. else return 2;
  83. }
  84. temp[0] <<= 4;
  85. temp[0] &= 0xF0;
  86. src[j++] = temp[0] | temp[1];
  87. }
  88. return 0;
  89. }
  90. /*
  91. 从长度为len的src缓存中查找第index个target,并返回下一个字节的索引
  92. src:缓存
  93. len:缓存长度
  94. target:要匹配的字节内容
  95. index:第几个target, 1,2,3,4.....
  96. return: < 0 失败,else, target后面一个字节的索引
  97. */
  98. int findByteFromStr(unsigned char *src, int len,unsigned char target,unsigned char index){
  99. int i;
  100. unsigned char num=0;
  101. for(i=0;i<len;i++){
  102. if(src[i]==target){
  103. if(++num==index) return i+1;
  104. }
  105. }
  106. return -1;
  107. }
  108. unsigned char AscToHex(unsigned char aHex)
  109. {
  110. if((aHex>=0)&&(aHex<=9))
  111. aHex += 0x30;
  112. else if((aHex>=10)&&(aHex<=15))//A-F
  113. //aHex += 0x37;
  114. aHex += 0x57;
  115. else aHex = 0xff;
  116. return aHex;
  117. }
  118. void AscStrTurnHexStr(char *AscStr, char *HexStr)
  119. {
  120. char *pAscStr=AscStr;
  121. unsigned char d,h,l;
  122. while(0!=(d=(unsigned char)*pAscStr++)){
  123. l=d&0x0f;
  124. h=d>>4;
  125. *HexStr++=AscToHex(h);
  126. *HexStr++=AscToHex(l);
  127. }
  128. *HexStr=0;
  129. }
  130. void LwEndingToBiEndingStr(char *data, int len){
  131. char tt[2];
  132. int i;
  133. for(i=0;i<len/4;i++){
  134. tt[0]=data[4*i];
  135. tt[1]=data[4*i+1];
  136. data[4*i]=data[4*i+2];
  137. data[4*i+1]=data[4*i+3];
  138. data[4*i+2]=tt[0];
  139. data[4*i+3]=tt[1];
  140. }
  141. }
  142. #include "ohpoc.h"
  143. MY_CLOCK poc_clock;
  144. char *ohpocGetICCID(void){
  145. static char ICCID[21];
  146. if(0==ql_sim_get_iccid(ICCID,sizeof(ICCID))){
  147. return &ICCID[0];
  148. }
  149. return NULL;
  150. //if(sutApp.ICCID[0]==0)return NULL;
  151. //return &sutApp.ICCID[0];
  152. };
  153. char *ohpocGetIMEI(void){
  154. static char IMEI[16];
  155. if(0==ql_dev_get_imei(IMEI,sizeof(IMEI))){
  156. return &IMEI[0];
  157. }
  158. return NULL;
  159. };
  160. void getMyClock(MY_CLOCK *myclock){
  161. myclock->year=poc_clock.year;
  162. myclock->month=poc_clock.month;
  163. myclock->day=poc_clock.day;
  164. myclock->hour=poc_clock.hour;
  165. myclock->min=poc_clock.min;
  166. myclock->sec=poc_clock.sec;
  167. }
  168. void ohpocGetClock(MY_CLOCK *myclock){
  169. //getMyClock(myclock);
  170. current_zone_time(myclock);
  171. };