function SetTcpData($Imei, $TcpData) { $Len = strlen($TcpData); if ($Len > 15) { $db = ord($TcpData[0]); if ($db >= 0x80 && $db <= 0x85) { $b = true; $s = 0; } else { if ($db <= 0x63) { $t = ord($TcpData[6]); if ($t >= 0x80 && $t <= 0x85) { $IMEI = sprintf('%02d%03d%03d%03d%03d', $db, ord($TcpData[1]) << 2 | ord($TcpData[2]) >> 6, (ord($TcpData[2]) & 0x3f) << 4 | ord($TcpData[3]) >> 4, (ord($TcpData[3]) & 0xf) << 6 | ord($TcpData[4]) >> 2, (ord($TcpData[4]) & 0x3) << 8 | ord($TcpData[5])); $c = 0; for ($i = 0; $i < 14; $i += 2) { $d = $IMEI[$i + 1] << 1; $c += $IMEI[$i] + (int) ($d / 10) + $d % 10; } $IMEI = sprintf('%s%d', $IMEI, (10 - $c % 10) % 10); $b = true; $s = 6; } } } if (isset($b)) { if (!isset($IMEI)) { if (!CheckImeiCrc($Imei)) { $this->Clear(); $this->mLastError = "CGPS::SetTcpData() provided IMEI invalid while required for TcpData without one"; return 0; } } else { $Imei = $IMEI; } $this->maParts = array(); for ($i = $s;; $i = $N) { if ($i < $Len) { switch ($t = ord($TcpData[$i])) { case 0x80: case 0x82: case 0x84: $N = ++$i + 33; if ($Len < $N) { break; } $this->maParts[] = bin2hex(substr($TcpData, $i, 33)); if ($t == 0x80) { continue 2; } $this->mWantAck = $t == 0x84 ? false : true; $this->mImei = $Imei; $p = $N - 34; $this->mCRC = CGPSsettings::CreateCRC($Imei . substr($TcpData, $s, $p - $s) . chr(0x80) . substr($TcpData, $p + 1, $N - $p - 1)); $this->SelectDataPart(0); return $N; case 0x81: case 0x83: case 0x85: $N = ++$i + 15; if ($Len < $N) { break; } $this->achanh(bin2hex(substr($TcpData, $i, 15))); if ($t == 0x81) { continue 2; } $this->mWantAck = $t == 0x85 ? false : true; $this->mImei = $Imei; $p = $N - 16; $this->mCRC = CGPSsettings::CreateCRC($Imei . substr($TcpData, $s, $p - $s) . chr(0x81) . substr($TcpData, $p + 1, $N - $p - 1)); $this->SelectDataPart(0); return $N; default: if (!count($this->maParts)) { $this->Clear(); } else { $this->SelectDataPart(0); } $this->mImei = $Imei; $this->mLastError = 'CGPS::SetTcpData() unsupported type for data part ' . count($this->maParts); return 0; } } $this->mImei = $Imei; $this->SelectDataPart(0); $this->mLastError = 'CGPS::SetTcpData() TcpData too short for data part ' . count($this->maParts); return 0; } } else { $l = strpos($TcpData, "\n"); $r = strpos($TcpData, "\r"); if ($r !== false && $r < $l) { $l = $r; } if ($l !== false) { if ($db > 57 || $db < 48) { $b = $this->SetHttpData($Imei . substr($TcpData, 0, $l)); } else { $b = $this->SetHttpData(substr($TcpData, 0, $l)); } if ($b) { if ($TcpData[$l] == "\r") { return -($l + 1); } return $l + 1; } $this->mLastError = 'CGPS::SetTcpData() forward ' . $this->mLastError; return 0; } } } $this->Clear(); $this->mLastError = "CGPS::SetTcpData() Invalid TcpData"; return 0; }