diff --git a/include/ixsync.hpp b/include/ixsync.hpp index e6aa98d..f930b18 100644 --- a/include/ixsync.hpp +++ b/include/ixsync.hpp @@ -18,7 +18,7 @@ using namespace std; typedef function xsync_on_timecode_msg_t; typedef function xsync_on_record_sig_change_msg_t; -typedef function xsync_on_connect_state_change_t; +typedef function xsync_on_connect_state_change_t; typedef function xsync_on_camera_sync_msg_t; typedef struct { @@ -43,22 +43,20 @@ class IXsync { /** * @brief * 注意事项: - * 1. 网络配置,需要在设备重启后生效 - * 2. 设备软重启可以调用reboot方法 - * 3. 只有网络配置是掉电不丢失的,其他配置均掉电丢失 + * 1. 网络配置完成后需要调用storageConfig才会持久化 + * 2. 网络配置,需要在设备重启后生效,设备软重启可以调用reboot方法 */ - // virtual xs_error_code_t NetworkConfig_setMode(NetworkMode_t mode) = 0; - // virtual xs_error_code_t NetworkConfig_getMode(NetworkMode_t &mode) = 0; + virtual xs_error_code_t NetworkConfig_setMode(NetworkMode_t mode) = 0; + virtual xs_error_code_t NetworkConfig_getMode(NetworkMode_t &mode) = 0; - // virtual xs_error_code_t NetworkConfigStaticIpMode_setIp(string ip) = 0; - // virtual xs_error_code_t NetworkConfigStaticIpMode_getIp(string &ip) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_setIp(string ip) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_setMask(string mask) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_setGateway(string gateway) = 0; - // virtual xs_error_code_t NetworkConfigStaticIpMode_setMask(string mask) = 0; - // virtual xs_error_code_t NetworkConfigStaticIpMode_getMask(string &mask) = 0; - - // virtual xs_error_code_t NetworkConfigStaticIpMode_setGateway(string gateway) = 0; - // virtual xs_error_code_t NetworkConfigStaticIpMode_getGateway(string &gateway) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_getIp(string &ip) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_getMask(string &mask) = 0; + virtual xs_error_code_t NetworkConfigStaticIpMode_getGateway(string &gateway) = 0; /******************************************************************************* * 事件回调函数注册 * @@ -76,6 +74,7 @@ class IXsync { virtual xs_error_code_t reboot() = 0; virtual xs_error_code_t readSn(string &sn) = 0; virtual xs_error_code_t readMac(string &mac) = 0; + virtual xs_error_code_t storageConfig() = 0; // virtual xs_error_code_t changeNetworkConfig(string ip, string mask, string gateway) = 0; diff --git a/include/xsync_utils.hpp b/include/xsync_utils.hpp index 57a09ec..97817fb 100644 --- a/include/xsync_utils.hpp +++ b/include/xsync_utils.hpp @@ -18,6 +18,8 @@ namespace xsync { uint32_t ipToUint32(const std::string &ipAddress, bool &suc); +string uint32ToIp(uint32_t ip); + XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64); Timecode64_t timecodeTo64(XsyncTimecode_t tc); diff --git a/include/xsync_v2_sig_type.hpp b/include/xsync_v2_sig_type.hpp index 5981788..05dcec1 100644 --- a/include/xsync_v2_sig_type.hpp +++ b/include/xsync_v2_sig_type.hpp @@ -182,4 +182,5 @@ typedef enum { DHCP_MODE = 1, } NetworkMode_t; + } // namespace xsync diff --git a/src/xsync_utils.cpp b/src/xsync_utils.cpp index 09f5b9c..ca4ebdd 100644 --- a/src/xsync_utils.cpp +++ b/src/xsync_utils.cpp @@ -35,6 +35,18 @@ uint32_t ipToUint32(const std::string &ipAddress, bool &suc) { return result_n; } +string uint32ToIp(uint32_t ip) { + std::string ip_str; + ip_str += std::to_string(ip & 0xff); + ip_str += "."; + ip_str += std::to_string((ip >> 8) & 0xff); + ip_str += "."; + ip_str += std::to_string((ip >> 16) & 0xff); + ip_str += "."; + ip_str += std::to_string((ip >> 24) & 0xff); + return ip_str; +} + XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64) { uint8_t frameuints = tc64.tc0 & 0x0f; uint8_t frame10s = (tc64.tc0 >> 8) & 0x3; diff --git a/src/xsync_v2.cpp b/src/xsync_v2.cpp index becaed0..929da80 100644 --- a/src/xsync_v2.cpp +++ b/src/xsync_v2.cpp @@ -80,7 +80,7 @@ class Xsync : public IXsync { lock_guard lock(connectStatelock_); m_is_connected = connected; m_xsync_ip = ip; - if (m_on_connect_state_change_cb) m_on_connect_state_change_cb(m_xsync_ip, m_is_connected); + if (m_on_connect_state_change_cb) m_on_connect_state_change_cb(m_is_connected, m_xsync_ip); } string _getXsyncIp() { @@ -170,9 +170,9 @@ class Xsync : public IXsync { // 设备枚举线程 device_state_monitor_thread.reset(new thread([this]() { while (!destoryflag) { - int32_t has_not_receive_packet_time_s = (Xsync_GetTicket() - m_last_receive_packet_tp) / 1000 / 1000 / 1000; + int32_t has_not_receive_packet_time_s = (Xsync_GetTicket() - m_last_receive_packet_tp) / 1000 / 1000; // ZLOGI(TAG, "%lld ", has_not_receive_packet_time_s); - if (m_is_connected && (has_not_receive_packet_time_s) > 5) { + if (m_is_connected && (has_not_receive_packet_time_s) > 500) { _setNetworkState(false, ""); } this_thread::sleep_for(chrono::seconds(1)); @@ -199,6 +199,75 @@ class Xsync : public IXsync { virtual bool getConnectState() override { return _getConnectState(); } virtual string getDeviceIp() override { return _getXsyncIp(); } + virtual xs_error_code_t NetworkConfig_setMode(NetworkMode_t mode) { REG_WRITE(reg::kstm32_obtaining_ip_mode, mode); } + virtual xs_error_code_t NetworkConfig_getMode(NetworkMode_t &mode) override { + uint32_t mode_u32; + DO_XSYNC(reg_read(reg::kstm32_obtaining_ip_mode, mode_u32, 10)); + mode = (NetworkMode_t)mode_u32; + return kxs_ec_success; + } + + virtual xs_error_code_t NetworkConfigStaticIpMode_setIp(string ip) override { + xs_error_code_t ecode; + bool suc = false; + uint32_t ip32 = (uint32_t)ipToUint32(ip.c_str(), suc); + if (!suc) return kxs_ec_param_error; + + uint32_t readbak = 0; + + ecode = reg_write(reg::kstm32_ip, ip32, readbak); + if (ecode != kxs_ec_success) return ecode; + + return kxs_ec_success; + } + virtual xs_error_code_t NetworkConfigStaticIpMode_setMask(string mask) override { + xs_error_code_t ecode; + bool suc = false; + uint32_t mask32 = (uint32_t)ipToUint32(mask.c_str(), suc); + if (!suc) return kxs_ec_param_error; + + uint32_t readbak = 0; + + ecode = reg_write(reg::kstm32_netmask, mask32, readbak); + if (ecode != kxs_ec_success) return ecode; + + return kxs_ec_success; + } + virtual xs_error_code_t NetworkConfigStaticIpMode_setGateway(string gateway) override { + xs_error_code_t ecode; + bool suc = false; + uint32_t gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc); + if (!suc) return kxs_ec_param_error; + + uint32_t readbak = 0; + + ecode = reg_write(reg::kstm32_gw, gateway32, readbak); + if (ecode != kxs_ec_success) return ecode; + + return kxs_ec_success; + } + + virtual xs_error_code_t NetworkConfigStaticIpMode_getIp(string &ip) override { + uint32_t ip32; + DO_XSYNC(reg_read(reg::kstm32_ip, ip32, 10)); + ip = uint32ToIp(ip32); + return kxs_ec_success; + } + virtual xs_error_code_t NetworkConfigStaticIpMode_getMask(string &mask) override { + uint32_t mask32; + DO_XSYNC(reg_read(reg::kstm32_netmask, mask32, 10)); + + mask = uint32ToIp(mask32); + return kxs_ec_success; + } + virtual xs_error_code_t NetworkConfigStaticIpMode_getGateway(string &gateway) override { + uint32_t gateway32; + DO_XSYNC(reg_read(reg::kstm32_gw, gateway32, 10)); + + gateway = uint32ToIp(gateway32); + return kxs_ec_success; + } + virtual void registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t cb) override { m_on_timecode_msg_cb = cb; } virtual void registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t cb) override { m_on_camera_sync_msg_cb = cb; } virtual void registerOnRecordSigChangeMsgCallback(xsync_on_record_sig_change_msg_t cb) override { m_on_record_sig_change_msg_cb = cb; } @@ -355,7 +424,18 @@ Xsync &Xsync::Ins() { } xs_error_code_t Xsync::readSn(string &sn) { - sn = "X1001000000000"; + uint32_t sn0; + uint32_t sn1; + uint32_t sn2; + + DO_XSYNC(reg_read(reg::ksn_id0, sn0)); + DO_XSYNC(reg_read(reg::ksn_id1, sn1)); + DO_XSYNC(reg_read(reg::ksn_id2, sn2)); + + char buf[128] = {0}; + sprintf(buf, "%02d%02d%04d", sn0, sn1, sn2); + sn = buf; + return kxs_ec_success; } xs_error_code_t Xsync::readMac(string &mac) { diff --git a/src/xsync_v2_sig_type.cpp b/src/xsync_v2_sig_type.cpp index 5ff3a26..6f83e19 100644 --- a/src/xsync_v2_sig_type.cpp +++ b/src/xsync_v2_sig_type.cpp @@ -211,4 +211,5 @@ list TriggerEdgeStrSet() { } return ret; } + } // namespace xsync