Skip to content

Commit

Permalink
Merge pull request #21 from ciot-platform/feat/ciot_decoder
Browse files Browse the repository at this point in the history
Feat/ciot decoder
  • Loading branch information
wjsan authored May 19, 2024
2 parents ea5037f + 94e1d4e commit f82b89c
Show file tree
Hide file tree
Showing 191 changed files with 30,765 additions and 168 deletions.
2 changes: 1 addition & 1 deletion CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ cmake_minimum_required(VERSION 3.5)

add_subdirectory(examples/iot_device)
add_subdirectory(examples/nrf_dfu_master)
# add_subdirectory(tests)
add_subdirectory(tests)
6 changes: 5 additions & 1 deletion common/Common.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
set(CIOT_DIR ../ciot_c)
set(CIOT_DIR_RELATIVE ../../)

get_filename_component(CIOT_DIR "${CMAKE_CURRENT_LIST_FILE}/${CIOT_DIR_RELATIVE}" ABSOLUTE)

message(${CIOT_DIR})

set(CIOT_INCLUDE_DIRS
${CIOT_DIR}/include/types
Expand Down
124 changes: 124 additions & 0 deletions include/ciot_decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @file ciot_decoder.h
* @author Wesley Santos (wesleypro37@gmail.com)
* @brief Header file for CIOT (Custom IoT) Decoder library.
* @version 0.1
* @date 2024-04-27
*
* @copyright Copyright (c) 2024
*
*/

#ifndef __CIOT_DECODER__H__
#define __CIOT_DECODER__H__

#include <inttypes.h>

#include "ciot_err.h"

/**
* @brief Pointer to a CIOT decoder object.
*/
typedef struct ciot_decoder *ciot_decoder_t;

/**
* @brief Function pointer type for decoding data.
*
* @param base The base CIOT decoder object.
* @param byte The byte to decode.
* @return ciot_err_t The decoding error code.
*/
typedef ciot_err_t (ciot_decoder_decode_fn)(ciot_decoder_t base, uint8_t byte);

/**
* @brief Function pointer type for encoding data.
*
* @param base The base CIOT decoder object.
* @param data The data to encode.
* @param size The size of the data to encode.
* @return ciot_err_t The encoding error code.
*/
typedef ciot_err_t (ciot_decoder_encode_fn)(ciot_decoder_t base, uint8_t *data, int size);

/**
* @brief Enumeration for CIOT decoder types.
*/
typedef enum ciot_decoder_type
{
CIOT_DECODER_TYPE_NONE, /**< No specific decoder type. */
CIOT_DECODER_TYPE_SLIP, /**< SLIP decoder type. */
CIOT_DECODER_TYPE_S, /**< S decoder type. */
CIOT_DECODER_TYPE_CUSTOM /**< Custom decoder type. */
} ciot_decoder_type_t;

/**
* @brief Enumeration for CIOT decoder states.
*/
typedef enum ciot_decoder_state
{
CIOT_DECODER_STATE_ERROR = -1, /**< Error state. */
CIOT_DECODER_STATE_IDLE, /**< Idle state. */
CIOT_DECODER_STATE_DECODING, /**< Decoding state. */
CIOT_DECODER_STATE_DONE /**< Decoding done state. */
} ciot_decoder_state_t;

/**
* @brief Structure for CIOT decoder buffer.
*/
typedef struct ciot_decoder_buffer
{
uint8_t *buf; /**< Buffer pointer. */
int size; /**< Buffer size. */
} ciot_decoder_buffer_t;

/**
* @brief Structure for CIOT decoder configuration.
*/
typedef struct ciot_decoder_cfg
{
ciot_decoder_buffer_t decoder; /**< Decoder buffer. */
ciot_decoder_buffer_t encoder; /**< Encoder buffer. */
} ciot_decoder_cfg_t;

/**
* @brief Main CIOT decoder structure.
*/
struct ciot_decoder
{
ciot_decoder_type_t type; /**< Decoder type. */
ciot_decoder_state_t state; /**< Decoder state. */
ciot_decoder_buffer_t decoder; /**< Decoder buffer. */
ciot_decoder_buffer_t encoder; /**< Encoder buffer. */
ciot_decoder_decode_fn *decode; /**< Decode function pointer. */
ciot_decoder_encode_fn *encode; /**< Encode function pointer. */
void *self; /**< Pointer to self. */
};

/**
* @brief Creates a new CIOT decoder object based on the provided configuration.
*
* @param cfg Pointer to the configuration for the decoder.
* @return ciot_decoder_t The newly created CIOT decoder object.
*/
ciot_decoder_t ciot_decoder_new(ciot_decoder_cfg_t *cfg);

/**
* @brief Decodes a byte of data using the specified CIOT decoder object.
*
* @param base The base CIOT decoder object.
* @param byte The byte to decode.
* @return ciot_err_t The decoding error code.
*/
ciot_err_t ciot_decoder_decode(ciot_decoder_t base, uint8_t byte);

/**
* @brief Encodes data using the specified CIOT decoder object.
*
* @param base The base CIOT decoder object.
* @param data The data to encode.
* @param size The size of the data to encode.
* @return ciot_err_t The encoding error code.
*/
ciot_err_t ciot_decoder_encode(ciot_decoder_t base, uint8_t *data, int size);

#endif //!__CIOT_DECODER__H__
44 changes: 44 additions & 0 deletions include/ciot_decoder_s.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @file ciot_decoder_s.h
* @author Wesley Santos (wesleypro37@gmail.com)
* @brief Header file for the S-type CIOT (Custom IoT) Decoder library.
* @version 0.1
* @date 2024-04-27
*
* @copyright Copyright (c) 2024
*
*/

#ifndef __CIOT_DECODER_S__H__
#define __CIOT_DECODER_S__H__

#include "ciot_decoder.h"

/**
* @brief Creates a new S-type CIOT decoder object based on the provided configuration.
*
* @param cfg Pointer to the configuration for the S-type decoder.
* @return ciot_decoder_t The newly created S-type CIOT decoder object.
*/
ciot_decoder_t ciot_decoder_s_new(ciot_decoder_cfg_t *cfg);

/**
* @brief Decodes a byte of data using the specified S-type CIOT decoder object.
*
* @param base The base S-type CIOT decoder object.
* @param byte The byte to decode.
* @return ciot_err_t The decoding error code.
*/
ciot_err_t ciot_decoder_s_decode(ciot_decoder_t base, uint8_t byte);

/**
* @brief Encodes data using the specified S-type CIOT decoder object.
*
* @param base The base S-type CIOT decoder object.
* @param data The data to encode.
* @param size The size of the data to encode.
* @return ciot_err_t The encoding error code.
*/
ciot_err_t ciot_decoder_s_encode(ciot_decoder_t base, uint8_t *data, int size);

#endif //!__CIOT_DECODER_S__H__
44 changes: 44 additions & 0 deletions include/ciot_decoder_slip.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @file ciot_decoder_slip.h
* @author Wesley Santos (wesleypro37@gmail.com)
* @brief Header file for the SLIP (Serial Line Internet Protocol) CIOT Decoder library.
* @version 0.1
* @date 2024-04-27
*
* @copyright Copyright (c) 2024
*
*/

#ifndef __CIOT_DECODER_SLIP__H__
#define __CIOT_DECODER_SLIP__H__

#include "ciot_decoder.h"

/**
* @brief Creates a new SLIP (Serial Line Internet Protocol) CIOT decoder object based on the provided configuration.
*
* @param cfg Pointer to the configuration for the SLIP decoder.
* @return ciot_decoder_t The newly created SLIP CIOT decoder object.
*/
ciot_decoder_t ciot_decoder_slip_new(ciot_decoder_cfg_t *cfg);

/**
* @brief Decodes a byte of data using the specified SLIP CIOT decoder object.
*
* @param base The base SLIP CIOT decoder object.
* @param byte The byte to decode.
* @return ciot_err_t The decoding error code.
*/
ciot_err_t ciot_decoder_slip_decode(ciot_decoder_t base, uint8_t byte);

/**
* @brief Encodes data using the specified SLIP CIOT decoder object.
*
* @param base The base SLIP CIOT decoder object.
* @param data The data to encode.
* @param size The size of the data to encode.
* @return ciot_err_t The encoding error code.
*/
ciot_err_t ciot_decoder_slip_encode(ciot_decoder_t base, uint8_t *data, int size);

#endif //!__CIOT_DECODER_SLIP__H__
92 changes: 52 additions & 40 deletions include/ciot_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#define __CIOT_ERR__H__

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif

#include "ciot_log.h"
Expand All @@ -36,6 +37,15 @@ extern "C" {
if (x == NULL) \
return CIOT_ERR_NULL_ARG

/**
* @brief Macro to evaluate if an interface pointer type is correct
* @param iface The pointer of iface to check.
* @param enum_type Enum member used to check if iface type is correct
*/
#define CIOT_IFACEP_TYPE_CHECK(iface, enum_type) \
if (iface->type != enum_type) \
return CIOT_ERR_INVALID_TYPE

/**
* @brief Macro to print an error message if an operation fails.
* @param x The operation to check for errors.
Expand Down Expand Up @@ -65,46 +75,48 @@ extern "C" {
} \
} while (0)

/**
* @brief Enumeration of CIOT error codes.
*/
typedef enum {
CIOT_FAIL = -1, /**< General failure. */
CIOT_OK, /**< Operation successful. */
CIOT_ERR_NULL_ARG = CIOT_ERR_BASE + 1, /**< Null argument error. */
CIOT_ERR_INVALID_ID, /**< Invalid ID error. */
CIOT_ERR_INVALID_TYPE, /**< Invalid type error. */
CIOT_ERR_OVERFLOW, /**< Overflow error. */
CIOT_ERR_NOT_IMPLEMENTED, /**< Not implemented error. */
CIOT_ERR_NOT_SUPPORTED, /**< Operation not supported error. */
CIOT_ERR_BUSY, /**< Busy error. */
CIOT_ERR_INVALID_STATE, /**< Invalid state error. */
CIOT_ERR_SERIALIZATION, /**< Serialization error. */
CIOT_ERR_DESERIALIZATION, /**< Deserialization error. */
CIOT_ERR_SEND_DATA, /**< Error sending data. */
CIOT_ERR_RECV_DATA, /**< Error receiving data. */
CIOT_ERR_INVALID_SIZE, /**< Invalid size error. */
CIOT_ERR_CLOSED, /**< Closed error. */
CIOT_ERR_NOT_FOUND, /**< Not found error. */
CIOT_ERR_VALIDATION_FAILED, /**< Validation failed error. */
CIOT_ERR_CONNECTION, /**< Connection error. */
CIOT_ERR_DISCONNECTION, /**< Disconnection error. */
CIOT_ERR_EXCEPTION, /**< Exception error. */
CIOT_ERR_TERMINATOR_MISSING, /**< Terminator missing error. */
CIOT_ERR_INVALID_ARG, /**< Invalid argument error. */
CIOT_ERR_NO_MEMORY, /**< Out of memory error. */
CIOT_ERR_TIMEOUT, /**< Timeout error. */
CIOT_ERR_MONGOOSE, /**< Mongoose error. */
CIOT_ERR_IMPOSSIBLE_OP, /**< Impossible operation error. */
CIOT_ERR_CHECKSUM, /**< Checksum error. */
} ciot_err_t;
/**
* @brief Enumeration of CIOT error codes.
*/
typedef enum
{
CIOT_FAIL = -1, /**< General failure. */
CIOT_OK, /**< Operation successful. */
CIOT_ERR_NULL_ARG = CIOT_ERR_BASE + 1, /**< Null argument error. */
CIOT_ERR_INVALID_ID, /**< Invalid ID error. */
CIOT_ERR_INVALID_TYPE, /**< Invalid type error. */
CIOT_ERR_OVERFLOW, /**< Overflow error. */
CIOT_ERR_NOT_IMPLEMENTED, /**< Not implemented error. */
CIOT_ERR_NOT_SUPPORTED, /**< Operation not supported error. */
CIOT_ERR_BUSY, /**< Busy error. */
CIOT_ERR_INVALID_STATE, /**< Invalid state error. */
CIOT_ERR_SERIALIZATION, /**< Serialization error. */
CIOT_ERR_DESERIALIZATION, /**< Deserialization error. */
CIOT_ERR_SEND_DATA, /**< Error sending data. */
CIOT_ERR_RECV_DATA, /**< Error receiving data. */
CIOT_ERR_INVALID_SIZE, /**< Invalid size error. */
CIOT_ERR_CLOSED, /**< Closed error. */
CIOT_ERR_NOT_FOUND, /**< Not found error. */
CIOT_ERR_VALIDATION_FAILED, /**< Validation failed error. */
CIOT_ERR_CONNECTION, /**< Connection error. */
CIOT_ERR_DISCONNECTION, /**< Disconnection error. */
CIOT_ERR_EXCEPTION, /**< Exception error. */
CIOT_ERR_TERMINATOR_MISSING, /**< Terminator missing error. */
CIOT_ERR_INVALID_ARG, /**< Invalid argument error. */
CIOT_ERR_NO_MEMORY, /**< Out of memory error. */
CIOT_ERR_TIMEOUT, /**< Timeout error. */
CIOT_ERR_MONGOOSE, /**< Mongoose error. */
CIOT_ERR_IMPOSSIBLE_OP, /**< Impossible operation error. */
CIOT_ERR_CHECKSUM, /**< Checksum error. */
CIOT_ERR_PROTOCOL_VIOLATION, /**< Protocol violation error. */
} ciot_err_t;

/**
* @brief Converts a CIOT error code to a human-readable message.
* @param err The error code.
* @return A pointer to the error message.
*/
const char *ciot_err_to_message(ciot_err_t err);
/**
* @brief Converts a CIOT error code to a human-readable message.
* @param err The error code.
* @return A pointer to the error message.
*/
const char *ciot_err_to_message(ciot_err_t err);

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions include/mainpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
- nRF5 SDK 17 for Nordic devices
@endverbatim
* <hr>
* @todo Create readme.md
* @todo Create an common decoding/encoding abstraction
* @todo Implement ciot_s and slip encoding/decoding modules using the ciot_decoding abstraction
* @todo Integrate the modular decoding/encoding tool to serial interfaces like UART/USB/Bluetooth
* @todo Add support to STM32 devices
* @todo Implement ciot_mbus interface
* @todo Implement ciot_opcuas interface
* @todo Implement ciot_opcuac interface
Expand Down
38 changes: 38 additions & 0 deletions src/common/ciot_decoder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file ciot_decoder.c
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2024-04-27
*
* @copyright Copyright (c) 2024
*
*/

#include <stdlib.h>

#include "ciot_decoder.h"

ciot_decoder_t ciot_decoder_new(ciot_decoder_cfg_t *cfg)
{
ciot_decoder_t base = calloc(1, sizeof(struct ciot_decoder));
base->decoder = cfg->decoder;
base->encoder = cfg->encoder;
return base;
}

ciot_err_t ciot_decoder_decode(ciot_decoder_t base, uint8_t byte)
{
CIOT_NULL_CHECK(base);
CIOT_NULL_CHECK(base->decode);
CIOT_NULL_CHECK(base->decoder.buf);
return base->decode(base, byte);
}

ciot_err_t ciot_decoder_encode(ciot_decoder_t base, uint8_t *data, int size)
{
CIOT_NULL_CHECK(base);
CIOT_NULL_CHECK(base->encode);
CIOT_NULL_CHECK(base->encoder.buf);
return base->encode(base, data, size);
}
Loading

0 comments on commit f82b89c

Please sign in to comment.