Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created August 28, 2023 12:16
Show Gist options
  • Select an option

  • Save ashraf267/de07c36e05da5089b690b17eec606efd to your computer and use it in GitHub Desktop.

Select an option

Save ashraf267/de07c36e05da5089b690b17eec606efd to your computer and use it in GitHub Desktop.
v2 of Demo.c - it crashes
#include "Ui.h"
#include "Common.h"
#include "Communication.h"
#include "jsmn.h"
const char* GetWallet(unsigned char *pcOut)
{
short sRC;
char url[] = "https://devapi.ucard.store/identity/Agent/login/wallet/initate";
unsigned char requestData[] = "{\"walletId\": \"5149\"}";
unsigned short requestDataLen = strlen((char *)requestData);
// char pExtHeadr[] = "Content-Type: application/json\r\n";
sRC = UIWireHttpSend(url, requestData, requestDataLen, NULL);
// sRC = UIWireHttpSend(url, requestData, requestDataLen, pExtHeadr);
// if (!(sRC == TY_OK))
// {
// return (const char *)TY_ERR;
// }
// unsigned char responseBuffer[4096];
// const char responseBuffer[4096];
const char response = "{\"isSuccessful\": true, \"requestModel\": {\"requestHash\": \"363P2a88274de10b4bfc8ddc94e7a57b62b5\", \"emailAddress\": null}, \"message\": \"A verification OTP has been sent to your mobile number 345728.\", \"statusCode\": null}";
unsigned short responseLength = sizeof(response);
// sRC = UIWireHttpRecv((unsigned char *)responseBuffer, &responseLength);
sRC = UIWireHttpRecv((unsigned char *)response, &responseLength);
// if (!(sRC == TY_OK))
// {
// return (const char *)TY_ERR;
// }
// jsmn here
// Initialize jsmn parser
jsmn_parser parser;
jsmn_init(&parser);
// Estimate the number of tokens needed for parsing
int num_tokens = 20;
jsmntok_t tokens[num_tokens];
// Parse the JSON input
int ret = jsmn_parse(&parser, response, strlen(response), tokens, num_tokens);
if (ret < 0)
{
printf("Error parsing JSON: %d\n", ret);
// return 1;
}
jsmntok_t *value; // Declare the value variable outside the inner loop
char myHash[100];
const char *reqHash;
// Iterate through the JSON tokens
for (int i = 0; i < ret; i++)
{
jsmntok_t *token = &tokens[i];
// Check if token is an object
if (token->type == JSMN_OBJECT)
{
printf("Object:\n");
// Iterate through the object's key-value pairs
for (int j = i + 1; j < ret; j++)
{
jsmntok_t *key = &tokens[j];
value = &tokens[j + 1]; // Assign value here
// Check if key is a string
if (key->type == JSMN_STRING)
{
char key_str[key->end - key->start + 1];
strncpy(key_str, response + key->start, key->end - key->start);
key_str[key->end - key->start] = '\0';
printf("Key: %s\n", key_str);
// Check if value is a string
if (value->type == JSMN_STRING)
{
char value_str[value->end - value->start + 1];
strncpy(value_str, response + value->start, value->end - value->start);
value_str[value->end - value->start] = '\0';
printf("Value: %s\n", value_str);
// Check if the key is "name"
// Check if the key is "requestHash"
if (strcmp(key_str, "requestHash") == 0)
{
// strncpy(myName, value_str, sizeof(myName));
strncpy(myHash, value_str, sizeof(myHash));
reqHash = (const char *)myHash;
}
}
}
// Skip the value token
j++;
}
// Move the main index to the end of the object
i = value->end;
}
}
return reqHash;
// return 0;
}
short ShowScreen(int iTimeout)
{
short sRC;
Lib_LcdDrowArea(DEFAULT_FORG_COLOR, 0, 30, 320, 210);
// const char *myReqHash = GetWallet((unsigned char *)"5149");
const char *myReqHash;
while (1)
{
// DRAW_TITLE("363Pfe9b9d2c38a64838a3e2d53d3aa086b8");
// DRAW_TITLE(myReqHash);
sRC = UIWaitKey(iTimeout, 1);
if (UI_INPUT_TIMEOUT == sRC || UI_INPUT_CANCEL == sRC)
{
return sRC;
}
else
{
continue;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment