Lely core libraries  1.9.2
dev.c File Reference

This file is part of the CANopen library; it contains the implementation of the device description. More...

#include "co.h"
#include <lely/util/cmp.h>
#include <lely/util/errnum.h>
#include <lely/util/frbuf.h>
#include <lely/util/fwbuf.h>
#include "obj.h"
#include <lely/co/dev.h>
#include <assert.h>
#include <stdlib.h>
#include <lely/co/def/basic.def>
Include dependency graph for dev.c:

Go to the source code of this file.

Data Structures

struct  __co_dev
 A CANopen device. More...
 

Functions

co_dev_tco_dev_create (co_unsigned8_t id)
 Creates a new CANopen device. More...
 
void co_dev_destroy (co_dev_t *dev)
 Destroys a CANopen device, including all objects in its object dictionary. More...
 
co_unsigned8_t co_dev_get_netid (const co_dev_t *dev)
 Returns the network-ID of a CANopen device. More...
 
int co_dev_set_netid (co_dev_t *dev, co_unsigned8_t id)
 Sets the network-ID of a CANopen device. More...
 
co_unsigned8_t co_dev_get_id (const co_dev_t *dev)
 Returns the node-ID of a CANopen device. More...
 
int co_dev_set_id (co_dev_t *dev, co_unsigned8_t id)
 Sets the node-ID of a CANopen device. More...
 
co_unsigned16_t co_dev_get_idx (const co_dev_t *dev, co_unsigned16_t maxidx, co_unsigned16_t *idx)
 Retrieves a list of object indices in the object dictionary of a CANopen device. More...
 
int co_dev_insert_obj (co_dev_t *dev, co_obj_t *obj)
 Inserts an object into the object dictionary of a CANopen device. More...
 
int co_dev_remove_obj (co_dev_t *dev, co_obj_t *obj)
 Removes an object from the object dictionary a CANopen device. More...
 
co_obj_tco_dev_find_obj (const co_dev_t *dev, co_unsigned16_t idx)
 Finds an object in the object dictionary of a CANopen device. More...
 
