int send_graph_data(int fd, DB_ADAPTER * db, char *graph, char *args) { char *p; const char *json; long index = 0; long timespan = 300; // parse arguments if (strtok(args, "?") != NULL) { p = strtok(NULL, "="); while (p != NULL) { if (!strcmp(p, "index")) index = atoi(strtok(NULL, "&")); else if (!strcmp(p, "timespan")) timespan = atoi(strtok(NULL, "&")); p = strtok(NULL, "="); } } json = json_graph_data(db, graph, index, timespan); if (send_json(fd, json) == -1) return -1; return 0; }
TR50_EXPORT int tr50_method_exec_ex_sync(void *tr50, const char *method, JSON *req_params, void *optional_params, void **optional_reply) { int ret; JSON *params = NULL; JSON *reply = NULL; if (method == NULL) { if (req_params) tr50_json_delete(req_params); if (optional_params) tr50_json_delete(optional_params); return ERR_TR50_PARMS; } if (optional_params != NULL) { params = optional_params; } else { if ((params = tr50_json_create_object()) == NULL) { if (req_params) tr50_json_delete(req_params); return ERR_TR50_MALLOC; } } tr50_json_add_string_to_object(params, "method", method); tr50_json_add_item_to_object(params, "params", req_params); if ((ret = send_json(tr50, "method.exec", params, &reply, NULL)) != 0) { if (optional_reply) { *optional_reply = reply; } else if (reply) { tr50_json_delete(reply); } return ret; } return 0; }
int send_averages(int fd, DB_ADAPTER * db) { const char *json; json = json_get_averages(db); if (send_json(fd, json) == -1) return -1; return 0; }
int w_send_json_api(msg_t *_m, char *param1) { int ret = -1; _m->profile_name = param1; LERR("SEND_JSON_API: [%s]\n", param1); ret = send_json(_m); return ret; }
int write_handler(int fd) { json_object *jobj; // Should we assume that even the TCP send's cannot be made when we want ? // In other words is it possible that TCP send's would wait or get stuck ? // If so we cannot use send_json instead improve the logic here -- kmuriki #ifdef WWDEBUG fprintf(stderr, "About to write on FD - %d, type - %d\n", fd, sock_data[fd].ctype); #endif jobj = json_object_new_object(); if (sock_data[fd].ctype == UNKNOWN) { json_object_object_add(jobj, "COMMAND", json_object_new_string("Send Type")); } else if (sock_data[fd].ctype == COLLECTOR) { json_object_object_add(jobj, "COMMAND", json_object_new_string("Send Data")); } else if (sock_data[fd].ctype == APPLICATION) { if (sock_data[fd].sqlite_cmd != NULL) { //printf("SQL cmd - %s\n", sock_data[fd].sqlite_cmd); json_object_object_add(jobj, "JSON_CT", json_object_new_int(0)); sqlite3_exec(db, sock_data[fd].sqlite_cmd, json_from_db, jobj, NULL); //printf("JSON - %s\n",json_object_to_json_string(jobj)); free(sock_data[fd].sqlite_cmd); } else { json_object_object_add(jobj, "COMMAND", json_object_new_string("Send SQL query")); } } send_json(fd, jobj); json_object_put(jobj); //printf("send successful!\n"); FD_CLR(fd, &wfds); FD_SET(fd, &rfds); return 0; }
void push_changes(lua_State *l, const char *base_path, const char *full_path) { struct dirent **dir_list = NULL; struct dirent *dir = NULL; int results; int i; int rv; char *path; if (strncmp(base_path, full_path, strlen(base_path)) != 0) die("wtf? %s != %s len %li", base_path, full_path, strlen(base_path)); gettimeofday(&now, NULL); path = strdup(full_path + strlen(base_path) + 1); /* Skip trailing slash in full_path */ log_debug("relative path is %s", path); scandir_baton_t baton; baton.ig = root_ignores; baton.base_path = base_path; baton.level = 0; results = ds_scandir(full_path, &dir_list, &changed_filter, &baton); if (results == -1) { log_debug("Error scanning directory %s: %s", full_path, strerror(errno)); return; } else if (results == 0) { log_debug("No results found in directory %s", full_path); return; } char *file_path; char *file_path_rel; for (i = 0; i < results; i++) { dir = dir_list[i]; ds_asprintf(&file_path, "%s%s", full_path, dir->d_name); ds_asprintf(&file_path_rel, "%s%s", path, dir->d_name); if (is_ignored(file_path)) { /* we triggered this event */ unignore_change(file_path); goto cleanup; } if (is_directory(full_path, dir)) { /* TODO: figure out if we need to recurse */ goto cleanup; } buf_t *buf = get_buf(file_path_rel); if (buf == NULL) { log_err("buf not found for path %s", file_path_rel); goto cleanup; } const char *f2 = file_path; mmapped_file_t *mf; struct stat file_stats; off_t f2_size; rv = lstat(f2, &file_stats); if (rv) { die("Error lstat()ing file %s.", f2); } f2_size = file_stats.st_size; if (f2_size == 0) { log_debug("%s is empty"); goto cleanup; } mf = mmap_file(f2, f2_size, 0, 0); if (is_binary(mf->buf, mf->len)) { log_debug("%s is binary. skipping", file_path); goto diff_cleanup; } char *new_text = strndup(mf->buf, mf->len); char *patch_text = make_patch(l, buf->buf, new_text); free(new_text); if (strlen(patch_text) == 0) { log_debug("no change. not sending patch"); goto diff_cleanup; } char *md5_after = md5(mf->buf, mf->len); send_json( "{s:s s:i s:s s:s s:s s:s}", "name", "patch", "id", buf->id, "patch", patch_text, "path", buf->path, "md5_before", buf->md5, "md5_after", md5_after ); free(md5_after); free(patch_text); buf->buf = realloc(buf->buf, mf->len + 1); buf->len = mf->len; memcpy(buf->buf, mf->buf, buf->len); diff_cleanup:; munmap_file(mf); free(mf); cleanup:; free(file_path); free(file_path_rel); free(dir); } free(dir_list); free(path); }
void ICACHE_FLASH_ATTR server_recv_cb(void *arg, char *http_raw, unsigned short length) { struct espconn *pespconn = (struct espconn *)arg; //print("[server_recv_cb] Received data:"); char method[10]; char path[60]; char headers[60]; char body[256]; parse_http(http_raw, length, method, path, headers, body); int GET = (os_strcmp(method, "GET") == 0); int POST = (os_strcmp(method, "POST") == 0); if (GET) { // No body if not [post/put/patch]ing // Static files if (os_strcmp(path, "/base.css") == 0) { send_ok(pespconn, base_css); } else if (os_strcmp(path, "/connect.js") == 0) { send_ok(pespconn, connect_js); } else if (os_strcmp(path, "/register.js") == 0) { send_ok(pespconn, register_js); } // JSON responses else if (os_strcmp(path, "/connection.json") == 0) { int station_connect_status = wifi_station_get_connect_status(); if (station_connect_status == STATION_GOT_IP) { struct ip_info ipConfig; wifi_get_ip_info(STATION_IF, &ipConfig); char json_str[54]; os_sprintf(json_str, "{\"status\": \"connected\", \"ip\": \"%d.%d.%d.%d\"}", IP2STR(&ipConfig.ip)); send_json(pespconn, json_str); } else { char *status_str; if (connection_status == CONNECTION_UNCONFIGURED) status_str = "unconfigured"; else switch (station_connect_status) { case STATION_CONNECTING: status_str = "connecting"; break; case STATION_WRONG_PASSWORD: status_str = "failed"; break; case STATION_NO_AP_FOUND: status_str = "failed"; break; case STATION_CONNECT_FAIL: status_str = "failed"; break; } char json_str[54]; os_sprintf(json_str, "{\"status\": \"%s\"}", status_str); send_json(pespconn, json_str); } } else if (os_strcmp(path, "/registration.json") == 0) { char *status_str; switch (registration_status) { case REGISTER_UNREGISTERED: status_str = "unregistered"; break; case REGISTER_REGISTERING: status_str = "registering"; break; case REGISTER_REGISTERED: status_str = "registered"; break; case REGISTER_FAILED: status_str = "failed"; break; } char json_str[54]; os_sprintf(json_str, "{\"status\": \"%s\"}", status_str); send_json(pespconn, json_str); } // HTML pages else if (os_strcmp(path, "/read") == 0) { if (registration_status == REGISTER_REGISTERED) { char temp_json_str[128]; char hum_json_str[128]; measurement_json(temp_json_str, "temperature", "F", last_temp); measurement_json(hum_json_str, "humidity", "%", last_hum); char full_json_str[256] = ""; strcat(full_json_str, temp_json_str); strcat(full_json_str, hum_json_str); full_json_str[os_strlen(temp_json_str)+os_strlen(hum_json_str)] = 0; send_ok_templated(pespconn, full_json_str); } else { send_ok_templated(pespconn, last_unknown); } } else if (os_strcmp(path, "/register") == 0) { send_ok_templated(pespconn, register_html); } else if (os_strcmp(path, "/scan.json") == 0) { char json_str[256] = "["; int si = 0; for (; si < n_scanned; si++) { char json_obj[100]; os_sprintf(json_obj, "{\"ssid\": \"%s\", \"rssi\": %d}", scanned_stations[si], scanned_dbs[si]); os_strcat(json_str, json_obj); if (si < n_scanned - 1) { os_strcat(json_str, ","); } else { os_strcat(json_str, "]"); } } send_json(pespconn, json_str); } else if (os_strcmp(path, "/") == 0) { send_ok_templated(pespconn, index_html); } else { send_404(pespconn); } return; } else if (POST) { // Parse JSON with jsmn jsmn_parser parser; jsmn_init(&parser); jsmntok_t tokens[32]; jsmnerr_t r; r = jsmn_parse(&parser, body, 1024, tokens, 256); if (r < 0) { //print("JSON Parse error?"); return; } // Look for ssid and pass char station_ssid[20]; char station_pass[20]; //print("JSON Parse success?"); if (os_strcmp(path, "/connect.json") == 0) { // Parse ssid and pass from JSON int ti = 0; int has_ssid = 0; int has_pass = 0; int on_ssid = 0; int on_pass = 0; for(; tokens[ti].end; ti++) { char tv[256]; token_string(tv, body, tokens[ti]); if (on_ssid) { //print("Found ssid"); on_ssid = 0; os_strcpy(station_ssid, tv); has_ssid = 1; } if (on_pass) { //print("Found pass"); on_pass = 0; os_strcpy(station_pass, tv); has_pass = 1; if (has_ssid) { break; } } on_ssid = ti % 2 == 1 && os_strcmp(tv, "ssid") == 0; on_pass = ti % 2 == 1 && os_strcmp(tv, "pass") == 0; } //ets_uart_printf("Hopefully ssid=%s and pass=%s\r\n", station_ssid, station_pass); send_ok(pespconn, "<h1>maia</h1><p>OK</p>"); setup_station(station_ssid, station_pass); } else if (os_strcmp(path, "/register.json") == 0) { // Parse email and password from JSON int ti = 0; char user_email[64]; char user_password[64]; int has_email = 0; int has_password = 0; int on_email = 0; int on_password = 0; for(; tokens[ti].end; ti++) { char tv[256]; token_string(tv, body, tokens[ti]); if (on_email) { //print("Found email"); on_email = 0; os_strcpy(user_email, tv); has_email = 1; } if (on_password) { //print("Found password"); on_password = 0; os_strcpy(user_password, tv); has_password = 1; if (has_email) { break; } } on_email = ti % 2 == 1 && os_strcmp(tv, "email") == 0; on_password = ti % 2 == 1 && os_strcmp(tv, "password") == 0; } char register_response[256]; os_sprintf(register_response, "Registering as %d...", DEVICE_ID); send_ok_templated(pespconn, register_response); char register_json[256]; os_sprintf(register_json, "{" "\"device_id\": \"0x%x\"," "\"kind\": \"%s\"," "\"email\": \"%s\"," "\"password\": \"%s\"" "}", DEVICE_ID, DEVICE_KIND, user_email, user_password); registration_status = REGISTER_REGISTERING; post_json(API_BASE "/devices.json", register_json); } else { send_404(pespconn); } return; } send_404(pespconn); return; }
static int tapi2p_callback(struct libwebsocket_context *ctx, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void* user, void* in, size_t len) { switch(reason) { case LWS_CALLBACK_ESTABLISHED: lwsl_notice("Handshaked with client.\n"); libwebsocket_callback_on_writable(ctx, wsi); break; case LWS_CALLBACK_SERVER_WRITEABLE: { if(corefd==-1) { char* conn_reply="Could not connect to tapi2p. Is tapi2p core running?"; websocket_send(wsi, conn_reply, strlen(conn_reply)); } else if(!welcome_message_sent) { struct config* conf=getconfig(); if(conf) { struct configitem* ci=config_find_item(conf, "Nick", "Account"); if(ci && ci->val) { json_t *root=json_object(); json_object_set_new(root, "cmd", json_integer(Message)); json_object_set_new(root, "data", json_string("Welcome to tapi2p, ")); json_object_set_new(root, "own_nick", json_string(ci->val)); char *jsonstr=json_dumps(root, 0); welcome_message_sent=1; websocket_send(wsi, jsonstr, strlen(jsonstr)); free(jsonstr); json_decref(root); lwsl_notice("Sent welcome message.\n"); } } } else if(unsent_data>0) { pthread_mutex_lock(&datalock); while(unsent_data) { char* data=data_to_send[--unsent_data]; printf("Sent: %s\n", data); websocket_send(wsi, data, strlen(data)); free(data); } pthread_mutex_unlock(&datalock); } usleep(10000); libwebsocket_callback_on_writable(ctx, wsi); break; } case LWS_CALLBACK_CLOSED: lwsl_notice("Connection to client closed.\n"); break; case LWS_CALLBACK_RECEIVE: lwsl_notice("Message from client\n"); printf("Json: %s %d\n", (char*)in, len); send_json((char*) in, len); break; } return 0; }