00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "m_serial.h"
00023 #include "controller.h"
00024 #include "protostructs.h"
00025 #include "endian.h"
00026 #include "debug.h"
00027 #include <stdexcept>
00028
00029 namespace Barry { namespace Mode {
00030
00031
00032
00033
00034 Serial::Serial( Controller &con,
00035 DeviceDataCallback callback,
00036 void *callback_context)
00037 : m_con(con)
00038 , m_ModeSocket(0)
00039 , m_CtrlSocket(0)
00040 , m_callback(callback)
00041 , m_callback_context(callback_context)
00042 {
00043 if( !m_con.HasQueue() )
00044 throw std::logic_error("A SocketRoutingQueue is required in the Controller class when using Mode::Serial.");
00045 }
00046
00047 Serial::~Serial()
00048 {
00049 }
00050
00051
00052
00053
00054
00055 void Serial::DataCallback(void *context, Data *data)
00056 {
00057 ddout("Serial::DataCallback called");
00058
00059 Serial *ser = (Serial*) context;
00060
00061 if( data->GetSize() <= 4 )
00062 return;
00063
00064
00065 if( ser->m_callback ) {
00066 (*ser->m_callback)(ser->m_callback_context,
00067 data->GetData() + 4,
00068 data->GetSize() - 4);
00069 }
00070
00071
00072
00073
00074 }
00075
00076 void Serial::CtrlCallback(void *context, Data *data)
00077 {
00078
00079
00080
00081 ddout("CtrlCallback received:\n" << *data);
00082 }
00083
00084
00085
00086
00087 void Serial::Open(const char *password)
00088 {
00089 if( m_ModeSocket ) {
00090 m_data->Close();
00091 m_data.reset();
00092 m_ModeSocket = 0;
00093 }
00094
00095 if( m_CtrlSocket ) {
00096 m_ctrl->Close();
00097 m_ctrl.reset();
00098 m_CtrlSocket = 0;
00099 }
00100
00101 m_ModeSocket = m_con.SelectMode(Controller::UsbSerData);
00102 m_data = m_con.m_zero.Open(m_ModeSocket, password);
00103
00104 m_CtrlSocket = m_con.SelectMode(Controller::UsbSerCtrl);
00105 m_ctrl = m_con.m_zero.Open(m_CtrlSocket, password);
00106
00107
00108 m_data->RegisterInterest(DataCallback, this);
00109 m_ctrl->RegisterInterest(CtrlCallback, this);
00110
00111 const unsigned char start[] =
00112 { 0, 0, 0x0a, 0, 0x01, 0x01, 0xc2, 0x00, 0x40, 0x00 };
00113 Data block(start, sizeof(start));
00114 m_ctrl->Send(block);
00115 }
00116
00117 void Serial::Close()
00118 {
00119 ddout("Serial:: Closing connection.");
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 void Serial::Write(const Data &data, int timeout)
00147 {
00148 if( data.GetSize() <= 0 )
00149 return;
00150
00151 if( !m_data.get() )
00152 throw std::logic_error("Must call Open() before Write() in Mode::Serial");
00153
00154
00155 Data &filtered = m_filter.Write(data, 4);
00156
00157
00158 unsigned char *buf = filtered.GetBuffer();
00159 MAKE_PACKETPTR_BUF(spack, buf);
00160 spack->size = htobs(filtered.GetSize());
00161
00162
00163 m_data->Send(filtered, timeout);
00164 }
00165
00166 }}
00167