gateway.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "includes.h"
  2. #include "device.h"
  3. #include "gateway.h"
  4. #include "net_proc.h"
  5. typedef struct _gb_gateway_info {
  6. gb_gateway_bind_info_t bind;
  7. u16 gw_echo_fail_count;
  8. u16 node_echo_fail_count[GB_SUPPORT_MAX_NODE];
  9. } gb_gateway_info_t;
  10. gb_gateway_info_t gw_mgr;
  11. int gateway_gw_id_flush(u32 gw_device_id)
  12. {
  13. int ret = 0;
  14. gw_mgr.bind.gw_device_id = gw_device_id;
  15. gw_mgr.gw_echo_fail_count = 0;
  16. return ret;
  17. }
  18. int gateway_id_cmp(u32 gw_device_id)
  19. {
  20. int ret = 0;
  21. if(gw_mgr.bind.gw_device_id == gw_device_id) {
  22. ret = 1;
  23. }
  24. return ret;
  25. }
  26. int gateway_gw_id_get(u32 *gw_device_id)
  27. {
  28. int ret = 0;
  29. if(gw_device_id) {
  30. *gw_device_id = gw_mgr.bind.gw_device_id;
  31. }
  32. if(gw_mgr.bind.gw_device_id) {
  33. ret = 1;
  34. }
  35. return ret;
  36. }
  37. int gateway_id_and_chn_get(u32 des_device_type, u32 des_device_id,
  38. u32 *gw_device_id, uint8_t *chn)
  39. {
  40. int ret = 0, i = 0;
  41. if(gw_device_id) {
  42. *gw_device_id = gw_mgr.bind.gw_device_id;
  43. }
  44. if(chn) {
  45. for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
  46. gb_node_info_t *p_node = &gw_mgr.bind.node[i];
  47. if((p_node->device_type == des_device_type)&&
  48. (p_node->device_id == des_device_id)) {
  49. *chn = i;
  50. ret = 1;//find
  51. break;
  52. }
  53. }
  54. }
  55. return ret;
  56. }
  57. int gateway_node_type_and_id_flush(u32 des_device_type, u32 des_device_id)
  58. {
  59. int i = 0, choice = 0xff;
  60. gb_node_info_t *p_node = NULL;
  61. for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
  62. p_node = &gw_mgr.bind.node[i];
  63. if((p_node->device_type == des_device_type)&&
  64. (p_node->device_id == des_device_id)) {
  65. gw_mgr.node_echo_fail_count[i] = 0;
  66. break;
  67. } else if((choice == 0xff) &&(p_node->device_type == 0)) {
  68. choice = i;
  69. }
  70. }
  71. if((i == GB_SUPPORT_MAX_NODE)&&(choice != 0xff)) {/* bind */
  72. p_node = &gw_mgr.bind.node[choice];
  73. p_node->device_type = des_device_type;
  74. p_node->device_id = des_device_id;
  75. gw_mgr.node_echo_fail_count[choice] = 0;
  76. }
  77. return 0;
  78. }
  79. //void gateway_msg_timer(void *p_tmr, void *p_arg)
  80. //{
  81. // inner_msg_format_t *p_msg = NULL;
  82. // u8 type = 0, len = 0, i;
  83. // p_msg = (inner_msg_format_t *)net_queue_mem_calloc();
  84. // if(p_msg == NULL) {
  85. // return ;
  86. // }
  87. //
  88. // if((device_type_get() == DEV_TYPE_COLLECT_STATION)||
  89. // (device_type_get() == DEV_TYPE_OILTANK_STATION)) {/* collect */
  90. // gb_node_bind_info_t *p_node_bind = (gb_node_bind_info_t *)p_msg->info;
  91. // if(gw_mgr.bind.gw_device_id == 0) {
  92. // p_node_bind->gw_device_id = 0;
  93. // type = net_msg_type_bind_req;
  94. // } else {
  95. // p_node_bind->gw_device_id = gw_mgr.bind.gw_device_id;
  96. // type = net_msg_type_node_echo;
  97. // }
  98. // device_type_and_id_get(&p_node_bind->device_type, &p_node_bind->device_id);
  99. // len = sizeof(gb_node_bind_info_t);
  100. // gw_mgr.gw_echo_fail_count++;
  101. // if(gw_mgr.gw_echo_fail_count > GATEWAY_ECHO_MAX_COUNT) {/* aging. */
  102. // gw_mgr.bind.gw_device_id = 0;
  103. // }
  104. // } else if(device_type_get() == DEV_TYPE_GATEWAY){/* gateway */
  105. // type = net_msg_type_gw_echo;
  106. // memcpy(p_msg->info, &gw_mgr.bind, sizeof(gb_gateway_bind_info_t));
  107. // len = sizeof(gb_gateway_bind_info_t);
  108. // for(i = 0;i < GB_SUPPORT_MAX_NODE;i++) {
  109. // gw_mgr.node_echo_fail_count[i]++;
  110. // if(gw_mgr.node_echo_fail_count[i] > GATEWAY_ECHO_MAX_COUNT) {/* aging. */
  111. // gw_mgr.bind.node[i].device_type = 0;
  112. // gw_mgr.bind.node[i].device_id = 0;
  113. // }
  114. // }
  115. // }
  116. //
  117. // net_msg_frame_fill(p_msg, type, len);
  118. // net_queue_insert((char *)p_msg, sizeof(inner_msg_format_t));
  119. //}
  120. OS_TMR gateway_timer;
  121. int gateway_init(void)
  122. {
  123. // OS_ERR err;
  124. // if(device_type_get() == DEV_TYPE_GATEWAY) {
  125. // device_type_and_id_get(NULL, &gw_mgr.bind.gw_device_id);
  126. // }
  127. // OSTmrCreate(&gateway_timer, "gateway_tmr",
  128. // GW_MSG_PERIOD, GW_MSG_PERIOD, OS_OPT_TMR_PERIODIC, gateway_msg_timer, NULL, &err);
  129. // OSTmrStart(&gateway_timer, &err);
  130. return 0;
  131. }