<?php
class LifeXControl {
public $uniq;
public $frame = array(
/** FRAME **/
'size'=>0, //uint16_t
'origin'=>0,//uint8_t
'tagged'=>0, //bit bool
'addressable'=>0,//bit bool
'protocol'=>1024,//uint16 - leave as is
'source'=>0,//unique identifier uint32
);
public $frameAddress = array(
/**FRAME ADDRESS**/
'target'=>0,//uint64_t MAC address or 0 for all
'reserved'=>array(0,0,0,0,0,0), // all zero uint8_t
'reserved2'=>"\x00\x00\x00\x00\x00\x00", //all zeros 6 bytes
'ack_required'=>0, //bit bool
'res_required'=>0,//bit bool
'sequence'=>0, //uint8t unique sequence
);
public $protocolHeader = array (
/** PROTOCOL HEADER **/
'reserved'=>0,//uint64_t
'type'=>0,//uint16_t message type
'reserved2'=>0,//uint64_t
);
public $setColorPayLoad = array(
'reserved'=>0,//uint8_t
'hue'=>0,//uint16
'saturation'=>0,//uint16
'brightness'=>0,//uint16
'kelvin'=>0, //uint16
);
public function packFrameAddress() {
//'target'=>0,//uint64_t MAC address or 0 for all
//'reserved'=>array(0,0,0,0,0,0), // all zero uint8_t
//'reserved2'=>"\x00\x00\x00\x00\x00\x00", //all zeros 6 bytes
//'ack_required'=>0, //bit bool
//'res_required'=>0,//bit bool
//'sequence'=>0, //uint8t unique sequence
//);
//$bits = $bits & 0x000000;
$bits = 0b00000000;
if ($this->frameAddress['res_required']) $bits = $bits ^ 0b00000010;
if ($this->frameAddress['ack_required']) $bits = $bits ^ 0b00000001;
$packedFrame =
pack("CCCCCCCCCCCCCCCC",
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,
$bits,
0
);
return $packedFrame;
}
public function packProtocolHeader() {
$protocolHeader = pack("Pvv",
$this->protocolHeader['reserved'],
$this->protocolHeader['type'],
$this->protocolHeader['reserved2']);
return $protocolHeader;
}
public function packPayload() {
return pack('CvvvvV',
0,
$this->setColorPayLoad['hue'],
$this->setColorPayLoad['saturation'],
$this->setColorPayLoad['brightness'],
$this->setColorPayLoad['kelvin'],
100
);
}
public function constructFrame($frameAddress, $protocolHeader, $payload) {
$otap = 0b00000000000000;
$otap = $otap ^ 0b10000000000000;//tagged
$otap = $otap ^ 0b01000000000000; //addressable
$pnum = $this->frame['protocol'];
$otap = $otap ^ $pnum;
$thisSize = 8 + strlen($frameAddress) + strlen($protocolHeader) + strlen($payload);
//echo $thisSize;
$thisSize -= 12;
$frame = pack('vvV',
$thisSize,
$otap,
$this->uniq
);
return $frame;
}
public function setColor($hue,$sat,$bright,$kel) {
$this->protocolHeader['type']=102;
$this->setColorPayLoad['hue'] = $hue;
$this->setColorPayLoad['saturation'] = $sat;
$this->setColorPayLoad['brightness'] = $bright;
$this->setColorPayLoad['kelvin'] = $kel;
$frameAddress = $this->packFrameAddress();
$protocolHeader = $this->packProtocolHeader();
$payload = $this->packPayload();
$frame = $this->constructFrame($frameAddress,$protocolHeader,$payload);
$packet = $frame . $frameAddress . $protocolHeader . $payload;
/*
$p2 = bin2hex($packet);
$p3 = '\\x';
$p3 .= chunk_split($p2,2,"\\x");
$p3 = rtrim($p3,"\\x");
echo $p3;
exit;
//var_dump($packet);
for ($x = 0; $x<=49; $x++ ) {
echo $packet[$x]."\n";
}
*/
//echo $packet;
//file_put_contents("shit.txt",$packet);
//$packet = file_get_contents("packt.txt");
$sock = fsockopen("udp://192.168.0.7",56700,$errno,$errstr);
fwrite($sock,$packet);
fclose($sock);
}
function __construct($targetIp ) {
$this->uniq = mt_rand(655455,1000000);
$targetIp =0;
$this->frame['source'] = $this->uniq;
$this->frameAddress['target']=$targetIp;
$this->frameAddress['ack_required'] = 0;
$this->frameAddress['res_required'] = 0;
}
}
$test = new LifeXControl(0);
while (1) {
$x=0;
while ( $x < 65535) {
$test->setColor($x,65535,65535,mt_rand(0,9000));
$x+=500;
//echo $x . "\n";
usleep(100000);
}
while ( $x > 0) {
$test->setColor($x,65535,65535,mt_rand(0,9000));
$x-=500;
//echo $x . "\n";
usleep(100000);
}
usleep(30000);
}