feat: main loop
This commit is contained in:
@@ -40,14 +40,17 @@ pub const HttpRequest = struct {
|
|||||||
body: []const u8,
|
body: []const u8,
|
||||||
headers: []const HttpHeader,
|
headers: []const HttpHeader,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const HttpResponse = struct {};
|
||||||
|
|
||||||
pub const HttpHeader = struct {
|
pub const HttpHeader = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
value: []const u8,
|
value: []const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const HttpContext = struct { client: *const Client, request: *const HttpRequest };
|
pub const HttpContext = struct { client: *const Client, request: *const HttpRequest, response: ?*const HttpResponse = null, finished: bool = false };
|
||||||
|
|
||||||
fn tcp_recv_callback(_: ?*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");
|
print("asd2");
|
||||||
if (p1 == null) {
|
if (p1 == null) {
|
||||||
print("asd3");
|
print("asd3");
|
||||||
@@ -58,6 +61,10 @@ fn tcp_recv_callback(_: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf, _: cN
|
|||||||
_ = cNet.pbuf_free(p1);
|
_ = cNet.pbuf_free(p1);
|
||||||
}
|
}
|
||||||
print("asd4");
|
print("asd4");
|
||||||
|
const http_context: *HttpContext = @ptrCast(@alignCast(context));
|
||||||
|
const response = http_context.client.allocator.create(HttpResponse) catch unreachable;
|
||||||
|
http_context.response = response;
|
||||||
|
http_context.finished = true;
|
||||||
// std.debug.print("Received data: {s}\n", .{@ptrCast([*]const u8, p1.?.payload)});
|
// std.debug.print("Received data: {s}\n", .{@ptrCast([*]const u8, p1.?.payload)});
|
||||||
return cNet.ERR_OK;
|
return cNet.ERR_OK;
|
||||||
}
|
}
|
||||||
@@ -158,11 +165,9 @@ fn dns_callback(_: [*c]const u8, ipaddr: [*c]const cNet.struct_ip4_addr, context
|
|||||||
|
|
||||||
pub const Client = struct {
|
pub const Client = struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
pub fn sendRequest(client: *const Client, request: *const HttpRequest) !void {
|
pub fn sendRequest(client: *const Client, request: *const HttpRequest) !?*const HttpResponse {
|
||||||
print("Client.sendRequest");
|
print("Client.sendRequest");
|
||||||
const asd: *const HttpContext = &.{ .client = client, .request = request };
|
const context: HttpContext = .{ .client = client, .request = request };
|
||||||
const context: *HttpContext = @constCast(asd);
|
|
||||||
print_usize(@intFromPtr(context));
|
|
||||||
|
|
||||||
cNet.cyw43_arch_lwip_begin();
|
cNet.cyw43_arch_lwip_begin();
|
||||||
var cached_address = cNet.ip_addr_t{};
|
var cached_address = cNet.ip_addr_t{};
|
||||||
@@ -203,14 +208,13 @@ pub const Client = struct {
|
|||||||
print(host_c_string);
|
print(host_c_string);
|
||||||
print_usize(host_string.len);
|
print_usize(host_string.len);
|
||||||
print_usize(host_c_string.len);
|
print_usize(host_c_string.len);
|
||||||
print_usize(@intFromPtr(context));
|
|
||||||
if (context.request.url.host != null) {
|
if (context.request.url.host != null) {
|
||||||
print("HOST");
|
print("HOST");
|
||||||
} else {
|
} else {
|
||||||
print("NOT HOST");
|
print("NOT HOST");
|
||||||
}
|
}
|
||||||
print(context.request.url.scheme);
|
print(context.request.url.scheme);
|
||||||
const result = cNet.dns_gethostbyname(@ptrCast(host_c_string), &cached_address, dns_callback, @ptrCast(@alignCast(context)));
|
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");
|
||||||
@@ -222,9 +226,12 @@ pub const Client = struct {
|
|||||||
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 (true) {
|
while (!context.finished) {
|
||||||
cNet.sleep_ms(2000);
|
cNet.sleep_ms(2000);
|
||||||
print("loop...");
|
print("loop...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("Request finished");
|
||||||
|
return context.response;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
117
src/main.zig
117
src/main.zig
@@ -21,7 +21,7 @@ fn print(text: []const u8) void {
|
|||||||
// Basically the pico_w blink sample
|
// Basically the pico_w blink sample
|
||||||
export fn main() c_int {
|
export fn main() c_int {
|
||||||
_ = p.stdio_init_all();
|
_ = p.stdio_init_all();
|
||||||
p.sleep_ms(5000);
|
p.sleep_ms(2000);
|
||||||
print("Starting ...");
|
print("Starting ...");
|
||||||
|
|
||||||
p.gpio_init(BUTTON_PIN);
|
p.gpio_init(BUTTON_PIN);
|
||||||
@@ -29,97 +29,37 @@ export fn main() c_int {
|
|||||||
|
|
||||||
platform.init_arch();
|
platform.init_arch();
|
||||||
|
|
||||||
// platform.sleep_until_gpio_high(BUTTON_PIN);
|
|
||||||
|
|
||||||
print("Connecting to wifi...");
|
|
||||||
platform.connect_wifi(@embedFile("wifi.txt"), @embedFile("password.txt"));
|
|
||||||
print("Connected!");
|
|
||||||
|
|
||||||
main2() catch unreachable;
|
|
||||||
|
|
||||||
// p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, true);
|
|
||||||
// p.sleep_ms(200);
|
|
||||||
// p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, false);
|
|
||||||
// p.sleep_ms(200);
|
|
||||||
|
|
||||||
// p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, false);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// // printf("Switching to XOSC\n");
|
print("Going to sleep");
|
||||||
// p.uart_default_tx_wait_blocking();
|
platform.sleep_until_gpio_high(BUTTON_PIN);
|
||||||
//
|
|
||||||
// // Set the crystal oscillator as the dormant clock source, UART will be reconfigured from here
|
// Resuming from here after wake up
|
||||||
// // This is necessary before sending the pico into dormancy
|
// platform.init_arch();
|
||||||
// p.sleep_run_from_xosc();
|
p.gpio_init(BUTTON_PIN);
|
||||||
//
|
p.gpio_set_dir(BUTTON_PIN, GPIO_IN);
|
||||||
// // printf("Going dormant until GPIO %d goes edge high\n", WAKE_GPIO);
|
platform.set_cyw43_led(true);
|
||||||
// p.uart_default_tx_wait_blocking();
|
|
||||||
//
|
print("Connecting to wifi...");
|
||||||
// // Go to sleep until we see a high edge on GPIO 10
|
platform.connect_wifi(@embedFile("wifi.txt"), @embedFile("password.txt"));
|
||||||
// p.sleep_goto_dormant_until_edge_high(BUTTON_PIN);
|
print("Connected!");
|
||||||
//
|
|
||||||
// p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, true);
|
send_doorbell_notification() catch unreachable;
|
||||||
// // Re-enabling clock sources and generators.
|
|
||||||
// p.sleep_power_up();
|
print("Disconnecting from wifi...");
|
||||||
// _ = p.stdio_init_all();
|
platform.disconnect_wifi();
|
||||||
// printf("Now awake for 10s\n");
|
print("Disconnected!");
|
||||||
platform.set_cyw43_led(true);
|
|
||||||
p.sleep_ms(5000);
|
while (p.gpio_get(BUTTON_PIN)) {
|
||||||
|
print("Button is still pressed");
|
||||||
|
p.sleep_ms(2000);
|
||||||
|
}
|
||||||
|
|
||||||
// while (p.gpio_get(BUTTON_PIN)) {
|
|
||||||
// p.sleep_ms(50);
|
|
||||||
// }
|
|
||||||
platform.set_cyw43_led(false);
|
platform.set_cyw43_led(false);
|
||||||
p.sleep_ms(2000);
|
p.sleep_ms(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn tcp_recv_callback(_: ?*anyopaque, pcb: ?*c.tcp_pcb, p1: ?*c.pbuf, _: c.err_t) callconv(.C) c.err_t {
|
pub fn send_doorbell_notification() !void {
|
||||||
// if (p1 == null) {
|
|
||||||
// _ = c.tcp_close(pcb);
|
|
||||||
// return c.ERR_OK;
|
|
||||||
// }
|
|
||||||
// defer {
|
|
||||||
// _ = c.pbuf_free(p1);
|
|
||||||
// }
|
|
||||||
// // std.debug.print("Received data: {s}\n", .{@ptrCast([*]const u8, p1.?.payload)});
|
|
||||||
// return c.ERR_OK;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn send_http_request(pcb: ?*c.tcp_pcb) void {
|
|
||||||
// const request = "POST /todo-channel-name HTTP/1.1\r\nHost: ntfy.sh\r\nTitle: Csengo\r\nContent-Length: 6\r\n\r\nCsengo";
|
|
||||||
// _ = c.tcp_write(pcb, request, request.len, c.TCP_WRITE_FLAG_COPY);
|
|
||||||
// _ = c.tcp_output(pcb);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn tcp_connected_callback(_: ?*anyopaque, pcb: ?*c.tcp_pcb, err: c.err_t) callconv(.C) c.err_t {
|
|
||||||
// if (err != c.ERR_OK) {
|
|
||||||
// return err;
|
|
||||||
// }
|
|
||||||
// // std.debug.print("Connected to server\n", .{});
|
|
||||||
// _ = c.tcp_recv(pcb, tcp_recv_callback);
|
|
||||||
// send_http_request(pcb);
|
|
||||||
// return c.ERR_OK;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn dns_callback(_: [*c]const u8, ipaddr: [*c]const c.struct_ip4_addr, _: ?*anyopaque) callconv(.C) void {
|
|
||||||
// if (ipaddr) |addr| {
|
|
||||||
// const pcb = c.tcp_new();
|
|
||||||
// if (pcb == null) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// _ = c.tcp_connect(pcb, addr, 80, tcp_connected_callback);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn main2() !void {
|
|
||||||
// c.cyw43_arch_lwip_begin();
|
|
||||||
// var cached_address = c.ip_addr_t{};
|
|
||||||
//
|
|
||||||
// const result = c.dns_gethostbyname("ntfy.sh", &cached_address, dns_callback, null);
|
|
||||||
//
|
|
||||||
// if (result == c.ERR_OK) {} else if (result == c.ERR_INPROGRESS) {} else if (result == c.ERR_ARG) {}
|
|
||||||
// c.cyw43_arch_lwip_end();
|
|
||||||
|
|
||||||
var store: [8192]u8 = undefined;
|
var store: [8192]u8 = undefined;
|
||||||
var fba = std.heap.FixedBufferAllocator.init(&store);
|
var fba = std.heap.FixedBufferAllocator.init(&store);
|
||||||
const allocator = fba.allocator();
|
const allocator = fba.allocator();
|
||||||
@@ -129,5 +69,8 @@ pub fn main2() !void {
|
|||||||
const request = &httpClient.HttpRequest{ .method = .POST, .url = try std.Uri.parse("http://ntfy.sh/todo-channel-name"), .body = "Csengo", .headers = &[_]httpClient.HttpHeader{
|
const request = &httpClient.HttpRequest{ .method = .POST, .url = try std.Uri.parse("http://ntfy.sh/todo-channel-name"), .body = "Csengo", .headers = &[_]httpClient.HttpHeader{
|
||||||
httpClient.HttpHeader{ .name = "Title", .value = "Csengo" },
|
httpClient.HttpHeader{ .name = "Title", .value = "Csengo" },
|
||||||
} };
|
} };
|
||||||
try client.sendRequest(request);
|
const response = try client.sendRequest(request);
|
||||||
|
if (response) |r| {
|
||||||
|
defer allocator.destroy(r);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ pub fn connect_wifi(ssid: []const u8, password: []const u8) void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn disconnect_wifi() void {
|
||||||
|
p.cyw43_arch_disable_sta_mode();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_cyw43_led(input: bool) void {
|
pub fn set_cyw43_led(input: bool) void {
|
||||||
p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, input);
|
p.cyw43_arch_gpio_put(p.CYW43_WL_GPIO_LED_PIN, input);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user