|
|
@ -1,6 +1,43 @@ |
|
|
|
#include "xsync.hpp"
|
|
|
|
#define ENABLE_LOG
|
|
|
|
#ifdef ENABLE_LOG
|
|
|
|
#include "../src/logger.hpp"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TAG "XSYNC"
|
|
|
|
|
|
|
|
using namespace iflytop; |
|
|
|
|
|
|
|
static uint32_t ipToUint32(const std::string &ipAddress, bool &suc) { |
|
|
|
uint32_t result = 0; |
|
|
|
std::istringstream iss(ipAddress); |
|
|
|
std::string segment; |
|
|
|
int i = 0; |
|
|
|
|
|
|
|
while (std::getline(iss, segment, '.')) { |
|
|
|
uint32_t octet = std::stoi(segment); |
|
|
|
if (octet > 255) { |
|
|
|
suc = false; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
result |= (octet << ((3 - i) * 8)); |
|
|
|
i++; |
|
|
|
} |
|
|
|
if (i != 4) { |
|
|
|
suc = false; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
suc = true; |
|
|
|
|
|
|
|
uint32_t result_n = 0; |
|
|
|
result_n |= ((result & 0xff000000) >> 24); |
|
|
|
result_n |= ((result & 0x00ff0000) >> 8); |
|
|
|
result_n |= ((result & 0x0000ff00) << 8); |
|
|
|
result_n |= ((result & 0x000000ff) << 24); |
|
|
|
|
|
|
|
return result_n; |
|
|
|
} |
|
|
|
|
|
|
|
Xsync::Xsync(/* args */) {} |
|
|
|
|
|
|
|
Xsync &Xsync::Ins() { |
|
|
@ -86,7 +123,7 @@ xsync_net_state_t Xsync::getNetState() { return m_net_state; } |
|
|
|
void Xsync::regOnTimecodeMsg(xsync_on_timecode_msg_t on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; } |
|
|
|
void Xsync::regOnCameraSyncMsg(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb) { m_on_camera_sync_msg_cb = on_camera_sync_msg_cb; } |
|
|
|
|
|
|
|
xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize) { |
|
|
|
xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize, int32_t overtime_ms) { |
|
|
|
lock_guard<recursive_mutex> lock(lock_); |
|
|
|
if (!m_xsync_reg_udp) return kxs_ec_lose_connect; |
|
|
|
m_xsync_reg_udp->clearRxBuffer(); |
|
|
@ -101,14 +138,24 @@ xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, |
|
|
|
} |
|
|
|
|
|
|
|
XsyncNetAdd fromadd; |
|
|
|
ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, 100); |
|
|
|
if (ecode != kxs_ec_success) { |
|
|
|
return ecode; |
|
|
|
while (true) { |
|
|
|
// ZLOGI(TAG, "start rx wait for rxdata");
|
|
|
|
ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, overtime_ms); |
|
|
|
// ZLOGI(TAG, "end rx wait for rxdata");
|
|
|
|
if (ecode != kxs_ec_success) { |
|
|
|
return ecode; |
|
|
|
} |
|
|
|
if (rx_data->index != cmd->index) { |
|
|
|
// ZLOGI(TAG, "packet index error %d %d", cmd->index, rx_data->index);
|
|
|
|
continue; |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
return (xs_error_code_t)rx_data->data[0]; |
|
|
|
} |
|
|
|
|
|
|
|
xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue) { |
|
|
|
xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue, int32_t overtime_ms) { |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 协议说明 |
|
|
@ -131,7 +178,7 @@ xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &r |
|
|
|
txpacket->data[0] = regadd; |
|
|
|
txpacket->data[1] = regvalue; |
|
|
|
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata)); |
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms); |
|
|
|
if (ecode != kxs_ec_success) { |
|
|
|
return ecode; |
|
|
|
} |
|
|
@ -140,7 +187,7 @@ xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &r |
|
|
|
return ecode; |
|
|
|
} |
|
|
|
|
|
|
|
xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t ®value) { |
|
|
|
xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t ®value, int32_t overtime_ms) { |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 协议说明 |
|
|
@ -163,14 +210,14 @@ xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t ®value) { |
|
|
|
txpacket->data[0] = regadd; |
|
|
|
txpacket->data[1] = regvalue; |
|
|
|
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata)); |
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms); |
|
|
|
if (ecode != kxs_ec_success) { |
|
|
|
return ecode; |
|
|
|
} |
|
|
|
regvalue = rxpacket->data[1]; |
|
|
|
return ecode; |
|
|
|
} |
|
|
|
xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> ®values) { |
|
|
|
xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> ®values, int32_t overtime_ms) { |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 协议说明 |
|
|
@ -193,7 +240,7 @@ xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint |
|
|
|
txpacket->data[0] = regadd; |
|
|
|
txpacket->data[1] = nreg; |
|
|
|
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata)); |
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms); |
|
|
|
if (ecode != kxs_ec_success) { |
|
|
|
return ecode; |
|
|
|
} |
|
|
@ -212,9 +259,9 @@ void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t l |
|
|
|
xysnc_timecode_t timecode; |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* |
|
|
|
* |
|
|
|
* 01 02 03 04 01 02 03 04 |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
|
|
|
|
uint8_t frameuints = packet->timecode0 & 0x0f; |
|
|
@ -237,10 +284,10 @@ void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t l |
|
|
|
void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) { |
|
|
|
uint32_t count = 0; |
|
|
|
|
|
|
|
uint32_t data0 = data[0]; |
|
|
|
uint32_t data1 = data[1]; |
|
|
|
uint32_t data2 = data[2]; |
|
|
|
uint32_t data3 = data[3]; |
|
|
|
uint32_t data0 = data[7]; |
|
|
|
uint32_t data1 = data[6]; |
|
|
|
uint32_t data2 = data[5]; |
|
|
|
uint32_t data3 = data[4]; |
|
|
|
|
|
|
|
count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24); |
|
|
|
|
|
|
@ -249,3 +296,58 @@ void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t |
|
|
|
|
|
|
|
if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data); |
|
|
|
} |
|
|
|
|
|
|
|
// xsync_stm32_action_generator_new_mac, //
|
|
|
|
// xsync_stm32_action_factory_reset, //
|
|
|
|
// xsync_stm32_action_reboot, //
|
|
|
|
// xsync_stm32_action_storage_cfg,
|
|
|
|
xs_error_code_t Xsync::generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); } |
|
|
|
xs_error_code_t Xsync::factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); } |
|
|
|
xs_error_code_t Xsync::reboot() { return doaction(xsync_stm32_action_reboot, 0, nullptr); } |
|
|
|
xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); } |
|
|
|
xs_error_code_t Xsync::changeNetworkConfig(string ip, string mask, string gateway) { |
|
|
|
uint32_t ip32 = 0; |
|
|
|
uint32_t mask32 = 0; |
|
|
|
uint32_t gateway32 = 0; |
|
|
|
xs_error_code_t ecode; |
|
|
|
bool suc = false; |
|
|
|
|
|
|
|
ip32 = (uint32_t)ipToUint32(ip.c_str(), suc); |
|
|
|
if (!suc) return kxs_ec_param_error; |
|
|
|
mask32 = (uint32_t)ipToUint32(mask.c_str(), suc); |
|
|
|
if (!suc) return kxs_ec_param_error; |
|
|
|
gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc); |
|
|
|
if (!suc) return kxs_ec_param_error; |
|
|
|
|
|
|
|
uint32_t readbak = 0; |
|
|
|
|
|
|
|
ecode = reg_write(kxsync_reg_stm32_ip, ip32, readbak); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
ecode = reg_write(kxsync_reg_stm32_netmask, mask32, readbak); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
ecode = reg_write(kxsync_reg_stm32_gw, gateway32, readbak); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
|
|
|
|
ecode = storageConfig(); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
|
|
|
|
return kxs_ec_success; |
|
|
|
} |
|
|
|
|
|
|
|
xs_error_code_t Xsync::clearXsyncCameraSyncIndexCount() { |
|
|
|
uint32_t readbak = 0; |
|
|
|
return reg_write(kxsync_reg_stm32_camera_sync_signal_count, 0, readbak); |
|
|
|
} |
|
|
|
|
|
|
|
xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) { |
|
|
|
//
|
|
|
|
uint32_t readbak = 0; |
|
|
|
xs_error_code_t ecode; |
|
|
|
ecode = reg_write(kxsync_reg_stm32_action_val0, actionval, readbak); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
|
|
|
|
ecode = reg_write(kxsync_reg_stm32_action0, action, readbak, overtime_ms); |
|
|
|
if (ecode != kxs_ec_success) return ecode; |
|
|
|
if (ackreturn) *ackreturn = readbak; |
|
|
|
return ecode; |
|
|
|
} |