Example #1
0
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;
}
Example #2
0
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;
}
Example #3
0
function getNameOld($mac, $s20Table)
{
    //
    // Returns the registered name in S20 specified by the mac $mac.
    // Uses previous device information available in $s20Table.
    //
    $ip = $s20Table[$mac]['ip'];
    subscribe($mac, $s20Table);
    $getSocketData = "6864001D7274" . $mac . TWENTIES . "0000000004001700000000";
    $s = createSocketAndSendMsg($getSocketData, $ip);
    $recIp = "";
    $recPort = 0;
    $stay = 1;
    $loop_count = 0;
    while ($stay) {
        if (++$loop_count > MAX_RETRIES) {
            echo "<h1> Error: too many retries without successfull reply in getName()</h1>\n";
            exit(0);
        }
        $n = @socket_recvfrom($s, $binRecMsg, 168, 0, $recIP, $recPort);
        if ($n == 0) {
            // This is probably due to timeout; retry...
            if (DEBUG) {
                error_log("retrying in getName()" . $loop_count);
            }
            sendHexMsg($s, $getSocketData, $ip);
        } else {
            $recMsg = hex_byte2str($binRecMsg, $n);
            //            print_r( "getName (".$n.")=".$recMsg."\n");
            if ($n == 168) {
                if (substr($recMsg, 0, 4) == MAGIC_KEY) {
                    $rmac = substr($recMsg, 12, 12);
                    if ($rmac == $mac) {
                        $name = substr($binRecMsg, 70, 16);
                        $stay = 0;
                    }
                }
            }
        }
        //        ob_flush();
    }
    if (DEBUG) {
        error_log("Number of retries getName = " . $loop_count . "\n");
    }
    socket_close($s);
    return trim($name);
}