void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags, const mosquitto_property *properties) { int rc = MOSQ_ERR_SUCCESS; UNUSED(obj); UNUSED(flags); UNUSED(properties); if(!result){ switch(cfg.pub_mode){ case MSGMODE_CMD: case MSGMODE_FILE: case MSGMODE_STDIN_FILE: rc = my_publish(mosq, &mid_sent, cfg.topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain); break; case MSGMODE_NULL: rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain); break; case MSGMODE_STDIN_LINE: status = STATUS_CONNACK_RECVD; break; } if(rc){ if(!cfg.quiet){ switch(rc){ case MOSQ_ERR_INVAL: fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n"); break; case MOSQ_ERR_NOMEM: fprintf(stderr, "Error: Out of memory when trying to publish message.\n"); break; case MOSQ_ERR_NO_CONN: fprintf(stderr, "Error: Client not connected when trying to publish.\n"); break; case MOSQ_ERR_PROTOCOL: fprintf(stderr, "Error: Protocol error when communicating with broker.\n"); break; case MOSQ_ERR_PAYLOAD_SIZE: fprintf(stderr, "Error: Message payload is too large.\n"); break; case MOSQ_ERR_QOS_NOT_SUPPORTED: fprintf(stderr, "Error: Message QoS not supported on broker, try a lower QoS.\n"); break; } } mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); } }else{ if(result && !cfg.quiet){ if(cfg.protocol_version == MQTT_PROTOCOL_V5){ fprintf(stderr, "%s\n", mosquitto_reason_string(result)); }else{ fprintf(stderr, "%s\n", mosquitto_connack_string(result)); } } } }
/** The dispatch.c callback for OPENBEACON_PROTO_BEACONTRACKER. * @param d User data, as given to dispatch_set_callback. * @param bytes OpenBeacon packet byte stream. * @param rxer Receiver identity. * @param tv Timestamp of packet reception. */ void beacontracker_cb(void *d , uint8_t *bytes , dispatch_rx_info *rxi , const struct timeval *tv ) { openbeacon_tracker_data *btd = d; rx_id rxer = rxi->rxid; openbeacon_tracker_packet pp; read_beacontracker(bytes, &pp); /* Because nobody's been good about keeping their OID spaces * separate, despite the whopping 32 bit space available to * them, we at least ensure that our plaintext ones end up * not getting stomped on by anybody else. */ int oid = pp.oid + ((rxi->keyid+1)<<16); #if 0 // Removed because we feel like tracking a lot more things. if ((oid > BADGE_MAXIMUM_ID) || (oid < BADGE_MINIMUM_ID)) { printf("Suspicious OID (outside designated range): %d\n", oid); return; } #endif gpointer _b = g_hash_table_lookup(btd->oid_estdata, &oid); openbeacon_badge *b = _b; if(!_b) { b = calloc(1, sizeof(openbeacon_badge)); b->id = oid; g_hash_table_insert(btd->oid_estdata, &b->id, (gpointer) b); } gpointer rxloc = g_hash_table_lookup(btd->rxid_location, &rxer); if(!rxloc) { printf("Discarding (unknown RX %8.8x)\n", rxer); return; } if(DEBUG) printf("RXer %8.8x saw %d (seq=%x, pl=%x))\n", rxer, oid, pp.seq, pp.strength); struct timeval last_time = b->last_print_time; uint8_t prox = ((~pp.strength) & 0xF) + 1; update_badge_pos(&b->data, pp.seq >> CONFIG_SEQUENCE_ID_SHIFT, (rx_loc*)rxloc, prox); if(tv != NULL && pp.flags != 0xFF) { b->last_touch_time = *tv; b->last_touch_value = pp.flags; } if(tv == NULL || (tv->tv_sec - last_time.tv_sec >= 1)) { if(tv != NULL) b->last_print_time = *tv; if(btd->mqtt_out){ #define BAVG(x) (b->data.sum##x / b->data.denom) my_publish(b->id,BAVG(x), BAVG(y), BAVG(z)); #undef BAVG } if(btd->human_out_file) print_badge_human_data(btd->human_out_file, btd, b); if(btd->structured_out_file) print_badge_structured_data(btd->structured_out_file, btd, b); } }
int pub_shared_loop(struct mosquitto *mosq) { int read_len; int pos; int rc, rc2; char *buf2; int buf_len_actual; int mode; int loop_delay = 1000; if(cfg.repeat_count > 1 && (cfg.repeat_delay.tv_sec == 0 || cfg.repeat_delay.tv_usec != 0)){ loop_delay = cfg.repeat_delay.tv_usec / 2000; } mode = cfg.pub_mode; if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_start(mosq); } do{ if(mode == MSGMODE_STDIN_LINE){ if(status == STATUS_CONNACK_RECVD){ pos = 0; read_len = line_buf_len; while(connected && fgets(&line_buf[pos], read_len, stdin)){ buf_len_actual = strlen(line_buf); if(line_buf[buf_len_actual-1] == '\n'){ line_buf[buf_len_actual-1] = '\0'; rc2 = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain); if(rc2){ if(!cfg.quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props); } break; }else{ line_buf_len += 1024; pos += 1023; read_len = 1024; buf2 = realloc(line_buf, line_buf_len); if(!buf2){ fprintf(stderr, "Error: Out of memory.\n"); return MOSQ_ERR_NOMEM; } line_buf = buf2; } } if(feof(stdin)){ if(mid_sent == -1){ /* Empty file */ mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); disconnect_sent = true; status = STATUS_DISCONNECTING; }else{ last_mid = mid_sent; status = STATUS_WAITING; } } }else if(status == STATUS_WAITING){ if(last_mid_sent == last_mid && disconnect_sent == false){ mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props); disconnect_sent = true; } #ifdef WIN32 Sleep(100); #else struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = 100000000; nanosleep(&ts, NULL); #endif } rc = MOSQ_ERR_SUCCESS; }else{ rc = mosquitto_loop(mosq, loop_delay, 1); if(ready_for_repeat && check_repeat_time()){ rc = 0; switch(cfg.pub_mode){ case MSGMODE_CMD: case MSGMODE_FILE: case MSGMODE_STDIN_FILE: rc = my_publish(mosq, &mid_sent, cfg.topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain); break; case MSGMODE_NULL: rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain); break; case MSGMODE_STDIN_LINE: break; } if(rc){ fprintf(stderr, "Error sending repeat publish: %s", mosquitto_strerror(rc)); } } } }while(rc == MOSQ_ERR_SUCCESS && connected); if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_stop(mosq, false); } return 0; }