function searchS20() { // // This function searchs for all S20 in a local network // through a broadcast call // and returns an associative array $s20Table indexed // by each S20 mac adress. Each array position is itself // an associative array which contains // // $s20Table[$mac)['ip'] - IP adresss // $s20Table[$mac)['st'] - current S20 status (ON=1,OFF=0) // $s20Table[$mac)['imac'] - Inverted mac,not strictly required, // computed just once for sake of efficiency. // // Note that $mac and is represented as a sequence of hexadecimals // without the usual separators; for example, ac:cf:23:34:e2:b8 is represented // as "accf2334e2b8". // // An additional field $s20Table[$mac]['name'] is later added // to each entry with the name assigned to each device. // This is done in a specific function since it requires a separate // request to each S20 (see function getName() and updNamesAndTimerAfterOn below). // // Returns the $s20Table array // // echo "Searching S20<p>"; $s = createSocketAndSendMsg(DISCOVERY_MSG, IP_BROADCAST); $recIP = ""; $recPort = 0; $s20Table = array(); $loop_count = 0; while (1) { $n = @socket_recvfrom($s, $binRecMsg, BUFFER_SIZE, 0, $recIP, $recPort); if ($n == 0) { if (++$loop_count > 3) { if (count($s20Table) == 0) { error_log("Giving up searching for sockets"); echo "<h1>Internal server error (see logs)</h1>"; exit(1); } else { break; } } sendHexMsg($s, DISCOVERY_MSG, IP_BROADCAST); continue; } if ($n >= 42) { $recMsg = hex_byte2str($binRecMsg, $n); if (substr($recMsg, 0, 4) == MAGIC_KEY && substr($recMsg, 8, 4) == "7161") { $mac = substr($recMsg, 14, 12); $status = (int) substr($recMsg, -1); $s20Table[$mac] = array(); $s20Table[$mac]['ip'] = $recIP; $s20Table[$mac]['st'] = $status; $s20Table[$mac]['imac'] = invMac($mac); } } } socket_close($s); return $s20Table; }
function searchS20($s20Table) { // // This function searchs for all S20 in a local network // through a broadcast call // and returns an associative array $s20Table indexed // by each S20 mac adress. Each array position is itself // an associative array which contains // // $s20Table[$mac)['ip'] - IP adresss // $s20Table[$mac)['st'] - current S20 status (ON=1,OFF=0) // // Note that $mac and is represented as a sequence of hexadecimals // without the usual separators; for example, ac:cf:23:34:e2:b8 is represented // as "accf2334e2b8". // // An additional field $s20Table[$mac]['name'] is later added // to each entry with the name assigned to each device. // This is done in a specific function since it requires a separate // request to each S20 (see function getName() and updNamesAndTimerAfterOn below). // // Several other fields were later added during application development // // Returns the $s20Table array // $s = createSocketAndSendMsg(DISCOVERY_MSG, IP_BROADCAST); $recIP = ""; $recPort = 0; if (!$s20Table || count($s20Table) == 0) { $s20Table = array(); } $loopCount = 0; while (1) { $n = @socket_recvfrom($s, $binRecMsg, BUFFER_SIZE, 0, $recIP, $recPort); $now = time(); if ($n == 0) { if (++$loopCount > 3) { if (count($s20Table) == 0) { error_log("Giving up searching for sockets"); echo "<h2>No sockets found</h2>\n\n"; echo " Please check if all sockets are on-line and assure that they\n"; echo " are not locked:\n (check WiWo app -> select socket -> more -> advanced).<p>\n\n"; echo " In this version, locked or password protected devices are not supported.\n\n<p>"; exit(1); } else { break; } } sendHexMsg($s, DISCOVERY_MSG, IP_BROADCAST); continue; } if ($n >= 42) { $recMsg = hex_byte2str($binRecMsg, $n); if (substr($recMsg, 0, 4) == MAGIC_KEY && substr($recMsg, 8, 4) == "7161") { $mac = substr($recMsg, 14, 12); $status = (int) substr($recMsg, -1); if (!array_key_exists($mac, $s20Table)) { $s20Table[$mac] = array(); $s20Table[$mac]['imac'] = invMac($mac); $s20Table[$mac]['next'] = NUMBER_OF_NEXT_ACTIONS_DISPLAYED_IN_MAIN_PAGE; } $s20Table[$mac]['ip'] = $recIP; $s20Table[$mac]['st'] = $status; $s20Table[$mac]['time'] = getSocketTime($recMsg); $s20Table[$mac]['serverTime'] = $now; } } } socket_close($s); return $s20Table; }