Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock platform #949

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions api/mraa/initio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace mraa
class MraaIo
{
private:
mraa_io_descriptor* descs;
mraa_io_descriptor* descs = nullptr;

public:
MraaIo(const std::string& initStr) : descs()
Expand Down Expand Up @@ -109,39 +109,41 @@ class MraaIo

~MraaIo()
{
if (descs->leftover_str) {
free(descs->leftover_str);
}

if (descs->n_aio) {
free(descs->aios);
}
if (descs->n_gpio) {
free(descs->gpios);
}
if (descs->n_i2c) {
free(descs->i2cs);
}
if(descs != nullptr) {
if (descs->leftover_str) {
free(descs->leftover_str);
}

if (descs->n_aio) {
free(descs->aios);
}
if (descs->n_gpio) {
free(descs->gpios);
}
if (descs->n_i2c) {
free(descs->i2cs);
}
#if !defined(PERIPHERALMAN)
if (descs->n_iio) {
free(descs->iios);
}
if (descs->n_iio) {
free(descs->iios);
}
#endif
if (descs->n_pwm) {
free(descs->pwms);
}
if (descs->n_spi) {
free(descs->spis);
}
if (descs->n_uart) {
free(descs->uarts);
if (descs->n_pwm) {
free(descs->pwms);
}
if (descs->n_spi) {
free(descs->spis);
}
if (descs->n_uart) {
free(descs->uarts);
}
if (descs->n_uart_ow) {
free(descs->uart_ows);
}

/* Finally free the mraa_io_descriptor structure. */
free(descs);
}
if (descs->n_uart_ow) {
free(descs->uart_ows);
}

/* Finally free the mraa_io_descriptor structure. */
free(descs);
}

public:
Expand Down
13 changes: 12 additions & 1 deletion include/mock/mock_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ extern "C" {

#include "mraa_internal.h"

#define MRAA_MOCK_PINCOUNT 10
#define MRAA_MOCK_GPIO_COUNT 1
#define MRAA_MOCK_AIO_COUNT 1
#define MRAA_MOCK_I2C_BUS_COUNT 1
#define MRAA_MOCK_SPI_BUS_COUNT 1
#define MRAA_MOCK_PWM_DEV_COUNT 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the mock platform description in docs accordingly, right now the PWM functionality and the extra pin are not mentioned.

#define MRAA_MOCK_UART_DEV_COUNT 1
#define MRAA_MOCK_PINCOUNT (MRAA_MOCK_GPIO_COUNT + MRAA_MOCK_AIO_COUNT + \
2 * MRAA_MOCK_I2C_BUS_COUNT + 4 * MRAA_MOCK_SPI_BUS_COUNT + \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: shis and line below need one more space to align on opening (

MRAA_MOCK_PWM_DEV_COUNT + 2 * MRAA_MOCK_UART_DEV_COUNT)

#define MRAA_PWM_OFFSET (MRAA_MOCK_GPIO_COUNT + MRAA_MOCK_AIO_COUNT + \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a base for MRAA_MOCK_PINCOUNT to avoid repeating the same piece two times.

2 * MRAA_MOCK_I2C_BUS_COUNT + 4 * MRAA_MOCK_SPI_BUS_COUNT)

mraa_board_t*
mraa_mock_board();
Expand Down
2 changes: 1 addition & 1 deletion include/mock/mock_board_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern "C" {
// Mock I2C device address
#define MOCK_I2C_DEV_ADDR 0x33
// Mock I2C device data registers block length in bytes. Our code assumes it's >= 1.
#define MOCK_I2C_DEV_DATA_LEN 10
#define MOCK_I2C_DEV_DATA_LEN 255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why this change?
  2. If this is really necessary, you have to update the mock platform description in docs as well.
  3. If this is really necessary, the respective test updates must be in the same commit, so that master is never broken.

// Initial value for each byte in the mock I2C device data registers
#define MOCK_I2C_DEV_DATA_INIT_BYTE 0xAB

Expand Down
50 changes: 50 additions & 0 deletions include/mock/mock_board_pwm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Author: Adelin Dobre <[email protected]>
* Copyright (c) 2019 Adelin Dobre.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include "mraa_internal.h"

mraa_result_t
mraa_mock_pwm_init_raw_replace(mraa_pwm_context dev, int pin);

mraa_result_t
mraa_mock_pwm_write_period_replace(mraa_pwm_context dev, int period);

mraa_result_t
mraa_mock_pwm_write_duty_replace(mraa_pwm_context dev, int duty);

int
mraa_mock_pwm_read_duty_replace(mraa_pwm_context dev);

mraa_result_t
mraa_mock_pwm_enable_replace(mraa_pwm_context dev, int enable);

#ifdef __cplusplus
}
#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ set (mraa_LIB_MOCK_SRCS_NOAUTO
${PROJECT_SOURCE_DIR}/src/mock/mock_board_gpio.c
${PROJECT_SOURCE_DIR}/src/mock/mock_board_aio.c
${PROJECT_SOURCE_DIR}/src/mock/mock_board_i2c.c
${PROJECT_SOURCE_DIR}/src/mock/mock_board_pwm.c
${PROJECT_SOURCE_DIR}/src/mock/mock_board_spi.c
${PROJECT_SOURCE_DIR}/src/mock/mock_board_uart.c
)
Expand Down
1 change: 0 additions & 1 deletion src/initio/initio.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ mraa_io_init(const char* strdesc, mraa_io_descriptor** desc)
strncat(new_desc->leftover_str, str_descs[i], strlen(str_descs[i]));
}

strncat(new_desc->leftover_str, str_descs[i], strlen(str_descs[i]));
leftover_str_len += strlen(str_descs[i]) + 1;
new_desc->leftover_str[leftover_str_len - 1] = ',';
new_desc->leftover_str[leftover_str_len] = '\0';
Expand Down
Loading