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: mbed-http lr1110 sx12xx_hal
combain.cpp
00001 #include "main.h" 00002 #if (GEOLOCATION_PROVIDER == COMBAIN) 00003 #include "radio.h" 00004 00005 char my_copy[128]; 00006 00007 const char COMBAIN_SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n" 00008 "MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" 00009 "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" 00010 "DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" 00011 "PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" 00012 "Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" 00013 "AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" 00014 "rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" 00015 "OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" 00016 "xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" 00017 "7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" 00018 "aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" 00019 "HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" 00020 "SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" 00021 "ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" 00022 "AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" 00023 "R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" 00024 "JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" 00025 "Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" 00026 "-----END CERTIFICATE-----\n"; 00027 00028 /* parse_json: get result sent from server */ 00029 void parse_json(const char *body, float *lat, float *lng, int *accuracy) 00030 { 00031 bool in_location = false; 00032 bool get_lat = false; 00033 bool get_lng = false; 00034 bool get_accuracy = false; 00035 unsigned n; 00036 strncpy(my_copy, body, sizeof(my_copy)); 00037 strtok(my_copy, "\""); 00038 for (n = 0; n < 10; n++) { 00039 char *t = strtok(NULL, "\":"); 00040 if (!t) 00041 break; 00042 if (in_location) { 00043 if (get_lat) { 00044 sscanf(t, "%f", lat); 00045 get_lat = false; 00046 } else if (get_lng) { 00047 sscanf(t, "%f", lng); 00048 get_lng = false; 00049 } 00050 if (strchr(t, '}')) 00051 in_location = false; 00052 else if (strcmp(t, "lat") == 0) 00053 get_lat = true; 00054 else if (strcmp(t, "lng") == 0) 00055 get_lng = true; 00056 } else { 00057 if (get_accuracy) { 00058 sscanf(t, "%d", accuracy); 00059 get_accuracy = false; 00060 } 00061 if (strcmp(t, "location") == 0) 00062 in_location = true; 00063 else if (strcmp(t, "accuracy") == 0) 00064 get_accuracy = true; 00065 } 00066 } 00067 } 00068 00069 int post_scan_result(const char *body, float *lat, float *lng, int *accuracy) 00070 { 00071 HttpsRequest* post_req = new HttpsRequest(network, COMBAIN_SSL_CA_PEM, HTTP_POST, "https://apiv2.combain.com?key=YOUR_API_KEY"); 00072 00073 post_req->set_header("Content-Type", "application/json"); 00074 00075 HttpResponse* post_res = post_req->send(body, strlen(body)); 00076 if (!post_res) { 00077 printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00078 return -1; 00079 } 00080 00081 dump_response(post_res); 00082 parse_json(post_res->get_body_as_string().c_str(), lat, lng, accuracy); 00083 delete post_req; 00084 00085 return 0; 00086 } 00087 00088 /* 00089 * https://combain.com/api/#request-body 00090 */ 00091 00092 void wifi_result_to_json(bool first, const uint8_t *result, unsigned macStart, unsigned rssi_idx) 00093 { 00094 char str[8]; 00095 unsigned i; 00096 00097 if (!first) 00098 strcat(json, ","); // end previous wifiAccessPoint 00099 00100 strcat(json, "{\"macAddress\": \""); 00101 for (i = 0; i < 6; i++) { 00102 sprintf(str, "%02x", result[i + macStart]); 00103 strcat(json, str); 00104 if (i < 5) 00105 strcat(json, ":"); 00106 } 00107 strcat(json, "\",\"signalStrength\": "); 00108 sprintf(str, "%d", (int8_t)result[rssi_idx]); 00109 strcat(json, str); 00110 /* TODO: combain takes channel and/or frequency of access point */ 00111 00112 strcat(json, "}"); 00113 } 00114 00115 void json_start() 00116 { 00117 strcpy(json, "{\"considerIp\": \"false\","); 00118 strcat(json, "\"wifiAccessPoints\": ["); 00119 } 00120 00121 void json_end() 00122 { 00123 strcat(json, "]"); 00124 strcat(json, "}"); 00125 } 00126 00127 #endif /* GEOLOCATION_PROVIDER == COMBAIN */
Generated on Fri Jul 15 2022 20:32:49 by
1.7.2