Skip to content

Commit

Permalink
Added thread names to services
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensElflein committed Nov 8, 2024
1 parent d9120a4 commit ed19d84
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
7 changes: 7 additions & 0 deletions codegen/templates/ServiceTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,11 @@ bool ServiceTemplateBase::setRegister(uint16_t target_id, const void *payload, s
return false;
}
return false;
}

/*[[[cog
cog.outl(f"const char* {service['class_name']}::GetName() {{")
]]]*/
//[[[end]]]
return (const char*)SERVICE_NAME;
}
8 changes: 8 additions & 0 deletions codegen/templates/ServiceTemplate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class ServiceTemplateBase : public xbot::service::Service {
#endif
}

/*[[[cog
cog.outl(f"\u002f*\n{service['service_json']}\n*\u002f")
cog.outl(f"static constexpr unsigned char SERVICE_NAME[] = \"{service['type']}\";")
]]]*/
//[[[end]]]

const char* GetName() override;

/*[[[cog
cog.outl(f"\u002f*\n{service['service_json']}\n*\u002f")
cog.out("static constexpr unsigned char SERVICE_DESCRIPTION_CBOR[] = ");
Expand Down
33 changes: 14 additions & 19 deletions libxbot-service/include/xbot-service/Service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
namespace xbot::service {
class Service : public ServiceIo {
public:
explicit Service(uint16_t service_id, uint32_t tick_rate_micros,
void *processing_thread_stack,
explicit Service(uint16_t service_id, uint32_t tick_rate_micros, void *processing_thread_stack,
size_t processing_thread_stack_size);

virtual ~Service();
Expand All @@ -37,15 +36,12 @@ class Service : public ServiceIo {
* @param service Pointer to the service to start
* @return null
*/
static void startProcessingHelper(void *service) {
static_cast<Service *>(service)->runProcessing();
}
static void startProcessingHelper(void *service) { static_cast<Service *>(service)->runProcessing(); }

protected:
// Buffer to serialize service announcements and also custom serialized data
// (zcbor) or transactions.
uint8_t
scratch_buffer[config::max_packet_size - sizeof(datatypes::XbotHeader)];
uint8_t scratch_buffer[config::max_packet_size - sizeof(datatypes::XbotHeader)];

// Track how much of the scratch_buffer is already full
size_t scratch_buffer_fill_ = 0;
Expand Down Expand Up @@ -85,6 +81,11 @@ class Service : public ServiceIo {
*/
virtual void OnStop() = 0;

/**
* Gets the service name
*/
virtual const char *GetName() = 0;

private:
/**
* The main thread for the service.
Expand All @@ -111,14 +112,10 @@ class Service : public ServiceIo {

void runProcessing();

void HandleClaimMessage(datatypes::XbotHeader *header, const void *payload,
size_t payload_len);
void HandleDataMessage(datatypes::XbotHeader *header, const void *payload,
size_t payload_len);
void HandleDataTransaction(datatypes::XbotHeader *header, const void *payload,
size_t payload_len);
void HandleConfigurationTransaction(datatypes::XbotHeader *header,
const void *payload, size_t payload_len);
void HandleClaimMessage(datatypes::XbotHeader *header, const void *payload, size_t payload_len);
void HandleDataMessage(datatypes::XbotHeader *header, const void *payload, size_t payload_len);
void HandleDataTransaction(datatypes::XbotHeader *header, const void *payload, size_t payload_len);
void HandleConfigurationTransaction(datatypes::XbotHeader *header, const void *payload, size_t payload_len);

void fillHeader();

Expand All @@ -133,11 +130,9 @@ class Service : public ServiceIo {
virtual bool isConfigured() = 0;
virtual void clearConfiguration() = 0;

virtual bool handleData(uint16_t target_id, const void *payload,
size_t length) = 0;
virtual bool handleData(uint16_t target_id, const void *payload, size_t length) = 0;

virtual bool setRegister(uint16_t target_id, const void *payload,
size_t length) = 0;
virtual bool setRegister(uint16_t target_id, const void *payload, size_t length) = 0;
};
} // namespace xbot::service

Expand Down
2 changes: 1 addition & 1 deletion libxbot-service/include/xbot-service/portable/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace xbot::service::thread {
typedef XBOT_THREAD_TYPEDEF* ThreadPtr;

bool initialize(ThreadPtr thread, void (*threadfunc)(void*), void* arg,
void* stackbuf, size_t buflen);
void* stackbuf, size_t buflen, const char* name);
void deinitialize(ThreadPtr thread);
} // namespace xbot::service::thread

Expand Down
2 changes: 1 addition & 1 deletion libxbot-service/src/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool xbot::service::Service::start() {

if (!thread::initialize(&process_thread_, Service::startProcessingHelper,
this, processing_thread_stack_,
processing_thread_stack_size_)) {
processing_thread_stack_size_, GetName())) {
return false;
}

Expand Down

0 comments on commit ed19d84

Please sign in to comment.