Yodiwo / Mbed 2 deprecated YoPlegma

Dependencies:   EthernetInterface FXOS8700Q HTTPClient HTTPD MQTTS SDFileSystem YodiwoPlegma mbed-rpc mbed-rtos mbed wolfSSL

pairing_backend.cpp

Committer:
mitsarionas
Date:
2015-09-01
Revision:
0:00797f1ebe04
Child:
1:c5abc450140c

File content as of revision 0:00797f1ebe04:

#include "pairing_backend.h"
#include "HTTPClient.h"
#include "mbed.h"
#include "rtos.h"

HTTPClient http;
char recvBuff[1024*20];
int __get_tokens(pairing_context *ctx);
 
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;
}

#define STACK_SIZE 24000

void get_tokens_thread(void const *arg);

int start_pairing(pairing_context *ctx)
{
    printf("getting tokens from server\n");
    Thread t(get_tokens_thread, ctx, 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);    
}
    
int __get_tokens(pairing_context *ctx)
{

    strcpy(url, ctx->postUrl);
    strcat(url, "/gettokens");
//    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);
    if (!ret)
    {
      printf("Executed POST 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());
    }
    return ret;
    
}