00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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"
00049 #include "os_dep_serial.h"
00050 #include "messages.h"
00051
00052
00053
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;
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)
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
00147 for(indice=0 ; indice<255 ; indice++)
00148 Rec_buffer[indice] = 0;
00149
00150
00151 for(indice=0 ; indice<8 ; indice++)
00152 tabss[indice] = 0;
00153
00154 coderr = 113 ;
00155 XA_insertB (lo (somme));
00156 XA_raw_insertB (29) ;
00157
00158 time_now = clock();
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 )
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
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
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
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 }