Parcourir la source

try to make sniffing more robust by sniffing one channel set at a time

Chris Mullins il y a 8 ans
Parent
commit
b7a94c85a8

+ 43 - 0
lib/MiLight/MiLightClient.cpp

@@ -270,4 +270,47 @@ MiLightRadioType MiLightClient::getRadioType(const String& typeName) {
   } else {
     return UNKNOWN;
   }
+}
+
+const MiLightRadioConfig& MiLightClient::getRadioConfig(const String& typeName) {
+  switch (getRadioType(typeName)) {
+    case RGBW:
+      return MilightRgbwConfig;
+    case CCT:
+      return MilightCctConfig;
+    case RGBW_CCT:
+      return MilightRgbwCctConfig;
+    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");
+  }
 }

+ 3 - 0
lib/MiLight/MiLightClient.h

@@ -108,6 +108,9 @@ 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;

+ 11 - 0
lib/MiLight/MiLightRadio.cpp

@@ -62,6 +62,8 @@ int MiLightRadio::configure() {
 
 bool MiLightRadio::available()
 {
+  configure();
+  
   if (_waiting) {
 #ifdef DEBUG_PRINTF
   printf("_waiting\n");
@@ -70,13 +72,22 @@ bool MiLightRadio::available()
   }
   
   if (_pl1167.receive(config.channels[0]) > 0) {
+#ifdef DEBUG_PRINTF
+  printf("0");
+#endif
     size_t packet_length = sizeof(_packet);
     if (_pl1167.readFIFO(_packet, packet_length) < 0) {
       return false;
     }
+#ifdef DEBUG_PRINTF
+  printf("1");
+#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++;

+ 2 - 0
lib/MiLight/MiLightRadio.h

@@ -16,6 +16,8 @@
 #include "AbstractPL1167.h"
 #include <MiLightRadioConfig.h>
 
+#define DEBUG_PRINTF
+
 #ifndef MILIGHTRADIO_H_
 #define MILIGHTRADIO_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>