@@ -11,124 +11,77 @@ namespace mesh::connection {
1111 * Base abstract class of ST2110Rx. ST2110_20Rx/ST2110_22Rx/ST2110_30Rx
1212 * inherit this class.
1313 */
14- template <typename FRAME, typename HANDLE, typename OPS> class ST2110Rx : public ST2110 {
14+ template <typename FRAME, typename HANDLE, typename OPS>
15+ class ST2110Rx : public ST2110 <FRAME, HANDLE, OPS> {
1516 public:
16- ST2110Rx () { _kind = Kind::receiver; }
17-
18- ~ST2110Rx () {
19- shutdown (_ctx);
20- if (ops.name )
21- free ((void *)ops.name );
22- }
17+ ST2110Rx () { this ->_kind = Kind::receiver; }
2318
2419 protected:
25- HANDLE mtl_session = nullptr ;
26- OPS ops = {0 };
27- size_t transfer_size = 0 ;
2820 std::jthread frame_thread_handle;
29- context::Context _ctx = context::WithCancel(context::Background());
30-
31- virtual FRAME *get_frame (HANDLE) = 0;
32- virtual int put_frame (HANDLE, FRAME *) = 0;
33- virtual HANDLE create_session (mtl_handle, OPS *) = 0;
34- virtual int close_session (HANDLE) = 0;
3521
3622 int configure_common (context::Context& ctx, const std::string& dev_port,
37- const MeshConfig_ST2110& cfg_st2110) {
38- int session_id = 0 ;
39- mtl_device = get_mtl_device (dev_port, MTL_LOG_LEVEL_CRIT, cfg_st2110.local_ip_addr , session_id);
40- if (!mtl_device) {
41- log::error (" Failed to get MTL device" );
42- return -1 ;
43- }
44-
45- inet_pton (AF_INET, cfg_st2110.remote_ip_addr , ops.port .ip_addr [MTL_PORT_P]);
46- inet_pton (AF_INET, cfg_st2110.local_ip_addr , ops.port .mcast_sip_addr [MTL_PORT_P]);
47- ops.port .udp_port [MTL_PORT_P] = cfg_st2110.local_port ;
48-
49- strlcpy (ops.port .port [MTL_PORT_P], dev_port.c_str (), MTL_PORT_MAX_LEN);
50- ops.port .num_port = 1 ;
23+ const MeshConfig_ST2110& cfg_st2110) override {
24+ ST2110<FRAME, HANDLE, OPS>::configure_common (ctx, dev_port, cfg_st2110);
5125
52- char session_name[NAME_MAX] = " " ;
53- snprintf (session_name, NAME_MAX, " mcm_mtl_rx_%d" , session_id);
54- if (ops.name )
55- free ((void *)ops.name );
56- ops.name = strdup (session_name);
57- ops.framebuff_cnt = 4 ;
58-
59- ops.priv = this ; // app handle register to lib
60- ops.notify_frame_available = frame_available_cb;
26+ inet_pton (AF_INET, cfg_st2110.remote_ip_addr , this ->ops .port .ip_addr [MTL_PORT_P]);
27+ inet_pton (AF_INET, cfg_st2110.local_ip_addr , this ->ops .port .mcast_sip_addr [MTL_PORT_P]);
28+ this ->ops .port .udp_port [MTL_PORT_P] = cfg_st2110.local_port ;
6129
6230 log::info (" ST2110Rx: configure" )
63- (" port" , ops.port .port [MTL_PORT_P])
64- (" ip_addr" , std::to_string (ops.port .ip_addr [MTL_PORT_P][0 ]) + " " +
65- std::to_string (ops.port .ip_addr [MTL_PORT_P][1 ]) + " " +
66- std::to_string (ops.port .ip_addr [MTL_PORT_P][2 ]) + " " +
67- std::to_string (ops.port .ip_addr [MTL_PORT_P][3 ]))
68- (" mcast_sip_addr" , std::to_string (ops.port .mcast_sip_addr [MTL_PORT_P][0 ]) + " " +
69- std::to_string (ops.port .mcast_sip_addr [MTL_PORT_P][1 ]) + " " +
70- std::to_string (ops.port .mcast_sip_addr [MTL_PORT_P][2 ]) + " " +
71- std::to_string (ops.port .mcast_sip_addr [MTL_PORT_P][3 ]))
72- (" num_port" , ops.port .num_port )
73- (" udp_port" , ops.port .udp_port [MTL_PORT_P])
74- (" name" , ops.name )
75- (" framebuff_cnt" , ops.framebuff_cnt );
76-
31+ (" ip_addr" , std::to_string (this ->ops .port .ip_addr [MTL_PORT_P][0 ]) + " " +
32+ std::to_string (this ->ops .port .ip_addr [MTL_PORT_P][1 ]) + " " +
33+ std::to_string (this ->ops .port .ip_addr [MTL_PORT_P][2 ]) + " " +
34+ std::to_string (this ->ops .port .ip_addr [MTL_PORT_P][3 ]))
35+ (" mcast_sip_addr" , std::to_string (this ->ops .port .mcast_sip_addr [MTL_PORT_P][0 ]) + " " +
36+ std::to_string (this ->ops .port .mcast_sip_addr [MTL_PORT_P][1 ]) + " " +
37+ std::to_string (this ->ops .port .mcast_sip_addr [MTL_PORT_P][2 ]) + " " +
38+ std::to_string (this ->ops .port .mcast_sip_addr [MTL_PORT_P][3 ]));
7739 return 0 ;
7840 }
7941
8042 Result on_establish (context::Context& ctx) override {
81- _ctx = context::WithCancel (ctx);
82- init_frame_available ();
83-
84- mtl_session = create_session (mtl_device, &ops);
85- if (!mtl_session) {
86- log::error (" Failed to create session" );
87- set_state (ctx, State::closed);
88- return set_result (Result::error_general_failure);
43+ Result res = ST2110<FRAME, HANDLE, OPS>::on_establish (ctx);
44+ if (res != Result::success) {
45+ return res;
8946 }
9047
9148 /* Start MTL session thread. */
9249 try {
9350 frame_thread_handle = std::jthread (&ST2110Rx::frame_thread, this );
9451 } catch (const std::system_error& e) {
9552 log::error (" Failed to create thread" );
96- set_state (ctx, State::closed);
97- return set_result (Result::error_out_of_memory);
53+ this -> set_state (ctx, State::closed);
54+ return this -> set_result (Result::error_out_of_memory);
9855 }
9956
100- set_state (ctx, State::active);
101- return set_result (Result::success);
57+ this -> set_state (ctx, State::active);
58+ return this -> set_result (Result::success);
10259 }
10360
10461 Result on_shutdown (context::Context& ctx) override {
105- _ctx.cancel ();
106- notify_frame_available ();
62+ Result res = ST2110<FRAME, HANDLE, OPS>::on_shutdown (ctx);
63+ if (res != Result::success) {
64+ return res;
65+ }
10766
10867 frame_thread_handle.join ();
10968
110- if (mtl_session) {
111- close_session (mtl_session);
112- mtl_session = nullptr ;
113- }
114- set_state (ctx, State::closed);
115- return set_result (Result::success);
69+ this ->set_state (ctx, State::closed);
70+ return this ->set_result (Result::success);
11671 };
11772
118- virtual void on_delete (context::Context& ctx) override {}
119-
12073 private:
12174 void frame_thread () {
122- while (!_ctx.cancelled ()) {
75+ while (!this -> _ctx .cancelled ()) {
12376 // Get full buffer from MTL
124- FRAME *frame_ptr = get_frame (mtl_session);
77+ FRAME *frame_ptr = this -> get_frame (this -> mtl_session );
12578 if (frame_ptr) {
12679 // Forward buffer to emulated receiver
127- transmit (_ctx, get_frame_data_ptr (frame_ptr), transfer_size);
80+ this -> transmit (this -> _ctx , get_frame_data_ptr (frame_ptr), this -> transfer_size );
12881 // Return used buffer to MTL
129- put_frame (mtl_session, frame_ptr);
82+ this -> put_frame (this -> mtl_session , frame_ptr);
13083 } else {
131- wait_frame_available ();
84+ this -> wait_frame_available ();
13285 }
13386 }
13487 }
0 commit comments