LCOV - code coverage report
Current view: top level - AP_HAL_Linux - TCPServerDevice.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 65 0.0 %
Date: 2026-09-09 11:02:08 Functions: 0 9 0.0 %

          Line data    Source code
       1             : #include "TCPServerDevice.h"
       2             : 
       3             : #include <errno.h>
       4             : #include <stdio.h>
       5             : #include <stdlib.h>
       6             : #include <unistd.h>
       7             : 
       8             : #include <AP_HAL/AP_HAL.h>
       9             : 
      10             : extern const AP_HAL::HAL& hal;
      11             : 
      12           0 : TCPServerDevice::TCPServerDevice(const char *ip, uint16_t port, bool wait):
      13           0 :     _ip(ip),
      14           0 :     _port(port),
      15           0 :     _wait(wait)
      16             : {
      17           0 : }
      18             : 
      19           0 : TCPServerDevice::~TCPServerDevice()
      20             : {
      21           0 :     if (sock != nullptr) {
      22           0 :         delete sock;
      23           0 :         sock = nullptr;
      24             :     }
      25           0 : }
      26             : 
      27           0 : ssize_t TCPServerDevice::write(const uint8_t *buf, uint16_t n)
      28             : {
      29           0 :     if (sock == nullptr) {
      30           0 :         return -1;
      31             :     }
      32           0 :     return sock->send(buf, n);
      33             : }
      34             : 
      35             : /*
      36             :   when we try to read we accept new connections if one isn't already
      37             :   established
      38             :  */
      39           0 : ssize_t TCPServerDevice::read(uint8_t *buf, uint16_t n)
      40             : {
      41           0 :     if (sock == nullptr) {
      42           0 :         sock = listener.accept(0);
      43           0 :         if (sock != nullptr) {
      44           0 :             sock->set_blocking(_blocking);
      45             :         }
      46             :     }
      47           0 :     if (sock == nullptr) {
      48           0 :         return -1;
      49             :     }
      50           0 :     ssize_t ret = sock->recv(buf, n, 1);
      51           0 :     if (ret == 0) {
      52             :         // EOF, go back to waiting for a new connection
      53           0 :         delete sock;
      54           0 :         sock = nullptr;
      55           0 :         return -1;
      56             :     }
      57           0 :     return ret;
      58             : }
      59             : 
      60           0 : bool TCPServerDevice::open()
      61             : {
      62           0 :     listener.reuseaddress();
      63             : 
      64           0 :     if (!listener.bind(_ip, _port)) {
      65           0 :         if (AP_HAL::millis() - _last_bind_warning > 5000) {
      66           0 :             ::printf("bind failed on %s port %u - %s\n",
      67             :                      _ip,
      68           0 :                      _port,
      69           0 :                      strerror(errno));
      70           0 :             _last_bind_warning = AP_HAL::millis();
      71             :         }
      72           0 :         return false;
      73             :     }
      74             : 
      75           0 :     if (!listener.listen(1)) {
      76           0 :         if (AP_HAL::millis() - _last_bind_warning > 5000) {
      77           0 :             ::printf("listen failed on %s port %u - %s\n",
      78             :                      _ip,
      79           0 :                      _port,
      80           0 :                      strerror(errno));
      81           0 :             _last_bind_warning = AP_HAL::millis();
      82             :         }
      83           0 :         return false;
      84             :     }
      85             : 
      86           0 :     listener.set_blocking(false);
      87             : 
      88           0 :     if (_wait) {
      89           0 :         ::printf("Waiting for connection on %s:%u ....\n",
      90           0 :                  _ip, (unsigned)_port);
      91           0 :         ::fflush(stdout);
      92           0 :         while (sock == nullptr) {
      93           0 :             sock = listener.accept(1000);
      94             :         }
      95           0 :         sock->set_blocking(_blocking);
      96           0 :         ::printf("connected\n");
      97           0 :         ::fflush(stdout);
      98             :     }
      99             : 
     100           0 :     return true;
     101             : }
     102             : 
     103           0 : bool TCPServerDevice::close()
     104             : {
     105           0 :     if (sock != nullptr) {
     106           0 :         delete sock;
     107           0 :         sock = nullptr;
     108             :     }
     109           0 :     return true;
     110             : }
     111             : 
     112           0 : void TCPServerDevice::set_blocking(bool blocking)
     113             : {
     114           0 :     _blocking = blocking;
     115           0 :     listener.set_blocking(_blocking);
     116           0 : }
     117             : 
     118           0 : void TCPServerDevice::set_speed(uint32_t speed)
     119             : {
     120           0 : }

Generated by: LCOV version 1.14