Fork of the espurna firmware for `mhsw` switches
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.

116 lines
3.1 KiB

  1. /*
  2. TUYA MODULE
  3. Copyright (C) 2019 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  4. */
  5. #pragma once
  6. #include "tuya_dataframe.h"
  7. #include "tuya_types.h"
  8. #include "tuya_transport.h"
  9. namespace Tuya {
  10. // 2 known Data Protocols:
  11. //
  12. // id type len value
  13. // 0x?? 0x01 0x00 0x01 0x00 - bool (0 for false, 1 for true)
  14. // 0x?? 0x02 0x00 0x04 0x00 0x00 0x00 0x00 - int
  15. //
  16. // Note: 'id' varies between devices. instead, matching DP in sequence with local controllers
  17. // Note: 'int' type is mostly used for dimmer and while it is 4 byte value,
  18. // only the first byte is used (i.e. value is between 0 and 255)
  19. Type dataType(const DataFrame& frame) {
  20. if (!frame.length) return Type::UNKNOWN;
  21. const Type type = static_cast<Type>(frame[1]);
  22. switch (type) {
  23. case Type::BOOL:
  24. if (frame.length != 5) break;
  25. if (frame[3] != 0x01) break;
  26. return type;
  27. case Type::INT:
  28. if (frame.length != 8) break;
  29. if (frame[3] != 0x04) break;
  30. return type;
  31. default:
  32. return Type::UNKNOWN;
  33. }
  34. return Type::UNKNOWN;
  35. }
  36. // Since we know of the type only at runtime, specialize the protocol container
  37. template <typename T>
  38. class DataProtocol {
  39. public:
  40. DataProtocol(const uint8_t id, const T value) :
  41. _id(id), _value(value)
  42. {}
  43. DataProtocol(const DataFrame& frame);
  44. uint8_t id() const { return _id; }
  45. T value() const { return _value; }
  46. std::vector<uint8_t> serialize();
  47. private:
  48. uint8_t _id;
  49. T _value;
  50. };
  51. template <typename T>
  52. DataProtocol<T>::DataProtocol(const DataFrame& frame) {
  53. static_assert(sizeof(T) != sizeof(T), "No constructor yet for this type!");
  54. }
  55. template <>
  56. DataProtocol<bool>::DataProtocol(const DataFrame& frame) {
  57. auto data = frame.cbegin();
  58. _id = data[0],
  59. _value = data[4];
  60. }
  61. template <>
  62. DataProtocol<uint32_t>::DataProtocol(const DataFrame& frame) {
  63. auto data = frame.cbegin();
  64. _id = data[0];
  65. _value = static_cast<uint32_t>(data[4] << 24)
  66. | static_cast<uint32_t>(data[5] << 16)
  67. | static_cast<uint32_t>(data[6] << 8)
  68. | static_cast<uint32_t>(data[7]);
  69. }
  70. template <>
  71. std::vector<uint8_t> DataProtocol<bool>::serialize() {
  72. return std::vector<uint8_t> {
  73. _id, static_cast<uint8_t>(Type::BOOL), 0x00, 0x01,
  74. static_cast<uint8_t>(_value)
  75. };
  76. }
  77. template <>
  78. std::vector<uint8_t> DataProtocol<uint32_t>::serialize() {
  79. return std::vector<uint8_t> {
  80. _id, static_cast<uint8_t>(Type::INT), 0x00, 0x04,
  81. static_cast<uint8_t>((_value >> 24) & 0xff),
  82. static_cast<uint8_t>((_value >> 16) & 0xff),
  83. static_cast<uint8_t>((_value >> 8) & 0xff),
  84. static_cast<uint8_t>(_value & 0xff)
  85. };
  86. }
  87. }