Lely core libraries  2.3.4
ssdo.c File Reference
#include "co.h"
#include "sdo.h"
#include <lely/co/crc.h>
#include <lely/co/dev.h>
#include <lely/co/obj.h>
#include <lely/co/ssdo.h>
#include <lely/co/val.h>
#include <lely/util/endian.h>
#include <lely/util/errnum.h>
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for ssdo.c:

Go to the source code of this file.

Data Structures

struct  __co_ssdo
 A CANopen Server-SDO. More...
 
struct  __co_ssdo_state
 A CANopen Server-SDO state. More...
 

Typedefs

typedef const struct __co_ssdo_state co_ssdo_state_t
 An opaque CANopen Server-SDO state type.
 

Functions

static int co_ssdo_update (co_ssdo_t *sdo)
 Updates and (de)activates a Server-SDO service. More...
 
static co_unsigned32_t co_1200_dn_ind (co_sub_t *sub, struct co_sdo_req *req, void *data)
 The download indication function for (all sub-objects of) CANopen objects 1200..127F (SDO server parameter). More...
 
static int co_ssdo_recv (const struct can_msg *msg, void *data)
 The CAN receive callback function for a Server-SDO service. More...
 
static int co_ssdo_timer (const struct timespec *tp, void *data)
 The CAN timer callback function for a Server-SDO service. More...
 
static void co_ssdo_enter (co_ssdo_t *sdo, co_ssdo_state_t *next)
 Enters the specified state of a Server-SDO service.
 
static void co_ssdo_emit_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 Invokes the 'abort' transition function of the current state of a Server-SDO service. More...
 
static void co_ssdo_emit_time (co_ssdo_t *sdo, const struct timespec *tp)
 Invokes the 'timeout' transition function of the current state of a Server-SDO service. More...
 
static void co_ssdo_emit_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 Invokes the 'CAN frame received' transition function of the current state of a Server-SDO service. More...
 
static co_ssdo_state_tco_ssdo_wait_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'abort' transition function of the 'waiting' state.
 
static co_ssdo_state_tco_ssdo_wait_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'waiting' state.
 
static co_ssdo_state_tco_ssdo_dn_ini_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'download initiate' state.
 
static co_ssdo_state_tco_ssdo_dn_seg_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'download initiate' state. More...
 
static co_ssdo_state_tco_ssdo_dn_seg_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'download segment' state.
 
static co_ssdo_state_tco_ssdo_dn_seg_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'download segment' state.
 
static co_ssdo_state_tco_ssdo_up_ini_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'upload initiate' state.
 
static co_ssdo_state_tco_ssdo_up_seg_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'upload initiate' state. More...
 
static co_ssdo_state_tco_ssdo_up_seg_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'upload segment' state.
 
static co_ssdo_state_tco_ssdo_up_seg_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'upload segment' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_ini_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block download initiate' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_sub_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'block download initiate' state. More...
 
static co_ssdo_state_tco_ssdo_blk_dn_sub_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'block download sub-block' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_sub_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block download sub-block' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_end_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'abort' transition function of the 'block download end' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_end_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'block download end' state.
 
static co_ssdo_state_tco_ssdo_blk_dn_end_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block download end' state.
 
static co_ssdo_state_tco_ssdo_blk_up_ini_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block upload initiate' state.
 
static co_ssdo_state_tco_ssdo_blk_up_sub_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'block upload initiate' state. More...
 
static co_ssdo_state_tco_ssdo_blk_up_sub_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'block upload sub-block' state.
 
static co_ssdo_state_tco_ssdo_blk_up_sub_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block upload sub-block' state.
 
static co_ssdo_state_tco_ssdo_blk_up_end_on_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 The 'abort' transition function of the 'block upload end' state.
 
static co_ssdo_state_tco_ssdo_blk_up_end_on_time (co_ssdo_t *sdo, const struct timespec *tp)
 The 'timeout' transition function of the 'block upload end' state.
 
static co_ssdo_state_tco_ssdo_blk_up_end_on_recv (co_ssdo_t *sdo, const struct can_msg *msg)
 The 'CAN frame received' transition function of the 'block upload end' state.
 
