mb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. //
  29. // if(h->tid) {
  30. // mb_stop(h);
  31. // }
  32. // h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
  33. // if(h->tid==NULL) {
  34. // //LOG_E("___ thread create failed\n");
  35. // return -1;
  36. // }
  37. //
  38. // h->quit = 0;
  39. // rt_thread_startup(h->tid);
  40. return 0;
  41. }
  42. static int mb_stop(mb_handle_t *h)
  43. {
  44. if(h->mode==MB_MODE_MASTER || h->tid==NULL) {
  45. return -1;
  46. }
  47. h->quit = 1;
  48. mb_destory(h->hinst);
  49. while(h->quit!=2);
  50. rt_thread_delete(h->tid);
  51. h->tid = NULL;
  52. return 0;
  53. }
  54. void* mb_init(mb_para_t *para)
  55. {
  56. list_cfg_t lc={0,LIST_FULL_FIFO,10};
  57. mb_handle_t *h=NULL;
  58. if(!para) {
  59. return NULL;
  60. }
  61. h = (mb_handle_t*)MALLOC(sizeof(mb_handle_t));
  62. if(h) {
  63. memset(h, 0, sizeof(mb_handle_t));
  64. mb_setup(h, para);
  65. }
  66. return h;
  67. }
  68. int mb_setup(void *h, mb_para_t *para)
  69. {
  70. int r;
  71. mb_backend_param_t param={0};
  72. mb_handle_t *mh=(mb_handle_t*)h;
  73. if(!mh || !para) {
  74. return -1;
  75. }
  76. mh->type = para->type;
  77. mh->mode = para->mode;
  78. if(mh->type==MB_TYPE_RTU) {
  79. if(mh->hinst) {
  80. mb_disconn(mh->hinst);
  81. }
  82. param.rtu = para->para.rtu;
  83. mh->hinst = mb_create(mh->type, &param);
  84. if(!mh->hinst) {
  85. return -1;
  86. }
  87. r = mb_connect(mh->hinst);
  88. if(r) {
  89. LOGE("___ mb_connect failed\n");
  90. }
  91. }
  92. else if(mh->type==MB_TYPE_TCP) {
  93. }
  94. else {
  95. return -1;
  96. }
  97. mb_start(h);
  98. return 0;
  99. }
  100. int mb_deinit(void *h)
  101. {
  102. mb_handle_t *mh=(mb_handle_t*)h;
  103. if(!mh) {
  104. return -1;
  105. }
  106. mb_stop(mh);
  107. FREE(mh);
  108. return 0;
  109. }
  110. //
  111. int mb_read(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt, int timeout)
  112. {
  113. int r=-1,sz=0;
  114. mb_handle_t *mh=(mb_handle_t*)h;
  115. if(!mh || !data || cnt<=0) {
  116. return -1;
  117. }
  118. #if 0
  119. rt_tick_t end = rt_tick_get_millisecond()+timeout;
  120. while(1) {
  121. sz = list_size(mh->rx);
  122. if(sz>0) {
  123. break;
  124. }
  125. rt_thread_mdelay(1);
  126. if(timeout>0 && rt_tick_get_millisecond()>end) {
  127. sz = list_size(mh->rx);
  128. break;
  129. }
  130. }
  131. if(sz>0) {
  132. list_node_t *ln=NULL;
  133. r = list_take_node(mh->rx, &ln, 0);
  134. if(r==0) {
  135. node_t *nd=&ln->data;
  136. if(len>=nd->dlen) {
  137. memcpy(data, nd->buf, nd->dlen);
  138. }
  139. else {
  140. r = -1;
  141. }
  142. list_back_node(mh->rx, ln);
  143. }
  144. }
  145. #else
  146. mb_set_tmo(mh->hinst, timeout, 50);
  147. mb_set_slave(mh->hinst, addr);
  148. sz = mb_read_regs(mh->hinst, reg, cnt, data);
  149. if(sz==cnt) {
  150. r = 0;
  151. }
  152. #endif
  153. return r;
  154. }
  155. int mb_write(void *h, uint8_t addr, uint16_t reg, uint16_t *data, int cnt)
  156. {
  157. int r=-1,sz;
  158. mb_handle_t *mh=(mb_handle_t*)h;
  159. if(!mh || !data || cnt<=0) {
  160. return -1;
  161. }
  162. #if 0
  163. r = list_append(mh->tx, 0, data, len);
  164. #else
  165. mb_set_slave(mh->hinst, addr);
  166. r = mb_write_regs(mh->hinst, reg, cnt, data);
  167. if(sz==cnt) {
  168. r = 0;
  169. }
  170. #endif
  171. return r;
  172. }