Skip to content

Commit

Permalink
added simple http
Browse files Browse the repository at this point in the history
  • Loading branch information
KenthJohan committed Dec 13, 2023
1 parent e3168e5 commit fe1ac31
Show file tree
Hide file tree
Showing 16 changed files with 413 additions and 37 deletions.
23 changes: 22 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@
"externalConsole": false,
}
},
{
"name": "Launch meta_download",
"type": "cppdbg",
"request": "launch",
"stopAtEntry": false,
"cwd": "${workspaceFolder}/examples/meta_download",
"environment": [],
"envFile": "${workspaceFolder}/.env",
"program": "${workspaceFolder}/examples/meta_download/bin/x64-Linux-debug/meta_download",
"args": [
"${config:OUSTER_HOST}",
],
"osx": {
"MIMode": "lldb",
"targetArchitecture": "arm64",
"externalConsole": false,
},
"linux": {
"externalConsole": false,
}
},
{
"name": "Launch ouster_monitor",
"type": "cppdbg",
Expand Down Expand Up @@ -107,7 +128,7 @@
"program": "${workspaceFolder}/tools/ouster_meta/bin/x64-Linux-debug/ouster_meta",
"args": [
"-m",
"${config:OUSTER_META_PATH}.new",
"new_meta.json",
"-s",
"${config:OUSTER_HOST}",
],
Expand Down
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"mutex": "c",
"convert.h": "c",
"ouster_assert.h": "c",
"ouster_field.h": "c"
"ouster_field.h": "c",
"ouster_client.h": "c",
"ouster_http.h": "c",
"socket.h": "c",
"string.h": "c",
"stdio.h": "c",
"fcntl.h": "c"
},
"terminal.integrated.env.linux": {
"GTK_PATH": ""
Expand Down
20 changes: 8 additions & 12 deletions examples/meta_download/src/meta_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE (1024 * 1024)

int main(int argc, char *argv[])
{
ouster_fs_pwd();
Expand All @@ -13,18 +15,12 @@ int main(int argc, char *argv[])
return 0;
}

if (argc <= 2) {
printf("Missing output meta file destination\n");
return 0;
}

ouster_client_t client =
{
.host = argv[1]};

ouster_client_init(&client);
ouster_client_download_meta_file(&client, argv[2]);
ouster_client_fini(&client);
int s = ouster_sock_create_tcp(argv[1], 80);
ouster_vec_t v = {0};
ouster_vec_init(&v, 1, 1024);
ouster_http_request(s, argv[1], OUSTER_HTTP_GET_METADATA, &v);
printf("%s\n\n", (char *)v.data);
return 0;

return 0;
}
10 changes: 10 additions & 0 deletions http_samples/metadata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GET /api/v1/sensor/metadata HTTP/1.1
Host: 192.168.1.137
Accept: */*

HTTP/1.1 200 OK
content-type: application/json
content-length: 2224
Connection: close

{"lidar_data_format": {"pixels_per_column": 16, "columns_per_packet": 16, "udp_profile_imu": "LEGACY", "pixel_shift_by_row": [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4], "udp_profile_lidar": "RNG19_RFL8_SIG16_NIR16", "columns_per_frame": 1024, "column_window": [0, 1023]}, "calibration_status": {"reflectivity": {"valid": false, "timestamp": ""}}, "config_params": {"udp_port_imu": 7503, "sync_pulse_out_angle": 360, "udp_profile_lidar": "RNG19_RFL8_SIG16_NIR16", "sync_pulse_out_polarity": "ACTIVE_HIGH", "nmea_leap_seconds": 0, "operating_mode": "NORMAL", "sync_pulse_in_polarity": "ACTIVE_HIGH", "sync_pulse_out_frequency": 1, "nmea_ignore_valid_char": 0, "sync_pulse_out_pulse_width": 10, "nmea_in_polarity": "ACTIVE_HIGH", "udp_port_lidar": 7502, "columns_per_packet": 16, "udp_dest": "192.168.1.113", "multipurpose_io_mode": "OFF", "timestamp_mode": "TIME_FROM_INTERNAL_OSC", "nmea_baud_rate": "BAUD_9600", "azimuth_window": [0, 360000], "phase_lock_enable": false, "lidar_mode": "1024x10", "udp_profile_imu": "LEGACY", "phase_lock_offset": 0, "signal_multiplier": 1}, "beam_intrinsics": {"beam_altitude_angles": [14.412000000000001, 12.271000000000001, 10.167999999999999, 8.0760000000000005, 5.9880000000000004, 3.9129999999999998, 1.825, -0.254, -2.339, -4.4160000000000004, -6.508, -8.6080000000000005, -10.711, -12.83, -14.976000000000001, -17.18], "beam_azimuth_angles": [0.89200000000000002, 0.91900000000000004, 0.92000000000000004, 0.93600000000000005, 0.95699999999999996, 0.98099999999999998, 1.01, 1.0249999999999999, 1.0469999999999999, 1.0860000000000001, 1.1200000000000001, 1.139, 1.1930000000000001, 1.212, 1.264, 1.3280000000000001], "lidar_origin_to_beam_origin_mm": 12.163}, "sensor_info": {"build_rev": "v2.4.0", "build_date": "2022-09-21T17:47:45Z", "prod_pn": "840-101855-02", "prod_line": "OS-1-16-A2", "prod_sn": "991940000262", "image_rev": "ousteros-image-prod-aries-v2.4.0+20220921174636", "status": "RUNNING", "initialization_id": 2573177}, "lidar_intrinsics": {"lidar_to_sensor_transform": [-1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 36.18, 0, 0, 0, 1]}, "imu_intrinsics": {"imu_to_sensor_transform": [1, 0, 0, 6.2530000000000001, 0, 1, 0, -11.775, 0, 0, 1, 7.6449999999999996, 0, 0, 0, 1]}}
2 changes: 2 additions & 0 deletions include/ouster_clib.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ typedef struct
#include "ouster_clib/ouster_meta.h"
#include "ouster_clib/ouster_net.h"
#include "ouster_clib/ouster_sock.h"
#include "ouster_clib/ouster_vec.h"
#include "ouster_clib/ouster_http.h"

#ifdef OUSTER_NO_UDPCAP
#undef OUSTER_USE_UDPCAP
Expand Down
2 changes: 2 additions & 0 deletions include/ouster_clib/ouster_client.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef OUSTER_CLIENT_H
#define OUSTER_CLIENT_H
#include <stdio.h>

#ifdef __cplusplus
extern "C"
Expand All @@ -23,6 +24,7 @@ typedef struct
void ouster_client_init(ouster_client_t *client);
void ouster_client_fini(ouster_client_t *client);
void ouster_client_download_meta_file(ouster_client_t *client, char const *path);
void ouster_client_write_meta_file(ouster_client_t *client, FILE * f);

#ifdef __cplusplus
}
Expand Down
18 changes: 18 additions & 0 deletions include/ouster_clib/ouster_http.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef OUSTER_HTTP_H
#define OUSTER_HTTP_H
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#define OUSTER_HTTP_GET_METADATA "/api/v1/sensor/metadata"
#define OUSTER_HTTP_GET_FIRMWARE "/api/v1/system/firmware"

void ouster_http_request(int sock, char const *host, char const *path, ouster_vec_t *v);

#ifdef __cplusplus
}
#endif

#endif // OUSTER_CLIENT_H
2 changes: 1 addition & 1 deletion include/ouster_clib/ouster_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int ouster_sock_create_udp_lidar(int port, int rcvbuf_size);

int ouster_sock_create_udp_imu(int port, int rcvbuf_size);

int ouster_sock_create_tcp(char const *hint_name);
int ouster_sock_create_tcp(char const *hint_name, int port);

#ifdef __cplusplus
}
Expand Down
31 changes: 31 additions & 0 deletions include/ouster_clib/ouster_vec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef OUSTER_VEC_H
#define OUSTER_VEC_H
#include <stdio.h>

#ifdef __cplusplus
extern "C"
{
#endif


#define OUSTER_OFFSET(o, offset) (void*)(((uintptr_t)(o)) + ((uintptr_t)(offset)))

typedef struct
{
int cap;
int count;
int esize;
void * data;
} ouster_vec_t;

void ouster_vec_init(ouster_vec_t * v, int esize, int cap);

void ouster_vec_append(ouster_vec_t * v, void const * data, int n, float factor);

void test_ouster_vec();

#ifdef __cplusplus
}
#endif

#endif // OUSTER_VEC_H
122 changes: 120 additions & 2 deletions ouster_clib.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,29 @@ void ouster_client_download_meta_file(ouster_client_t *client, char const *path)
fclose(f);
}

void ouster_client_write_meta_file(ouster_client_t *client, FILE * f)
{
ouster_assert_notnull(client);
req_get(client, URLAPI_METADATA);
fwrite(client->buf.data, client->buf.size, 1, f);
}

















#endif
#include <string.h>

Expand Down Expand Up @@ -1072,6 +1095,52 @@ char *ouster_fs_readfile(char const *path)
}
return NULL;
}

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#define HTTP_EOL "\r\n\r\n"

void ouster_http_request(int sock, char const *host, char const *path, ouster_vec_t *v)
{
ouster_assert_notnull(host);
ouster_assert_notnull(path);
ouster_assert_notnull(v);
ouster_assert(v->esize == 1, "");
{
char buf[1024] = {0};
int n = snprintf(buf, sizeof(buf), "GET %s HTTP/1.1\r\nHost: %s\r\nAccept: */*" HTTP_EOL, path, host);
ouster_log("REQ START:\n%s\nREQ END\n", buf);
write(sock, buf, n);
}

