bf16-ctrldevice.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "bf16-ctrldevice.h"
  2. #include "miner.h"
  3. #define MAX_TOKENS_ALLOWED 10
  4. char *ctrl_device_name = "ctrl";
  5. static char *d_state_on = "ON";
  6. static char *d_state_off = "OFF";
  7. static char* D_STATE(int state)
  8. {
  9. return (state == 0) ? d_state_off : d_state_on;
  10. }
  11. char* get_ctrl_data(int channel, int state, int fn)
  12. {
  13. char *request = malloc(CTRL_BUFFER_SIZE);
  14. memset(request, 0, CTRL_BUFFER_SIZE);
  15. int index = (state == 0) ? 0 : 1;
  16. switch (fn) {
  17. case F_BUZZER:
  18. sprintf(request, "%s=%s\n", BUZZER, D_STATE(index));
  19. break;
  20. case F_LED1:
  21. sprintf(request, "%s=%s\n", LED_GREEN, D_STATE(index));
  22. break;
  23. case F_LED2:
  24. sprintf(request, "%s=%s\n", LED_RED, D_STATE(index));
  25. break;
  26. case F_BRST:
  27. (channel == 1) ?
  28. sprintf(request, "%s=%s\n", CH1_MSP_RST, D_STATE(index)) :
  29. sprintf(request, "%s=%s\n", CH2_MSP_RST, D_STATE(index));
  30. break;
  31. case F_RST:
  32. (channel == 1) ?
  33. sprintf(request, "%s=%s\n", CH1_SPI_RES, D_STATE(index)) :
  34. sprintf(request, "%s=%s\n", CH2_SPI_RES, D_STATE(index));
  35. break;
  36. case F_BDET:
  37. (channel == 1) ?
  38. sprintf(request, "%s=%s\n", CH1_BRD_DET, D_STATE(get_ch1_det())) :
  39. sprintf(request, "%s=%s\n", CH2_BRD_DET, D_STATE(get_ch2_det()));
  40. break;
  41. case F_STAT:
  42. default:
  43. request[0] = '\n';
  44. }
  45. return request;
  46. }
  47. static int D_VALUE(char *value)
  48. {
  49. if (strcasecmp(value, d_state_on) == 0)
  50. return 1;
  51. if (strcasecmp(value, d_state_off) == 0)
  52. return 0;
  53. return -1;
  54. }
  55. static int8_t D_FUNCTION(uint8_t value, char *cmd)
  56. {
  57. if (strcmp(cmd, BUZZER) == 0)
  58. return set_buzzer(value);
  59. else if (strcmp(cmd, LED_GREEN) == 0)
  60. return set_led_green(value);
  61. else if (strcmp(cmd, LED_RED) == 0)
  62. return set_led_red(value);
  63. else if (strcmp(cmd, CH1_MSP_RST) == 0)
  64. return set_ch1_rst(value);
  65. else if (strcmp(cmd, CH2_MSP_RST) == 0)
  66. return set_ch2_rst(value);
  67. else if (strcmp(cmd, CH1_SPI_RES) == 0)
  68. return set_ch1_spi(value);
  69. else if (strcmp(cmd, CH2_SPI_RES) == 0)
  70. return set_ch2_spi(value);
  71. else
  72. applog(LOG_ERR, "CTRL DEVICE, D_FUNCTION: unknown command: [%s]", cmd);
  73. return -1;
  74. }
  75. static int split_tokens(char* cmd, char token, char** tokens)
  76. {
  77. int len=0;
  78. char *src, *ptr;
  79. src = cmd;
  80. ptr = cmd;
  81. while ((*src != '\n') && (*src != '\r') && (*src != '\0') &&
  82. (len < MAX_TOKENS_ALLOWED)) {
  83. if (*src == token) {
  84. *src = 0;
  85. tokens[len++] = ptr;
  86. ptr = src + 1;
  87. }
  88. src++;
  89. }
  90. *src = 0;
  91. if (src != ptr)
  92. tokens[len++] = ptr;
  93. return len;
  94. }
  95. int8_t ctrl_init(device_t* attr, char *device, uint16_t size)
  96. {
  97. attr->device = device;
  98. attr->mode = 0;
  99. attr->speed = 0;
  100. attr->bits = 0;
  101. attr->size = size;
  102. attr->rx = malloc(size);
  103. attr->tx = malloc(size);
  104. brd_init();
  105. return 0;
  106. }
  107. int8_t ctrl_transfer(device_t *attr)
  108. {
  109. char* cmd = (char*) attr->tx;
  110. char* tokens[MAX_TOKENS_ALLOWED];
  111. int8_t ret = 0;
  112. int len = split_tokens(cmd, ';', tokens);
  113. cmd = (char*) attr->rx;
  114. if (len == 0) {
  115. len += snprintf(cmd + len, attr->size, "%s=%d;", HW_VER, get_hw_ver());
  116. len += snprintf(cmd + len, attr->size, "%s=%s;", BTN_FR, D_STATE(get_btn_fr()));
  117. len += snprintf(cmd + len, attr->size, "%s=%s;", BTN_DISCOVERY, D_STATE(get_btn_discovery()));
  118. len += snprintf(cmd + len, attr->size, "%s=%s;", CH1_DET, D_STATE(get_ch1_det()));
  119. len += snprintf(cmd + len, attr->size, "%s=%s\n",CH2_DET, D_STATE(get_ch2_det()));
  120. attr->datalen = len;
  121. } else {
  122. char* values[2];
  123. int i;
  124. int datalen = 0;
  125. for (i = 0; i < len; i++) {
  126. char *tok = tokens[i];
  127. int n = split_tokens(tok, '=', values);
  128. if (n == 2) {
  129. int state = D_VALUE(values[1]);
  130. if (state != -1) {
  131. applog(LOG_DEBUG, "BF16: CTRL: token applying: %s>%s", values[0], values[1]);
  132. if (strcasecmp(values[0], CH1_BRD_DET) == 0)
  133. datalen += snprintf(cmd + datalen, attr->size, "%s:%s\n", CH1_DET, D_STATE(get_ch1_det()));
  134. else if (strcasecmp(values[0], CH2_BRD_DET) == 0)
  135. datalen += snprintf(cmd + datalen, attr->size, "%s:%s\n", CH2_DET, D_STATE(get_ch2_det()));
  136. else
  137. ret = D_FUNCTION(state, values[0]);
  138. }
  139. }
  140. }
  141. if (datalen == 0) {
  142. datalen = 1;
  143. cmd[0] = '\n';
  144. } else
  145. attr->datalen = datalen;
  146. }
  147. return ret;
  148. }
  149. void ctrl_release(device_t *attr)
  150. {
  151. free(attr->rx);
  152. free(attr->tx);
  153. }