/*---------------------------------------------------------------------------*/ int jsontree_print_next(struct jsontree_context *js_ctx) { struct jsontree_value *v; int index; v = js_ctx->values[js_ctx->depth]; /* Default operation after switch is to back up one level */ switch(v->type) { case JSON_TYPE_OBJECT: case JSON_TYPE_ARRAY: { struct jsontree_array *o = (struct jsontree_array *)v; struct jsontree_value *ov; index = js_ctx->index[js_ctx->depth]; if(index == 0) { js_ctx->putchar(v->type); js_ctx->putchar('\n'); } if(index >= o->count) { js_ctx->putchar('\n'); js_ctx->putchar(v->type + 2); /* Default operation: back up one level! */ break; } if(index > 0) { js_ctx->putchar(','); js_ctx->putchar('\n'); } if(v->type == JSON_TYPE_OBJECT) { jsontree_write_string(js_ctx, ((struct jsontree_object *)o)->pairs[index].name); js_ctx->putchar(':'); ov = ((struct jsontree_object *)o)->pairs[index].value; } else { ov = o->values[index]; } /* TODO check max depth */ js_ctx->depth++; /* step down to value... */ js_ctx->index[js_ctx->depth] = 0; /* and init index */ js_ctx->values[js_ctx->depth] = ov; /* Continue on this new level */ return 1; } case JSON_TYPE_STRING: jsontree_write_string(js_ctx, ((struct jsontree_string *)v)->value); /* Default operation: back up one level! */ break; case JSON_TYPE_INT: jsontree_write_int(js_ctx, ((struct jsontree_int *)v)->value); /* Default operation: back up one level! */ break; case JSON_TYPE_CALLBACK: { /* pre-formatted json string currently */ struct jsontree_callback *callback; callback = (struct jsontree_callback *)v; if(js_ctx->index[js_ctx->depth] == 0) { /* First call: reset the callback status */ js_ctx->callback_state = 0; } if(callback->output == NULL) { jsontree_write_string(js_ctx, ""); } else if(callback->output(js_ctx)) { /* The callback wants to output more */ js_ctx->index[js_ctx->depth]++; return 1; } /* Default operation: back up one level! */ break; } default: PRINTF("\nError: Illegal json type:'%c'\n", v->type); return 0; } /* Done => back up one level! */ if(js_ctx->depth > 0) { js_ctx->depth--; js_ctx->index[js_ctx->depth]++; return 1; } return 0; }
/****************************************************************************** * FunctionName : scan_get * Description : set up the scan data as a JSON format * Parameters : js_ctx -- A pointer to a JSON set up * Returns : result *******************************************************************************/ LOCAL int ICACHE_FLASH_ATTR scanres_get(struct jsontree_context *js_ctx) { const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1); struct user_wifi_scanresult *sr = NULL; if(param.le) sr = param.le->data; if (os_strncmp(path, "id", 2) == 0) { jsontree_write_string(js_ctx, param.id); } else if (os_strncmp(path, "totalpage", 9) == 0) { jsontree_write_int(js_ctx, param.totalpage); } else if (os_strncmp(path, "pagenum", 7) == 0) { jsontree_write_int(js_ctx, param.pagenum); } else if (os_strncmp(path, "bssid", 5) == 0) { jsontree_write_string(js_ctx, sr ? sr->bssid : ""); } else if (os_strncmp(path, "ssid", 4) == 0) { jsontree_write_string(js_ctx, sr ? sr->ssid : ""); } else if (os_strncmp(path, "rssi", 4) == 0) { jsontree_write_int(js_ctx, sr ? -(sr->rssi) : 0); } else if (os_strncmp(path, "authmode", 8) == 0) { if(sr) { struct le *cur = param.le; param.le = cur->next; switch(sr->authmode) { case AUTH_OPEN: jsontree_write_string(js_ctx, "OPEN"); break; case AUTH_WEP: jsontree_write_string(js_ctx, "WEP"); break; case AUTH_WPA_PSK: jsontree_write_string(js_ctx, "WPAPSK"); break; case AUTH_WPA2_PSK: jsontree_write_string(js_ctx, "WPA2PSK"); break; case AUTH_WPA_WPA2_PSK: jsontree_write_string(js_ctx, "WPAPSK/WPA2PSK"); break; default : jsontree_write_int(js_ctx, sr->authmode); break; } } else jsontree_write_string(js_ctx, ""); } return 0; }