function setSwitchOffTimer($mac, $sec, &$s20Table) { // // Sets the automatic switch off timer in table 4 to $sec // subscribe($mac, $s20Table); $table = 4; $vflag = "17"; $recTable = getTable($mac, $table, $vflag, $s20Table); $switchOffData = substr($recTable, 164 * 2, 8); // This substring defines the format for // "automatic switch off after switch on" according to the following // // XXYYZZZZ // // XX, Enabled satus: XX=00 Disabled,XX=01 - Enabled // YY, Action to be performed after switch on: 00-turn off, 01-turn on // ZZZZ Initial countdown in seconds, little endian // // Not sure what is the purpose of YY=01 (switch on after switch on), // but we have seen this configuration ocasionaly, possibly as // a side effect of some other operation. // $switchOffData = ($sec == 0 ? "00" : "01") . "00"; $switchOffData = $switchOffData . secToHexLE($sec); // Set timer $newTableAux = substr_replace($recTable, $switchOffData, 2 * 164, 8); // replace receive code with send code $newTableAux = substr_replace($newTableAux, WRITE_SOCKET_CODE, 2 * 4, 4); // // Delete byte 18 (??) // Wireshark shows... // $newTable = substr_replace($newTableAux, "", 18 * 2, 2); // // Delete byte 25 and 26 of resulting (??) // Wireshark shows... // $newTable = substr_replace($newTable, "", 25 * 2, 4); // Update msg size, just in case it has changed $newTable = adjustMsgSize($newTable); $reply = createSocketSendHexMsgWaitReply($mac, $newTable, $s20Table); $newSec = getSwitchOffTimer($mac, $s20Table); return $newSec; }
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; }