Example #1
0
int SonosEsp::discoverSonos(){
    _numberOfDevices=0;
    WiFiUDP Udp;
    Udp.begin(1900);
    IPAddress sonosIP;
    bool timedOut = false;
    unsigned long timeLimit = 15000;
    unsigned long firstSearch = millis();
    do {
        Serial.println("Sending M-SEARCH multicast");
        Udp.beginPacketMulticast(IPAddress(239, 255, 255, 250), 1900, WiFi.localIP());
        Udp.write("M-SEARCH * HTTP/1.1\r\n"
        "HOST: 239.255.255.250:1900\r\n"
        "MAN: \"ssdp:discover\"\r\n"
        "MX: 1\r\n"
        "ST: urn:schemas-upnp-org:device:ZonePlayer:1\r\n");
        Udp.endPacket();
        unsigned long lastSearch = millis();

        while((millis() - lastSearch) < 5000){
            int packetSize = Udp.parsePacket();
            if(packetSize){
                char packetBuffer[255];
                //Serial.print("Received packet of size ");
                //Serial.println(packetSize);
                //Serial.print("From ");
                sonosIP = Udp.remoteIP();

                //xxx if new IP, it should be put in an array

                addIp(sonosIP);
                //found = true; 
                Serial.print(sonosIP);
                Serial.print(", port ");
                Serial.println(Udp.remotePort());
                
                // read the packet into packetBufffer
                int len = Udp.read(packetBuffer, 255);
                if (len > 0) {
                    packetBuffer[len] = 0;
                }
                //Serial.println("Contents:");
                //Serial.println(packetBuffer);
            }
            delay(50);
        }
    } while((millis()-firstSearch)<timeLimit);
    //if (!found) {
      //sonosIP.fromString("0.0.0.0"); xxx 
    //}
    return _numberOfDevices;
}
Example #2
0
void Protocol::loop()
{
  _packetSize = Udp.parsePacket();

  if (_packetSize) {
    now = millis();

    char _packetBuffer[PACKET_SIZE] = {}; // UDP_TX_PACKET_MAX_SIZE is too large: 8192

    Udp.read(_packetBuffer, _packetSize);

    #ifdef MODULE_CAN_DEBUG
    _remoteIP    = Udp.remoteIP();
    _remotePort  = Udp.remotePort();

    Serial.print("New packet received from: ");
    Serial.print(_remoteIP);
    Serial.print(":");
    Serial.println(_remotePort);
    Serial.print("Message: ");
    Serial.println(_packetBuffer);
    #endif

    _lastTalkTime = now;

    if (strcmp(_packetBuffer, "hi") == 0) {
      _isConnected = true;

      _onConnectedCb();
    } else if (strcmp(_packetBuffer, "bye") == 0) {
      _isConnected = false;

      _onDisconnectedCb();
    } else if (strcmp(_packetBuffer, "ping") == 0) {
      send("ping");
    } else {
      _onMessageCb(String(_packetBuffer));
    }
  } else if (_isConnected) {
    now = millis();

    if(now - _lastTalkTime > TIMEOUT) {
      _isConnected = false;

      _onDisconnectedCb();
    }
  }
}
Example #3
0
// Check UDP for incoming packets
void check_incoming() {
  int packetSize;
  int i;
  int empty = 0;
  int found = -1;

  packetSize = udp.parsePacket();
  if (packetSize) {
    IPAddress remote = udp.remoteIP();
    // read the packet into packetBufffer
    udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    #ifdef DEBUG
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    for (i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(udp.remotePort());
    Serial.print("Dump: ");
    for (i = 0; i < packetSize / sizeof(uint16_t); i++) {
      Serial.print(*((uint16_t *) (packetBuffer + i * sizeof(uint16_t))));
      Serial.print(" ");
    }
    Serial.println();
    #endif

    // Master sends all packets to its clients and updates cache
    if (my_node_type == MSGMULTI_MASTER) {
      found = -1;
      empty = 0;
      send_packet(packetBuffer, packetSize, remote);
      for (i = 0; i < MSGMULTI_MAXCLIENTS; i++) {
        if (clients[i].client == remote) {
          found = i;
          break;
        }
        if (clients[i].expire <= 0)
          empty = i;
      }
      if (found >= 0)
        clients[found].expire = 1024;
      else {
        clients[empty].client = remote;
        clients[empty].expire = 1024;
      }
    }

    // Process incoming statuses
    for (i = 0; i < *((uint16_t *) packetBuffer); i++) {
      receive_status((struct msgrecord *) (packetBuffer + i * sizeof(struct msgrecord) + sizeof(uint16_t)));
    }

    // Repeat incoming check to process all packets in queue
    check_incoming();
  }

  // Check if we have to repeat last sent statuses
  resend_status();
} // void check_incoming()