etisserant@0: /* etisserant@0: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: etisserant@0: Copyright (C): Edouard TISSERANT and Francis DUPIN etisserant@0: etisserant@0: See COPYING file for copyrights details. etisserant@0: etisserant@0: This library is free software; you can redistribute it and/or etisserant@0: modify it under the terms of the GNU Lesser General Public etisserant@0: License as published by the Free Software Foundation; either etisserant@0: version 2.1 of the License, or (at your option) any later version. etisserant@0: etisserant@0: This library is distributed in the hope that it will be useful, etisserant@0: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: Lesser General Public License for more details. etisserant@0: etisserant@0: You should have received a copy of the GNU Lesser General Public etisserant@0: License along with this library; if not, write to the Free Software etisserant@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: */ etisserant@0: frdupin@84: /* #define DEBUG_WAR_CONSOLE_ON */ frdupin@84: /* #define DEBUG_ERR_CONSOLE_ON */ etisserant@0: etisserant@0: #include "objacces.h" etisserant@0: #include "sdo.h" etisserant@149: #include "canfestival.h" etisserant@0: frdupin@71: /* Uncomment if your compiler does not support inline functions */ frdupin@86: #define NO_INLINE frdupin@71: frdupin@71: #ifdef NO_INLINE frdupin@71: #define INLINE frdupin@71: #else frdupin@86: #define INLINE inline frdupin@71: #endif frdupin@71: frdupin@53: /*Internals prototypes*/ frdupin@53: frdupin@53: /** Called by writeNetworkDict */ frdupin@71: INLINE UNS8 _writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, frdupin@53: UNS8 subIndex, UNS8 count, UNS8 dataType, void *data, SDOCallback_t Callback); frdupin@53: frdupin@53: /** Called by readNetworkDict */ frdupin@71: INLINE UNS8 _readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, frdupin@53: UNS8 dataType, SDOCallback_t Callback); frdupin@53: frdupin@53: etisserant@0: /***************************************************************************/ frdupin@71: /* SDO (un)packing macros */ etisserant@0: etisserant@0: /** Returns the command specifier (cs, ccs, scs) from the first byte of the SDO etisserant@0: */ etisserant@0: #define getSDOcs(byte) (byte >> 5) etisserant@0: etisserant@0: /** Returns the number of bytes without data from the first byte of the SDO. Coded in 2 bits etisserant@0: */ etisserant@0: #define getSDOn2(byte) ((byte >> 2) & 3) etisserant@0: etisserant@0: /** Returns the number of bytes without data from the first byte of the SDO. Coded in 3 bits etisserant@0: */ etisserant@0: #define getSDOn3(byte) ((byte >> 1) & 7) etisserant@0: etisserant@0: /** Returns the transfer type from the first byte of the SDO etisserant@0: */ etisserant@0: #define getSDOe(byte) ((byte >> 1) & 1) etisserant@0: etisserant@0: /** Returns the size indicator from the first byte of the SDO etisserant@0: */ etisserant@0: #define getSDOs(byte) (byte & 1) etisserant@0: etisserant@0: /** Returns the indicator of end transmission from the first byte of the SDO etisserant@0: */ etisserant@0: #define getSDOc(byte) (byte & 1) etisserant@0: etisserant@0: /** Returns the toggle from the first byte of the SDO etisserant@0: */ etisserant@0: #define getSDOt(byte) ((byte >> 4) & 1) etisserant@0: etisserant@0: /** Returns the index from the bytes 1 and 2 of the SDO etisserant@0: */ etisserant@0: #define getSDOindex(byte1, byte2) ((byte2 << 8) | (byte1)) etisserant@0: etisserant@0: /** Returns the subIndex from the byte 3 of the SDO etisserant@0: */ frdupin@71: etisserant@0: #define getSDOsubIndex(byte3) (byte3) etisserant@0: etisserant@0: /*************************************************************************** etisserant@0: ** etisserant@0: */ etisserant@0: void SDOTimeoutAlarm(CO_Data* d, UNS32 id) etisserant@0: { etisserant@0: MSG_ERR(0x1A01, "SDO timeout. SDO response not received.", 0); etisserant@0: MSG_WAR(0x2A02, "server node : ", d->transfers[id].nodeId); etisserant@0: MSG_WAR(0x2A02, " index : ", d->transfers[id].index); etisserant@0: MSG_WAR(0x2A02, " subIndex : ", d->transfers[id].subIndex); frdupin@71: /* Reset timer handler */ etisserant@0: d->transfers[id].timer = TIMER_NONE; frdupin@71: /* Call the user function to inform of the problem.*/ etisserant@145: (*d->SDOtimeoutError)((UNS8)id); frdupin@71: /* Sending a SDO abort */ etisserant@0: sendSDOabort(d, d->transfers[id].whoami, etisserant@0: d->transfers[id].index, d->transfers[id].subIndex, SDOABT_TIMED_OUT); frdupin@71: /* Reset the line*/ etisserant@145: resetSDOline(d, (UNS8)id); etisserant@0: } etisserant@0: etisserant@0: #define StopSDO_TIMER(id) \ etisserant@0: MSG_WAR(0x3A05, "StopSDO_TIMER for line : ", line);\ etisserant@0: d->transfers[id].timer = DelAlarm(d->transfers[id].timer); etisserant@0: etisserant@0: #define StartSDO_TIMER(id) \ etisserant@0: MSG_WAR(0x3A06, "StartSDO_TIMER for line : ", line);\ etisserant@0: d->transfers[id].timer = SetAlarm(d,id,&SDOTimeoutAlarm,MS_TO_TIMEVAL(SDO_TIMEOUT_MS),0); etisserant@0: etisserant@0: #define RestartSDO_TIMER(id) \ etisserant@0: MSG_WAR(0x3A07, "restartSDO_TIMER for line : ", line);\ etisserant@0: if(d->transfers[id].timer != TIMER_NONE) { StopSDO_TIMER(id) StartSDO_TIMER(id) } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: /** Reset all sdo buffers etisserant@0: */ etisserant@0: void resetSDO (CO_Data* d) etisserant@0: { etisserant@0: UNS8 j; etisserant@0: etisserant@0: /* transfer structure initialization */ etisserant@0: for (j = 0 ; j < SDO_MAX_SIMULTANEOUS_TRANSFERTS ; j++) etisserant@0: resetSDOline(d, j); etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS32 SDOlineToObjdict (CO_Data* d, UNS8 line) etisserant@0: { etisserant@0: UNS8 size; etisserant@0: UNS32 errorCode; etisserant@1: MSG_WAR(0x3A08, "Enter in SDOlineToObjdict ", line); etisserant@145: size = (UNS8)d->transfers[line].count; etisserant@0: errorCode = setODentry(d, d->transfers[line].index, d->transfers[line].subIndex, etisserant@0: (void *) d->transfers[line].data, &size, 1); etisserant@0: if (errorCode != OD_SUCCESSFUL) etisserant@0: return errorCode; etisserant@1: MSG_WAR(0x3A08, "exit of SDOlineToObjdict ", line); etisserant@0: return 0; etisserant@0: etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS32 objdictToSDOline (CO_Data* d, UNS8 line) etisserant@0: { etisserant@0: UNS8 size = 0; etisserant@0: UNS8 dataType; etisserant@0: UNS32 errorCode; etisserant@0: dejoigny@12: MSG_WAR(0x3A05, "objdict->line index : ", d->transfers[line].index); dejoigny@12: MSG_WAR(0x3A06, " subIndex : ", d->transfers[line].subIndex); etisserant@0: etisserant@0: errorCode = getODentry(d, d->transfers[line].index, etisserant@0: d->transfers[line].subIndex, etisserant@0: (void *)d->transfers[line].data, etisserant@0: &size, &dataType, 0); etisserant@0: etisserant@0: if (errorCode != OD_SUCCESSFUL) etisserant@0: return errorCode; etisserant@0: etisserant@0: d->transfers[line].count = size; etisserant@0: d->transfers[line].offset = 0; dejoigny@12: #if 0 frdupin@71: /*Me laisser ça, please ! (FD)*/ dejoigny@12: { dejoigny@12: UNS8 i; dejoigny@12: for (i = 0 ; i < 10 ; i++) { dejoigny@12: MSG_WAR(i, "data= ", d->transfers[line].data[i]); dejoigny@12: } dejoigny@12: } dejoigny@12: #endif etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 lineToSDO (CO_Data* d, UNS8 line, UNS8 nbBytes, UNS8* data) { etisserant@0: UNS8 i; etisserant@0: UNS8 offset; etisserant@0: etisserant@0: if ((d->transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFERT) { etisserant@0: MSG_ERR(0x1A10,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFERT", nbBytes); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: if ((d->transfers[line].offset + nbBytes) > d->transfers[line].count) { etisserant@0: MSG_ERR(0x1A11,"SDO Size of data too large. Exceed count", nbBytes); etisserant@0: return 0xFF; etisserant@0: } etisserant@145: offset = (UNS8)d->transfers[line].offset; etisserant@0: for (i = 0 ; i < nbBytes ; i++) etisserant@0: * (data + i) = d->transfers[line].data[offset + i]; etisserant@0: d->transfers[line].offset = d->transfers[line].offset + nbBytes; etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 SDOtoLine (CO_Data* d, UNS8 line, UNS8 nbBytes, UNS8* data) etisserant@0: { etisserant@0: UNS8 i; etisserant@0: UNS8 offset; etisserant@0: etisserant@0: if ((d->transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFERT) { etisserant@0: MSG_ERR(0x1A15,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFERT", nbBytes); etisserant@0: return 0xFF; etisserant@0: } etisserant@145: offset = (UNS8)d->transfers[line].offset; etisserant@0: for (i = 0 ; i < nbBytes ; i++) etisserant@0: d->transfers[line].data[offset + i] = * (data + i); etisserant@0: d->transfers[line].offset = d->transfers[line].offset + nbBytes; etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 failedSDO (CO_Data* d, UNS8 nodeId, UNS8 whoami, UNS16 index, etisserant@0: UNS8 subIndex, UNS32 abortCode) etisserant@0: { etisserant@0: UNS8 err; etisserant@0: UNS8 line; etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); frdupin@71: if (!err) /* If a line on use have been found.*/ etisserant@0: MSG_WAR(0x3A20, "FailedSDO : line found : ", line); etisserant@0: if ((! err) && (whoami == SDO_SERVER)) { etisserant@0: resetSDOline( d, line ); etisserant@0: MSG_WAR(0x3A21, "FailedSDO : line released : ", line); etisserant@0: } etisserant@1: if ((! err) && (whoami == SDO_CLIENT)) { etisserant@1: StopSDO_TIMER(line); etisserant@0: d->transfers[line].state = SDO_ABORTED_INTERNAL; etisserant@1: } etisserant@0: MSG_WAR(0x3A22, "Sending SDO abort ", 0); etisserant@0: err = sendSDOabort(d, whoami, index, subIndex, abortCode); etisserant@0: if (err) { etisserant@0: MSG_WAR(0x3A23, "Unable to send the SDO abort", 0); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: void resetSDOline ( CO_Data* d, UNS8 line ) etisserant@0: { etisserant@0: UNS8 i; etisserant@1: MSG_WAR(0x3A25, "reset SDO line nb : ", line); etisserant@0: initSDOline(d, line, 0, 0, 0, SDO_RESET); etisserant@0: for (i = 0 ; i < SDO_MAX_LENGTH_TRANSFERT ; i++) etisserant@0: d->transfers[line].data[i] = 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 initSDOline (CO_Data* d, UNS8 line, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 state) etisserant@0: { etisserant@1: MSG_WAR(0x3A25, "init SDO line nb : ", line); etisserant@0: if (state == SDO_DOWNLOAD_IN_PROGRESS || state == SDO_UPLOAD_IN_PROGRESS){ etisserant@0: StartSDO_TIMER(line) etisserant@0: }else{ etisserant@0: StopSDO_TIMER(line) etisserant@0: } etisserant@0: d->transfers[line].nodeId = nodeId; etisserant@0: d->transfers[line].index = index; etisserant@0: d->transfers[line].subIndex = subIndex; etisserant@0: d->transfers[line].state = state; etisserant@0: d->transfers[line].toggle = 0; etisserant@0: d->transfers[line].count = 0; etisserant@0: d->transfers[line].offset = 0; etisserant@0: d->transfers[line].dataType = 0; etisserant@32: d->transfers[line].Callback = NULL; etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 getSDOfreeLine ( CO_Data* d, UNS8 whoami, UNS8 *line ) etisserant@0: { etisserant@0: etisserant@0: UNS8 i; etisserant@0: etisserant@0: for (i = 0 ; i < SDO_MAX_SIMULTANEOUS_TRANSFERTS ; i++){ etisserant@0: if ( d->transfers[i].state == SDO_RESET ) { etisserant@0: *line = i; etisserant@0: d->transfers[i].whoami = whoami; etisserant@0: return 0; frdupin@71: } /* end if */ frdupin@71: } /* end for */ etisserant@0: MSG_ERR(0x1A25, "Too many SDO in progress. Aborted.", i); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 getSDOlineOnUse (CO_Data* d, UNS8 nodeId, UNS8 whoami, UNS8 *line) etisserant@0: { etisserant@0: etisserant@0: UNS8 i; etisserant@0: etisserant@0: for (i = 0 ; i < SDO_MAX_SIMULTANEOUS_TRANSFERTS ; i++){ etisserant@0: if ( (d->transfers[i].state != SDO_RESET) && etisserant@0: (d->transfers[i].nodeId == nodeId) && etisserant@0: (d->transfers[i].whoami == whoami) ) { etisserant@0: *line = i; etisserant@0: return 0; etisserant@0: } etisserant@0: } etisserant@0: return 0xFF; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 closeSDOtransfer (CO_Data* d, UNS8 nodeId, UNS8 whoami) etisserant@0: { etisserant@0: UNS8 err; etisserant@0: UNS8 line; etisserant@0: err = getSDOlineOnUse(d, nodeId, whoami, &line); etisserant@0: if (err) { etisserant@0: MSG_WAR(0x2A30, "No SDO communication to close for node : ", nodeId); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: resetSDOline(d, line); etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 getSDOlineRestBytes (CO_Data* d, UNS8 line, UNS8 * nbBytes) etisserant@0: { frdupin@71: if (d->transfers[line].count == 0) /* if received initiate SDO protocol with e=0 and s=0 */ etisserant@0: * nbBytes = 0; etisserant@0: else etisserant@145: * nbBytes = (UNS8)d->transfers[line].count - (UNS8)d->transfers[line].offset; etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 setSDOlineRestBytes (CO_Data* d, UNS8 line, UNS8 nbBytes) etisserant@0: { etisserant@0: if (nbBytes > SDO_MAX_LENGTH_TRANSFERT) { etisserant@0: MSG_ERR(0x1A35,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFERT", nbBytes); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: d->transfers[line].count = nbBytes; etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 sendSDO (CO_Data* d, UNS8 whoami, s_SDO sdo) etisserant@0: { etisserant@0: UNS16 offset; etisserant@0: UNS16 lastIndex; etisserant@0: UNS8 found = 0; etisserant@0: Message m; etisserant@0: UNS8 i; etisserant@0: UNS32 * pwCobId = NULL; frdupin@87: UNS32 * pwNodeId = NULL; etisserant@0: etisserant@0: MSG_WAR(0x3A38, "sendSDO",0); etisserant@0: if( !((d->nodeState == Operational) || (d->nodeState == Pre_operational ))) { etisserant@0: MSG_WAR(0x2A39, "unable to send the SDO (not in op or pre-op mode", d->nodeState); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: etisserant@0: /*get the server->client cobid*/ etisserant@0: if ( whoami == SDO_SERVER ) {/*case server. Easy because today only one server SDO is authorized in CanFestival*/ etisserant@0: offset = d->firstIndex->SDO_SVR; etisserant@0: if (offset == 0) { etisserant@0: MSG_ERR(0x1A42, "SendSDO : No SDO server found", 0); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: pwCobId = d->objdict[offset].pSubindex[2].pObject; etisserant@0: MSG_WAR(0x3A41, "I am server. cobId : ", *pwCobId); etisserant@0: } etisserant@0: else { /*case client*/ etisserant@0: /* Get the client->server cobid.*/ etisserant@0: UNS16 sdoNum = 0; etisserant@0: offset = d->firstIndex->SDO_CLT; etisserant@0: lastIndex = d->lastIndex->SDO_CLT; etisserant@0: if (offset == 0) { etisserant@0: MSG_ERR(0x1A42, "SendSDO : No SDO client index found", 0); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: /* First, have to find at the index where is defined the communication with the server node */ etisserant@0: while (offset <= lastIndex){ etisserant@0: MSG_WAR(0x3A43,"Reading index : ", 0x1280 + sdoNum); etisserant@0: if (d->objdict[offset].bSubCount <= 3) { etisserant@0: MSG_ERR(0x1A28, "Subindex 3 not found at index ", 0x1280 + sdoNum); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: pwNodeId = d->objdict[offset].pSubindex[3].pObject; etisserant@0: MSG_WAR(0x3A44, "Found nodeId server = ", *pwNodeId); etisserant@0: if(*pwNodeId == sdo.nodeId) { etisserant@0: found = 1; etisserant@0: break; etisserant@0: } etisserant@0: offset ++; etisserant@0: sdoNum ++; etisserant@0: } etisserant@0: if (! found){ etisserant@0: MSG_WAR (0x2A45, "No SDO client corresponds to the mesage to send to node ", sdo.nodeId); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: /* Second, read the cobid client->server */ etisserant@0: pwCobId = d->objdict[offset].pSubindex[1].pObject; etisserant@0: } etisserant@0: /* message copy for sending */ etisserant@0: m.cob_id.w = *pwCobId; etisserant@0: m.rtr = NOT_A_REQUEST; frdupin@71: /* the length of SDO must be 8 */ etisserant@0: m.len = 8; etisserant@0: for (i = 0 ; i < 8 ; i++) { etisserant@0: m.data[i] = sdo.body.data[i]; etisserant@0: } etisserant@149: return canSend(d->canHandle,&m); etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 sendSDOabort (CO_Data* d, UNS8 whoami, UNS16 index, UNS8 subIndex, UNS32 abortCode) etisserant@0: { etisserant@0: s_SDO sdo; etisserant@0: UNS8 ret; etisserant@1: MSG_WAR(0x2A50,"Sending SDO abort ", abortCode); etisserant@0: sdo.nodeId = *d->bDeviceNodeId; etisserant@0: sdo.body.data[0] = 0x80; frdupin@71: /* Index */ frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ frdupin@71: /* Subindex */ etisserant@0: sdo.body.data[3] = subIndex; frdupin@71: /* Data */ etisserant@0: sdo.body.data[4] = (UNS8)(abortCode & 0xFF); etisserant@0: sdo.body.data[5] = (UNS8)((abortCode >> 8) & 0xFF); etisserant@0: sdo.body.data[6] = (UNS8)((abortCode >> 16) & 0xFF); etisserant@0: sdo.body.data[7] = (UNS8)((abortCode >> 24) & 0xFF); etisserant@0: ret = sendSDO(d, whoami, sdo); etisserant@0: etisserant@0: return ret; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: UNS8 proceedSDO (CO_Data* d, Message *m) etisserant@0: { etisserant@0: UNS8 err; etisserant@0: UNS8 line; frdupin@71: UNS8 nbBytes; /* received or to be transmited. */ frdupin@71: UNS8 nodeId = 0; /* The node from which the SDO is received */ frdupin@87: UNS32 nodeId_32; /* node id in 32 bits, for temporary use */ frdupin@87: UNS32 *pNodeId = NULL; frdupin@71: UNS8 whoami = SDO_UNKNOWN; /* SDO_SERVER or SDO_CLIENT.*/ frdupin@71: UNS32 errorCode; /* while reading or writing in the local object dictionary.*/ frdupin@71: s_SDO sdo; /* SDO to transmit */ etisserant@0: UNS16 index; etisserant@0: UNS8 subIndex; etisserant@0: UNS32 abortCode; etisserant@0: UNS8 i,j; etisserant@0: UNS32 * pCobId = NULL; etisserant@0: UNS16 offset; etisserant@0: UNS16 lastIndex; etisserant@0: etisserant@0: MSG_WAR(0x3A60, "proceedSDO ", 0); etisserant@0: whoami = SDO_UNKNOWN; frdupin@71: /* Looking for the cobId in the object dictionary. */ frdupin@71: /* Am-I a server ? */ etisserant@0: offset = d->firstIndex->SDO_SVR; etisserant@0: lastIndex = d->lastIndex->SDO_SVR; etisserant@0: j = 0; etisserant@0: if(offset) while (offset <= lastIndex) { etisserant@0: if (d->objdict[offset].bSubCount <= 1) { etisserant@0: MSG_ERR(0x1A61, "Subindex 1 not found at index ", 0x1200 + j); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: pCobId = d->objdict[offset].pSubindex[1].pObject; etisserant@0: if ( *pCobId == (*m).cob_id.w ) { etisserant@0: whoami = SDO_SERVER; etisserant@0: MSG_WAR(0x3A62, "proceedSDO. I am server. index : ", 0x1200 + j); frdupin@71: /* In case of server, the node id of the client may be unknown. So we put the index minus offset */ frdupin@71: /* 0x1200 where the cobid received is defined. */ etisserant@0: nodeId = j; etisserant@0: break; etisserant@0: } etisserant@0: j++; etisserant@0: offset++; frdupin@71: } /* end while */ etisserant@0: if (whoami == SDO_UNKNOWN) { frdupin@71: /* Am-I client ? */ etisserant@0: offset = d->firstIndex->SDO_CLT; etisserant@0: lastIndex = d->lastIndex->SDO_CLT; etisserant@0: j = 0; etisserant@0: if(offset) while (offset <= lastIndex) { etisserant@0: if (d->objdict[offset].bSubCount <= 3) { etisserant@0: MSG_ERR(0x1A63, "Subindex 3 not found at index ", 0x1280 + j); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* a) Looking for the cobid received. */ etisserant@0: pCobId = d->objdict[offset].pSubindex[2].pObject; etisserant@0: if (*pCobId == (*m).cob_id.w ) { frdupin@71: /* b) cobid found, so reading the node id of the server. */ etisserant@0: pNodeId = d->objdict[offset].pSubindex[3].pObject; etisserant@0: whoami = SDO_CLIENT; frdupin@87: nodeId_32 = *pNodeId; frdupin@87: nodeId = (UNS8)nodeId_32; etisserant@0: MSG_WAR(0x3A64, "proceedSDO. I am server. index : ", 0x1280 + j); etisserant@0: MSG_WAR(0x3A65, " Server nodeId : ", nodeId); etisserant@0: break; etisserant@0: } etisserant@0: j++; etisserant@0: offset++; frdupin@71: } /* end while */ etisserant@0: } etisserant@0: if (whoami == SDO_UNKNOWN) { frdupin@71: return 0xFF;/* This SDO was not for us ! */ frdupin@71: } frdupin@71: frdupin@71: /* Test if the size of the SDO is ok */ etisserant@0: if ( (*m).len != 8) { etisserant@0: MSG_ERR(0x1A67, "Error size SDO. CobId : ", (*m).cob_id.w); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: etisserant@0: if (whoami == SDO_CLIENT) { etisserant@0: MSG_WAR(0x3A68, "I am CLIENT. Received SDO from nodeId : ", nodeId); etisserant@0: } etisserant@0: else { etisserant@0: MSG_WAR(0x3A69, "I am SERVER. Received SDO cobId : ", (*m).cob_id.w); etisserant@0: } etisserant@0: frdupin@71: /* Testing the command specifier */ frdupin@71: /* Allowed : cs = 0, 1, 2, 3, 4. (= all except those for block tranfert). */ frdupin@71: /* cs = other : Not allowed -> abort. */ etisserant@0: switch (getSDOcs(m->data[0])) { etisserant@0: etisserant@0: case 0: frdupin@71: /* I am SERVER */ etisserant@0: if (whoami == SDO_SERVER) { frdupin@71: /* Receiving a download segment data. */ frdupin@71: /* A SDO transfert should have been yet initiated. */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A70, "SDO error : Received download segment for unstarted trans. index 0x1200 + ", etisserant@0: nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: MSG_WAR(0x3A71, "Received SDO download segment defined at index 0x1200 + ", nodeId); etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; frdupin@71: /* Toggle test. */ etisserant@0: if (d->transfers[line].toggle != getSDOt(m->data[0])) { etisserant@0: MSG_ERR(0x1A72, "SDO error : Toggle error : ", getSDOt(m->data[0])); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Nb of data to be downloaded */ etisserant@0: nbBytes = 7 - getSDOn3(m->data[0]); frdupin@71: /* Store the data in the transfert structure. */ etisserant@0: err = SDOtoLine(d, line, nbBytes, (*m).data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Sending the SDO response, CS = 1 */ frdupin@71: sdo.nodeId = *d->bDeviceNodeId; /* The node id of the server, (here it is the sender). */ etisserant@0: sdo.body.data[0] = (1 << 5) | (d->transfers[line].toggle << 4); etisserant@0: for (i = 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: MSG_WAR(0x3A73, "SDO. Send response to download request defined at index 0x1200 + ", nodeId); etisserant@0: sendSDO(d, whoami, sdo); frdupin@71: /* Inverting the toggle for the next segment. */ etisserant@0: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; frdupin@71: /* If it was the last segment, */ etisserant@0: if (getSDOc(m->data[0])) { frdupin@71: /* Transfering line data to object dictionary. */ frdupin@71: /* The code does not use the "d" of initiate frame. So it is safe if e=s=0 */ etisserant@0: errorCode = SDOlineToObjdict(d, line); etisserant@0: if (errorCode) { etisserant@0: MSG_ERR(0x1A54, "SDO error : Unable to copy the data in the object dictionary", 0); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, errorCode); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Release of the line */ etisserant@0: resetSDOline(d, line); etisserant@0: MSG_WAR(0x3A74, "SDO. End of download defined at index 0x1200 + ", nodeId); etisserant@0: } frdupin@71: } /* end if SERVER */ frdupin@71: else { /* if CLIENT */ frdupin@71: /* I am CLIENT */ frdupin@71: /* It is a request for a previous upload segment. We should find a line opened for this.*/ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A75, "SDO error : Received segment response for unknown trans. from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; frdupin@71: /* test of the toggle; */ etisserant@0: if (d->transfers[line].toggle != getSDOt(m->data[0])) { etisserant@0: MSG_ERR(0x1A76, "SDO error : Received segment response Toggle error. from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* nb of data to be uploaded */ etisserant@0: nbBytes = 7 - getSDOn3(m->data[0]); frdupin@71: /* Storing the data in the line structure. */ etisserant@0: err = SDOtoLine(d, line, nbBytes, (*m).data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Inverting the toggle for the next segment. */ etisserant@0: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; frdupin@71: /* If it was the last segment,*/ etisserant@0: if ( getSDOc(m->data[0])) { frdupin@71: /* Put in state finished */ frdupin@71: /* The code is safe for the case e=s=0 in initiate frame. */ etisserant@0: StopSDO_TIMER(line) etisserant@0: d->transfers[line].state = SDO_FINISHED; etisserant@32: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); etisserant@32: etisserant@0: MSG_WAR(0x3A77, "SDO. End of upload from node : ", nodeId); etisserant@0: } frdupin@71: else { /* more segments to receive */ frdupin@71: /* Sending the request for the next segment. */ etisserant@0: sdo.nodeId = nodeId; etisserant@0: sdo.body.data[0] = (3 << 5) | (d->transfers[line].toggle << 4); etisserant@0: for (i = 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: sendSDO(d, whoami, sdo); etisserant@0: MSG_WAR(0x3A78, "SDO send upload segment request to nodeId", nodeId); etisserant@0: } frdupin@71: } /* End if CLIENT */ etisserant@0: break; etisserant@0: etisserant@0: case 1: frdupin@71: /* I am SERVER */ frdupin@71: /* Receive of an initiate download */ etisserant@0: if (whoami == SDO_SERVER) { etisserant@0: index = getSDOindex(m->data[1],m->data[2]); etisserant@0: subIndex = getSDOsubIndex(m->data[3]); etisserant@0: MSG_WAR(0x3A79, "Received SDO Initiate Download (to store data) defined at index 0x1200 + ", etisserant@0: nodeId); etisserant@0: MSG_WAR(0x3A80, "Writing at index : ", index); etisserant@0: MSG_WAR(0x3A80, "Writing at subIndex : ", subIndex); etisserant@0: frdupin@71: /* Search if a SDO transfert have been yet initiated */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (! err) { etisserant@0: MSG_ERR(0x1A81, "SDO error : Transmission yet started.", 0); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* No line on use. Great ! */ frdupin@71: /* Try to open a new line. */ etisserant@0: err = getSDOfreeLine( d, whoami, &line ); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A82, "SDO error : No line free, too many SDO in progress. Aborted.", 0); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: initSDOline(d, line, nodeId, index, subIndex, SDO_DOWNLOAD_IN_PROGRESS); etisserant@0: frdupin@71: if (getSDOe(m->data[0])) { /* If SDO expedited */ frdupin@71: /* nb of data to be downloaded */ etisserant@0: nbBytes = 4 - getSDOn2(m->data[0]); frdupin@71: /* Storing the data in the line structure. */ etisserant@0: d->transfers[line].count = nbBytes; etisserant@0: err = SDOtoLine(d, line, nbBytes, (*m).data + 4); etisserant@0: etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: frdupin@71: /* SDO expedited -> transfert finished. Data can be stored in the dictionary. */ frdupin@71: /* The line will be reseted when it is downloading in the dictionary. */ etisserant@0: MSG_WAR(0x3A83, "SDO Initiate Download is an expedited transfert. Finished.: ", nodeId); frdupin@71: /* Transfering line data to object dictionary. */ etisserant@0: errorCode = SDOlineToObjdict(d, line); etisserant@0: if (errorCode) { etisserant@0: MSG_ERR(0x1A84, "SDO error : Unable to copy the data in the object dictionary", 0); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, errorCode); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Release of the line. */ etisserant@0: resetSDOline(d, line); etisserant@0: } frdupin@71: else {/* So, if it is not an expedited transfert */ etisserant@0: if (getSDOs(m->data[0])) { frdupin@71: /* TODO : if e and s = 0, not reading m->data[4] but put nbBytes = 0 */ frdupin@71: nbBytes = m->data[4]; /* Transfert limited to 255 bytes. */ etisserant@0: err = setSDOlineRestBytes(d, nodeId, nbBytes); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: } etisserant@0: } frdupin@71: /*Sending a SDO, cs=3*/ frdupin@71: sdo.nodeId = *d->bDeviceNodeId; /* The node id of the server, (here it is the sender).*/ etisserant@0: sdo.body.data[0] = 3 << 5; frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ etisserant@0: sdo.body.data[3] = subIndex; etisserant@0: for (i = 4 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: sendSDO(d, whoami, sdo); frdupin@71: } /* end if I am SERVER */ etisserant@0: else { frdupin@71: /* I am CLIENT */ frdupin@71: /* It is a response for a previous download segment. We should find a line opened for this. */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A85, "SDO error : Received segment response for unknown trans. from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; frdupin@71: /* test of the toggle; */ etisserant@0: if (d->transfers[line].toggle != getSDOt(m->data[0])) { etisserant@0: MSG_ERR(0x1A86, "SDO error : Received segment response Toggle error. from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: frdupin@71: /* End transmission or downloading next segment. We need to know if it will be the last one. */ etisserant@0: getSDOlineRestBytes(d, line, &nbBytes); etisserant@0: if (nbBytes == 0) { etisserant@0: MSG_WAR(0x3A87, "SDO End download. segment response received. OK. from nodeId", nodeId); etisserant@0: StopSDO_TIMER(line) etisserant@0: d->transfers[line].state = SDO_FINISHED; etisserant@32: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); etisserant@0: return 0x00; etisserant@0: } frdupin@71: /* At least one transfer to send. */ etisserant@0: if (nbBytes > 7) { frdupin@71: /* several segments to download.*/ frdupin@71: /* code to send the next segment. (cs = 0; c = 0) */ etisserant@0: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4); etisserant@0: err = lineToSDO(d, line, 7, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: } etisserant@0: else { frdupin@71: /* Last segment. */ frdupin@71: /* code to send the last segment. (cs = 0; c = 1)*/ etisserant@0: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1; etisserant@0: err = lineToSDO(d, line, nbBytes, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: for (i = nbBytes + 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: } etisserant@0: MSG_WAR(0x3A88, "SDO sending download segment to nodeId", nodeId); etisserant@0: sendSDO(d, whoami, sdo); frdupin@71: } /* end if I am a CLIENT */ etisserant@0: break; etisserant@0: etisserant@0: case 2: frdupin@71: /* I am SERVER */ frdupin@71: /* Receive of an initiate upload.*/ etisserant@0: if (whoami == SDO_SERVER) { etisserant@0: index = getSDOindex(m->data[1],m->data[2]); etisserant@0: subIndex = getSDOsubIndex(m->data[3]); etisserant@0: MSG_WAR(0x3A89, "Received SDO Initiate upload (to send data) defined at index 0x1200 + ", etisserant@0: nodeId); etisserant@0: MSG_WAR(0x3A90, "Reading at index : ", index); etisserant@0: MSG_WAR(0x3A91, "Reading at subIndex : ", subIndex); frdupin@71: /* Search if a SDO transfert have been yet initiated*/ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (! err) { dejoigny@12: MSG_ERR(0x1A92, "SDO error : Transmission yet started at line : ", line); etisserant@0: MSG_WAR(0x3A93, "nodeId = ", nodeId); dejoigny@12: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); dejoigny@12: return 0xFF; etisserant@0: } frdupin@71: /* No line on use. Great !*/ frdupin@71: /* Try to open a new line.*/ etisserant@0: err = getSDOfreeLine( d, whoami, &line ); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A71, "SDO error : No line free, too many SDO in progress. Aborted.", 0); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: initSDOline(d, line, nodeId, index, subIndex, SDO_UPLOAD_IN_PROGRESS); frdupin@71: /* Transfer data from dictionary to the line structure. */ etisserant@0: errorCode = objdictToSDOline(d, line); etisserant@0: etisserant@0: if (errorCode) { etisserant@0: MSG_ERR(0x1A94, "SDO error : Unable to copy the data from object dictionary. Err code : ", etisserant@0: errorCode); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, errorCode); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Preparing the response.*/ frdupin@71: getSDOlineRestBytes(d, line, &nbBytes); /* Nb bytes to transfer ? */ frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: if (nbBytes > 4) { frdupin@71: /* normal transfert. (segmented). */ frdupin@71: /* code to send the initiate upload response. (cs = 2) */ etisserant@0: sdo.body.data[0] = (2 << 5) | 1; frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ etisserant@0: sdo.body.data[3] = subIndex; frdupin@71: sdo.body.data[4] = nbBytes; /* Limitation of canfestival2 : Max tranfert is 256 bytes.*/ frdupin@71: /* It takes too much memory to upgrate to 2^32 because the size of data is also coded */ frdupin@71: /* in the object dictionary, at every index and subindex. */ etisserant@0: for (i = 5 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: MSG_WAR(0x3A95, "SDO. Sending normal upload initiate response defined at index 0x1200 + ", nodeId); etisserant@0: sendSDO(d, whoami, sdo); etisserant@0: } etisserant@0: else { frdupin@71: /* Expedited upload. (cs = 2 ; e = 1) */ etisserant@0: sdo.body.data[0] = (2 << 5) | ((4 - nbBytes) << 2) | 3; frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ etisserant@0: sdo.body.data[3] = subIndex; etisserant@0: err = lineToSDO(d, line, nbBytes, sdo.body.data + 4); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: for (i = 4 + nbBytes ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: MSG_WAR(0x3A96, "SDO. Sending expedited upload initiate response defined at index 0x1200 + ", etisserant@0: nodeId); etisserant@0: sendSDO(d, whoami, sdo); frdupin@71: /* Release the line.*/ etisserant@0: resetSDOline(d, line); etisserant@0: } frdupin@71: } /* end if I am SERVER*/ etisserant@0: else { frdupin@71: /* I am CLIENT */ frdupin@71: /* It is the response for the previous initiate upload request.*/ frdupin@71: /* We should find a line opened for this. */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1A97, "SDO error : Received response for unknown upload request from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; etisserant@0: frdupin@71: if (getSDOe(m->data[0])) { /* If SDO expedited */ frdupin@71: /* nb of data to be uploaded */ etisserant@0: nbBytes = 4 - getSDOn2(m->data[0]); frdupin@71: /* Storing the data in the line structure. */ etisserant@0: err = SDOtoLine(d, line, nbBytes, (*m).data + 4); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* SDO expedited -> transfert finished. data are available via getReadResultNetworkDict(). */ etisserant@0: MSG_WAR(0x3A98, "SDO expedited upload finished. Response received from node : ", nodeId); etisserant@0: StopSDO_TIMER(line) etisserant@0: d->transfers[line].count = nbBytes; etisserant@0: d->transfers[line].state = SDO_FINISHED; etisserant@32: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); etisserant@0: return 0; etisserant@0: } frdupin@71: else { /* So, if it is not an expedited transfert */ frdupin@71: /* Storing the nb of data to receive. */ etisserant@0: if (getSDOs(m->data[0])) { frdupin@71: nbBytes = m->data[4]; /* Remember the limitation to 255 bytes to transfert */ etisserant@0: err = setSDOlineRestBytes(d, line, nbBytes); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: } frdupin@71: /* Requesting next segment. (cs = 3) */ etisserant@0: sdo.nodeId = nodeId; etisserant@0: sdo.body.data[0] = 3 << 5; etisserant@0: for (i = 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: MSG_WAR(0x3A99, "SDO. Sending upload segment request to node : ", nodeId); etisserant@0: sendSDO(d, whoami, sdo); etisserant@0: } frdupin@71: } /* End if CLIENT */ etisserant@0: break; etisserant@0: etisserant@0: case 3: frdupin@71: /* I am SERVER */ etisserant@0: if (whoami == SDO_SERVER) { frdupin@71: /* Receiving a upload segment. */ frdupin@71: /* A SDO transfert should have been yet initiated. */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AA0, "SDO error : Received upload segment for unstarted trans. index 0x1200 + ", etisserant@0: nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: MSG_WAR(0x3AA1, "Received SDO upload segment defined at index 0x1200 + ", nodeId); etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; frdupin@71: /* Toggle test.*/ etisserant@0: if (d->transfers[line].toggle != getSDOt(m->data[0])) { etisserant@0: MSG_ERR(0x1AA2, "SDO error : Toggle error : ", getSDOt(m->data[0])); etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Uploading next segment. We need to know if it will be the last one. */ etisserant@0: getSDOlineRestBytes(d, line, &nbBytes); etisserant@0: if (nbBytes > 7) { frdupin@71: /* The segment to transfer is not the last one.*/ frdupin@71: /* code to send the next segment. (cs = 0; c = 0) */ frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4); etisserant@0: err = lineToSDO(d, line, 7, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Inverting the toggle for the next tranfert. */ etisserant@0: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; etisserant@0: MSG_WAR(0x3AA3, "SDO. Sending upload segment defined at index 0x1200 + ", nodeId); etisserant@0: sendSDO(d, whoami, sdo); etisserant@0: } etisserant@0: else { frdupin@71: /* Last segment. */ frdupin@71: /* code to send the last segment. (cs = 0; c = 1) */ frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1; etisserant@0: err = lineToSDO(d, line, nbBytes, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: for (i = nbBytes + 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: MSG_WAR(0x3AA4, "SDO. Sending last upload segment defined at index 0x1200 + ", nodeId); etisserant@0: sendSDO(d, whoami, sdo); frdupin@71: /* Release the line */ etisserant@0: resetSDOline(d, line); etisserant@0: } frdupin@71: } /* end if SERVER*/ etisserant@0: else { frdupin@71: /* I am CLIENT */ frdupin@71: /* It is the response for the previous initiate download request. */ frdupin@71: /* We should find a line opened for this. */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line); etisserant@0: if (!err) etisserant@0: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AA5, "SDO error : Received response for unknown download request from nodeId", nodeId); etisserant@0: failedSDO(d, nodeId, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Reset the wathdog */ etisserant@0: RestartSDO_TIMER(line) etisserant@0: index = d->transfers[line].index; etisserant@0: subIndex = d->transfers[line].subIndex; frdupin@71: /* End transmission or requesting next segment. */ etisserant@0: getSDOlineRestBytes(d, line, &nbBytes); etisserant@0: if (nbBytes == 0) { etisserant@0: MSG_WAR(0x3AA6, "SDO End download expedited. Response received. from nodeId", nodeId); etisserant@0: StopSDO_TIMER(line) etisserant@0: d->transfers[line].state = SDO_FINISHED; etisserant@32: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); etisserant@0: return 0x00; etisserant@0: } etisserant@0: if (nbBytes > 7) { frdupin@71: /* more than one request to send */ frdupin@71: /* code to send the next segment. (cs = 0; c = 0) */ frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4); etisserant@0: err = lineToSDO(d, line, 7, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: } etisserant@0: else { frdupin@71: /* Last segment.*/ frdupin@71: /* code to send the last segment. (cs = 0; c = 1) */ frdupin@71: sdo.nodeId = nodeId; /* The server node Id; */ etisserant@0: sdo.body.data[0] = (d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1; etisserant@0: err = lineToSDO(d, line, nbBytes, sdo.body.data + 1); etisserant@0: if (err) { etisserant@0: failedSDO(d, nodeId, whoami, index, subIndex, SDOABT_GENERAL_ERROR); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: for (i = nbBytes + 1 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: } etisserant@0: MSG_WAR(0x3AA7, "SDO sending download segment to nodeId", nodeId); etisserant@0: sendSDO(d, whoami, sdo); etisserant@0: frdupin@71: } /* end if I am a CLIENT */ etisserant@0: break; etisserant@0: frdupin@82: case 4: frdupin@82: abortCode = (*m).data[3] | frdupin@82: ((UNS32)m->data[5] << 8) | frdupin@82: ((UNS32)m->data[6] << 16) | frdupin@82: ((UNS32)m->data[7] << 24); frdupin@71: /* Received SDO abort. */ frdupin@71: /* Looking for the line concerned. */ etisserant@0: if (whoami == SDO_SERVER) { etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (!err) { etisserant@0: resetSDOline( d, line ); etisserant@0: MSG_WAR(0x3AA8, "SD0. Received SDO abort. Line released. Code : ", abortCode); etisserant@0: } etisserant@0: else etisserant@0: MSG_WAR(0x3AA9, "SD0. Received SDO abort. No line found. Code : ", abortCode); frdupin@71: /* Tips : The end user has no way to know that the server node has received an abort SDO. */ frdupin@71: /* Its is ok, I think.*/ etisserant@0: } frdupin@71: else { /* If I am CLIENT */ etisserant@0: err = getSDOlineOnUse( d, nodeId, whoami, &line ); etisserant@0: if (!err) { frdupin@71: /* The line *must* be released by the core program. */ etisserant@0: StopSDO_TIMER(line) etisserant@0: d->transfers[line].state = SDO_ABORTED_RCV; etisserant@0: MSG_WAR(0x3AB0, "SD0. Received SDO abort. Line state ABORTED. Code : ", abortCode); etisserant@0: } etisserant@0: else etisserant@0: MSG_WAR(0x3AB1, "SD0. Received SDO abort. No line found. Code : ", abortCode); etisserant@0: } etisserant@0: break; etisserant@0: default: frdupin@71: /* Error : Unknown cs */ etisserant@0: MSG_ERR(0x1AB2, "SDO. Received unknown command specifier : ", getSDOcs(m->data[0])); etisserant@0: return 0xFF; etisserant@0: frdupin@71: } /* End switch */ etisserant@0: return 0; etisserant@0: } etisserant@0: etisserant@0: /*******************************************************************)******/ frdupin@71: INLINE UNS8 _writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, etisserant@32: UNS8 subIndex, UNS8 count, UNS8 dataType, void *data, SDOCallback_t Callback) etisserant@0: { etisserant@0: UNS8 err; etisserant@0: UNS8 SDOfound = 0; etisserant@0: UNS8 line; frdupin@71: s_SDO sdo; /* SDO to transmit */ etisserant@0: UNS8 i, j; etisserant@0: UNS16 lastIndex; etisserant@0: UNS16 offset; frdupin@86: UNS32 *pNodeIdServer; frdupin@86: UNS32 nodeIdServer; etisserant@0: etisserant@0: MSG_WAR(0x3AC0, "Send SDO to write in the dictionary of node : ", nodeId); etisserant@0: MSG_WAR(0x3AC1, " At index : ", index); etisserant@0: MSG_WAR(0x3AC2, " subIndex : ", subIndex); etisserant@0: MSG_WAR(0x3AC3, " nb bytes : ", count); etisserant@0: frdupin@71: /* Verify that there is no SDO communication yet. */ etisserant@0: err = getSDOlineOnUse(d, nodeId, SDO_CLIENT, &line); etisserant@0: if (!err) { etisserant@0: MSG_ERR(0x1AC4, "SDO error : Communication yet established. with node : ", nodeId); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Taking the line ... */ etisserant@0: err = getSDOfreeLine( d, SDO_CLIENT, &line ); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AC5, "SDO error : No line free, too many SDO in progress. Aborted for node : ", nodeId); etisserant@0: return (0xFF); etisserant@0: } frdupin@71: /* Check which SDO to use to communicate with the node */ etisserant@0: offset = d->firstIndex->SDO_CLT; etisserant@0: lastIndex = d->lastIndex->SDO_CLT; etisserant@0: if (offset == 0) { etisserant@0: MSG_ERR(0x1AC6, "writeNetworkDict : No SDO client index found", 0); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: i = 0; etisserant@0: while (offset <= lastIndex) { etisserant@0: if (d->objdict[offset].bSubCount <= 3) { etisserant@0: MSG_ERR(0x1AC8, "Subindex 3 not found at index ", 0x1280 + i); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* looking for the nodeId server */ etisserant@0: pNodeIdServer = d->objdict[offset].pSubindex[3].pObject; etisserant@0: nodeIdServer = *pNodeIdServer; etisserant@0: MSG_WAR(0x1AD2, "index : ", 0x1280 + i); etisserant@0: MSG_WAR(0x1AD3, "nodeIdServer : ", nodeIdServer); etisserant@0: frdupin@86: if(nodeIdServer == (UNS32)nodeId) { etisserant@0: SDOfound = 1; etisserant@0: break; etisserant@0: } etisserant@0: offset++; etisserant@0: i++; frdupin@71: } /* end while */ etisserant@0: if (!SDOfound) { etisserant@0: MSG_ERR(0x1AC9, "SDO. Error. No client found to communicate with node : ", nodeId); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: MSG_WAR(0x3AD0," SDO client defined at index : ", 0x1280 + i); etisserant@0: initSDOline(d, line, nodeId, index, subIndex, SDO_DOWNLOAD_IN_PROGRESS); etisserant@0: d->transfers[line].count = count; etisserant@0: d->transfers[line].dataType = dataType; etisserant@0: frdupin@71: /* Copy data to transfers structure. */ etisserant@0: for (j = 0 ; j < count ; j++) { etisserant@0: # ifdef CANOPEN_BIG_ENDIAN etisserant@0: if (dataType == 0) etisserant@0: d->transfers[line].data[count - 1 - j] = ((char *)data)[j]; frdupin@71: else /* String of bytes. */ etisserant@0: d->transfers[line].data[j] = ((char *)data)[j]; etisserant@0: # else etisserant@0: d->transfers[line].data[j] = ((char *)data)[j]; etisserant@0: # endif etisserant@0: } frdupin@71: /* Send the SDO to the server. Initiate download, cs=1. */ etisserant@0: sdo.nodeId = nodeId; frdupin@71: if (count <= 4) { /* Expedited transfert */ etisserant@0: sdo.body.data[0] = (1 << 5) | ((4 - count) << 2) | 3; etisserant@0: for (i = 4 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = d->transfers[line].data[i - 4]; etisserant@0: d->transfers[line].offset = count; etisserant@0: } frdupin@71: else { /* Normal transfert */ etisserant@0: sdo.body.data[0] = (1 << 5) | 1; frdupin@71: sdo.body.data[4] = count; /* nb of byte to transmit. Max = 255. (canfestival2 limitation). */ etisserant@0: for (i = 5 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: } frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ etisserant@0: sdo.body.data[3] = subIndex; etisserant@0: etisserant@0: err = sendSDO(d, SDO_CLIENT, sdo); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AD1, "SDO. Error while sending SDO to node : ", nodeId); frdupin@71: /* release the line */ etisserant@0: resetSDOline(d, line); etisserant@0: return 0xFF; etisserant@32: } etisserant@32: d->transfers[line].Callback = Callback; etisserant@0: return 0; etisserant@0: } frdupin@53: frdupin@53: /*--------------------------------------------------------------------------*/ frdupin@53: etisserant@32: UNS8 writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, etisserant@32: UNS8 subIndex, UNS8 count, UNS8 dataType, void *data) etisserant@32: { etisserant@32: return _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, NULL); etisserant@32: } etisserant@32: frdupin@53: /*--------------------------------------------------------------------------*/ frdupin@53: etisserant@32: UNS8 writeNetworkDictCallBack (CO_Data* d, UNS8 nodeId, UNS16 index, etisserant@32: UNS8 subIndex, UNS8 count, UNS8 dataType, void *data, SDOCallback_t Callback) etisserant@32: { etisserant@32: return _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, Callback); etisserant@32: } etisserant@32: etisserant@32: etisserant@32: /***************************************************************************/ frdupin@71: INLINE UNS8 _readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, SDOCallback_t Callback) etisserant@0: { etisserant@0: UNS8 err; etisserant@0: UNS8 SDOfound = 0; etisserant@0: UNS8 i; etisserant@0: UNS8 line; frdupin@71: s_SDO sdo; /* SDO to transmit */ frdupin@86: UNS32 *pNodeIdServer; frdupin@86: UNS32 nodeIdServer; etisserant@0: UNS16 offset; etisserant@0: UNS16 lastIndex; etisserant@0: MSG_WAR(0x3AD5, "Send SDO to read in the dictionary of node : ", nodeId); etisserant@0: MSG_WAR(0x3AD6, " At index : ", index); etisserant@0: MSG_WAR(0x3AD7, " subIndex : ", subIndex); etisserant@0: etisserant@0: frdupin@71: /* Verify that there is no SDO communication yet. */ etisserant@0: err = getSDOlineOnUse(d, nodeId, SDO_CLIENT, &line); etisserant@0: if (!err) { etisserant@0: MSG_ERR(0x1AD8, "SDO error : Communication yet established. with node : ", nodeId); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* Taking the line ... */ etisserant@0: err = getSDOfreeLine( d, SDO_CLIENT, &line ); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AD9, "SDO error : No line free, too many SDO in progress. Aborted for node : ", nodeId); etisserant@0: return (0xFF); etisserant@0: } etisserant@0: else etisserant@0: MSG_WAR(0x3AE0, "Transmission on line : ", line); etisserant@0: frdupin@71: /* Check which SDO to use to communicate with the node */ etisserant@0: offset = d->firstIndex->SDO_CLT; etisserant@0: lastIndex = d->lastIndex->SDO_CLT; etisserant@0: if (offset == 0) { etisserant@0: MSG_ERR(0x1AE1, "writeNetworkDict : No SDO client index found", 0); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: i = 0; etisserant@0: while (offset <= lastIndex) { etisserant@0: if (d->objdict[offset].bSubCount <= 3) { etisserant@0: MSG_ERR(0x1AE2, "Subindex 3 not found at index ", 0x1280 + i); etisserant@0: return 0xFF; etisserant@0: } frdupin@71: /* looking for the nodeId server */ etisserant@0: pNodeIdServer = d->objdict[offset].pSubindex[3].pObject; etisserant@0: nodeIdServer = *pNodeIdServer; etisserant@0: frdupin@86: if(nodeIdServer == (UNS32)nodeId) { etisserant@0: SDOfound = 1; etisserant@0: break; etisserant@0: } etisserant@0: offset++; etisserant@0: i++; frdupin@71: } /* end while */ etisserant@0: if (!SDOfound) { etisserant@0: MSG_ERR(0x1AE3, "SDO. Error. No client found to communicate with node : ", nodeId); etisserant@0: return 0xFF; etisserant@0: } etisserant@0: MSG_WAR(0x3AE4," SDO client defined at index : ", 0x1280 + i); etisserant@0: initSDOline(d, line, nodeId, index, subIndex, SDO_UPLOAD_IN_PROGRESS); etisserant@0: getSDOlineOnUse(d, nodeId, SDO_CLIENT, &line); etisserant@0: sdo.nodeId = nodeId; frdupin@71: /* Send the SDO to the server. Initiate upload, cs=2. */ etisserant@0: d->transfers[line].dataType = dataType; etisserant@0: sdo.body.data[0] = (2 << 5); frdupin@71: sdo.body.data[1] = index & 0xFF; /* LSB */ frdupin@71: sdo.body.data[2] = (index >> 8) & 0xFF; /* MSB */ etisserant@0: sdo.body.data[3] = subIndex; etisserant@0: for (i = 4 ; i < 8 ; i++) etisserant@0: sdo.body.data[i] = 0; etisserant@0: err = sendSDO(d, SDO_CLIENT, sdo); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AE5, "SDO. Error while sending SDO to node : ", nodeId); frdupin@71: /* release the line */ etisserant@0: resetSDOline(d, line); etisserant@0: return 0xFF; etisserant@0: } etisserant@32: d->transfers[line].Callback = Callback; etisserant@0: return 0; etisserant@0: } etisserant@0: frdupin@53: /*--------------------------------------------------------------------------*/ frdupin@53: etisserant@32: UNS8 readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType) etisserant@32: { etisserant@32: return _readNetworkDict (d, nodeId, index, subIndex, dataType, NULL); etisserant@32: } etisserant@32: frdupin@53: /*--------------------------------------------------------------------------*/ etisserant@32: UNS8 readNetworkDictCallback (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, SDOCallback_t Callback) etisserant@32: { etisserant@32: return _readNetworkDict (d, nodeId, index, subIndex, dataType, Callback); etisserant@32: } frdupin@53: etisserant@0: /***************************************************************************/ etisserant@0: etisserant@0: UNS8 getReadResultNetworkDict (CO_Data* d, UNS8 nodeId, void* data, UNS8 *size, etisserant@0: UNS32 * abortCode) etisserant@0: { etisserant@0: UNS8 i; etisserant@0: UNS8 err; etisserant@0: UNS8 line; etisserant@0: * size = 0; etisserant@0: frdupin@71: /* Looking for the line tranfert. */ etisserant@0: err = getSDOlineOnUse(d, nodeId, SDO_CLIENT, &line); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AF0, "SDO error : No line found for communication with node : ", nodeId); etisserant@0: return SDO_ABORTED_INTERNAL; etisserant@0: } etisserant@0: if (d->transfers[line].state != SDO_FINISHED) etisserant@0: return d->transfers[line].state; etisserant@0: frdupin@71: /* Transfert is finished. Put the value in the data. */ etisserant@145: * size = (UNS8)d->transfers[line].count; etisserant@0: for ( i = 0 ; i < *size ; i++) { etisserant@0: # ifdef CANOPEN_BIG_ENDIAN etisserant@0: if (d->transfers[line].dataType != visible_string) etisserant@0: ( (char *) data)[*size - 1 - i] = d->transfers[line].data[i]; frdupin@71: else /* String of bytes. */ etisserant@0: ( (char *) data)[i] = d->transfers[line].data[i]; etisserant@0: # else etisserant@0: ( (char *) data)[i] = d->transfers[line].data[i]; etisserant@0: # endif etisserant@0: } etisserant@0: return SDO_FINISHED; etisserant@0: } etisserant@0: etisserant@0: /***************************************************************************/ etisserant@0: etisserant@0: UNS8 getWriteResultNetworkDict (CO_Data* d, UNS8 nodeId, UNS32 * abortCode) etisserant@0: { etisserant@0: UNS8 line = 0; etisserant@0: UNS8 err; etisserant@0: etisserant@0: * abortCode = 0; frdupin@71: /* Looking for the line tranfert. */ etisserant@0: err = getSDOlineOnUse(d, nodeId, SDO_CLIENT, &line); etisserant@0: if (err) { etisserant@0: MSG_ERR(0x1AF1, "SDO error : No line found for communication with node : ", nodeId); etisserant@0: return SDO_ABORTED_INTERNAL; etisserant@0: } etisserant@0: * abortCode = d->transfers[line].abortCode; etisserant@0: return d->transfers[line].state; etisserant@0: } etisserant@1: etisserant@149: void _SDOtimeoutError (UNS8 line){}