/*
 *******************************************************************************
 *  Copyright (c) 2010-2022 VATICS(KNERON) Inc. All rights reserved.
 *
 *  +-----------------------------------------------------------------+
 *  | THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED |
 *  | AND COPIED IN ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH  |
 *  | A LICENSE AND WITH THE INCLUSION OF THE THIS COPY RIGHT NOTICE. |
 *  | THIS SOFTWARE OR ANY OTHER COPIES OF THIS SOFTWARE MAY NOT BE   |
 *  | PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. THE   |
 *  | OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED.        |
 *  |                                                                 |
 *  | THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT   |
 *  | ANY PRIOR NOTICE AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY |
 *  | VATICS(KNERON) INC.                                             |
 *  +-----------------------------------------------------------------+
 *
 *******************************************************************************
 */
  

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include "MsgBroker/msg_broker.h"
#include "lvgl_pcsimulator.h"
#include "lvgl_pcsimulator_msg_sender.h"

static void print_usage(const char *);
static int build_msg_host_cmd(MsgContext *, const char *, void *, uint32_t);

void print_usage(const char *exec)
{
	printf("Usage: %s [-b button_id <0 ~ 3>] [-r] [-m example_id] \n"
		" -b Button ID\n"
		" -r Reset\n"
		" -m Example ID\n"
		"ex: ./lvgl_example_msg_sender -b 3\n"
		"ex: ./lvgl_example_msg_sender -r 0\n"
		"ex: ./lvgl_example_msg_sender -m 2\n"
		, exec);
}

int build_msg_host_cmd(MsgContext *ptMsgCtx, const char *cmd, void *data, uint32_t size)
{
	if (!ptMsgCtx)
		return -1;

	ptMsgCtx->bHasResponse = 0;
	ptMsgCtx->pszHost = MODULE_NAME;
	ptMsgCtx->dwHostLen = strlen(MODULE_NAME) + 1;
	ptMsgCtx->pszCmd = cmd;
	ptMsgCtx->dwCmdLen = strlen(cmd) + 1;
	ptMsgCtx->dwDataSize = size;
	ptMsgCtx->pbyData = (uint8_t *) data;

	MsgBroker_SendMsg(LVGL_CMD_FIFO, ptMsgCtx);

	return 0;
}

int main(int argc, char **argv)
{
	int ch;
	MsgContext tMsgContext;
	int id, iExampleId;

	memset(&tMsgContext, 0, sizeof tMsgContext);
	
	while ((ch = getopt(argc, argv, "b:r:m:h:")) != -1) {
		switch(ch) {
		case 'b':
			id = atoi(optarg);				
			if (id > 3 || id < 0) {
				printf("ID should be 0 ~ 3!\n");
				print_usage(argv[0]);
				return -1;
			}
			build_msg_host_cmd(&tMsgContext, MSG_LVGL_BTN, &id, sizeof id);
			break;
		case 'r':
			build_msg_host_cmd(&tMsgContext, MSG_LVGL_RST, NULL, 0);
			break;
		case 'm':
			iExampleId = atoi(optarg);		

			
			if (id >= EXAMPLE_ID_TOTAL || id < 0) {
				printf("EXAMPLE ID should be 0 ~ %d!\n", EXAMPLE_ID_TOTAL - 1);
				print_usage(argv[0]);
				return -1;
			}
			build_msg_host_cmd(&tMsgContext, MSG_LVGL_EG, &iExampleId, sizeof iExampleId);
			break;			
		case 'h':
		default:
			print_usage(argv[0]);
			return -1;
		}
	}

	return 0;
}

