You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
4.6 KiB

  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  17. * Copyright (C) 2021 Nuno Goncalves <nunojpg@gmail.com>
  18. */
  19. #include <fcntl.h>
  20. #include <termios.h>
  21. #include <libubox/ulog.h>
  22. #include <libubox/ustream.h>
  23. #include <libubox/utils.h>
  24. #include <libubox/uloop.h>
  25. #include <libubus.h>
  26. static struct ustream_fd stream;
  27. static struct ubus_auto_conn conn;
  28. static struct blob_buf b;
  29. struct Battery
  30. {
  31. float temperature;
  32. uint16_t cycles;
  33. uint8_t soc;
  34. bool charging;
  35. bool set;
  36. } battery;
  37. static bool
  38. process(char *read)
  39. {
  40. if (read[0] != '{' ||
  41. read[1] != 'O' ||
  42. read[2] != 'K' ||
  43. read[3] != '}' ||
  44. read[4] != ',')
  45. return false;
  46. const char *from = read + 5;
  47. char *to;
  48. battery.soc = strtoul(from, &to, 10);
  49. if (from == to)
  50. return false;
  51. from = to + 1;
  52. battery.temperature = strtoul(from, &to, 10) / 10.0f;
  53. if (from == to)
  54. return false;
  55. if (to[0] != ',' || (to[1] != '0' && to[1] != '1') || to[2] != ',')
  56. return false;
  57. battery.charging = to[1] == '1';
  58. from = to + 3;
  59. battery.cycles = strtoul(from, &to, 10);
  60. if (from == to)
  61. return false;
  62. return true;
  63. }
  64. static int
  65. consume(struct ustream *s, char **a)
  66. {
  67. char *eol = strstr(*a, "\n");
  68. if (!eol)
  69. return -1;
  70. *eol++ = '\0';
  71. battery.set = process(*a);
  72. if (!battery.set)
  73. ULOG_ERR("failed to parse message from serial: %s", a);
  74. ustream_consume(s, eol - *a);
  75. *a = eol;
  76. return 0;
  77. }
  78. static void
  79. msg_cb(struct ustream *s, int bytes)
  80. {
  81. int len;
  82. char *a = ustream_get_read_buf(s, &len);
  83. while (!consume(s, &a))
  84. ;
  85. }
  86. static void
  87. notify_cb(struct ustream *s)
  88. {
  89. if (!s->eof)
  90. return;
  91. ULOG_ERR("tty error, shutting down\n");
  92. exit(-1);
  93. }
  94. static int
  95. serial_open(char *dev)
  96. {
  97. const int tty = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
  98. if (tty < 0)
  99. {
  100. ULOG_ERR("%s: device open failed: %s\n", dev, strerror(errno));
  101. return -1;
  102. }
  103. struct termios config;
  104. tcgetattr(tty, &config);
  105. cfmakeraw(&config);
  106. cfsetispeed(&config, B9600);
  107. cfsetospeed(&config, B9600);
  108. tcsetattr(tty, TCSANOW, &config);
  109. stream.stream.string_data = true;
  110. stream.stream.notify_read = msg_cb;
  111. stream.stream.notify_state = notify_cb;
  112. ustream_fd_init(&stream, tty);
  113. tcflush(tty, TCIFLUSH);
  114. return 0;
  115. }
  116. static struct uloop_timeout serial_query_timer;
  117. static void
  118. serial_query_handler(struct uloop_timeout *timeout)
  119. {
  120. const char cmd[] = "{ \"mcu_status\": \"1\" }\n";
  121. const unsigned cmd_len = sizeof(cmd) - 1;
  122. ustream_write(&stream.stream, cmd, cmd_len, false);
  123. uloop_timeout_set(&serial_query_timer, 3000); // timeout in 3 sec
  124. uloop_timeout_add(timeout);
  125. }
  126. static int
  127. battery_info(struct ubus_context *ctx, struct ubus_object *obj,
  128. struct ubus_request_data *req, const char *method,
  129. struct blob_attr *msg)
  130. {
  131. blob_buf_init(&b, 0);
  132. if (!battery.set)
  133. {
  134. blobmsg_add_u8(&b, "error", 1);
  135. }
  136. else
  137. {
  138. blobmsg_add_u16(&b, "soc", battery.soc);
  139. blobmsg_add_u8(&b, "charging", battery.charging);
  140. blobmsg_add_double(&b, "temperature", battery.temperature);
  141. blobmsg_add_u16(&b, "cycles", battery.cycles);
  142. }
  143. ubus_send_reply(ctx, req, b.head);
  144. return UBUS_STATUS_OK;
  145. }
  146. static const struct ubus_method battery_methods[] = {
  147. UBUS_METHOD_NOARG("info", battery_info),
  148. };
  149. static struct ubus_object_type battery_object_type =
  150. UBUS_OBJECT_TYPE("battery", battery_methods);
  151. static struct ubus_object battery_object = {
  152. .name = "battery",
  153. .type = &battery_object_type,
  154. .methods = battery_methods,
  155. .n_methods = ARRAY_SIZE(battery_methods),
  156. };
  157. static void
  158. ubus_connect_handler(struct ubus_context *ctx)
  159. {
  160. int ret;
  161. ret = ubus_add_object(ctx, &battery_object);
  162. if (ret)
  163. fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
  164. }
  165. int
  166. main(int argc, char **argv)
  167. {
  168. uloop_init();
  169. conn.path = NULL;
  170. conn.cb = ubus_connect_handler;
  171. ubus_auto_connect(&conn);
  172. if (serial_open("/dev/ttyUSB0") < 0)
  173. return -1;
  174. serial_query_timer.cb = serial_query_handler;
  175. serial_query_handler(&serial_query_timer);
  176. uloop_run();
  177. uloop_done();
  178. return 0;
  179. }