Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL
pairing_backend.cpp
- Committer:
- mitsarionas
- Date:
- 2015-09-21
- Revision:
- 4:cc4c5b6d9730
- Parent:
- 3:11b767300d32
File content as of revision 4:cc4c5b6d9730:
#include "pairing_backend.h"
#include "HTTPClient.h"
#include "mbed.h"
#include "rtos.h"
#include "yodiwo_helpers.h"
#include "jsmn.h"
#include "config.h"
HTTPClient http;
char recvBuff[1024*20];
int __get_tokens(pairing_context *ctx);
int __get_keys(pairing_context *ctx);
int do_on_thread(function_with_result func, void *args, unsigned int stack_size);
void __thread_wrapper(void const *arg);
#define PAGE_SERVER_PHASE2 "userconfirm"
int pairing_context_init_with_defaults(pairing_context *ctx, onPaired_callback callback)
{
ctx->postUrl = "http://10.30.254.199:3334/pairing/";
ctx->uuid = "1337CNode";
ctx->name = "Nodas";
ctx->onPaired = callback;
return 0;
}
extern Yodiwo_Tools_APIGenerator_CNodeConfig_t *activeConfig;
int pairing_context_init_from_config(pairing_context *ctx, onPaired_callback callback)
{
printf("server url: %s\n", activeConfig->PairingServerUrl);
printf("uuid: %s\n", activeConfig->Uuid);
printf("name: %s\n", activeConfig->Name);
ctx->postUrl = activeConfig->PairingServerUrl;
ctx->uuid = activeConfig->Uuid;
ctx->name = activeConfig->Name;
ctx->onPaired = callback;
return 0;
}
#define STACK_SIZE 24000
int pairing_get_tokens(pairing_context *ctx)
{
printf("getting tokens from server\n");
return do_on_thread((function_with_result)__get_tokens, ctx, STACK_SIZE);
}
char* get_server_phase2_url(pairing_context *ctx, char *nodeUrlBase)
{
int len = 200; //TODO: proper calculation
char* result = (char *)malloc(sizeof(char) * len);
if (!result)
return NULL;
sprintf(result, "%s" "1/" PAGE_SERVER_PHASE2 "?token2=%s&noderedirect=%snext", ctx->postUrl, ctx->token2, nodeUrlBase);
return result;
}
int pairing_get_keys(pairing_context *ctx)
{
printf("getting keys from server\n");
return do_on_thread((function_with_result)__get_keys, ctx, STACK_SIZE);
}
char str[512];
char url[100];
struct thread_info
{
function_with_result func;
void * args;
int result;
};
void __thread_wrapper(void const *arg)
{
struct thread_info *info = (struct thread_info *)arg;
info->result = info->func(info->args);
}
int do_on_thread(function_with_result func, void *args, unsigned int stack_size)
{
struct thread_info info;
info.func = func;
info.args = args;
Thread t(__thread_wrapper, &info, osPriorityNormal, stack_size);
while (t.get_state() != Thread::Inactive) {
// printf("yielding... %d\n", t.get_state());
// Thread::yield();
// Thread::wait(1000);
}
return info.result;
}
int __get_tokens(pairing_context *ctx)
{
strcpy(url, ctx->postUrl);
strcat(url, "/1/gettokensreq");
http.dumpReqHeader(true);
http.dumpResHeader(true);
int ret;
//GET data
// printf("\nTrying to fetch page...\n");
// ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128);
// if (!ret)
// {
// printf("Page fetched successfully - read %d characters\n", strlen(str));
// printf("Result: %s\n", str);
// }
// else
// {
// printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
// }
//POST data
HTTPMap map;
HTTPText inText(str, 512);
map.put("name", ctx->name);
map.put("uuid", ctx->uuid);
printf("\nTrying to post data...\n");
ret = http.post(url, map, &inText);
Yodiwo_Plegma_NodePairing_PairingServerTokensResponse_t tokens;
if (!ret)
{
printf("Executed POST successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
int jret = Yodiwo_Plegma_NodePairing_PairingServerTokensResponse_FromJson(str, strlen(str), &tokens);
if (jret == Yodiwo_JsonSuccessParse) {
ctx->token1 = tokens.token1;
ctx->token2 = tokens.token2;
return 0;
} else {
printf("error parsing response");
return -2;
}
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
return ret;
}
int __get_keys(pairing_context *ctx)
{
strcpy(url, ctx->postUrl);
strcat(url, "/1/getkeysreq");
http.dumpReqHeader(true);
http.dumpResHeader(true);
int ret;
//POST data
HTTPMap map;
HTTPText inText(str, 512);
map.put("uuid", ctx->uuid);
map.put("token1", ctx->token1);
printf("\nTrying to post data...\n");
ret = http.post(url, map, &inText);
Yodiwo_Plegma_NodePairing_PairingServerKeysResponse_t keys;
if (!ret)
{
printf("Executed POST successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
int jret = Yodiwo_Plegma_NodePairing_PairingServerKeysResponse_FromJson(str, strlen(str), &keys);
if (jret == Yodiwo_JsonSuccessParse) {
ctx->nodeKey = keys.nodeKey;
ctx->secretKey = keys.secretKey;
return 0;
} else {
printf("error parsing response");
return -2;
}
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
return ret;
}