LAN_rhk_hwi_dev.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Farid El Gabaly <farid.elgabaly@uam.es>, 
00009  * Juan de la Figuera <juan.delafiguera@uam.es>
00010  * WWW Home: http://gxsm.sf.net
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00025  */
00026 
00027 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00028 
00029 /* irnore this module for docuscan
00030 % PlugInModuleIgnore
00031  */
00032 
00033 // "C++" headers
00034 #include <iostream>
00035 
00036 // "C" headers
00037 #include <cstdio>
00038 #include <cstring>
00039 
00040 // system headers
00041 #include <unistd.h>
00042 #include <fcntl.h>
00043 #include <sys/ioctl.h>
00044 #include <sys/socket.h>
00045 #include <netinet/in.h>
00046 #include <arpa/inet.h>
00047 #include <netdb.h>
00048 #include <poll.h>
00049 
00050 // Gxsm headers
00051 #include "glbvars.h"
00052 
00053 #include "LAN_rhk_hwi.h"
00054 
00055 
00056 // use "gxsm2 --debug-level=5" (5 or higher) to enable debug!
00057 #define INTERNET_DEBUG(S) XSM_DEBUG (DBG_L4, S)
00058 
00059 /* Constructor for Gxsm Internet device support base class
00060  * ==================================================
00061  * - open device
00062  * - init things
00063  * - ...
00064  */
00065 LAN_rhk_hwi_dev::LAN_rhk_hwi_dev(){
00066         char host_name[40];
00067         char *tport;
00068         int port;
00069         struct sockaddr_in pin;
00070         struct hostent *server_host_name;
00071 
00072         strcpy(host_name, xsmres.DSPDev);
00073         tport=strstr(host_name,":");
00074         if (tport==NULL)
00075                 {
00076                         INTERNET_DEBUG("No : in remote address name, assuming 5027");
00077                         port=5027;
00078                 }
00079         else
00080                 {
00081                         tport[0]=0;
00082                         tport++;
00083                         port=atoi(tport);
00084                 }
00085 
00086         INTERNET_DEBUG("open socket to " << host_name << ", port " << port);
00087         max_points_per_line = 262145; // Should be returned by the external program.
00088 
00089         if ((server_host_name = gethostbyname(host_name)) == 0) {
00090                 INTERNET_DEBUG("Error resolving local host");
00091                 exit(1);
00092         }
00093 
00094         bzero(&pin, sizeof(pin));
00095         pin.sin_family = AF_INET;
00096         pin.sin_addr.s_addr = htonl(INADDR_ANY);
00097         pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;
00098         pin.sin_port = htons(port);
00099 
00100         if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00101                 INTERNET_DEBUG("Error opening socket\n");
00102                 exit(1);
00103         }
00104 
00105 
00106         if (connect(socket_descriptor, (struct sockaddr *)&pin, sizeof(pin)) == -1) {
00107                 INTERNET_DEBUG("Error connecting to socket\n");
00108                 exit(1);
00109         }
00110 
00111 }
00112 
00113 /* Destructor
00114  * close device
00115  */
00116 LAN_rhk_hwi_dev::~LAN_rhk_hwi_dev(){
00117         INTERNET_DEBUG("close remote connection");
00118 
00119         close(socket_descriptor);
00120 
00121 }
00122 
00123 int LAN_rhk_hwi_dev::SendCommand(char *Cmd){
00124         int n;
00125         n=strlen(Cmd);
00126         return(write(socket_descriptor, Cmd, n));
00127 }
00128 
00129 size_t LAN_rhk_hwi_dev::ReadData(void *Data, size_t count) {
00130         struct pollfd list[1];
00131 
00132         list[0].fd=socket_descriptor;
00133         list[0].events=POLLIN;
00134         while (!poll(list,1,0)) {
00135                 gapp->check_events();
00136         }
00137         return(recv(socket_descriptor, Data, count,MSG_WAITALL ));
00138 }
00139 
00140 void LAN_rhk_hwi_dev::ExecCmd(int Cmd){
00141         // Exec "DSP" command (additional stuff)
00142         INTERNET_DEBUG("Exec Cmd 0x" << std::hex << Cmd);
00143         // Put CMD to DSP for execution.
00144         // Wait until done and
00145         // call whenever you are waiting longer for sth.:
00146         // gapp->check_events(); // inbetween!
00147 }
00148 
00149 int LAN_rhk_hwi_dev::WaitExec(int data){
00150         return 0; // not needed today, always returns 0
00151 }
00152 
00153 // you may want this later for mover and other tasks...
00154 void LAN_rhk_hwi_dev::SetParameter(PARAMETER_SET &hps, int scanflg){
00155 }
00156 
00157 void LAN_rhk_hwi_dev::GetParameter(PARAMETER_SET &hps){
00158 }
00159 
00160 int LAN_rhk_hwi_dev::ReadScanData(int y_index, int num_srcs, Mem2d *m[MAX_SRCS_CHANNELS]){
00161         static time_t t0 = 0; // only for demo
00162         int len = m[0]->GetNx();
00163         int count;
00164         SHT *linebuffer = new SHT[len];
00165         char txt[20];
00166 
00167         // create dummy data (need to read some stuff from driver/tmp buffer...)
00168         // should use ReadData (see above) later!
00169         // and should not block...
00170 
00171         //   INTERNET_DEBUG("ReadData:" << y_index);
00172 
00173         if (t0 == 0) t0 = time (NULL);
00174         int drift = (int) (time (NULL) - t0);
00175         if (y_index < 0) // 2D HS Area Capture/Scan
00176                 for (int k=0; k<m[0]->GetNy (); ++k)
00177                         for (int i=0; i<num_srcs; ++i){
00178                                 count=sprintf(txt, "read %d\n", len);
00179                                 write(socket_descriptor, txt, count);
00180                                 ReadData(linebuffer, len*sizeof(short));
00181                                 write(socket_descriptor, txt, count); // This is ugly!!! So far a hack for reading one
00182                                 ReadData(linebuffer, len*sizeof(short)); // channel and dropping the forward image!!!!
00183                                 if (m[i])
00184                                         m[i]->PutDataLine (k, linebuffer);
00185                         }
00186         else
00187                 for (int i=0; i<num_srcs; ++i){
00188                         count=sprintf(txt, "read %d\n", len);
00189                         write(socket_descriptor, txt, count);
00190                         ReadData(linebuffer, len*sizeof(short));
00191                         write(socket_descriptor, txt, count); // This is ugly!!! So far a hack for reading one
00192                         count=ReadData(linebuffer, len*sizeof(short)); // channel and dropping the forward image!!!!
00193                         if (m[i])
00194                                 m[i]->PutDataLine (y_index, linebuffer);
00195                 }
00196         delete[] linebuffer;
00197         return 0;
00198 }
00199 
00200 int LAN_rhk_hwi_dev::ReadProbeData(int nsrcs, int nprobe, int kx, int ky, Mem2d *m, double scale){
00201         // later
00202         return 1;
00203 }
00204 
00205 
00206 /*
00207  * provide some info about the connected hardware/driver/etc.
00208  */
00209 gchar* LAN_rhk_hwi_dev::get_info(){
00210         return g_strdup("*--GXSM LAN/Internet HwI base class--*\n"
00211                         "Internet device: do not know\n"
00212                         "*--Features--*\n"
00213                         "SCAN: Yes\n"
00214                         "PROBE: No\n"
00215                         "ACPROBE: No\n"
00216                         "*--EOF--*\n"
00217                         );
00218 }

Generated on Sat Apr 1 09:04:15 2006 for GXSM by  doxygen 1.4.6