Skip to content

Commit

Permalink
Formatted with clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
jmadden173 committed Sep 26, 2024
1 parent 4aea845 commit 9321ce4
Show file tree
Hide file tree
Showing 39 changed files with 904 additions and 1,013 deletions.
64 changes: 31 additions & 33 deletions esp32/lib/dirtviz/include/dirtviz.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/**
* @brief Library used to interface with the Dirtviz API
*
*
* Assumes that WiFi interface is connected to the network.
*
*
* @author John Madden <[email protected]>
* @date 2023-11-29
*/

#include <cstdlib>
#include <cstdio>
#include <cstring>
*/

#include <WiFi.h>
#include <WiFiClientSecure.h>

#include <cstdio>
#include <cstdlib>
#include <cstring>

/**
* @brief HTTP interface for Dirtviz API
*/
class Dirtviz
{
private:
*/
class Dirtviz {
private:
/** URL of API */
char *url = nullptr;

Expand All @@ -29,73 +28,72 @@ class Dirtviz
/** Buffer for the HTTP response */
char *response = nullptr;


public:
public:
/**
* @brief Default constructor
*
*
* Allows for setting of URL on initialization.
*
*
* @param url API URL
* @param port API port number
*/
*/
Dirtviz(const char *url, const uint16_t &port);

/**
* @brief Frees all memory
*/
*/
~Dirtviz();

/**
* @brief Setter for @p url
*
*
* @param new_url New API URL
*/
*/
void SetUrl(const char *new_url);

/**
* @brief Getter for @p url
*
*
* @returns Pointer to @p url
*/
*/
const char *GetUrl(void) const;

/**
* @brief Setter for @p port
*
*
* @param new_port New port number
*/
*/
void SetPort(const uint16_t &new_port);

/**
* @brief Getter for @p port
*
*
* @returns value of @p url
*/
*/
uint16_t GetPort(void) const;

/**
* @brief Send serialized measurement to the API
*
*
* The entire response is stored in a dynamically allocated buffer. Use
* Dirtviz::GetResponse to get the binary response data.
*
*
* @param meas Pointer to serialized measurement data
* @param meas_len Number of bytes in @p meas
*
*
* @return HTTP response code, -1 indicates an error in parsing
*/
*/
int SendMeasurement(const uint8_t *meas, size_t meas_len);

/**
* @brief Get binary data from response
*
*
* A returned length of 0 indicates an error and the pointer @p data has not
* been modified.
*
*
* @param data Pointer to response binary data
*
*
* @return Length of @p data
*/
*/
size_t GetResponse(const uint8_t *data) const;
};
81 changes: 32 additions & 49 deletions esp32/lib/dirtviz/src/dirtviz.cpp
Original file line number Diff line number Diff line change
@@ -1,64 +1,55 @@
/**
* @see dirtviz.hpp
*
*
* @author John Madden <[email protected]>
* @date 2023-11-29
*/
*/

#include <Arduino.h>
#include "dirtviz.hpp"

Dirtviz::Dirtviz(const char *url, const uint16_t &port) : url(nullptr), response(nullptr)
{
#include <Arduino.h>

Dirtviz::Dirtviz(const char *url, const uint16_t &port)
: url(nullptr), response(nullptr) {
// set parameters
this->SetUrl(url);
this->SetPort(port);
}

Dirtviz::~Dirtviz()
{
Dirtviz::~Dirtviz() {
// free memory
free(this->url);
free(this->response);
}

void Dirtviz::SetUrl(const char *new_url)
{
void Dirtviz::SetUrl(const char *new_url) {
// get length of new url string, add 1 for null char
size_t url_len = strlen(new_url);
++url_len;

// allocate memory
char * temp_url = (char *) realloc(this->url, url_len);
char *temp_url = (char *)realloc(this->url, url_len);

if (temp_url != nullptr) {
this->url = temp_url;
strcpy(this->url, new_url); // strcpy is safe here because we just allocated enough space
strcpy(
this->url,
new_url); // strcpy is safe here because we just allocated enough space
} else {
// Handle allocation failure (e.g., set an error flag, use a default URL, etc.)

// Handle allocation failure (e.g., set an error flag, use a default URL,
// etc.)
}
}

const char *Dirtviz::GetUrl(void) const
{
return this->url;
}
const char *Dirtviz::GetUrl(void) const { return this->url; }

void Dirtviz::SetPort(const uint16_t &new_port)
{
this->port = new_port;
}
void Dirtviz::SetPort(const uint16_t &new_port) { this->port = new_port; }

uint16_t Dirtviz::GetPort(void) const
{
return this->port;
}
uint16_t Dirtviz::GetPort(void) const { return this->port; }

int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
{
int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len) {
// WiFi client for connection with API
//WiFiClientSecure client;
// WiFiClientSecure client;
WiFiClient client;

// buffer for making post requests
Expand All @@ -67,10 +58,9 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
Serial.print(":");
Serial.print(this->port);
// try connection return negative length if error
//client.setInsecure();
//client.setCACert(rootCACertificate);
if (!client.connect(this->url, this->port))
{
// client.setInsecure();
// client.setCACert(rootCACertificate);
if (!client.connect(this->url, this->port)) {
Serial.println("Connection failure");
return -1;
}
Expand All @@ -94,14 +84,12 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
// newline indicating end of headers
client.println();
// send data
for (int idx = 0; idx < meas_len; ++idx)
{
for (int idx = 0; idx < meas_len; ++idx) {
client.write(meas[idx]);
}


// read response

// get length of response
int resp_len = client.available();

Expand All @@ -110,7 +98,7 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
this->response = nullptr;

// allocate memory
this->response = (char *) realloc(this->response, resp_len + 1);
this->response = (char *)realloc(this->response, resp_len + 1);

// copy into buffer
if (this->response != nullptr) {
Expand All @@ -119,7 +107,7 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
while (client.available() && bytesRead < resp_len) {
this->response[bytesRead++] = client.read();
}
this->response[bytesRead] = '\0'; // Null-terminate the response
this->response[bytesRead] = '\0'; // Null-terminate the response
} else {
Serial.println("Null pointer failure");
return -1;
Expand All @@ -129,38 +117,33 @@ int Dirtviz::SendMeasurement(const uint8_t *meas, size_t meas_len)
// disconnect after message is sent
client.stop();


// find status code
int status_code;
if (sscanf(this->response, "%*s %d", &status_code) != 1)
{
if (sscanf(this->response, "%*s %d", &status_code) != 1) {
Serial.println("Unable to parse status code");
return -1;
}

return status_code;
return status_code;
}

size_t Dirtviz::GetResponse(const uint8_t *data) const
{
size_t Dirtviz::GetResponse(const uint8_t *data) const {
// find response length from header

// get pointer to start of line
const char *length_start = strstr(this->response, "Content-Length:");
if (length_start == nullptr)
{
if (length_start == nullptr) {
return 0;
}

// parse the length
size_t data_len;
if (sscanf(length_start, "%*s %u", &data_len))
{
if (sscanf(length_start, "%*s %u", &data_len)) {
return 0;
}

// read binary data, look for double CRLF
data = (const uint8_t *) strstr(this->response, "\r\n\r\n");
data = (const uint8_t *)strstr(this->response, "\r\n\r\n");
data += 4;

// return the length of data
Expand Down
23 changes: 11 additions & 12 deletions esp32/src/examples/example_dirtviz.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @brief Example of using dirtviz library
*
*
* @author John Madden <[email protected]>
* @date 2023-11-30
*/
Expand All @@ -23,15 +23,16 @@ const size_t data_len = 21;

/**
* @brief Initialization code run on startup
*
*
*/
void setup()
{
*
*
*/
void setup() {
// Start serial interface
Serial.begin(115200);
// Wait for serial connection
while (!Serial) { delay(100); }
while (!Serial) {
delay(100);
}

Serial.print("SSID: ");
Serial.println(ssid);
Expand All @@ -43,8 +44,7 @@ void setup()
WiFi.begin(ssid, pass);

// Wait for WiFi to connect
while (WiFi.status() != WL_CONNECTED)
{
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Expand All @@ -54,15 +54,14 @@ void setup()
Serial.println(WiFi.localIP());
}

void loop()
{
void loop() {
int resp_code;

const uint8_t *resp_data;
size_t resp_data_len;

// Send example measurement
resp_code = api.SendMeasurement((const uint8_t*) data, data_len);
resp_code = api.SendMeasurement((const uint8_t *)data, data_len);
Serial.print("Response Code: ");
Serial.println(resp_code);

Expand Down
Loading

0 comments on commit 9321ce4

Please sign in to comment.