diff --git a/sys/include/transceiver.h b/sys/include/transceiver.h index e8025b815f..ad01c7a3fc 100644 --- a/sys/include/transceiver.h +++ b/sys/include/transceiver.h @@ -228,5 +228,15 @@ int transceiver_start(void); */ uint8_t transceiver_register(transceiver_type_t transceivers, int pid); +/** + * @brief unregister a thread for events from certain transceivers + * + * @param transceivers The transceiver types to register for + * @param pid The pid of the thread to register + * + * @return 1 on success, 0 otherwise + */ +uint8_t transceiver_unregister(transceiver_type_t transceivers, int pid); + #endif /* TRANSCEIVER_H */ /** @} */ diff --git a/sys/transceiver/transceiver.c b/sys/transceiver/transceiver.c index 0ea0892e8b..03f491ca6c 100644 --- a/sys/transceiver/transceiver.c +++ b/sys/transceiver/transceiver.c @@ -233,22 +233,37 @@ int transceiver_start(void) /* Register an upper layer thread */ uint8_t transceiver_register(transceiver_type_t t, int pid) { - uint8_t i; - - /* find pid in registered threads or first unused space */ - for (i = 0; ((i < TRANSCEIVER_MAX_REGISTERED) && - (reg[i].pid != pid) && - (reg[i].transceivers != TRANSCEIVER_NONE)); i++); - - if (i >= TRANSCEIVER_MAX_REGISTERED) { - return ENOMEM; + int result = 0; + int state = disableIRQ(); + for (size_t i = 0; i < TRANSCEIVER_MAX_REGISTERED; i++) { + if ((reg[i].pid == pid) || (reg[i].transceivers == TRANSCEIVER_NONE)) { + reg[i].transceivers |= t; + reg[i].pid = pid; + DEBUG("transceiver: Thread %i registered for %i\n", reg[i].pid, reg[i].transceivers); + restoreIRQ(state); + result = 1; + break; + } } - else { - reg[i].transceivers |= t; - reg[i].pid = pid; - DEBUG("transceiver: Thread %i registered for %i\n", reg[i].pid, reg[i].transceivers); - return 1; + restoreIRQ(state); + return result; +} + +/* Unregister an upper layer thread */ +uint8_t transceiver_unregister(transceiver_type_t t, int pid) +{ + int result = 0; + int state = disableIRQ(); + for (size_t i = 0; i < TRANSCEIVER_MAX_REGISTERED; ++i) { + if (reg[i].pid == pid) { + reg[i].transceivers &= ~t; + restoreIRQ(state); + result = 1; + break; + } } + restoreIRQ(state); + return result; } /*------------------------------------------------------------------------------------*/ @@ -465,7 +480,7 @@ static void receive_packet(uint16_t type, uint8_t pos) #ifdef DBG_IGNORE - for (uint8_t i = 0; (i < TRANSCEIVER_MAX_IGNORED_ADDR) && (transceiver_ignored_addr[i]); i++) { + for (size_t i = 0; (i < TRANSCEIVER_MAX_IGNORED_ADDR) && (transceiver_ignored_addr[i]); i++) { DEBUG("check if source (%u) is ignored -> %u\n", transceiver_buffer[transceiver_buffer_pos].src, transceiver_ignored_addr[i]); if (transceiver_buffer[transceiver_buffer_pos].src == transceiver_ignored_addr[i]) { @@ -1223,7 +1238,7 @@ static int16_t ignore_add(transceiver_type_t transceiver, void *address) (void) transceiver; radio_address_t addr = *((radio_address_t *)address); - for (uint8_t i = 0; i < TRANSCEIVER_MAX_IGNORED_ADDR; i++) { + for (size_t i = 0; i < TRANSCEIVER_MAX_IGNORED_ADDR; i++) { if (transceiver_ignored_addr[i] == 0) { transceiver_ignored_addr[i] = addr; DEBUG("addr %u will be ignored (%u)\n", addr, i);