feat: utils, cleanup
This commit is contained in:
@@ -6,20 +6,9 @@ const cNet = @cImport({
|
|||||||
@cInclude("lwip/dns.h");
|
@cInclude("lwip/dns.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const utils = @import("utils.zig");
|
||||||
fn print(text: []const u8) void {
|
fn print(text: []const u8) void {
|
||||||
var buffer: [4096]u8 = undefined;
|
utils.print(text);
|
||||||
var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
|
||||||
const allocator = fba.allocator();
|
|
||||||
const c_text = allocator.alloc(u8, text.len + 1) catch unreachable;
|
|
||||||
allocator.free(c_text);
|
|
||||||
|
|
||||||
for (0..text.len) |i| {
|
|
||||||
c_text[i] = text[i];
|
|
||||||
}
|
|
||||||
c_text[c_text.len - 1] = 0;
|
|
||||||
|
|
||||||
_ = cNet.printf(c_text.ptr);
|
|
||||||
_ = cNet.printf("\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_usize(num: usize) void {
|
fn print_usize(num: usize) void {
|
||||||
@@ -51,16 +40,13 @@ pub const HttpHeader = struct {
|
|||||||
pub const HttpContext = struct { client: *const Client, request: *const HttpRequest, response: ?*const HttpResponse = null, finished: bool = false };
|
pub const HttpContext = struct { client: *const Client, request: *const HttpRequest, response: ?*const HttpResponse = null, finished: bool = false };
|
||||||
|
|
||||||
fn tcp_recv_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf, _: cNet.err_t) callconv(.C) cNet.err_t {
|
fn tcp_recv_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf, _: cNet.err_t) callconv(.C) cNet.err_t {
|
||||||
print("asd2");
|
|
||||||
if (p1 == null) {
|
if (p1 == null) {
|
||||||
print("asd3");
|
|
||||||
_ = cNet.tcp_close(pcb);
|
_ = cNet.tcp_close(pcb);
|
||||||
return cNet.ERR_OK;
|
return cNet.ERR_OK;
|
||||||
}
|
}
|
||||||
defer {
|
defer {
|
||||||
_ = cNet.pbuf_free(p1);
|
_ = cNet.pbuf_free(p1);
|
||||||
}
|
}
|
||||||
print("asd4");
|
|
||||||
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
||||||
const response = http_context.client.allocator.create(HttpResponse) catch unreachable;
|
const response = http_context.client.allocator.create(HttpResponse) catch unreachable;
|
||||||
http_context.response = response;
|
http_context.response = response;
|
||||||
@@ -71,28 +57,15 @@ fn tcp_recv_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf,
|
|||||||
|
|
||||||
fn send_http_request(pcb: ?*cNet.tcp_pcb, context: *HttpContext) !void {
|
fn send_http_request(pcb: ?*cNet.tcp_pcb, context: *HttpContext) !void {
|
||||||
print("Preparing request");
|
print("Preparing request");
|
||||||
print_usize(@intFromPtr(context));
|
|
||||||
// const request = "POST /todo-channel-name HTTP/1.1\r\nHost: ntfy.sh\r\nTitle: Csengo\r\nContent-Length: 6\r\n\r\nCsengo";
|
|
||||||
print("Getting request");
|
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
const host = if (context.request.url.host) |host| host else unreachable;
|
const host = if (context.request.url.host) |host| host else unreachable;
|
||||||
const host_string = switch (host) {
|
const host_string = switch (host) {
|
||||||
.raw => |v| v,
|
.raw => |v| v,
|
||||||
.percent_encoded => |v| v,
|
.percent_encoded => |v| v,
|
||||||
};
|
};
|
||||||
print("Got host");
|
|
||||||
print(host_string);
|
|
||||||
print("Getting path");
|
|
||||||
const path_string = switch (context.request.url.path) {
|
const path_string = switch (context.request.url.path) {
|
||||||
.raw => |v| v,
|
.raw => |v| v,
|
||||||
.percent_encoded => |v| v,
|
.percent_encoded => |v| v,
|
||||||
};
|
};
|
||||||
print("Got path");
|
|
||||||
print(path_string);
|
|
||||||
|
|
||||||
const request_data = .{ @tagName(context.request.method), path_string, host_string, context.request.body.len, context.request.body };
|
const request_data = .{ @tagName(context.request.method), path_string, host_string, context.request.body.len, context.request.body };
|
||||||
|
|
||||||
@@ -108,43 +81,40 @@ fn send_http_request(pcb: ?*cNet.tcp_pcb, context: *HttpContext) !void {
|
|||||||
}
|
}
|
||||||
fn tcp_connected_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, err: cNet.err_t) callconv(.C) cNet.err_t {
|
fn tcp_connected_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, err: cNet.err_t) callconv(.C) cNet.err_t {
|
||||||
print("tcp_connected_callback");
|
print("tcp_connected_callback");
|
||||||
print_usize(@intFromPtr(context.?));
|
|
||||||
print(if (err != cNet.ERR_OK) "not ok" else "ok");
|
|
||||||
if (err != cNet.ERR_OK) {
|
if (err != cNet.ERR_OK) {
|
||||||
|
print("ERR");
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
// std.debug.print("Connected to server\n", .{});
|
// std.debug.print("Connected to server\n", .{});
|
||||||
print("Connected to server");
|
print("Connected to server");
|
||||||
_ = cNet.tcp_recv(pcb, tcp_recv_callback);
|
_ = cNet.tcp_recv(pcb, tcp_recv_callback);
|
||||||
// TODO: handle this error
|
// TODO: handle this error
|
||||||
print("asd1");
|
|
||||||
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
||||||
print_usize(@intFromPtr(http_context));
|
|
||||||
send_http_request(pcb, http_context) catch unreachable;
|
send_http_request(pcb, http_context) catch unreachable;
|
||||||
print("asd2");
|
|
||||||
return cNet.ERR_OK;
|
return cNet.ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_tcp_connection(ipaddr: [*c]const cNet.struct_ip4_addr, context: *HttpContext, context2: ?*anyopaque) void {
|
fn tcp_connection_error(_: ?*anyopaque, err: cNet.err_t) callconv(.c) void {
|
||||||
print("open_tcp_connection");
|
print("TCP connection error");
|
||||||
if (context.request.url.host != null) {
|
utils.print_i8(err);
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
}
|
||||||
print_usize(@intFromPtr(context));
|
|
||||||
print_usize(@intFromPtr(context2.?));
|
fn open_tcp_connection(ipaddr: [*c]const cNet.struct_ip4_addr, context: *const HttpContext, context2: ?*anyopaque) void {
|
||||||
|
print("open_tcp_connection");
|
||||||
const pcb = cNet.tcp_new();
|
const pcb = cNet.tcp_new();
|
||||||
if (pcb == null) {
|
if (pcb == null) {
|
||||||
|
print("pcb is null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
cNet.tcp_arg(pcb, context2);
|
cNet.tcp_arg(pcb, context2);
|
||||||
_ = cNet.tcp_connect(pcb, ipaddr, context.request.url.port orelse 80, tcp_connected_callback);
|
cNet.tcp_err(pcb, tcp_connection_error);
|
||||||
|
print("Opening tcp connection");
|
||||||
|
const tcp_result = cNet.tcp_connect(pcb, ipaddr, context.request.url.port orelse 80, tcp_connected_callback);
|
||||||
|
if (tcp_result == cNet.ERR_OK) {
|
||||||
|
print("TCP ok");
|
||||||
|
} else {
|
||||||
|
print("TCP not ok");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dns_callback(_: [*c]const u8, ipaddr: [*c]const cNet.struct_ip4_addr, context: ?*anyopaque) callconv(.C) void {
|
fn dns_callback(_: [*c]const u8, ipaddr: [*c]const cNet.struct_ip4_addr, context: ?*anyopaque) callconv(.C) void {
|
||||||
@@ -152,13 +122,6 @@ fn dns_callback(_: [*c]const u8, ipaddr: [*c]const cNet.struct_ip4_addr, context
|
|||||||
if (ipaddr) |addr| {
|
if (ipaddr) |addr| {
|
||||||
print("DNS resolution successful!");
|
print("DNS resolution successful!");
|
||||||
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
||||||
print_usize(@intFromPtr(http_context));
|
|
||||||
print(http_context.request.url.scheme);
|
|
||||||
if (http_context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
open_tcp_connection(addr, http_context, context);
|
open_tcp_connection(addr, http_context, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,65 +130,42 @@ pub const Client = struct {
|
|||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
pub fn sendRequest(client: *const Client, request: *const HttpRequest) !?*const HttpResponse {
|
pub fn sendRequest(client: *const Client, request: *const HttpRequest) !?*const HttpResponse {
|
||||||
print("Client.sendRequest");
|
print("Client.sendRequest");
|
||||||
const context: HttpContext = .{ .client = client, .request = request };
|
var context: HttpContext = .{ .client = client, .request = request };
|
||||||
|
|
||||||
cNet.cyw43_arch_lwip_begin();
|
cNet.cyw43_arch_lwip_begin();
|
||||||
var cached_address = cNet.ip_addr_t{};
|
var cached_address = cNet.ip_addr_t{};
|
||||||
|
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
|
|
||||||
const host = if (request.url.host) |host| host else unreachable;
|
const host = if (request.url.host) |host| host else unreachable;
|
||||||
const host_string = switch (host) {
|
const host_string = switch (host) {
|
||||||
.raw => |v| v,
|
.raw => |v| v,
|
||||||
.percent_encoded => |v| v,
|
.percent_encoded => |v| v,
|
||||||
};
|
};
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
|
|
||||||
const host_c_string = try client.allocator.alloc(u8, host_string.len + 1);
|
const host_c_string = try client.allocator.alloc(u8, host_string.len + 1);
|
||||||
defer client.allocator.free(host_c_string);
|
defer client.allocator.free(host_c_string);
|
||||||
print_usize(host_string.len);
|
|
||||||
print_usize(host_c_string.len);
|
|
||||||
host_c_string[host_c_string.len - 1] = 0;
|
host_c_string[host_c_string.len - 1] = 0;
|
||||||
// @memcpy(host_c_string, host_string);
|
|
||||||
for (0..host_string.len) |i| {
|
for (0..host_string.len) |i| {
|
||||||
host_c_string[i] = host_string[i];
|
host_c_string[i] = host_string[i];
|
||||||
}
|
}
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
|
|
||||||
print("Request host is");
|
const result = cNet.dns_gethostbyname(@ptrCast(host_c_string), &cached_address, dns_callback, @ptrCast(@alignCast(&context)));
|
||||||
print(host_c_string);
|
|
||||||
print_usize(host_string.len);
|
|
||||||
print_usize(host_c_string.len);
|
|
||||||
if (context.request.url.host != null) {
|
|
||||||
print("HOST");
|
|
||||||
} else {
|
|
||||||
print("NOT HOST");
|
|
||||||
}
|
|
||||||
print(context.request.url.scheme);
|
|
||||||
const result = cNet.dns_gethostbyname(@ptrCast(host_c_string), &cached_address, dns_callback, @ptrCast(@alignCast(@constCast(&context))));
|
|
||||||
|
|
||||||
if (result == cNet.ERR_OK) {
|
if (result == cNet.ERR_OK) {
|
||||||
print("DNS resolution returned with OK");
|
print("DNS resolution returned with OK");
|
||||||
|
cNet.sleep_ms(10000);
|
||||||
|
open_tcp_connection(&cached_address, &context, @ptrCast(@alignCast(&context)));
|
||||||
|
context.finished = true;
|
||||||
} else if (result == cNet.ERR_INPROGRESS) {
|
} else if (result == cNet.ERR_INPROGRESS) {
|
||||||
print("DNS resolution returned with INPROGRESS");
|
print("DNS resolution returned with INPROGRESS");
|
||||||
} else if (result == cNet.ERR_ARG) {
|
} else if (result == cNet.ERR_ARG) {
|
||||||
print("DNS resolution returned with ERR_ARG");
|
print("DNS resolution returned with ERR_ARG");
|
||||||
|
context.finished = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Calling cyw43_arch_lwip_end");
|
print("Calling cyw43_arch_lwip_end");
|
||||||
cNet.cyw43_arch_lwip_end();
|
cNet.cyw43_arch_lwip_end();
|
||||||
print("Ending sendRequest...");
|
print("Ending sendRequest...");
|
||||||
|
|
||||||
while (!context.finished) {
|
while (!context.finished) {
|
||||||
cNet.sleep_ms(2000);
|
cNet.sleep_ms(2000);
|
||||||
print("loop...");
|
print("loop...");
|
||||||
|
|||||||
11
src/main.zig
11
src/main.zig
@@ -1,21 +1,22 @@
|
|||||||
pub const p = @cImport({
|
const std = @import("std");
|
||||||
|
const p = @cImport({
|
||||||
@cInclude("pico.h");
|
@cInclude("pico.h");
|
||||||
@cInclude("stdio.h");
|
@cInclude("stdio.h");
|
||||||
@cInclude("pico/stdlib.h");
|
@cInclude("pico/stdlib.h");
|
||||||
|
@cInclude("hardware/watchdog.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
const httpClient = @import("httpClient.zig");
|
const httpClient = @import("httpClient.zig");
|
||||||
const platform = @import("platform.zig");
|
const platform = @import("platform.zig");
|
||||||
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
const BUTTON_PIN = 15;
|
const BUTTON_PIN = 15;
|
||||||
const GPIO_IN = false;
|
const GPIO_IN = false;
|
||||||
|
|
||||||
pub const std_options: std.Options = .{ .page_size_max = 4 * 1024, .page_size_min = 4 * 1024 };
|
pub const std_options: std.Options = .{ .page_size_max = 4 * 1024, .page_size_min = 4 * 1024 };
|
||||||
|
|
||||||
fn print(text: []const u8) void {
|
fn print(text: []const u8) void {
|
||||||
_ = p.printf(text.ptr);
|
utils.print(text);
|
||||||
_ = p.printf("\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basically the pico_w blink sample
|
// Basically the pico_w blink sample
|
||||||
@@ -34,6 +35,7 @@ export fn main() c_int {
|
|||||||
platform.sleep_until_gpio_high(BUTTON_PIN);
|
platform.sleep_until_gpio_high(BUTTON_PIN);
|
||||||
|
|
||||||
// Resuming from here after wake up
|
// Resuming from here after wake up
|
||||||
|
// _ = p.stdio_init_all();
|
||||||
// platform.init_arch();
|
// platform.init_arch();
|
||||||
p.gpio_init(BUTTON_PIN);
|
p.gpio_init(BUTTON_PIN);
|
||||||
p.gpio_set_dir(BUTTON_PIN, GPIO_IN);
|
p.gpio_set_dir(BUTTON_PIN, GPIO_IN);
|
||||||
@@ -56,6 +58,7 @@ export fn main() c_int {
|
|||||||
|
|
||||||
platform.set_cyw43_led(false);
|
platform.set_cyw43_led(false);
|
||||||
p.sleep_ms(1000);
|
p.sleep_ms(1000);
|
||||||
|
p.watchdog_reboot(0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ pub const p = @cImport({
|
|||||||
@cInclude("pico/cyw43_arch.h");
|
@cInclude("pico/cyw43_arch.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const utils = @import("utils.zig");
|
||||||
fn print(text: []const u8) void {
|
fn print(text: []const u8) void {
|
||||||
_ = p.printf(text.ptr);
|
utils.print(text);
|
||||||
_ = p.printf("\r\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_arch() void {
|
pub fn init_arch() void {
|
||||||
|
|||||||
29
src/utils.zig
Normal file
29
src/utils.zig
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("pico.h");
|
||||||
|
@cInclude("stdio.h");
|
||||||
|
@cInclude("pico/stdlib.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn print(text: []const u8) void {
|
||||||
|
var buffer: [4096]u8 = undefined;
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
||||||
|
const allocator = fba.allocator();
|
||||||
|
const c_text = allocator.alloc(u8, text.len + 1) catch unreachable;
|
||||||
|
allocator.free(c_text);
|
||||||
|
|
||||||
|
for (0..text.len) |i| {
|
||||||
|
c_text[i] = text[i];
|
||||||
|
}
|
||||||
|
c_text[c_text.len - 1] = 0;
|
||||||
|
|
||||||
|
_ = c.printf(c_text.ptr);
|
||||||
|
_ = c.printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_i8(number: i8) void {
|
||||||
|
var buf: [10]u8 = undefined;
|
||||||
|
const data = std.fmt.bufPrint(&buf, "{d}\x00", .{number}) catch unreachable;
|
||||||
|
_ = c.printf(data.ptr);
|
||||||
|
_ = c.printf("\r\n");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user