Pārlūkot izejas kodu

Merge pull request #11 from sidoh/sniff_single_channelset

Sniff single channelset
Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
f9d885bc0b

+ 47 - 2
lib/MiLight/MiLightClient.cpp

@@ -8,8 +8,8 @@ MiLightRadio* MiLightClient::getRadio(const MiLightRadioType type) {
     stack = rgbwRadio;
   } else if (type == CCT) {
     stack = cctRadio;
-  } else if (type == RGBW_CCT) {
-    stack = rgbwCctRadio;
+  } else if (type == RGB_CCT) {
+    stack = rgbCctRadio;
   }
   
   if (stack != NULL) {
@@ -267,7 +267,52 @@ MiLightRadioType MiLightClient::getRadioType(const String& typeName) {
     return RGBW;
   } else if (typeName.equalsIgnoreCase("cct")) {
     return CCT;
+  } else if (typeName.equalsIgnoreCase("rgb_cct")) {
+    return RGB_CCT;
   } else {
     return UNKNOWN;
   }
+}
+
+const MiLightRadioConfig& MiLightClient::getRadioConfig(const String& typeName) {
+  switch (getRadioType(typeName)) {
+    case RGBW:
+      return MilightRgbwConfig;
+    case CCT:
+      return MilightCctConfig;
+    case RGB_CCT:
+      return MilightRgbCctConfig;
+    default:
+      Serial.print("Unknown radio type: ");
+      Serial.println(typeName);
+      return MilightRgbwConfig;
+  }
+}
+    
+void MiLightClient::formatPacket(MiLightRadioConfig& config, uint8_t* packet, char* buffer) {
+  if (config.type == RGBW || config.type == CCT) {
+    String format = String("Request type  : %02X\n") 
+      + "Device ID     : %02X%02X\n"
+      + "b1            : %02X\n"
+      + "b2            : %02X\n"
+      + "b3            : %02X\n"
+      + "Sequence Num. : %02X";
+      
+    sprintf(
+      buffer,
+      format.c_str(),
+      packet[0],
+      packet[1], packet[2],
+      packet[3],
+      packet[4],
+      packet[5],
+      packet[6]
+    );
+  } else {
+    for (int i = 0; i < config.packetLength; i++) {
+      sprintf(buffer, "%02X ", packet[i]);
+      buffer += 3;
+    }
+    sprintf(buffer, "\n\n");
+  }
 }

+ 7 - 4
lib/MiLight/MiLightClient.h

