Example #1
0
static void process_ro_access_report(LLRP_tSRO_ACCESS_REPORT *report) {
    LLRP_tSTagReportData *pTagReportData;
    int ni;


    // set all nodes to "not seen on any antenna"
    for (ni = 0; ni < bionet_hab_get_num_nodes(hab); ni ++) {
        bionet_node_t *node = bionet_hab_get_node_by_index(hab, ni);
        node_data_t *node_data = bionet_node_get_user_data(node);
        int i;

        if (node == reader_node) continue;

        node_data->still_here = 0;

        for (i = 1; i <= 4; i ++) {
            char resource_id[BIONET_NAME_COMPONENT_MAX_LEN];
            bionet_resource_t *resource;

            snprintf(resource_id, sizeof(resource_id), "Antenna-%d", i);
            resource = bionet_node_get_resource_by_id(node, resource_id);
            if (resource == NULL) {
                g_warning("error getting Resource %s:%s", bionet_node_get_id(node), resource_id);
                return;
            }
            bionet_resource_set_binary(resource, 0, NULL);
        }
    }


    // handle each TagReportData entry separately
    // any reports of an antenna seeing a node update that antenna's resources on the node and set the still-here flag
    for(
        pTagReportData = report->listTagReportData;
        NULL != pTagReportData;
        pTagReportData = (LLRP_tSTagReportData *)pTagReportData->hdr.pNextSubParameter
    ) {
        handle_tag_report_data(pTagReportData);
    }


    // report changes to the nodes (gone nodes, node with new datapoints
    for (ni = 0; ni < bionet_hab_get_num_nodes(hab); ni ++) {
        bionet_node_t *node = bionet_hab_get_node_by_index(hab, ni);
        node_data_t *node_data = bionet_node_get_user_data(node);

        if (node == reader_node) continue;

        if (node_data->still_here) {
            hab_report_datapoints(node);
        } else {
            free(node_data);
            bionet_node_set_user_data(node, NULL);
            bionet_hab_remove_node_by_id(hab, bionet_node_get_id(node));
            hab_report_lost_node(node);
            bionet_node_free(node);
            ni --;
        }
    }
}
bionet_resource_t *bionet_cache_lookup_resource(const char *hab_type, const char *hab_id, const char *node_id, const char *resource_id) {
    bionet_node_t *node;

    node = bionet_cache_lookup_node(hab_type, hab_id, node_id);
    if (node == NULL) {
        return NULL;
    }

    return bionet_node_get_resource_by_id(node, resource_id);
}
bionet_resource_t *bdm_cache_lookup_resource_uid(const uint8_t node_uid[BDM_UUID_LEN], const char *resource_id) {
    bionet_node_t *node;

    node = bdm_cache_lookup_node_uid(node_uid);
    if (node == NULL) {
        return NULL;
    }

    return bionet_node_get_resource_by_id(node, resource_id);
}
int bionet_node_set_resource_value(
    bionet_node_t *node,
    const char *resource_id,
    const void *value,
    const struct timeval *timestamp
) {
    bionet_resource_t *resource;
    int r;


    //
    // sanity checking
    //

    if (node == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bionet_node_set_resource_value(): NULL Node passed in");
        return -1;
    }

    if (resource_id == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bionet_node_set_resource_value(): NULL Resource-ID passed in");
        return -1;
    }

    if (value == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bionet_node_set_resource_value(): NULL value passed in");
        return -1;
    }


    resource = bionet_node_get_resource_by_id(node, resource_id);
    if (resource == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "couldnt find Resource '%s' of Node '%s'", resource_id, node->id);
        return -1;
    }

    r = bionet_resource_value_from_pointer(value, resource);
    if (r < 0) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "error updating resource value!");
        return -1;
    }

    r = bionet_resource_time_from_timeval(timestamp, resource);
    if (r < 0) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "error updating resource time!");
        return -1;
    }

    return 0;
}
bionet_resource_t *bdm_cache_lookup_resource(const char *hab_type, const char *hab_id,
					     const char * node_id,
					     const char * resource_id) {
    
    bionet_node_t *node = NULL;
    bionet_resource_t *resource = NULL;

    /* Sanity check */
    if (hab_type == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bdm_cache_lookup_resource(): NULL hab_type passed in!");
        return NULL;
    }

    if (hab_id == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bdm_cache_lookup_resource(): NULL hab_id passed in!");
        return NULL;
    }

    if (node_id == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bdm_cache_lookup_resource(): NULL node_id passed in!");
        return NULL;
    }

    if (resource_id == NULL) {
        g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "bdm_cache_lookup_resource(): NULL resource_id passed in!");
        return NULL;
    }

    /* find the node which would own this resource */
    node = bdm_cache_lookup_node(hab_type, hab_id, node_id);
    if (NULL == node) {
	g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s(): Unable to find node %s.%s.%s",
	      __FUNCTION__, hab_type, hab_id, node_id);
	return NULL;
    }

    resource = bionet_node_get_resource_by_id(node, resource_id);
    if (NULL == resource) {
	g_log(BIONET_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "%s(): Unable to find node %s.%s.%s:%s",
	      __FUNCTION__, hab_type, hab_id, node_id, resource_id);
    }

    return resource;

} /* bdm_cache_lookup_resource() */
Example #6
0
void uptime_update(bionet_node_t *node) {
    //Precondition: None
    //Postcondition: Sets the parameter 'Seconds of uptime'

    int r;
    float uptime_value;
    bionet_resource_t *resource;

    uptime_value = uptime_get();

    resource = bionet_node_get_resource_by_id(node, "Seconds-of-uptime");
    if (resource == NULL) {
        g_log("", G_LOG_LEVEL_WARNING, "uptime_init(): unable to find resource Seconds-of-uptime");
        return;
    }

    r = bionet_resource_set_float(resource, uptime_value, NULL);
    if (r < 0)
        g_log("", G_LOG_LEVEL_WARNING, "uptime_init(): Could not set resource value for Seconds-of-uptime");
    else 
        g_log("", G_LOG_LEVEL_DEBUG, "uptime_init(): set Seconds of uptime to: %f\n", uptime_value);
}
int read_data_from_stethoscope_and_write(int fd, bionet_stream_t * stream, int num_listeners)
{
    static char buffer[512];
    static int  buffer_size = 0;
    static int  state = RSP_STATUS_WAITING;

    int r;

    bionet_resource_t * resource;

    if (state == RSP_STATUS_WAITING)
    {
        r = read(fd, buffer+buffer_size, 8-buffer_size);

        if (r < 0)
        {
            if (errno == EAGAIN)
                return 0;

            return -1;
        }

        buffer_size += r;

        if (buffer_size == 8)
        {
            if ((r = ame_is_modem_command(buffer)))
            {
                r = read(fd, buffer+buffer_size, r-buffer_size);
		if (0 > r) {
		    if (EAGAIN == errno || EINTR == errno) {
			return 0;
		    } else {
			g_warning("Read error: %m");
			return -1;
		    }
		}
                buffer_size = 0;
            }
            else
                state = RSP_STATUS_GOT_COMMAND;
        }
    }

    if (state == RSP_STATUS_GOT_COMMAND)
    {
        switch ((uint8_t)buffer[1])
        {
        case '#':
            g_debug("attempting to purge buffer of crap...buffer_size = %d", buffer_size);
            memmove(buffer, buffer+6, buffer_size - 6);
            buffer_size -= 6;
            state = RSP_STATUS_WAITING;
            break;

        case RSP_HEADER:
            g_debug("attempting to purge buffer of crap byte...buffer_size = %d", buffer_size);
            memmove(buffer, buffer+1, buffer_size - 1);
            buffer_size -= 1;
            state = RSP_STATUS_WAITING;
            break;

        case CMD_READ_BATTERY_STATUS:
            g_debug("Received battery response");
            {
                float voltage;
                struct timeval tv;

                r = gettimeofday(&tv, NULL);
                if (r < 0) {
                    g_warning("error with gettimeofday: %s", strerror(errno));
                    return -1;
                }

                r = read(fd, buffer+buffer_size, 12-buffer_size);

                if (r <= 0)
                {
                    if (errno == EAGAIN)
                        return 0;

                    return -1;
                }

                buffer_size += r;

                voltage = ame_parse_battery_voltage(buffer, buffer_size);
                g_debug("Got the battery voltage: %f, size = %d", voltage, buffer_size);

		resource = bionet_node_get_resource_by_id(node, "Battery-Voltage");
                r = bionet_resource_set_float(resource, voltage, &tv);

                if (r < 0)
                    g_warning("error updating resource value!");
                else
                    hab_report_datapoints(node);
            }

            buffer_size = 0;
            state = RSP_STATUS_WAITING;
            
            break;

        case CMD_RETURN_DATA:
        {
            // Need to byte escape the bytes that we've already read in...
            char tmp[5] = {buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]};

            buffer_size = 3;
            buffer_size += unbyte_escape_and_copy(tmp, buffer+buffer_size, 5);

            state = RSP_STATUS_SAMPLING_ENABLED;
        }
            break;

        default:
            g_debug("Unknown response: %c - Clearing and starting over", buffer[1]);
            buffer_size = 0;
            state = RSP_STATUS_WAITING;
            break;
        }
    }

    if ((state == RSP_STATUS_SAMPLING_ENABLED) && (num_listeners))
    {
        static int size = 248;

        char tmp[256];

        r = read(fd, tmp, size-buffer_size);

        if (r <= 0)
        {
            if (errno == EAGAIN)
                return 0;

            return -1;
        }

        buffer_size += unbyte_escape_and_copy(buffer+buffer_size, tmp, r);

        if (buffer_size == size)
        {
            uint16_t frame = 0;
            int16_t pcm16data[SAMPLE_PADDING*FRAME_SIZE*CHANNELS];
            int i = 0;


            frame += (uint8_t)buffer[4];
            frame += (uint8_t)(buffer[3] << 8);
            g_debug("Got streaming frame %d, start bytes are %c %c %c 0x00 %c %c %c %c", frame,
                    buffer[0], buffer[1], buffer[2], /*buffer[3],*/ buffer[4], buffer[5], buffer[6], buffer[7]);


            // Upsample for minimum alsa rate and for stereo
            // This method sounds like total shit!!!  MUST LPF!
            // Also, because 22050/1200 isn't an integer, output will
            // underrun about every 5 seconds.  Total shit!!
            for (i = 0; i < FRAME_SIZE; i++)
            {
                int k;

                for (k = 0; k < SAMPLE_PADDING*CHANNELS; k++)
                    pcm16data[SAMPLE_PADDING*CHANNELS*i+k] = mulaw2pcm16((uint8_t)buffer[i+5]) * gain;
            }


	    hab_publish_stream(stream, pcm16data, sizeof(pcm16data));

            buffer_size = 0;
            state = RSP_STATUS_WAITING;
        }
    }


    return 0;
}
Example #8
0
static void process_ro_access_report_with_loss_timeout(LLRP_tSRO_ACCESS_REPORT *report) {
    LLRP_tSTagReportData *pTagReportData;
    int ni;

    // handle each TagReportData entry separately
    // any reports of an antenna seeing a node update that antenna's resources on the node and set the still-here flag
    for(
        pTagReportData = report->listTagReportData;
        NULL != pTagReportData;
        pTagReportData = (LLRP_tSTagReportData *)pTagReportData->hdr.pNextSubParameter
    ) {
        handle_tag_report_data(pTagReportData);
    }

    // set all nodes to "not seen on any antenna"
    for (ni = 0; ni < bionet_hab_get_num_nodes(hab); ni ++) {
        bionet_node_t *node = bionet_hab_get_node_by_index(hab, ni);
        node_data_t *node_data = bionet_node_get_user_data(node);
        int i;

        if (node == reader_node) continue;

	int counter = 0;

        for (i = 1; i <= 4; i ++) {
            char resource_id[BIONET_NAME_COMPONENT_MAX_LEN];
            bionet_resource_t *resource;

            snprintf(resource_id, sizeof(resource_id), "Antenna-%d", i);
            resource = bionet_node_get_resource_by_id(node, resource_id);
            if (resource == NULL) {
                g_warning("error getting Resource %s:%s", bionet_node_get_id(node), resource_id);
                return;
            }

	    int value;
	    struct timeval tv = { 0, 0 };
	    struct timeval curtime;
            if (0 == bionet_resource_get_num_datapoints(resource) || bionet_resource_get_binary(resource, &value, &tv)) {
		counter++;
	    } else {
		gettimeofday(&curtime, NULL);
		struct timeval diff = bionet_timeval_subtract(&curtime, &tv);
		if (diff.tv_sec >= loss_timeout) {
		    bionet_resource_set_binary(resource, 0, NULL);
		    counter++;
		}
	    }
	}

	if (counter == 4) {
            free(node_data);
            bionet_node_set_user_data(node, NULL);
            bionet_hab_remove_node_by_id(hab, bionet_node_get_id(node));
            hab_report_lost_node(node);
            bionet_node_free(node);
            ni --;	    
        }

	hab_report_datapoints(node);
    }

}