ble_wechat_util.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "ble_wechat_util.h"
  2. data_handler first_handler = {PRODUCT_TYPE_UNKNOWN, NULL, NULL, NULL, NULL};
  3. //function for getting handler by type
  4. //find the type in the list structure of data handler then return the node pointer
  5. data_handler* get_handler_by_type(int type)
  6. {
  7. data_handler* handler = &first_handler;
  8. while(handler->next != NULL) {
  9. handler = handler->next;
  10. if (handler->m_product_type == type) {
  11. return handler;
  12. }
  13. }
  14. return NULL;
  15. }
  16. #define BigLittleSwap16(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
  17. (((uint16_t)(A) & 0x00ff) << 8))
  18. #define BigLittleSwap32(A) ((((uint32_t)(A) & 0xff000000) >> 24) | \
  19. (((uint32_t)(A) & 0x00ff0000) >> 8) | \
  20. (((uint32_t)(A) & 0x0000ff00) << 8) | \
  21. (((uint32_t)(A) & 0x000000ff) << 24))
  22. int checkCPUendian()
  23. {
  24. union{
  25. unsigned long i;
  26. uint8_t s[4];
  27. }c;
  28. c.i = 0x12345678;
  29. return (0x12 == c.s[0]);
  30. }
  31. unsigned long t_htonl(unsigned long h)
  32. {
  33. return checkCPUendian() ? h : BigLittleSwap32(h);
  34. }
  35. unsigned long t_ntohl(unsigned long n)
  36. {
  37. return checkCPUendian() ? n : BigLittleSwap32(n);
  38. }
  39. unsigned short htons(unsigned short h)
  40. {
  41. return checkCPUendian() ? h : BigLittleSwap16(h);
  42. }
  43. unsigned short ntohs(unsigned short n)
  44. {
  45. return checkCPUendian() ? n : BigLittleSwap16(n);
  46. }
  47. /*turn an unsigned short value to big-endian value */
  48. /*for example 0x1234 in the memory of X86 is 0x34 and 0x12 */
  49. /*then turn it to Network Byte Order is 0x12 and 0x34 */