more implementation
This commit is contained in:
@@ -46,7 +46,14 @@ class CoreParser {
|
||||
return self::parseBatteryAndStorage($payload);
|
||||
case CoreProtocol::RESP_CODE_NO_MORE_MESSAGES:
|
||||
return (object)['code'=>CoreProtocol::RESP_CODE_NO_MORE_MESSAGES];
|
||||
case CoreProtocol::RESP_CODE_ADVERT_PATH:
|
||||
return self::parseAdvertPathResponse($payload);
|
||||
case CoreProtocol::RESP_CODE_STATS:
|
||||
return self::parseCodeStatusResponse($payload);
|
||||
case CoreProtocol::RESP_CODE_DEVICE_INFO:
|
||||
return self::parseDeviceInfoResponse($payload);
|
||||
default:
|
||||
echo "Unparsed response: " . \Menking\Meshcore\Util\Debug::hexDump($payload) . "\n";
|
||||
return $payload;
|
||||
}
|
||||
}
|
||||
@@ -284,6 +291,68 @@ class CoreParser {
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function parseAdvertPathResponse(string $payload) {
|
||||
$info = [
|
||||
'code'=>ord($payload[0]),
|
||||
'received_timestamp'=>unpack('V', substr($payload, 1, 4))[1],
|
||||
'path_len'=>ord($payload[5]),
|
||||
'path'=>substr($payload, 6, ord($payload[5]))
|
||||
];
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function parseCodeStatusResponse(string $payload) {
|
||||
$info = [
|
||||
'code'=>ord($payload[0]),
|
||||
'type'=>ord($payload[1]),
|
||||
];
|
||||
|
||||
switch($info['type']) {
|
||||
case CoreProtocol::STATS_TYPE_CORE:
|
||||
$info['battery_mv'] = unpack('v', substr($payload, 2, 2))[1];
|
||||
$info['uptime_secs'] = unpack('V', substr($payload, 4, 4))[1];
|
||||
$info['err_flags'] = unpack('v', substr($payload, 8, 2))[1];
|
||||
$info['queue_len'] = ord($payload[10]);
|
||||
break;
|
||||
case CoreProtocol::STATS_TYPE_RADIO:
|
||||
$info['noise_floor'] = unpack('v', substr($payload, 2, 2))[1];
|
||||
$info['last_rssi'] = (ord($payload[4]) >= 128) ? (ord($payload[4]) - 256): ord($payload[4]);
|
||||
$info['last_snr'] = ord($payload[5]);
|
||||
$info['tx_air_secs'] = unpack('V', substr($payload, 6, 4))[1];
|
||||
$info['rx_air_secs'] = unpack('V', substr($payload, 10, 4))[1];
|
||||
break;
|
||||
case CoreProtocol::STATS_TYPE_PACKETS:
|
||||
$info['recv'] = unpack('V', substr($payload, 2, 4))[1];
|
||||
$info['sent'] = unpack('V', substr($payload, 6, 4))[1];
|
||||
$info['n_sent_flood'] = unpack('V', substr($payload, 10, 4))[1];
|
||||
$info['n_sent_direct'] = unpack('V', substr($payload, 14, 4))[1];
|
||||
$info['n_recv_flood'] = unpack('V', substr($payload, 18, 4))[1];
|
||||
$info['n_recv_direct'] = unpack('V', substr($payload, 22, 4))[1];
|
||||
$info['n_rec_errors'] = unpack('V', substr($payload, 26, 4))[1];
|
||||
break;
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function parseDeviceInfoResponse(string $payload) {
|
||||
$info = [
|
||||
'code'=>ord($payload[0]),
|
||||
'fw_ver_code'=>ord($payload[1]),
|
||||
'max_contacts'=>ord($payload[2]) * 2,
|
||||
'max_group_channels'=>ord($payload[3]),
|
||||
'ble_pin'=>unpack('V', substr($payload, 4, 4))[1],
|
||||
'fw_build_date'=>trim(substr($payload, 8, 12)),
|
||||
'manufacturer_name'=>trim(substr($payload, 20, 40)),
|
||||
'fw_version'=>trim(substr($payload, 60, 20)),
|
||||
'client_repeat'=>ord($payload[80]),
|
||||
'path_hash_mode'=>ord($payload[81])
|
||||
];
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public static function parseMessage(string $payload) {
|
||||
$data = [
|
||||
'code'=>ord($payload[0]),
|
||||
|
||||
+73
-15
@@ -157,32 +157,87 @@ class CoreProtocol {
|
||||
const CMD_GET_CONTACTS = 4;
|
||||
const CMD_GET_DEVICE_TIME = 5;
|
||||
const CMD_SET_DEVICE_TIME = 6;
|
||||
const CMD_SEND_SELF_ADVERT = 7;
|
||||
const CMD_SET_ADVERT_NAME = 8;
|
||||
const CMD_ADD_UPDATE_CONTACT = 9;
|
||||
const CMD_SYNC_NEXT_MESSAGE = 10;
|
||||
const CMD_SET_RADIO_PARAMS = 11;
|
||||
const CMD_SET_RADIO_TX_POWER = 12;
|
||||
const CMD_RESET_PATH = 13;
|
||||
const CMD_SET_ADVERT_LATLON = 14;
|
||||
const CMD_REMOVE_CONTACT = 15;
|
||||
const CMD_SHARE_CONTACT = 16;
|
||||
const CMD_EXPORT_CONTACT = 17;
|
||||
const CMD_IMPORT_CONTACT = 18;
|
||||
const CMD_REBOOT = 19;
|
||||
const CMD_GET_BATT_AND_STORAGE = 20;
|
||||
const CMD_GET_BATT_AND_STORAGE = 20; // was CMD_GET_BATTERY_VOLTAGE
|
||||
const CMD_SET_TUNING_PARAMS = 21;
|
||||
const CMD_DEVICE_QUERY = 22;
|
||||
const CMD_LOGIN = 26;
|
||||
const CMD_EXPORT_PRIVATE_KEY = 23;
|
||||
const CMD_IMPORT_PRIVATE_KEY = 24;
|
||||
const CMD_SEND_RAW_DATA = 25;
|
||||
const CMD_SEND_LOGIN = 26;
|
||||
const CMD_SEND_STATUS_REQ = 27;
|
||||
const CMD_HAS_CONNECTION = 28;
|
||||
const CMD_LOGOUT = 29;
|
||||
const CMD_LOGOUT = 29; // 'Disconnect'
|
||||
const CMD_GET_CONTACT_BY_KEY = 30;
|
||||
const CMD_GET_CHANNEL = 31;
|
||||
const CMD_SEND_TELEMETRY_REQ = 39;
|
||||
const CMD_SEND_BINARY_REQ = 50; // 0x32
|
||||
const CMD_SEND_ANON_REQ = 57;
|
||||
const CMD_SET_CHANNEL = 32;
|
||||
const CMD_SIGN_START = 33;
|
||||
const CMD_SIGN_DATA = 34;
|
||||
const CMD_SIGN_FINISH = 35;
|
||||
const CMD_SEND_TRACE_PATH = 36;
|
||||
const CMD_SET_DEVICE_PIN = 37;
|
||||
const CMD_SET_OTHER_PARAMS = 38;
|
||||
const CMD_SEND_TELEMETRY_REQ = 39; // can deprecate this
|
||||
const CMD_GET_CUSTOM_VARS = 40;
|
||||
const CMD_SET_CUSTOM_VAR = 41;
|
||||
const CMD_GET_ADVERT_PATH = 42;
|
||||
const CMD_GET_TUNING_PARAMS = 43;
|
||||
const CMD_SEND_BINARY_REQ = 50;
|
||||
const CMD_FACTORY_RESET = 51;
|
||||
const CMD_SEND_PATH_DISCOVERY_REQ = 52;
|
||||
const CMD_SET_FLOOD_SCOPE_KEY = 54; // v8+
|
||||
const CMD_SEND_CONTROL_DATA = 55; // v8+
|
||||
const CMD_GET_STATS = 56; // v8+, second byte is stats type
|
||||
const CMD_SEND_ANON_REQ = 57;
|
||||
const CMD_SET_AUTOADD_CONFIG = 58;
|
||||
const CMD_GET_AUTOADD_CONFIG = 59;
|
||||
const CMD_GET_ALLOWED_REPEAT_FREQ = 60;
|
||||
const CMD_SET_PATH_HASH_MODE = 61;
|
||||
const CMD_SEND_CHANNEL_DATA = 62;
|
||||
const CMD_SET_DEFAULT_FLOOD_SCOPE = 63;
|
||||
const CMD_GET_DEFAULT_FLOOD_SCOPE = 64;
|
||||
|
||||
const RESP_CODE_OK = 0;
|
||||
const RESP_CODE_ERR = 1;
|
||||
const RESP_CODE_CONTACTS_START = 2;
|
||||
const RESP_CODE_CONTACT = 3;
|
||||
const RESP_CODE_END_OF_CONTACTS = 4;
|
||||
const RESP_CODE_SELF_INFO = 5;
|
||||
const RESP_CODE_SENT = 6;
|
||||
const RESP_CODE_CURR_TIME = 9;
|
||||
const RESP_CODE_NO_MORE_MESSAGES = 10;
|
||||
const RESP_CODE_CONTACTS_START = 2; // first reply to CMD_GET_CONTACTS
|
||||
const RESP_CODE_CONTACT = 3; // multiple of these (after CMD_GET_CONTACTS)
|
||||
const RESP_CODE_END_OF_CONTACTS = 4; // last reply to CMD_GET_CONTACTS
|
||||
const RESP_CODE_SELF_INFO = 5; // reply to CMD_APP_START
|
||||
const RESP_CODE_SENT = 6; // reply to CMD_SEND_TXT_MSG
|
||||
const RESP_CODE_CONTACT_MSG_RECV = 7; // a reply to CMD_SYNC_NEXT_MESSAGE (ver < 3)
|
||||
const RESP_CODE_CHANNEL_MSG_RECV = 8; // a reply to CMD_SYNC_NEXT_MESSAGE (ver < 3)
|
||||
const RESP_CODE_CURR_TIME = 9; // a reply to CMD_GET_DEVICE_TIME
|
||||
const RESP_CODE_NO_MORE_MESSAGES = 10; // a reply to CMD_SYNC_NEXT_MESSAGE
|
||||
const RESP_CODE_EXPORT_CONTACT = 11;
|
||||
const RESP_CODE_BATT_AND_STORAGE = 12;
|
||||
const RESP_CODE_DEVICE_INFO = 13;
|
||||
const RESP_CODE_BATT_AND_STORAGE = 12; // a reply to a CMD_GET_BATT_AND_STORAGE
|
||||
const RESP_CODE_DEVICE_INFO = 13; // a reply to CMD_DEVICE_QEURY
|
||||
const RESP_CODE_PRIVATE_KEY = 14; // a reply to CMD_EXPORT_PRIVATE_KEY
|
||||
const RESP_CODE_DISABLED = 15;
|
||||
const RESP_CODE_CONTACT_MSG_RECV_V3 = 16; // a reply to CMD_SYNC_NEXT_MESSAGE (ver >= 3)
|
||||
const RESP_CODE_CHANNEL_MSG_RECV_V3 = 17; // a reply to CMD_SYNC_NEXT_MESSAGE (ver >= 3)
|
||||
const RESP_CODE_CHANNEL_INFO = 18; // a reply to CMD_GET_CHANNEL
|
||||
const RESP_CODE_SIGN_START = 19;
|
||||
const RESP_CODE_SIGNATURE = 20;
|
||||
const RESP_CODE_CUSTOM_VARS = 21;
|
||||
const RESP_CODE_ADVERT_PATH = 22;
|
||||
const RESP_CODE_TUNING_PARAMS = 23;
|
||||
const RESP_CODE_STATS = 24; // v8+, second byte is stats type
|
||||
const RESP_CODE_AUTOADD_CONFIG = 25;
|
||||
const RESP_ALLOWED_REPEAT_FREQ = 26;
|
||||
const RESP_CODE_CHANNEL_DATA_RECV = 27;
|
||||
const RESP_CODE_DEFAULT_FLOOD_SCOPE = 28;
|
||||
|
||||
const ERR_CODE_UNSUPPORTED_CMD = 1;
|
||||
const ERR_CODE_NOT_FOUND = 2;
|
||||
@@ -231,4 +286,7 @@ class CoreProtocol {
|
||||
const LPP_GYRO = 0x86;
|
||||
const LPP_GPS = 0x88;
|
||||
|
||||
const STATS_TYPE_CORE = 0;
|
||||
const STATS_TYPE_RADIO = 1;
|
||||
const STATS_TYPE_PACKETS = 2;
|
||||
}
|
||||
|
||||
@@ -296,6 +296,30 @@ class Meshcore {
|
||||
return CoreParser::parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $contact_pk
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAdvertPath(string $contact_pk): mixed {
|
||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_ADVERT_PATH) . chr(0x00) . base64_decode($contact_pk));
|
||||
$response = CoreProtocol::readFrame($this->serial);
|
||||
|
||||
return CoreParser::parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param int $type
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStats(int $type): mixed {
|
||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_STATS) . chr($type));
|
||||
$response = CoreProtocol::readFrame($this->serial);
|
||||
|
||||
return CoreParser::parseResponse($response);
|
||||
}
|
||||
|
||||
public function readFrame(): mixed {
|
||||
return CoreProtocol::readFrame($this->serial);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user