Skip to content

Commit

Permalink
merged master
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPork committed Mar 25, 2016
2 parents 6df0fca + 2a4a225 commit 32258ec
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 25 deletions.
2 changes: 1 addition & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS += -g -Wall -Wextra
CFLAGS += -g -Wall -Wextra -I.

OBJECTS = \
main.o \
Expand Down
16 changes: 8 additions & 8 deletions library/sbmp_config.h → example/sbmp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

/* --- Configuration ------------------- */


#ifndef SBMP_LOGGING
/**
* @brief Enable logging.
*
Expand All @@ -13,10 +11,8 @@
* Disable logging to free up memory taken by the messages.
*/
#define SBMP_LOGGING 1
#endif


#ifndef SBMP_MALLOC
/**
* @brief Enable malloc if NULL is passed.
*
Expand All @@ -27,11 +23,9 @@
* If disabled, init funcs will return NULL if NULL is passed
* as argument.
*/
#define SBMP_MALLOC 1
#endif
#define SBMP_USE_MALLOC 1


#ifndef SBMP_HAS_CRC32
/**
* @brief Add support for CRC32
*
Expand All @@ -45,7 +39,13 @@
* supported here, and should start using XOR.
*/
#define SBMP_HAS_CRC32 1
#endif


/* ---------- MALLOC --------------- */

# define sbmp_malloc malloc
# define sbmp_free free
# define sbmp_calloc calloc

/* ------------------------------------- */

Expand Down
12 changes: 9 additions & 3 deletions library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ Configuration & porting

You can customize a lot of aspects of SBMP in `sbmp_config.h`.

If you included the library as a submodule, and want to avoid manual edits, you can set
the options using compiler flags (eg. to disable CRC32: `-DSBMP_HAS_CRC32=0`).
A template for this file is `sbmp_config.example.h`. Copy it to your project and make
sure it's in the include path.

You can also set some of the options using compiler flags (eg. to disable CRC32:
`-DSBMP_HAS_CRC32=0`).

**What to change for Arduino**

Basically disable all you can in the config file.

- You don't want `malloc()`
- Logging is useless if there's only one USART anyway
- Logging is useless if there's only one USART anyway - disable logging
- CRC32 can be replaced with XOR if you don't care about reliability that much.

This gains you a huge size reduction, and the whole library will take only around
2.5 kB flash and 150 B ram (of course, not including your Rx buffer).

It works really well for ATmega328P - the usual Arduino chip.

If you *need* CRC32 on AVR, you can optimize the CRC32 routines to use progmem - it's
not there by default for better portability.

Example for AVR
---------------

Expand Down
8 changes: 4 additions & 4 deletions library/sbmp_bulk.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@


/** Offer a bulk data transfer. */
bool FLASH_FN sbmp_bulk_offer(SBMP_Endpoint *ep, uint32_t bulk_length, const uint8_t *user_data, uint16_t user_data_len, uint16_t sesn)
bool FLASH_FN sbmp_bulk_offer(SBMP_Endpoint *ep, uint32_t bulk_length, const uint8_t *xtra_data, uint16_t xtra_data_len, uint16_t sesn)
{
return sbmp_ep_start_response(ep, DG_BULK_OFFER, user_data_len + 4, sesn) // using response because it allows to use a custom sesn
return sbmp_ep_start_response(ep, DG_BULK_OFFER, xtra_data_len + sizeof(uint32_t), sesn) // using response because it allows to use a custom sesn
&& sbmp_ep_send_u32(ep, bulk_length)
&& sbmp_ep_send_buffer(ep, user_data, user_data_len, NULL);
&& sbmp_ep_send_buffer(ep, xtra_data, xtra_data_len, NULL);
}

/** Request a chunk of the bulk data. */
bool FLASH_FN sbmp_bulk_request(SBMP_Endpoint *ep, uint32_t offset, uint16_t chunk_size, uint16_t sesn)
{
return sbmp_ep_start_response(ep, DG_BULK_OFFER, 4 + 2, sesn)
return sbmp_ep_start_response(ep, DG_BULK_OFFER, sizeof(uint32_t) + sizeof(uint16_t), sesn)
&& sbmp_ep_send_u32(ep, offset)
&& sbmp_ep_send_u16(ep, chunk_size);
}
Expand Down
68 changes: 68 additions & 0 deletions library/sbmp_config.example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef SBMP_CONFIG_H
#define SBMP_CONFIG_H

