ethercatfoe.c
Go to the documentation of this file.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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <stdio.h>
00051 #include <string.h>
00052 #include <sys/time.h>
00053 #include <unistd.h>
00054 #include "ethercattype.h"
00055 #include "nicdrv.h"
00056 #include "ethercatbase.h"
00057 #include "ethercatmain.h"
00058 #include "ethercatfoe.h"
00059
00060 #define EC_MAXFOEDATA 512
00061
00062
00063
00064
00065 typedef struct PACKED
00066 {
00067 ec_mbxheadert MbxHeader;
00068 uint8 OpCode;
00069 uint8 Reserved;
00070 union
00071 {
00072 uint32 Password;
00073 uint32 PacketNumber;
00074 uint32 ErrorCode;
00075 };
00076 union
00077 {
00078 char FileName[EC_MAXFOEDATA];
00079 uint8 Data[EC_MAXFOEDATA];
00080 char ErrorText[EC_MAXFOEDATA];
00081 };
00082 } ec_FOEt;
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 int ec_FOEread(uint16 slave, char *filename, uint32 password, int *psize, void *p, int timeout)
00095 {
00096 ec_FOEt *FOEp, *aFOEp;
00097 int wkc;
00098 int32 dataread = 0;
00099 int32 buffersize, packetnumber, prevpacket = 0;
00100 uint16 fnsize, maxdata, segmentdata;
00101 ec_mbxbuft MbxIn, MbxOut;
00102 uint8 cnt;
00103 boolean worktodo;
00104
00105 buffersize = *psize;
00106 ec_clearmbx(&MbxIn);
00107
00108 wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
00109 ec_clearmbx(&MbxOut);
00110 aFOEp = (ec_FOEt *)&MbxIn;
00111 FOEp = (ec_FOEt *)&MbxOut;
00112 fnsize = strlen(filename);
00113 maxdata = ec_slave[slave].mbx_l - 12;
00114 if (fnsize > maxdata)
00115 fnsize = maxdata;
00116 FOEp->MbxHeader.length = htoes(0x0006 + fnsize);
00117 FOEp->MbxHeader.address = htoes(0x0000);
00118 FOEp->MbxHeader.priority = 0x00;
00119
00120 cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
00121 ec_slave[slave].mbx_cnt = cnt;
00122 FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
00123 FOEp->OpCode = ECT_FOE_READ;
00124 FOEp->Password = htoel(password);
00125
00126 memcpy(&FOEp->FileName[0], filename, fnsize);
00127
00128 wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
00129 if (wkc > 0)
00130 {
00131 do
00132 {
00133 worktodo = FALSE;
00134
00135 ec_clearmbx(&MbxIn);
00136
00137 wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
00138 if (wkc > 0)
00139 {
00140
00141 if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
00142 {
00143 if(aFOEp->OpCode == ECT_FOE_DATA)
00144 {
00145 segmentdata = etohs(aFOEp->MbxHeader.length) - 0x0006;
00146 packetnumber = etohl(aFOEp->PacketNumber);
00147 if ((packetnumber == ++prevpacket) && (dataread + segmentdata <= buffersize))
00148 {
00149 memcpy(p, &aFOEp->Data[0], segmentdata);
00150 dataread += segmentdata;
00151 p += segmentdata;
00152 if (segmentdata == maxdata)
00153 worktodo = TRUE;
00154 FOEp->MbxHeader.length = htoes(0x0006);
00155 FOEp->MbxHeader.address = htoes(0x0000);
00156 FOEp->MbxHeader.priority = 0x00;
00157
00158 cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
00159 ec_slave[slave].mbx_cnt = cnt;
00160 FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
00161 FOEp->OpCode = ECT_FOE_ACK;
00162 FOEp->PacketNumber = htoel(packetnumber);
00163
00164 wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
00165 if (wkc <= 0)
00166 worktodo = FALSE;
00167 }
00168 else
00169 {
00170
00171 wkc = -EC_ERR_TYPE_FOE_BUF2SMALL;
00172 }
00173 }
00174 else
00175 if(aFOEp->OpCode == ECT_FOE_ERROR)
00176 {
00177
00178 wkc = -EC_ERR_TYPE_FOE_ERROR;
00179 }
00180 else
00181 {
00182
00183 wkc = -EC_ERR_TYPE_PACKET_ERROR;
00184 }
00185 }
00186 else
00187 {
00188
00189 wkc = -EC_ERR_TYPE_PACKET_ERROR;
00190 }
00191 *psize = dataread;
00192 }
00193 } while (worktodo);
00194 }
00195
00196 return wkc;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 int ec_FOEwrite(uint16 slave, char *filename, uint32 password, int psize, void *p, int timeout)
00210 {
00211 ec_FOEt *FOEp, *aFOEp;
00212 int wkc;
00213 int32 packetnumber, sendpacket = 0;
00214 uint16 fnsize, maxdata;
00215 int segmentdata;
00216 ec_mbxbuft MbxIn, MbxOut;
00217 uint8 cnt;
00218 boolean worktodo;
00219 int tsize;
00220
00221 ec_clearmbx(&MbxIn);
00222
00223 wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
00224 ec_clearmbx(&MbxOut);
00225 aFOEp = (ec_FOEt *)&MbxIn;
00226 FOEp = (ec_FOEt *)&MbxOut;
00227 fnsize = strlen(filename);
00228 maxdata = ec_slave[slave].mbx_l - 12;
00229 if (fnsize > maxdata)
00230 fnsize = maxdata;
00231 FOEp->MbxHeader.length = htoes(0x0006 + fnsize);
00232 FOEp->MbxHeader.address = htoes(0x0000);
00233 FOEp->MbxHeader.priority = 0x00;
00234
00235 cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
00236 ec_slave[slave].mbx_cnt = cnt;
00237 FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
00238 FOEp->OpCode = ECT_FOE_WRITE;
00239 FOEp->Password = htoel(password);
00240
00241 memcpy(&FOEp->FileName[0], filename, fnsize);
00242
00243 wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
00244 if (wkc > 0)
00245 {
00246 do
00247 {
00248 worktodo = FALSE;
00249
00250 ec_clearmbx(&MbxIn);
00251
00252 wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
00253 if (wkc > 0)
00254 {
00255
00256 if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
00257 {
00258 if(aFOEp->OpCode == ECT_FOE_ACK)
00259 {
00260 packetnumber = etohl(aFOEp->PacketNumber);
00261 if (packetnumber == sendpacket)
00262 {
00263 tsize = psize;
00264 if (tsize > maxdata)
00265 {
00266 worktodo = TRUE;
00267 tsize = maxdata;
00268 }
00269 segmentdata = tsize;
00270 psize -= segmentdata;
00271 FOEp->MbxHeader.length = htoes(0x0006 + segmentdata);
00272 FOEp->MbxHeader.address = htoes(0x0000);
00273 FOEp->MbxHeader.priority = 0x00;
00274
00275 cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
00276 ec_slave[slave].mbx_cnt = cnt;
00277 FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
00278 FOEp->OpCode = ECT_FOE_DATA;
00279 FOEp->PacketNumber = htoel(++sendpacket);
00280 memcpy(&FOEp->Data[0], p, segmentdata);
00281 p += segmentdata;
00282
00283 wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, EC_TIMEOUTTXM);
00284 if (wkc <= 0)
00285 worktodo = FALSE;
00286 }
00287 else
00288 {
00289
00290 wkc = -EC_ERR_TYPE_FOE_PACKETNUMBER;
00291 }
00292 }
00293 else
00294 if(aFOEp->OpCode == ECT_FOE_ERROR)
00295 {
00296
00297 wkc = -EC_ERR_TYPE_FOE_ERROR;
00298 }
00299 else
00300 {
00301
00302 wkc = -EC_ERR_TYPE_PACKET_ERROR;
00303 }
00304 }
00305 else
00306 {
00307
00308 wkc = -EC_ERR_TYPE_PACKET_ERROR;
00309 }
00310 }
00311 } while (worktodo);
00312 }
00313
00314 return wkc;
00315 }