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
Diff: pairing_backend.cpp
- Revision:
- 2:b7489c070d1f
- Parent:
- 1:c5abc450140c
- Child:
- 3:11b767300d32
--- a/pairing_backend.cpp Tue Sep 01 12:01:50 2015 +0000
+++ b/pairing_backend.cpp Fri Sep 04 08:41:34 2015 +0000
@@ -4,10 +4,17 @@
#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)
{
@@ -18,35 +25,77 @@
return 0;
}
+extern config_t configuration;
+
+int pairing_context_init_from_config(pairing_context *ctx, onPaired_callback callback)
+{
+ printf("server url: %s\n", configuration.pairingServerUrl);
+ printf("uuid: %s\n", configuration.uuid);
+ printf("name: %s\n", configuration.name);
+ ctx->postUrl = configuration.pairingServerUrl;
+ ctx->uuid = configuration.uuid;
+ ctx->name = configuration.name;
+ ctx->onPaired = callback;
+ return 0;
+}
+
#define STACK_SIZE 24000
-void get_tokens_thread(void const *arg);
-int start_pairing(pairing_context *ctx)
+int pairing_get_tokens(pairing_context *ctx)
{
printf("getting tokens from server\n");
- Thread t(get_tokens_thread, ctx, osPriorityNormal, STACK_SIZE);
+ 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" 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);
}
- printf("thread ended?\n");
- return 0;
-}
-
-char str[512];
-char url[100];
-
-void get_tokens_thread(void const *arg)
-{
- printf("in thread\n");
- __get_tokens((pairing_context *)arg);
+ return info.result;
}
int __get_tokens(pairing_context *ctx)
{
-
strcpy(url, ctx->postUrl);
strcat(url, "/gettokens");
http.dumpReqHeader(true);
@@ -87,8 +136,6 @@
printf("error parsing response");
return -2;
}
-
-
}
else
{
@@ -98,3 +145,41 @@
}
+
+int __get_keys(pairing_context *ctx)
+{
+ strcpy(url, ctx->postUrl);
+ strcat(url, "/getkeys");
+ 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_PairingServerKeysResponse_t keys;
+ if (!ret)
+ {
+ printf("Executed POST successfully - read %d characters\n", strlen(str));
+ printf("Result: %s\n", str);
+ int jret = Yodiwo_Plegma_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;
+
+}