int setup_sources() { int i, mni=0, mxi=1; sources = (mapper_device*)calloc(1, num_sources * sizeof(mapper_device)); sendsigs = (mapper_signal*)calloc(1, num_sources * sizeof(mapper_signal)); for (i = 0; i < num_sources; i++) { sources[i] = mapper_device_new("testconvergent-send", 0, 0); if (!sources[i]) goto error; sendsigs[i] = mapper_device_add_output_signal(sources[i], "sendsig", 1, 'i', 0, &mni, &mxi); if (!sendsigs[i]) goto error; eprintf("source %d created.\n", i); } return 0; error: for (i = 0; i < num_sources; i++) { if (sources[i]) mapper_device_free(sources[i]); } return 1; }
void cleanup_destination() { if (destination) { eprintf("Freeing destination.. "); fflush(stdout); mapper_device_free(destination); eprintf("ok\n"); } }
void cleanup_source() { if (source) { eprintf("Freeing source.. "); fflush(stdout); mapper_device_free(source); eprintf("ok\n"); } }
void cleanup_source() { for (int i = 0; i < num_sources; i++) { if (sources[i]) { eprintf("Freeing source %d... ", i); fflush(stdout); mapper_device_free(sources[i]); eprintf("ok\n"); } } free(sources); free(sendsigs); }
int test_network() { int error = 0, wait; net = mapper_network_new(0, 0, 0); if (!net) { eprintf("Error creating network structure.\n"); return 1; } eprintf("Network structure initialized.\n"); dev = mapper_device_new("tester", 0, net); if (!dev) { eprintf("Error creating device structure.\n"); return 1; } eprintf("Device structure initialized.\n"); eprintf("Found interface %s has IP %s\n", net->interface_name, inet_ntoa(net->interface_ip)); while (!dev->local->registered) { usleep(10000); mapper_network_poll(net, 1); } eprintf("Using port %d.\n", mapper_device_port(dev)); eprintf("Allocated ordinal %d.\n", dev->local->ordinal.value); eprintf("Delaying for 5 seconds..\n"); wait = 50; while (wait-- > 0) { usleep(50000); mapper_network_poll(net, 1); if (!verbose) { printf("."); fflush(stdout); } } mapper_device_free(dev); eprintf("Device structure freed.\n"); mapper_network_free(net); eprintf("Network structure freed.\n"); return error; }
int main(int argc, char **argv) { int i, j, seen, result = 0; // process flags for -v verbose, -h help for (i = 1; i < argc; i++) { if (argv[i] && argv[i][0] == '-') { int len = strlen(argv[i]); for (j = 1; j < len; j++) { switch (argv[i][j]) { case 'h': eprintf("testprops.c: possible arguments " "-q quiet (suppress output), " "-h help\n"); return 1; break; case 'q': verbose = 0; break; default: break; } } } } mapper_device dev = mapper_device_new("testprops", 0, 0); mapper_signal sig = mapper_device_add_input_signal(dev, "test", 1, 'f', "Hz", 0, 0, 0, 0); while (!mapper_device_ready(dev)) { mapper_device_poll(dev, 100); } /* Test that default parameters are all listed. */ eprintf("Test 1: checking default parameters... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding maximum causes it to be listed. */ float mx = 35.0; mapper_signal_set_maximum(sig, &mx); eprintf("Test 2: adding static property 'maximum'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding an extra parameter causes the extra parameter * to be listed. */ char *str = "test_value"; mapper_signal_set_property(sig, "test", 1, 's', str, 1); eprintf("Test 3: adding extra string property 'test'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 4: retrieving property 'test'... "); char type; const void *val; int length; if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("ERROR\n"); result = 1; goto cleanup; } eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected %c)\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%s' ... ", (char*)val); if (strcmp((char*)val, str)) { eprintf("ERROR (expected '%s')\n", str); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that removing an extra parameter causes the extra * parameter to _not_ be listed. */ mapper_signal_remove_property(sig, "test"); eprintf("Test 5: removing extra property 'test'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding two more properties works as expected. */ int x = 123; mapper_signal_set_property(sig, "x", 1, 'i', &x, 1); int y = 234; mapper_signal_set_property(sig, "y", 1, 'i', &y, 1); eprintf("Test 6: adding extra integer properties 'x' and 'y'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX | SEEN_X | SEEN_Y)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test the type and value associated with "x". */ eprintf("Test 7: retrieving property 'x'..."); if (mapper_signal_property(sig, "x", &length, &type, &val)) { eprintf("ERROR\n"); result = 1; goto cleanup; } eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: %i ... ", *(int*)val); if (*(int*)val != 123) { eprintf("ERROR (expected %d)\n", 123); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is no value associated with previously-removed * "test". */ eprintf("Test 8: retrieving removed property 'test': "); if (!mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("found... ERROR\n"); result = 1; goto cleanup; } else eprintf("not found... OK\n"); /* Check that there is an integer value associated with static, * required property "length". */ eprintf("Test 9: retrieving static, required property 'length'... "); if (mapper_signal_property(sig, "length", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%d' ... ", *(int*)val); if (*(int*)val != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is a string value associated with static, * required property "name". */ eprintf("Test 10: retrieving static, required property 'name'... "); if (mapper_signal_property(sig, "name", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected %c)\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%s' ... ", (char*)val); if (strcmp((char*)val, "test")) { eprintf("ERROR (expected '%s')\n", str); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is a float value associated with static, * optional property "max". */ eprintf("Test 11: retrieving static, optional property 'max'... "); if (mapper_signal_property(sig, "max", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'f') { eprintf("ERROR (expected %c)\n", 'f'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%f' ... ", *(float*)val); if (*(float*)val != 35.0f) { eprintf("ERROR (expected %f)\n", *(float*)val); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that removing maximum causes it to _not_ be listed. */ mapper_signal_set_maximum(sig, 0); eprintf("Test 12: removing optional property 'max'... "); seen = check_keys(sig); if (seen & SEEN_MAX) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 13: retrieving optional property 'max': "); if (!mapper_signal_property(sig, "max", &length, &type, &val)) { eprintf("found... ERROR\n"); result = 1; goto cleanup; } else eprintf("not found... OK\n"); /* Test adding and retrieving an integer vector property. */ eprintf("Test 14: adding an extra integer vector property 'test'... "); int set_int[] = {1, 2, 3, 4, 5}; mapper_signal_set_property(sig, "test", 5, 'i', &set_int, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 15: retrieving vector property 'test': "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 5) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); int *read_int = (int*)val; int matched = 0; eprintf("\t checking value: [%i,%i,%i,%i,%i] ... ", read_int[0], read_int[1], read_int[2], read_int[3], read_int[4]); for (i = 0; i < 5; i++) { if (read_int[i] == set_int[i]) matched++; } if (matched != 5) { eprintf("ERROR (expected [%i,%i,%i,%i,%i])\n", set_int[0], set_int[1], set_int[2], set_int[3], set_int[4]); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test rewriting 'test' as float vector property. */ eprintf("Test 16: rewriting 'test' as vector float property... "); float set_float[] = {10., 20., 30., 40., 50.}; mapper_signal_set_property(sig, "test", 5, 'f', &set_float, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 17: retrieving property 'test'... "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'f') { eprintf("ERROR (expected '%c')\n", 'f'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %i ... ", length); if (length != 5) { eprintf("ERROR (expected %d)\n", length); result = 1; goto cleanup; } else eprintf("OK\n"); float *read_float = (float*)val; eprintf("\t checking value: [%f,%f,%f,%f,%f] ... ", read_float[0], read_float[1], read_float[2], read_float[3], read_float[4]); matched = 0; for (i = 0; i < 5; i++) { if (read_float[i] == set_float[i]) matched++; } if (matched != 5) { eprintf("ERROR (expected [%f,%f,%f,%f,%f]\n", set_float[0], set_float[1], set_float[2], set_float[3], set_float[4]); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test rewriting property 'test' as string vector property. */ eprintf("Test 18: rewriting 'test' as vector string property... "); char *set_string[] = {"foo", "bar"}; mapper_signal_set_property(sig, "test", 2, 's', &set_string, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 19: retrieving property 'test'... "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected '%c')\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ...", length); if (length != 2) { eprintf("ERROR (expected %d)\n", 2); result = 1; goto cleanup; } else eprintf("OK\n"); char **read_string = (char**)val; eprintf("\t checking value: ['%s','%s'] ... ", read_string[0], read_string[1]); matched = 0; for (i = 0; i < 2; i++) { if (read_string[i] && strcmp(read_string[i], set_string[i]) == 0) matched++; } if (matched != 2) { eprintf("ERROR (expected ['%s','%s'])\n", set_string[0], set_string[1]); result = 1; goto cleanup; } else eprintf("OK\n"); cleanup: if (dev) mapper_device_free(dev); if (!verbose) printf(".................................................."); printf("Test %s.\n", result ? "FAILED" : "PASSED"); return result; }
// ********************************************************* // -(new)--------------------------------------------------- void *impmap_new(t_symbol *s, int argc, t_atom *argv) { impmap *x = NULL; long i; const char *alias = NULL; const char *iface = NULL; #ifdef MAXMSP if ((x = object_alloc(mapper_class))) { x->outlet3 = listout((t_object *)x); x->outlet2 = listout((t_object *)x); x->outlet1 = listout((t_object *)x); #else if (x = (impmap *) pd_new(mapper_class) ) { x->outlet1 = outlet_new(&x->ob, gensym("list")); x->outlet2 = outlet_new(&x->ob, gensym("list")); x->outlet3 = outlet_new(&x->ob, gensym("list")); #endif x->name = strdup("implicitmap"); for (i = 0; i < argc; i++) { if ((argv + i)->a_type == A_SYM) { if(strcmp(maxpd_atom_get_string(argv+i), "@alias") == 0) { if ((argv+i+1)->a_type == A_SYM) { alias = maxpd_atom_get_string(argv+i+1); i++; } } else if(strcmp(maxpd_atom_get_string(argv+i), "@interface") == 0) { if ((argv+i+1)->a_type == A_SYM) { iface = maxpd_atom_get_string(argv+i+1); i++; } } } } if (alias) { free(x->name); x->name = *alias == '/' ? strdup(alias+1) : strdup(alias); } if (impmap_setup_mapper(x, iface)) { post("implicitmap: Error initializing."); } else { x->ready = 0; x->mute = 0; x->new_in = 0; x->query_count = 0; x->num_snapshots = 0; x->snapshots = 0; // initialize input and output buffers for (i = 0; i < MAX_LIST; i++) { maxpd_atom_set_float(x->buffer_in+i, 0); maxpd_atom_set_float(x->buffer_out+i, 0); x->signals_in[i].x = x; x->signals_out[i].x = x; } x->size_in = 0; x->size_out = 0; #ifdef MAXMSP x->clock = clock_new(x, (method)impmap_poll); // Create the timing clock x->timeout = clock_new(x, (method)impmap_output_snapshot); #else x->clock = clock_new(x, (t_method)impmap_poll); x->timeout = clock_new(x, (t_method)impmap_output_snapshot); #endif clock_delay(x->clock, INTERVAL); // Set clock to go off after delay } } return (x); } // ********************************************************* // -(free)-------------------------------------------------- void impmap_free(impmap *x) { if (x->clock) { clock_unset(x->clock); // Remove clock routine from the scheduler clock_free(x->clock); // Frees memory used by clock } if (x->device) { mapper_device_free(x->device); } if (x->name) { free(x->name); } impmap_clear_snapshots(x); } // ********************************************************* // -(print properties)-------------------------------------- void impmap_print_properties(impmap *x) { if (x->ready) { //output name maxpd_atom_set_string(&x->msg_buffer, mapper_device_name(x->device)); outlet_anything(x->outlet3, gensym("name"), 1, &x->msg_buffer); mapper_network net = mapper_device_network(x->device); //output interface maxpd_atom_set_string(&x->msg_buffer, (char *)mapper_network_interface(net)); outlet_anything(x->outlet3, gensym("interface"), 1, &x->msg_buffer); //output IP const struct in_addr *ip = mapper_network_ip4(net); if (ip) { maxpd_atom_set_string(&x->msg_buffer, inet_ntoa(*ip)); outlet_anything(x->outlet3, gensym("IP"), 1, &x->msg_buffer); } //output port maxpd_atom_set_int(&x->msg_buffer, mapper_device_port(x->device)); outlet_anything(x->outlet3, gensym("port"), 1, &x->msg_buffer); //output numInputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) - 1); outlet_anything(x->outlet3, gensym("numInputs"), 1, &x->msg_buffer); //output numOutputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) - 1); outlet_anything(x->outlet3, gensym("numOutputs"), 1, &x->msg_buffer); } } // ********************************************************* // -(inlet/outlet assist - maxmsp only)--------------------- #ifdef MAXMSP void impmap_assist(impmap *x, void *b, long m, long a, char *s) { if (m == ASSIST_INLET) { // inlet sprintf(s, "OSC input"); } else { // outlet if (a == 0) { sprintf(s, "Mapped OSC inputs"); } else if (a == 1) { sprintf(s, "Snapshot data"); } else { sprintf(s, "Device information"); } } } #endif // ********************************************************* // -(snapshot)---------------------------------------------- void impmap_snapshot(impmap *x) { // if previous snapshot still in progress, output current snapshot status if (x->query_count) { post("still waiting for last snapshot"); return; } mapper_signal *psig; x->query_count = 0; // allocate a new snapshot if (x->ready) { t_snapshot new_snapshot = (t_snapshot)malloc(sizeof(t_snapshot)); new_snapshot->id = x->num_snapshots++; new_snapshot->next = x->snapshots; new_snapshot->inputs = calloc(x->size_in, sizeof(float)); new_snapshot->outputs = calloc(x->size_out, sizeof(float)); x->snapshots = new_snapshot; } // iterate through input signals and store their current values psig = mapper_device_signals(x->device, MAPPER_DIR_INCOMING); while (psig) { if (*psig != x->dummy_input) { void *value = (void*)mapper_signal_value(*psig, 0); t_signal_ref *ref = mapper_signal_user_data(*psig); int siglength = mapper_signal_length(*psig); int length = ref->offset + siglength < MAX_LIST ? siglength : MAX_LIST - ref->offset; // we can simply use memcpy here since all our signals are type 'f' memcpy(&x->snapshots->inputs[ref->offset], value, length * sizeof(float)); } psig = mapper_signal_query_next(psig); } mapper_timetag_now(&x->tt); mapper_device_start_queue(x->device, x->tt); // iterate through output signals and query the remote ends psig = mapper_device_signals(x->device, MAPPER_DIR_OUTGOING); while (psig) { if (*psig != x->dummy_output) { // query the remote value x->query_count += mapper_signal_query_remotes(*psig, MAPPER_NOW); } psig = mapper_signal_query_next(psig); } mapper_device_send_queue(x->device, x->tt); post("sent %i queries", x->query_count); if (x->query_count) clock_delay(x->timeout, 1000); // Set clock to go off after delay } // ********************************************************* // -(snapshot)---------------------------------------------- void impmap_output_snapshot(impmap *x) { if (x->query_count) { post("query timeout! setting query count to 0 and outputting current values."); x->query_count = 0; } maxpd_atom_set_int(x->buffer_in, x->snapshots->id+1); outlet_anything(x->outlet3, gensym("numSnapshots"), 1, x->buffer_in); maxpd_atom_set_float_array(x->buffer_in, x->snapshots->inputs, x->size_in); outlet_anything(x->outlet2, gensym("in"), x->size_in, x->buffer_in); maxpd_atom_set_float_array(x->buffer_out, x->snapshots->outputs, x->size_out); outlet_anything(x->outlet2, gensym("out"), x->size_out, x->buffer_out); maxpd_atom_set_int(x->buffer_in, x->snapshots->id); outlet_anything(x->outlet2, gensym("snapshot"), 1, x->buffer_in); } // ********************************************************* // -(mute output)------------------------------------------- void impmap_mute_output(impmap *x, t_symbol *s, int argc, t_atom *argv) { if (argc) { if (argv->a_type == A_FLOAT) x->mute = (int)atom_getfloat(argv); #ifdef MAXMSP else if (argv->a_type == A_LONG) x->mute = atom_getlong(argv); #endif } } // ********************************************************* // -(process)----------------------------------------------- void impmap_process(impmap *x) { outlet_anything(x->outlet2, gensym("process"), 0, 0); }