Пример #1
0
unsigned int openiot_alert(char *hardwareId, char *alertType, char *alertMessage, int64_t eventDate,
                           uint8_t *buffer, size_t length, char *originator) {
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

    OpenIoT_Header header = {};
    header.command = OpenIoT_Command_DEVICEALERT;
    if (originator != NULL) {
        header.has_originator = true;
        strcpy(header.originator, originator);
    }
    if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) {
        return 0;
    }

    OpenIoT_DeviceAlert alert = {};
    strcpy(alert.hardwareId, hardwareId);
    strcpy(alert.alertType, alertType);
    strcpy(alert.alertMessage, alertMessage);
    if (eventDate != 0) {
        alert.has_eventDate = true;
        alert.eventDate = eventDate;
    }
    if (!pb_encode_delimited(&stream, OpenIoT_DeviceAlert_fields, &alert)) {
        return 0;
    }

    return stream.bytes_written;
}
Пример #2
0
unsigned int openiot_acknowledge(char *hardwareId, char *message, uint8_t *buffer, size_t length, char *originator) {
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

    OpenIoT_Header header = {};
    header.command = OpenIoT_Command_ACKNOWLEDGE;
    if (originator != NULL) {
        header.has_originator = true;
        strcpy(header.originator, originator);
    }
    if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) {
        return 0;
    }

    OpenIoT_Acknowledge ack = {};
    strcpy(ack.hardwareId, hardwareId);
    if (message != NULL) {
        ack.has_message = true;
        strcpy(ack.message, message);
    }
    if (!pb_encode_delimited(&stream, OpenIoT_Acknowledge_fields, &ack)) {
        return 0;
    }

    return stream.bytes_written;
}
Пример #3
0
unsigned int openiot_location(char *hardwareId, char *lat, char *lon, char *ele, int64_t eventDate,
                              uint8_t *buffer, size_t length, char *originator) {
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

    OpenIoT_Header header = {};
    header.command = OpenIoT_Command_DEVICELOCATION;
    if (originator != NULL) {
        header.has_originator = true;
        strcpy(header.originator, originator);
    }
    if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) {
        return 0;
    }

    OpenIoT_DeviceLocation location = {};
    strcpy(location.hardwareId, hardwareId);

    strcpy(location.latitude, lat);
    strcpy(location.longitude, lon);
    strcpy(location.elevation, ele);
    location.has_elevation = true;

    if (eventDate != 0) {
        location.has_eventDate = true;
        location.eventDate = eventDate;
    }
    if (!pb_encode_delimited(&stream, OpenIoT_DeviceLocation_fields, &location)) {
        return 0;
    }

    return stream.bytes_written;
}
Пример #4
0
unsigned int sw_location(char* hardwareId, float lat, float lon, float ele, int64_t eventDate,
		uint8_t* buffer, size_t length, char* originator) {
	pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

	SiteWhere_Header header = { };
	header.command = SiteWhere_Command_DEVICELOCATION;
	if (originator != NULL) {
		header.has_originator = true;
		strcpy(header.originator, originator);
	}
	if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
		return 0;
	}

	SiteWhere_DeviceLocation location = { };
	strcpy(location.hardwareId, hardwareId);
	location.latitude = float_to_double(lat);
	location.longitude = float_to_double(lon);
	location.elevation = float_to_double(ele);
	location.has_elevation = true;

	if (eventDate != NULL) {
		location.has_eventDate = true;
		location.eventDate = eventDate;
	}
	if (!pb_encode_delimited(&stream, SiteWhere_DeviceLocation_fields, &location)) {
		return 0;
	}

	return stream.bytes_written;
}
Пример #5
0
unsigned int sw_measurement(char* hardwareId, char* name, float value, int64_t eventDate,
		uint8_t* buffer, size_t length, char* originator) {
	pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

	SiteWhere_Header header = { };
	header.command = SiteWhere_Command_DEVICEMEASUREMENT;
	if (originator != NULL) {
		header.has_originator = true;
		strcpy(header.originator, originator);
	}
	if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
		return 0;
	}

	SiteWhere_DeviceMeasurements measurements = { };
	strcpy(measurements.hardwareId, hardwareId);

	SiteWhere_Measurement measurement = { };
	strcpy(measurement.measurementId, name);
	measurement.measurementValue = float_to_double(value);
	measurements.measurement[0] = measurement;
	measurements.measurement_count = 1;

	if (eventDate != NULL) {
		measurements.has_eventDate = true;
		measurements.eventDate = eventDate;
	}
	if (!pb_encode_delimited(&stream, SiteWhere_DeviceMeasurements_fields, &measurements)) {
		return 0;
	}

	return stream.bytes_written;
}
Пример #6
0
unsigned int openiot_measurement_multi(char *hardwareId, char **measurementTokens, int64_t eventDate,
                                       uint8_t *buffer, size_t length, char *originator) {
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

    OpenIoT_Header header = {};
    header.command = OpenIoT_Command_DEVICEMEASUREMENT;
    if (originator != NULL) {
        header.has_originator = true;
        strcpy(header.originator, originator);
    }
    if (!pb_encode_delimited(&stream, OpenIoT_Header_fields, &header)) {
        return 0;
    }

    OpenIoT_DeviceMeasurements measurements = {};
    strcpy(measurements.hardwareId, hardwareId);

    measurements.measurement_count = 0;
    if (measurementTokens) {
        int i;
        for (i = 0; *(measurementTokens + i); i++) {
            OpenIoT_Measurement measurement = {};
            char *key = strtok(*(measurementTokens + i), ":");
            //float value = atof(strtok(NULL, ":"));
            char *value = strtok(NULL, ":");
            //printf("value is %lf\n", value);
            strcpy(measurement.measurementId, key);
            //measurement.measurementValue = float_to_double(value);
            //measurement.measurementValue = value;
            strcpy(measurement.measurementValue, value);
            measurements.measurement[i] = measurement;
            measurements.measurement_count++;
            free(*(measurementTokens + i));
        }
        free(measurementTokens);
    }

    if (eventDate != 0) {
        measurements.has_eventDate = true;
        measurements.eventDate = eventDate;
    }
    if (!pb_encode_delimited(&stream, OpenIoT_DeviceMeasurements_fields, &measurements)) {
        return 0;
    }

    return stream.bytes_written;
}
Пример #7
0
void cdc_rx_notify(uint8_t port) {
  l("cdc_rx_notify [%d]", port);

  uint8_t b = udi_cdc_getc();
  if (b != 0x08) {
    l("Protocol desync");
  }
  l("First byte ok");
  uint32_t offset = 0;
  do {
    buffer[offset++] = b;
    b = udi_cdc_getc();
    l("-> 0x%02x", b);
  } while (b & 0x80);
  buffer[offset++] = b;
  // Now we have enough to know the size
  l("Length read, decoding...");
  l("... 0x%02x 0x%02x", buffer[0], buffer[1]);

  pb_istream_t istream = pb_istream_from_buffer(buffer + 1, USB_BUFFER_SIZE);
  l("istream bytes_left before %d", istream.bytes_left);
  uint64_t len = 0;
  pb_decode_varint(&istream, &len);
  l("message_length %d", (uint32_t)len);
  l("offset %d", offset);
  udi_cdc_read_buf(buffer + offset, len);
  l("decode message");
  istream = pb_istream_from_buffer(buffer + offset, len);
  DonglePiRequest request = {0};
  request.config.i2c.funcs.decode = handle_i2c_config_cb;
  request.config.uart.funcs.decode = handle_uart_config_cb;
  request.config.spi.funcs.decode = handle_spi_config_cb;
  request.config.gpio.pins.funcs.decode = handle_gpio_pin_config_cb;
  request.data.i2c.writes.funcs.decode = handle_i2c_write_cb;

  if (!pb_decode(&istream, DonglePiRequest_fields, &request)) {
    l("failed to decode the packet, wait for more data");
    return;
  }

  l("Request #%d received", request.message_nb);

  if (request.has_data && request.data.has_gpio) {
    handle_gpio_write(request.data.gpio);
  }

  pb_ostream_t ostream = pb_ostream_from_buffer(buffer, USB_BUFFER_SIZE);
  DonglePiResponse response = {};
  response.message_nb = request.message_nb;
  l("Create response for #%d", response.message_nb);

  handle_gpio_read(&response);

  pb_encode_delimited(&ostream, DonglePiResponse_fields, &response);
  l("Write response nb_bytes = %d", ostream.bytes_written);
  uint32_t wrote = udi_cdc_write_buf(buffer, ostream.bytes_written);
  l("Done. wrote %d bytes", wrote);
}
Пример #8
0
unsigned int sw_register(char* hardwareId, char* specificationToken, uint8_t* buffer, size_t length, char* originator) {
	pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);

	SiteWhere_Header header = { };
	header.command = SiteWhere_Command_REGISTER;
	if (originator != NULL) {
		header.has_originator = true;
		strcpy(header.originator, originator);
	}
	if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
		return 0;
	}

	SiteWhere_RegisterDevice registerDevice = { };
	strcpy(registerDevice.hardwareId, hardwareId);
	strcpy(registerDevice.specificationToken, specificationToken);
	if (!pb_encode_delimited(&stream, SiteWhere_RegisterDevice_fields, &registerDevice)) {
		return 0;
	}

	return stream.bytes_written;
}