while (1) {
char buf[1024] = {0};
ssize_t n = recv(sock, buf, sizeof(buf), 0);
if (n <= 0) {
break;
}
ouster_vec_append(v, buf, n, 1.5f);
}
{
char *e = OUSTER_OFFSET(v->data, v->esize * v->count);
e[0] = '\0';
}

char *json = strstr(v->data, HTTP_EOL);
if (json) {
json += strlen(HTTP_EOL);
size_t n = strlen(json);
memcpy(v->data, json, n);
v->count = n;
char *e = OUSTER_OFFSET(v->data, v->esize * v->count);
e[0] = '\0';
}
}
#include <string.h>

void ouster_lidar_header_get1(char const *buf, void *dst, int type)
Expand Down Expand Up @@ -2252,14 +2321,15 @@ int ouster_sock_create_udp_imu(int port, int rcvbuf_size)
return ouster_net_create(&desc);
}

int ouster_sock_create_tcp(char const *hint_name)
int ouster_sock_create_tcp(char const *hint_name, int port)
{
ouster_assert_notnull(hint_name);

ouster_net_sock_desc_t desc = {0};
desc.flags = OUSTER_NET_FLAGS_TCP | OUSTER_NET_FLAGS_CONNECT;
desc.hint_name = hint_name;
desc.hint_service = "7501";
desc.hint_service = NULL;
desc.port = port;
desc.rcvtimeout_sec = 10;
return ouster_net_create(&desc);
}
Expand Down Expand Up @@ -2362,3 +2432,51 @@ int ouster_udpcap_sock_to_file(ouster_udpcap_t *cap, int sock, FILE *f)

