mb.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "mb.h"
  2. #include "list.h"
  3. #define MALLOC malloc
  4. #define FREE free
  5. typedef struct {
  6. mb_inst_t *h;
  7. uint8_t mode;
  8. uint8_t type;
  9. }mb_conn_t;
  10. typedef struct {
  11. mb_conn_t rtu;
  12. mb_conn_t tcp;
  13. rt_thread_t tid;
  14. uint8_t quit;
  15. handle_t rx;
  16. handle_t tx;
  17. }mb_handle_t;
  18. static mb_handle_t mbHandle={0};
  19. typedef void (*thread_fn_t)(void *arg);
  20. static void mb_handle(mb_conn_t *c)
  21. {
  22. if(c->mode==MB_MODE_MASTER) {
  23. }
  24. else {
  25. mb_slave_fsm(c->h);
  26. }
  27. }
  28. static void mb_thread(void *arg)
  29. {
  30. mb_handle_t *h=(mb_handle_t*)arg;
  31. while(h->quit==0) {
  32. mb_handle(&h->rtu);
  33. //mb_handle(&h->tcp);
  34. }
  35. mb_destory(h->rtu.h);
  36. mb_destory(h->tcp.h);
  37. h->quit = 2;
  38. }
  39. static void mb_start(mb_handle_t *h, int flag)
  40. {
  41. if(flag) {
  42. h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
  43. if(h->tid==NULL) {
  44. //LOG_E("___ thread create failed\n");
  45. }
  46. h->quit = 0;
  47. rt_thread_startup(h->tid);
  48. }
  49. else {
  50. h->quit = 1;
  51. while(h->quit!=2);
  52. rt_thread_delete(h->tid);
  53. }
  54. }
  55. void* mb_init(mb_para_t *para)
  56. {
  57. list_cfg_t lc={0,LIST_FULL_FIFO,10};
  58. mb_handle_t *h=NULL;
  59. if(!para) {
  60. return NULL;
  61. }
  62. h = (mb_handle_t*)MALLOC(sizeof(mb_handle_t));
  63. if(h) {
  64. memset(h, 0, sizeof(mb_handle_t));
  65. h->rx = list_init(&lc);
  66. h->tx = list_init(&lc);
  67. mb_setup(h, para);
  68. mb_start(h, 1);
  69. }
  70. return h;
  71. }
  72. int mb_setup(void *h, mb_para_t *para)
  73. {
  74. mb_backend_param_t param={0};
  75. mb_handle_t *mh=(mb_handle_t*)h;
  76. if(!mh || !para) {
  77. return -1;
  78. }
  79. if(para->type==MB_TYPE_RTU) {
  80. if(mh->rtu.h) {
  81. mb_disconn(mh->rtu.h);
  82. mb_destory(mh->rtu.h);
  83. }
  84. param.rtu = para->para.rtu;
  85. mh->rtu.type = (para->type==MB_TYPE_RTU)?MB_BACKEND_TYPE_RTU:MB_BACKEND_TYPE_TCP;
  86. mh->rtu.mode = para->mode;
  87. mh->rtu.h = mb_create(mh->rtu.type, &param);
  88. if(!mh->rtu.h) {
  89. return -1;
  90. }
  91. }
  92. else if(para->type==MB_TYPE_TCP) {
  93. }
  94. else {
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. int mb_deinit(void *h)
  100. {
  101. mb_handle_t *mh=(mb_handle_t*)h;
  102. if(!mh) {
  103. return -1;
  104. }
  105. mb_start(mh, 0);
  106. list_free(mh->rx);
  107. list_free(mh->tx);
  108. FREE(mh);
  109. return 0;
  110. }
  111. //
  112. int mb_read(void *h, uint8_t addr, uint16_t reg, void *data, int len, int timeout)
  113. {
  114. int r=-1,sz=0;
  115. mb_handle_t *mh=(mb_handle_t*)h;
  116. if(!mh || !data || len<=0) {
  117. return -1;
  118. }
  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. return r;
  146. }
  147. int mb_write(void *h, uint8_t addr, uint16_t reg, void *data, int len)
  148. {
  149. mb_handle_t *mh=(mb_handle_t*)h;
  150. if(!mh) {
  151. return -1;
  152. }
  153. return list_append(mh->tx, 0, data, len);
  154. }