/*
 *******************************************************************************
 *  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 <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "MsgBroker/msg_broker.h"
#include "lvgl_example.h"
#include "lvgl_example_msg_sender.h"

static void print_usage(const char *);
static void print_msg(const char *, ...);
static int build_msg_host_cmd(MsgContext *, const char *, void *, unsigned int);

void print_usage(const char *exec)
{
	print_msg("Usage: %s [-b button_id <0 ~ 3>][-e][-r]\n"
		" -b Select Buttons:"
		"	0: show abc\n" 
		"	1: show def\n" 
		"	2: show ghi\n" 
		"	3: show jkl\n" 
		"-e Enter\n"
		"-r Reset\n"
		"ex: ./lvgl_example_msg_sender -b 3 && ./lvgl_example_msg_sender -e\n"
		"ex: ./lvgl_example_msg_sender -r\n"
		, exec);
}

int build_msg_host_cmd(MsgContext *ptMsgCtx, const char *cmd, void *data, unsigned int 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 = (unsigned char *)data;

	MsgBroker_SendMsg(LVGL_CMD_FIFO, ptMsgCtx);

	return 0;
}

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

	memset(&tMsgContext, 0, sizeof tMsgContext);
	
	while ((ch = getopt(argc, argv, "b:erh")) != -1) {
		switch(ch) {
		case 'b':
			id = atoi(optarg);				
			if (id > 3 || id < 0) {
				print_msg("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 'e':
			build_msg_host_cmd(&tMsgContext, MSG_LVGL_ENT, NULL, 0);
			break;
		case 'r':
			build_msg_host_cmd(&tMsgContext, MSG_LVGL_RST, NULL, 0);
			break;
		case 'h':
		default:
			print_usage(argv[0]);
			return -1;
		}
	}

	return 0;
}

