2022-03-21 23:15:53 +00:00
|
|
|
#ifndef MAIN_H
|
|
|
|
#define MAIN_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2023-10-28 23:37:53 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdint.h>
|
2022-04-03 00:22:42 +00:00
|
|
|
#include "nxjson.c"
|
2022-03-21 23:15:53 +00:00
|
|
|
|
2023-05-18 13:55:29 +00:00
|
|
|
/// Convert seconds to nanoseconds
|
|
|
|
#define SEC_TO_NS(sec) ((sec)*1000000000)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
#define WAKU_CALL(call) \
|
|
|
|
do { \
|
|
|
|
int ret = call; \
|
|
|
|
if (ret != 0) { \
|
|
|
|
printf("Failed the call to: %s. Returned code: %d\n", #call, ret); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2023-05-18 13:55:29 +00:00
|
|
|
|
|
|
|
uint64_t nowInNanosecs(){
|
|
|
|
uint64_t nanoseconds;
|
|
|
|
struct timespec ts;
|
|
|
|
int return_code = timespec_get(&ts, TIME_UTC);
|
|
|
|
if (return_code == 0)
|
|
|
|
{
|
|
|
|
printf("Failed to obtain timestamp.\n");
|
|
|
|
nanoseconds = UINT64_MAX; // use this to indicate error
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// `ts` now contains your timestamp in seconds and nanoseconds! To
|
|
|
|
// convert the whole struct to nanoseconds, do this:
|
|
|
|
nanoseconds = SEC_TO_NS((uint64_t)ts.tv_sec) + (uint64_t)ts.tv_nsec;
|
|
|
|
}
|
|
|
|
return nanoseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
bool isError(char *input)
|
|
|
|
{
|
|
|
|
char *jsonStr = malloc(strlen(input) + 1);
|
|
|
|
strcpy(jsonStr, input);
|
|
|
|
const nx_json *json = nx_json_parse(jsonStr, 0);
|
|
|
|
bool result = false;
|
|
|
|
if (json)
|
|
|
|
{
|
|
|
|
const char *errTxt = nx_json_get(json, "error")->text_value;
|
|
|
|
result = errTxt != NULL;
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
printf("ERROR: %s\n", errTxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nx_json_free(json);
|
|
|
|
free(jsonStr);
|
|
|
|
return result;
|
|
|
|
}
|
2022-03-21 23:15:53 +00:00
|
|
|
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
char *utils_extract_wakumessage_from_signal(const nx_json *wakuMsgJson)
|
2022-04-03 00:22:42 +00:00
|
|
|
{
|
|
|
|
const char *payload = nx_json_get(wakuMsgJson, "payload")->text_value;
|
|
|
|
const char *contentTopic = nx_json_get(wakuMsgJson, "contentTopic")->text_value;
|
|
|
|
int version = nx_json_get(wakuMsgJson, "version")->int_value;
|
|
|
|
long long timestamp = nx_json_get(wakuMsgJson, "timestamp")->int_value;
|
2023-03-09 15:48:25 +00:00
|
|
|
char wakuMsg[6000];
|
2022-04-03 00:22:42 +00:00
|
|
|
sprintf(wakuMsg, "{\"payload\":\"%s\",\"contentTopic\":\"%s\",\"timestamp\":%lld, \"version\":%d}", payload, contentTopic, timestamp, version);
|
|
|
|
char *response = (char *)malloc(sizeof(char) * (strlen(wakuMsg) + 1));
|
|
|
|
strcpy(response, wakuMsg);
|
|
|
|
return response;
|
|
|
|
}
|
2022-03-21 23:15:53 +00:00
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
|
|
|
|
#endif /* MAIN_H */
|