co_sub_tco_dev_find_sub (const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
 Finds a sub-object in the object dictionary of a CANopen device. More...
 
const char * co_dev_get_name (const co_dev_t *dev)
 Returns the name of a CANopen device. More...
 
int co_dev_set_name (co_dev_t *dev, const char *name)
 Sets the name of a CANopen device. More...
 
const char * co_dev_get_vendor_name (const co_dev_t *dev)
 Returns a pointer to the vendor name of a CANopen device. More...
 
int co_dev_set_vendor_name (co_dev_t *dev, const char *vendor_name)
 Sets the vendor name of a CANopen device. More...
 
co_unsigned32_t co_dev_get_vendor_id (const co_dev_t *dev)
 Returns the vendor ID of a CANopen device. More...
 
void co_dev_set_vendor_id (co_dev_t *dev, co_unsigned32_t vendor_id)
 Sets the vendor ID of a CANopen device. More...
 
const char * co_dev_get_product_name (const co_dev_t *dev)
 Returns a pointer to the product name of a CANopen device. More...
 
int co_dev_set_product_name (co_dev_t *dev, const char *product_name)
 Sets the product name of a CANopen device. More...
 
co_unsigned32_t co_dev_get_product_code (const co_dev_t *dev)
 Returns the product code of a CANopen device. More...
 
void co_dev_set_product_code (co_dev_t *dev, co_unsigned32_t product_code)
 Sets the product code of a CANopen device. More...
 
co_unsigned32_t co_dev_get_revision (const co_dev_t *dev)
 Returns the revision number of a CANopen device. More...
 
void co_dev_set_revision (co_dev_t *dev, co_unsigned32_t revision)
 Sets the revision number of a CANopen device. More...
 
const char * co_dev_get_order_code (const co_dev_t *dev)
 Returns a pointer to the order code of a CANopen device. More...
 
int co_dev_set_order_code (co_dev_t *dev, const char *order_code)
 Sets the order code of a CANopen device. More...
 
unsigned int co_dev_get_baud (const co_dev_t *dev)
 Returns the supported bit rates of a CANopen device (any combination of CO_BAUD_1000, CO_BAUD_800, CO_BAUD_500, CO_BAUD_250, CO_BAUD_125, CO_BAUD_50, CO_BAUD_20, CO_BAUD_10 and CO_BAUD_AUTO). More...
 
void co_dev_set_baud (co_dev_t *dev, unsigned int baud)
 Sets the supported bit rates of a CANopen device. More...
 
co_unsigned16_t co_dev_get_rate (const co_dev_t *dev)
 Returns the (pending) baudrate of a CANopen device (in kbit/s). More...
 
void co_dev_set_rate (co_dev_t *dev, co_unsigned16_t rate)
 Sets the (pending) baudrate of a CANopen device. More...
 
int co_dev_get_lss (const co_dev_t *dev)
 Returns 1 if LSS is supported and 0 if not. More...
 
void co_dev_set_lss (co_dev_t *dev, int lss)
 Sets the LSS support flag. More...
 
co_unsigned32_t co_dev_get_dummy (const co_dev_t *dev)
 Returns the data types supported by a CANopen device for mapping dummy entries in PDOs (one bit for each of the basic types). More...
 
void co_dev_set_dummy (co_dev_t *dev, co_unsigned32_t dummy)
 Sets the data types supported by a CANopen device for mapping dummy entries in PDOs. More...
 
const void * co_dev_get_val (const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx)
 Returns a pointer to the current value of a CANopen sub-object. More...
 
size_t co_dev_set_val (co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx, const void *ptr, size_t n)
 Sets the current value of a CANopen sub-object. More...
 
size_t co_dev_read_sub (co_dev_t *dev, co_unsigned16_t *pidx, co_unsigned8_t *psubidx, const uint8_t *begin, const uint8_t *end)
 Reads a value from a memory buffer, in the concise DCF format, and stores it in a sub-object in the object dictionary of a CANopen device. More...
 
size_t co_dev_write_sub (const co_dev_t *dev, co_unsigned16_t idx, co_unsigned8_t subidx, uint8_t *begin, uint8_t *end)
 Loads the value of a sub-object from the object dictionary of a CANopen device, and writes it to a memory buffer, in the concise DCF format. More...
 
int co_dev_read_dcf (co_dev_t *dev, co_unsigned16_t *pmin, co_unsigned16_t *pmax, void *const *ptr)
 Reads the values of a range of objects from a memory buffer, in the concise DCF format, and stores them in the object dictionary of a CANopen device. More...
 
int co_dev_read_dcf_file (co_dev_t *dev, co_unsigned16_t *pmin, co_unsigned16_t *pmax, const char *filename)
 Reads the values of a range of objects from a file, in the concise DCF format, and stores them in the object dictionary of a CANopen device. More...
 
int co_dev_write_dcf (const co_dev_t *dev, co_unsigned16_t min, co_unsigned16_t max, void **ptr)
 Loads the values of a range of objects in the object dictionary of a CANopen device, and writes them to a memory buffer, in the concise DCF format. More...
 
int co_dev_write_dcf_file (const co_dev_t *dev, co_unsigned16_t min, co_unsigned16_t max, const char *filename)
 Loads the values of a range of objects in the object dictionary of a CANopen device, and writes them to a file, in the concise DCF format. More...
 

Detailed Description

This file is part of the CANopen library; it contains the implementation of the device description.

See also
lely/co/dev.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 dev.c.

Function Documentation

◆ co_dev_create()

co_dev_t* co_dev_create ( co_unsigned8_t  id)

Creates a new CANopen device.

Parameters
idthe node-ID of the device (in the range [1..127, 255]).
Returns
a pointer to a new CANopen device, or NULL on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_destroy()

Definition at line 141 of file dev.c.

◆ co_dev_destroy()

void co_dev_destroy ( co_dev_t dev)

Destroys a CANopen device, including all objects in its object dictionary.

See also
co_dev_create()

Definition at line 166 of file dev.c.

◆ co_dev_get_netid()

co_unsigned8_t co_dev_get_netid ( const co_dev_t dev)

Returns the network-ID of a CANopen device.

See also
co_dev_set_netid()

Definition at line 175 of file dev.c.

◆ co_dev_set_netid()

int co_dev_set_netid ( co_dev_t dev,
co_unsigned8_t  id 
)

Sets the network-ID of a CANopen device.

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

Definition at line 183 of file dev.c.

◆ co_dev_get_id()

co_unsigned8_t co_dev_get_id ( const co_dev_t dev)

Returns the node-ID of a CANopen device.

See also
co_dev_set_id()

Definition at line 198 of file dev.c.

◆ co_dev_set_id()

int co_dev_set_id ( co_dev_t dev,
co_unsigned8_t  id 
)

Sets the node-ID of a CANopen device.

This function will also update any sub-object values of the form $NODEID { "+" number }.

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

Definition at line 206 of file dev.c.

◆ co_dev_get_idx()

co_unsigned16_t co_dev_get_idx ( const co_dev_t dev,
co_unsigned16_t  maxidx,
co_unsigned16_t *  idx 
)

Retrieves a list of object indices in the object dictionary of a CANopen device.

Parameters
deva pointer to a CANopen device.
maxidxthe maximum number of object indices to return.
idxan array of at least maxidx indices (can be NULL). On success, *idx contains the object indices.
Returns
the total number of object indices in the object dictionary (which may be different from maxidx).

Definition at line 224 of file dev.c.

◆ co_dev_insert_obj()

int co_dev_insert_obj ( co_dev_t dev,
co_obj_t obj 
)

Inserts an object into the object dictionary of a CANopen device.

This function fails if the object is already part of the object dictionary of another device, or if another object with the same index already exists.

Parameters
deva pointer to a CANopen device.
obja pointer to the object to be inserted.
Returns
0 on success, or -1 on error.
See also
co_dev_remove_obj(), co_dev_find_obj()

Definition at line 243 of file dev.c.

◆ co_dev_remove_obj()

int co_dev_remove_obj ( co_dev_t dev,
co_obj_t obj 
)

Removes an object from the object dictionary a CANopen device.

Parameters
deva pointer to a CANopen device.
obja pointer to the object to be removed.
Returns
0 on success, or -1 on error.
See also
co_dev_insert_obj()

Definition at line 264 of file dev.c.

◆ co_dev_find_obj()

co_obj_t* co_dev_find_obj ( const co_dev_t dev,
co_unsigned16_t  idx 
)

Finds an object in the object dictionary of a CANopen device.

Parameters
deva pointer to a CANopen device.
idxthe object index.
Returns
a pointer to the object if found, or NULL if not.
See also
co_dev_insert_obj()

Definition at line 279 of file dev.c.

◆ co_dev_find_sub()

co_sub_t* co_dev_find_sub ( const co_dev_t dev,
co_unsigned16_t  idx,
co_unsigned8_t  subidx 
)

Finds a sub-object in the object dictionary of a CANopen device.

Parameters
deva pointer to a CANopen device.
idxthe object index.
subidxthe object sub-index.
Returns
a pointer to the sub-object if found, or NULL if not.

Definition at line 290 of file dev.c.

◆ co_dev_get_name()

const char* co_dev_get_name ( const co_dev_t dev)

Returns the name of a CANopen device.

See also
co_dev_set_name()

Definition at line 297 of file dev.c.

◆ co_dev_set_name()

int co_dev_set_name ( co_dev_t dev,
const char *  name 
)

Sets the name of a CANopen device.

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

Definition at line 305 of file dev.c.

◆ co_dev_get_vendor_name()

const char* co_dev_get_vendor_name ( const co_dev_t dev)

Returns a pointer to the vendor name of a CANopen device.

See also
co_dev_set_vendor_name()

Definition at line 327 of file dev.c.

◆ co_dev_set_vendor_name()

int co_dev_set_vendor_name ( co_dev_t dev,
const char *  vendor_name 
)

Sets the vendor name of a CANopen device.

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

Definition at line 335 of file dev.c.

◆ co_dev_get_vendor_id()

co_unsigned32_t co_dev_get_vendor_id ( const co_dev_t dev)

Returns the vendor ID of a CANopen device.

See also
co_dev_set_vendor_id()

Definition at line 357 of file dev.c.

◆ co_dev_set_vendor_id()

void co_dev_set_vendor_id ( co_dev_t dev,
co_unsigned32_t  vendor_id 
)

Sets the vendor ID of a CANopen device.

See also
co_dev_get_vendor_id()

Definition at line 365 of file dev.c.

◆ co_dev_get_product_name()

const char* co_dev_get_product_name ( const co_dev_t dev)

Returns a pointer to the product name of a CANopen device.

See also
co_dev_set_product_name()

Definition at line 373 of file dev.c.

◆ co_dev_set_product_name()

int co_dev_set_product_name ( co_dev_t dev,
const char *  product_name 
)

Sets the product name of a CANopen device.

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

Definition at line 381 of file dev.c.

◆ co_dev_get_product_code()

co_unsigned32_t co_dev_get_product_code ( const co_dev_t dev)

Returns the product code of a CANopen device.

See also
co_dev_set_product_code()

Definition at line 403 of file dev.c.

◆ co_dev_set_product_code()

void co_dev_set_product_code ( co_dev_t dev,
co_unsigned32_t  product_code 
)

Sets the product code of a CANopen device.

See also
co_dev_get_product_code()

Definition at line 411 of file dev.c.

◆ co_dev_get_revision()

co_unsigned32_t co_dev_get_revision ( const co_dev_t dev)

Returns the revision number of a CANopen device.

See also
co_dev_set_revision()

Definition at line 419 of file dev.c.

◆ co_dev_set_revision()

void co_dev_set_revision ( co_dev_t dev,
co_unsigned32_t  revision 
)

Sets the revision number of a CANopen device.

See also
co_dev_get_revision()

Definition at line 427 of file dev.c.

◆ co_dev_get_order_code()

const char* co_dev_get_order_code ( const co_dev_t dev)

Returns a pointer to the order code of a CANopen device.

See also
co_dev_set_order_code()

Definition at line 435 of file dev.c.

◆ co_dev_set_order_code()

int co_dev_set_order_code ( co_dev_t dev,
const char *  order_code 
)

Sets the order code of a CANopen device.

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

Definition at line 443 of file dev.c.

◆ co_dev_get_baud()

unsigned int co_dev_get_baud ( const co_dev_t dev)

Returns the supported bit rates of a CANopen device (any combination of CO_BAUD_1000, CO_BAUD_800, CO_BAUD_500, CO_BAUD_250, CO_BAUD_125, CO_BAUD_50, CO_BAUD_20, CO_BAUD_10 and CO_BAUD_AUTO).

See also
co_dev_set_baud()

Definition at line 465 of file dev.c.

◆ co_dev_set_baud()

void co_dev_set_baud ( co_dev_t dev,
unsigned int  baud 
)

Sets the supported bit rates of a CANopen device.

Parameters
deva pointer to a CANopen device.
baudthe supported bit rates (any combination of CO_BAUD_1000, CO_BAUD_800, CO_BAUD_500, CO_BAUD_250, CO_BAUD_125, CO_BAUD_50, CO_BAUD_20, CO_BAUD_10 and CO_BAUD_AUTO).
See also
co_dev_get_baud()

Definition at line 473 of file dev.c.

◆ co_dev_get_rate()

co_unsigned16_t co_dev_get_rate ( const co_dev_t dev)

Returns the (pending) baudrate of a CANopen device (in kbit/s).

See also
co_dev_set_rate()

Definition at line 481 of file dev.c.

◆ co_dev_set_rate()

void co_dev_set_rate ( co_dev_t dev,
co_unsigned16_t  rate 
)

Sets the (pending) baudrate of a CANopen device.

Parameters
deva pointer to a CANopen device.
ratethe baudrate (in kbit/s).
See also
co_dev_get_rate()

Definition at line 489 of file dev.c.

◆ co_dev_get_lss()

int co_dev_get_lss ( const co_dev_t dev)

Returns 1 if LSS is supported and 0 if not.

See also
co_dev_set_lss()

Definition at line 497 of file dev.c.

◆ co_dev_set_lss()

void co_dev_set_lss ( co_dev_t dev,
int  lss 
)

Sets the LSS support flag.

See also
co_dev_get_lss()

Definition at line 505 of file dev.c.

◆ co_dev_get_dummy()

co_unsigned32_t co_dev_get_dummy ( const co_dev_t dev)

Returns the data types supported by a CANopen device for mapping dummy entries in PDOs (one bit for each of the basic types).

See also
co_dev_set_dummy()

Definition at line 513 of file dev.c.

◆ co_dev_set_dummy()

void co_dev_set_dummy ( co_dev_t dev,
co_unsigned32_t  dummy 
)

Sets the data types supported by a CANopen device for mapping dummy entries in PDOs.

Parameters
deva pointer to a CANopen device.
dummythe data types supported for mapping dummy entries in PDOs (one bit for each of the basic types).
See also
co_dev_get_dummy()

Definition at line 521 of file dev.c.

◆ co_dev_get_val()

const void* co_dev_get_val ( const co_dev_t dev,
co_unsigned16_t  idx,
co_unsigned8_t  subidx 
)

Returns a pointer to the current value of a CANopen sub-object.

In the case of strings or domains, this is the address of a pointer to the first byte in the array.

See also
co_dev_set_val(), co_sub_get_val()

Definition at line 529 of file dev.c.

◆ co_dev_set_val()

size_t co_dev_set_val ( co_dev_t dev,
co_unsigned16_t  idx,
co_unsigned8_t  subidx,
const void *  ptr,
size_t  n 
)

Sets the current value of a CANopen sub-object.

Parameters
deva pointer to a CANopen device.
idxthe object index.
subidxthe object sub-index.
ptra pointer to the bytes to be copied. In case of strings or domains, ptr MUST point to the first byte in the array.
nthe number of bytes at ptr. In case of strings, n SHOULD exclude the terminating null byte(s).
Returns
the number of bytes copied (i.e., n), or 0 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_get_val(), co_sub_set_val()

Definition at line 539 of file dev.c.

◆ co_dev_read_sub()

size_t co_dev_read_sub ( co_dev_t dev,
co_unsigned16_t *  pidx,
co_unsigned8_t *  psubidx,
const uint8_t *  begin,
const uint8_t *  end 
)

Reads a value from a memory buffer, in the concise DCF format, and stores it in a sub-object in the object dictionary of a CANopen device.

If the sub-object does not exist, the value is discarded.

Parameters
deva pointer to a CANopen device.
pidxthe address at which to store the object index (can be NULL).
psubidxthe address at which to store the object sub-index (can be NULL).
begina pointer to the start of the buffer.
enda pointer to the end of the buffer.
Returns
the number of bytes read on success (at least 7), or 0 on error.
See also
co_dev_write_sub()

Definition at line 580 of file dev.c.

◆ co_dev_write_sub()

size_t co_dev_write_sub ( const co_dev_t dev,
co_unsigned16_t  idx,
co_unsigned8_t  subidx,
uint8_t *  begin,
uint8_t *  end 
)

Loads the value of a sub-object from the object dictionary of a CANopen device, and writes it to a memory buffer, in the concise DCF format.

Parameters
deva pointer to a CANopen device.
idxthe object index.
subidxthe object sub-index.
begina pointer to the start of the buffer. If begin is NULL, nothing is written.
enda pointer to the end of the buffer. If end is not NULL, and the buffer is too small (i.e., end - begin is less than the return value), nothing is written.
Returns
the number of bytes that would have been written had the buffer been sufficiently large, or 0 on error.
See also
co_dev_read_sub()

Definition at line 638 of file dev.c.

◆ co_dev_read_dcf()

int co_dev_read_dcf ( co_dev_t dev,
co_unsigned16_t *  pmin,
co_unsigned16_t *  pmax,
void *const *  ptr 
)

Reads the values of a range of objects from a memory buffer, in the concise DCF format, and stores them in the object dictionary of a CANopen device.

If an object does not exist, the value is discarded.

Parameters
deva pointer to a CANopen device.
pminthe address at which to store the minimum object index (can be NULL).
pmaxthe address at which to store the maximum object index (can be NULL).
ptrthe address of a pointer to a DOMAIN value.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_write_dcf()

Definition at line 682 of file dev.c.

◆ co_dev_read_dcf_file()

int co_dev_read_dcf_file ( co_dev_t dev,
co_unsigned16_t *  pmin,
co_unsigned16_t *  pmax,
const char *  filename 
)

Reads the values of a range of objects from a file, in the concise DCF format, and stores them in the object dictionary of a CANopen device.

If an object does not exist, the value is discarded.

Parameters
deva pointer to a CANopen device.
pminthe address at which to store the minimum object index (can be NULL).
pmaxthe address at which to store the maximum object index (can be NULL).
filenamea pointer to the name of the file.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_read_dcf()

Definition at line 725 of file dev.c.

◆ co_dev_write_dcf()

int co_dev_write_dcf ( const co_dev_t dev,
co_unsigned16_t  min,
co_unsigned16_t  max,
void **  ptr 
)

Loads the values of a range of objects in the object dictionary of a CANopen device, and writes them to a memory buffer, in the concise DCF format.

Parameters
deva pointer to a CANopen device.
minthe minimum object index.
maxthe maximum object index.
ptrthe address of a pointer. On success, *ptr points to a DOMAIN value.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_read_dcf()

Definition at line 776 of file dev.c.

◆ co_dev_write_dcf_file()

int co_dev_write_dcf_file ( const co_dev_t dev,
co_unsigned16_t  min,
co_unsigned16_t  max,
const char *  filename 
)

Loads the values of a range of objects in the object dictionary of a CANopen device, and writes them to a file, in the concise DCF format.

Parameters
deva pointer to a CANopen device.
minthe minimum object index.
maxthe maximum object index.
filenamea pointer to the name of the file.
Returns
0 on success, or -1 on error. In the latter case, the error number can be obtained with get_errc().
See also
co_dev_write_dcf()

Definition at line 851 of file dev.c.