Main Page | Namespace List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

messages.c

Go to the documentation of this file.
00001 /*  Implémentation Linux de la bibliotheque XADLL de Sidena
00002  *  Copyright (C) 2005 ENSTAR
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *  You can contact us:
00019  *    Association ENSTAR
00020  *    Ecole Nationale Superieure de Techniques Avancees
00021  *    32 boulevard Victor
00022  *    75739 Paris Cedex 15
00023  *    FRANCE
00024  *
00025  *    enstar@ensta.fr
00026  */
00027 
00028 /** @file    messages.c
00029  *  @brief   Construction des messages
00030  *  @author  Francois Fevotte <francois.fevotte@ensta.org>
00031  *  $Revision: 51 $
00032  *  $Date: 2006-03-15 12:14:13 +0100 (Wed, 15 Mar 2006) $
00033  */
00034 
00035 /* $Id: messages.c 51 2006-03-15 11:14:13Z francois $ */
00036 
00037 #define MESSAGES_C
00038 #include <time.h>
00039 
00040 #include <stdio.h>
00041 #include <unistd.h>
00042 #include <string.h>
00043 #include <sys/time.h>
00044 #include <sys/types.h>
00045 
00046 #include "statique.h"
00047 #include "xalib.h"
00048 #include "os_dep_usb.h"    /* Fonctions bas-niveau spécifiques pour l'USB    */
00049 #include "os_dep_serial.h" /* Fonctions bas-niveau spécifiques pour le RS232 */
00050 #include "messages.h"
00051 
00052 
00053 /* Gestion des erreurs */
00054 #define SOURCE_ERREUR "messages"
00055 #ifdef DEBUG
00056 #define SCREEN_LEVEL 3 // messages de warning a l'ecran
00057 #else
00058 #define SCREEN_LEVEL 2 // pas de messages de warning
00059 #endif
00060 #include "erreur.h"
00061 
00062 
00063 unsigned int somme;        /**< Checksum */
00064 
00065 
00066 void XA_raw_insertB (unsigned char octet)
00067 {
00068   Out_buffer [XA_lastcar_out] = octet ;
00069  
00070   if( XA_lastcar_out < 255 )
00071     XA_lastcar_out++;
00072  
00073   somme += octet ;
00074 }
00075 
00076 
00077 void XA_insertB (unsigned char octet)
00078 {
00079   if( (octet >= 26) && (octet <= 30) )
00080     {
00081       XA_raw_insertB (26);
00082       XA_raw_insertB (octet - 26);
00083     }
00084   else
00085     {
00086       XA_raw_insertB (octet) ;
00087     }
00088 }
00089 
00090 
00091 void XA_createmsg (unsigned char adrmod, char read)
00092 {
00093   if( XA_lastcar_out == 0) /* Nouveau message */
00094     {
00095       XA_raw_insertB (27);
00096       if( (XA_typepack==2) || (XA_typepack==3) )
00097         XA_insertB (XA_adrpack);
00098       somme = 27;
00099     }
00100   else
00101     {
00102       XA_raw_insertB (28);
00103     }
00104  
00105 
00106 
00107   if( read==1 )
00108     XA_insertB (adrmod + adrmod + 1);
00109   else
00110     XA_insertB (adrmod + adrmod);
00111 }
00112 
00113 
00114 void XA_build (unsigned char adrmod, unsigned char fonction, unsigned char lngrec)
00115 {
00116   XA_createmsg (adrmod, 1) ;
00117   XA_insertB (lngrec) ;
00118   XA_insertB (fonction) ;
00119 }
00120 
00121 
00122 char XA_send()
00123 {
00124   int indice;
00125   clock_t time_now;
00126   unsigned char octet;
00127  
00128   if( !XA_opendone )
00129     {
00130       coderr = 103;
00131       XA_lastss = 0;
00132       XA_ptrget = 1;
00133       XA_lastcar_out = 0;
00134       return coderr;
00135     }
00136  
00137   if( XA_lastcar_out==0 )
00138     {
00139       coderr = 104;
00140       XA_lastss = 0;
00141       XA_ptrget = 1;
00142       XA_lastcar_out = 0;
00143       return coderr;
00144     }
00145  
00146   /* nettoyages */
00147   for(indice=0 ; indice<255 ; indice++)
00148     Rec_buffer[indice] = 0;
00149  
00150   /* table des positions des sous messages */
00151   for(indice=0 ; indice<8 ; indice++)
00152     tabss[indice] = 0;
00153  
00154   coderr = 113 ;             /* Erreur non identifiée */
00155   XA_insertB (lo (somme));   /* ajouter la checksum */
00156   XA_raw_insertB (29) ;      /* ajouter 29 (=fin message) */
00157 
00158   time_now = clock();        /* Top départ */
00159   octet = XA_lastss = XA_lastcar_in = somme = 0;
00160 
00161   if( XA_typepack > 2 || XA_typepack < 0 )
00162     {
00163       char message[1024];
00164       snprintf(message,1023,"XA_send : type de pack inconnu (typepack=%d)",XA_typepack);
00165       erreur::fatale( message );
00166       coderr = 102;
00167       return coderr;
00168     }
00169 
00170   if( XA_typepack==0 ) /* USB */
00171     {
00172       int i;
00173       if( !(usb::write(XA_lastcar_out)))
00174         {
00175           XA_lastss = 0;
00176           XA_ptrget = 1;
00177           XA_lastcar_out = 0;
00178           return coderr;
00179         }
00180   
00181       /* Lecture ... */
00182       usb::read( 3+Out_buffer[2] );
00183       octet = Rec_buffer[XA_lastcar_in-1];
00184       for( i=0 ; i<XA_lastcar_in-1 ; i++ )
00185         {
00186           somme += Rec_buffer[i];
00187         }
00188 
00189       if( octet != 30 )
00190         {
00191           char message[1024];
00192           snprintf(message,1023,"XA_send : message non termine (dernier caractere = %02X)",octet);
00193           erreur::warning(message);
00194           XA_lastss = 0;
00195           XA_ptrget = 1;
00196           XA_lastcar_out = 0;
00197           coderr = 117;
00198           return coderr;
00199         }
00200     }
00201   else /* Port série */
00202     {
00203       if( !(serial::write(XA_lastcar_out)))
00204         {
00205           XA_lastss = 0;
00206           XA_ptrget = 1;
00207           XA_lastcar_out = 0;
00208           return coderr;
00209         }
00210   
00211       /* Lecture ... */
00212       serial::read( &somme );
00213       octet = Rec_buffer[XA_lastcar_in-1];
00214 
00215       if( octet != 30 )
00216         {
00217           char message[1024];
00218           snprintf(message,1023,"XA_send : message non termine (dernier caractere = %02X)",octet);
00219           erreur::warning(message);
00220           XA_lastss = 0;
00221           XA_ptrget = 1;
00222           XA_lastcar_out = 0;
00223           coderr = 117;
00224           return coderr;
00225         }
00226     }
00227 
00228   XA_lastss = 0;
00229   XA_ptrget = 1;
00230   XA_lastcar_out = 0;
00231 
00232   if ((somme%256) != 255)
00233     {
00234       char message[1024];
00235       snprintf(message,1023,"XA_send : erreur de checksum (checksum = %3d)",somme%256);
00236       erreur::warning(message);
00237       coderr = 106;
00238       return coderr;
00239     }
00240 
00241   if( (coderr = Rec_buffer[0]) != 0 )
00242     {
00243       char message[1024];
00244       snprintf(message,1023,"XA_send : erreur renvoyee par les cartes XA (code = %3d)",coderr);
00245       erreur::warning(message);
00246       return coderr;
00247     }
00248  
00249   return coderr;
00250 }

Project hosted on SourceForge.net
Documentation generated on Mon Apr 2 14:52:55 2007 for libXA by doxygen 1.4.4