/* --- Configuration ------------------- */


#ifndef SBMP_LOGGING
/**
* @brief Enable logging.
*
* Logging functions are WEAK stubs in sbmp_logging.
*
* Disable logging to free up memory taken by the messages.
*/
#define SBMP_LOGGING 1
#endif


#ifndef SBMP_USE_MALLOC
/**
* @brief Enable malloc if NULL is passed.
*
* This lets you malloc() the struct / buffer if you pass NULL
* to the init functions.
*
* Disable malloc to free up memory taken by the malloc routine.
* If disabled, init funcs will return NULL if NULL is passed
* as argument.
*/
#define SBMP_USE_MALLOC 1
#endif


#ifndef SBMP_HAS_CRC32
/**
* @brief Add support for CRC32
*
* Disabling CRC32 will reduce program size (for small micros).
* If CRC32 is disabled, XOR will be used as the preferred checksum
* method.
*
* Received CRC32'd messages will be accepted without checking.
*
* If handshake is used, the peer will detect that CRC32 is not
* supported here, and should start using XOR.
*/
#define SBMP_HAS_CRC32 1
#endif


/* ---------- MALLOC --------------- */

#ifndef sbmp_malloc
# define sbmp_malloc malloc
#endif

#ifndef sbmp_free
# define sbmp_free free
#endif

#ifndef sbmp_calloc
# define sbmp_calloc calloc
#endif

/* ------------------------------------- */


#endif // SBMP_CONFIG_H
4 changes: 2 additions & 2 deletions library/sbmp_datagram.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ SBMP_Datagram FLASH_FN *sbmp_dg_parse(SBMP_Datagram *dg, const uint8_t *payload,

// S.N. (2 B) | Dg Type (1 B) | Payload

#if SBMP_MALLOC
#if SBMP_USE_MALLOC
if (dg == NULL) {
// request to allocate
dg = malloc(sizeof(SBMP_Datagram));
dg = sbmp_malloc(sizeof(SBMP_Datagram));
}
#else
if (dg == NULL) {
Expand Down
6 changes: 3 additions & 3 deletions library/sbmp_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ SBMP_FrmInst FLASH_FN *sbmp_frm_init(
{
bool frm_mallocd = false;

#if SBMP_MALLOC
#if SBMP_USE_MALLOC

if (frm == NULL) {
// caller wants us to allocate it
frm = malloc(sizeof(SBMP_FrmInst));
frm = sbmp_malloc(sizeof(SBMP_FrmInst));
if (frm == NULL) return NULL; // malloc failed
frm_mallocd = true;
}

if (buffer == NULL) {
// caller wants us to allocate it
buffer = malloc(buffer_size);
buffer = sbmp_malloc(buffer_size);
if (buffer == NULL) { // malloc failed
if (frm_mallocd) free(frm);
return NULL;
Expand Down
9 changes: 5 additions & 4 deletions library/sbmp_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ SBMP_Endpoint FLASH_FN *sbmp_ep_init(

if (ep == NULL) {
// request to allocate it
#if SBMP_MALLOC
ep = malloc(sizeof(SBMP_Endpoint));
#if SBMP_USE_MALLOC
ep = sbmp_malloc(sizeof(SBMP_Endpoint));
if (!ep) return NULL; // malloc failed
ep_mallocd = true;
#else
Expand Down Expand Up @@ -105,8 +105,9 @@ bool FLASH_FN sbmp_ep_init_listeners(SBMP_Endpoint *ep, SBMP_SessionListenerSlot
// NULL is allowed only if count is 0
if (listener_slots == NULL && slot_count > 0) {
// request to allocate it
#if SBMP_MALLOC
listener_slots = malloc(slot_count * sizeof(SBMP_SessionListenerSlot));
#if SBMP_USE_MALLOC
listener_slots = sbmp_malloc(slot_count * sizeof(SBMP_SessionListenerSlot));

if (!listener_slots) { // malloc failed
return false;
}
Expand Down

0 comments on commit 32258ec

Please sign in to comment.