Created
September 6, 2023 10:17
-
-
Save ashraf267/af4e141bffd90915442b9c6a782e68c4 to your computer and use it in GitHub Desktop.
This kind of works for calling api. not fully tested.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Ui.h" | |
#include "Common.h" | |
#include "Communication.h" | |
#include "jsmn.h" | |
#include "LibApi.h" | |
#include "Print.h" | |
#include "MessageBox.h" | |
#include "net_if.h" | |
// for print | |
#define FONT16 16 | |
#define FONT24 24 | |
#define PRINT_ONELINE_BUF_LEN 1024 | |
#define PRINT_REVERSE 0x10 | |
#define TYERR_TPH_NO_PAPER (-10) | |
int PrintDemoReceipt(void) | |
{ | |
int iRc = 0; | |
unsigned char aucPrintBuf[PRINT_ONELINE_BUF_LEN + 1] = {0}; | |
const char *dashes = "--------------------------------------------------"; // 50 dashes | |
// unsigned short usLen = 0; | |
Lib_PrnOpen(); | |
Again: | |
Lib_PrnSetSpace(2); | |
// add a function to print a logo here, if you have any logo to print | |
Lib_PrnSetFont(FONT24, 2, 2); | |
// show Merchant Business Name | |
iRc = Lib_PrintStr(ALIGN_M | PRINT_REVERSE, (unsigned char *)"MURABELLA", strlen("MURABELLA")); | |
if (TY_OK != iRc) | |
{ | |
return iRc; | |
} | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "INWARD TRANSFER"); | |
iRc = Lib_PrintStr(ALIGN_M, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "Customer\'s Copy"); | |
iRc = Lib_PrintStr(ALIGN_M, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
// add some date & time here | |
// content start | |
// amount | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Amount:", strlen("Amount:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "₦30,000"); | |
Lib_PrnSetFont(FONT16, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
// account name | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Account Name:", strlen("Account Name:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "Ashraf Otagun"); | |
Lib_PrnSetFont(FONT16, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
// account number | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Account Number:", strlen("Account Number:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "8892100011"); | |
Lib_PrnSetFont(FONT16, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
// date | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Date:", strlen("Date:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "05 - 09 - 2023"); | |
Lib_PrnSetFont(FONT16, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
// narration | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Narration:", strlen("Narration:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "NULL"); | |
Lib_PrnSetFont(FONT16, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
// transaction ref | |
Lib_PrnSetFont(FONT24, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
iRc = Lib_PrintStr(ALIGN_L, (unsigned char *)"Transaction Ref:", strlen("Transaction Ref:")); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "ghfjsjshsgdgsjj64784737dsacs6546dc"); | |
iRc = Lib_PrintStr(ALIGN_L, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)dashes, strlen(dashes)); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
// content end | |
Lib_PrnSetFont(FONT24, 2, 2); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
// add transaction status | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "**SUCCESSFUL**"); // triple asterisks would overflow | |
iRc = Lib_PrintStr(ALIGN_M, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
// empty line | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
Lib_PrnSetFont(FONT24, 1, 1); | |
// add software version | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s", "UCARD"); | |
iRc = Lib_PrintStr(ALIGN_M, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
Lib_PrnSetFont(FONT16, 1, 1); | |
iRc = Lib_PrintStr(ALIGN_M, (unsigned char *)" ", 1); | |
memset(aucPrintBuf, 0, sizeof(aucPrintBuf)); | |
sprintf((char *)aucPrintBuf, "%s %s", "VERSION:", "v 1.5"); | |
iRc = Lib_PrintStr(ALIGN_M, aucPrintBuf, strlen((char *)aucPrintBuf)); | |
if (TY_OK != iRc) | |
{ | |
return iRc; | |
} | |
iRc = Lib_PrnClose(); | |
if (iRc == TYERR_TPH_NO_PAPER) | |
{ | |
char aucDispBuffer[128] = {0}; | |
memset(aucDispBuffer, 0x00, sizeof(aucDispBuffer)); | |
sprintf((char *)aucDispBuffer, "%s\n %s", "Out of paper!", "Add paper[Enter]"); | |
iRc = MessageBox(aucDispBuffer, MB_COLOR_YELLOW | MB_CENTER | MB_ICONQUESTION, 60000); | |
if (iRc == UI_INPUT_ENTER) | |
{ | |
UIClearRect(0, 1, LCD_MAX_SEG, MAX_COM_LINES - 1); | |
UITextAlign(ALIGN_M, LCD_OP_Y_S, DISP_MODE_NORMAL, "Printing..."); | |
Lib_PrnOpen(); | |
goto Again; | |
} | |
} | |
return TY_OK; | |
} | |
// static int jsoneq(const char *json, jsmntok_t *tok, const char *s) | |
// { | |
// if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start && | |
// strncmp(json + tok->start, s, tok->end - tok->start) == 0) | |
// { | |
// return 0; | |
// } | |
// return -1; | |
// } | |
// char *GetTitle() | |
// { | |
// // start | |
// // char url[] = "https://jsonplaceholder.typicode.com/posts/1"; | |
// char url[] = "https://devapi.ucard.store/identity/Agent/login/wallet/initate"; | |
// unsigned char responseBuffer[4096]; // Adjust buffer size as needed | |
// unsigned short responseLength = sizeof(responseBuffer); | |
// // Send the GET request | |
// if (UIWireHttpSnd(url, NULL, 0, NULL) < 0) | |
// { | |
// // printf("Failed to send GET request\n"); | |
// // return -1; | |
// TRACE("Ayo: send failed!"); | |
// return "call failed"; | |
// } | |
// else | |
// { | |
// TRACE("Ayo: send successful"); | |
// // Receive the response | |
// if (UIWireHttpRecev(responseBuffer, &responseLength) < 0) | |
// { | |
// // printf("Failed to receive HTTP response\n"); | |
// // return -1; | |
// TRACE("Ayo: recv failed!"); | |
// return "recv failed"; | |
// } | |
// } | |
// TRACE("Ayo: recv successful"); | |
// // end | |
// jsmn_parser parser; | |
// jsmntok_t tokens[100]; // Adjust this based on the expected number of tokens in your response | |
// jsmn_init(&parser); | |
// int tokenCount = jsmn_parse(&parser, (char *)responseBuffer, responseLength, tokens, sizeof(tokens) / sizeof(tokens[0])); | |
// if (tokenCount < 0) | |
// { | |
// // printf("JSON parsing error: %d\n", tokenCount); | |
// return NULL; | |
// } | |
// char *myTitle = NULL; | |
// for (int i = 1; i < tokenCount; i++) | |
// { | |
// if (jsoneq((char *)responseBuffer, &tokens[i], "title") == 0) | |
// { | |
// int titleLength = tokens[i + 1].end - tokens[i + 1].start; | |
// myTitle = (char *)malloc(titleLength + 1); | |
// strncpy(myTitle, (char *)responseBuffer + tokens[i + 1].start, titleLength); | |
// myTitle[titleLength] = '\0'; | |
// break; | |
// } | |
// } | |
// return myTitle; | |
// } | |
// short TestScreen(int iTimeout) | |
// { | |
// short sRC; | |
// Lib_LcdDrowArea(DEFAULT_FORG_COLOR, 0, 30, 320, 210); | |
// // char *myTitle = GetTitle(); | |
// sRC = HttpsGetTest(500); | |
// while (1) | |
// { | |
// // DRAW_TITLE("Test API"); | |
// // if (myReqHash == NULL) | |
// // { | |
// // DRAW_TITLE("No Response!"); | |
// // } | |
// // else | |
// // { | |
// // DRAW_TITLE(myReqHash); | |
// // } | |
// DRAW_TITLE("Test API"); | |
// DRAW_TEXT(" Enter digit sent to your Mobile number", 90 + 10); | |
// DRAW_TEXT("Press 1 to continue, 0 to cancel", 170 + 10); | |
// // if (myTitle) | |
// // { | |
// // // DRAW_TITLE(myTitle); | |
// // DRAW_TITLE("call successful"); | |
// // // DRAW_TEXT(" Enter digit sent to your Mobile number"); | |
// // DRAW_TEXT(" Enter digit sent to your Mobile number", 90 + 10); | |
// // DRAW_TEXT("Press 1 to continue, 0 to cancel", 170 + 10); | |
// // // free(myTitle); | |
// // } | |
// // else | |
// // { | |
// // DRAW_TITLE("Title not found"); | |
// // } | |
// sRC = UIWaitKey(iTimeout, 1); | |
// if (UI_INPUT_TIMEOUT == sRC || UI_INPUT_CANCEL == sRC) | |
// { | |
// return sRC; | |
// } | |
// else | |
// { | |
// continue; | |
// } | |
// } | |
// } | |
short ApiMustWork(int iTimeout) | |
{ | |
int sRC = -1; | |
unsigned char ucRecvBuff[3500] = {0}; | |
unsigned short usRecvBUffLen = sizeof(ucRecvBuff); | |
char *myTitle = NULL; | |
// Set the network connection mode to SSL mode | |
Net_SetSocketMode(NET_MODE_SSL); | |
Lib_SetSslSwitch(0); | |
// Set the server as: 106.55.177.122:443 | |
SetServerOpt(SERVER_OPT_POSP_MAIN); | |
sRC = UIWireNetTcpLink(NET_MAX_TCPLINK_TIME); | |
if (sRC != TY_OK) | |
{ | |
FUN_IN_ERR; | |
TRACE("sRC = %d", sRC); | |
return TY_ERR; | |
} | |
// https GET test | |
sRC = UIWireHttpSend("https://api.bemfa.com/api/time/now/?type=1", NULL, 0, NULL); | |
if (sRC != TY_OK) | |
{ | |
FUN_IN_ERR; | |
TRACE("sRC = %d", sRC); | |
return TY_ERR; | |
} | |
// Get Response | |
sRC = UIWireHttpRecv(ucRecvBuff, &usRecvBUffLen); | |
if (sRC != TY_OK) | |
{ | |
FUN_IN_ERR; | |
TRACE("sRC = %d", sRC); | |
return TY_ERR; | |
} | |
// Output response data | |
TRACE("usRecvBUffLen = %d", usRecvBUffLen); | |
TRACEBUF(ucRecvBuff, usRecvBUffLen); | |
Lib_DelayMs(20); | |
// Close the network connection | |
UIWireNetClose(); | |
myTitle = "Fine fine fine..."; | |
Lib_LcdDrowArea(DEFAULT_FORG_COLOR, 0, 30, 320, 210); | |
while (1) | |
{ | |
if (myTitle == NULL) | |
{ | |
DRAW_TITLE("GET Api call"); | |
} | |
else | |
{ | |
DRAW_TITLE(myTitle); | |
} | |
sRC = UIWaitKey(iTimeout, 1); | |
if (UI_INPUT_TIMEOUT == sRC || UI_INPUT_CANCEL == sRC) | |
{ | |
return sRC; | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
return TY_OK; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment