|
| 1 | +// Warning: This example is compiling for target `thumbv7m-none-eabi` |
| 2 | +// but have never been tested for this target. |
| 3 | +// |
| 4 | +// Treat it as a reference only! |
| 5 | + |
| 6 | +#![no_std] |
| 7 | +#![no_main] |
| 8 | + |
| 9 | +extern crate alloc; |
| 10 | + |
| 11 | +use core::{ |
| 12 | + alloc::{GlobalAlloc, Layout}, |
| 13 | + panic::PanicInfo, |
| 14 | +}; |
| 15 | + |
| 16 | +use alloc::string::String; |
| 17 | +use pubnub::{ |
| 18 | + core::{ |
| 19 | + transport::{blocking::Transport, PUBNUB_DEFAULT_BASE_URL}, |
| 20 | + transport_request::TransportRequest, |
| 21 | + transport_response::TransportResponse, |
| 22 | + PubNubError, |
| 23 | + }, |
| 24 | + Keyset, PubNubClientBuilder, |
| 25 | +}; |
| 26 | +use serde::Serialize; |
| 27 | + |
| 28 | +#[derive(Debug, Serialize)] |
| 29 | +struct State { |
| 30 | + is_doing: String, |
| 31 | +} |
| 32 | + |
| 33 | +// As getrandom crate has limited support of targets, we need to provide custom |
| 34 | +// implementation of `getrandom` function. |
| 35 | +getrandom::register_custom_getrandom!(custom_random); |
| 36 | +fn custom_random(buf: &mut [u8]) -> Result<(), getrandom::Error> { |
| 37 | + // We're using `42` as a random number, because it's the answer |
| 38 | + // to the Ultimate Question of Life, the Universe, and Everything. |
| 39 | + // In your program, you should use proper random number generator that is supported by your target. |
| 40 | + for i in buf.iter_mut() { |
| 41 | + *i = 42; |
| 42 | + } |
| 43 | + |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +// Many targets have very specific requirements for networking, so it's hard to |
| 48 | +// provide a generic implementation. |
| 49 | +// Depending on the target, you will probably need to implement `Transport` trait. |
| 50 | +struct MyTransport; |
| 51 | + |
| 52 | +impl Transport for MyTransport { |
| 53 | + fn send(&self, _: TransportRequest) -> Result<TransportResponse, PubNubError> { |
| 54 | + let _hostname = PUBNUB_DEFAULT_BASE_URL; |
| 55 | + |
| 56 | + // Send your request here |
| 57 | + |
| 58 | + Ok(TransportResponse::default()) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// As our target does not have `std` library, we need to provide custom |
| 63 | +// implementation of `GlobalAlloc` trait. |
| 64 | +// |
| 65 | +// In your program, you should use proper allocator that is supported by your target. |
| 66 | +// Here you have dummy implementation that does nothing. |
| 67 | +#[derive(Default)] |
| 68 | +pub struct Allocator; |
| 69 | + |
| 70 | +unsafe impl GlobalAlloc for Allocator { |
| 71 | + unsafe fn alloc(&self, _: Layout) -> *mut u8 { |
| 72 | + core::ptr::null_mut() |
| 73 | + } |
| 74 | + unsafe fn dealloc(&self, _: *mut u8, _layout: Layout) {} |
| 75 | +} |
| 76 | + |
| 77 | +#[global_allocator] |
| 78 | +static GLOBAL_ALLOCATOR: Allocator = Allocator; |
| 79 | + |
| 80 | +// As our target does not have `std` library, we need to provide custom |
| 81 | +// implementation of `panic_handler`. |
| 82 | +// |
| 83 | +// In your program, you should use proper panic handler that is supported by your target. |
| 84 | +// Here you have dummy implementation that does nothing. |
| 85 | +#[panic_handler] |
| 86 | +fn panicking(_: &PanicInfo) -> ! { |
| 87 | + loop {} |
| 88 | +} |
| 89 | + |
| 90 | +// As we're using `no_main` attribute, we need to define `main` function manually. |
| 91 | +// For this example we're using `extern "C"` ABI to make it work. |
| 92 | +#[no_mangle] |
| 93 | +pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> usize { |
| 94 | + publish_example().map(|_| 0).unwrap() |
| 95 | +} |
| 96 | + |
| 97 | +// In standard subscribe examples we use `println` macro to print the result of the operation |
| 98 | +// and it shows the idea of the example. `no_std` does not support `println` macro, |
| 99 | +// so we're using `do_a_thing` function instead. |
| 100 | +fn do_a_thing<T>(_: &T) {} |
| 101 | + |
| 102 | +// As `no_std` does not support `Error` trait, we use `PubNubError` instead. |
| 103 | +// In your program, you should handle the error properly for your use case. |
| 104 | +fn publish_example() -> Result<(), PubNubError> { |
| 105 | + // As `no_std` does not support `env::var`, you need to set the keys manually. |
| 106 | + let publish_key = "SDK_PUB_KEY"; |
| 107 | + let subscribe_key = "SDK_SUB_KEY"; |
| 108 | + |
| 109 | + let client = PubNubClientBuilder::with_blocking_transport(MyTransport) |
| 110 | + .with_keyset(Keyset { |
| 111 | + subscribe_key, |
| 112 | + publish_key: Some(publish_key), |
| 113 | + secret_key: None, |
| 114 | + }) |
| 115 | + .with_user_id("user_id") |
| 116 | + .build()?; |
| 117 | + |
| 118 | + // As `no_std` does not support `println` macro, we're using `do_a_thing` function instead |
| 119 | + // to show possible usage of the result. |
| 120 | + |
| 121 | + client |
| 122 | + .set_presence_state(State { |
| 123 | + is_doing: "Nothing... Just hanging around...".into(), |
| 124 | + }) |
| 125 | + .channels(["my_channel".into(), "other_channel".into()].to_vec()) |
| 126 | + .user_id("user_id") |
| 127 | + .execute_blocking()?; |
| 128 | + |
| 129 | + let states = client |
| 130 | + .get_presence_state() |
| 131 | + .channels(["my_channel".into(), "other_channel".into()].to_vec()) |
| 132 | + .user_id("user_id") |
| 133 | + .execute_blocking()?; |
| 134 | + |
| 135 | + do_a_thing(&states); |
| 136 | + |
| 137 | + states.iter().for_each(|channel| { |
| 138 | + do_a_thing(&channel.channel); |
| 139 | + do_a_thing(&channel.state); |
| 140 | + }); |
| 141 | + |
| 142 | + Ok(()) |
| 143 | +} |
0 commit comments