Example node for Yodiwo's Plegma API

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

Revision:
5:1ef168357347
Parent:
2:b7489c070d1f
--- a/pairing_http_handler.cpp	Mon Sep 21 06:56:11 2015 +0000
+++ b/pairing_http_handler.cpp	Mon Sep 28 08:55:29 2015 +0000
@@ -9,11 +9,16 @@
 onPaired_callback pairing_done_cb;
 EthernetInterface *interface;
 void pairing_handler (int id);
+int server_port; //TODO: get this from HTTPD?
 
-int start_pairing_http_server(EthernetInterface *eth, int port, onPaired_callback onPaired)
+
+int startpairing(int id);
+int next(int id);
+
+int start_pairing_http_server(void *ifdata, int port, onPaired_callback onPaired)
 {
+    interface = (EthernetInterface *)ifdata;
     pairing_done_cb = onPaired;
-    interface = eth;
     httpd = new HTTPD;
 //    httpd->attach("/cgi-bin/", &callback_cgi);
 //    httpd->attach("/ws/", &callback_ws);
@@ -24,56 +29,60 @@
 
 void pairing_handler (int id)
 {
-    int i, n;
-    int ret;
-    char buf[256];
-    char backUrl[100];
-    
     if (!strcmp(httpd->getFilename(id), "startpairing")) {
-        printf("starting pairing...\n");
-        pairing_context_init_from_config(&pairing_state, pairing_done_cb);
-        pairing_get_tokens(&pairing_state);
-        
-        int idx = 0;
-        i = 0;
-        sprintf(backUrl, "http://%s", interface->getIPAddress());
-        char *uri = httpd->getUri(id);
-        printf("1st url part: %s\n",backUrl);
-        while (uri[i] != '\0') {
-            if (uri[i] == '/') {
-                idx = i;
-            }
-            i++;
+        if (!startpairing(id)) {
+            return;
         }
-        strncat(backUrl, uri, idx + 1);
-        printf("more of url: %s\n", backUrl);
-        char *redirect = get_server_phase2_url(&pairing_state, backUrl);
-        printf("redirect url: %s\n", redirect);
-        if (redirect) {
-            httpd->redirect(id, 302, redirect, NULL, 0, NULL);
-//            free(redirect);
+    } else if (!strcmp(httpd->getFilename(id), "next")) {
+        if (!next(id)) {
             return;
         }
 
-    } else if (!strcmp(httpd->getFilename(id), "next")) {
-        ret = pairing_get_keys(&pairing_state);
-        pairing_done_cb(pairing_state.nodeKey, pairing_state.secretKey);
-        sprintf(buf, "OK\r\nnodeKey: %s\r\nsecretKey: %s\r\n", pairing_state.nodeKey, pairing_state.secretKey);
-        httpd->send(id, buf, strlen(buf), "Content-Type: text/plain\r\n");
     } else {
         httpd->httpdError(id, 404);
     }
-    strcpy(buf, httpd->getFilename(id));
-    strcat(buf, "\r\n");
-    strcat(buf, httpd->getQueryString(id));
-    strcat(buf, "\r\n");
-    n = strlen(buf);
- 
-    i = httpd->receive(id, &buf[n], sizeof(buf) - n);
-    if (i < 0) return;
-    i += n;
-    buf[i] = 0;
- 
-    printf("CGI %d %s\r\n", id, buf);
-//    httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
-}
\ No newline at end of file
+    httpd->httpdError(id, 400);
+}
+
+int startpairing(int id)
+{
+    char urlBase[100];
+    int i;
+    printf("starting pairing...\n");
+    pairing_context_init_from_config(&pairing_state, pairing_done_cb);
+    pairing_get_tokens(&pairing_state);
+    
+    int idx = 0;
+    i = 0;
+    char *uri = httpd->getUri(id);
+    while (uri[i] != '\0') {
+        if (uri[i] == '/') {
+            idx = i;
+        }
+        i++;
+    }
+    urlBase[0] = '\0';
+    strncat(urlBase, uri, idx + 1);
+    char *redirect = get_server_phase2_url(&pairing_state, interface->getIPAddress(), server_port, urlBase);
+    printf("redirect url: %s\n", redirect);
+    if (redirect) {
+        httpd->redirect(id, 302, redirect, NULL, 0, NULL);
+        free(redirect);
+        return 0;
+    }
+    return -1;
+}
+
+int next(int id)
+{
+    char buf[300];
+    int ret;
+    ret = pairing_get_keys(&pairing_state);
+    if (ret) {
+        return ret;
+    }
+    pairing_done_cb(pairing_state.nodeKey, pairing_state.secretKey);
+    sprintf(buf, "OK\r\nnodeKey: %s\r\nsecretKey: %s\r\n", pairing_state.nodeKey, pairing_state.secretKey);
+    httpd->send(id, buf, strlen(buf), "Content-Type: text/plain\r\n");
+    return 0;
+}