Util.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "Util.h"
  2. #include <sys/sysinfo.h>
  3. /*----------------------------------------
  4. * GetBits
  5. *----------------------------------------*/
  6. uint8_t
  7. GetBits (uint8_t Val, uint8_t Mask)
  8. {
  9. uint8_t ExtVal = 0;
  10. while (0 != Mask)
  11. {
  12. if (0 != (Mask & 0x80))
  13. {
  14. ExtVal <<= 1;
  15. if (0 != (Val & 0x80)) { ExtVal += 0x01; }
  16. }
  17. Val <<= 1;
  18. Mask <<= 1;
  19. }
  20. return ExtVal;
  21. }
  22. /*------------------------------------------
  23. * SetBits
  24. *------------------------------------------*/
  25. uint8_t
  26. SetBits (uint8_t Mask, uint8_t Val)
  27. {
  28. uint8_t FinVal = 0;
  29. uint8_t i;
  30. for (i = 0; i < 8; i++)
  31. {
  32. FinVal >>= 1;
  33. if (0 != (Mask & 0x01))
  34. {
  35. if (0 != (Val & 0x01)) { FinVal |= 0x80; }
  36. Val >>= 1;
  37. }
  38. Mask >>= 1;
  39. }
  40. return FinVal;
  41. }
  42. /*------------------------------------------
  43. * CalculateCheckSum
  44. *------------------------------------------*/
  45. uint8_t
  46. CalculateCheckSum (uint8_t* Data, uint16_t Len)
  47. {
  48. uint8_t Sum;
  49. uint16_t i;
  50. Sum = 0;
  51. for (i = 0; i < Len; i++)
  52. {
  53. Sum += Data [i];
  54. }
  55. return (0x100 - Sum);
  56. }
  57. /*
  58. * @fn TimeUpdate
  59. * @return Returns the system uptime
  60. */
  61. uint32_t TimeUpdate()
  62. {
  63. struct sysinfo time;
  64. sysinfo(&time);
  65. return time.uptime;
  66. }
  67. // /**
  68. // *@fn GetSysCtlvalue
  69. // *@brief This function retrieves the values from Sysctl
  70. // *@param TagName -Tagname form which value has to be retrieved
  71. // *@param SysVal - Where the value is stored
  72. // *@return Returns 0 on success
  73. // * Returns 1 on failure
  74. // */
  75. // int GetJiffySysCtlvalue (const char *TagName, long long *SysVal)
  76. // {
  77. // unsigned long RetVal;
  78. // FILE* SysFile = fopen (TagName, "r");
  79. // unsigned long Hertz = sysconf(_SC_CLK_TCK);
  80. // if ((!SysFile) || (!SysVal))
  81. // return 1;
  82. // fscanf (SysFile, "%lu", &RetVal);
  83. // fclose (SysFile);
  84. // *SysVal = (RetVal*1000)/Hertz;
  85. // return 0;
  86. // }
  87. /*
  88. index = 0,1,2,3,4,5
  89. */
  90. uint8_t mac2hex(const char *strMac, uint8_t index)
  91. {
  92. int i = 0;
  93. char *pStr = (char *)strMac;
  94. char *pTmp = NULL;
  95. uint8_t macByte = 0;;
  96. char tmp[3] = {0};
  97. if(index > 5)
  98. {
  99. printf("Invalid index %d\n", index);
  100. return -1;
  101. }
  102. //find
  103. while(pStr)
  104. {
  105. if(i == index)
  106. break;
  107. if(*pStr == ':')
  108. i++;
  109. pStr++;
  110. }
  111. if(*(pStr+1) == ':')
  112. {
  113. tmp[0] = *pStr;
  114. tmp[1] = '\0';
  115. tmp[2] = '\0';
  116. }
  117. else
  118. {
  119. tmp[0] = *pStr;
  120. tmp[1] = *(pStr+1);
  121. tmp[2] = '\0';
  122. }
  123. i=0;
  124. macByte = 0;
  125. while(tmp[i])
  126. {
  127. macByte = macByte<<4;
  128. if((tmp[i]>='0') && (tmp[i]<='9'))
  129. macByte += (tmp[i]-'0');
  130. else if((tmp[i]>='a') && (tmp[i]<='f'))
  131. macByte += (tmp[i]-'a') + 10;
  132. else if((tmp[i]>='A') && (tmp[i]<='F'))
  133. macByte += (tmp[i]-'A') + 10;
  134. i++;
  135. }
  136. return macByte;
  137. }
  138. /*
  139. index = 0,1,2,3
  140. */
  141. uint8_t ip2dec(const char *strIp, uint8_t index)
  142. {
  143. int i = 0;
  144. char *pStr = (char *)strIp;
  145. uint8_t ipByte = 0;;
  146. char tmp[4] = {0};
  147. if(index > 3)
  148. {
  149. printf("Invalid index %d\n", index);
  150. return -1;
  151. }
  152. //find
  153. while(pStr)
  154. {
  155. if(i == index)
  156. break;
  157. if(*pStr == '.')
  158. i++;
  159. pStr++;
  160. }
  161. if(*(pStr+1) == '.')
  162. {
  163. tmp[0] = *pStr;
  164. tmp[1] = '\0';
  165. tmp[2] = '\0';
  166. tmp[3] = '\0';
  167. }
  168. else if(*(pStr+2) == '.')
  169. {
  170. tmp[0] = *pStr;
  171. tmp[1] = *(pStr+1);
  172. tmp[2] = '\0';
  173. tmp[3] = '\0';
  174. }
  175. else
  176. {
  177. tmp[0] = *pStr;
  178. tmp[1] = *(pStr+1);
  179. tmp[2] = *(pStr+2);
  180. tmp[3] = '\0';
  181. }
  182. ipByte = atoi(tmp);
  183. return ipByte;
  184. }
  185. int FlushUserInfo(void)
  186. {
  187. //TODO:
  188. return 0;
  189. }
  190. int FlushFRU(void)
  191. {
  192. //TODO:
  193. return 0;
  194. }
  195. int FlushSDR(void)
  196. {
  197. //TODO:
  198. return 0;
  199. }
  200. int FlushIpmiConfig(void)
  201. {
  202. //TODO:
  203. return 0;
  204. }
  205. int FlushSEL(void)
  206. {
  207. //TODO:
  208. return 0;
  209. }