etisserant@0: /* fbeaulier@663: This file is part of CanFestival, a library implementing CanOpen Stack. fbeaulier@663: fbeaulier@663: Copyright (C): Edouard TISSERANT and Francis DUPIN fbeaulier@663: fbeaulier@663: See COPYING file for copyrights details. fbeaulier@663: fbeaulier@663: This library is free software; you can redistribute it and/or fbeaulier@663: modify it under the terms of the GNU Lesser General Public fbeaulier@663: License as published by the Free Software Foundation; either fbeaulier@663: version 2.1 of the License, or (at your option) any later version. fbeaulier@663: fbeaulier@663: This library is distributed in the hope that it will be useful, fbeaulier@663: but WITHOUT ANY WARRANTY; without even the implied warranty of fbeaulier@663: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU fbeaulier@663: Lesser General Public License for more details. fbeaulier@663: fbeaulier@663: You should have received a copy of the GNU Lesser General Public fbeaulier@663: License along with this library; if not, write to the Free Software fbeaulier@663: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA fbeaulier@663: */ fbeaulier@663: /*! fbeaulier@663: ** @file sdo.c fbeaulier@663: ** @author Edouard TISSERANT and Francis DUPIN fbeaulier@663: ** @date Tue Jun 5 09:32:32 2007 fbeaulier@663: ** fbeaulier@663: ** @brief fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: */ etisserant@0: frdupin@84: /* #define DEBUG_WAR_CONSOLE_ON */ frdupin@84: /* #define DEBUG_ERR_CONSOLE_ON */ etisserant@0: fbeaulier@667: #include fbeaulier@667: etisserant@383: #include "canfestival.h" etisserant@370: #include "sysdep.h" etisserant@0: frdupin@71: /* Uncomment if your compiler does not support inline functions */ etisserant@506: #define NO_INLINE frdupin@71: frdupin@71: #ifdef NO_INLINE fbeaulier@663: #define INLINE frdupin@71: #else fbeaulier@663: #define INLINE inline frdupin@71: #endif frdupin@71: frdupin@53: /*Internals prototypes*/ frdupin@53: Mongo@669: UNS8 GetSDOClientFromNodeId( CO_Data* d, UNS8 nodeId ); Mongo@669: nico@208: /*! fbeaulier@663: ** Called by writeNetworkDict fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param count fbeaulier@663: ** @param dataType fbeaulier@663: ** @param data fbeaulier@663: ** @param Callback fbeaulier@663: ** @param endianize fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@506: INLINE UNS8 _writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, fbeaulier@664: UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 endianize, UNS8 useBlockMode); fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** Called by readNetworkDict fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param dataType fbeaulier@663: ** @param Callback fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@506: INLINE UNS8 _readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, fbeaulier@664: UNS8 dataType, SDOCallback_t Callback, UNS8 useBlockMode); etisserant@506: frdupin@53: etisserant@0: /***************************************************************************/ frdupin@71: /* SDO (un)packing macros */ etisserant@0: etisserant@506: /** Returns the command specifier (cs, ccs, scs) from the first byte of the SDO fbeaulier@663: */ etisserant@0: #define getSDOcs(byte) (byte >> 5) etisserant@0: etisserant@506: /** Returns the number of bytes without data from the first byte of the SDO. Coded in 2 bits fbeaulier@663: */ etisserant@0: #define getSDOn2(byte) ((byte >> 2) & 3) etisserant@0: etisserant@506: /** Returns the number of bytes without data from the first byte of the SDO. Coded in 3 bits fbeaulier@663: */ etisserant@0: #define getSDOn3(byte) ((byte >> 1) & 7) etisserant@0: etisserant@506: /** Returns the transfer type from the first byte of the SDO fbeaulier@663: */ etisserant@0: #define getSDOe(byte) ((byte >> 1) & 1) etisserant@0: etisserant@506: /** Returns the size indicator from the first byte of the SDO fbeaulier@663: */ etisserant@0: #define getSDOs(byte) (byte & 1) etisserant@0: etisserant@506: /** Returns the indicator of end transmission from the first byte of the SDO fbeaulier@663: */ etisserant@0: #define getSDOc(byte) (byte & 1) etisserant@0: etisserant@506: /** Returns the toggle from the first byte of the SDO fbeaulier@663: */ etisserant@0: #define getSDOt(byte) ((byte >> 4) & 1) etisserant@0: etisserant@506: /** Returns the index from the bytes 1 and 2 of the SDO fbeaulier@663: */ edouard@604: #define getSDOindex(byte1, byte2) (((UNS16)byte2 << 8) | ((UNS16)byte1)) etisserant@0: etisserant@506: /** Returns the subIndex from the byte 3 of the SDO fbeaulier@663: */ etisserant@0: #define getSDOsubIndex(byte3) (byte3) etisserant@0: JaFojtik@694: /** Returns the subcommand in SDO block transfer fbeaulier@664: */ fbeaulier@664: #define getSDOblockSC(byte) (byte & 3) fbeaulier@664: fbeaulier@664: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param id fbeaulier@663: **/ etisserant@0: void SDOTimeoutAlarm(CO_Data* d, UNS32 id) etisserant@0: { fbeaulier@663: UNS16 offset; fbeaulier@663: UNS8 nodeId; fbeaulier@663: /* Get the client->server cobid.*/ fbeaulier@663: offset = d->firstIndex->SDO_CLT; fbeaulier@663: if ((offset == 0) || ((offset+d->transfers[id].CliServNbr) > d->lastIndex->SDO_CLT)) { fbeaulier@663: return ; fbeaulier@663: } hacking@700: nodeId = (UNS8) *((UNS32*) d->objdict[offset+d->transfers[id].CliServNbr].pSubindex[3].pObject); fbeaulier@663: MSG_ERR(0x1A01, "SDO timeout. SDO response not received.", 0); fbeaulier@664: MSG_WAR(0x2A02, "server node id : ", nodeId); fbeaulier@664: MSG_WAR(0x2A02, " index : ", d->transfers[id].index); fbeaulier@664: MSG_WAR(0x2A02, " subIndex : ", d->transfers[id].subIndex); fbeaulier@663: /* Reset timer handler */ fbeaulier@663: d->transfers[id].timer = TIMER_NONE; fbeaulier@663: /*Set aborted state*/ fbeaulier@663: d->transfers[id].state = SDO_ABORTED_INTERNAL; fbeaulier@663: /* Sending a SDO abort */ fbeaulier@663: sendSDOabort(d, d->transfers[id].whoami, d->transfers[id].CliServNbr, fbeaulier@663: d->transfers[id].index, d->transfers[id].subIndex, SDOABT_TIMED_OUT); fbeaulier@663: d->transfers[id].abortCode = SDOABT_TIMED_OUT; fbeaulier@663: /* Call the user function to inform of the problem.*/ fbeaulier@663: if(d->transfers[id].Callback) fbeaulier@663: /*If ther is a callback, it is responsible to close SDO transfer (client)*/ fbeaulier@663: (*d->transfers[id].Callback)(d, nodeId); fbeaulier@663: /*Reset the line if (whoami == SDO_SERVER) or the callback did not close the line. fbeaulier@663: Otherwise this sdo transfer would never be closed. */ fbeaulier@665: if(d->transfers[id].abortCode == SDOABT_TIMED_OUT) fbeaulier@665: resetSDOline(d, (UNS8)id); etisserant@0: } etisserant@0: etisserant@0: #define StopSDO_TIMER(id) \ fbeaulier@663: 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) \ fbeaulier@663: 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) \ fbeaulier@663: 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@506: /*! fbeaulier@663: ** Reset all sdo buffers fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: **/ etisserant@0: void resetSDO (CO_Data* d) etisserant@0: { fbeaulier@663: UNS8 j; fbeaulier@663: fbeaulier@663: /* transfer structure initialization */ JaFojtik@694: for (j = 0 ; j < SDO_MAX_SIMULTANEOUS_TRANSFERS ; j++) fbeaulier@663: resetSDOline(d, j); fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@0: UNS32 SDOlineToObjdict (CO_Data* d, UNS8 line) etisserant@0: { fbeaulier@663: UNS32 size; fbeaulier@663: UNS32 errorCode; fbeaulier@663: MSG_WAR(0x3A08, "Enter in SDOlineToObjdict ", line); fbeaulier@663: /* if SDO initiated with e=0 and s=0 count is null, offset carry effective size*/ fbeaulier@663: if( d->transfers[line].count == 0) fbeaulier@663: d->transfers[line].count = d->transfers[line].offset; fbeaulier@663: size = d->transfers[line].count; Christian@636: Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION JaFojtik@694: if (size > SDO_MAX_LENGTH_TRANSFER) fbeaulier@663: { fbeaulier@663: errorCode = setODentry(d, d->transfers[line].index, d->transfers[line].subIndex, fbeaulier@663: (void *) d->transfers[line].dynamicData, &size, 1); fbeaulier@663: } fbeaulier@663: else fbeaulier@663: { fbeaulier@663: errorCode = setODentry(d, d->transfers[line].index, d->transfers[line].subIndex, fbeaulier@663: (void *) d->transfers[line].data, &size, 1); fbeaulier@663: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: errorCode = setODentry(d, d->transfers[line].index, d->transfers[line].subIndex, fbeaulier@663: (void *) d->transfers[line].data, &size, 1); Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: if (errorCode != OD_SUCCESSFUL) fbeaulier@663: return errorCode; fbeaulier@663: MSG_WAR(0x3A08, "exit of SDOlineToObjdict ", line); fbeaulier@663: return 0; fbeaulier@663: fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@0: UNS32 objdictToSDOline (CO_Data* d, UNS8 line) etisserant@0: { JaFojtik@694: UNS32 size = SDO_MAX_LENGTH_TRANSFER; fbeaulier@663: UNS8 dataType; fbeaulier@663: UNS32 errorCode; fbeaulier@663: fbeaulier@663: MSG_WAR(0x3A05, "objdict->line index : ", d->transfers[line].index); fbeaulier@663: MSG_WAR(0x3A06, " subIndex : ", d->transfers[line].subIndex); etisserant@0: Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@666: /* Try to use the static buffer. */ fbeaulier@663: errorCode = getODentry(d, d->transfers[line].index, fbeaulier@663: d->transfers[line].subIndex, fbeaulier@663: (void *)d->transfers[line].data, fbeaulier@663: &size, &dataType, 1); fbeaulier@666: if (errorCode == SDOABT_OUT_OF_MEMORY) { fbeaulier@666: /* The static buffer is too small, try again using a dynamic buffer. * fbeaulier@666: * 'size' now contains the real size of the requested object. */ fbeaulier@666: if (size <= SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE) { fbeaulier@666: d->transfers[line].dynamicData = (UNS8 *) malloc(size * sizeof(UNS8)); fbeaulier@666: if (d->transfers[line].dynamicData != NULL) { fbeaulier@666: d->transfers[line].dynamicDataSize = size; fbeaulier@666: errorCode = getODentry(d, fbeaulier@666: d->transfers[line].index, fbeaulier@666: d->transfers[line].subIndex, fbeaulier@666: (void *) d->transfers[line].dynamicData, fbeaulier@666: &d->transfers[line].dynamicDataSize, fbeaulier@666: &dataType, fbeaulier@666: 1); fbeaulier@666: } fbeaulier@666: } fbeaulier@666: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: errorCode = getODentry(d, d->transfers[line].index, fbeaulier@663: d->transfers[line].subIndex, fbeaulier@663: (void *)d->transfers[line].data, fbeaulier@663: &size, &dataType, 1); Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION etisserant@506: fbeaulier@663: if (errorCode != OD_SUCCESSFUL) fbeaulier@663: return errorCode; fbeaulier@663: fbeaulier@663: d->transfers[line].count = size; fbeaulier@663: d->transfers[line].offset = 0; fbeaulier@663: fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** @param nbBytes fbeaulier@663: ** @param data fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@539: UNS8 lineToSDO (CO_Data* d, UNS8 line, UNS32 nbBytes, UNS8* data) { fbeaulier@663: UNS8 i; fbeaulier@663: UNS32 offset; etisserant@0: Christian@636: #ifndef SDO_DYNAMIC_BUFFER_ALLOCATION JaFojtik@694: if ((d->transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) { JaFojtik@694: MSG_ERR(0x1A10,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes); fbeaulier@663: return 0xFF; fbeaulier@663: } Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: if ((d->transfers[line].offset + nbBytes) > d->transfers[line].count) { fbeaulier@663: MSG_ERR(0x1A11,"SDO Size of data too large. Exceed count", nbBytes); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: offset = d->transfers[line].offset; Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION JaFojtik@694: if (d->transfers[line].count <= SDO_MAX_LENGTH_TRANSFER) fbeaulier@663: { fbeaulier@663: for (i = 0 ; i < nbBytes ; i++) fbeaulier@663: * (data + i) = d->transfers[line].data[offset + i]; fbeaulier@663: } fbeaulier@663: else fbeaulier@663: { fbeaulier@663: if (d->transfers[line].dynamicData == NULL) fbeaulier@663: { fbeaulier@663: MSG_ERR(0x1A11,"SDO's dynamic buffer not allocated. Line", line); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: for (i = 0 ; i < nbBytes ; i++) fbeaulier@663: * (data + i) = d->transfers[line].dynamicData[offset + i]; fbeaulier@663: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: for (i = 0 ; i < nbBytes ; i++) fbeaulier@663: * (data + i) = d->transfers[line].data[offset + i]; Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: d->transfers[line].offset = d->transfers[line].offset + nbBytes; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** @param nbBytes fbeaulier@663: ** @param data fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@539: UNS8 SDOtoLine (CO_Data* d, UNS8 line, UNS32 nbBytes, UNS8* data) etisserant@0: { fbeaulier@663: UNS8 i; fbeaulier@663: UNS32 offset; Christian@636: #ifndef SDO_DYNAMIC_BUFFER_ALLOCATION JaFojtik@694: if ((d->transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) { JaFojtik@694: MSG_ERR(0x1A15,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes); fbeaulier@663: return 0xFF; fbeaulier@663: } Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: offset = d->transfers[line].offset; Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: { fbeaulier@663: UNS8* lineData = d->transfers[line].data; JaFojtik@694: if ((d->transfers[line].offset + nbBytes) > SDO_MAX_LENGTH_TRANSFER) { fbeaulier@663: if (d->transfers[line].dynamicData == NULL) { fbeaulier@663: d->transfers[line].dynamicData = (UNS8*) malloc(SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE); fbeaulier@663: d->transfers[line].dynamicDataSize = SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE; fbeaulier@663: fbeaulier@663: if (d->transfers[line].dynamicData == NULL) { fbeaulier@663: MSG_ERR(0x1A15,"SDO allocating dynamic buffer failed, size", SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: //Copy present data fbeaulier@663: memcpy(d->transfers[line].dynamicData, d->transfers[line].data, offset); fbeaulier@663: } fbeaulier@663: else if ((d->transfers[line].offset + nbBytes) > d->transfers[line].dynamicDataSize) fbeaulier@663: { fbeaulier@663: UNS8* newDynamicBuffer = (UNS8*) realloc(d->transfers[line].dynamicData, d->transfers[line].dynamicDataSize + SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE); fbeaulier@663: if (newDynamicBuffer == NULL) { fbeaulier@663: MSG_ERR(0x1A15,"SDO reallocating dynamic buffer failed, size", d->transfers[line].dynamicDataSize + SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: d->transfers[line].dynamicData = newDynamicBuffer; fbeaulier@663: d->transfers[line].dynamicDataSize += SDO_DYNAMIC_BUFFER_ALLOCATION_SIZE; fbeaulier@663: } fbeaulier@663: lineData = d->transfers[line].dynamicData; fbeaulier@663: } fbeaulier@663: fbeaulier@663: for (i = 0 ; i < nbBytes ; i++) fbeaulier@663: lineData[offset + i] = * (data + i); fbeaulier@663: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: for (i = 0 ; i < nbBytes ; i++) fbeaulier@663: d->transfers[line].data[offset + i] = * (data + i); Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: d->transfers[line].offset = d->transfers[line].offset + nbBytes; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param whoami fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param abortCode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 failedSDO (CO_Data* d, UNS8 CliServNbr, UNS8 whoami, UNS16 index, etisserant@0: UNS8 subIndex, UNS32 abortCode) etisserant@0: { fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 line; fbeaulier@663: err = getSDOlineOnUse( d, CliServNbr, whoami, &line ); fbeaulier@663: if (!err) /* If a line on use have been found.*/ fbeaulier@663: MSG_WAR(0x3A20, "FailedSDO : line found : ", line); fbeaulier@663: if ((! err) && (whoami == SDO_SERVER)) { fbeaulier@663: resetSDOline( d, line ); fbeaulier@663: MSG_WAR(0x3A21, "FailedSDO : line released : ", line); fbeaulier@663: } fbeaulier@663: if ((! err) && (whoami == SDO_CLIENT)) { fbeaulier@663: StopSDO_TIMER(line); fbeaulier@663: d->transfers[line].state = SDO_ABORTED_INTERNAL; fbeaulier@664: d->transfers[line].abortCode = abortCode; fbeaulier@663: } fbeaulier@663: MSG_WAR(0x3A22, "Sending SDO abort ", 0); fbeaulier@663: err = sendSDOabort(d, whoami, CliServNbr, index, subIndex, abortCode); fbeaulier@663: if (err) { fbeaulier@663: MSG_WAR(0x3A23, "Unable to send the SDO abort", 0); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: **/ etisserant@0: void resetSDOline ( CO_Data* d, UNS8 line ) etisserant@0: { fbeaulier@663: UNS32 i; fbeaulier@663: MSG_WAR(0x3A25, "reset SDO line nb : ", line); fbeaulier@663: initSDOline(d, line, 0, 0, 0, SDO_RESET); JaFojtik@694: for (i = 0 ; i < SDO_MAX_LENGTH_TRANSFER ; i++) fbeaulier@663: d->transfers[line].data[i] = 0; fbeaulier@663: d->transfers[line].whoami = 0; fbeaulier@663: d->transfers[line].abortCode = 0; etisserant@0: } etisserant@0: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param state fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 initSDOline (CO_Data* d, UNS8 line, UNS8 CliServNbr, UNS16 index, UNS8 subIndex, UNS8 state) fbeaulier@663: { fbeaulier@663: MSG_WAR(0x3A25, "init SDO line nb : ", line); fbeaulier@664: if (state == SDO_DOWNLOAD_IN_PROGRESS || state == SDO_UPLOAD_IN_PROGRESS || fbeaulier@664: state == SDO_BLOCK_DOWNLOAD_IN_PROGRESS || state == SDO_BLOCK_UPLOAD_IN_PROGRESS){ fbeaulier@663: StartSDO_TIMER(line) fbeaulier@663: }else{ fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: } fbeaulier@663: d->transfers[line].CliServNbr = CliServNbr; fbeaulier@663: d->transfers[line].index = index; fbeaulier@663: d->transfers[line].subIndex = subIndex; fbeaulier@663: d->transfers[line].state = state; fbeaulier@663: d->transfers[line].toggle = 0; fbeaulier@663: d->transfers[line].count = 0; fbeaulier@663: d->transfers[line].offset = 0; fbeaulier@664: d->transfers[line].peerCRCsupport = 0; fbeaulier@664: d->transfers[line].blksize = 0; fbeaulier@664: d->transfers[line].ackseq = 0; fbeaulier@664: d->transfers[line].objsize = 0; fbeaulier@664: d->transfers[line].lastblockoffset = 0; fbeaulier@664: d->transfers[line].seqno = 0; fbeaulier@664: d->transfers[line].endfield = 0; fbeaulier@664: d->transfers[line].rxstep = RXSTEP_INIT; fbeaulier@663: d->transfers[line].dataType = 0; fbeaulier@663: d->transfers[line].Callback = NULL; Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: free(d->transfers[line].dynamicData); fbeaulier@663: d->transfers[line].dynamicData = 0; fbeaulier@663: d->transfers[line].dynamicDataSize = 0; Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param whoami fbeaulier@663: ** @param line fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@0: UNS8 getSDOfreeLine ( CO_Data* d, UNS8 whoami, UNS8 *line ) etisserant@0: { etisserant@506: fbeaulier@663: UNS8 i; fbeaulier@663: JaFojtik@694: for (i = 0 ; i < SDO_MAX_SIMULTANEOUS_TRANSFERS ; i++){ fbeaulier@663: if ( d->transfers[i].state == SDO_RESET ) { fbeaulier@663: *line = i; fbeaulier@663: d->transfers[i].whoami = whoami; fbeaulier@663: return 0; fbeaulier@663: } /* end if */ fbeaulier@663: } /* end for */ fbeaulier@663: MSG_ERR(0x1A25, "Too many SDO in progress. Aborted.", i); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param whoami fbeaulier@663: ** @param line fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 getSDOlineOnUse (CO_Data* d, UNS8 CliServNbr, UNS8 whoami, UNS8 *line) fbeaulier@663: { fbeaulier@663: fbeaulier@663: UNS8 i; fbeaulier@663: JaFojtik@694: for (i = 0 ; i < SDO_MAX_SIMULTANEOUS_TRANSFERS ; i++){ fbeaulier@663: if ( (d->transfers[i].state != SDO_RESET) && fbeaulier@663: (d->transfers[i].state != SDO_ABORTED_INTERNAL) && fbeaulier@663: (d->transfers[i].CliServNbr == CliServNbr) && fbeaulier@663: (d->transfers[i].whoami == whoami) ) { fbeaulier@663: if (line) *line = i; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param whoami fbeaulier@663: ** @param line fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 getSDOlineToClose (CO_Data* d, UNS8 CliServNbr, UNS8 whoami, UNS8 *line) fbeaulier@663: { fbeaulier@663: fbeaulier@663: UNS8 i; fbeaulier@663: JaFojtik@694: for (i = 0 ; i < SDO_MAX_SIMULTANEOUS_TRANSFERS ; i++){ fbeaulier@663: if ( (d->transfers[i].state != SDO_RESET) && fbeaulier@663: (d->transfers[i].CliServNbr == CliServNbr) && fbeaulier@663: (d->transfers[i].whoami == whoami) ) { fbeaulier@663: if (line) *line = i; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param whoami fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ Mongo@669: UNS8 closeSDOtransfer (CO_Data* d, UNS8 nodeId, UNS8 whoami) fbeaulier@663: { fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 line; Mongo@669: UNS8 CliNbr; Mongo@669: /* First let's find the corresponding SDO client in our OD */ Mongo@669: CliNbr = GetSDOClientFromNodeId(d, nodeId); Mongo@669: if(CliNbr >= 0xFE) Mongo@669: return SDO_ABORTED_INTERNAL; Mongo@669: err = getSDOlineToClose(d, CliNbr, whoami, &line); fbeaulier@663: if (err) { fbeaulier@663: MSG_WAR(0x2A30, "No SDO communication to close", 0); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: resetSDOline(d, line); fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** @param nbBytes fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@539: UNS8 getSDOlineRestBytes (CO_Data* d, UNS8 line, UNS32 * nbBytes) etisserant@0: { fbeaulier@663: /* SDO initiated with e=0 and s=0 have count set to null */ fbeaulier@663: if (d->transfers[line].count == 0) fbeaulier@663: * nbBytes = 0; fbeaulier@663: else fbeaulier@663: * nbBytes = d->transfers[line].count - d->transfers[line].offset; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param line fbeaulier@663: ** @param nbBytes fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@539: UNS8 setSDOlineRestBytes (CO_Data* d, UNS8 line, UNS32 nbBytes) etisserant@0: { Christian@636: #ifndef SDO_DYNAMIC_BUFFER_ALLOCATION JaFojtik@694: if (nbBytes > SDO_MAX_LENGTH_TRANSFER) { JaFojtik@694: MSG_ERR(0x1A35,"SDO Size of data too large. Exceed SDO_MAX_LENGTH_TRANSFER", nbBytes); fbeaulier@663: return 0xFF; fbeaulier@663: } Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: d->transfers[line].count = nbBytes; fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param whoami fbeaulier@663: ** @param CliServNbr fbeaulier@663: ** @param pData fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 sendSDO (CO_Data* d, UNS8 whoami, UNS8 CliServNbr, UNS8 *pData) fbeaulier@663: { fbeaulier@663: UNS16 offset; fbeaulier@663: UNS8 i; fbeaulier@663: Message m; fbeaulier@663: fbeaulier@663: MSG_WAR(0x3A38, "sendSDO",0); fbeaulier@663: if( !((d->nodeState == Operational) || (d->nodeState == Pre_operational ))) { fbeaulier@663: MSG_WAR(0x2A39, "unable to send the SDO (not in op or pre-op mode", d->nodeState); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*get the server->client cobid*/ fbeaulier@663: if ( whoami == SDO_SERVER ) { fbeaulier@663: offset = d->firstIndex->SDO_SVR; fbeaulier@663: if ((offset == 0) || ((offset+CliServNbr) > d->lastIndex->SDO_SVR)) { fbeaulier@663: MSG_ERR(0x1A42, "SendSDO : SDO server not found", 0); fbeaulier@663: return 0xFF; fbeaulier@663: } hacking@700: m.cob_id = (UNS16) *((UNS32*) d->objdict[offset+CliServNbr].pSubindex[2].pObject); fbeaulier@664: MSG_WAR(0x3A41, "I am server Tx cobId : ", m.cob_id); fbeaulier@663: } fbeaulier@663: else { /*case client*/ fbeaulier@663: /* Get the client->server cobid.*/ fbeaulier@663: offset = d->firstIndex->SDO_CLT; fbeaulier@663: if ((offset == 0) || ((offset+CliServNbr) > d->lastIndex->SDO_CLT)) { fbeaulier@663: MSG_ERR(0x1A42, "SendSDO : SDO client not found", 0); fbeaulier@663: return 0xFF; fbeaulier@663: } hacking@700: m.cob_id = (UNS16) *((UNS32*) d->objdict[offset+CliServNbr].pSubindex[1].pObject); fbeaulier@664: MSG_WAR(0x3A41, "I am client Tx cobId : ", m.cob_id); fbeaulier@663: } fbeaulier@663: /* message copy for sending */ fbeaulier@663: m.rtr = NOT_A_REQUEST; fbeaulier@663: /* the length of SDO must be 8 */ fbeaulier@663: m.len = 8; fbeaulier@663: for (i = 0 ; i < 8 ; i++) { fbeaulier@663: m.data[i] = pData[i]; fbeaulier@663: } fbeaulier@663: return canSend(d->canHandle,&m); fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param whoami fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param abortCode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: UNS8 sendSDOabort (CO_Data* d, UNS8 whoami, UNS8 CliServNbr, UNS16 index, UNS8 subIndex, UNS32 abortCode) fbeaulier@663: { fbeaulier@663: UNS8 data[8]; fbeaulier@663: UNS8 ret; fbeaulier@663: fbeaulier@663: MSG_WAR(0x2A50,"Sending SDO abort ", abortCode); fbeaulier@663: data[0] = 0x80; fbeaulier@663: /* Index */ fbeaulier@663: data[1] = index & 0xFF; /* LSB */ fbeaulier@663: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@663: /* Subindex */ fbeaulier@663: data[3] = subIndex; fbeaulier@663: /* Data */ fbeaulier@663: data[4] = (UNS8)(abortCode & 0xFF); fbeaulier@663: data[5] = (UNS8)((abortCode >> 8) & 0xFF); fbeaulier@663: data[6] = (UNS8)((abortCode >> 16) & 0xFF); fbeaulier@663: data[7] = (UNS8)((abortCode >> 24) & 0xFF); fbeaulier@663: ret = sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: fbeaulier@663: return ret; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param m fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@0: UNS8 proceedSDO (CO_Data* d, Message *m) etisserant@0: { fbeaulier@663: UNS8 err; fbeaulier@664: UNS8 cs; fbeaulier@663: UNS8 line; fbeaulier@663: UNS32 nbBytes; /* received or to be transmited. */ fbeaulier@663: UNS8 nodeId = 0; /* The node Id of the server if client otherwise unused */ fbeaulier@663: UNS8 CliServNbr; fbeaulier@663: UNS8 whoami = SDO_UNKNOWN; /* SDO_SERVER or SDO_CLIENT.*/ fbeaulier@663: UNS32 errorCode; /* while reading or writing in the local object dictionary.*/ fbeaulier@663: UNS8 data[8]; /* data for SDO to transmit */ fbeaulier@663: UNS16 index; fbeaulier@663: UNS8 subIndex; fbeaulier@663: UNS32 abortCode; fbeaulier@663: UNS32 i; fbeaulier@663: UNS8 j; fbeaulier@663: UNS32 *pCobId = NULL; fbeaulier@663: UNS16 offset; fbeaulier@663: UNS16 lastIndex; JaFojtik@694: UNS8 SubCommand; /* Block transfer only */ JaFojtik@694: UNS8 SeqNo; /* Sequence number in block transfer */ fbeaulier@664: UNS8 AckSeq; /* Sequence number of last segment that was received successfully */ JaFojtik@694: UNS8 NbBytesNoData; /* Number of bytes that do not contain data in last segment of block transfer */ fbeaulier@663: fbeaulier@663: MSG_WAR(0x3A60, "proceedSDO ", 0); fbeaulier@663: whoami = SDO_UNKNOWN; fbeaulier@663: /* Looking for the cobId in the object dictionary. */ fbeaulier@663: /* Am-I a server ? */ fbeaulier@663: offset = d->firstIndex->SDO_SVR; fbeaulier@663: lastIndex = d->lastIndex->SDO_SVR; fbeaulier@663: j = 0; fbeaulier@663: if(offset) while (offset <= lastIndex) { fbeaulier@663: if (d->objdict[offset].bSubCount <= 1) { fbeaulier@663: MSG_ERR(0x1A61, "Subindex 1 not found at index ", 0x1200 + j); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Looking for the cobid received. */ fbeaulier@663: pCobId = (UNS32*) d->objdict[offset].pSubindex[1].pObject; fbeaulier@663: if ( *pCobId == UNS16_LE(m->cob_id) ) { fbeaulier@663: whoami = SDO_SERVER; fbeaulier@663: MSG_WAR(0x3A62, "proceedSDO. I am server. index : ", 0x1200 + j); fbeaulier@663: /* Defining Server number = index minus 0x1200 where the cobid received is defined. */ fbeaulier@663: CliServNbr = j; fbeaulier@663: break; fbeaulier@663: } fbeaulier@663: j++; fbeaulier@663: offset++; fbeaulier@663: } /* end while */ fbeaulier@663: if (whoami == SDO_UNKNOWN) { fbeaulier@663: /* Am-I client ? */ fbeaulier@663: offset = d->firstIndex->SDO_CLT; fbeaulier@663: lastIndex = d->lastIndex->SDO_CLT; fbeaulier@663: j = 0; fbeaulier@663: if(offset) while (offset <= lastIndex) { fbeaulier@663: if (d->objdict[offset].bSubCount <= 3) { fbeaulier@663: MSG_ERR(0x1A63, "Subindex 3 not found at index ", 0x1280 + j); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Looking for the cobid received. */ fbeaulier@663: pCobId = (UNS32*) d->objdict[offset].pSubindex[2].pObject; fbeaulier@663: if (*pCobId == UNS16_LE(m->cob_id) ) { fbeaulier@663: whoami = SDO_CLIENT; fbeaulier@664: MSG_WAR(0x3A64, "proceedSDO. I am client index : ", 0x1280 + j); fbeaulier@663: /* Defining Client number = index minus 0x1280 where the cobid received is defined. */ fbeaulier@663: CliServNbr = j; fbeaulier@663: /* Reading the server node ID, if client it is mandatory in the OD */ fbeaulier@663: nodeId = *((UNS8*) d->objdict[offset].pSubindex[3].pObject); fbeaulier@663: break; fbeaulier@663: } fbeaulier@663: j++; fbeaulier@663: offset++; fbeaulier@663: } /* end while */ fbeaulier@663: } fbeaulier@663: if (whoami == SDO_UNKNOWN) { fbeaulier@663: return 0xFF;/* This SDO was not for us ! */ fbeaulier@663: } fbeaulier@663: fbeaulier@663: /* Test if the size of the SDO is ok */ fbeaulier@663: if ( (*m).len != 8) { fbeaulier@663: MSG_ERR(0x1A67, "Error size SDO", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: if (whoami == SDO_CLIENT) { fbeaulier@663: MSG_WAR(0x3A68, "I am CLIENT number ", CliServNbr); fbeaulier@663: } fbeaulier@663: else { fbeaulier@663: MSG_WAR(0x3A69, "I am SERVER number ", CliServNbr); fbeaulier@663: } fbeaulier@663: JaFojtik@694: /* Look for an SDO transfer already initiated. */ fbeaulier@664: err = getSDOlineOnUse( d, CliServNbr, whoami, &line ); fbeaulier@664: fbeaulier@664: /* Let's find cs value, first it is set as "not valid" */ fbeaulier@664: cs = 0xFF; JaFojtik@694: /* Special cases for block transfer : in frames with segment data cs is not spécified */ fbeaulier@664: if (!err) { fbeaulier@664: if ((whoami == SDO_SERVER) && (d->transfers[line].state == SDO_BLOCK_DOWNLOAD_IN_PROGRESS) || fbeaulier@664: (whoami == SDO_CLIENT) && (d->transfers[line].state == SDO_BLOCK_UPLOAD_IN_PROGRESS)) { fbeaulier@664: if(m->data[0] == 0x80) /* If first byte is 0x80 it is an abort frame (seqno = 0 not allowed) */ fbeaulier@664: cs = 4; fbeaulier@664: else fbeaulier@664: cs = 6; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: /* Other cases : cs is specified */ fbeaulier@664: if (cs == 0xFF) fbeaulier@664: cs = getSDOcs(m->data[0]); fbeaulier@664: fbeaulier@663: /* Testing the command specifier */ fbeaulier@664: /* Allowed : cs = 0, 1, 2, 3, 4, 5, 6 */ fbeaulier@663: /* cs = other : Not allowed -> abort. */ fbeaulier@664: switch (cs) { fbeaulier@663: fbeaulier@663: case 0: fbeaulier@663: /* I am SERVER */ fbeaulier@663: if (whoami == SDO_SERVER) { JaFojtik@694: /* Receiving a download segment data : an SDO transfer should have been yet initiated. */ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A70, "SDO error : Received download segment for unstarted trans. index 0x1200 + ", fbeaulier@663: CliServNbr); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the wathdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: MSG_WAR(0x3A71, "Received SDO download segment defined at index 0x1200 + ", CliServNbr); fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: /* Toggle test. */ fbeaulier@663: if (d->transfers[line].toggle != getSDOt(m->data[0])) { fbeaulier@663: MSG_ERR(0x1A72, "SDO error : Toggle error : ", getSDOt(m->data[0])); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Nb of data to be downloaded */ fbeaulier@663: nbBytes = 7 - getSDOn3(m->data[0]); JaFojtik@694: /* Store the data in the transfer structure. */ fbeaulier@663: err = SDOtoLine(d, line, nbBytes, (*m).data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Sending the SDO response, CS = 1 */ fbeaulier@663: data[0] = (1 << 5) | (d->transfers[line].toggle << 4); fbeaulier@663: for (i = 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: MSG_WAR(0x3A73, "SDO. Send response to download request defined at index 0x1200 + ", CliServNbr); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: /* Inverting the toggle for the next segment. */ fbeaulier@663: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; fbeaulier@663: /* If it was the last segment, */ fbeaulier@663: if (getSDOc(m->data[0])) { fbeaulier@663: /* Transfering line data to object dictionary. */ fbeaulier@663: /* The code does not use the "d" of initiate frame. So it is safe if e=s=0 */ fbeaulier@663: errorCode = SDOlineToObjdict(d, line); fbeaulier@663: if (errorCode) { fbeaulier@663: MSG_ERR(0x1A54, "SDO error : Unable to copy the data in the object dictionary", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, errorCode); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Release of the line */ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: MSG_WAR(0x3A74, "SDO. End of download defined at index 0x1200 + ", CliServNbr); fbeaulier@663: } fbeaulier@663: } /* end if SERVER */ fbeaulier@663: else { /* if CLIENT */ fbeaulier@663: /* I am CLIENT */ fbeaulier@663: /* It is a request for a previous upload segment. We should find a line opened for this.*/ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A75, "SDO error : Received segment response for unknown trans. from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the wathdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: /* test of the toggle; */ fbeaulier@663: if (d->transfers[line].toggle != getSDOt(m->data[0])) { fbeaulier@663: MSG_ERR(0x1A76, "SDO error : Received segment response Toggle error. from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* nb of data to be uploaded */ fbeaulier@663: nbBytes = 7 - getSDOn3(m->data[0]); fbeaulier@663: /* Storing the data in the line structure. */ fbeaulier@663: err = SDOtoLine(d, line, nbBytes, (*m).data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Inverting the toggle for the next segment. */ fbeaulier@663: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; fbeaulier@663: /* If it was the last segment,*/ fbeaulier@663: if ( getSDOc(m->data[0])) { fbeaulier@663: /* Put in state finished */ fbeaulier@663: /* The code is safe for the case e=s=0 in initiate frame. */ fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: d->transfers[line].state = SDO_FINISHED; fbeaulier@663: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@663: fbeaulier@663: MSG_WAR(0x3A77, "SDO. End of upload from node : ", nodeId); fbeaulier@663: } fbeaulier@663: else { /* more segments to receive */ fbeaulier@663: /* Sending the request for the next segment. */ fbeaulier@663: data[0] = (3 << 5) | (d->transfers[line].toggle << 4); fbeaulier@663: for (i = 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: MSG_WAR(0x3A78, "SDO send upload segment request to nodeId", nodeId); fbeaulier@663: } fbeaulier@663: } /* End if CLIENT */ fbeaulier@663: break; fbeaulier@663: fbeaulier@663: case 1: fbeaulier@663: /* I am SERVER */ fbeaulier@663: /* Receive of an initiate download */ fbeaulier@663: if (whoami == SDO_SERVER) { fbeaulier@663: index = getSDOindex(m->data[1],m->data[2]); fbeaulier@663: subIndex = getSDOsubIndex(m->data[3]); fbeaulier@663: MSG_WAR(0x3A79, "Received SDO Initiate Download (to store data) defined at index 0x1200 + ", fbeaulier@663: CliServNbr); fbeaulier@663: MSG_WAR(0x3A80, "Writing at index : ", index); fbeaulier@663: MSG_WAR(0x3A80, "Writing at subIndex : ", subIndex); fbeaulier@663: JaFojtik@694: /* Search if a SDO transfer have been yet initiated */ fbeaulier@663: if (! err) { fbeaulier@663: MSG_ERR(0x1A81, "SDO error : Transmission yet started.", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* No line on use. Great ! */ fbeaulier@663: /* Try to open a new line. */ fbeaulier@663: err = getSDOfreeLine( d, whoami, &line ); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A82, "SDO error : No line free, too many SDO in progress. Aborted.", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: initSDOline(d, line, CliServNbr, index, subIndex, SDO_DOWNLOAD_IN_PROGRESS); fbeaulier@663: fbeaulier@663: if (getSDOe(m->data[0])) { /* If SDO expedited */ fbeaulier@663: /* nb of data to be downloaded */ fbeaulier@663: nbBytes = 4 - getSDOn2(m->data[0]); fbeaulier@663: /* Storing the data in the line structure. */ fbeaulier@663: d->transfers[line].count = nbBytes; fbeaulier@663: err = SDOtoLine(d, line, nbBytes, (*m).data + 4); fbeaulier@663: fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: JaFojtik@694: /* SDO expedited -> transfer finished. Data can be stored in the dictionary. */ fbeaulier@663: /*The line will be reseted when it is downloading in the dictionary. */ JaFojtik@694: MSG_WAR(0x3A83, "SDO Initiate Download is an expedited transfer. Finished. ", 0); fbeaulier@663: /* Transfering line data to object dictionary. */ fbeaulier@663: errorCode = SDOlineToObjdict(d, line); fbeaulier@663: if (errorCode) { fbeaulier@663: MSG_ERR(0x1A84, "SDO error : Unable to copy the data in the object dictionary", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, errorCode); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Release of the line. */ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: } JaFojtik@694: else {/* So, if it is not an expedited transfer */ fbeaulier@663: if (getSDOs(m->data[0])) { fbeaulier@663: nbBytes = (m->data[4]) + ((UNS32)(m->data[5])<<8) + ((UNS32)(m->data[6])<<16) + ((UNS32)(m->data[7])<<24); hacking@682: err = setSDOlineRestBytes(d, line, nbBytes); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: } fbeaulier@663: /*Sending a SDO, cs=3*/ fbeaulier@663: data[0] = 3 << 5; fbeaulier@663: data[1] = index & 0xFF; /* LSB */ fbeaulier@663: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@663: data[3] = subIndex; fbeaulier@663: for (i = 4 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: } /* end if I am SERVER */ fbeaulier@663: else { fbeaulier@663: /* I am CLIENT */ fbeaulier@663: /* It is a response for a previous download segment. We should find a line opened for this. */ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A85, "SDO error : Received segment response for unknown trans. from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the wathdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: /* test of the toggle; */ fbeaulier@663: if (d->transfers[line].toggle != getSDOt(m->data[0])) { fbeaulier@663: MSG_ERR(0x1A86, "SDO error : Received segment response Toggle error. from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /* End transmission or downloading next segment. We need to know if it will be the last one. */ fbeaulier@663: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@663: if (nbBytes == 0) { fbeaulier@663: MSG_WAR(0x3A87, "SDO End download. segment response received. OK. from nodeId", nodeId); fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: d->transfers[line].state = SDO_FINISHED; fbeaulier@663: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@663: return 0x00; fbeaulier@663: } fbeaulier@663: /* At least one transfer to send. */ fbeaulier@663: if (nbBytes > 7) { fbeaulier@663: /* several segments to download.*/ fbeaulier@663: /* code to send the next segment. (cs = 0; c = 0) */ fbeaulier@663: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; fbeaulier@663: data[0] = (d->transfers[line].toggle << 4); fbeaulier@663: err = lineToSDO(d, line, 7, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: else { fbeaulier@663: /* Last segment. */ fbeaulier@663: /* code to send the last segment. (cs = 0; c = 1)*/ fbeaulier@663: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; fbeaulier@663: data[0] = (UNS8)((d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1); fbeaulier@663: err = lineToSDO(d, line, nbBytes, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: for (i = nbBytes + 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: } fbeaulier@663: MSG_WAR(0x3A88, "SDO sending download segment to nodeId", nodeId); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: } /* end if I am a CLIENT */ fbeaulier@663: break; fbeaulier@663: fbeaulier@663: case 2: fbeaulier@663: /* I am SERVER */ fbeaulier@663: /* Receive of an initiate upload.*/ fbeaulier@663: if (whoami == SDO_SERVER) { fbeaulier@663: index = getSDOindex(m->data[1],m->data[2]); fbeaulier@663: subIndex = getSDOsubIndex(m->data[3]); fbeaulier@663: MSG_WAR(0x3A89, "Received SDO Initiate upload (to send data) defined at index 0x1200 + ", fbeaulier@663: CliServNbr); fbeaulier@663: MSG_WAR(0x3A90, "Reading at index : ", index); fbeaulier@663: MSG_WAR(0x3A91, "Reading at subIndex : ", subIndex); JaFojtik@694: /* Search if a SDO transfer have been yet initiated*/ fbeaulier@663: if (! err) { fbeaulier@663: MSG_ERR(0x1A92, "SDO error : Transmission yet started at line : ", line); fbeaulier@663: MSG_WAR(0x3A93, "Server Nbr = ", CliServNbr); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* No line on use. Great !*/ fbeaulier@663: /* Try to open a new line.*/ fbeaulier@663: err = getSDOfreeLine( d, whoami, &line ); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A71, "SDO error : No line free, too many SDO in progress. Aborted.", 0); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: initSDOline(d, line, CliServNbr, index, subIndex, SDO_UPLOAD_IN_PROGRESS); fbeaulier@663: /* Transfer data from dictionary to the line structure. */ fbeaulier@663: errorCode = objdictToSDOline(d, line); fbeaulier@663: fbeaulier@663: if (errorCode) { fbeaulier@663: MSG_ERR(0x1A94, "SDO error : Unable to copy the data from object dictionary. Err code : ", fbeaulier@663: errorCode); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, errorCode); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Preparing the response.*/ fbeaulier@663: getSDOlineRestBytes(d, line, &nbBytes); /* Nb bytes to transfer ? */ fbeaulier@663: if (nbBytes > 4) { JaFojtik@694: /* normal transfer. (segmented). */ fbeaulier@663: /* code to send the initiate upload response. (cs = 2) */ fbeaulier@663: data[0] = (2 << 5) | 1; fbeaulier@663: data[1] = index & 0xFF; /* LSB */ fbeaulier@663: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@663: data[3] = subIndex; hacking@700: data[4] = (UNS8) nbBytes; hacking@700: data[5] = (UNS8) (nbBytes >> 8); hacking@700: data[6] = (UNS8) (nbBytes >> 16); hacking@700: data[7] = (UNS8) (nbBytes >> 24); fbeaulier@664: MSG_WAR(0x3A95, "SDO. Sending normal upload initiate response defined at index 0x1200 + ", nodeId); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: } fbeaulier@663: else { fbeaulier@663: /* Expedited upload. (cs = 2 ; e = 1) */ fbeaulier@663: data[0] = (UNS8)((2 << 5) | ((4 - nbBytes) << 2) | 3); fbeaulier@663: data[1] = index & 0xFF; /* LSB */ fbeaulier@663: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@663: data[3] = subIndex; fbeaulier@663: err = lineToSDO(d, line, nbBytes, data + 4); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: for (i = 4 + nbBytes ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: MSG_WAR(0x3A96, "SDO. Sending expedited upload initiate response defined at index 0x1200 + ", fbeaulier@663: CliServNbr); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: /* Release the line.*/ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: } fbeaulier@663: } /* end if I am SERVER*/ fbeaulier@663: else { fbeaulier@663: /* I am CLIENT */ fbeaulier@663: /* It is the response for the previous initiate upload request.*/ fbeaulier@663: /* We should find a line opened for this. */ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1A97, "SDO error : Received response for unknown upload request from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the wathdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: fbeaulier@663: if (getSDOe(m->data[0])) { /* If SDO expedited */ fbeaulier@663: /* nb of data to be uploaded */ fbeaulier@663: nbBytes = 4 - getSDOn2(m->data[0]); fbeaulier@663: /* Storing the data in the line structure. */ fbeaulier@663: err = SDOtoLine(d, line, nbBytes, (*m).data + 4); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } JaFojtik@694: /* SDO expedited -> transfer finished. data are available via getReadResultNetworkDict(). */ fbeaulier@663: MSG_WAR(0x3A98, "SDO expedited upload finished. Response received from node : ", nodeId); fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: d->transfers[line].count = nbBytes; fbeaulier@663: d->transfers[line].state = SDO_FINISHED; fbeaulier@663: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@663: return 0; fbeaulier@663: } JaFojtik@694: else { /* So, if it is not an expedited transfer */ fbeaulier@663: /* Storing the nb of data to receive. */ fbeaulier@663: if (getSDOs(m->data[0])) { fbeaulier@663: nbBytes = m->data[4] + ((UNS32)(m->data[5])<<8) + ((UNS32)(m->data[6])<<16) + ((UNS32)(m->data[7])<<24); fbeaulier@663: err = setSDOlineRestBytes(d, line, nbBytes); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: /* Requesting next segment. (cs = 3) */ fbeaulier@663: data[0] = 3 << 5; fbeaulier@663: for (i = 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: MSG_WAR(0x3A99, "SDO. Sending upload segment request to node : ", nodeId); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: } fbeaulier@663: } /* End if CLIENT */ fbeaulier@663: break; fbeaulier@663: fbeaulier@663: case 3: fbeaulier@663: /* I am SERVER */ fbeaulier@663: if (whoami == SDO_SERVER) { fbeaulier@663: /* Receiving a upload segment. */ JaFojtik@694: /* A SDO transfer should have been yet initiated. */ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_UPLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AA0, "SDO error : Received upload segment for unstarted trans. index 0x1200 + ", fbeaulier@663: CliServNbr); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the wathdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: MSG_WAR(0x3AA1, "Received SDO upload segment defined at index 0x1200 + ", CliServNbr); fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: /* Toggle test.*/ fbeaulier@663: if (d->transfers[line].toggle != getSDOt(m->data[0])) { fbeaulier@663: MSG_ERR(0x1AA2, "SDO error : Toggle error : ", getSDOt(m->data[0])); fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_TOGGLE_NOT_ALTERNED); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Uploading next segment. We need to know if it will be the last one. */ fbeaulier@663: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@663: if (nbBytes > 7) { fbeaulier@663: /* The segment to transfer is not the last one.*/ fbeaulier@663: /* code to send the next segment. (cs = 0; c = 0) */ fbeaulier@663: data[0] = (d->transfers[line].toggle << 4); fbeaulier@663: err = lineToSDO(d, line, 7, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Inverting the toggle for the next tranfert. */ fbeaulier@663: d->transfers[line].toggle = ! d->transfers[line].toggle & 1; fbeaulier@663: MSG_WAR(0x3AA3, "SDO. Sending upload segment defined at index 0x1200 + ", CliServNbr); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: } fbeaulier@663: else { fbeaulier@663: /* Last segment. */ fbeaulier@663: /* code to send the last segment. (cs = 0; c = 1) */ fbeaulier@663: data[0] = (UNS8)((d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1); fbeaulier@663: err = lineToSDO(d, line, nbBytes, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: for (i = nbBytes + 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: MSG_WAR(0x3AA4, "SDO. Sending last upload segment defined at index 0x1200 + ", CliServNbr); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: /* Release the line */ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: } fbeaulier@663: } /* end if SERVER*/ fbeaulier@663: else { fbeaulier@663: /* I am CLIENT */ fbeaulier@663: /* It is the response for the previous initiate download request. */ fbeaulier@663: /* We should find a line opened for this. */ fbeaulier@663: if (!err) fbeaulier@663: err = d->transfers[line].state != SDO_DOWNLOAD_IN_PROGRESS; fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AA5, "SDO error : Received response for unknown download request from nodeId", nodeId); fbeaulier@663: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Reset the watchdog */ fbeaulier@663: RestartSDO_TIMER(line) fbeaulier@663: index = d->transfers[line].index; fbeaulier@663: subIndex = d->transfers[line].subIndex; fbeaulier@663: /* End transmission or requesting next segment. */ fbeaulier@663: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@663: if (nbBytes == 0) { fbeaulier@663: MSG_WAR(0x3AA6, "SDO End download expedited. Response received. from nodeId", nodeId); fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: d->transfers[line].state = SDO_FINISHED; fbeaulier@663: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@663: return 0x00; fbeaulier@663: } fbeaulier@663: if (nbBytes > 7) { fbeaulier@663: /* more than one request to send */ fbeaulier@663: /* code to send the next segment. (cs = 0; c = 0) */ fbeaulier@663: data[0] = (d->transfers[line].toggle << 4); fbeaulier@663: err = lineToSDO(d, line, 7, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: } fbeaulier@663: else { fbeaulier@663: /* Last segment.*/ fbeaulier@663: /* code to send the last segment. (cs = 0; c = 1) */ fbeaulier@663: data[0] = (UNS8)((d->transfers[line].toggle << 4) | ((7 - nbBytes) << 1) | 1); fbeaulier@663: err = lineToSDO(d, line, nbBytes, data + 1); fbeaulier@663: if (err) { fbeaulier@663: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: for (i = nbBytes + 1 ; i < 8 ; i++) fbeaulier@663: data[i] = 0; fbeaulier@663: } fbeaulier@663: MSG_WAR(0x3AA7, "SDO sending download segment to nodeId", nodeId); fbeaulier@663: sendSDO(d, whoami, CliServNbr, data); fbeaulier@663: fbeaulier@663: } /* end if I am a CLIENT */ fbeaulier@663: break; fbeaulier@663: fbeaulier@663: case 4: fbeaulier@663: abortCode = fbeaulier@663: (UNS32)m->data[4] | fbeaulier@663: ((UNS32)m->data[5] << 8) | fbeaulier@663: ((UNS32)m->data[6] << 16) | fbeaulier@663: ((UNS32)m->data[7] << 24); fbeaulier@663: /* Received SDO abort. */ fbeaulier@663: if (whoami == SDO_SERVER) { fbeaulier@663: if (!err) { fbeaulier@663: resetSDOline( d, line ); fbeaulier@663: MSG_WAR(0x3AA8, "SD0. Received SDO abort. Line released. Code : ", abortCode); fbeaulier@663: } fbeaulier@663: else fbeaulier@663: MSG_WAR(0x3AA9, "SD0. Received SDO abort. No line found. Code : ", abortCode); fbeaulier@663: /* Tips : The end user has no way to know that the server node has received an abort SDO. */ fbeaulier@663: /* Its is ok, I think.*/ fbeaulier@663: } fbeaulier@663: else { /* If I am CLIENT */ fbeaulier@663: if (!err) { fbeaulier@663: /* The line *must* be released by the core program. */ fbeaulier@663: StopSDO_TIMER(line) fbeaulier@663: d->transfers[line].state = SDO_ABORTED_RCV; fbeaulier@663: d->transfers[line].abortCode = abortCode; fbeaulier@663: MSG_WAR(0x3AB0, "SD0. Received SDO abort. Line state ABORTED. Code : ", abortCode); fbeaulier@663: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@663: } fbeaulier@663: else fbeaulier@663: MSG_WAR(0x3AB1, "SD0. Received SDO abort. No line found. Code : ", abortCode); fbeaulier@663: } fbeaulier@663: break; fbeaulier@664: case 5: /* Command specifier for data transmission - the client or server is the data producer */ fbeaulier@664: SubCommand = getSDOblockSC(m->data[0]); fbeaulier@664: if (whoami == SDO_SERVER) { /* Server block upload */ fbeaulier@664: if (SubCommand == SDO_BCS_INITIATE_UPLOAD_REQUEST) { fbeaulier@664: index = getSDOindex(m->data[1],m->data[2]); fbeaulier@664: subIndex = getSDOsubIndex(m->data[3]); fbeaulier@664: MSG_WAR(0x3AB2, "Received SDO Initiate block upload defined at index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: MSG_WAR(0x3AB3, "Reading at index : ", index); fbeaulier@664: MSG_WAR(0x3AB4, "Reading at subIndex : ", subIndex); JaFojtik@694: /* Search if a SDO transfer have been yet initiated */ fbeaulier@664: if (! err) { fbeaulier@664: MSG_ERR(0x1A93, "SDO error : Transmission yet started at line : ", line); fbeaulier@664: MSG_WAR(0x3AB5, "Server Nbr = ", CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* No line on use. Great !*/ fbeaulier@664: /* Try to open a new line.*/ fbeaulier@664: err = getSDOfreeLine( d, whoami, &line ); fbeaulier@664: if (err) { fbeaulier@664: MSG_ERR(0x1A73, "SDO error : No line free, too many SDO in progress. Aborted.", 0); fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: initSDOline(d, line, CliServNbr, index, subIndex, SDO_BLOCK_UPLOAD_IN_PROGRESS); fbeaulier@664: d->transfers[line].peerCRCsupport = ((m->data[0])>>2) & 1; fbeaulier@664: d->transfers[line].blksize = m->data[4]; fbeaulier@664: /* Transfer data from dictionary to the line structure. */ fbeaulier@664: errorCode = objdictToSDOline(d, line); fbeaulier@664: if (errorCode) { fbeaulier@664: MSG_ERR(0x1A95, "SDO error : Unable to copy the data from object dictionary. Err code : ", fbeaulier@664: errorCode); fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, errorCode); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* Preparing the response.*/ fbeaulier@664: getSDOlineRestBytes(d, line, &nbBytes); /* get Nb bytes to transfer */ fbeaulier@664: d->transfers[line].objsize = nbBytes; fbeaulier@664: data[0] = (6 << 5) | (1 << 1) | SDO_BSS_INITIATE_UPLOAD_RESPONSE; fbeaulier@664: data[1] = index & 0xFF; /* LSB */ fbeaulier@664: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@664: data[3] = subIndex; hacking@700: data[4] = (UNS8) nbBytes; hacking@700: data[5] = (UNS8) (nbBytes >> 8); hacking@700: data[6] = (UNS8) (nbBytes >> 16); hacking@700: data[7] = (UNS8) (nbBytes >> 24); fbeaulier@664: MSG_WAR(0x3A9A, "SDO. Sending normal block upload initiate response defined at index 0x1200 + ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: } fbeaulier@664: else if (SubCommand == SDO_BCS_END_UPLOAD_REQUEST) { fbeaulier@664: MSG_WAR(0x3AA2, "Received SDO block END upload request defined at index 0x1200 + ", CliServNbr); JaFojtik@694: /* A SDO transfer should have been yet initiated. */ fbeaulier@664: if (!err) fbeaulier@664: err = d->transfers[line].state != SDO_BLOCK_UPLOAD_IN_PROGRESS; fbeaulier@664: if (err) { fbeaulier@664: MSG_ERR(0x1AA1, "SDO error : Received block upload request for unstarted trans. index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* Release the line */ fbeaulier@664: resetSDOline(d, line); fbeaulier@664: } fbeaulier@664: else if ((SubCommand == SDO_BCS_UPLOAD_RESPONSE) || (SubCommand == SDO_BCS_START_UPLOAD)) { JaFojtik@694: /* A SDO transfer should have been yet initiated. */ fbeaulier@664: if (!err) fbeaulier@664: err = d->transfers[line].state != SDO_BLOCK_UPLOAD_IN_PROGRESS; fbeaulier@664: if (err) { fbeaulier@664: MSG_ERR(0x1AA1, "SDO error : Received block upload response for unstarted trans. index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* Reset the wathdog */ fbeaulier@664: RestartSDO_TIMER(line); fbeaulier@664: /* Uploading first or next block */ fbeaulier@664: index = d->transfers[line].index; fbeaulier@664: subIndex = d->transfers[line].subIndex; fbeaulier@664: if (SubCommand == SDO_BCS_UPLOAD_RESPONSE) { fbeaulier@664: MSG_WAR(0x3AA2, "Received SDO block upload response defined at index 0x1200 + ", CliServNbr); fbeaulier@664: d->transfers[line].blksize = m->data[2]; fbeaulier@664: AckSeq = (m->data[1]) & 0x7f; fbeaulier@664: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@664: if((nbBytes == 0) && (AckSeq == d->transfers[line].seqno)){ /* Si tout est envoyé et confirmé reçu on envoi un block end upload response */ fbeaulier@664: data[0] = (6 << 5) | ((d->transfers[line].endfield) << 2) | SDO_BSS_END_UPLOAD_RESPONSE; fbeaulier@664: for (i = 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AA5, "SDO. Sending block END upload response defined at index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: break; fbeaulier@664: } fbeaulier@664: else fbeaulier@664: d->transfers[line].offset = d->transfers[line].lastblockoffset + 7 * AckSeq; fbeaulier@664: if(d->transfers[line].offset > d->transfers[line].count) { /* Bad AckSeq reveived (too high) */ fbeaulier@664: MSG_ERR(0x1AA1, "SDO error : Received upload response with bad ackseq index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: else fbeaulier@664: MSG_WAR(0x3AA2, "Received SDO block START upload defined at index 0x1200 + ", CliServNbr); hacking@700: d->transfers[line].lastblockoffset = (UNS8) d->transfers[line].offset; fbeaulier@664: for(SeqNo = 1 ; SeqNo <= d->transfers[line].blksize ; SeqNo++) { fbeaulier@664: d->transfers[line].seqno = SeqNo; fbeaulier@664: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@664: if (nbBytes > 7) { fbeaulier@664: /* The segment to transfer is not the last one.*/ fbeaulier@664: data[0] = SeqNo; fbeaulier@664: err = lineToSDO(d, line, 7, data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: MSG_WAR(0x3AA5, "SDO. Sending upload segment defined at index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: /* Last segment is in this block */ fbeaulier@664: data[0] = 0x80 | SeqNo; fbeaulier@664: err = lineToSDO(d, line, nbBytes, data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: for (i = nbBytes + 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AA5, "SDO. Sending last upload segment defined at index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); hacking@700: d->transfers[line].endfield = (UNS8) (7 - nbBytes); fbeaulier@664: break; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: } fbeaulier@664: } /* end if SERVER */ fbeaulier@664: else { /* if CLIENT (block download) */ fbeaulier@664: if ((SubCommand == SDO_BSS_INITIATE_DOWNLOAD_RESPONSE) || (SubCommand == SDO_BSS_DOWNLOAD_RESPONSE)) { fbeaulier@664: /* We should find a line opened for this. */ fbeaulier@664: if (!err) fbeaulier@664: err = d->transfers[line].state != SDO_BLOCK_DOWNLOAD_IN_PROGRESS; fbeaulier@664: if (err) { fbeaulier@664: MSG_ERR(0x1AAA, "SDO error : Received response for unknown block download request from node id", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* Reset the watchdog */ fbeaulier@664: RestartSDO_TIMER(line) fbeaulier@664: if (SubCommand == SDO_BSS_INITIATE_DOWNLOAD_RESPONSE) { fbeaulier@664: index = d->transfers[line].index; fbeaulier@664: subIndex = d->transfers[line].subIndex; fbeaulier@664: d->transfers[line].peerCRCsupport = ((m->data[0])>>2) & 1; fbeaulier@664: d->transfers[line].blksize = m->data[4]; fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: d->transfers[line].blksize = m->data[2]; fbeaulier@664: AckSeq = (m->data[1]) & 0x7f; fbeaulier@664: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@664: if((nbBytes == 0) && (AckSeq == d->transfers[line].seqno)){ /* Si tout est envoyé et confirmé reçu on envoi un block end download request */ fbeaulier@664: data[0] = (6 << 5) | ((d->transfers[line].endfield) << 2) | SDO_BCS_END_DOWNLOAD_REQUEST; fbeaulier@664: for (i = 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AA5, "SDO. Sending block END download request defined at index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: break; fbeaulier@664: } fbeaulier@664: else fbeaulier@664: d->transfers[line].offset = d->transfers[line].lastblockoffset + 7 * AckSeq; fbeaulier@664: if(d->transfers[line].offset > d->transfers[line].count) { /* Bad AckSeq reveived (too high) */ fbeaulier@664: MSG_ERR(0x1AA1, "SDO error : Received upload segment with bad ackseq index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } hacking@700: d->transfers[line].lastblockoffset = (UNS8) d->transfers[line].offset; fbeaulier@664: for(SeqNo = 1 ; SeqNo <= d->transfers[line].blksize ; SeqNo++) { fbeaulier@664: d->transfers[line].seqno = SeqNo; fbeaulier@664: getSDOlineRestBytes(d, line, &nbBytes); fbeaulier@664: if (nbBytes > 7) { fbeaulier@664: /* The segment to transfer is not the last one.*/ fbeaulier@664: data[0] = SeqNo; fbeaulier@664: err = lineToSDO(d, line, 7, data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: MSG_WAR(0x3AAB, "SDO. Sending download segment to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: /* Last segment is in this block */ fbeaulier@664: data[0] = 0x80 | SeqNo; fbeaulier@664: err = lineToSDO(d, line, nbBytes, data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: for (i = nbBytes + 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AAB, "SDO. Sending last download segment to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); hacking@700: d->transfers[line].endfield = (UNS8) (7 - nbBytes); fbeaulier@664: break; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: } fbeaulier@664: else if (SubCommand == SDO_BSS_END_DOWNLOAD_RESPONSE) { fbeaulier@664: MSG_WAR(0x3AAC, "SDO End block download response from nodeId", nodeId); fbeaulier@664: StopSDO_TIMER(line) fbeaulier@664: d->transfers[line].state = SDO_FINISHED; fbeaulier@664: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@664: return 0x00; fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: MSG_ERR(0x1AAB, "SDO error block download : Received wrong subcommand from nodeId", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } /* end if CLIENT */ fbeaulier@664: break; fbeaulier@664: case 6: /* Command specifier for data reception - the client or server is the data consumer */ fbeaulier@664: if (whoami == SDO_SERVER) { /* Server block download */ fbeaulier@664: if (err) { fbeaulier@664: /* Nothing already started */ fbeaulier@664: SubCommand = (m->data[0]) & 1; fbeaulier@664: if (SubCommand != SDO_BCS_INITIATE_DOWNLOAD_REQUEST) { fbeaulier@664: MSG_ERR(0x1AAC, "SDO error block download : Received wrong subcommand from node id", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: index = getSDOindex(m->data[1],m->data[2]); fbeaulier@664: subIndex = getSDOsubIndex(m->data[3]); fbeaulier@664: MSG_WAR(0x3A9B, "Received SDO block download initiate defined at index 0x1200 + ", fbeaulier@664: CliServNbr); fbeaulier@664: MSG_WAR(0x3A9B, "Writing at index : ", index); fbeaulier@664: MSG_WAR(0x3A9B, "Writing at subIndex : ", subIndex); fbeaulier@664: /* Try to open a new line. */ fbeaulier@664: err = getSDOfreeLine( d, whoami, &line ); fbeaulier@664: if (err) { fbeaulier@664: MSG_ERR(0x1A89, "SDO error : No line free, too many SDO in progress. Aborted.", 0); fbeaulier@664: failedSDO(d, CliServNbr, whoami, index, subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: initSDOline(d, line, CliServNbr, index, subIndex, SDO_BLOCK_DOWNLOAD_IN_PROGRESS); fbeaulier@664: d->transfers[line].rxstep = RXSTEP_STARTED; fbeaulier@664: d->transfers[line].peerCRCsupport = ((m->data[0])>>2) & 1; fbeaulier@664: if ((m->data[0]) & 2) /* if data set size is indicated */ fbeaulier@664: d->transfers[line].objsize = (UNS32)m->data[4] + (UNS32)m->data[5]*256 + (UNS32)m->data[6]*256*256 + (UNS32)m->data[7]*256*256*256; fbeaulier@664: data[0] = (5 << 5) | SDO_BSS_INITIATE_DOWNLOAD_RESPONSE; hacking@700: data[1] = (UNS8) index; /* LSB */ hacking@700: data[2] = (UNS8) (index >> 8); /* MSB */ fbeaulier@664: data[3] = subIndex; fbeaulier@664: data[4] = SDO_BLOCK_SIZE; fbeaulier@664: data[5] = data[6] = data[7] = 0; fbeaulier@664: MSG_WAR(0x3AAD, "SDO. Sending block download initiate response - index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: } fbeaulier@664: else if (d->transfers[line].rxstep == RXSTEP_STARTED) { fbeaulier@664: MSG_WAR(0x3A9B, "Received SDO block download data segment - index 0x1200 + ", CliServNbr); fbeaulier@664: RestartSDO_TIMER(line) fbeaulier@664: SeqNo = m->data[0] & 0x7F; fbeaulier@664: if (m->data[0] & 0x80) { /* Last segment ? */ fbeaulier@664: if(SeqNo == (d->transfers[line].seqno + 1)) { fbeaulier@664: d->transfers[line].rxstep = RXSTEP_END; fbeaulier@664: d->transfers[line].seqno = SeqNo; fbeaulier@664: /* Store the data temporary because we don't know yet how many bytes do not contain data */ fbeaulier@664: memcpy(d->transfers[line].tmpData, m->data, 8); fbeaulier@664: } fbeaulier@664: data[0] = (5 << 5) | SDO_BSS_DOWNLOAD_RESPONSE; fbeaulier@664: data[1] = d->transfers[line].seqno; fbeaulier@664: data[2] = SDO_BLOCK_SIZE; fbeaulier@664: data[3] = data[4] = data[5] = data[6] = data[7] = 0; fbeaulier@664: MSG_WAR(0x3AAE, "SDO. Sending block download response - index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: d->transfers[line].seqno = 0; fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: if (SeqNo == (d->transfers[line].seqno + 1)) { fbeaulier@664: d->transfers[line].seqno = SeqNo; JaFojtik@694: /* Store the data in the transfer structure. */ fbeaulier@664: err = SDOtoLine(d, line, 7, (*m).data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: if (SeqNo == SDO_BLOCK_SIZE) { fbeaulier@664: data[0] = (5 << 5) | SDO_BSS_DOWNLOAD_RESPONSE; fbeaulier@664: data[1] = d->transfers[line].seqno; fbeaulier@664: data[2] = SDO_BLOCK_SIZE; fbeaulier@664: data[3] = data[4] = data[5] = data[6] = data[7] = 0; fbeaulier@664: MSG_WAR(0x3AAE, "SDO. Sending block download response - index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: d->transfers[line].seqno = 0; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: } fbeaulier@664: else if (d->transfers[line].rxstep == RXSTEP_END) { /* endphase */ fbeaulier@664: MSG_WAR(0x3A9B, "Received SDO block download end request - index 0x1200 + ", CliServNbr); fbeaulier@664: /* here store remaining bytes in tmpData to line, check size and confirm or abort */ fbeaulier@664: if ((m->data[0] & 1) != SDO_BCS_END_DOWNLOAD_REQUEST) { fbeaulier@664: MSG_ERR(0x1AAD, "SDO error block download : Received wrong subcommand - index 0x1200 + ", CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: RestartSDO_TIMER(line) fbeaulier@664: NbBytesNoData = (m->data[0]>>2) & 0x07; JaFojtik@694: /* Store the data in the transfer structure. */ fbeaulier@664: err = SDOtoLine(d, line, 7-NbBytesNoData, d->transfers[line].tmpData + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: if(d->transfers[line].objsize){ /* If size was indicated in the initiate request */ fbeaulier@664: if (d->transfers[line].objsize != d->transfers[line].offset){ fbeaulier@664: MSG_ERR(0x1AAE, "SDO error block download : sizes do not match - index 0x1200 + ", CliServNbr); fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: data[0] = (5 << 5) | SDO_BSS_END_DOWNLOAD_RESPONSE; fbeaulier@664: for (i = 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AAF, "SDO. Sending block download end response - index 0x1200 + ", CliServNbr); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: /* Transfering line data to object dictionary. */ fbeaulier@664: errorCode = SDOlineToObjdict(d, line); fbeaulier@664: if (errorCode) { fbeaulier@664: MSG_ERR(0x1AAF, "SDO error : Unable to copy the data in the object dictionary", 0); fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, errorCode); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: /* Release of the line */ fbeaulier@664: resetSDOline(d, line); fbeaulier@664: MSG_WAR(0x3AAF, "SDO. End of block download defined at index 0x1200 + ", CliServNbr); fbeaulier@664: } fbeaulier@664: } /* end if SERVER */ fbeaulier@664: else { /* if CLIENT (block upload) */ fbeaulier@664: if (err) { fbeaulier@664: /* Nothing already started */ fbeaulier@664: MSG_ERR(0x1AAD, "SDO error block upload : no transmission started", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: RestartSDO_TIMER(line) fbeaulier@664: if (d->transfers[line].rxstep == RXSTEP_INIT) { fbeaulier@664: if ((m->data[0] & 1) == SDO_BSS_INITIATE_UPLOAD_RESPONSE) { fbeaulier@664: MSG_WAR(0x3A9C, "Received SDO block upload response from node id ", nodeId); fbeaulier@664: d->transfers[line].rxstep = RXSTEP_STARTED; fbeaulier@664: d->transfers[line].peerCRCsupport = ((m->data[0])>>2) & 1; fbeaulier@664: if ((m->data[0]) & 2) /* if data set size is indicated */ fbeaulier@664: d->transfers[line].objsize = (UNS32)m->data[4] + (UNS32)m->data[5]*256 + (UNS32)m->data[6]*256*256 + (UNS32)m->data[7]*256*256*256; fbeaulier@664: data[0] = (5 << 5) | SDO_BCS_START_UPLOAD; fbeaulier@664: for (i = 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AB6, "SDO. Sending block upload start to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: } fbeaulier@664: } fbeaulier@664: else if (d->transfers[line].rxstep == RXSTEP_STARTED) { fbeaulier@664: SeqNo = m->data[0] & 0x7F; fbeaulier@664: if (m->data[0] & 0x80) { /* Last segment ? */ fbeaulier@664: if(SeqNo == (d->transfers[line].seqno + 1)) { fbeaulier@664: d->transfers[line].rxstep = RXSTEP_END; fbeaulier@664: d->transfers[line].seqno = SeqNo; fbeaulier@664: /* Store the data temporary because we don't know yet how many bytes do not contain data */ fbeaulier@664: memcpy(d->transfers[line].tmpData, m->data, 8); fbeaulier@664: } fbeaulier@664: data[0] = (5 << 5) | SDO_BCS_UPLOAD_RESPONSE; fbeaulier@664: data[1] = d->transfers[line].seqno; fbeaulier@664: data[2] = SDO_BLOCK_SIZE; fbeaulier@664: data[3] = data[4] = data[5] = data[6] = data[7] = 0; fbeaulier@664: MSG_WAR(0x3AB7, "SDO. Sending block upload response to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: d->transfers[line].seqno = 0; fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: if (SeqNo == (d->transfers[line].seqno + 1)) { fbeaulier@664: d->transfers[line].seqno = SeqNo; JaFojtik@694: /* Store the data in the transfer structure. */ fbeaulier@664: err = SDOtoLine(d, line, 7, (*m).data + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: if (SeqNo == SDO_BLOCK_SIZE) { fbeaulier@664: data[0] = (5 << 5) | SDO_BCS_UPLOAD_RESPONSE; fbeaulier@664: data[1] = d->transfers[line].seqno; fbeaulier@664: data[2] = SDO_BLOCK_SIZE; fbeaulier@664: data[3] = data[4] = data[5] = data[6] = data[7] = 0; fbeaulier@664: MSG_WAR(0x3AAE, "SDO. Sending block upload response to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: d->transfers[line].seqno = 0; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: } fbeaulier@664: else if (d->transfers[line].rxstep == RXSTEP_END) { /* endphase */ fbeaulier@664: /* here store remaining bytes in tmpData to line, check size and confirm or abort */ fbeaulier@664: if ((m->data[0] & 1) != SDO_BSS_END_UPLOAD_RESPONSE) { fbeaulier@664: MSG_ERR(0x1AAD, "SDO error block upload : Received wrong subcommand from node id ", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, 0, 0, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: NbBytesNoData = (m->data[0]>>2) & 0x07; JaFojtik@694: /* Store the data in the transfer structure. */ fbeaulier@664: err = SDOtoLine(d, line, 7-NbBytesNoData, d->transfers[line].tmpData + 1); fbeaulier@664: if (err) { fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_GENERAL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: if(d->transfers[line].objsize){ /* If size was indicated in the initiate request */ fbeaulier@664: if (d->transfers[line].objsize != d->transfers[line].offset){ fbeaulier@664: MSG_ERR(0x1AAE, "SDO error block download : sizes do not match - from node id ", nodeId); fbeaulier@664: failedSDO(d, CliServNbr, whoami, d->transfers[line].index, d->transfers[line].subIndex, SDOABT_LOCAL_CTRL_ERROR); fbeaulier@664: return 0xFF; fbeaulier@664: } fbeaulier@664: } fbeaulier@664: data[0] = (5 << 5) | SDO_BCS_END_UPLOAD_REQUEST; fbeaulier@664: for (i = 1 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: MSG_WAR(0x3AAF, "SDO. Sending block upload end request to node id ", nodeId); fbeaulier@664: sendSDO(d, whoami, CliServNbr, data); fbeaulier@664: MSG_WAR(0x3AAF, "SDO. End of block upload request", 0); fbeaulier@664: StopSDO_TIMER(line) fbeaulier@664: d->transfers[line].state = SDO_FINISHED; fbeaulier@664: if(d->transfers[line].Callback) (*d->transfers[line].Callback)(d,nodeId); fbeaulier@664: } fbeaulier@664: } /* end if CLIENT */ fbeaulier@664: break; fbeaulier@663: default: fbeaulier@663: /* Error : Unknown cs */ fbeaulier@664: MSG_ERR(0x1AB2, "SDO. Received unknown command specifier : ", cs); fbeaulier@663: return 0xFF; fbeaulier@663: fbeaulier@663: } /* End switch */ fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: ** 0xFF : No SDO client available fbeaulier@663: ** 0xFE : Not found fbeaulier@663: ** otherwise : SDO client number fbeaulier@663: **/ fbeaulier@663: UNS8 GetSDOClientFromNodeId( CO_Data* d, UNS8 nodeId ) fbeaulier@663: { fbeaulier@663: UNS8 SDOfound = 0; fbeaulier@663: UNS8 CliNbr; fbeaulier@663: UNS16 lastIndex; fbeaulier@663: UNS16 offset; fbeaulier@663: UNS8 nodeIdServer; fbeaulier@663: fbeaulier@663: offset = d->firstIndex->SDO_CLT; fbeaulier@663: lastIndex = d->lastIndex->SDO_CLT; fbeaulier@663: if (offset == 0) { fbeaulier@663: MSG_ERR(0x1AC6, "No SDO client index found for nodeId ", nodeId); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: CliNbr = 0; fbeaulier@663: while (offset <= lastIndex) { fbeaulier@663: if (d->objdict[offset].bSubCount <= 3) { fbeaulier@663: MSG_ERR(0x1AC8, "Subindex 3 not found at index ", 0x1280 + CliNbr); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* looking for the server nodeId */ fbeaulier@663: nodeIdServer = *((UNS8*) d->objdict[offset].pSubindex[3].pObject); fbeaulier@663: MSG_WAR(0x1AD2, "index : ", 0x1280 + CliNbr); fbeaulier@663: MSG_WAR(0x1AD3, "nodeIdServer : ", nodeIdServer); fbeaulier@663: fbeaulier@663: if(nodeIdServer == nodeId) { fbeaulier@663: SDOfound = 1; fbeaulier@663: break; fbeaulier@663: } fbeaulier@663: offset++; fbeaulier@663: CliNbr++; fbeaulier@663: } /* end while */ fbeaulier@663: if (!SDOfound) { Mongo@711: MSG_WAR(0x1AC9, "SDO No preset client found to communicate with node : ", nodeId); fbeaulier@663: return 0xFE; fbeaulier@663: } fbeaulier@663: MSG_WAR(0x3AD0," SDO client defined at index : ", 0x1280 + CliNbr); fbeaulier@663: fbeaulier@663: return CliNbr; fbeaulier@663: } fbeaulier@663: fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param count fbeaulier@663: ** @param dataType fbeaulier@663: ** @param data fbeaulier@663: ** @param Callback fbeaulier@663: ** @param endianize fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@663: INLINE UNS8 _writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, fbeaulier@664: UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 endianize, UNS8 useBlockMode) fbeaulier@663: { fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 line; fbeaulier@663: UNS8 CliNbr; fbeaulier@663: UNS32 j; fbeaulier@663: UNS8 i; fbeaulier@663: UNS8 buf[8]; fbeaulier@663: fbeaulier@663: MSG_WAR(0x3AC0, "Send SDO to write in the dictionary of node : ", nodeId); fbeaulier@663: MSG_WAR(0x3AC1, " At index : ", index); fbeaulier@663: MSG_WAR(0x3AC2, " subIndex : ", subIndex); fbeaulier@663: MSG_WAR(0x3AC3, " nb bytes : ", count); fbeaulier@663: fbeaulier@663: /* First let's find the corresponding SDO client in our OD */ fbeaulier@663: CliNbr = GetSDOClientFromNodeId( d, nodeId); fbeaulier@663: if(CliNbr >= 0xFE) fbeaulier@663: return CliNbr; fbeaulier@663: /* Verify that there is no SDO communication yet. */ fbeaulier@663: err = getSDOlineOnUse(d, CliNbr, SDO_CLIENT, &line); fbeaulier@663: if (!err) { fbeaulier@663: MSG_ERR(0x1AC4, "SDO error : Communication yet established. with node : ", nodeId); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Taking the line ... */ fbeaulier@663: err = getSDOfreeLine( d, SDO_CLIENT, &line ); etisserant@0: if (err) { fbeaulier@663: MSG_ERR(0x1AC5, "SDO error : No line free, too many SDO in progress. Aborted for node : ", nodeId); fbeaulier@663: return (0xFF); fbeaulier@663: } Mongo@669: else Mongo@669: MSG_WAR(0x3AE1, "Transmission on line : ", line); fbeaulier@664: if(useBlockMode) { fbeaulier@664: initSDOline(d, line, CliNbr, index, subIndex, SDO_BLOCK_DOWNLOAD_IN_PROGRESS); fbeaulier@664: d->transfers[line].objsize = count; fbeaulier@664: } fbeaulier@664: else fbeaulier@664: initSDOline(d, line, CliNbr, index, subIndex, SDO_DOWNLOAD_IN_PROGRESS); fbeaulier@663: d->transfers[line].count = count; fbeaulier@663: d->transfers[line].dataType = dataType; Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: { fbeaulier@663: UNS8* lineData = d->transfers[line].data; JaFojtik@694: if (count > SDO_MAX_LENGTH_TRANSFER) fbeaulier@663: { fbeaulier@663: d->transfers[line].dynamicData = (UNS8*) malloc(count); fbeaulier@663: d->transfers[line].dynamicDataSize = count; fbeaulier@663: if (d->transfers[line].dynamicData == NULL) fbeaulier@663: { fbeaulier@663: MSG_ERR(0x1AC9, "SDO. Error. Could not allocate enough bytes : ", count); fbeaulier@663: return 0xFE; fbeaulier@663: } fbeaulier@663: lineData = d->transfers[line].dynamicData; fbeaulier@663: } Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: fbeaulier@663: /* Copy data to transfers structure. */ fbeaulier@663: for (j = 0 ; j < count ; j++) { Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION Christian@636: # ifdef CANOPEN_BIG_ENDIAN fbeaulier@663: if (dataType == 0 && endianize) fbeaulier@663: lineData[count - 1 - j] = ((char *)data)[j]; fbeaulier@663: else /* String of bytes. */ fbeaulier@663: lineData[j] = ((char *)data)[j]; Christian@636: # else fbeaulier@663: lineData[j] = ((char *)data)[j]; Christian@636: # endif fbeaulier@663: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION etisserant@0: # ifdef CANOPEN_BIG_ENDIAN fbeaulier@663: if (dataType == 0 && endianize) fbeaulier@663: d->transfers[line].data[count - 1 - j] = ((char *)data)[j]; fbeaulier@663: else /* String of bytes. */ fbeaulier@663: d->transfers[line].data[j] = ((char *)data)[j]; etisserant@506: # else fbeaulier@663: d->transfers[line].data[j] = ((char *)data)[j]; etisserant@0: # endif Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: } fbeaulier@664: if(useBlockMode) { fbeaulier@664: buf[0] = (6 << 5) | (1 << 1 ); /* CCS = 6 , CC = 0 , S = 1 , CS = 0 */ fbeaulier@664: for (i = 0 ; i < 4 ; i++) fbeaulier@664: buf[i+4] = (UNS8)((count >> (i<<3))); /* i*8 */ fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: /* Send the SDO to the server. Initiate download, cs=1. */ JaFojtik@694: if (count <= 4) { /* Expedited transfer */ fbeaulier@664: buf[0] = (UNS8)((1 << 5) | ((4 - count) << 2) | 3); fbeaulier@664: for (i = 4 ; i < 8 ; i++) fbeaulier@664: buf[i] = d->transfers[line].data[i - 4]; fbeaulier@664: d->transfers[line].offset = count; fbeaulier@664: } JaFojtik@694: else { /** Normal transfer */ fbeaulier@664: buf[0] = (1 << 5) | 1; fbeaulier@664: for (i = 0 ; i < 4 ; i++) fbeaulier@664: buf[i+4] = (UNS8)((count >> (i<<3))); /* i*8 */ fbeaulier@664: } fbeaulier@664: } fbeaulier@663: buf[1] = index & 0xFF; /* LSB */ fbeaulier@663: buf[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@663: buf[3] = subIndex; fbeaulier@663: fbeaulier@663: d->transfers[line].Callback = Callback; fbeaulier@663: fbeaulier@663: err = sendSDO(d, SDO_CLIENT, CliNbr, buf); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AD1, "SDO. Error while sending SDO to node : ", nodeId); fbeaulier@663: /* release the line */ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: fbeaulier@663: fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param count fbeaulier@663: ** @param dataType fbeaulier@663: ** @param data Mongo@717: ** @param useBlockMode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@506: UNS8 writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, fbeaulier@664: UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, UNS8 useBlockMode) fbeaulier@664: { fbeaulier@664: return _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, NULL, 1, useBlockMode); etisserant@32: } etisserant@32: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param count fbeaulier@663: ** @param dataType fbeaulier@663: ** @param data fbeaulier@663: ** @param Callback Mongo@717: ** @param useBlockMode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@506: UNS8 writeNetworkDictCallBack (CO_Data* d, UNS8 nodeId, UNS16 index, fbeaulier@664: UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 useBlockMode) fbeaulier@664: { fbeaulier@664: return _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, Callback, 1, useBlockMode); etisserant@506: } etisserant@506: etisserant@506: UNS8 writeNetworkDictCallBackAI (CO_Data* d, UNS8 nodeId, UNS16 index, fbeaulier@664: UNS8 subIndex, UNS32 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 endianize, UNS8 useBlockMode) etisserant@506: { etisserant@506: UNS8 ret; etisserant@506: UNS16 lastIndex; etisserant@506: UNS16 offset; etisserant@506: UNS8 nodeIdServer; etisserant@506: UNS8 i; etisserant@506: fbeaulier@664: ret = _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, Callback, endianize, useBlockMode); etisserant@506: if(ret == 0xFE) etisserant@506: { etisserant@506: offset = d->firstIndex->SDO_CLT; etisserant@506: lastIndex = d->lastIndex->SDO_CLT; etisserant@506: if (offset == 0) etisserant@506: { etisserant@506: MSG_ERR(0x1AC6, "writeNetworkDict : No SDO client index found", 0); etisserant@506: return 0xFF; etisserant@506: } etisserant@506: i = 0; etisserant@506: while (offset <= lastIndex) etisserant@506: { etisserant@506: if (d->objdict[offset].bSubCount <= 3) fbeaulier@663: { fbeaulier@663: MSG_ERR(0x1AC8, "Subindex 3 not found at index ", 0x1280 + i); fbeaulier@663: return 0xFF; fbeaulier@663: } etisserant@506: nodeIdServer = *(UNS8*) d->objdict[offset].pSubindex[3].pObject; etisserant@506: if(nodeIdServer == 0) etisserant@506: { etisserant@506: *(UNS32*)d->objdict[offset].pSubindex[1].pObject = (UNS32)(0x600 + nodeId); etisserant@506: *(UNS32*)d->objdict[offset].pSubindex[2].pObject = (UNS32)(0x580 + nodeId); etisserant@506: *(UNS8*) d->objdict[offset].pSubindex[3].pObject = nodeId; Mongo@672: return _writeNetworkDict (d, nodeId, index, subIndex, count, dataType, data, Callback, endianize, useBlockMode); etisserant@506: } etisserant@506: offset++; etisserant@506: } etisserant@506: return 0xFF; etisserant@506: } etisserant@506: else if(ret == 0) etisserant@506: { etisserant@506: return 0; etisserant@506: } etisserant@506: else etisserant@506: { etisserant@506: return 0xFF; etisserant@506: } etisserant@506: } etisserant@506: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param dataType fbeaulier@663: ** @param Callback Mongo@717: ** @param useBlockMode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@664: INLINE UNS8 _readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, SDOCallback_t Callback, UNS8 useBlockMode) etisserant@0: { fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 i; fbeaulier@663: UNS8 CliNbr; fbeaulier@663: UNS8 line; fbeaulier@663: UNS8 data[8]; fbeaulier@663: fbeaulier@663: MSG_WAR(0x3AD5, "Send SDO to read in the dictionary of node : ", nodeId); fbeaulier@663: MSG_WAR(0x3AD6, " At index : ", index); fbeaulier@663: MSG_WAR(0x3AD7, " subIndex : ", subIndex); fbeaulier@663: fbeaulier@663: /* First let's find the corresponding SDO client in our OD */ fbeaulier@663: CliNbr = GetSDOClientFromNodeId( d, nodeId); fbeaulier@663: if(CliNbr >= 0xFE) fbeaulier@663: return CliNbr; fbeaulier@663: fbeaulier@663: /* Verify that there is no SDO communication yet. */ fbeaulier@663: err = getSDOlineOnUse(d, CliNbr, SDO_CLIENT, &line); fbeaulier@663: if (!err) { fbeaulier@663: MSG_ERR(0x1AD8, "SDO error : Communication yet established. with node : ", nodeId); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: /* Taking the line ... */ fbeaulier@663: err = getSDOfreeLine( d, SDO_CLIENT, &line ); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AD9, "SDO error : No line free, too many SDO in progress. Aborted for node : ", nodeId); fbeaulier@663: return (0xFF); fbeaulier@663: } fbeaulier@663: else fbeaulier@663: MSG_WAR(0x3AE0, "Transmission on line : ", line); fbeaulier@663: fbeaulier@664: if(useBlockMode) { fbeaulier@664: initSDOline(d, line, CliNbr, index, subIndex, SDO_BLOCK_UPLOAD_IN_PROGRESS); fbeaulier@664: /* Send the SDO to the server. Initiate block upload, cs=0. */ fbeaulier@664: d->transfers[line].dataType = dataType; fbeaulier@664: data[0] = (5 << 5) | SDO_BCS_INITIATE_UPLOAD_REQUEST; fbeaulier@664: data[1] = index & 0xFF; /* LSB */ fbeaulier@664: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@664: data[3] = subIndex; fbeaulier@664: data[4] = SDO_BLOCK_SIZE; fbeaulier@664: for (i = 5 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: } fbeaulier@664: else { fbeaulier@664: initSDOline(d, line, CliNbr, index, subIndex, SDO_UPLOAD_IN_PROGRESS); fbeaulier@664: /* Send the SDO to the server. Initiate upload, cs=2. */ fbeaulier@664: d->transfers[line].dataType = dataType; fbeaulier@664: data[0] = (2 << 5); fbeaulier@664: data[1] = index & 0xFF; /* LSB */ fbeaulier@664: data[2] = (index >> 8) & 0xFF; /* MSB */ fbeaulier@664: data[3] = subIndex; fbeaulier@664: for (i = 4 ; i < 8 ; i++) fbeaulier@664: data[i] = 0; fbeaulier@664: } fbeaulier@663: d->transfers[line].Callback = Callback; fbeaulier@663: err = sendSDO(d, SDO_CLIENT, CliNbr, data); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AE5, "SDO. Error while sending SDO to node : ", nodeId); fbeaulier@663: /* release the line */ fbeaulier@663: resetSDOline(d, line); fbeaulier@663: return 0xFF; fbeaulier@663: } fbeaulier@663: return 0; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param dataType Mongo@717: ** @param useBlockMode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@664: UNS8 readNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, UNS8 useBlockMode) fbeaulier@664: { fbeaulier@664: return _readNetworkDict (d, nodeId, index, subIndex, dataType, NULL, useBlockMode); etisserant@32: } etisserant@32: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param index fbeaulier@663: ** @param subIndex fbeaulier@663: ** @param dataType fbeaulier@663: ** @param Callback Mongo@717: ** @param useBlockMode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ fbeaulier@664: UNS8 readNetworkDictCallback (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, SDOCallback_t Callback, UNS8 useBlockMode) fbeaulier@664: { fbeaulier@664: return _readNetworkDict (d, nodeId, index, subIndex, dataType, Callback, useBlockMode); fbeaulier@664: } fbeaulier@664: fbeaulier@664: UNS8 readNetworkDictCallbackAI (CO_Data* d, UNS8 nodeId, UNS16 index, UNS8 subIndex, UNS8 dataType, SDOCallback_t Callback, UNS8 useBlockMode) etisserant@506: { etisserant@506: UNS8 ret; etisserant@506: UNS16 lastIndex; etisserant@506: UNS16 offset; etisserant@506: UNS8 nodeIdServer; etisserant@506: UNS8 i; etisserant@506: fbeaulier@664: ret = _readNetworkDict (d, nodeId, index, subIndex, dataType, Callback, useBlockMode); etisserant@506: if(ret == 0xFE) etisserant@506: { etisserant@506: offset = d->firstIndex->SDO_CLT; etisserant@506: lastIndex = d->lastIndex->SDO_CLT; etisserant@506: if (offset == 0) etisserant@506: { etisserant@506: MSG_ERR(0x1AC6, "writeNetworkDict : No SDO client index found", 0); etisserant@506: return 0xFF; etisserant@506: } etisserant@506: i = 0; etisserant@506: while (offset <= lastIndex) etisserant@506: { etisserant@506: if (d->objdict[offset].bSubCount <= 3) fbeaulier@663: { fbeaulier@663: MSG_ERR(0x1AC8, "Subindex 3 not found at index ", 0x1280 + i); fbeaulier@663: return 0xFF; fbeaulier@663: } etisserant@506: nodeIdServer = *(UNS8*) d->objdict[offset].pSubindex[3].pObject; etisserant@506: if(nodeIdServer == 0) etisserant@506: { etisserant@506: *(UNS32*)d->objdict[offset].pSubindex[1].pObject = (UNS32)(0x600 + nodeId); etisserant@506: *(UNS32*)d->objdict[offset].pSubindex[2].pObject = (UNS32)(0x580 + nodeId); etisserant@506: *(UNS8*) d->objdict[offset].pSubindex[3].pObject = nodeId; fbeaulier@664: return _readNetworkDict (d, nodeId, index, subIndex, dataType, Callback, useBlockMode); etisserant@506: } etisserant@506: offset++; etisserant@506: } etisserant@506: return 0xFF; etisserant@506: } etisserant@506: else if(ret == 0) etisserant@506: { etisserant@506: return 0; etisserant@506: } etisserant@506: else etisserant@506: { etisserant@506: return 0xFF; etisserant@506: } etisserant@506: } etisserant@506: etisserant@506: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param data fbeaulier@664: ** @param size : *size MUST contain the size of *data buffer before calling fbeaulier@664: ** The function set it to the actual number of written bytes fbeaulier@663: ** @param abortCode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@664: ** SDO_PROVIDED_BUFFER_TOO_SMALL if *data is not big enough fbeaulier@664: ** or any transmission status value. fbeaulier@663: **/ etisserant@539: UNS8 getReadResultNetworkDict (CO_Data* d, UNS8 nodeId, void* data, UNS32 *size, fbeaulier@663: UNS32 * abortCode) fbeaulier@663: { fbeaulier@663: UNS32 i; fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 CliNbr; fbeaulier@663: UNS8 line; fbeaulier@663: * abortCode = 0; fbeaulier@663: fbeaulier@663: /* First let's find the corresponding SDO client in our OD */ fbeaulier@663: CliNbr = GetSDOClientFromNodeId(d, nodeId); fbeaulier@664: if(CliNbr >= 0xFE) { fbeaulier@664: *size = 0; fbeaulier@663: return SDO_ABORTED_INTERNAL; fbeaulier@664: } fbeaulier@663: fbeaulier@663: /* Looking for the line tranfert. */ fbeaulier@663: err = getSDOlineOnUse(d, CliNbr, SDO_CLIENT, &line); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AF0, "SDO error : No line found for communication with node : ", nodeId); fbeaulier@664: *size = 0; fbeaulier@664: return SDO_ABORTED_INTERNAL; fbeaulier@664: } fbeaulier@664: JaFojtik@694: /* If transfer not finished just return, but if aborted set abort code and size to 0 */ fbeaulier@664: if (d->transfers[line].state != SDO_FINISHED) { fbeaulier@664: if((d->transfers[line].state == SDO_ABORTED_RCV) || (d->transfers[line].state == SDO_ABORTED_INTERNAL)) { fbeaulier@664: *abortCode = d->transfers[line].abortCode; fbeaulier@664: *size = 0; fbeaulier@664: } fbeaulier@663: return d->transfers[line].state; fbeaulier@664: } fbeaulier@663: fbeaulier@663: /* if SDO initiated with e=0 and s=0 count is null, offset carry effective size*/ fbeaulier@663: if( d->transfers[line].count == 0) fbeaulier@663: d->transfers[line].count = d->transfers[line].offset; fbeaulier@664: fbeaulier@664: /* Check if the provided buffer is big enough */ fbeaulier@664: if(*size < d->transfers[line].count) { fbeaulier@664: *size = 0; fbeaulier@664: return SDO_PROVIDED_BUFFER_TOO_SMALL; fbeaulier@664: } fbeaulier@664: fbeaulier@664: /* Give back actual size */ fbeaulier@664: *size = d->transfers[line].count; fbeaulier@663: fbeaulier@663: /* Copy payload to data pointer */ Christian@636: #ifdef SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: { fbeaulier@663: UNS8 *lineData = d->transfers[line].data; fbeaulier@663: fbeaulier@663: if (d->transfers[line].dynamicData && d->transfers[line].dynamicDataSize) fbeaulier@663: { fbeaulier@663: lineData = d->transfers[line].dynamicData; fbeaulier@663: } fbeaulier@663: for ( i = 0 ; i < *size ; i++) { Christian@636: # ifdef CANOPEN_BIG_ENDIAN fbeaulier@663: if (d->transfers[line].dataType != visible_string) fbeaulier@663: ( (char *) data)[*size - 1 - i] = lineData[i]; fbeaulier@663: else /* String of bytes. */ fbeaulier@663: ( (char *) data)[i] = lineData[i]; Christian@636: # else fbeaulier@663: ( (char *) data)[i] = lineData[i]; Christian@636: # endif fbeaulier@663: } fbeaulier@663: } Christian@636: #else //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@663: for ( i = 0 ; i < *size ; i++) { etisserant@0: # ifdef CANOPEN_BIG_ENDIAN fbeaulier@663: if (d->transfers[line].dataType != visible_string) fbeaulier@663: ( (char *) data)[*size - 1 - i] = d->transfers[line].data[i]; fbeaulier@663: else /* String of bytes. */ fbeaulier@663: ( (char *) data)[i] = d->transfers[line].data[i]; etisserant@506: # else fbeaulier@663: ( (char *) data)[i] = d->transfers[line].data[i]; etisserant@0: # endif fbeaulier@663: } Christian@636: #endif //SDO_DYNAMIC_BUFFER_ALLOCATION fbeaulier@664: resetSDOline(d, line); fbeaulier@663: return SDO_FINISHED; fbeaulier@663: } fbeaulier@663: fbeaulier@663: /*! fbeaulier@663: ** fbeaulier@663: ** fbeaulier@663: ** @param d fbeaulier@663: ** @param nodeId fbeaulier@663: ** @param abortCode fbeaulier@663: ** fbeaulier@663: ** @return fbeaulier@663: **/ etisserant@0: UNS8 getWriteResultNetworkDict (CO_Data* d, UNS8 nodeId, UNS32 * abortCode) etisserant@0: { fbeaulier@663: UNS8 line = 0; fbeaulier@663: UNS8 err; fbeaulier@663: UNS8 CliNbr; fbeaulier@663: * abortCode = 0; fbeaulier@663: fbeaulier@663: /* First let's find the corresponding SDO client in our OD */ fbeaulier@663: CliNbr = GetSDOClientFromNodeId(d, nodeId); fbeaulier@663: if(CliNbr >= 0xFE) fbeaulier@663: return SDO_ABORTED_INTERNAL; fbeaulier@663: fbeaulier@663: /* Looking for the line tranfert. */ fbeaulier@663: err = getSDOlineOnUse(d, CliNbr, SDO_CLIENT, &line); fbeaulier@663: if (err) { fbeaulier@663: MSG_ERR(0x1AF1, "SDO error : No line found for communication with node : ", nodeId); fbeaulier@663: return SDO_ABORTED_INTERNAL; fbeaulier@663: } fbeaulier@663: * abortCode = d->transfers[line].abortCode; fbeaulier@664: if (d->transfers[line].state != SDO_FINISHED) fbeaulier@664: return d->transfers[line].state; fbeaulier@664: resetSDOline(d, line); fbeaulier@664: return SDO_FINISHED; fbeaulier@664: }