int ext_send_encoded_msg(int rx_chan, bool debug, const char *dst, const char *cmd, const char *fmt, ...) { va_list ap; char *s; if (cmd == NULL || fmt == NULL) return 0; conn_t *conn = ext_users[rx_chan].conn; if (!conn || !conn->mc) return -1; va_start(ap, fmt); vasprintf(&s, fmt, ap); va_end(ap); char *buf = str_encode(s); free(s); ext_send_msg(rx_chan, debug, "%s %s=%s", dst, cmd, buf); free(buf); return 0; }
bool navtex_msgs(char *msg, int rx_chan) { navtex_t *e = &navtex[rx_chan]; int n; //printf("### navtex_msgs RX%d <%s>\n", rx_chan, msg); if (strcmp(msg, "SET ext_server_init") == 0) { e->rx_chan = rx_chan; // remember our receiver channel number ext_send_msg(e->rx_chan, DEBUG_MSG, "EXT ready"); return true; } n = sscanf(msg, "SET run=%d", &e->run); if (n == 1) { return true; } return false; }
bool s4285_msgs(char *msg, int rx_chan) { s4285_t *e = &s4285[rx_chan]; int n; printf("### s4285_msgs RX%d <%s>\n", rx_chan, msg); if (strcmp(msg, "SET ext_server_init") == 0) { e->rx_chan = rx_chan; // remember our receiver channel number ext_send_msg(e->rx_chan, S4285_DEBUG_MSG, "EXT ready"); return true; } n = sscanf(msg, "SET run=%d", &e->run); if (n == 1) { if (e->run) { if (!e->rx_task) { e->rx_task = CreateTask(s4285_rx, 0, EXT_PRIORITY); } m_CSt4285[rx_chan].reset(); //m_CSt4285[rx_chan].setSampleRate(ext_get_sample_rateHz()); m_CSt4285[rx_chan].registerRxCallback(s4285_rx_callback, rx_chan); m_CSt4285[rx_chan].registerTxCallback(s4285_tx_callback); //m_CSt4285[rx_chan].control((void *) "SET MODE 600L", NULL, 0); //ext_register_receive_iq_samps(s4285_data, rx_chan); ext_register_receive_real_samps(s4285_data, rx_chan); } else { ext_unregister_receive_iq_samps(rx_chan); if (e->rx_task) { TaskRemove(e->rx_task); e->rx_task = 0; } } if (e->points == 0) e->points = 128; return true; } n = sscanf(msg, "SET mode=%d", &e->mode); if (n == 1) { return true; } int gain; n = sscanf(msg, "SET gain=%d", &gain); if (n == 1) { // 0 .. +100 dB of S4285_MAX_VAL e->gain = gain? pow(10.0, ((float) -gain) / 10.0) : 0; printf("e->gain %.6f\n", e->gain); return true; } int points; n = sscanf(msg, "SET points=%d", &points); if (n == 1) { e->points = points; printf("points %d\n", points); return true; } int draw; n = sscanf(msg, "SET draw=%d", &draw); if (n == 1) { e->draw = draw; printf("draw %d\n", draw); return true; } n = strcmp(msg, "SET clear"); if (n == 0) { e->cma = e->ncma = 0; return true; } return false; }
void extint_w2a(void *param) { int n, i, j; conn_t *conn = (conn_t *) param; u4_t ka_time = timer_sec(); // initialize extension for this connection send_msg_mc(conn->mc, false, "EXT ext_client_init"); nbuf_t *nb = NULL; while (TRUE) { int rx_chan, ext_rx_chan; ext_t *ext; if (nb) web_to_app_done(conn, nb); n = web_to_app(conn, &nb); if (n) { char *cmd = nb->buf; cmd[n] = 0; // okay to do this -- see nbuf.c:nbuf_allocq() char id[64]; ka_time = timer_sec(); // receive and send a roundtrip keepalive i = strcmp(cmd, "SET keepalive"); if (i == 0) { ext_send_msg(conn->ext_rx_chan, false, "EXT keepalive"); continue; } ext_rx_chan = conn->ext_rx_chan; //printf("extint_w2a: %s CONN%d-%p RX%d-%p %d <%s>\n", conn->ext? conn->ext->name:"?", conn->self_idx, conn, ext_rx_chan, (ext_rx_chan == -1)? 0:ext_users[ext_rx_chan].conn, strlen(cmd), cmd); // answer from client ext about who they are // match against list of known extensions and register msg handler char client[32]; int first_time; i = sscanf(cmd, "SET ext_switch_to_client=%s first_time=%d rx_chan=%d", client, &first_time, &rx_chan); if (i == 3) { for (i=0; i < n_exts; i++) { ext = ext_list[i]; if (strcmp(client, ext->name) == 0) { //printf("ext_switch_to_client: found func %p CONN%d-%p for ext %s RX%d\n", ext->receive_msgs, conn->self_idx, conn, client, rx_chan); ext_users[rx_chan].ext = ext; ext_users[rx_chan].conn = conn; conn->ext_rx_chan = rx_chan; conn->ext = ext; TaskNameS(ext->name); break; } } if (i == n_exts) panic("ext_switch_to_client: unknown ext"); // automatically let extension server-side know the connection has been established and // our stream thread is running if (first_time) ext->receive_msgs((char *) "SET ext_server_init", rx_chan); continue; } i = sscanf(cmd, "SET ext_blur=%d", &rx_chan); if (i == 1) { extint_ext_users_init(rx_chan); continue; } i = strcmp(cmd, "SET init"); if (i == 0) { continue; } i = sscanf(cmd, "SERVER DE CLIENT %s", id); if (i == 1) { continue; } ext_rx_chan = conn->ext_rx_chan; if (ext_rx_chan == -1) { printf("### extint_w2a: %s CONN%d-%p ext_rx_chan == -1?\n", conn->ext? conn->ext->name:"?", conn->self_idx, conn); continue; } ext = ext_users[ext_rx_chan].ext; if (ext == NULL) { printf("### extint_w2a: %s CONN%d-%p ext_rx_chan %d ext NULL?\n", conn->ext? conn->ext->name:"?", conn->self_idx, conn, ext_rx_chan); continue; } if (ext->receive_msgs) { //printf("extint_w2a: %s ext->receive_msgs() %p CONN%d-%p RX%d-%p %d <%s>\n", conn->ext? conn->ext->name:"?", ext->receive_msgs, conn->self_idx, conn, ext_rx_chan, (ext_rx_chan == -1)? 0:ext_users[ext_rx_chan].conn, strlen(cmd), cmd); if (ext->receive_msgs(cmd, ext_rx_chan)) continue; } else { printf("### extint_w2a: %s CONN%d-%p RX%d-%p ext->receive_msgs == NULL?\n", conn->ext? conn->ext->name:"?", conn->self_idx, conn, ext_rx_chan, (ext_rx_chan == -1)? 0:ext_users[ext_rx_chan].conn); continue; } printf("extint_w2a: %s CONN%d-%p unknown command: <%s> ======================================================\n", conn->ext? conn->ext->name:"?", conn->self_idx, conn, cmd); continue; } conn->keep_alive = timer_sec() - ka_time; bool keepalive_expired = (conn->keep_alive > KEEPALIVE_SEC); if (keepalive_expired) { ext_rx_chan = conn->ext_rx_chan; ext = ext_users[ext_rx_chan].ext; printf("EXT KEEP-ALIVE EXPIRED RX%d %s\n", ext_rx_chan, ext? ext->name : "(no ext)"); if (ext != NULL && ext->close_conn != NULL) ext->close_conn(ext_rx_chan); extint_ext_users_init(ext_rx_chan); rx_server_remove(conn); panic("shouldn't return"); } TaskSleep(250000); } }