Skip to content

Commit f3a3a69

Browse files
authored
Merge pull request #35 from nattgris/store
Pass the read/write intent to the OD store open call
2 parents d052af3 + e2494e3 commit f3a3a69

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

include/co_api.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ typedef enum co_store
278278
CO_STORE_LAST,
279279
} co_store_t;
280280

281+
/** Dictionary store open modes */
282+
typedef enum co_mode
283+
{
284+
CO_MODE_READ, /**< Open for reading */
285+
CO_MODE_WRITE, /**< Open for writing */
286+
} co_mode_t;
287+
281288
/** CANopen stack configuration */
282289
typedef struct co_cfg
283290
{
@@ -308,7 +315,7 @@ typedef struct co_cfg
308315
void (*cb_notify) (void * arg, uint16_t index, uint8_t subindex);
309316

310317
/** Function to open dictionary store */
311-
void * (*open) (co_store_t store);
318+
void * (*open) (co_store_t store, co_mode_t mode);
312319

313320
/** Function to read from dictionary store */
314321
int (*read) (void * arg, void * data, size_t size);

src/co_lss.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static void co_lss_store_configuration (co_net_t * net, uint8_t * _msg)
266266
goto error1;
267267
}
268268

269-
arg = net->open (CO_STORE_LSS);
269+
arg = net->open (CO_STORE_LSS, CO_MODE_WRITE);
270270
if (arg == NULL)
271271
goto error1;
272272

@@ -489,7 +489,7 @@ uint8_t co_lss_get_persistent_node_id (co_net_t * net)
489489
if (net->open == NULL || net->read == NULL || net->close == NULL)
490490
goto error1;
491491

492-
arg = net->open (CO_STORE_LSS);
492+
arg = net->open (CO_STORE_LSS, CO_MODE_READ);
493493
if (arg == NULL)
494494
goto error1;
495495

@@ -521,7 +521,7 @@ int co_lss_get_persistent_bitrate (co_net_t * net)
521521
if (net->open == NULL || net->read == NULL || net->close == NULL)
522522
goto error1;
523523

524-
arg = net->open (CO_STORE_LSS);
524+
arg = net->open (CO_STORE_LSS, CO_MODE_READ);
525525
if (arg == NULL)
526526
goto error1;
527527

src/co_main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ struct co_net
278278
void (*cb_notify) (void * arg, uint16_t index, uint8_t subindex);
279279

280280
/** Function to open dictionary store */
281-
void * (*open) (co_store_t store);
281+
void * (*open) (co_store_t store, co_mode_t mode);
282282

283283
/** Function to read from dictionary store */
284284
int (*read) (void * arg, void * data, size_t size);

src/co_od.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ uint32_t co_od_load (co_net_t * net, co_store_t store)
359359
if (net->open == NULL || net->read == NULL || net->close == NULL)
360360
return CO_SDO_ABORT_GENERAL;
361361

362-
arg = net->open (store);
362+
arg = net->open (store, CO_MODE_READ);
363363
if (arg == NULL)
364364
return CO_SDO_ABORT_GENERAL;
365365

@@ -384,19 +384,19 @@ uint32_t co_od_load (co_net_t * net, co_store_t store)
384384
if (net->read (arg, &subindex, sizeof (subindex)) < 0)
385385
goto error;
386386

387-
if (net->read (arg, &size, sizeof (size)) < 0)
387+
if (net->read (arg, &size, sizeof (size)) < 0 || size == 0)
388388
goto error;
389389

390390
/* Attempt to set value. Errors are ignored to support firmware
391391
update of dictionary */
392392

393393
obj = co_obj_find (net, index);
394394
if (obj == NULL)
395-
continue;
395+
goto skip;
396396

397397
entry = co_entry_find (net, obj, subindex);
398-
if (entry == NULL || !(entry->flags & OD_WRITE))
399-
continue;
398+
if (entry == NULL || !(entry->flags & OD_WRITE) || (entry->flags & OD_TRANSIENT))
399+
goto skip; /* Not storable in this OD */
400400

401401
if (size <= sizeof (value))
402402
{
@@ -418,16 +418,20 @@ uint32_t co_od_load (co_net_t * net, co_store_t store)
418418
else
419419
{
420420
/* Stored size does not match object size. Discard data. */
421-
while (size > sizeof(value))
422-
{
423-
if (net->read (arg, &value, sizeof (value)) < 0)
424-
goto error;
425-
size -= sizeof(value);
426-
}
421+
goto skip;
422+
}
427423

428-
if (net->read (arg, &value, size) < 0)
424+
continue;
425+
skip:
426+
while (size > sizeof (value))
427+
{
428+
if (net->read (arg, &value, sizeof (value)) < 0)
429429
goto error;
430+
size -= sizeof (value);
430431
}
432+
433+
if (net->read (arg, &value, size) < 0)
434+
goto error;
431435
}
432436

433437
/* Ignore any error on close */
@@ -457,7 +461,7 @@ uint32_t co_od_store (co_net_t * net, co_store_t store, uint16_t min, uint16_t m
457461
if (net->open == NULL || net->write == NULL || net->close == NULL)
458462
return CO_SDO_ABORT_HW_ERROR;
459463

460-
arg = net->open (store);
464+
arg = net->open (store, CO_MODE_WRITE);
461465
if (arg == NULL)
462466
return CO_SDO_ABORT_HW_ERROR;
463467

@@ -555,7 +559,7 @@ uint32_t co_od_restore (co_net_t * net, co_store_t store)
555559
if (net->open == NULL || net->write == NULL || net->close == NULL)
556560
return CO_SDO_ABORT_HW_ERROR;
557561

558-
arg = net->open (store);
562+
arg = net->open (store, CO_MODE_WRITE);
559563
if (arg == NULL)
560564
return CO_SDO_ABORT_HW_ERROR;
561565

test/mocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void store_init (void)
187187
}
188188

189189
unsigned int store_open_calls;
190-
void * store_open (co_store_t store)
190+
void * store_open (co_store_t store, co_mode_t mode)
191191
{
192192
store_open_calls++;
193193
_fd.p = the_store;

test/mocks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void cb_notify (void * arg, uint16_t index, uint8_t subindex);
108108

109109
void store_init (void);
110110
extern unsigned int store_open_calls;
111-
void * store_open (co_store_t store);
111+
void * store_open (co_store_t store, co_mode_t mode);
112112
int store_read (void * arg, void * data, size_t size);
113113
int store_write (void * arg, const void * data, size_t size);
114114
int store_close (void * arg);

util/slave/slave.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static struct fd
158158
uint8_t * p;
159159
} fd;
160160

161-
static void * store_open (co_store_t store)
161+
static void * store_open (co_store_t store, co_mode_t mode)
162162
{
163163
if (store >= CO_STORE_LAST)
164164
return NULL;

0 commit comments

Comments
 (0)