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 mbed-rtos mbed nsdl rgb_sensor_buffer
Fork of mbed_nsdl by
Diff: resources/beacon_resource.cpp
- Revision:
- 9:9d5b0c43579b
- Child:
- 10:9c11a8889d62
diff -r 27e94f52810b -r 9d5b0c43579b resources/beacon_resource.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/beacon_resource.cpp Wed Oct 29 18:28:59 2014 +0000
@@ -0,0 +1,129 @@
+#include <ctype.h>
+#include "mbed.h"
+#include "rtos.h"
+#include "MODSERIAL.h"
+#include "nsdl_support.h"
+#include "beacon_resource.h"
+#include "payload.h"
+
+#define RESOURCE_ID "sensor/beacon"
+
+extern MODSERIAL console;
+
+/* stored data for observable resource */
+static uint8_t obs_number = 0;
+static uint8_t *obs_token_ptr = NULL;
+static uint8_t obs_token_len = 0;
+
+static MODSERIAL beacon(p9, p10);
+
+#define UUID_LEN (36)
+
+static const char *UUID = "3AD81C9B-8B1A-494D-B0C3-C1B6DC87A2A8";
+
+//
+// A BLE beacon advertisement record
+typedef struct {
+ char uuid[UUID_LEN + 1];
+ int rssi;
+ int calibration;
+ int major;
+ int minor;
+ int channel;
+} beacon_record_t;
+
+static beacon_record_t record = {0};
+
+// Encode the beacon record as JSON.
+void encode_beacon_record(const beacon_record_t *record, char *json, int *json_length) {
+ static const char *format = "{'r':%d, 'c':%d,'m':%d,'n':%d,'ch':%d}";
+ *json_length = sprintf(json, format, record->rssi, record->calibration, record->major, record->minor, record->channel);
+}
+
+
+// read next character from UART
+static inline int next_char() {
+ while (!beacon.readable()) {
+ __WFE();
+ }
+ return beacon.getc();
+}
+
+// Given that we have consumed the begin record character ('>'), parse
+// characters until a comma is met, and confirm this is the UUID we are
+// looking for.
+//
+// Returns true if the read UUID matches, or false otherwise.
+//
+static bool parse_uuid(char *uuid) {
+ int chars_read = 0;
+
+ int ch = next_char();
+ while (isspace(ch)) {
+ ch = next_char();
+ }
+
+ do {
+ uuid[chars_read++] = ch;
+ } while ((ch = next_char()) != ',' && chars_read <= UUID_LEN);
+
+ uuid[chars_read] = '\0';
+
+ return chars_read == UUID_LEN && memcmp(uuid, UUID, UUID_LEN) == 0;
+}
+
+// Parse an integer using the given radix.
+static void parse_int(int *value, int radix) {
+ static const int BUF_SIZE = 16;
+ char buf[BUF_SIZE] = {0};
+ int chars_read = 0;
+ int ch = next_char();
+
+ do {
+ buf[chars_read++] = ch;
+ } while ((ch = next_char()) != ',' && chars_read <= BUF_SIZE);
+
+ *value = strtol(buf, NULL, radix);
+}
+
+static void exec_call_thread(void const *args)
+{
+ while(1) {
+ int ch = next_char();
+ if (ch == '>') {
+ if (!parse_uuid(record.uuid)) {
+ continue;
+ }
+
+ parse_int(&record.rssi, 10);
+ parse_int(&record.calibration, 10);
+ parse_int(&record.major, 16);
+ parse_int(&record.minor, 16);
+ parse_int(&record.channel, 10);
+
+ console.printf("Parsed record: [uuid:%s, rssi:%d, calibration:%d, major:%d, minor:%d, channel: %d]\r\n", record.uuid, record.rssi, record.calibration, record.major, record.minor, record.channel);
+
+ if (obs_number == 0 && obs_token_ptr == NULL)
+ console.printf("ERROR: obs_number: %d, obs_token_ptr: %0x%08x\r\n", obs_number, obs_token_ptr);
+
+ obs_number++;
+
+ static const int JSON_BUFFER_SIZE = 1024;
+ char json[JSON_BUFFER_SIZE] = {0};
+ int json_length = 0;
+ encode_beacon_record(&record, json, &json_length);
+
+ int result = sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t *)json, json_length, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
+ console.printf("Sent to mDS: '%s', result: %d\r\n", json, result);
+ }
+ }
+}
+
+int create_beacon_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+ beacon.baud(115200);
+ static Thread exec_thread(exec_call_thread);
+ //nsdl_create_dynamic_resource(resource_ptr, sizeof(RESOURCE_ID) - 1, (uint8_t*)RESOURCE_ID, 0, 0, 1, &resource_cb, SN_GRS_GET_ALLOWED);
+ console.printf("Created beacon resource\r\n");
+ return 0;
+}
