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

xalib.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    xalib.c
00029  *  @brief   Front-end de la bibliothèque
00030  *  @author  Francois Fevotte <francois.fevotte@ensta.org>
00031  *  $Revision: 28 $
00032  *  $Date: 2007-03-22 13:48:33 +0100 (jeu, 22 mar 2007) $
00033  */
00034 
00035 /* $Id: xalib.c 28 2007-03-22 12:48:33Z franceso $ */
00036 
00037 #define XALIB_C
00038 
00039 #include <stdio.h>
00040 #include <time.h>
00041 #include <unistd.h>
00042 #include "xalib.h"
00043 #include "statique.h"
00044 #include "messages.h"
00045 #include "os_dep_usb.h"    /* Fonctions spécifique pour la communication avec le port USB */
00046 #include "os_dep_serial.h" /* Fonctions spécifique pour la communication avec le port série */
00047 #include "codes.h"         /* Codes de fonctions et de valeurs de retour */
00048 
00049 #define SOURCE_ERREUR "xalib"
00050 #include "erreur.h"
00051 
00052 /* Buffers */
00053 int lastget=0;             /** Le dernier caractère lu dans le buffer Rec_buffer */
00054 
00055 
00056 
00057 
00058 /*constantes*/
00059 int npre = 50, carpre = 240, npre_start = 5;
00060 
00061 /*variables globales*/
00062 char *XA_adrip;
00063 /*thandle fhandle; */
00064 
00065 
00066 /** Ouverture d'un pack
00067     @param typepack le code du pack à ouvrir
00068     @param ncom numéro du port série (le cas échéant)
00069     @param baudrate la vitesse de transmission dans le cas d'un port série
00070     @return le code de retour correspondant (Cf. table des codes de retour)
00071     @li 0: pas de problème
00072     @li 102: erreur d'ouverture
00073     @remarks le code de retour est copié dans la variable globale 'coderr'
00074 */
00075 int XA_open (int typepack, int ncom, int baudrate)
00076 {
00077   XA_typepack = typepack;
00078         
00079   switch( XA_typepack )
00080     {
00081     case 0 : /* USB */
00082       if( usb::open() )
00083         {
00084           coderr = 102;
00085           return coderr;
00086         }
00087       break;
00088 
00089     case 1 :
00090     case 2 :  /* série */
00091       if( serial::open(XA_typepack) )
00092         {
00093           coderr = 102;
00094           return coderr;
00095         }
00096       break;
00097                 
00098       /* Ajouter ici les autres types de packs */
00099                 
00100     default : /* Type de pack inconnu */
00101       {
00102         char message[1024];
00103         sprintf(message, "XA_open : type de pack inconnu (typepack=%d)", typepack);
00104         erreur::fatale( message );
00105         coderr = 102;
00106         return coderr;
00107       }
00108     }
00109         
00110   XA_opendone = 1;
00111   coderr = 0;
00112   return coderr;
00113 }
00114 
00115 
00116 /** Fermeture du pack courant
00117  *  @return Cf. table des codes de retour:
00118  */
00119 int XA_close ()
00120 {
00121   switch( XA_typepack )
00122     {
00123     case 0: /* USB */
00124       usb::close();
00125       break;
00126     case 1:
00127     case 2: /* série */
00128       serial::close();
00129       break;
00130     default : /* Type de pack inconnu */
00131       {
00132         char message[1024];
00133         sprintf(message, "XA_close : type de pack inconnu (typepack=%d)", XA_typepack);
00134         erreur::fatale( message );
00135         coderr = 102;
00136         return coderr;
00137       }
00138     }
00139   
00140   XA_opendone = 0;
00141   return 0;
00142 }
00143 
00144 int XA_coderr ()
00145 {
00146   return coderr;
00147 }
00148 
00149 int XA_getB ()
00150 {
00151   int res = Rec_buffer[XA_ptrget];
00152   lastget = res;
00153         
00154   if (XA_ptrget < 255)
00155     XA_ptrget++;
00156         
00157   return res;
00158 }
00159 
00160 long XA_lastgetB ()
00161 {
00162   return lastget;
00163 }
00164 
00165 unsigned int XA_getW () {
00166   unsigned int mot;
00167      
00168   mot = Rec_buffer[XA_ptrget];
00169   if (XA_ptrget < 255) XA_ptrget++;
00170 
00171   mot = (mot << 8) + Rec_buffer[XA_ptrget];
00172   if (XA_ptrget < 255) XA_ptrget++;
00173 
00174   return mot;
00175 }
00176 
00177 
00178 long XA_getL () {
00179   long Long = Rec_buffer[XA_ptrget];
00180   int i;
00181 
00182   for (i=0;i<3;i++) {
00183     if (XA_ptrget < 255)
00184       XA_ptrget++;
00185     Long = (Long << 8) + Rec_buffer[XA_ptrget];
00186   }
00187   if (XA_ptrget < 255)
00188     XA_ptrget++;
00189   return Long;
00190 }
00191 
00192 
00193 unsigned char hi (int a)
00194 {
00195   return (a >> 8) & 0xFF;
00196 }
00197 
00198 
00199 unsigned char lo(int x)
00200 {
00201   return (x & 0xFF);
00202 }
00203 
00204 
00205 long XA (char send, long fonction, long adrmod, long arg1, long arg2, long arg3, long arg4, long arg5)
00206 {
00207   int foncbyte, entier, res;
00208         
00209   coderr = 255;
00210   res = coderr ;
00211   foncbyte = fonction & 255;
00212   
00213   /* Fonctions sans arguments */
00214   if( ((foncbyte >0)&&(foncbyte<20)) || (foncbyte==204) || (foncbyte==205) || (foncbyte==207) )
00215     {
00216       XA_build(adrmod,foncbyte,1);
00217       if(send) res = XA_send();
00218     }
00219   
00220   /* Fonctions avec 1 caractère */
00221   if (((foncbyte >=20)&&(foncbyte<30)) || (foncbyte == 80))
00222     {
00223       XA_build(adrmod,foncbyte,1);
00224       XA_insertB(arg1);
00225       if(send) res = XA_send();
00226     }
00227         
00228   /* Fonctions avec 1 mot */
00229   if ((foncbyte >=30)&&(foncbyte<40))
00230     {
00231       XA_build(adrmod,foncbyte,1) ;
00232       XA_insertB(hi(arg1));
00233       XA_insertB(lo(arg1));
00234       if(send) res = XA_send();
00235     }
00236         
00237   /* Fonctions avec 1 long */
00238   if ((foncbyte >=40)&&(foncbyte<50))
00239     {
00240       XA_build(adrmod,foncbyte,1) ;
00241       entier = arg1 >> 16 ;
00242       XA_insertB(hi(entier));
00243       XA_insertB(lo(entier));
00244       XA_insertB(hi(arg1));
00245       XA_insertB(lo(arg1));
00246       if(send) res = XA_send();
00247     }
00248         
00249   /* Fonctions avec 2 caractères */
00250   if ((foncbyte >=50)&&(foncbyte<60))
00251     {
00252       XA_build(adrmod,foncbyte,1);
00253       XA_insertB(arg1);
00254       XA_insertB(arg2);
00255       if(send) res = XA_send();
00256     }
00257         
00258   /* Fonctions avec 1 caractère et 1 mot */
00259   if ((foncbyte >=60)&&(foncbyte<70))
00260     {
00261       XA_build (adrmod, foncbyte, 1) ;
00262       XA_insertB (arg1) ;
00263       XA_insertB (hi (arg2)) ;
00264       XA_insertB (lo (arg2)) ;
00265       if(send) res = XA_send();
00266     }
00267         
00268   /* Fonctions avec 1 caractère et 1 long */
00269   if ((foncbyte >=70)&&(foncbyte<80))
00270     {
00271       XA_build(adrmod,foncbyte,1);
00272       XA_insertB(arg1);
00273       entier = arg2 >> 16;
00274       XA_insertB(hi(entier));
00275       XA_insertB(lo(entier));
00276       XA_insertB(hi(arg2));
00277       XA_insertB(lo(arg2));
00278       if(send) res = XA_send();
00279     }
00280 
00281   /* Lectures  simples de N octets */
00282   if ((foncbyte >=100)&&(foncbyte<=115))
00283     {
00284       XA_build(adrmod,foncbyte,(foncbyte-98));
00285       if(send) res = XA_send();
00286     }
00287         
00288   /* Ecriture 1 caractere et lecture N octets */
00289   if ((foncbyte >=120)&&(foncbyte<=135))
00290     {
00291       XA_build(adrmod,foncbyte,(foncbyte-118));
00292       XA_insertB(arg1);
00293       if(send) res = XA_send();
00294     }
00295         
00296   /* Fonction spéciale DSI */
00297   if (foncbyte == 140)
00298     {
00299       XA_build(adrmod,foncbyte,1);
00300       XA_insertB(hi(arg2));
00301       XA_insertB(lo(arg2));
00302       XA_insertB(hi(arg3));
00303       XA_insertB(lo(arg3));
00304       XA_insertB(hi(arg4));
00305       XA_insertB(lo(arg4));
00306       XA_insertB(hi(arg5));
00307       XA_insertB(lo(arg5));
00308       XA_insertB(arg1);
00309       if(send) res = XA_send();
00310     }
00311         
00312   /* Fonction spéciale d'ouverture du pack */
00313   if (foncbyte == 200)
00314     res = XA_open(adrmod,arg1,arg2);
00315         
00316   /* Fonction spéciale de fermeture du pack */
00317   if (foncbyte == 201)
00318     res = XA_close();
00319 
00320   /* Fonction spéciale de changement d'adresse du pack */
00321   if (foncbyte == 202)
00322     XA_adrpack = adrmod;
00323 
00324   /* Fonction spéciale de réadressage du module */
00325   if (foncbyte == 203)
00326     {
00327       XA_build(adrmod,foncbyte,1);
00328       XA_insertB(arg1);
00329       if(send) res = XA_send();
00330     }
00331         
00332   /* Lecture version */
00333   if (foncbyte == 206)
00334     {
00335       XA_build(adrmod,foncbyte,3);
00336       if(send) res = XA_send();
00337     }
00338         
00339         
00340         
00341         
00342   /* Ecritures I2C pures */
00343         
00344   /* 1 caractère */
00345   if (foncbyte == 210)
00346     {
00347       XA_createmsg(adrmod,0);
00348       XA_insertB(arg1);
00349       if(send) res = XA_send();
00350     }
00351 
00352   /* 1 mot */
00353   if (foncbyte == 211)
00354     {
00355       XA_createmsg(adrmod,0);
00356       XA_insertB(arg1);
00357       XA_insertB(arg2);
00358       if(send) res = XA_send();
00359     }
00360         
00361   /* 1 mot */
00362   if (foncbyte == 212)
00363     {
00364       XA_createmsg(adrmod,0);
00365       XA_insertB(arg1);
00366       XA_insertB(arg2);
00367       XA_insertB(arg3);
00368       if(send) res = XA_send();
00369     }
00370         
00371   /* 1 mot */
00372   if (foncbyte == 213)
00373     {
00374       XA_createmsg(adrmod,0);
00375       XA_insertB(arg1);
00376       XA_insertB(arg2);
00377       XA_insertB(arg3);
00378       XA_insertB(arg4);
00379       if(send) res = XA_send();
00380     }
00381         
00382   /* 1 mot */
00383   if (foncbyte == 214)
00384     {
00385       XA_createmsg(adrmod,0);
00386       XA_insertB(arg1);
00387       XA_insertB(arg2);
00388       XA_insertB(arg3);
00389       XA_insertB(arg4);
00390       XA_insertB(arg5);
00391       if(send) res = XA_send();
00392     }
00393         
00394   /* Lecture I2C pure */
00395   if (foncbyte == 220)
00396     {
00397       XA_createmsg(adrmod,1);
00398       XA_insertB(arg1);
00399       if(send) res = XA_send();
00400     }
00401         
00402   return res;
00403 }
00404 
00405 
00406 long XAN_str (long fonction, long adrmod, char *chaine, long arg)
00407 {
00408   int ncar;
00409   int res;
00410         
00411   coderr = 255;
00412   res = coderr;
00413         
00414   switch (fonction)
00415     {
00416     case 81 :
00417       XA_build(adrmod,81,1);
00418       if (*chaine)
00419         {
00420           for( ncar=0; chaine[ncar]; ncar++ )
00421             XA_insertB(((int)(chaine[ncar])));
00422           res = XA_send();
00423         }
00424       break;
00425     case 202 :
00426       coderr = 0;
00427       XA_adrip = chaine;
00428       break;
00429     default :
00430       break;
00431     }
00432         
00433   res = coderr;
00434   return res;
00435 }
00436 
00437 void XA_startup()
00438 {
00439   /* ouverture du fichier de log d'erreur */
00440   erreur::openlog();
00441 
00442   /* message d'initialisation */
00443   fprintf( stdout,  "Initialisation de la bibliothèque XA\n" );
00444   fprintf( stdout,  "\t$HeadURL: https://libextralink.svn.sourceforge.net/svnroot/libextralink/trunk/xalib.c $\n\t$Revision: 28 $\n\n" );
00445   fprintf( logfile, "Initialisation de la bibliothèque XA\n" );
00446   fprintf( logfile, "\t$HeadURL: https://libextralink.svn.sourceforge.net/svnroot/libextralink/trunk/xalib.c $\n\t$Revision: 28 $\n\n" );
00447 
00448   /* ouverture */
00449   XAN( OPEN,   1, 0,0,0,0,0 );
00450   
00451   /* clignotement de la LED pour tester */
00452   XAN( LEDON,  8, 0,0,0,0,0 );
00453   usleep( 200000 );
00454   XAN( LEDOFF, 8, 0,0,0,0,0 );
00455   usleep( 200000 );
00456   XAN( LEDON,  8, 0,0,0,0,0 );
00457   usleep( 200000 );
00458   XAN( LEDOFF, 8, 0,0,0,0,0 );
00459 }
00460 
00461 void XA_cleanup()
00462 {
00463   /* fermeture de la connexion */
00464   XAN( CLOSE, 1, 0,0,0,0,0 );
00465 
00466   /* fermeture du fichier de log d'erreur */
00467   erreur::closelog();
00468 
00469 }

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