Skip to content

Commit

Permalink
Move OT JOB to OT COMMAND
Browse files Browse the repository at this point in the history
  • Loading branch information
biboc committed Jun 1, 2017
1 parent 0b4334e commit 0256eff
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 91 deletions.
14 changes: 7 additions & 7 deletions pkg/openthread/contrib/netdev/openthread_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ static otInstance *sInstance;
#endif
/** @} */

uint8_t ot_call_job(char* name, void *arg, void* answer) {
ot_job_t _job;
uint8_t ot_call_command(char* command, void *arg, void* answer) {
ot_job_t job;

_job.mName = name;
_job.mArg = arg;
_job.mAnswer = answer;
job.command = command;
job.arg = arg;
job.answer = answer;

msg_t msg, reply;
msg.type = OPENTHREAD_JOB_MSG_TYPE_EVENT;
msg.content.ptr = &_job;
msg.content.ptr = &job;
msg_send_receive(&msg, &reply, openthread_get_pid());
return (uint8_t)reply.content.value;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ static void *_openthread_event_loop(void *arg) {
break;
case OPENTHREAD_JOB_MSG_TYPE_EVENT:
job = msg.content.ptr;
reply.content.value = ot_exec_job(sInstance, job->mName, job->mArg, job->mAnswer);
reply.content.value = ot_exec_command(sInstance, job->command, job->arg, job->answer);
msg_reply(&msg, &reply);
break;
}
Expand Down
148 changes: 79 additions & 69 deletions pkg/openthread/contrib/platform_functions_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,110 +28,120 @@
#define ENABLE_DEBUG (1)
#include "debug.h"

typedef uint8_t OT_JOB;
typedef uint8_t OT_COMMAND;

OT_JOB ot_channel(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_eui64(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_extaddr(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_ipaddr(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_masterkey(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_networkname(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_mode(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_panid(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_parent(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_state(otInstance* ot_instance, void* arg, void* answer);
OT_JOB ot_thread(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_channel(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_eui64(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_extaddr(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_ipaddr(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_masterkey(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_networkname(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_mode(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_panid(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_parent(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_state(otInstance* ot_instance, void* arg, void* answer);
OT_COMMAND ot_thread(otInstance* ot_instance, void* arg, void* answer);

/**
* @brief Struct containing an OpenThread job command
*/
typedef struct {
const char *mName; /**< A pointer to the job name string. */
OT_JOB (*mFunction)(otInstance*, void*, void*); /**< function to be called when executing job */
} ot_job_command_t;
const char *name; /**< A pointer to the job name string. */
OT_COMMAND (*function)(otInstance*, void*, void*); /**< function to be called */
} ot_command_t;

const ot_job_command_t otJobs[] =
const ot_command_t otCommands[] =
{
{ "channel", &ot_channel }, /* arg NULL: get channel in answer | arg not NULL: set channel */
{ "eui64", &ot_eui64 }, /* arg NULL: get eui64 in answer | arg not NULL: set eui64 */
{ "extaddr", &ot_extaddr }, /* arg NULL: get extaddr in answer | arg not NULL: set extaddr */
{ "ipaddr", &ot_ipaddr }, /* arg NULL: get number of ipaddr in answer | arg not NULL: get ipaddr[arg] */
{ "masterkey", &ot_masterkey }, /* arg NULL: get masterkey in answer | arg not NULL: set masterkey */
{ "mode", ot_mode }, /* arg NULL: get mode in answer | arg not NULL: set mode */
{ "networkname", &ot_networkname }, /* arg NULL: get networkname in answer | arg not NULL: set networkname */
{ "panid", &ot_panid }, /* arg NULL: get panid in answer | arg not NULL: set panid */
{ "parent", &ot_parent }, /* arg NULL: get parent in answer */
{ "state", &ot_state }, /* arg NULL: get state in answer */
{ "thread", &ot_thread }, /* arg "start"/"stop": start/stop thread operation */
/* channel: arg NULL: get channel in answer | arg not NULL: set channel */
{ "channel", &ot_channel },
/* eui64 : arg NULL: get eui64 in answer | arg not NULL: set eui64 */
{ "eui64", &ot_eui64 },
/* extaddr: arg NULL: get extaddr in answer | arg not NULL: set extaddr */
{ "extaddr", &ot_extaddr },
/* ipaddr: arg NULL: get nb ipaddr in answer | arg not NULL: get ipaddr[arg] */
{ "ipaddr", &ot_ipaddr },
/* masterkey: arg NULL: get masterkey in answer | arg not NULL: set masterkey */
{ "masterkey", &ot_masterkey },
/* mode: arg NULL: get mode in answer | arg not NULL: set mode */
{ "mode", ot_mode },
/* networkname: arg NULL: get networkname in answer | arg not NULL: set networkname */
{ "networkname", &ot_networkname },
/* panid: arg NULL: get panid in answer | arg not NULL: set panid */
{ "panid", &ot_panid },
/* parent: arg NULL: get parent in answer */
{ "parent", &ot_parent },
/* state: arg NULL: get state in answer */
{ "state", &ot_state },
/* thread: arg "start"/"stop": start/stop thread operation */
{ "thread", &ot_thread },
};

uint8_t ot_exec_job(otInstance *ot_instance, const char* name, void *arg, void* answer) {
uint8_t ot_exec_command(otInstance *ot_instance, const char* command, void *arg, void* answer) {
uint8_t res = 0xFF;
/* Check running thread */
if (openthread_get_pid() == thread_getpid()) {
for (uint8_t i = 0; i < sizeof(otJobs) / sizeof(otJobs[0]); i++) {
if (strcmp(name, otJobs[i].mName) == 0) {
res = (*otJobs[i].mFunction)(ot_instance, arg, answer);
for (uint8_t i = 0; i < sizeof(otCommands) / sizeof(otCommands[0]); i++) {
if (strcmp(command, otCommands[i].name) == 0) {
res = (*otCommands[i].function)(ot_instance, arg, answer);
break;
}
}
if (res == 0xFF) {
DEBUG("Wrong ot_job name\n");
DEBUG("Wrong ot_COMMAND name\n");
res = 1;
}
} else {
printf("ERROR: ot_exec_job needs to run in OpenThread thread\n");
DEBUG("ERROR: ot_exec_job needs to run in OpenThread thread\n");
}
return res;
}

void output_bytes(const char* name, const uint8_t *aBytes, uint8_t aLength)
{
DEBUG("%s: ", name);
for (int i = 0; i < aLength; i++)
{
for (int i = 0; i < aLength; i++) {
DEBUG("%02x", aBytes[i]);
}
DEBUG("\n");
}

OT_JOB ot_channel(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_channel(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
*((uint8_t *) answer) = otLinkGetChannel(ot_instance);
DEBUG("Channel: %04x\n", *((uint8_t *) answer));
} else if (arg != NULL) {
uint8_t channel = *((uint8_t *) arg);
otLinkSetChannel(ot_instance, channel);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_eui64(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_eui64(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
otExtAddress extAddress;
otLinkGetFactoryAssignedIeeeEui64(ot_instance, &extAddress);
output_bytes("eui64", extAddress.m8, OT_EXT_ADDRESS_SIZE);
*((otExtAddress *) answer) = extAddress;
otExtAddress address;
otLinkGetFactoryAssignedIeeeEui64(ot_instance, &address);
output_bytes("eui64", address.m8, OT_EXT_ADDRESS_SIZE);
*((otExtAddress *) answer) = address;
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}


OT_JOB ot_extaddr(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_extaddr(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
answer = (void*)otLinkGetExtendedAddress(ot_instance);
output_bytes("extaddr", (const uint8_t *)answer, OT_EXT_ADDRESS_SIZE);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_ipaddr(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_ipaddr(otInstance* ot_instance, void* arg, void* answer) {
uint8_t cnt = 0;
for (const otNetifAddress *addr = otIp6GetUnicastAddresses(ot_instance); addr; addr = addr->mNext) {
if (arg != NULL && answer != NULL && cnt == *((uint8_t *) arg)) {
Expand All @@ -143,70 +153,70 @@ OT_JOB ot_ipaddr(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
*((uint8_t *) answer) = cnt;
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_masterkey(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_masterkey(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
const otMasterKey* masterkey = otThreadGetMasterKey(ot_instance);
*((otMasterKey *) answer) = *masterkey;
output_bytes("masterkey", (const uint8_t *)answer, OT_MASTER_KEY_SIZE);
} else if (arg != NULL) {
otThreadSetMasterKey(ot_instance, (otMasterKey*)arg);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_mode(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_mode(otInstance* ot_instance, void* arg, void* answer) {
if (arg != NULL) {
otLinkModeConfig linkMode;
memset(&linkMode, 0, sizeof(otLinkModeConfig));
otLinkModeConfig link_mode;
memset(&link_mode, 0, sizeof(otLinkModeConfig));
char mode[6] = "";
memcpy(mode, (char*)arg, 5);
mode[5] = '\0';
for (char *arg = &mode[0]; *arg != '\0'; arg++) {
switch (*arg) {
case 'r':
linkMode.mRxOnWhenIdle = 1;
link_mode.mRxOnWhenIdle = 1;
break;
case 's':
linkMode.mSecureDataRequests = 1;
link_mode.mSecureDataRequests = 1;
break;
case 'd':
linkMode.mDeviceType = 1;
link_mode.mDeviceType = 1;
break;
case 'n':
linkMode.mNetworkData = 1;
link_mode.mNetworkData = 1;
break;
}
}
otThreadSetLinkMode(ot_instance, linkMode);
otThreadSetLinkMode(ot_instance, link_mode);
DEBUG("OT mode changed to %s\n", (char*)arg);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_networkname(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_networkname(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
const char* networkName = otThreadGetNetworkName(ot_instance);
strcpy((char*) answer, networkName);
DEBUG("networkname: %.*s\n", OT_NETWORK_NAME_MAX_SIZE, networkName);
} else if (arg != NULL) {
otThreadSetNetworkName(ot_instance, (char*) arg);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}


OT_JOB ot_panid(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_panid(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
*((uint16_t *) answer) = otLinkGetPanId(ot_instance);
DEBUG("PanID: %04x\n", *((uint16_t *) answer));
Expand All @@ -217,25 +227,25 @@ OT_JOB ot_panid(otInstance* ot_instance, void* arg, void* answer) {
otLinkSetPanId(ot_instance, panid);
otThreadSetEnabled(ot_instance, true);
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_parent(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_parent(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
otRouterInfo parentInfo;
otThreadGetParentInfo(ot_instance, &parentInfo);
output_bytes("parent", (const uint8_t *)parentInfo.mExtAddress.m8, sizeof(parentInfo.mExtAddress));
DEBUG("Rloc: %x\n", parentInfo.mRloc16);
*((otRouterInfo *) answer) = parentInfo;
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_state(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_state(otInstance* ot_instance, void* arg, void* answer) {
if (answer != NULL) {
uint8_t state = otThreadGetDeviceRole(ot_instance);
*((uint8_t *) answer) = state;
Expand Down Expand Up @@ -264,12 +274,12 @@ OT_JOB ot_state(otInstance* ot_instance, void* arg, void* answer) {
break;
}
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}

OT_JOB ot_thread(otInstance* ot_instance, void* arg, void* answer) {
OT_COMMAND ot_thread(otInstance* ot_instance, void* arg, void* answer) {
if (arg != NULL) {
if (strcmp((char*)arg, "start") == 0) {
otThreadSetEnabled(ot_instance, true);
Expand All @@ -278,10 +288,10 @@ OT_JOB ot_thread(otInstance* ot_instance, void* arg, void* answer) {
otThreadSetEnabled(ot_instance, false);
DEBUG("Thread stop\n");
} else {
printf("ERROR: thread available args: start/stop\n");
DEBUG("ERROR: thread available args: start/stop\n");
}
} else {
printf("ERROR: wrong argument\n");
DEBUG("ERROR: wrong argument\n");
}
return 0;
}
28 changes: 14 additions & 14 deletions pkg/openthread/include/ot.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ typedef struct {
* @brief Struct containing an OpenThread job
*/
typedef struct {
const char *mName; /**< A pointer to the job name string. */
void *mArg; /**< arg for the job **/
void *mAnswer; /**< answer from the job **/
const char *command; /**< A pointer to the job name string. */
void *arg; /**< arg for the job **/
void *answer; /**< answer from the job **/
} ot_job_t;

/**
Expand Down Expand Up @@ -120,30 +120,30 @@ void ot_random_init(void);
void openthread_uart_run(void);

/**
* @brief Execute OpenThread job. Call this function only in OpenThread thread
* @brief Execute OpenThread command. Call this function only in OpenThread thread
*
* @param[in] ot_instance OpenThread instance
* @param[in] job callback pointer to an OpenThread job function
* @param[in] arg arg for the job
* @param[out] answer answer for the job
* @param[in] command OpenThread command name
* @param[in] arg arg for the command
* @param[out] answer answer for the command
*
* @return 0 on success, 1 on error
*/
uint8_t ot_exec_job(otInstance *ot_instance, const char* name, void *arg, void* answer);
uint8_t ot_exec_command(otInstance *ot_instance, const char* command, void *arg, void* answer);

/**
* @brief Call OpenThread job in same thread as OT core (due to concurrency).
* @brief Call OpenThread command in same thread as OT core (due to concurrency).
*
* @note An OpenThread job allows direct calls to OpenThread API (otXXX functions) without worrying about concurrency
* @note An OpenThread command allows direct calls to OpenThread API (otXXX functions) without worrying about concurrency
* issues. All API calls should be made in OT_JOB type functions.
*
* @param[in] name name of the job to call
* @param[in] arg arg for the job
* @param[out] answer answer for the job
* @param[in] command name of the command to call
* @param[in] arg arg for the command
* @param[out] answer answer for the command
*
* @return 0 on success, 1 on error
*/
uint8_t ot_call_job(char* name, void *arg, void* answer);
uint8_t ot_call_command(char* command, void *arg, void* answer);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 0256eff

Please sign in to comment.