void run(char *device_name) { pcap_t *device; const u_char *packet; struct pcap_pkthdr *hdr = (struct pcap_pkthdr *) malloc(sizeof *hdr); struct pcap_pkthdr *lasthdr = (struct pcap_pkthdr *) malloc(sizeof *lasthdr); int is3G = (!strncmp(device_name, "rmnet0", strlen("rmnet0"))) ? 1 : 0; device = device_load(device_name); while (1) { packet = pcap_next(device, hdr); if (packet) { packet_process(hdr, packet, is3G); } } free(hdr); free(lasthdr); }
void SetDevice (DviWidget dw, const char *name) { XtWidgetGeometry request, reply; XtGeometryResult ret; ForgetFonts (dw); dw->dvi.device = device_load (name); if (!dw->dvi.device) return; dw->dvi.sizescale = dw->dvi.device->sizescale; dw->dvi.device_resolution = dw->dvi.device->res; dw->dvi.native = dw->dvi.device->X11; dw->dvi.paperlength = dw->dvi.device->paperlength; dw->dvi.paperwidth = dw->dvi.device->paperwidth; if (dw->dvi.native) { dw->dvi.display_resolution = dw->dvi.device_resolution; dw->dvi.scale_factor = 1.0; } else { dw->dvi.display_resolution = dw->dvi.default_resolution; dw->dvi.scale_factor = ((double)dw->dvi.display_resolution / dw->dvi.device_resolution); } request.request_mode = CWWidth|CWHeight; request.width = MY_WIDTH(dw); request.height = MY_HEIGHT(dw); ret = XtMakeGeometryRequest ((Widget)dw, &request, &reply); if (ret == XtGeometryAlmost && reply.height >= request.height && reply.width >= request.width) { request.width = reply.width; request.height = reply.height; XtMakeGeometryRequest ((Widget)dw, &request, &reply); } }
int serial_in(int sd, struct mosquitto *mosq, char *md_id) { static char serial_buf[SERIAL_MAX_BUF]; static int buf_len = 0; char *buf_p; char id[DEVICE_ID_SIZE + 1]; struct device *dev; int rc, sread; if (buf_len) buf_p = &serial_buf[buf_len - 1]; else buf_p = &serial_buf[0]; sread = serialport_read_until(sd, buf_p, eolchar, SERIAL_MAX_BUF - buf_len, config.serial.timeout); if (sread == -1) { fprintf(stderr, "Serial - Read Error.\n"); return -1; } if (sread == 0) return 0; buf_len += sread; if (serial_buf[buf_len - 1] == eolchar) { serial_buf[buf_len - 1] = 0; //replace eolchar buf_len--; // eolchar was counted, decreasing 1 if (config.debug > 3) printf("Serial - size:%d, serial_buf:%s\n", buf_len, serial_buf); if (buf_len < SERIAL_INIT_LEN) { // We need at least SERIAL_INIT_LEN to count as a valid command if (config.debug > 1) printf("Invalid serial input.\n"); buf_len = 0; return 0; } sread = buf_len; // for return buf_len = 0; // reseting for the next input buf_p = &serial_buf[SERIAL_INIT_LEN]; // Serial debug if (!strncmp(serial_buf, SERIAL_INIT_DEBUG, SERIAL_INIT_LEN)) { if (config.debug) printf("Debug: %s\n", buf_p); return sread; } else if (!strncmp(serial_buf, SERIAL_INIT_MSG, SERIAL_INIT_LEN)) { if (config.debug > 2) printf("Serial - message: %s\n", serial_buf); if (getString(&buf_p, id, DEVICE_ID_SIZE, ',') != DEVICE_ID_SIZE) { if (config.debug > 1) printf("Serial - Invalid data.\n"); return 0; } if (!device_isValid_id(id)) { if (config.debug > 1) printf("Serial - Invalid device id.\n"); return 0; } dev = device_get(&bridge, id); if (!dev) { rc = device_load(&bridge, config.devices_folder, id); if (rc == -1) { run = 0; return sread; } if (rc) { rc = device_add_dev(&bridge, id, md_id); if (rc == -1) { run = 0; return sread; } if (rc) { if (config.debug > 2) printf("Serial - Failed to add device.\n"); return sread; } } dev = device_get(&bridge, id); if (config.debug > 1) { printf("New device:\n"); device_print_device(dev); } } else dev->alive = ALIVE_CNT; bridge_message(mosq, sd, dev, buf_p); } else { if (config.debug > 1) printf("Unknown serial data.\n"); } return sread; } else if (buf_len == SERIAL_MAX_BUF) { if (config.debug > 1) printf("Serial buffer full.\n"); buf_len = 0; } else { if (config.debug > 1) printf("Serial chunked.\n"); } return 0; }
void on_mqtt_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) { char *payload; int *sd; char id[DEVICE_ID_SIZE + 1]; struct device *dev; int rc; sd = (int *)obj; payload = (char *)msg->payload; if (config.debug > 2) printf("MQTT - topic: %s - payload: %s\n", msg->topic, payload); if (!strcmp(msg->topic, bridge.config_topic)) { if (getString(&payload, id, DEVICE_ID_SIZE, ',') != DEVICE_ID_SIZE) { if (config.debug > 1) printf("MQTT - Invalid data.\n"); return; } } else { strncpy(id, &msg->topic[7], DEVICE_ID_SIZE); // 7 - strlen("config/"); } if (!device_isValid_id(id)) { if (config.debug > 1) printf("MQTT - Invalid device id.\n"); return; } dev = device_get(&bridge, id); if (!dev) { rc = device_load(&bridge, config.devices_folder, id); if (rc == -1) { run = 0; return; } if (rc) { rc = device_add_dev(&bridge, id, MODULE_MQTT_ID); if (rc == -1) { run = 0; return; } if (rc) { if (config.debug > 2) printf("MQTT - Failed to add device.\n"); return; } } dev = device_get(&bridge, id); if (config.debug > 1) printf("New device:\n"); device_print_device(dev); if (dev->type == DEVICE_TYPE_NODE) { snprintf(gbuf, GBUF_SIZE, "status/%s", dev->id); rc = mosquitto_subscribe(mosq, NULL, gbuf, config.mqtt_qos); if (rc) { fprintf(stderr, "MQTT - Subscribe ERROR: %s\n", mosquitto_strerror(rc)); run = 0; return; } } else if (dev->type == DEVICE_TYPE_CONTROLLER) { bridge.controller = true; } } else dev->alive = ALIVE_CNT; bridge_message(mosq, *sd, dev, payload); }