static co_ssdo_state_tco_ssdo_abort_ind (co_ssdo_t *sdo)
 Processes an abort transfer indication by aborting any ongoing transfer of a Server-SDO and returning it to the waiting state. More...
 
static co_ssdo_state_tco_ssdo_abort_res (co_ssdo_t *sdo, co_unsigned32_t ac)
 Sends an abort transfer request and aborts any ongoing transfer by invoking co_ssdo_abort_ind(). More...
 
static co_unsigned32_t co_ssdo_dn_ind (co_ssdo_t *sdo)
 Processes a download indication of a Server-SDO by checking access to the requested sub-object and reading the data from the frame. More...
 
static co_unsigned32_t co_ssdo_up_ind (co_ssdo_t *sdo)
 Processes an upload indication of a Server-SDO by checking access to the requested sub-object and writing the data to the internal buffer. More...
 
static co_unsigned32_t co_ssdo_up_buf (co_ssdo_t *sdo, size_t nbyte)
 Copies at most nbyte bytes from a CANopen SDO upload request, obtaining more bytes with co_ssdo_up_ind() as necessary. More...
 
static void co_ssdo_send_abort (co_ssdo_t *sdo, co_unsigned32_t ac)
 Sends an abort transfer request. More...
 
static void co_ssdo_send_dn_ini_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'download initiate' response.
 
static void co_ssdo_send_dn_seg_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'download segment' response.
 
static void co_ssdo_send_up_exp_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'upload initiate' (expedited) response.
 
static void co_ssdo_send_up_ini_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'upload initiate' response.
 
static void co_ssdo_send_up_seg_res (co_ssdo_t *sdo, int last)
 Sends a Server-SDO 'upload segment' response. More...
 
static void co_ssdo_send_blk_dn_ini_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'block download initiate' response.
 
static void co_ssdo_send_blk_dn_sub_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'block upload sub-block' response.
 
static void co_ssdo_send_blk_dn_end_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'block download end' response.
 
static void co_ssdo_send_blk_up_ini_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'block upload initiate' response.
 
static void co_ssdo_send_blk_up_sub_res (co_ssdo_t *sdo, int last)
 Sends a Server-SDO 'block upload sub-block' response. More...
 
static void co_ssdo_send_blk_up_end_res (co_ssdo_t *sdo)
 Sends a Server-SDO 'block upload end' response.
 
static void co_ssdo_init_ini_res (co_ssdo_t *sdo, struct can_msg *msg, co_unsigned8_t cs)
 Initializes a Server-SDO download/upload initiate response CAN frame. More...
 
static void co_ssdo_init_seg_res (co_ssdo_t *sdo, struct can_msg *msg, co_unsigned8_t cs)
 Initializes a Server-SDO download/upload segment response CAN frame. More...
 
co_ssdo_tco_ssdo_create (can_net_t *net, co_dev_t *dev, co_unsigned8_t num)
 Creates a new CANopen Server-SDO service. More...
 
void co_ssdo_destroy (co_ssdo_t *ssdo)
 Destroys a CANopen Server-SDO service. More...
 
int co_ssdo_start (co_ssdo_t *sdo)
 Starts a Server-SDO service. More...
 
void co_ssdo_stop (co_ssdo_t *sdo)
 Stops a Server-SDO service. More...
 
int co_ssdo_is_stopped (const co_ssdo_t *sdo)
 Retuns 1 if the specified Server-SDO service is stopped, and 0 if not. More...
 
can_net_tco_ssdo_get_net (const co_ssdo_t *sdo)
 Returns a pointer to the CAN network of a Server-SDO.
 
co_dev_tco_ssdo_get_dev (const co_ssdo_t *sdo)
 Returns a pointer to the CANopen device of a Server-SDO.
 
co_unsigned8_t co_ssdo_get_num (const co_ssdo_t *sdo)
 Returns the SDO number of a Server-SDO.
 
const struct co_sdo_parco_ssdo_get_par (const co_ssdo_t *sdo)
 Returns a pointer to the SDO parameter record of a Server-SDO.
 
