6 changed files with 153 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||||
|
/*
|
||||
|
* GPCommand.cpp |
||||
|
* |
||||
|
* Created on: Jul 18, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#include "GPCommand.hpp" |
||||
|
#include "app_ble.h" |
||||
|
|
||||
|
GPResponse::GPResponse(uint16_t response_identifier, bool was_successful) |
||||
|
: Response(response_identifier) { |
||||
|
(this->payload_ptr + this->get_payload_size())[0] = (uint8_t)was_successful; |
||||
|
this->add_to_payload_size(1); |
||||
|
} |
||||
|
|
||||
|
GPRequest::GPRequest(uint8_t * payload_ptr, uint16_t size) : Request(payload_ptr, size) { |
||||
|
uint16_t expected_length = this->buffer_offset + 1; |
||||
|
if (expected_length != size) { |
||||
|
this->has_error = true; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
uint8_t * data_ptr = payload_ptr + this->buffer_offset; |
||||
|
this->command_id = data_ptr[0]; |
||||
|
} |
||||
|
|
||||
|
GPResponse * GPRequest::execute_request(uint16_t response_identifier) { |
||||
|
bool was_successful = true; |
||||
|
|
||||
|
if (this->has_error) { |
||||
|
was_successful = false; |
||||
|
} else { |
||||
|
switch (this->command_id) |
||||
|
{ |
||||
|
case 1: |
||||
|
APP_BLE_Key_Button1_Action(); |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return new GPResponse(response_identifier, was_successful); |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,47 @@ |
|||||
|
/*
|
||||
|
* GPCommand.hpp |
||||
|
* |
||||
|
* Created on: Jul 18, 2021 |
||||
|
* Author: Andreas Berthoud |
||||
|
*/ |
||||
|
|
||||
|
#ifndef GPCOMMAND_HPP_ |
||||
|
#define GPCOMMAND_HPP_ |
||||
|
|
||||
|
#include "Request.hpp" |
||||
|
#include "Response.hpp" |
||||
|
|
||||
|
class GPResponse : public Response { |
||||
|
public: |
||||
|
GPResponse(uint16_t response_identifier, bool was_successful); |
||||
|
|
||||
|
CommandId get_command_id() override { return COMMAND_GP_RESPONSE; } |
||||
|
|
||||
|
private: |
||||
|
bool was_successful = false; |
||||
|
}; |
||||
|
|
||||
|
class GPRequest : public Request { |
||||
|
public: |
||||
|
/**
|
||||
|
* @brief Construct a new general purpose request object |
||||
|
* |
||||
|
* @param payload_ptr |
||||
|
* HEADER | command_id | |
||||
|
* | 1 byte | |
||||
|
* |
||||
|
* @param size |
||||
|
*/ |
||||
|
GPRequest(uint8_t * payload_ptr, uint16_t size); |
||||
|
|
||||
|
|
||||
|
GPResponse * execute_request(uint16_t response_identifier); |
||||
|
|
||||
|
CommandId get_command_id() override { return COMMAND_GP_REQUEST; } |
||||
|
|
||||
|
private: |
||||
|
bool has_error = false; |
||||
|
int command_id = 0; |
||||
|
}; |
||||
|
|
||||
|
#endif /* GPCOMMAND_HPP_ */ |
||||
Loading…
Reference in new issue