static unsigned char parse_msg(void) { int result = 0; int bytes_available = uip_datalen(); unsigned char* pData = uip_appdata; result = 1; lo_message message = lo_message_deserialise(pData, bytes_available, &result); if (result == 0) { //char* path = lo_url_get_path(lo_address_get_url(lo_message_get_source(message))); lo_arg** argv = lo_message_get_argv(message); //if (!strcmp(path,'/set/rgb')) { lo_arg* red = argv[0]; lo_arg* green = argv[1]; lo_arg* blue = argv[2]; float fRed = red->f; float fGreen = green->f; float fBlue = blue->f; analogWrite(redPin, (unsigned char)(fRed * 0xFF)); analogWrite(greenPin, (unsigned char)(fGreen * 0xFF)); analogWrite(bluePin, (unsigned char)(fBlue * 0xFF)); /*} else if (!strcmp(path, '/set/hsi')) { lo_arg* H = argv[0]; lo_arg* S = argv[1]; lo_arg* I = argv[2]; unsigned char rgb[3]; hsi2rgb(H, S, I, rgb); analogWrite(redPin, rgb[0]); analogWrite(greenPin, rgb[1]); analogWrite(bluePin, rgb[2]); }*/ } lo_message_free(message); s.state = STATE_QUIT; return 1; }
bool port_checker::server::send_msg(const char* address, size_t nargs, const rtosc_arg_val_t* args) { char buffer[2048]; int len = rtosc_avmessage(buffer, sizeof(buffer), address, nargs, args); int res = 0; lo_message msg = lo_message_deserialise(buffer, len, &res); if(msg == nullptr) { std::cout << "liblo error code: " << res << std::endl; throw std::runtime_error("could not deserialize message"); } // std::cout << "send message: " << address << ", args:" << std::endl; // lo_message_pp(msg); res = lo_send_message_from(target, srv, buffer, msg); if(res == -1) throw std::runtime_error("Could not send message"); return true; }
int main() { //clean buffer memset(buffer1, 0xc3, sizeof(buffer1)); //generate liblo message 1 size_t len = 128; lo_message message = lo_message_new(); assert_non_null(message, "Generating A Liblo Message 1 (int/float)", __LINE__); lo_message_add_float(message, 24.0); lo_message_add_int32(message, 42); lo_message_serialise(message, "/path", buffer1, &len); assert_str_eq("/path", buffer1, "Verifying Path From Message 1", __LINE__); assert_f32_eq(24.0f, rtosc_argument(buffer1, 0).f, "Verifying Float From Message 1", __LINE__); assert_int_eq(42, rtosc_argument(buffer1, 1).i, "Verifying Int From Message 1", __LINE__); assert_int_eq(20, rtosc_message_length(buffer1, 128), "Verifying Length From Message 1", __LINE__); //Message 2 size_t len2 = rtosc_message(buffer2, 1024, "/li", "bb", 4, buffer1, 4, buffer1); assert_int_eq(24, len2, "Generate A Rtosc Message 2 (bb)", __LINE__); lo_message msg2 = lo_message_deserialise((void*)buffer2, len2, &result_var); if(assert_non_null(msg2, "Deserialize Message 2 To Liblo", __LINE__)) printf("# Liblo Did Not Accept the Rtosc Message [error '%d']\n", result_var); //Message 3 size_t len3 = rtosc_message(buffer3+4, 2048, "/close-ui", ""); assert_int_eq(16, len3, "Generate A Rtosc Message 3 ()", __LINE__); lo_message msg3 = lo_message_deserialise((void*)(buffer3+4), len3, &result_var); if(assert_non_null(msg2, "Deserialize Message 3 To Liblo", __LINE__)) printf("#Liblo Did Not Accept the Rtosc Message [error '%d']\n", result_var); //Bundle 4 size_t len4 = rtosc_bundle(buffer4, 2048, 0xdeadbeefcafebaad, 3, buffer1, buffer2, buffer3+4); assert_int_eq(88, len4, "Generate A Bundle 4", __LINE__); //Bundle 5 lo_timetag time; time.sec = 0xdeadbeef; time.frac = 0xcafebaad; lo_bundle ms4 = lo_bundle_new(time); lo_bundle_add_message(ms4, "/path", message); lo_bundle_add_message(ms4, "/li", msg2); lo_bundle_add_message(ms4, "/close-ui", msg3); size_t len5 = 2048; lo_bundle_serialise(ms4,(void*)buffer5, &len5); //Verify 4 == 5 assert_non_null(ms4, "Generate A Liblo Bundle 5", __LINE__); assert_hex_eq(buffer5, buffer4, len5, len4, "Verify Liblo Style Bundles", __LINE__); //Cleanup lo_message_free(message); lo_message_free(msg2); lo_message_free(msg3); lo_bundle_free(ms4); return test_summary(); }
void test_deserialise() { char *buf, *buf2, *tmp; const char *types = NULL, *path; lo_arg **argv = NULL; size_t len, size; char data[256]; int result = 0; lo_blob btest = lo_blob_new(sizeof(testdata), testdata); uint8_t midi_data[4] = {0xff, 0xf7, 0xAA, 0x00}; lo_timetag tt = {0x1, 0x80000000}; lo_blob b = NULL; // build a message lo_message msg = lo_message_new(); TEST(0 == lo_message_get_argc(msg)); lo_message_add_float(msg, 0.12345678f); // 0 f lo_message_add_int32(msg, 123); // 1 i lo_message_add_string(msg, "123"); // 2 s lo_message_add_blob(msg, btest); // 3 b lo_message_add_midi(msg, midi_data); // 4 m lo_message_add_int64(msg, 0x0123456789abcdefULL); // 5 h lo_message_add_timetag(msg, tt); // 6 t lo_message_add_double(msg, 0.9999); // 7 d lo_message_add_symbol(msg, "sym"); // 8 S lo_message_add_char(msg, 'X'); // 9 c lo_message_add_char(msg, 'Y'); // 10 c lo_message_add_true(msg); // 11 T lo_message_add_false(msg); // 12 F lo_message_add_nil(msg); // 13 N lo_message_add_infinitum(msg); // 14 I // test types, args TEST(15 == lo_message_get_argc(msg)); types = lo_message_get_types(msg); TEST(NULL != types); argv = lo_message_get_argv(msg); TEST(NULL != argv); TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON); TEST('i' == types[1] && 123 == argv[1]->i); TEST('s' == types[2] && !strcmp(&argv[2]->s, "123")); TEST('b' == types[3]); b = (lo_blob)argv[3]; TEST(lo_blob_datasize(b) == sizeof(testdata)); TEST(12 == lo_blobsize(b)); TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata))); TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4)); TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h); TEST('t' == types[6] && 1 == argv[6]->t.sec && 0x80000000 == argv[6]->t.frac); TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON); TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym")); TEST('c' == types[9] && 'X' == argv[9]->c); TEST('c' == types[10] && 'Y' == argv[10]->c); TEST('T' == types[11] && NULL == argv[11]); TEST('F' == types[12] && NULL == argv[12]); TEST('N' == types[13] && NULL == argv[13]); TEST('I' == types[14] && NULL == argv[14]); // serialise it len = lo_message_length(msg, "/foo"); printf("serialise message_length=%d\n", (int)len); buf = calloc(len, sizeof(char)); size = 0; tmp = lo_message_serialise(msg, "/foo", buf, &size); TEST(tmp == buf && size == len && 92 == len); lo_message_free(msg); // deserialise it printf("deserialise\n"); path = lo_get_path(buf, len); TEST(NULL != path && !strcmp(path, "/foo")); msg = lo_message_deserialise(buf, size, NULL); TEST(NULL != msg); // repeat same test as above TEST(15 == lo_message_get_argc(msg)); types = lo_message_get_types(msg); TEST(NULL != types); argv = lo_message_get_argv(msg); TEST(NULL != argv); TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON); TEST('i' == types[1] && 123 == argv[1]->i); TEST('s' == types[2] && !strcmp(&argv[2]->s, "123")); TEST('b' == types[3]); b = (lo_blob)argv[3]; TEST(lo_blob_datasize(b) == sizeof(testdata)); TEST(12 == lo_blobsize(b)); TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata))); TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4)); TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h); TEST('t' == types[6] && 1 == argv[6]->t.sec && 0x80000000 == argv[6]->t.frac); TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON); TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym")); TEST('c' == types[9] && 'X' == argv[9]->c); TEST('c' == types[10] && 'Y' == argv[10]->c); TEST('T' == types[11] && NULL == argv[11]); TEST('F' == types[12] && NULL == argv[12]); TEST('N' == types[13] && NULL == argv[13]); TEST('I' == types[14] && NULL == argv[14]); // serialise it again, compare len = lo_message_length(msg, "/foo"); printf("serialise message_length=%d\n", (int)len); buf2 = calloc(len, sizeof(char)); size = 0; tmp = lo_message_serialise(msg, "/foo", buf2, &size); TEST(tmp == buf2 && size == len && 92 == len); TEST(!memcmp(buf, buf2, len)); lo_message_free(msg); lo_blob_free(btest); free(buf); free(buf2); // deserialise failure tests with invalid message data msg = lo_message_deserialise(data, 0, &result); // 0 size TEST(NULL == msg && LO_ESIZE == result); snprintf(data, 256, "%s", "/foo"); // unterminated path string msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_EINVALIDPATH == result); snprintf(data, 256, "%s", "/f_o"); // non-0 in pad area msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_EINVALIDPATH == result); snprintf(data, 256, "%s", "/t__"); // types missing replace_char(data, 4, '_', '\0'); msg = lo_message_deserialise(data, 4, &result); TEST(NULL == msg && LO_ENOTYPE == result); snprintf(data, 256, "%s%s", "/t__", "____"); // types empty replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EBADTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",f_"); // short message replace_char(data, 7, '_', '\0'); msg = lo_message_deserialise(data, 7, &result); TEST(NULL == msg && LO_EINVALIDTYPE == result); snprintf(data, 256, "%s%s", "/t__", "ifi_"); // types missing comma replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EBADTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",ifi"); // types unterminated replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 8, &result); TEST(NULL == msg && LO_EINVALIDTYPE == result); snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 12, &result); TEST(NULL == msg && LO_EINVALIDARG == result); snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data again replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 15, &result); TEST(NULL == msg && LO_EINVALIDARG == result); snprintf(data, 256, "%s%s", "/t__", ",f__"); // too much arg data replace_char(data, 8, '_', '\0'); msg = lo_message_deserialise(data, 16, &result); TEST(NULL == msg && LO_ESIZE == result); snprintf(data, 256, "%s%s", "/t__", ",bs_"); // blob longer than msg length replace_char(data, 8, '_', '\0'); *(uint32_t *)(data + 8) = lo_htoo32((uint32_t)99999); msg = lo_message_deserialise(data, 256, &result); TEST(NULL == msg && LO_EINVALIDARG == result); }
int lo_server_dispatch_data(lo_server s, void *data, size_t size) { int result = 0; char *path = data; ssize_t len = lo_validate_string(data, size); if (len < 0) { lo_throw(s, -len, "Invalid message path", NULL); return len; } if (!strcmp(data, "#bundle")) { char *pos; int remain; uint32_t elem_len; lo_timetag ts, now; ssize_t bundle_result = lo_validate_bundle(data, size); if (bundle_result < 0) { lo_throw(s, -bundle_result, "Invalid bundle", NULL); return bundle_result; } pos = (char *)data + len; remain = size - len; lo_timetag_now(&now); ts.sec = lo_otoh32(*((uint32_t *)pos)); pos += 4; ts.frac = lo_otoh32(*((uint32_t *)pos)); pos += 4; remain -= 8; while (remain >= 4) { lo_message msg; elem_len = lo_otoh32(*((uint32_t *)pos)); pos += 4; remain -= 4; msg = lo_message_deserialise(pos, elem_len, &result); if (!msg) { lo_throw(s, result, "Invalid bundle element received", path); return -result; } // set timetag from bundle msg->ts = ts; // test for immediate dispatch if ((ts.sec == LO_TT_IMMEDIATE.sec && ts.frac == LO_TT_IMMEDIATE.frac) || lo_timetag_diff(ts, now) <= 0.0) { dispatch_method(s, pos, msg); lo_message_free(msg); } else { queue_data(s, ts, pos, msg); } pos += elem_len; remain -= elem_len; } } else { lo_message msg = lo_message_deserialise(data, size, &result); if (NULL == msg) { lo_throw(s, result, "Invalid message received", path); return -result; } dispatch_method(s, data, msg); lo_message_free(msg); } return size; }