File: COMHelper.cpp

package info (click to toggle)
libapogee3 3.2%2B20221221183454-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,284 kB
  • sloc: cpp: 26,737; sh: 8; makefile: 3
file content (138 lines) | stat: -rw-r--r-- 4,359 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*! 
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright(c) 2011 Apogee Imaging Systems, Inc. 
* \class COMHelper 
* \brief namespace for helping of the new libapogee communicate with the old com interface, especially on camera creation 
* 
*/ 

#include "COMHelper.h" 
#include "AltaIo.h" 
#include "AscentBasedIo.h" 
#include "AspenIo.h" 
#include "apgHelper.h"
#include "helpers.h"
#include "windozeHelpers.h"
#include "CamHelpers.h"
#include <sstream>


//----------------------------------------------
//      GET    CAM        INFO
void COMHelper::GetCamInfo( const std::string & intrfcStr, 
                           const unsigned long idOne, 
                           const unsigned long idTwo,  
                           CamModel::PlatformType & platform, 
                           std::string & addr, uint16_t & frmwrRev,
                           uint16_t & camId )
{
     std::shared_ptr<CameraIo> cam;

     bool IsEthernet = false;
    
     if( 0 == intrfcStr.compare( "ethernet" ) )
     {
            //make address string
            std::stringstream ss;
            ss << ":" << idTwo;
            addr = COMHelper::ULong2IpStr( idOne );
            addr.append( ss.str() );
           
            // TODO get this fixed for Aspen
            // HACK!!!!!! Need to change this to be more 
            // robust when we have more time
            try
            {
                cam = std::shared_ptr<CameraIo>(
                    new AltaIo( CamModel::ETHERNET, addr) );
            }
            catch( std::exception & err )
            {
                 cam = std::shared_ptr<CameraIo>(
                    new AspenIo( CamModel::ETHERNET, addr) );
            }

            IsEthernet = true;
     }
     else if( 0 == intrfcStr.compare( "usb" ) )
     {
            std::stringstream ss;
            ss << idOne;
            addr = ss.str();

            uint16_t usb_enum = static_cast<uint16_t>( idOne & 0x00FF );
            uint16_t vid = 0;
            uint16_t pid = 0;
            windozeHelpers::GetVidAndPid( usb_enum, vid, pid );
            if( UsbFrmwr::ALTA_USB_PID == pid )
            {
                cam = std::shared_ptr<CameraIo>(
                    new AltaIo(CamModel::USB, addr ) );
            }
            else if( UsbFrmwr::ASCENT_USB_PID == pid )
            {
                 cam = std::shared_ptr<CameraIo>(
                    new AscentBasedIo(CamModel::USB, addr ) );
            }
            else if(  UsbFrmwr::ASPEN_USB_PID == pid )
            {
                 cam = std::shared_ptr<CameraIo>(
                    new AspenIo(CamModel::USB, addr ) );
            }
            else
            {
                std::runtime_error except( "COMHelper::GetCamInfo - Invalid pid" );
                throw except;
            }
    }
    else
    {
        apgHelper::throwRuntimeException( __FILE__, 
            "Invalid interface type", __LINE__, Apg::ErrorType_InvalidUsage );
    }

    frmwrRev = cam->GetFirmwareRev();
    camId = cam->GetId();
    platform = CamModel::GetPlatformType( camId, IsEthernet ) ;

}

//----------------------------------------------
//     ULONG       2      IP      STR 
std::string COMHelper::ULong2IpStr( const unsigned long ipAddr )
{
      std::stringstream ss;
      ss << ( (ipAddr >> 24) & 0xFF) << "." << ( (ipAddr >> 16) & 0xFF) << ".";
      ss << ( (ipAddr >> 8) & 0xFF)  << "." << (ipAddr & 0xFF);

      return ss.str();
}

//----------------------------------------------
//      IP STR         2      ULONG       
unsigned long COMHelper::IpStr2ULong( const std::string & ipAddr )
{
    std::vector<std::string> parts = help::MakeTokens( ipAddr, "." );

    std::vector<std::string>::iterator iter;
    int shift = 24;
    unsigned long result = 0;

    for( iter = parts.begin(); iter != parts.end(); ++iter, shift -= 8 )
    {
        //cannot use unsigned char, because the stringstream only converts
        //the first char of the input string...not 100% why, ushort type works the
        //way i woud expect.
        uint16_t num = 0;

        std::stringstream ss( (*iter) );
        ss >> num;

        result |= ( num << shift );
    }

    return result;
}