µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
usb-device.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the µOS++ distribution.
3  * (https://github.com/micro-os-plus)
4  * Copyright (c) 2016 Liviu Ionescu.
5  * Copyright (c) 2013-2014 ARM Ltd.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use,
11  * copy, modify, merge, publish, distribute, sublicense, and/or
12  * sell copies of the Software, and to permit persons to whom
13  * the Software is furnished to do so, subject to the following
14  * conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 /*
30  * The code is inspired by ARM CMSIS Driver_USBD.h file, v2.01,
31  * and tries to remain functionally close to the CMSIS specifications.
32  */
33 
35 #include <cmsis-plus/diag/trace.h>
36 #include <cassert>
37 
38 // ----------------------------------------------------------------------------
39 
40 namespace os
41 {
42  namespace driver
43  {
44  namespace usb
45  {
46  // ----------------------------------------------------------------------
47 
48  Device::Device () noexcept
49  {
50  trace::printf ("%s() %p\n", __func__, this);
51 
52  cb_device_func_ = nullptr;
53  cb_device_object_ = nullptr;
54 
55  cb_endpoint_func_ = nullptr;
56  cb_endpoint_object_ = nullptr;
57  }
58 
59  Device::~Device () noexcept
60  {
61  trace::printf ("%s() %p\n", __func__, this);
62  }
63 
64  void
66  const void* cb_object) noexcept
67  {
68  cb_device_func_ = cb_func;
69  cb_device_object_ = cb_object;
70  }
71 
72  void
74  device::signal_endpoint_event_t cb_func, const void* cb_object) noexcept
75  {
76  cb_endpoint_func_ = cb_func;
77  cb_endpoint_object_ = cb_object;
78  }
79 
80  // ----------------------------------------------------------------------
81 
82  return_t
83  Device::read_setup_packet (uint8_t* buf) noexcept
84  {
85  assert (buf != nullptr);
86  return do_read_setup_packet (buf);
87  }
88 
89  return_t
90  Device::transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept
91  {
92  assert (data != nullptr);
93  if (num == 0)
94  {
95  return RETURN_OK;
96  }
97  return do_transfer (ep_addr, data, num);
98  }
99 
100  // ----------------------------------------------------------------------
101 
102  void
104  {
105  if (cb_device_func_ != nullptr)
106  {
107  // Forward event to registered callback.
109  }
110  }
111 
112  void
114  {
115  if (cb_endpoint_func_ != nullptr)
116  {
117  // Forward event to registered callback.
118  cb_endpoint_func_ (cb_endpoint_object_, ep_addr, event);
119  }
120  }
121 
122  } /* namespace usb */
123  } /* namespace driver */
124 } /* namespace os */
125 
126 // ----------------------------------------------------------------------------
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition: usb-device.h:434
virtual ~Device() noexcept
Definition: usb-device.cpp:59
constexpr return_t RETURN_OK
< Operation succeeded
Definition: common.h:57
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition: usb-device.h:428
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition: usb-device.h:181
System namespace.
uint8_t endpoint_t
Definition: usb.h:74
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition: usb-device.h:425
uint32_t event_t
Definition: common.h:50
virtual return_t do_transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept=0
virtual return_t do_read_setup_packet(uint8_t *buf) noexcept=0
void register_device_callback(device::signal_device_event_t cb_func, const void *cb_object=nullptr) noexcept
Register device event callback.
Definition: usb-device.cpp:65
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition: trace.cpp:74
void(* signal_device_event_t)(const void *object, event_t event)
Definition: usb-device.h:178
return_t transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept
Read data from or Write data to USB Endpoint.
Definition: usb-device.cpp:90
void signal_device_event(event_t event) noexcept
Signal device events.
Definition: usb-device.cpp:103
device::signal_endpoint_event_t cb_endpoint_func_
Pointer to static function that implements the endpoint callback.
Definition: usb-device.h:431
int32_t return_t
Definition: common.h:51
return_t read_setup_packet(uint8_t *buf) noexcept
Read setup packet received over Control Endpoint.
Definition: usb-device.cpp:83
void signal_endpoint_event(endpoint_t ep_addr, event_t event) noexcept
Signal USB Endpoint Event.
Definition: usb-device.cpp:113
void register_endpoint_callback(device::signal_endpoint_event_t cb_func, const void *cb_object=nullptr) noexcept
Definition: usb-device.cpp:73