Prusa MINI Firmware overview
connect.cpp File Reference
#include "lwsapi_app.hpp"
#include "http_states.h"
#include "dbg.h"
#include <cstring>
#include <cstdarg>
#include "../Marlin/src/module/temperature.h"
#include "res/cc/index_html.c"
#include "res/cc/index_css.c"
#include "res/cc/index_js.c"
#include "res/cc/favicon_ico.c"
#include "res/cc/connect_black_svg.c"
#include "res/cc/status_filament_svg.c"
#include "res/cc/status_heatbed_svg.c"
#include "res/cc/status_nozzle_svg.c"
#include "res/cc/status_prnflow_svg.c"
#include "res/cc/status_prnspeed_svg.c"
#include "res/cc/status_z_axis_svg.c"
#include "res/cc/under_construction_gif.c"

Classes

struct  header_factory_t
 
struct  FileHandler_t
 File handler structure. More...
 
class  FileResponse
 Class to response any static files defined in files array. More...
 
class  BufferResponse
 Universal response class with internal buffer size by BUFFER_RESPONSE_SIZE. More...
 
class  WaitResponse
 

Macros

#define LAST_MODIFY   __TIMESTAMP__
 
#define BUFFER_RESPONSE_SIZE   256
 

Enumerations

enum  State_t { FIRST, NEXT, DONE }
 Response state enumeration. More...
 

Functions

IResponse::unique_ptr_t api_job (Environment &env)
 return coroutine_fn for /api/job request More...
 
IResponse::unique_ptr_t api_printer (Environment &env)
 return coroutine_fn for /api/printer request More...
 
IResponse::unique_ptr_t api_post (Environment &env)
 return 200 OK when file is post right More...
 
IResponse::unique_ptr_t not_found (Environment &env)
 return coroutine_fn for Not Found response More...
 
IHeaderrequest_header (const char *key, size_t key_length, const char *value, size_t value_length)
 request_header_fn callbacke, which is call from LwIP WSAPI http server More...
 
IResponse::unique_ptr_t application (Environment &env)
 application_fn callback, which is called from LwIP WSAPI http server More...
 

Variables

const header_factory_t req_headers []
 
static const FileHandler_t files []
 Array of files which could be responed by http. More...
 

Macro Definition Documentation

◆ LAST_MODIFY

#define LAST_MODIFY   __TIMESTAMP__

◆ BUFFER_RESPONSE_SIZE

#define BUFFER_RESPONSE_SIZE   256

Enumeration Type Documentation

◆ State_t

enum State_t

Response state enumeration.

Enumerator
FIRST 

First coroutine_fn call after creation

NEXT 

Any other coroutine_fn call while all data is not sent

DONE 

Last coroutine_fn call, coroutine_fn must delete response object

76  {
77  FIRST, /**< First coroutine_fn call after creation */
78  NEXT, /**< Any other coroutine_fn call while all data is not sent */
79  DONE /**< Last coroutine_fn call, coroutine_fn must delete response
80  object */
81 };

Function Documentation

◆ api_job()

return coroutine_fn for /api/job request

Function create BufferResponse object, set its pointer to arg and return buffer_coroutine.

211  {
212  std::unique_ptr<BufferResponse> res(new BufferResponse());
213  if (res.get() != nullptr) {
214  res->response = HTTP_200;
215  res->ct_header.value = "application/json";
216  res->printf("{}");
217  }
218  return std::move(res);
219 }
Here is the caller graph for this function:

◆ api_printer()

IResponse::unique_ptr_t api_printer ( Environment env)

return coroutine_fn for /api/printer request

Function create BufferResponse object, set its pointer to arg and return buffer_coroutine.

