Skip to content

Commit

Permalink
Merge pull request #447 from tyler-potyondy/openthread-dev
Browse files Browse the repository at this point in the history
OpenThread Fixes and Updates
  • Loading branch information
alevy committed Jun 28, 2024
2 parents 6ecb786 + 39008bb commit a527d4b
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 82 deletions.
3 changes: 2 additions & 1 deletion examples/openthread/openthread_hello/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand Down Expand Up @@ -56,7 +57,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

if (!otTaskletsArePending(instance)) {
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/openthread/openthread_udp_rx/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand Down Expand Up @@ -70,7 +71,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

if (!otTaskletsArePending(instance)) {
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/openthread/openthread_udp_tx/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand Down Expand Up @@ -81,7 +82,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

if (!otTaskletsArePending(instance)) {
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void read_temperature_timer_callback(__attribute__ ((unused)) uint32_t no
// __attribute__ ((unused)) void* opaque) {
// openthread_buffer[0] = local_temperature_setpoint;
// ipc_notify_service(openthread_svc_num);
// libtock_alarm_in_ms(500, update_network_timer_callback, NULL, &network_timer);
// libtock_alarm_in_ms(250, update_network_timer_callback, NULL, &network_timer);
//
// }

Expand Down
39 changes: 19 additions & 20 deletions examples/tutorials/thread_network/06_openthread/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand All @@ -24,11 +25,11 @@ static otUdpSocket sUdpSocket;
static void initUdp(otInstance* aInstance);
static void sendUdp(otInstance* aInstance);

uint8_t local_temperature_setpoint = 0;
uint8_t global_temperature_setpoint = 0;
uint8_t prior_global_temperature_setpoint = 0;
bool network_up = false;
bool pending_udp_send = false;
uint8_t local_temperature_setpoint = 22;
uint8_t global_temperature_setpoint = 255;
uint8_t prior_global_temperature_setpoint = 255;
bool network_up = false;
bool send_local_temp = false;

// Callback method for received udp packets.
static void handleUdpReceive(void* aContext, otMessage* aMessage,
Expand Down Expand Up @@ -64,6 +65,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
instance = otInstanceInitSingle();
assert(instance);

// TODO:
// Register this application as an IPC service under its name
// "org.tockos.thread-tutorial.openthread".

Expand All @@ -78,7 +80,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])

setNetworkConfiguration(instance);

// Set child timeout to 60 seconds.
// set child timeout to 60 seconds.
otThreadSetChildTimeout(instance, 60);

/* Start the Thread network interface (CLI cmd -> ifconfig up) */
Expand All @@ -102,16 +104,19 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
libtocksync_alarm_delay_ms(100);
}

// OpenThread mainloop
for ( ;;) {
// Process libtock drivers.
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

// Process OpenThread tasks.
otTaskletsProcess(instance);
if (send_local_temp) {
sendUdp(instance);
send_local_temp = false;
}

if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

// If no openthread tasks remain, yield.
if (!otTaskletsArePending(instance)) yield();
}

return 0;
Expand Down Expand Up @@ -155,6 +160,8 @@ static void stateChangeCallback(uint32_t flags, void* context) {
printf("[State Change] - Detached.\n");
break;
case OT_DEVICE_ROLE_CHILD:
network_up = true;
sendUdp(instance);
printf("[State Change] - Child.\n");
printf("Successfully attached to Thread network as a child.\n");
break;
Expand All @@ -174,8 +181,6 @@ static void print_ip_addr(otInstance* instance) {
const otNetifAddress* unicastAddrs = otIp6GetUnicastAddresses(instance);

printf("[THREAD] Device IPv6 Addresses: ");

// Iterate through IPV6 address printing each byte.
for (const otNetifAddress* addr = unicastAddrs; addr; addr = addr->mNext) {
const otIp6Address ip6_addr = addr->mAddress;
otIp6AddressToString(&ip6_addr, addr_string, sizeof(addr_string));
Expand All @@ -189,14 +194,10 @@ void handleUdpReceive(void* aContext, otMessage* aMessage,
OT_UNUSED_VARIABLE(aMessageInfo);
char buf[2];

// Obtain sender information
const otIp6Address sender_addr = aMessageInfo->mPeerAddr;
otIp6AddressToString(&sender_addr, buf, sizeof(buf));

// Copy the received message into our buffer.
otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, sizeof(buf) - 1);

// Update the global temperature setpoint.
global_temperature_setpoint = buf[0];
}

Expand All @@ -212,15 +213,13 @@ void initUdp(otInstance* aInstance) {
otUdpBind(aInstance, &sUdpSocket, &listenSockAddr, OT_NETIF_THREAD);
}

__attribute__ ((unused))
void sendUdp(otInstance* aInstance) {

otError error = OT_ERROR_NONE;
otMessage* message;
otMessageInfo messageInfo;
otIp6Address destinationAddr;

// Allocate and set messageInfo fields.
memset(&messageInfo, 0, sizeof(messageInfo));

otIp6AddressFromString(UDP_ROUTER_MULTICAST, &destinationAddr);
Expand Down
36 changes: 22 additions & 14 deletions examples/tutorials/thread_network/07_openthread_final/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand All @@ -24,18 +25,18 @@ static otUdpSocket sUdpSocket;
static void initUdp(otInstance* aInstance);
static void sendUdp(otInstance* aInstance);

uint8_t local_temperature_setpoint = 0;
uint8_t global_temperature_setpoint = 0;
uint8_t prior_global_temperature_setpoint = 0;
bool network_up = false;
bool pending_udp_send = false;
uint8_t local_temperature_setpoint = 22;
uint8_t global_temperature_setpoint = 255;
uint8_t prior_global_temperature_setpoint = 255;
bool network_up = false;
bool send_local_temp = false;

// Callback method for received udp packets.
static void handleUdpReceive(void* aContext, otMessage* aMessage,
const otMessageInfo* aMessageInfo);

static void openthread_ipc_callback(int pid, int len, int buf,
void* ud) {
__attribute__((unused)) void* ud) {
// A client has requested us to provide them the current temperature value.
// We must make sure that it provides us with a buffer sufficiently large to
// store a single integer:
Expand All @@ -44,25 +45,25 @@ static void openthread_ipc_callback(int pid, int len, int buf,
puts("[thread] ERROR: sensor IPC invoked with too small buffer.\r\n");
}

// copy value in buffer to local_temperature_setpoint
uint8_t passed_local_setpoint = *((uint8_t*) buf);
if (passed_local_setpoint != local_temperature_setpoint) {
// The local setpoint has changed, update it:
// The local setpoint has changed, update it.
local_temperature_setpoint = passed_local_setpoint;
sendUdp((otInstance*) ud);
send_local_temp = true;
}

if (network_up) {
if (prior_global_temperature_setpoint != global_temperature_setpoint) {
prior_global_temperature_setpoint = global_temperature_setpoint;

// The buffer is large enough, copy the current temperature into it:
// The buffer is large enough, copy the current temperature into it.
memcpy((void*) buf, &global_temperature_setpoint, sizeof(global_temperature_setpoint));

// Notify the client that the temperature has changed:
// Notify the client that the temperature has changed.
ipc_notify_client(pid);
}
}

}

// helper utility demonstrating network config setup
Expand All @@ -75,6 +76,7 @@ static void stateChangeCallback(uint32_t flags, void* context);
static void print_ip_addr(otInstance* instance);

int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[]) {
// Initialize OpenThread instance.
otSysInit(argc, argv);
otInstance* instance;
instance = otInstanceInitSingle();
Expand All @@ -83,7 +85,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
// Register this application as an IPC service under its name:
ipc_register_service_callback("org.tockos.thread-tutorial.openthread",
openthread_ipc_callback,
instance);
NULL);

/* As part of the initialization, we will:
- Init dataset with the following properties:
Expand Down Expand Up @@ -119,10 +121,15 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
}

for ( ;;) {
otSysProcessDrivers(instance);
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

if (send_local_temp) {
sendUdp(instance);
send_local_temp = false;
}

if (!otTaskletsArePending(instance)) {
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

Expand Down Expand Up @@ -170,6 +177,7 @@ static void stateChangeCallback(uint32_t flags, void* context) {
break;
case OT_DEVICE_ROLE_CHILD:
network_up = true;
sendUdp(instance);
printf("[State Change] - Child.\n");
printf("Successfully attached to Thread network as a child.\n");
break;
Expand Down
28 changes: 17 additions & 11 deletions examples/tutorials/thread_network/screen/openthread_app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assert.h>

#include <libopenthread/platform/openthread-system.h>
#include <libopenthread/platform/plat.h>
#include <openthread/dataset_ftd.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
Expand All @@ -24,18 +25,18 @@ static otUdpSocket sUdpSocket;
static void initUdp(otInstance* aInstance);
static void sendUdp(otInstance* aInstance);

uint8_t local_temperature_setpoint = 0;
uint8_t global_temperature_setpoint = 0;
uint8_t prior_global_temperature_setpoint = 0;
bool network_up = false;
bool pending_udp_send = false;
uint8_t local_temperature_setpoint = 22;
uint8_t global_temperature_setpoint = 255;
uint8_t prior_global_temperature_setpoint = 255;
bool network_up = false;
bool send_local_temp = false;

// Callback method for received udp packets.
static void handleUdpReceive(void* aContext, otMessage* aMessage,
const otMessageInfo* aMessageInfo);

static void openthread_ipc_callback(int pid, int len, int buf,
void* ud) {
__attribute__((unused)) void* ud) {
// A client has requested us to provide them the current temperature value.
// We must make sure that it provides us with a buffer sufficiently large to
// store a single integer:
Expand All @@ -49,7 +50,7 @@ static void openthread_ipc_callback(int pid, int len, int buf,
if (passed_local_setpoint != local_temperature_setpoint) {
// The local setpoint has changed, update it.
local_temperature_setpoint = passed_local_setpoint;
sendUdp((otInstance*) ud);
send_local_temp = true;
}

if (network_up) {
Expand All @@ -63,7 +64,6 @@ static void openthread_ipc_callback(int pid, int len, int buf,
ipc_notify_client(pid);
}
}

}

// helper utility demonstrating network config setup
Expand All @@ -85,7 +85,7 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
// Register this application as an IPC service under its name:
ipc_register_service_callback("org.tockos.thread-tutorial.openthread",
openthread_ipc_callback,
instance);
NULL);

/* As part of the initialization, we will:
- Init dataset with the following properties:
Expand Down Expand Up @@ -121,10 +121,15 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char* argv[])
}

for ( ;;) {
otSysProcessDrivers(instance);
otTaskletsProcess(instance);
otSysProcessDrivers(instance);

if (send_local_temp) {
sendUdp(instance);
send_local_temp = false;
}

if (!otTaskletsArePending(instance)) {
if (!otTaskletsArePending(instance) && !openthread_platform_pending_work()) {
yield();
}

Expand Down Expand Up @@ -172,6 +177,7 @@ static void stateChangeCallback(uint32_t flags, void* context) {
break;
case OT_DEVICE_ROLE_CHILD:
network_up = true;
sendUdp(instance);
printf("[State Change] - Child.\n");
printf("Successfully attached to Thread network as a child.\n");
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ TOCK_USERLAND_BASE_DIR = ../../../../..
# Which files to compile.
C_SRCS := $(wildcard *.c)

PACKAGE_NAME = thread_controller

STACK_SIZE = 4096
EXTERN_LIBS += $(TOCK_USERLAND_BASE_DIR)/u8g2

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void update_network_timer_callback(__attribute__ ((unused)) uint32_t now,
__attribute__ ((unused)) void* opaque) {
openthread_buffer[0] = local_temperature_setpoint;
ipc_notify_service(openthread_svc_num);
libtock_alarm_in_ms(500, update_network_timer_callback, NULL, &network_timer);
libtock_alarm_in_ms(250, update_network_timer_callback, NULL, &network_timer);

}

Expand Down
Loading

0 comments on commit a527d4b

Please sign in to comment.