int co_ssdo_get_timeout (const co_ssdo_t *sdo)
 Returns the timeout (in milliseconds) of a Server-SDO. More...
 
void co_ssdo_set_timeout (co_ssdo_t *sdo, int timeout)
 Sets the timeout of a Server-SDO. More...
 

Variables

static co_ssdo_state_t *const co_ssdo_stopped_state = &(co_ssdo_state_t){ .on_recv = NULL }
 The 'stopped' state.
 
static co_ssdo_state_t *const co_ssdo_wait_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_wait_on_abort, .on_recv = &co_ssdo_wait_on_recv }
 The 'waiting' state.
 
static co_ssdo_state_t *const co_ssdo_dn_seg_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_dn_seg_on_abort, .on_time = &co_ssdo_dn_seg_on_time, .on_recv = &co_ssdo_dn_seg_on_recv }
 The 'download segment' state.
 
static co_ssdo_state_t *const co_ssdo_up_seg_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_up_seg_on_abort, .on_time = &co_ssdo_up_seg_on_time, .on_recv = &co_ssdo_up_seg_on_recv }
 The 'upload segment' state.
 
static co_ssdo_state_t *const co_ssdo_blk_dn_sub_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_blk_dn_sub_on_abort, .on_time = &co_ssdo_blk_dn_sub_on_time, .on_recv = &co_ssdo_blk_dn_sub_on_recv }
 The 'block download sub-block' state.
 
static co_ssdo_state_t *const co_ssdo_blk_dn_end_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_blk_dn_end_on_abort, .on_time = &co_ssdo_blk_dn_end_on_time, .on_recv = &co_ssdo_blk_dn_end_on_recv }
 The 'block download end' state.
 
static co_ssdo_state_t *const co_ssdo_blk_up_sub_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_blk_up_sub_on_abort, .on_time = &co_ssdo_blk_up_sub_on_time, .on_recv = &co_ssdo_blk_up_sub_on_recv }
 The 'block upload sub-block' state.
 
static co_ssdo_state_t *const co_ssdo_blk_up_end_state = &(co_ssdo_state_t){ .on_abort = &co_ssdo_blk_up_end_on_abort, .on_time = &co_ssdo_blk_up_end_on_time, .on_recv = &co_ssdo_blk_up_end_on_recv }
 The 'block upload end' state.
 

Detailed Description

This file is part of the CANopen library; it contains the implementation of the Server-SDO functions.

See also
lely/co/ssdo.h, src/sdo.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file ssdo.c.

Function Documentation

◆ co_ssdo_update()

static int co_ssdo_update ( co_ssdo_t sdo)
static

Updates and (de)activates a Server-SDO service.

This function is invoked when one of the SDO server parameters (objects 1200..127F) is updated.

Returns
0 on success, or -1 on error.

Definition at line 803 of file ssdo.c.

◆ co_1200_dn_ind()

static co_unsigned32_t co_1200_dn_ind ( co_sub_t sub,
struct co_sdo_req req,
void *  data 
)
static

The download indication function for (all sub-objects of) CANopen objects 1200..127F (SDO server parameter).

See also
co_sub_dn_ind_t

Definition at line 830 of file ssdo.c.

◆ co_ssdo_recv()

static int co_ssdo_recv ( const struct can_msg msg,
void *  data 
)
static

The CAN receive callback function for a Server-SDO service.

See also
can_recv_func_t

Definition at line 916 of file ssdo.c.

◆ co_ssdo_timer()

static int co_ssdo_timer ( const struct timespec *  tp,
void *  data 
)
static

The CAN timer callback function for a Server-SDO service.

See also
can_timer_func_t

Definition at line 938 of file ssdo.c.

◆ co_ssdo_emit_abort()

static void co_ssdo_emit_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
inlinestatic

Invokes the 'abort' transition function of the current state of a Server-SDO service.

Parameters
sdoa pointer to a Server-SDO service.
acthe abort code.

Definition at line 959 of file ssdo.c.

◆ co_ssdo_emit_time()

