2017-09-15 16:16:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/wireless/ieee802154/i8sak/i8sak_events.c
|
|
|
|
*
|
2022-03-07 15:05:57 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-09-15 16:16:47 +02:00
|
|
|
*
|
2022-03-07 15:05:57 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-09-15 16:16:47 +02:00
|
|
|
*
|
2022-03-07 15:05:57 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-09-15 16:16:47 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
|
|
|
|
|
|
|
|
#include "wireless/ieee802154.h"
|
|
|
|
|
|
|
|
#include "i8sak.h"
|
|
|
|
#include "i8sak_events.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name : i8sak_eventthread
|
|
|
|
*
|
|
|
|
* Description :
|
|
|
|
* Listen for events from the MAC layer
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static pthread_addr_t i8sak_eventthread(pthread_addr_t arg)
|
|
|
|
{
|
|
|
|
FAR struct i8sak_s *i8sak = (FAR struct i8sak_s *)arg;
|
|
|
|
union ieee802154_macarg_u macarg;
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
struct ieee802154_netmac_s netarg;
|
|
|
|
#endif
|
|
|
|
FAR struct i8sak_eventreceiver_s *receiver;
|
2017-11-01 21:32:29 +01:00
|
|
|
FAR struct ieee802154_primitive_s *primitive = NULL;
|
2018-10-06 22:12:26 +02:00
|
|
|
int ret = OK;
|
2017-09-15 16:16:47 +02:00
|
|
|
|
|
|
|
if (i8sak->mode == I8SAK_MODE_CHAR)
|
|
|
|
{
|
|
|
|
macarg.enable = true;
|
|
|
|
ioctl(i8sak->fd, MAC802154IOC_ENABLE_EVENTS,
|
|
|
|
(unsigned long)((uintptr_t)&macarg));
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
else if (i8sak->mode == I8SAK_MODE_NETIF)
|
|
|
|
{
|
|
|
|
netarg.u.enable = true;
|
|
|
|
strncpy(netarg.ifr_name, i8sak->ifname, IFNAMSIZ);
|
|
|
|
ioctl(i8sak->fd, MAC802154IOC_ENABLE_EVENTS,
|
|
|
|
(unsigned long)((uintptr_t)&netarg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (i8sak->eventlistener_run)
|
2022-03-07 16:11:14 +01:00
|
|
|
{
|
2017-09-15 16:16:47 +02:00
|
|
|
if (i8sak->mode == I8SAK_MODE_CHAR)
|
|
|
|
{
|
|
|
|
ret = ioctl(i8sak->fd, MAC802154IOC_GET_EVENT,
|
|
|
|
(unsigned long)((uintptr_t)&macarg));
|
2017-11-01 21:32:29 +01:00
|
|
|
primitive = &macarg.primitive;
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
else if (i8sak->mode == I8SAK_MODE_NETIF)
|
|
|
|
{
|
|
|
|
ret = ioctl(i8sak->fd, MAC802154IOC_GET_EVENT,
|
|
|
|
(unsigned long)((uintptr_t)&netarg));
|
2017-11-01 21:32:29 +01:00
|
|
|
primitive = &netarg.u.primitive;
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2018-10-06 22:12:26 +02:00
|
|
|
i8sak->eventlistener_run = false;
|
|
|
|
continue;
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = sem_wait(&i8sak->eventsem);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2018-10-06 22:12:26 +02:00
|
|
|
i8sak->eventlistener_run = false;
|
|
|
|
continue;
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through event receivers and call callbacks for those receivers
|
|
|
|
* listening for this event type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
receiver = (FAR struct i8sak_eventreceiver_s *)sq_peek(
|
|
|
|
&i8sak->eventreceivers);
|
|
|
|
|
|
|
|
while (receiver != NULL)
|
|
|
|
{
|
2017-11-01 21:32:29 +01:00
|
|
|
if ((primitive->type == IEEE802154_PRIMITIVE_CONF_DATA &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.data) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_ASSOC &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.assoc) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_DISASSOC &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.disassoc) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_GTS &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.gts) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_RESET &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.reset) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_RXENABLE &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.rxenable) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_SCAN &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.scan) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_START &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.start) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_CONF_POLL &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.confevents.poll) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_ASSOC &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.assoc) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_DISASSOC &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.disassoc) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_BEACONNOTIFY &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.beacon) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_GTS &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.gts) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_ORPHAN &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.orphan) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_COMMSTATUS &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.commstatus) ||
|
2017-11-01 21:32:29 +01:00
|
|
|
(primitive->type == IEEE802154_PRIMITIVE_IND_SYNCLOSS &&
|
2017-09-15 16:16:47 +02:00
|
|
|
receiver->filter.indevents.syncloss))
|
|
|
|
{
|
2017-11-01 21:32:29 +01:00
|
|
|
receiver->cb(primitive, receiver->arg);
|
2017-09-15 16:16:47 +02:00
|
|
|
|
|
|
|
if (receiver->oneshot)
|
|
|
|
{
|
|
|
|
/* Unlink the receiver from the list */
|
|
|
|
|
|
|
|
sq_rem((FAR sq_entry_t *)receiver, &i8sak->eventreceivers);
|
|
|
|
|
|
|
|
/* Link the receiver back into the free list */
|
|
|
|
|
2022-03-07 16:11:14 +01:00
|
|
|
sq_addlast((FAR sq_entry_t *)receiver,
|
|
|
|
&i8sak->eventreceivers_free);
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver = (FAR struct i8sak_eventreceiver_s *)sq_next(
|
|
|
|
(FAR sq_entry_t *)receiver);
|
|
|
|
}
|
2022-03-07 16:11:14 +01:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
sem_post(&i8sak->eventsem);
|
|
|
|
}
|
|
|
|
|
2018-10-06 22:12:26 +02:00
|
|
|
if (i8sak->mode == I8SAK_MODE_CHAR)
|
|
|
|
{
|
|
|
|
macarg.enable = false;
|
|
|
|
ioctl(i8sak->fd, MAC802154IOC_ENABLE_EVENTS,
|
|
|
|
(unsigned long)((uintptr_t)&macarg));
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
else if (i8sak->mode == I8SAK_MODE_NETIF)
|
|
|
|
{
|
|
|
|
netarg.u.enable = false;
|
|
|
|
strncpy(netarg.ifr_name, i8sak->ifname, IFNAMSIZ);
|
|
|
|
ioctl(i8sak->fd, MAC802154IOC_ENABLE_EVENTS,
|
|
|
|
(unsigned long)((uintptr_t)&netarg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i8sak_eventlistener_start
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Starts internal threads to listen for events.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* i8sak - handle to the i8sak instance struct
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int i8sak_eventlistener_start(FAR struct i8sak_s *i8sak)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
i8sak->eventlistener_run = true;
|
|
|
|
|
2022-03-07 16:11:14 +01:00
|
|
|
ret = pthread_create(&i8sak->eventlistener_threadid,
|
|
|
|
NULL, i8sak_eventthread,
|
2017-09-15 16:16:47 +02:00
|
|
|
(void *)i8sak);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to start event thread: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i8sak_eventlistener_stop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stops event thread.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* handle - handle to the i8sak instance struct
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int i8sak_eventlistener_stop(FAR struct i8sak_s *i8sak)
|
|
|
|
{
|
2018-10-06 22:12:26 +02:00
|
|
|
int ret;
|
|
|
|
FAR void *value;
|
|
|
|
|
|
|
|
i8sak->eventlistener_run = false;
|
|
|
|
pthread_kill(i8sak->eventlistener_threadid, 2);
|
|
|
|
ret = pthread_join(i8sak->eventlistener_threadid, &value);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: pthread_join() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-09-15 16:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i8sak_eventlistener_addreceiver
|
|
|
|
*
|
|
|
|
* Description:
|
2022-03-07 16:11:14 +01:00
|
|
|
* Add an event receiver.
|
|
|
|
* An event receiver consists of a callback and flags for which events
|
|
|
|
* should be sent to the callback.
|
2017-09-15 16:16:47 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* handle - handle to the i8sak instance struct
|
|
|
|
* cb - callback to be called on reception of notification event from
|
|
|
|
* MAC layer that matches one of the masked events
|
|
|
|
* filter - struct containing event mask bits to indicate whether event
|
|
|
|
* type should trigger callback
|
|
|
|
* arg - user specified argument to send to the callback
|
|
|
|
* oneshot - whether the receiver is automatically unregistered after the
|
|
|
|
* first notification
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if successful; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int i8sak_eventlistener_addreceiver(FAR struct i8sak_s *i8sak,
|
|
|
|
i8sak_eventcallback_t cb,
|
|
|
|
FAR struct i8sak_eventfilter_s *filter,
|
|
|
|
bool oneshot)
|
|
|
|
{
|
|
|
|
FAR struct i8sak_eventreceiver_s *receiver;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sem_wait(&i8sak->eventsem);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to get exclusive access: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a receiver struct from the static pool */
|
|
|
|
|
|
|
|
receiver = (FAR struct i8sak_eventreceiver_s *)sq_remfirst(
|
2022-03-07 16:11:14 +01:00
|
|
|
&i8sak->eventreceivers_free);
|
2017-09-15 16:16:47 +02:00
|
|
|
if (receiver == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to add receiver: %d\n", ENOMEM);
|
|
|
|
sem_post(&i8sak->eventsem);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver->cb = cb;
|
|
|
|
memcpy(&receiver->filter, filter, sizeof(struct i8sak_eventfilter_s));
|
|
|
|
receiver->arg = i8sak;
|
|
|
|
receiver->oneshot = oneshot;
|
|
|
|
|
|
|
|
/* Link the receiver into the list */
|
|
|
|
|
|
|
|
sq_addlast((FAR sq_entry_t *)receiver, &i8sak->eventreceivers);
|
|
|
|
|
|
|
|
sem_post(&i8sak->eventsem);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i8sak_eventlistener_removereceiver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Removes a event receiver. Listener will no longer call callback. This
|
|
|
|
* function finds the first event receiver with the provided callback.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* handle - handle to the i8sak instance struct
|
|
|
|
* cb - callback function to search for.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int i8sak_eventlistener_removereceiver(FAR struct i8sak_s *i8sak,
|
|
|
|
i8sak_eventcallback_t cb)
|
|
|
|
{
|
|
|
|
FAR struct i8sak_eventreceiver_s *receiver;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sem_wait(&i8sak->eventsem);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2020-02-23 05:51:44 +01:00
|
|
|
fprintf(stderr, "failed to get exclusive access: %d\n", ret);
|
2017-09-15 16:16:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search through frame receivers until either we match the callback, or
|
|
|
|
* there is no more receivers to check.
|
|
|
|
*/
|
|
|
|
|
|
|
|
receiver = (FAR struct i8sak_eventreceiver_s *)sq_peek(
|
|
|
|
&i8sak->eventreceivers);
|
|
|
|
|
|
|
|
while (receiver != NULL)
|
|
|
|
{
|
|
|
|
/* Check if callback matches */
|
|
|
|
|
|
|
|
if (receiver->cb == cb)
|
|
|
|
{
|
|
|
|
/* Unlink the receiver from the list */
|
|
|
|
|
|
|
|
sq_rem((FAR sq_entry_t *)receiver, &i8sak->eventreceivers);
|
|
|
|
|
|
|
|
/* Link the receiver back into the free list */
|
|
|
|
|
2022-03-07 16:11:14 +01:00
|
|
|
sq_addlast((FAR sq_entry_t *)receiver,
|
|
|
|
&i8sak->eventreceivers_free);
|
2017-09-15 16:16:47 +02:00
|
|
|
|
|
|
|
sem_post(&i8sak->eventsem);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
receiver = (FAR struct i8sak_eventreceiver_s *)sq_next(
|
|
|
|
(FAR sq_entry_t *)receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&i8sak->eventsem);
|
|
|
|
fprintf(stderr, "failed to remove receiver");
|
|
|
|
return ERROR;
|
|
|
|
}
|