1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 17 #include <cstddef> 18 #include <cstdint> 19 20 #include "pw_chre/host_link.h" 21 22 namespace pw::chre { 23 24 /// A message to be sent to a CHRE nanoapp. 25 /// This message originated from the Application Processor (AP). 26 struct NanoappMessage { 27 /// The id of the nanoapp this message is sent to. 28 uint64_t nano_app_id; 29 /// The type of message this is. 30 uint32_t message_type; 31 /// The id of the host on the AP that sent this request. 32 uint16_t host_endpoint; 33 /// The actual message data. 34 const uint8_t* data; 35 /// The size in bytes of the message data. 36 size_t length; 37 }; 38 39 /// Initialize the CHRE environment and load any static nanoapps that exist. 40 /// This must be called before the event loop has been started. 41 void Init(); 42 43 /// Teardown the CHRE environment. 44 /// This must be called after Init and after the event loop has been stopped. 45 void Deinit(); 46 47 /// Run the CHRE event loop. 48 /// This function will not return until `StopEventLoop` is called. 49 void RunEventLoop(); 50 51 /// Stop the CHRE event loop. 52 /// This can be called from any thread. 53 void StopEventLoop(); 54 55 /// Send a message to a nano app. 56 /// This can be called from any thread. 57 /// @param[in] message The message being send to the nano app. 58 void SendMessageToNanoapp(NanoappMessage message); 59 60 /// Free a message that CHRE created to send to the AP (via `SendMessageToAp`). 61 /// This function must be called after the message is finishd being used. 62 /// After this function is called, the message data must not be accessed. 63 /// This can be called from any thread. 64 /// @param[in] context The message being freed. 65 void FreeMessageToAp(MessageToApContext context); 66 67 /// Set the estimated offset between the AP time and CHRE's time. 68 /// @param[in] offset The offset time in nanoseconds. 69 void SetEstimatedHostTimeOffset(int64_t offset); 70 71 } // namespace pw::chre 72