int main() { m2x_context *ctx = NULL; m2x_response response; const char *from = "2015-01-01T01:00:00.000Z"; const char *end = "2016-01-01T01:00:00.000Z"; char buf[2048]; ctx = m2x_open(M2X_KEY); sprintf(buf, "{\"from\": \"%s\", \"end\": \"%s\"}", from, end); printf("Deleting location hisory from: %s end: %s for device: %s\n", from, end, device_id); response = m2x_device_delete_location_history(ctx, device_id, buf); printf("Status code: %d\n", response.status); if (m2x_is_success(&response) && response.raw) { printf("%s\n", response.raw); } m2x_release_response(&response); m2x_close(ctx); return 0; }
int main() { m2x_context ctx; struct timespec last_check_time, current_time; m2x_open(M2X_KEY, &ctx); /* Set up this context for accepting incoming commands */ ctx.keepalive = 1; m2x_mqtt_connect(&ctx); while (1) { m2x_command *command = m2x_next_command(&ctx); /* If no command is available, try fetching from remotely. */ if (!command) if (m2x_fetch_pending_commands(&ctx, device_id)) command = m2x_next_command(&ctx); /* If a command was found, process it. */ if (command) { process_command(&ctx, command); m2x_release_command(&ctx, command); } else sleep(1); /* throttle loop speed */ } m2x_close(&ctx); return 0; }
int main() { m2x_context *ctx = NULL; char buf[2048]; m2x_response response; ctx = m2x_open(M2X_KEY); sprintf(buf, "{\"name\": \"%s\", \"description\": \"%s\", \"tags\": \"%s\", \"visibility\": \"%s\"}", name, description, tags, visibility); printf("Update device %s: %s\n", feed_id, buf); response = m2x_device_update(ctx, feed_id, buf); printf("Status code: %d\n", response.status); if (m2x_is_success(&response) && response.raw) { printf("%s\n", response.raw); } m2x_release_response(&response); m2x_close(ctx); return 0; }
int main() { m2x_context *ctx = NULL; JSON_Value *val = NULL; JSON_Array *feeds = NULL; size_t i; ctx = m2x_open(M2X_KEY); if (m2x_json_feed_list(ctx, "", &val) == 0) { feeds = json_object_get_array(json_value_get_object(val), "feeds"); for (i = 0; i < json_array_get_count(feeds); i++) { JSON_Object* feed = json_array_get_object(feeds, i); printf("Feed id: %s\n", json_object_get_string(feed, "id")); printf(" name: %s\n", json_object_get_string(feed, "name")); printf("Contains %ld streams\n\n", json_array_get_count(json_object_get_array(feed, "streams"))); } json_value_free(val); } m2x_close(ctx); return 0; }
int main() { m2x_context ctx; struct json_token *arr = NULL, *tok = NULL, *tok2 = NULL; int i, len; m2x_response response; char buf[40]; m2x_open(M2X_KEY, &ctx); response = m2x_device_list(&ctx, ""); printf("Response Status Code: %d\n", response.status); if (m2x_is_success(&response)) { arr = (struct json_token *) response.data; printf("Total: %d\n", parse_integer(find_json_token(arr, "body.total"))); printf("Pages: %d\n", parse_integer(find_json_token(arr, "body.pages"))); printf("Limit: %d\n\n", parse_integer(find_json_token(arr, "body.limit"))); tok = find_json_token(arr, "body.devices"); if (tok) { i = 0; while (1) { sprintf(buf, "[%d].id", i); tok2 = find_json_token(tok, buf); if (!tok2) { break; } print_token_as_string("Device ID: ", tok2); sprintf(buf, "[%d].name", i); tok2 = find_json_token(tok, buf); print_token_as_string("Device Name: ", tok2); printf("\n"); i++; } } } m2x_release_response(&ctx, &response); m2x_close(&ctx); return 0; }
int main(int argc, char *argv[]) { char *master_key=0; fp = 0; APPL_TRACE_INFO("Starting with M2x \n"); if (argc < 2) { printf("Correct Usage brcm_m2x_demo <<filename>>\n"); /* exit(1);*/ } else { APPL_TRACE_INFO("Opening File: %s\n", argv[1]); fp = fopen(argv[1], "r"); } if (argc < 2) { APPL_TRACE_INFO("Opening File: /etc/m2x/m2x_key \n" ); fp = fopen("/etc/m2x/m2x_key", "r"); } if (fp) { /* 1.1 Read the key from the file. */ master_key = (char *) malloc(strlen(M2X_KEY) * sizeof (char)); fscanf(fp,"%s",master_key ); APPL_TRACE_INFO("Master Key from file: %s\n", master_key); /*memcpy(master_key,, strlen(M2X_KEY));*/ ctx = m2x_open(master_key); free(master_key); fclose(fp); } else { /* 1.1 Open the Account using Master Key. */ APPL_TRACE_INFO("Error in opening file %s\n", argv[1]); APPL_TRACE_INFO("reading default master key: %s\n", M2X_KEY); ctx = m2x_open(M2X_KEY); } for (i=0; i < MAX_STREAM; i++) { printf("Need to create %d M2X Stream :\"%s\" of size=%d \n", i, stream_name_list[i], strlen(stream_name_list[i])); } /* 1.2 Enable Verbose */ m2x_set_verbose(ctx, 1); /* Create Device and Stream*/ #ifdef RUN_M2X create_device(); create_streams(); #endif /* Open Server Socket */ server_fd = socket_server_open(WICED_M2X_SOCK_PATH); APPL_TRACE_ERROR1("socket_local_client: fd = %d \n", server_fd); if(server_fd > 0) { APPL_TRACE_INFO("socket_local_server: opening server socket successful \n"); } else { APPL_TRACE_ERROR1("socket_local_server: returned error %d \n", errno ); exit(0); } while (1) { FD_ZERO(&socks_rd); FD_SET(server_fd, &socks_rd); max_fd = server_fd; if(read_fd > 0) { FD_SET(read_fd, &socks_rd); if (read_fd > max_fd) max_fd = read_fd; } status = select (max_fd + 1, &socks_rd, (fd_set *) NULL,(fd_set *) NULL,NULL); if(status < 0) { strerror_r(errno, errorstring, sizeof(errorstring)); APPL_TRACE_ERROR1("server: select failed(%s)", errorstring); exit(0); } else { length = 0; if(FD_ISSET(server_fd, &socks_rd)) { read_fd = accept(server_fd, NULL, NULL); if ( read_fd < 0) { strerror_r(errno, errorstring, sizeof(errorstring)); APPL_TRACE_ERROR1("server: accept error %s", errorstring); } else { APPL_TRACE_INFO("server: client connected %d", read_fd); } } /* If there is something to read from the socket */ if (FD_ISSET(read_fd, &socks_rd)) { /* Read length */ status = read(read_fd, (void *)&length, 2); if(status <= 0 ) { strerror_r(errno, errorstring, sizeof(errorstring)); APPL_TRACE_ERROR1("server: read failed(%s)", errorstring); close(read_fd); read_fd = -1; } else { APPL_TRACE_INFO("received length:%d", length); } status = read(read_fd, (UINT8 *) &buf[0], length); if(status <= 0 ) { strerror_r(errno, errorstring, sizeof(errorstring)); APPL_TRACE_ERROR1("server: read failed(%s)", errorstring); close(read_fd); read_fd = -1; } else { printf("Received data: "); for (i=0;i<length;i++) { printf("%c", buf[i]); } printf("\n"); post_to_m2x(length, buf); } } } } close(read_fd); close(server_fd); }