zephyr/subsys/net/lib/openthread/platform/entropy.c
Robert Lubos 83bb911a21 net: openthread: Update OpenThread version
Use the newest version of the OpenThread project, as updated in
https://github.com/zephyrproject-rtos/openthread/pull/2.

Introduce the following fixes along with the update (they're squashed to
retain bisectability of OT samples):

* Update configs and flags used
	Some OT configs were renamed, some new were introduced that Zephyr port
	needs to set.

* Add entropy platform driver
	OpenThreads `random` platform subsystem was replaced with `entropy`
	subsystem which is supposed to serve as an entropy source for the
	generic OpenThread's random generator.

* Halt OT thread when OT command is processed
	OpenThread can currently be processed from two threads - a
	genuine OpenThread thread and shell thread, which processes CLI
	commands. This could cause trouble, when context was switched
	during OT command processing (i.e. switched to process an incomming OT
	message, while still in unfinished command handler).

	In result, it was not possible to turn the commissioner role on via
	CLI, as the commissioner petition response was handled before the
	Commissioner::Start function finished its execution (if the
	petitioner is also the network leader, all messages are passed
	internally within the stack).

	Fix this by suspending the OT thread for the time of an OT command
	processing.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2019-10-28 13:00:56 +02:00

48 lines
1.0 KiB
C

/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <string.h>
#include <drivers/entropy.h>
#include <logging/log.h>
#include <openthread/platform/entropy.h>
#include "platform-zephyr.h"
LOG_MODULE_REGISTER(net_otPlat_entropy, CONFIG_OPENTHREAD_L2_LOG_LEVEL);
#if !defined(CONFIG_ENTROPY_HAS_DRIVER)
#error OpenThread requires an entropy source for a TRNG
#endif
otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength)
{
/* static to obtain it once in a first call */
static struct device *dev;
int err;
if ((aOutput == NULL) || (aOutputLength == 0)) {
return OT_ERROR_INVALID_ARGS;
}
if (dev == NULL) {
dev = device_get_binding(CONFIG_ENTROPY_NAME);
if (dev == NULL) {
LOG_ERR("Failed to obtain entropy device");
return OT_ERROR_FAILED;
}
}
err = entropy_get_entropy(dev, aOutput, aOutputLength);
if (err != 0) {
LOG_ERR("Failed to obtain entropy, err %d", err);
return OT_ERROR_FAILED;
}
return OT_ERROR_NONE;
}