function getIpFromMac($mac, &$s20Table) { $msg = MAGIC_KEY . "XXXX" . SEARCH_IP . $mac . TWENTIES; $msg = adjustMsgSize($msg); $s = createSocketAndSendMsg($msg, IP_BROADCAST); $loopCount = 0; $retIp = ""; $recIP = ""; $recPort = 0; while (1) { $n = @socket_recvfrom($s, $binRecMsg, BUFFER_SIZE, 0, $recIP, $recPort); $now = time(); if (!isset($n) || $n == 0) { if (++$loopCount > 3) { error_log("No reply in getIpFromMac after 3 null msg received: " . $mac . "\n"); break; } sendHexMsg($s, $msg, IP_BROADCAST); continue; } $recMsg = hex_byte2str($binRecMsg, $n); if ($recMsg == $msg) { continue; } if ($n >= 42) { if (substr($recMsg, 0, 4) == MAGIC_KEY && substr($recMsg, 8, 4) == SEARCH_IP) { $macRec = substr($recMsg, 14, 12); if ($macRec == $mac) { $s20Table[$mac]['time'] = getSocketTime($recMsg); $s20Table[$mac]['serverTime'] = time(); $retIp = $recIP; break; } } } if ($loopCount > 3) { sendHexMsg($s, $msg, IP_BROADCAST); } if (++$loopCount > 6) { error_log("No reply in getIpFromMac after 6 non-null msg received: " . $mac . "\n"); break; } } socket_close($s); return $retIp; }
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); $now = time(); if ($n == 0) { if (++$loop_count > 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 " they 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); $s20Table[$mac] = array(); $s20Table[$mac]['ip'] = $recIP; $s20Table[$mac]['st'] = $status; $s20Table[$mac]['imac'] = invMac($mac); $s20Table[$mac]['time'] = getSocketTime($recMsg); $s20Table[$mac]['serverTime'] = $now; } } } socket_close($s); return $s20Table; }