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@2:b7489c070d1f, 2015-09-04 (annotated)
- Committer:
- mitsarionas
- Date:
- Fri Sep 04 08:41:34 2015 +0000
- Revision:
- 2:b7489c070d1f
- Parent:
- 1:c5abc450140c
- Child:
- 3:11b767300d32
pairing & configuration read/write working
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mitsarionas | 0:00797f1ebe04 | 1 | #include "pairing_backend.h" |
| mitsarionas | 0:00797f1ebe04 | 2 | #include "HTTPClient.h" |
| mitsarionas | 0:00797f1ebe04 | 3 | #include "mbed.h" |
| mitsarionas | 0:00797f1ebe04 | 4 | #include "rtos.h" |
| mitsarionas | 1:c5abc450140c | 5 | #include "yodiwo_helpers.h" |
| mitsarionas | 1:c5abc450140c | 6 | #include "jsmn.h" |
| mitsarionas | 2:b7489c070d1f | 7 | #include "config.h" |
| mitsarionas | 0:00797f1ebe04 | 8 | |
| mitsarionas | 0:00797f1ebe04 | 9 | HTTPClient http; |
| mitsarionas | 0:00797f1ebe04 | 10 | char recvBuff[1024*20]; |
| mitsarionas | 0:00797f1ebe04 | 11 | int __get_tokens(pairing_context *ctx); |
| mitsarionas | 2:b7489c070d1f | 12 | int __get_keys(pairing_context *ctx); |
| mitsarionas | 2:b7489c070d1f | 13 | |
| mitsarionas | 2:b7489c070d1f | 14 | int do_on_thread(function_with_result func, void *args, unsigned int stack_size); |
| mitsarionas | 2:b7489c070d1f | 15 | void __thread_wrapper(void const *arg); |
| mitsarionas | 2:b7489c070d1f | 16 | |
| mitsarionas | 2:b7489c070d1f | 17 | #define PAGE_SERVER_PHASE2 "userconfirm" |
| mitsarionas | 1:c5abc450140c | 18 | |
| mitsarionas | 0:00797f1ebe04 | 19 | int pairing_context_init_with_defaults(pairing_context *ctx, onPaired_callback callback) |
| mitsarionas | 0:00797f1ebe04 | 20 | { |
| mitsarionas | 0:00797f1ebe04 | 21 | ctx->postUrl = "http://10.30.254.199:3334/pairing/"; |
| mitsarionas | 0:00797f1ebe04 | 22 | ctx->uuid = "1337CNode"; |
| mitsarionas | 0:00797f1ebe04 | 23 | ctx->name = "Nodas"; |
| mitsarionas | 0:00797f1ebe04 | 24 | ctx->onPaired = callback; |
| mitsarionas | 0:00797f1ebe04 | 25 | return 0; |
| mitsarionas | 0:00797f1ebe04 | 26 | } |
| mitsarionas | 0:00797f1ebe04 | 27 | |
| mitsarionas | 2:b7489c070d1f | 28 | extern config_t configuration; |
| mitsarionas | 2:b7489c070d1f | 29 | |
| mitsarionas | 2:b7489c070d1f | 30 | int pairing_context_init_from_config(pairing_context *ctx, onPaired_callback callback) |
| mitsarionas | 2:b7489c070d1f | 31 | { |
| mitsarionas | 2:b7489c070d1f | 32 | printf("server url: %s\n", configuration.pairingServerUrl); |
| mitsarionas | 2:b7489c070d1f | 33 | printf("uuid: %s\n", configuration.uuid); |
| mitsarionas | 2:b7489c070d1f | 34 | printf("name: %s\n", configuration.name); |
| mitsarionas | 2:b7489c070d1f | 35 | ctx->postUrl = configuration.pairingServerUrl; |
| mitsarionas | 2:b7489c070d1f | 36 | ctx->uuid = configuration.uuid; |
| mitsarionas | 2:b7489c070d1f | 37 | ctx->name = configuration.name; |
| mitsarionas | 2:b7489c070d1f | 38 | ctx->onPaired = callback; |
| mitsarionas | 2:b7489c070d1f | 39 | return 0; |
| mitsarionas | 2:b7489c070d1f | 40 | } |
| mitsarionas | 2:b7489c070d1f | 41 | |
| mitsarionas | 0:00797f1ebe04 | 42 | #define STACK_SIZE 24000 |
| mitsarionas | 0:00797f1ebe04 | 43 | |
| mitsarionas | 0:00797f1ebe04 | 44 | |
| mitsarionas | 2:b7489c070d1f | 45 | int pairing_get_tokens(pairing_context *ctx) |
| mitsarionas | 0:00797f1ebe04 | 46 | { |
| mitsarionas | 0:00797f1ebe04 | 47 | printf("getting tokens from server\n"); |
| mitsarionas | 2:b7489c070d1f | 48 | return do_on_thread((function_with_result)__get_tokens, ctx, STACK_SIZE); |
| mitsarionas | 2:b7489c070d1f | 49 | } |
| mitsarionas | 2:b7489c070d1f | 50 | |
| mitsarionas | 2:b7489c070d1f | 51 | char* get_server_phase2_url(pairing_context *ctx, char *nodeUrlBase) |
| mitsarionas | 2:b7489c070d1f | 52 | { |
| mitsarionas | 2:b7489c070d1f | 53 | int len = 200; //TODO: proper calculation |
| mitsarionas | 2:b7489c070d1f | 54 | char* result = (char *)malloc(sizeof(char) * len); |
| mitsarionas | 2:b7489c070d1f | 55 | if (!result) |
| mitsarionas | 2:b7489c070d1f | 56 | return NULL; |
| mitsarionas | 2:b7489c070d1f | 57 | sprintf(result, "%s" PAGE_SERVER_PHASE2 "?token2=%s&noderedirect=%snext", ctx->postUrl, ctx->token2, nodeUrlBase); |
| mitsarionas | 2:b7489c070d1f | 58 | return result; |
| mitsarionas | 2:b7489c070d1f | 59 | } |
| mitsarionas | 2:b7489c070d1f | 60 | |
| mitsarionas | 2:b7489c070d1f | 61 | int pairing_get_keys(pairing_context *ctx) |
| mitsarionas | 2:b7489c070d1f | 62 | { |
| mitsarionas | 2:b7489c070d1f | 63 | printf("getting keys from server\n"); |
| mitsarionas | 2:b7489c070d1f | 64 | return do_on_thread((function_with_result)__get_keys, ctx, STACK_SIZE); |
| mitsarionas | 2:b7489c070d1f | 65 | } |
| mitsarionas | 2:b7489c070d1f | 66 | |
| mitsarionas | 2:b7489c070d1f | 67 | char str[512]; |
| mitsarionas | 2:b7489c070d1f | 68 | char url[100]; |
| mitsarionas | 2:b7489c070d1f | 69 | |
| mitsarionas | 2:b7489c070d1f | 70 | struct thread_info |
| mitsarionas | 2:b7489c070d1f | 71 | { |
| mitsarionas | 2:b7489c070d1f | 72 | function_with_result func; |
| mitsarionas | 2:b7489c070d1f | 73 | void * args; |
| mitsarionas | 2:b7489c070d1f | 74 | int result; |
| mitsarionas | 2:b7489c070d1f | 75 | }; |
| mitsarionas | 2:b7489c070d1f | 76 | |
| mitsarionas | 2:b7489c070d1f | 77 | void __thread_wrapper(void const *arg) |
| mitsarionas | 2:b7489c070d1f | 78 | { |
| mitsarionas | 2:b7489c070d1f | 79 | struct thread_info *info = (struct thread_info *)arg; |
| mitsarionas | 2:b7489c070d1f | 80 | info->result = info->func(info->args); |
| mitsarionas | 2:b7489c070d1f | 81 | } |
| mitsarionas | 2:b7489c070d1f | 82 | |
| mitsarionas | 2:b7489c070d1f | 83 | int do_on_thread(function_with_result func, void *args, unsigned int stack_size) |
| mitsarionas | 2:b7489c070d1f | 84 | { |
| mitsarionas | 2:b7489c070d1f | 85 | struct thread_info info; |
| mitsarionas | 2:b7489c070d1f | 86 | info.func = func; |
| mitsarionas | 2:b7489c070d1f | 87 | info.args = args; |
| mitsarionas | 2:b7489c070d1f | 88 | Thread t(__thread_wrapper, &info, osPriorityNormal, stack_size); |
| mitsarionas | 0:00797f1ebe04 | 89 | while (t.get_state() != Thread::Inactive) { |
| mitsarionas | 0:00797f1ebe04 | 90 | // printf("yielding... %d\n", t.get_state()); |
| mitsarionas | 0:00797f1ebe04 | 91 | // Thread::yield(); |
| mitsarionas | 0:00797f1ebe04 | 92 | // Thread::wait(1000); |
| mitsarionas | 0:00797f1ebe04 | 93 | } |
| mitsarionas | 2:b7489c070d1f | 94 | return info.result; |
| mitsarionas | 0:00797f1ebe04 | 95 | } |
| mitsarionas | 0:00797f1ebe04 | 96 | |
| mitsarionas | 0:00797f1ebe04 | 97 | int __get_tokens(pairing_context *ctx) |
| mitsarionas | 0:00797f1ebe04 | 98 | { |
| mitsarionas | 0:00797f1ebe04 | 99 | strcpy(url, ctx->postUrl); |
| mitsarionas | 0:00797f1ebe04 | 100 | strcat(url, "/gettokens"); |
| mitsarionas | 1:c5abc450140c | 101 | http.dumpReqHeader(true); |
| mitsarionas | 1:c5abc450140c | 102 | http.dumpResHeader(true); |
| mitsarionas | 0:00797f1ebe04 | 103 | int ret; |
| mitsarionas | 0:00797f1ebe04 | 104 | |
| mitsarionas | 0:00797f1ebe04 | 105 | //GET data |
| mitsarionas | 0:00797f1ebe04 | 106 | // printf("\nTrying to fetch page...\n"); |
| mitsarionas | 0:00797f1ebe04 | 107 | // ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); |
| mitsarionas | 0:00797f1ebe04 | 108 | // if (!ret) |
| mitsarionas | 0:00797f1ebe04 | 109 | // { |
| mitsarionas | 0:00797f1ebe04 | 110 | // printf("Page fetched successfully - read %d characters\n", strlen(str)); |
| mitsarionas | 0:00797f1ebe04 | 111 | // printf("Result: %s\n", str); |
| mitsarionas | 0:00797f1ebe04 | 112 | // } |
| mitsarionas | 0:00797f1ebe04 | 113 | // else |
| mitsarionas | 0:00797f1ebe04 | 114 | // { |
| mitsarionas | 0:00797f1ebe04 | 115 | // printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
| mitsarionas | 0:00797f1ebe04 | 116 | // } |
| mitsarionas | 0:00797f1ebe04 | 117 | |
| mitsarionas | 0:00797f1ebe04 | 118 | //POST data |
| mitsarionas | 0:00797f1ebe04 | 119 | HTTPMap map; |
| mitsarionas | 0:00797f1ebe04 | 120 | HTTPText inText(str, 512); |
| mitsarionas | 0:00797f1ebe04 | 121 | map.put("name", ctx->name); |
| mitsarionas | 0:00797f1ebe04 | 122 | map.put("uuid", ctx->uuid); |
| mitsarionas | 0:00797f1ebe04 | 123 | printf("\nTrying to post data...\n"); |
| mitsarionas | 0:00797f1ebe04 | 124 | ret = http.post(url, map, &inText); |
| mitsarionas | 1:c5abc450140c | 125 | Yodiwo_Plegma_PairingServerTokensResponse_t tokens; |
| mitsarionas | 0:00797f1ebe04 | 126 | if (!ret) |
| mitsarionas | 0:00797f1ebe04 | 127 | { |
| mitsarionas | 1:c5abc450140c | 128 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
| mitsarionas | 1:c5abc450140c | 129 | printf("Result: %s\n", str); |
| mitsarionas | 1:c5abc450140c | 130 | int jret = Yodiwo_Plegma_PairingServerTokensResponse_FromJson(str, strlen(str), &tokens); |
| mitsarionas | 1:c5abc450140c | 131 | if (jret == Yodiwo_JsonSuccessParse) { |
| mitsarionas | 1:c5abc450140c | 132 | ctx->token1 = tokens.token1; |
| mitsarionas | 1:c5abc450140c | 133 | ctx->token2 = tokens.token2; |
| mitsarionas | 1:c5abc450140c | 134 | return 0; |
| mitsarionas | 1:c5abc450140c | 135 | } else { |
| mitsarionas | 1:c5abc450140c | 136 | printf("error parsing response"); |
| mitsarionas | 1:c5abc450140c | 137 | return -2; |
| mitsarionas | 1:c5abc450140c | 138 | } |
| mitsarionas | 0:00797f1ebe04 | 139 | } |
| mitsarionas | 0:00797f1ebe04 | 140 | else |
| mitsarionas | 0:00797f1ebe04 | 141 | { |
| mitsarionas | 0:00797f1ebe04 | 142 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
| mitsarionas | 0:00797f1ebe04 | 143 | } |
| mitsarionas | 0:00797f1ebe04 | 144 | return ret; |
| mitsarionas | 0:00797f1ebe04 | 145 | |
| mitsarionas | 0:00797f1ebe04 | 146 | } |
| mitsarionas | 1:c5abc450140c | 147 | |
| mitsarionas | 2:b7489c070d1f | 148 | |
| mitsarionas | 2:b7489c070d1f | 149 | int __get_keys(pairing_context *ctx) |
| mitsarionas | 2:b7489c070d1f | 150 | { |
| mitsarionas | 2:b7489c070d1f | 151 | strcpy(url, ctx->postUrl); |
| mitsarionas | 2:b7489c070d1f | 152 | strcat(url, "/getkeys"); |
| mitsarionas | 2:b7489c070d1f | 153 | http.dumpReqHeader(true); |
| mitsarionas | 2:b7489c070d1f | 154 | http.dumpResHeader(true); |
| mitsarionas | 2:b7489c070d1f | 155 | int ret; |
| mitsarionas | 2:b7489c070d1f | 156 | |
| mitsarionas | 2:b7489c070d1f | 157 | //POST data |
| mitsarionas | 2:b7489c070d1f | 158 | HTTPMap map; |
| mitsarionas | 2:b7489c070d1f | 159 | HTTPText inText(str, 512); |
| mitsarionas | 2:b7489c070d1f | 160 | map.put("uuid", ctx->uuid); |
| mitsarionas | 2:b7489c070d1f | 161 | map.put("token1", ctx->token1); |
| mitsarionas | 2:b7489c070d1f | 162 | printf("\nTrying to post data...\n"); |
| mitsarionas | 2:b7489c070d1f | 163 | ret = http.post(url, map, &inText); |
| mitsarionas | 2:b7489c070d1f | 164 | Yodiwo_Plegma_PairingServerKeysResponse_t keys; |
| mitsarionas | 2:b7489c070d1f | 165 | if (!ret) |
| mitsarionas | 2:b7489c070d1f | 166 | { |
| mitsarionas | 2:b7489c070d1f | 167 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
| mitsarionas | 2:b7489c070d1f | 168 | printf("Result: %s\n", str); |
| mitsarionas | 2:b7489c070d1f | 169 | int jret = Yodiwo_Plegma_PairingServerKeysResponse_FromJson(str, strlen(str), &keys); |
| mitsarionas | 2:b7489c070d1f | 170 | if (jret == Yodiwo_JsonSuccessParse) { |
| mitsarionas | 2:b7489c070d1f | 171 | ctx->nodeKey = keys.nodeKey; |
| mitsarionas | 2:b7489c070d1f | 172 | ctx->secretKey = keys.secretKey; |
| mitsarionas | 2:b7489c070d1f | 173 | return 0; |
| mitsarionas | 2:b7489c070d1f | 174 | } else { |
| mitsarionas | 2:b7489c070d1f | 175 | printf("error parsing response"); |
| mitsarionas | 2:b7489c070d1f | 176 | return -2; |
| mitsarionas | 2:b7489c070d1f | 177 | } |
| mitsarionas | 2:b7489c070d1f | 178 | } |
| mitsarionas | 2:b7489c070d1f | 179 | else |
| mitsarionas | 2:b7489c070d1f | 180 | { |
| mitsarionas | 2:b7489c070d1f | 181 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
| mitsarionas | 2:b7489c070d1f | 182 | } |
| mitsarionas | 2:b7489c070d1f | 183 | return ret; |
| mitsarionas | 2:b7489c070d1f | 184 | |
| mitsarionas | 2:b7489c070d1f | 185 | } |