static void co_ssdo_emit_time ( co_ssdo_t sdo,
const struct timespec *  tp 
)
inlinestatic

Invokes the 'timeout' transition function of the current state of a Server-SDO service.

Parameters
sdoa pointer to a Server-SDO service.
tpa pointer to the current time.

Definition at line 969 of file ssdo.c.

◆ co_ssdo_emit_recv()

static void co_ssdo_emit_recv ( co_ssdo_t sdo,
const struct can_msg msg 
)
inlinestatic

Invokes the 'CAN frame received' transition function of the current state of a Server-SDO service.

Parameters
sdoa pointer to a Server-SDO service.
msga pointer to the received CAN frame.

Definition at line 979 of file ssdo.c.

◆ co_ssdo_dn_seg_on_abort()

static co_ssdo_state_t * co_ssdo_dn_seg_on_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

The 'download initiate' state.

The 'abort' transition function of the 'download segment' state.

Definition at line 1072 of file ssdo.c.

◆ co_ssdo_up_seg_on_abort()

static co_ssdo_state_t * co_ssdo_up_seg_on_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

The 'upload initiate' state.

The 'abort' transition function of the 'upload segment' state.

Definition at line 1175 of file ssdo.c.

◆ co_ssdo_blk_dn_sub_on_abort()

static co_ssdo_state_t * co_ssdo_blk_dn_sub_on_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

The 'block download initiate' state.

The 'abort' transition function of the 'block download sub-block' state.

Definition at line 1276 of file ssdo.c.

◆ co_ssdo_blk_up_sub_on_abort()

static co_ssdo_state_t * co_ssdo_blk_up_sub_on_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

The 'block upload initiate' state.

The 'abort' transition function of the 'block upload sub-block' state.

Definition at line 1476 of file ssdo.c.

◆ co_ssdo_abort_ind()

static co_ssdo_state_t * co_ssdo_abort_ind ( co_ssdo_t sdo)
static

Processes an abort transfer indication by aborting any ongoing transfer of a Server-SDO and returning it to the waiting state.

Returns
co_ssdo_wait_state

Definition at line 1596 of file ssdo.c.

◆ co_ssdo_abort_res()

static co_ssdo_state_t * co_ssdo_abort_res ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

Sends an abort transfer request and aborts any ongoing transfer by invoking co_ssdo_abort_ind().

Parameters
sdoa pointer to a Server-SDO service.
acthe SDO abort code.
Returns
co_ssdo_wait_state
See also
co_ssdo_send_abort()

Definition at line 1620 of file ssdo.c.

◆ co_ssdo_dn_ind()

static co_unsigned32_t co_ssdo_dn_ind ( co_ssdo_t sdo)
static

Processes a download indication of a Server-SDO by checking access to the requested sub-object and reading the data from the frame.

Returns
0 on success, or an SDO abort code on error.

Definition at line 1630 of file ssdo.c.

◆ co_ssdo_up_ind()

static co_unsigned32_t co_ssdo_up_ind ( co_ssdo_t sdo)
static

Processes an upload indication of a Server-SDO by checking access to the requested sub-object and writing the data to the internal buffer.

Returns
0 on success, or an SDO abort code on error.

Definition at line 1648 of file ssdo.c.

◆ co_ssdo_up_buf()

static co_unsigned32_t co_ssdo_up_buf ( co_ssdo_t sdo,
size_t  nbyte 
)
static

Copies at most nbyte bytes from a CANopen SDO upload request, obtaining more bytes with co_ssdo_up_ind() as necessary.

Returns
0 on success, or an SDO abort code on error.

Definition at line 1672 of file ssdo.c.

◆ co_ssdo_send_abort()

static void co_ssdo_send_abort ( co_ssdo_t sdo,
co_unsigned32_t  ac 
)
static

Sends an abort transfer request.

Parameters
sdoa pointer to a Server-SDO service.
acthe SDO abort code.

Definition at line 1702 of file ssdo.c.

◆ co_ssdo_send_up_seg_res()

static void co_ssdo_send_up_seg_res ( co_ssdo_t sdo,
int  last 
)
static

