summaryrefslogtreecommitdiff
path: root/src/Serial.cpp
blob: 5183aa8f4878d8d35894e8e12a9364c6214b2f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// -*- mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
// ***** BEGIN LICENSE BLOCK *****
//////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2014 RALOVICH, Kristóf                            //
//                                                                      //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 3 of the License, or    //
// (at your option) any later version.                                  //
//                                                                      //
// This program is distributed in the hope that it will be useful,      //
// but WITHOUT ANY WARRANTY; without even the implied warranty of       //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        //
// GNU General Public License for more details.                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
// ***** END LICENSE BLOCK *****


#include "Serial.hpp"
#include "SerialUsb.hpp"
#include "SerialTty.hpp"
#include <fstream>
#include "Log.hpp"

#ifdef __linux__
# include <sys/utsname.h>
#endif

namespace antpm {



Serial*
Serial::instantiate(void*)
{
#ifdef __linux__
  utsname u;
  int urv = uname(&u);
  if(urv==0)
  {
    LOG(LOG_DBG) << "Running under: " << u.sysname << ", " << u.release << ", " << u.version << ", " << u.machine << "\n";
  }

  // check for cp210x kernel module
  std::string line;
  std::ifstream mods("/proc/modules");
  bool cp210x_found=false;
  bool usbserial_found=false;
  bool usbs_simple_found=false;
  if(!mods.is_open())
    LOG(LOG_WARN) << "Could not open /proc/modules!\n";
  else
  {
    while (mods.good())
    {
      getline(mods,line);
      if(line.find("cp210x ")==0)
        cp210x_found = true;
      if(line.find("usbserial ")==0)
        usbserial_found = true;
      if(line.find("usb_serial_simple ")==0)
        usbs_simple_found = true;
    }
    mods.close();
    if(cp210x_found)
      LOG(LOG_DBG) << "Found loaded cp210x kernel module.\n";
    else
      LOG(LOG_DBG) << "cp210x is not listed among loaded kernel modules.\n";
    if(usbserial_found)
      LOG(LOG_DBG) << "Found loaded usbserial kernel module.\n";
    else
      LOG(LOG_DBG) << "usbserial is not listed among loaded kernel modules.\n";
    if(usbs_simple_found)
      LOG(LOG_DBG) << "Found loaded usb_serial_simple kernel module.\n";
    else
      LOG(LOG_DBG) << "usb_serial_simple is not listed among loaded kernel modules.\n";
  }
#endif

  Serial* s = new SerialUsb();
  if(!s)
    return NULL;
#ifdef _WIN32
  if(!s->open())
  {
    delete s;
    return NULL;
  }
#else
  if(!s->open())
  {
    delete s;
    s = new SerialTty();
    if(!s)
      return NULL;
    if(!s->open())
    {
      delete s;
      return NULL;
    }
  }
#endif
  return s;
}


}