@@ -47,19 +47,19 @@ class MiLightClient {
     {
       rgbwRadio = new MiLightRadioStack(rf, MilightRgbwConfig);
       cctRadio = new MiLightRadioStack(rf, MilightCctConfig);
-      rgbwCctRadio = new MiLightRadioStack(rf, MilightRgbwCctConfig);
+      rgbCctRadio = new MiLightRadioStack(rf, MilightRgbCctConfig);
     }
     
     ~MiLightClient() {
       delete rgbwRadio;
       delete cctRadio;
-      delete rgbwCctRadio;
+      delete rgbCctRadio;
     }
     
     void begin() {
       rgbwRadio->getRadio()->begin();
       cctRadio->getRadio()->begin();
-      rgbwCctRadio->getRadio()->begin();
+      rgbCctRadio->getRadio()->begin();
     }
     
     void setResendCount(const unsigned int resendCount);
@@ -108,12 +108,15 @@ class MiLightClient {
     
     static uint8_t getCctStatusButton(uint8_t groupId, MiLightStatus status);
     static MiLightRadioType getRadioType(const String& typeName);
+    static const MiLightRadioConfig& getRadioConfig(const String& typeName);
+    
+    void formatPacket(MiLightRadioConfig& config, uint8_t* packet, char* buffer);
     
   private:
     RF24 rf;
     MiLightRadioStack* rgbwRadio;
     MiLightRadioStack* cctRadio;
-    MiLightRadioStack* rgbwCctRadio;
+    MiLightRadioStack* rgbCctRadio;
     MiLightRadioType currentRadio;
     
     uint8_t sequenceNum;

+ 12 - 3
lib/MiLight/MiLightRadio.cpp

@@ -52,7 +52,8 @@ int MiLightRadio::configure() {
     return retval;
   }
 
-  retval = _pl1167.setMaxPacketLength(8);
+  // +1 to be able to buffer the length 
+  retval = _pl1167.setMaxPacketLength(config.packetLength + 1);
   if (retval < 0) {
     return retval;
   }
@@ -60,8 +61,7 @@ int MiLightRadio::configure() {
   return 0;
 }
 
-bool MiLightRadio::available()
-{
+bool MiLightRadio::available() {
   if (_waiting) {
 #ifdef DEBUG_PRINTF
   printf("_waiting\n");
@@ -70,13 +70,22 @@ bool MiLightRadio::available()
   }
   
   if (_pl1167.receive(config.channels[0]) > 0) {
+#ifdef DEBUG_PRINTF
+  printf("MiLightRadio - received packet!\n");
+#endif
     size_t packet_length = sizeof(_packet);
     if (_pl1167.readFIFO(_packet, packet_length) < 0) {
       return false;
     }
+#ifdef DEBUG_PRINTF
+  printf("MiLightRadio - Checking packet length (expecting %d, is %d)\n", _packet[0] + 1U, packet_length);
+#endif
     if (packet_length == 0 || packet_length != _packet[0] + 1U) {
       return false;
     }
+#ifdef DEBUG_PRINTF
+  printf("2");
+#endif
     uint32_t packet_id = PACKET_ID(_packet);
     if (packet_id == _prev_packet_id) {
       _dupes_received++;

+ 5 - 1
lib/MiLight/MiLightRadio.h

@@ -16,6 +16,8 @@
 #include "AbstractPL1167.h"
 #include <MiLightRadioConfig.h>
 
+#define DEBUG_PRINTF
+
 #ifndef MILIGHTRADIO_H_
 #define MILIGHTRADIO_H_
 
@@ -30,12 +32,14 @@ class MiLightRadio {
     int write(uint8_t frame[], size_t frame_length);
     int resend();
     int configure();
+    
   private:
     AbstractPL1167 &_pl1167;
     const MiLightRadioConfig& config;
     uint32_t _prev_packet_id;
 
-    uint8_t _packet[8], _out_packet[8];
+    uint8_t _packet[10];
+    uint8_t _out_packet[10];
     bool _waiting;
     int _dupes_received;
 };

+ 4 - 4
lib/MiLight/MiLightRadioConfig.h

@@ -7,7 +7,7 @@ enum MiLightRadioType {
   UNKNOWN = 0,
   RGBW  = 0xB8,
   CCT   = 0x5A,
-  RGBW_CCT = 0x99
+  RGB_CCT = 0x99
 };
 
 class MiLightRadioConfig {
@@ -44,9 +44,9 @@ static MiLightRadioConfig MilightCctConfig(
   0x050A, 0x55AA, 7, CCT_CHANNELS, 3, CCT
 );
 
-const uint8_t RGBWCCT_CHANNELS[] = {70, 39, 8};
-static MiLightRadioConfig MilightRgbwCctConfig(
-  0x7236, 0x1809, 8, RGBWCCT_CHANNELS, 3, RGBW_CCT
+const uint8_t RGBCCT_CHANNELS[] = {70, 39, 8};
+static MiLightRadioConfig MilightRgbCctConfig(
+  0x7236, 0x1809, 9, RGBCCT_CHANNELS, 3, RGB_CCT
 );
 
 #endif

+ 14 - 3
lib/MiLight/PL1167_nRF24.cpp

@@ -146,12 +146,17 @@ int PL1167_nRF24::receive(uint8_t channel)
   _radio.startListening();
   if (_radio.available()) {
 #ifdef DEBUG_PRINTF
-  printf("Radio is available");
+  printf("Radio is available\n");
 #endif
     internal_receive();
   }
 
   if(_received) {
+#ifdef DEBUG_PRINTF
+  if (_packet_length > 0) {
+    printf("Received packet (len = %d)!\n", _packet_length);
+  }
+#endif
     return _packet_length;
   } else {
     return 0;
@@ -285,7 +290,7 @@ int PL1167_nRF24::internal_receive()
 #ifdef DEBUG_PRINTF
   printf("Packet received: ");
   for (int i = 0; i < _receive_length; i++) {
-    printf("%02X", reverse_bits(tmp[i]));
+    printf("%02X", tmp[i]);
   }
   printf("\n");
 #endif
@@ -360,7 +365,8 @@ int PL1167_nRF24::internal_receive()
     uint16_t crc = calc_crc(tmp, outp - 2);
     if ( ((crc & 0xff) != tmp[outp - 2]) || (((crc >> 8) & 0xff) != tmp[outp - 1]) ) {
 #ifdef DEBUG_PRINTF
-  printf("Failed CRC: expected %d, got (%d,%d)\n", crc, tmp[outp-2], tmp[outp-1]);
+  uint16_t recv_crc = ((tmp[outp - 2] & 0xFF) << 8) | (tmp[outp - 1] & 0xFF);
+  printf("Failed CRC: expected %d, got %d\n", crc, recv_crc);
 #endif
       return 0;
     }
@@ -371,6 +377,11 @@ int PL1167_nRF24::internal_receive()
   
   _packet_length = outp;
   _received = true;
+  
+#ifdef DEBUG_PRINTF
+  printf("Successfully parsed packet of length %d\n", _packet_length);
+#endif
+  
   return outp;
 }
 

+ 2 - 0
lib/MiLight/PL1167_nRF24.h

@@ -12,6 +12,8 @@
 #include "AbstractPL1167.h"
 #include "RF24.h"
 
+#define DEBUG_PRINTF
+
 #ifndef PL1167_NRF24_H_
 #define PL1167_NRF24_H_
 

+ 12 - 19
lib/WebServer/MiLightHttpServer.cpp

@@ -12,7 +12,7 @@ void MiLightHttpServer::begin() {
   server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, "application/json"));
   server.on("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
   server.on("/settings", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(SETTINGS_FILE));
-  server.on("/gateway_traffic", HTTP_GET, [this]() { handleListenGateway(); });
+  server.onPattern("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
   server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_PUT, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
   server.onPattern("/gateways/:device_id/:type", HTTP_PUT, [this](const UrlTokenBindings* b) { handleUpdateGateway(b); });
   server.on("/web", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
@@ -127,39 +127,32 @@ void MiLightHttpServer::handleUpdateSettings() {
   }
 }
 
-void MiLightHttpServer::handleListenGateway() {
-  uint8_t readType = 0;
-  MiLightRadioConfig *config;
+void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
+  bool available = false;
+  MiLightRadioConfig config = milightClient->getRadioConfig(bindings->get("type"));
   
-  while (readType == 0) {
+  while (!available) {
     if (!server.clientConnected()) {
       return;
     }
     
-    if (milightClient->available(RGBW)) {
-      readType = RGBW;
-      config = &MilightRgbwConfig;
-    } else if (milightClient->available(CCT)) {
-      readType = CCT;
-      config = &MilightCctConfig;
-    } else if (milightClient->available(RGBW_CCT)) {
-      readType = RGBW_CCT;
-      config = &MilightRgbwCctConfig;
+    if (milightClient->available(config.type)) {
+      available = true;
     }
     
     yield();
   }
   
-  uint8_t packet[config->packetLength];
-  milightClient->read(static_cast<MiLightRadioType>(readType), packet);
+  uint8_t packet[config.packetLength];
+  milightClient->read(static_cast<MiLightRadioType>(config.type), packet);
   
   String response = "Packet received (";
   response += String(sizeof(packet)) + " bytes)";
   response += ":\n";
   
-  for (int i = 0; i < sizeof(packet); i++) {
-    response += String(packet[i], HEX) + " ";
-  }
+  char ppBuffer[200];
+  milightClient->formatPacket(config, packet, ppBuffer);
+  response += String(ppBuffer);
   
   response += "\n\n";
   

+ 1 - 1
lib/WebServer/MiLightHttpServer.h

@@ -32,7 +32,7 @@ protected:
   void applySettings(Settings& settings);
   
   void handleUpdateSettings();
-  void handleListenGateway();
+  void handleListenGateway(const UrlTokenBindings* urlBindings);
   void handleUpdateGroup(const UrlTokenBindings* urlBindings);
   void handleUpdateGateway(const UrlTokenBindings* urlBindings);
   

+ 17 - 1
web/index.html

@@ -119,7 +119,9 @@
     var sniffRequest;
     var sniffing = false;
     var getTraffic = function() {
-      sniffRequest = $.get('/gateway_traffic', function(data) {
+      var sniffType = $('#sniff-type input:checked').data('value');
+      
+      sniffRequest = $.get('/gateway_traffic/' + sniffType, function(data) {
         $('#sniffed-traffic').html(data + $('#sniffed-traffic').html());
         getTraffic();
       });
@@ -588,6 +590,20 @@
       <div class="col-sm-12">
         <button type="button" id="sniff" class="btn btn-primary">Start Sniffing</button>
         
+        <div class="btn-group" id="sniff-type" data-toggle="buttons">
+          <label class="btn btn-secondary active">
+            <input type="radio" name="options" autocomplete="off" data-value="rgbw" checked> RGBW
+          </label>
+          <label class="btn btn-secondary">
+            <input type="radio" name="options" autocomplete="off" data-value="cct"> CCT
+          </label>
+          <label class="btn btn-secondary">
+            <input type="radio" name="options" autocomplete="off" data-value="rgbw_cct"> RGBW+CCT
+          </label>
+        </div>
+        
+        <div> &nbsp; </div>
+        
         <pre id="sniffed-traffic"></pre>
       </div>
     </div>