2018-11-21 01:27:05 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "board/sg-cmd.h"
|
|
|
|
#include "board/sg-led.h"
|
|
|
|
#include "board/sg-led-cmd.h"
|
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
|
|
|
static HRESULT sg_led_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-26 22:25:04 +00:00
|
|
|
void *v_res);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_reset(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_led_res_reset *res);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_get_info(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_led_res_get_info *res);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_set_color(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_led_req_set_color *req);
|
|
|
|
|
2023-12-03 17:45:42 +00:00
|
|
|
const char *sg_led_info[] = {
|
|
|
|
"15084\xFF\x10\x00\x12",
|
|
|
|
"000-00000\xFF\x11\x40",
|
|
|
|
// maybe the same?
|
|
|
|
"000-00000\xFF\x11\x40"
|
2018-11-21 01:27:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void sg_led_init(
|
|
|
|
struct sg_led *led,
|
|
|
|
uint8_t addr,
|
|
|
|
const struct sg_led_ops *ops,
|
2023-12-03 17:45:42 +00:00
|
|
|
unsigned int gen,
|
2018-11-21 01:27:05 +00:00
|
|
|
void *ctx)
|
|
|
|
{
|
|
|
|
assert(led != NULL);
|
|
|
|
assert(ops != NULL);
|
|
|
|
|
|
|
|
led->ops = ops;
|
|
|
|
led->ops_ctx = ctx;
|
|
|
|
led->addr = addr;
|
2023-12-03 17:45:42 +00:00
|
|
|
led->gen = gen;
|
2018-11-21 01:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sg_led_transact(
|
|
|
|
struct sg_led *led,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct iobuf *res_frame,
|
2018-11-21 01:27:05 +00:00
|
|
|
const void *req_bytes,
|
|
|
|
size_t req_nbytes)
|
|
|
|
{
|
|
|
|
assert(led != NULL);
|
2019-10-26 22:25:04 +00:00
|
|
|
assert(res_frame != NULL);
|
2018-11-21 01:27:05 +00:00
|
|
|
assert(req_bytes != NULL);
|
|
|
|
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_req_transact(res_frame, req_bytes, req_nbytes, sg_led_dispatch, led);
|
2018-11-21 01:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define sg_led_dprintfv(led, fmt, ap)
|
|
|
|
#define sg_led_dprintf(led, fmt, ...)
|
|
|
|
#else
|
|
|
|
static void sg_led_dprintfv(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
dprintf("RGB LED %02x: ", led->addr);
|
|
|
|
dprintfv(fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sg_led_dprintf(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
sg_led_dprintfv(led, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static HRESULT sg_led_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-26 22:25:04 +00:00
|
|
|
void *v_res)
|
2018-11-21 01:27:05 +00:00
|
|
|
{
|
|
|
|
const struct sg_led *led;
|
|
|
|
const union sg_led_req_any *req;
|
2019-10-26 22:25:04 +00:00
|
|
|
union sg_led_res_any *res;
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
led = ctx;
|
|
|
|
req = v_req;
|
2019-10-26 22:25:04 +00:00
|
|
|
res = v_res;
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
if (req->simple.hdr.addr != led->addr) {
|
|
|
|
/* Not addressed to us, don't send a response */
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req->simple.hdr.cmd) {
|
|
|
|
case SG_RGB_CMD_RESET:
|
2019-10-26 22:25:04 +00:00
|
|
|
return sg_led_cmd_reset(led, &req->simple, &res->reset);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
case SG_RGB_CMD_GET_INFO:
|
2019-10-26 22:25:04 +00:00
|
|
|
return sg_led_cmd_get_info(led, &req->simple, &res->get_info);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
case SG_RGB_CMD_SET_COLOR:
|
|
|
|
return sg_led_cmd_set_color(led, &req->set_color);
|
|
|
|
|
|
|
|
default:
|
|
|
|
sg_led_dprintf(led, "Unimpl command %02x\n", req->simple.hdr.cmd);
|
|
|
|
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_reset(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_led_res_reset *res)
|
2018-11-21 01:27:05 +00:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
sg_led_dprintf(led, "Reset\n");
|
2019-10-26 22:25:04 +00:00
|
|
|
sg_res_init(&res->res, req, sizeof(res->payload));
|
|
|
|
res->payload = 0;
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
if (led->ops->reset != NULL) {
|
|
|
|
hr = led->ops->reset(led->ops_ctx);
|
|
|
|
} else {
|
|
|
|
hr = S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
sg_led_dprintf(led, "led->ops->reset: Error %x\n", hr);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_get_info(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_req_header *req,
|
2019-10-26 22:25:04 +00:00
|
|
|
struct sg_led_res_get_info *res)
|
2018-11-21 01:27:05 +00:00
|
|
|
{
|
|
|
|
sg_led_dprintf(led, "Get info\n");
|
2023-12-03 17:45:42 +00:00
|
|
|
|
|
|
|
unsigned int len = strlen(sg_led_info[led->gen - 1]);
|
|
|
|
|
|
|
|
sg_res_init(&res->res, req, len);
|
|
|
|
memcpy(res->payload, sg_led_info[led->gen - 1], len);
|
2018-11-21 01:27:05 +00:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_led_cmd_set_color(
|
|
|
|
const struct sg_led *led,
|
|
|
|
const struct sg_led_req_set_color *req)
|
|
|
|
{
|
|
|
|
if (req->req.payload_len != sizeof(req->payload)) {
|
|
|
|
sg_led_dprintf(led, "%s: Payload size is incorrect\n", __func__);
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-02-26 02:55:49 +00:00
|
|
|
led->ops->set_color(
|
2018-11-21 01:27:05 +00:00
|
|
|
led->ops_ctx,
|
|
|
|
req->payload[0],
|
|
|
|
req->payload[1],
|
|
|
|
req->payload[2]);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
/* No response */
|
|
|
|
return S_FALSE;
|
|
|
|
}
|