226  {
227  std::unique_ptr<BufferResponse> res(new BufferResponse());
228  if (res.get() != nullptr) {
229  res->response = HTTP_200;
230  res->ct_header.value = "application/json";
231 
232 #ifndef TEST_INTEGRITY
233  float actual_nozzle = thermalManager.degHotend(0);
234  float target_nozzle = thermalManager.degTargetHotend(0);
235  float actual_heatbed = thermalManager.degBed();
236  float target_heatbed = thermalManager.degTargetBed();
237 #else
238  float actual_nozzle = 26;
239  float target_nozzle = 32;
240  float actual_heatbed = 55;
241  float target_heatbed = 16;
242 #endif
243 
244  res->printf(
245  "{\"temperature\":{"
246  "\"tool0\":{\"actual\":%f, \"target\":%f},"
247  "\"bed\":{\"actual\":%f, \"target\":%f}}}",
248  static_cast<double>(actual_nozzle), static_cast<double>(target_nozzle),
249  static_cast<double>(actual_heatbed), static_cast<double>(target_heatbed));
250  }
251  return std::move(res);
252 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ api_post()

IResponse::unique_ptr_t api_post ( Environment env)

return 200 OK when file is post right

257  {
258  printf("Request headers:\n");
259  size_t content_length = 0;
260  for (auto it = env.get_headers(); it != nullptr; it = it->next) {
261  char buff[80];
262  it->snprintf(buff); // TODO: volitelna velikost bufferu :D
263  printf(buff);
264  if (!strcmp(it->key, "Content-Length")) {
265  content_length = dynamic_cast<const NumberHeader *>(it)->value;
266  }
267  }
268 
269  std::unique_ptr<WaitResponse> res(new WaitResponse(content_length));
270  return res;
271 }
Here is the caller graph for this function:

◆ not_found()

IResponse::unique_ptr_t not_found ( Environment env)

return coroutine_fn for Not Found response

Function create BufferResponse object, set its pointer to arg and return buffer_coroutine.

277  {
278  std::unique_ptr<BufferResponse> res(new BufferResponse());
279  if (res.get() != nullptr) {
280  res->response = HTTP_404;
281  res->ct_header.value = "text/plain";
282 
283  res->printf("Not Found");
284  }
285  return res;
286 }
Here is the caller graph for this function:

◆ request_header()

IHeader* request_header ( const char *  key,
size_t  key_length,
const char *  value,
size_t  value_length 
)

request_header_fn callbacke, which is call from LwIP WSAPI http server

290  {
291  for (size_t i = 0; i < sizeof(req_headers) / sizeof(header_factory_t); i++) {
292  if (!std::strncmp(req_headers[i].key, key, key_length)) {
293  return req_headers[i].factory(req_headers[i].key, value, value_length);
294  }
295  }
296  return nullptr;
297 }

◆ application()

IResponse::unique_ptr_t application ( Environment env)

application_fn callback, which is called from LwIP WSAPI http server

Define of application functions.

Parameters
envrequest environment defined in lwsapi.h
argpointer to pointer to internal application reqeust object. This pointer is argument of coroutine_fn funtcions.
Returns
coroutine_fn for response
306  {
307 
308  _dbg("HTTP Request: %s %s", env.method, env.request_uri);
309 
310  if (!strcmp(env.method, "GET") || !strcmp(env.method, "HEAD")) {
311  // Static files
312  for (size_t i = 0; i < (sizeof(files) / sizeof(FileHandler_t)); i++) {
313  if (!strcmp(env.request_uri, files[i].file_name)) {
314  return std::unique_ptr<FileResponse>(new FileResponse(&files[i]));
315  }
316  }
317 
318  if (!strcmp(env.request_uri, "/api/job")) {
319  return api_job(env);
320  } else if (!strcmp(env.request_uri, "/api/printer")) {
321  return api_printer(env);
322  }
323  } else if (!strcmp(env.method, "POST")) {
324  if (!strcmp(env.request_uri, "/post")) {
325  return api_post(env);
326  }
327  }
328  return not_found(env);
329 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ req_headers

const header_factory_t req_headers[]
Initial value:
= {
{ "Content-Type", &dynamics_header_factory },
{ "Content-Length", &number_header_factory },
}

◆ files

const FileHandler_t files[]
static
Initial value:
= {
{ "/", "text/html", sizeof(index_html), index_html },
{ "/index.css", "text/css", sizeof(index_css), index_css },
{ "/index.js", "application/javasript", sizeof(index_js), index_js },
{ "/favicon.ico", "image/x-icon", sizeof(favicon_ico), favicon_ico },
{ "/connect_black.svg", "image/svg+xml", sizeof(connect_black_svg),
{ "/status_filament.svg", "image/svg+xml", sizeof(status_filament_svg),
{ "/status_heatbed.svg", "image/svg+xml", sizeof(status_heatbed_svg),
{ "/status_nozzle.svg", "image/svg+xml", sizeof(status_nozzle_svg),
{ "/status_prnflow.svg", "image/svg+xml", sizeof(status_prnflow_svg),
{ "/status_prnspeed.svg", "image/svg+xml", sizeof(status_prnspeed_svg),
{ "/status_z_axis.svg", "image/svg+xml", sizeof(status_z_axis_svg),
{ "/under_construction.gif", "image/gif", sizeof(under_construction_gif),
}

Array of files which could be responed by http.

header_factory_t
Definition: connect.cpp:32
api_job
IResponse::unique_ptr_t api_job(Environment &env)
return coroutine_fn for /api/job request
Definition: connect.cpp:211
status_heatbed_svg
const uint8_t status_heatbed_svg[]
Definition: status_heatbed_svg.c:3
Temperature::degTargetHotend
static FORCE_INLINE int16_t degTargetHotend(const uint8_t E_NAME)
Definition: temperature.h:562
index_js
const uint8_t index_js[]
Definition: index_js.c:3
HTTP_404
const char * HTTP_404
Definition: http_states.c:5
status_filament_svg
const uint8_t status_filament_svg[]
Definition: status_filament_svg.c:3
favicon_ico
const uint8_t favicon_ico[]
Definition: favicon_ico.c:3
not_found
IResponse::unique_ptr_t not_found(Environment &env)
return coroutine_fn for Not Found response
Definition: connect.cpp:277
files
static const FileHandler_t files[]
Array of files which could be responed by http.
Definition: connect.cpp:52
api_post
IResponse::unique_ptr_t api_post(Environment &env)
return 200 OK when file is post right
Definition: connect.cpp:257
number_header_factory
IHeader * number_header_factory(const char *key, const char *value, size_t value_length)
Response new NumberHeader.
Definition: lwsapi.cpp:445
FileResponse
Class to response any static files defined in files array.
Definition: connect.cpp:84
i
uint8_t i
Definition: screen_test_graph.c:72
status_prnspeed_svg
const uint8_t status_prnspeed_svg[]
Definition: status_prnspeed_svg.c:3
Temperature::degHotend
static FORCE_INLINE float degHotend(const uint8_t E_NAME)
Definition: temperature.h:544
FIRST
Definition: connect.cpp:77
dynamics_header_factory
IHeader * dynamics_header_factory(const char *key, const char *value, size_t value_length)
Return new DynamicsHeader.
Definition: lwsapi.cpp:440
NEXT
Definition: connect.cpp:78
status_nozzle_svg
const uint8_t status_nozzle_svg[]
Definition: status_nozzle_svg.c:3
req_headers
const header_factory_t req_headers[]
Definition: connect.cpp:37
status_prnflow_svg
const uint8_t status_prnflow_svg[]
Definition: status_prnflow_svg.c:3
under_construction_gif
const uint8_t under_construction_gif[]
Definition: under_construction_gif.c:3
DONE
Definition: connect.cpp:79
create_custom_upload_command_CDC.env
env
Definition: create_custom_upload_command_CDC.py:23
HTTP_200
const char * HTTP_200
Definition: http_states.c:3
header_factory_t::factory
const header_factory_fn factory
Definition: connect.cpp:34
_dbg
#define _dbg(...)
Definition: dbg.h:50
index_css
const uint8_t index_css[]
Definition: index_css.c:3
FileHandler_t::file_name
const char * file_name
Definition: connect.cpp:45
index_html
const uint8_t index_html[]
Definition: index_html.c:3
FileHandler_t
File handler structure.
Definition: connect.cpp:44
BufferResponse
Universal response class with internal buffer size by BUFFER_RESPONSE_SIZE.
Definition: connect.cpp:119
connect_black_svg
const uint8_t connect_black_svg[]
Definition: connect_black_svg.c:3
WaitResponse
Definition: connect.cpp:157
status_z_axis_svg
const uint8_t status_z_axis_svg[]
Definition: status_z_axis_svg.c:3
thermalManager
Temperature thermalManager
Definition: temperature.cpp:89
api_printer
IResponse::unique_ptr_t api_printer(Environment &env)
return coroutine_fn for /api/printer request
Definition: connect.cpp:226