mb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "mb.h"
  2. #include "list.h"
  3. #define MALLOC malloc
  4. #define FREE free
  5. typedef struct {
  6. mb_inst_t *hinst;
  7. uint8_t mode;
  8. uint8_t type;
  9. rt_thread_t tid;
  10. uint8_t quit;
  11. }mb_handle_t;
  12. static mb_handle_t mbHandle={0};
  13. typedef void (*thread_fn_t)(void *arg);
  14. static int mb_stop(mb_handle_t *h);
  15. static void mb_thread(void *arg)
  16. {
  17. mb_handle_t *h=(mb_handle_t*)arg;
  18. while(h->quit==0) {
  19. mb_slave_fsm(h->hinst);
  20. }
  21. h->quit = 2;
  22. }
  23. static int mb_start(mb_handle_t *h)
  24. {
  25. if(h->mode==MB_MODE_MASTER) {
  26. return -1;
  27. }
  28. if(h->tid) {
  29. mb_stop(h);
  30. }
  31. h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
  32. if(h->tid==NULL) {
  33. //LOG_E("___ thread create failed\n");
  34. return -1;
  35. }
  36. h->quit = 0;
  37. rt_thread_startup(h->tid);
  38. return 0;
  39. }
  40. static int mb_stop(mb_handle_t *h)
  41. {
  42. if(h->mode==MB_MODE_MASTER || h->tid==NULL) {
  43. return -1;
  44. }
  45. h->quit = 1;
  46. mb_destory(h->hinst);
  47. while(h->quit!=2);
  48. rt_thread_delete(h->tid);
  49. h->tid = NULL;
  50. return 0;
  51. }
  52. void* mb_init(mb_para_t *para)
  53. {
  54. list_cfg_t lc={0,LIST_FULL_FIFO,10};
  55. mb_handle_t *h=NULL;
  56. if(!para) {
  57. return NULL;
  58. }
  59. h = (mb_handle_t*)MALLOC(sizeof(mb_handle_t));
  60. if(h) {
  61. memset(h, 0, sizeof(mb_handle_t));
  62. mb_setup(h, para);
  63. }
  64. return h;
  65. }
  66. int mb_setup(void *h, mb_para_t *para)
  67. {
  68. mb_backend_param_t param={0};
  69. mb_handle_t *mh=(mb_handle_t*)h;
  70. if(!mh || !para) {
  71. return -1;
  72. }
  73. mh->type = (para->type==MB_TYPE_RTU)?MB_BACKEND_TYPE_RTU:MB_BACKEND_TYPE_TCP;
  74. mh->mode = para->mode;
  75. if(mh->type==MB_TYPE_RTU) {
  76. if(mh->hinst) {
  77. mb_disconn(mh->hinst);
  78. }
  79. param.rtu = para->para.rtu;
  80. mh->hinst = mb_create(mh->type, &param);
  81. if(!mh->hinst) {
  82. return -1;
  83. }
  84. }
  85. else if(mh->type==MB_TYPE_TCP) {
  86. }
  87. else {
  88. return -1;
  89. }
  90. mb_start(h);
  91. return 0;
  92. }
  93. int mb_deinit(void *h)
  94. {
  95. mb_handle_t *mh=(mb_handle_t*)h;
  96. if(!mh) {
  97. return -1;
  98. }
  99. mb_stop(mh);
  100. FREE(mh);
  101. return 0;
  102. }
  103. //
  104. int mb_read(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt, int timeout)
  105. {
  106. int r=-1,sz=0;
  107. mb_handle_t *mh=(mb_handle_t*)h;
  108. if(!mh || !data || cnt<=0) {
  109. return -1;
  110. }
  111. #if 0
  112. rt_tick_t end = rt_tick_get_millisecond()+timeout;
  113. while(1) {
  114. sz = list_size(mh->rx);
  115. if(sz>0) {
  116. break;
  117. }
  118. rt_thread_mdelay(1);
  119. if(timeout>0 && rt_tick_get_millisecond()>end) {
  120. sz = list_size(mh->rx);
  121. break;
  122. }
  123. }
  124. if(sz>0) {
  125. list_node_t *ln=NULL;
  126. r = list_take_node(mh->rx, &ln, 0);
  127. if(r==0) {
  128. node_t *nd=&ln->data;
  129. if(len>=nd->dlen) {
  130. memcpy(data, nd->buf, nd->dlen);
  131. }
  132. else {
  133. r = -1;
  134. }
  135. list_back_node(mh->rx, ln);
  136. }
  137. }
  138. #else
  139. mb_set_tmo(mh->hinst, timeout, 15);
  140. r = mb_read_regs(mh->hinst, addr, cnt, data);
  141. #endif
  142. return r;
  143. }
  144. int mb_write(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
  145. {
  146. int r;
  147. mb_handle_t *mh=(mb_handle_t*)h;
  148. if(!mh) {
  149. return -1;
  150. }
  151. #if 0
  152. r = list_append(mh->tx, 0, data, len);
  153. #else
  154. r = mb_read_regs(mh->hinst, addr, cnt, data);
  155. #endif
  156. return r;
  157. }