Sends a Server-SDO 'upload segment' response.

Parameters
sdoa pointer to a Server-SDO service.
lasta flag indicating whether this is the last segment.

Definition at line 1771 of file ssdo.c.

◆ co_ssdo_send_blk_up_sub_res()

static void co_ssdo_send_blk_up_sub_res ( co_ssdo_t sdo,
int  last 
)
static

Sends a Server-SDO 'block upload sub-block' response.

Parameters
sdoa pointer to a Server-SDO service.
lasta flag indicating whether this block contains the last segment.

Definition at line 1850 of file ssdo.c.

◆ co_ssdo_init_ini_res()

static void co_ssdo_init_ini_res ( co_ssdo_t sdo,
struct can_msg msg,
co_unsigned8_t  cs 
)
static

Initializes a Server-SDO download/upload initiate response CAN frame.

Parameters
sdoa pointer to a Server-SDO service.
msga pointer to the CAN frame to be initialized.
csthe command specifier.

Definition at line 1888 of file ssdo.c.

◆ co_ssdo_init_seg_res()

static void co_ssdo_init_seg_res ( co_ssdo_t sdo,
struct can_msg msg,
co_unsigned8_t  cs 
)
static

Initializes a Server-SDO download/upload segment response CAN frame.

Parameters
sdoa pointer to a Server-SDO service.
msga pointer to the CAN frame to be initialized.
csthe command specifier.

Definition at line 1908 of file ssdo.c.

◆ co_ssdo_create()

co_ssdo_t* co_ssdo_create ( can_net_t net,
co_dev_t dev,
co_unsigned8_t  num 
)

Creates a new CANopen Server-SDO service.

The service is started as if by co_ssdo_start().

Parameters
neta pointer to a CAN network.
deva pointer to a CANopen device describing the server.
numthe SDO number (in the range [1..128]). Except when num is 1, the SDO parameter record MUST exist in the object dictionary of dev.
Returns
a pointer to a new Server-SDO service, or NULL on error. In the latter case, the error number can be obtained with get_errc().
See also
co_ssdo_destroy()

Definition at line 650 of file ssdo.c.

◆ co_ssdo_destroy()

void co_ssdo_destroy ( co_ssdo_t sdo)

Destroys a CANopen Server-SDO service.

See also
co_ssdo_create()

Definition at line 677 of file ssdo.c.

◆ co_ssdo_start()

int co_ssdo_start ( co_ssdo_t sdo)

Starts a Server-SDO service.

Postcondition
on success, co_ssdo_is_stopped() returns 0.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_ssdo_stop()

Definition at line 687 of file ssdo.c.

◆ co_ssdo_stop()

void co_ssdo_stop ( co_ssdo_t sdo)

Stops a Server-SDO service.

Any ongoing request is aborted.

Postcondition
co_ssdo_is_stopped() returns 1.
See also
co_ssdo_start()

Definition at line 721 of file ssdo.c.

◆ co_ssdo_is_stopped()

int co_ssdo_is_stopped ( const co_ssdo_t sdo)

Retuns 1 if the specified Server-SDO service is stopped, and 0 if not.

See also
co_ssdo_start, co_ssdo_stop()

Definition at line 744 of file ssdo.c.

◆ co_ssdo_get_timeout()

int co_ssdo_get_timeout ( const co_ssdo_t sdo)

Returns the timeout (in milliseconds) of a Server-SDO.

A return value of 0 (the default) means no timeout is being used.

See also
co_ssdo_set_timeout()

Definition at line 784 of file ssdo.c.

◆ co_ssdo_set_timeout()

void co_ssdo_set_timeout ( co_ssdo_t sdo,
int  timeout 
)

Sets the timeout of a Server-SDO.

If the timeout expires before receiving a request from a client, the server aborts the transfer.

Parameters
sdoa pointer to a Server-SDO service.
timeoutthe timeout (in milliseconds). A value of 0 (the default) disables the timeout.
See also
co_ssdo_get_timeout()

Definition at line 792 of file ssdo.c.