static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { rpl_dag_t *dag; rpl_parent_t *parent; int32_t strpos = 0; const uip_ipaddr_t *addr; addr = &uip_ds6_if.addr_list[1].ipaddr; dag = rpl_get_any_dag(); parent_index = 0; if (dag != NULL) { /* seek to the parents entry and return it */ strpos += sprintf(&(buffer[strpos]),"{\"node\":\"n%x\"",addr->u8[15]); // last addr byte of mote strpos += sprintf(&(buffer[strpos]),",\"nbr\":{"); parent = nbr_table_head(rpl_parents); // addr of first neighbor while (parent != NULL) { etx_table[parent_index].nbr_addr = rpl_get_parent_ipaddr(parent)->u8[15]; etx_table[parent_index].nbr_etx = rpl_get_parent_link_metric(parent); etx_table[parent_index].p = parent; strpos += sprintf(&(buffer[strpos]),"\"n%x\":%u,",etx_table[parent_index].nbr_addr, etx_table[parent_index].nbr_etx); parent = nbr_table_next(rpl_parents, parent); parent_index++; } PRINTF("parent_index:%d\n",parent_index); } else { /* no DAG */ strpos += sprintf(&(buffer[strpos]),"{}\n"); } //PRINTF("strpos: %ld\n", strpos); //rpl_print_neighbor_list(); // get parents for debug purposes //PRINTF("buf_parents: %s\n", buffer); strpos += sprintf(&(buffer[strpos-1]),"}}\n"); //replace the last comma //PRINTF("strpos-after: %ld\n", strpos); REST.set_header_content_type(response, APPLICATION_JSON); REST.set_header_max_age(response, res_etx.periodic->period / CLOCK_SECOND); //*offset = -1; // try to fix Copper response REST.set_response_payload(response, buffer, snprintf((char *)buffer, preferred_size, "%s", buffer)); /* The REST.subscription_handler() will be called for observable resources by the REST framework. */ }
/* length of an neighbor entry, must be fixed width */ uint16_t create_parent_msg(char *buf, rpl_parent_t *parent, uint8_t preferred) { uint8_t n = 0; uip_ipaddr_t * addr = rpl_get_parent_ipaddr(parent); n += sprintf(&(buf[n]), "{\"eui\":\"%04x%04x%04x%04x\",", UIP_HTONS(addr->u16[4]), UIP_HTONS(addr->u16[5]), UIP_HTONS(addr->u16[6]), UIP_HTONS(addr->u16[7])); n += sprintf(&(buf[n]), "\"pref\":"); if(preferred == 1) { n += sprintf(&(buf[n]), "true,"); } else { n += sprintf(&(buf[n]), "false,"); } n += sprintf(&(buf[n]), "\"etx\":%d}", rpl_get_parent_link_metric(parent)); buf[n] = 0; PRINTF("buf: %s\n", buf); return n; }
/* * Additionally, a handler function named [resource name]_handler must be implemented for each PERIODIC_RESOURCE. * It will be called by the REST manager process with the defined period. */ static void res_periodic_handler() { uint8_t parent_counter = 0; uint16_t etx_temp; uint8_t etx_changed = 0; while(parent_counter < parent_index) { etx_temp = rpl_get_parent_link_metric(etx_table[parent_counter].p); PRINTF("etx_temp:%d\n",etx_temp); if(etx_temp > etx_table[parent_counter].nbr_etx * 2 || etx_temp < etx_table[parent_counter].nbr_etx / 2 ) { etx_table[parent_counter].nbr_etx = etx_temp ; etx_changed = 1; } parent_counter++; } /* Notify the registered observers which will trigger the res_get_handler to create the response. */ if (etx_changed) { PRINTF("etx_changed !\n"); REST.notify_subscribers(&res_etx); } }