Beispiel #1
0
/*develop the handler*/
void
routes_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
	int32_t strpos = 0;
	uip_ds6_route_t *r;
	volatile uint8_t i;

	size_t len = 0;
	uint8_t index;   //>>>>>>>>>>>>>>>>>>index used while experimenting eg. : GET rplinfo/routes?index=1
	const char *pstr;
	uint8_t count;

	/*Count the number of the routes and return them*/
	count=uip_ds6_route_num_routes();

	if((len = REST.get_query_variable(request, "index", &pstr))) // Parse for the index variable and compare value
	{
		index=(uint8_t)atoi(pstr);

		if(index >= count)
		{
			strpos = snprintf((char *)buffer,preferred_size, "{}"); 
			/*
			 if index is greater or same as count value return empty brackets
			 */
		}else
		{
			/*seek route entry and return it*/
			i = 0;
			for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r), i++)
			{
				if (i == index)
				{
					break;
				}
			}
			strpos = create_route_msg((char *)buffer, r);
		}

		REST.set_header_content_type(response, REST.type.APPLICATION_JSON); // Header will be in JSON format...
	}else
	{
		// index not provided
		strpos += snprintf((char *)buffer, preferred_size, "%d", count);
	}

	//*offset = -1;
	REST.set_response_payload(response, (char *)buffer, strpos); // Setting the payload of response.

}
Beispiel #2
0
void
routes_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
  int32_t strpos = 0;
  uip_ds6_route_t *r;
  volatile uint8_t i;

  size_t len = 0;
  uint8_t index;
  const char *pstr;
  uint8_t count;

  /* count the number of routes and return the total */
  count = uip_ds6_route_num_routes();

  if ((len = REST.get_query_variable(request, "index", &pstr))) {

    index = (uint8_t)atoi(pstr);

    if (index >= count ) {
      strpos = snprintf((char *)buffer, preferred_size, "{}");
    } else {
      /* seek to the route entry and return it */
      i = 0;
      for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r), i++) {
	if (i == index) {
	  break;
	}
      }
      strpos = create_route_msg((char *)buffer, r);
    }

    REST.set_header_content_type(response, APPLICATION_JSON);

  } else { /* index not provided */
    strpos += snprintf((char *)buffer, preferred_size, "%d", count);
  }

  *offset = -1;

  REST.set_response_payload(response, (char *)buffer, strpos);

}