return OUSTER_UDPCAP_OK;
}
#include <string.h>



void ouster_vec_init(ouster_vec_t * v, int esize, int cap)
{
ouster_assert_notnull(v);
ouster_assert(cap >= 0, "");
ouster_assert(esize >= 0, "");
v->data = malloc(cap * esize);
ouster_assert_notnull(v->data);
v->cap = cap;
v->esize = esize;
}

void ouster_vec_append(ouster_vec_t * v, void const * data, int n, float factor)
{
ouster_assert_notnull(v);
ouster_assert(factor >= 1.0f, "");
int count = v->count + n;
if (count > v->cap) {
int cap = (float)count * factor;
v->data = realloc(v->data, cap);
ouster_assert_notnull(v->data);
v->cap = cap + 1;
}
int offset = v->esize * v->count;
memcpy(OUSTER_OFFSET(v->data, offset), data, n);
v->count = count;
}



void test_ouster_vec()
{
char const * d1 = "Hello";
char const * d2 = " ";
char const * d3 = "world!";
ouster_vec_t v;
ouster_vec_init(&v, 1, 2);
ouster_vec_append(&v, d1, strlen(d1), 1.5f);
ouster_vec_append(&v, d2, strlen(d2), 1.5f);
ouster_vec_append(&v, d3, strlen(d3), 1.5f);

char const * str = v.data;
int diff = strcmp("Hello world!", str);
ouster_assert(diff == 0, "");
}
Loading

0 comments on commit fe1ac31

Please sign in to comment.